aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/security/credentials/credentials.h
blob: 85b3bc53503e28b1d9dd5da93cb64a85a6e98450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 *
 * Copyright 2015, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_CREDENTIALS_H
#define GRPC_CORE_LIB_SECURITY_CREDENTIALS_CREDENTIALS_H

#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/support/sync.h>
#include "src/core/lib/transport/metadata_batch.h"

#include "src/core/lib/http/httpcli.h"
#include "src/core/lib/http/parser.h"
#include "src/core/lib/iomgr/polling_entity.h"
#include "src/core/lib/security/transport/security_connector.h"

struct grpc_http_response;

/* --- Constants. --- */

typedef enum {
  GRPC_CREDENTIALS_OK = 0,
  GRPC_CREDENTIALS_ERROR
} grpc_credentials_status;

#define GRPC_FAKE_TRANSPORT_SECURITY_TYPE "fake"

#define GRPC_CHANNEL_CREDENTIALS_TYPE_SSL "Ssl"
#define GRPC_CHANNEL_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY \
  "FakeTransportSecurity"

#define GRPC_CALL_CREDENTIALS_TYPE_OAUTH2 "Oauth2"
#define GRPC_CALL_CREDENTIALS_TYPE_JWT "Jwt"
#define GRPC_CALL_CREDENTIALS_TYPE_IAM "Iam"
#define GRPC_CALL_CREDENTIALS_TYPE_COMPOSITE "Composite"

#define GRPC_AUTHORIZATION_METADATA_KEY "authorization"
#define GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY \
  "x-goog-iam-authorization-token"
#define GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY "x-goog-iam-authority-selector"

#define GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS 60

#define GRPC_COMPUTE_ENGINE_METADATA_HOST "metadata"
#define GRPC_COMPUTE_ENGINE_METADATA_TOKEN_PATH \
  "/computeMetadata/v1/instance/service-accounts/default/token"

#define GRPC_GOOGLE_OAUTH2_SERVICE_HOST "www.googleapis.com"
#define GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH "/oauth2/v3/token"

#define GRPC_SERVICE_ACCOUNT_POST_BODY_PREFIX                         \
  "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&" \
  "assertion="

#define GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING \
  "client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token"

/* --- Google utils --- */

/* It is the caller's responsibility to gpr_free the result if not NULL. */
char *grpc_get_well_known_google_credentials_file_path(void);

/* Implementation function for the different platforms. */
char *grpc_get_well_known_google_credentials_file_path_impl(void);

/* Override for testing only. Not thread-safe */
typedef char *(*grpc_well_known_credentials_path_getter)(void);
void grpc_override_well_known_credentials_path_getter(
    grpc_well_known_credentials_path_getter getter);

/* --- grpc_channel_credentials. --- */

typedef struct {
  void (*destruct)(grpc_channel_credentials *c);

  grpc_security_status (*create_security_connector)(
      grpc_channel_credentials *c, grpc_call_credentials *call_creds,
      const char *target, const grpc_channel_args *args,
      grpc_channel_security_connector **sc, grpc_channel_args **new_args);

  grpc_channel_credentials *(*duplicate_without_call_credentials)(
      grpc_channel_credentials *c);
} grpc_channel_credentials_vtable;

struct grpc_channel_credentials {
  const grpc_channel_credentials_vtable *vtable;
  const char *type;
  gpr_refcount refcount;
};

grpc_channel_credentials *grpc_channel_credentials_ref(
    grpc_channel_credentials *creds);
void grpc_channel_credentials_unref(grpc_channel_credentials *creds);

/* Creates a security connector for the channel. May also create new channel
   args for the channel to be used in place of the passed in const args if
   returned non NULL. In that case the caller is responsible for destroying
   new_args after channel creation. */
grpc_security_status grpc_channel_credentials_create_security_connector(
    grpc_channel_credentials *creds, const char *target,
    const grpc_channel_args *args, grpc_channel_security_connector **sc,
    grpc_channel_args **new_args);

/* Creates a version of the channel credentials without any attached call
   credentials. This can be used in order to open a channel to a non-trusted
   gRPC load balancer. */
grpc_channel_credentials *
grpc_channel_credentials_duplicate_without_call_credentials(
    grpc_channel_credentials *creds);

/* --- grpc_credentials_md. --- */

typedef struct {
  grpc_slice key;
  grpc_slice value;
} grpc_credentials_md;

typedef struct {
  grpc_credentials_md *entries;
  size_t num_entries;
  size_t allocated;
  gpr_refcount refcount;
} grpc_credentials_md_store;

grpc_credentials_md_store *grpc_credentials_md_store_create(
    size_t initial_capacity);

/* Will ref key and value. */
void grpc_credentials_md_store_add(grpc_credentials_md_store *store,
                                   grpc_slice key, grpc_slice value);
void grpc_credentials_md_store_add_cstrings(grpc_credentials_md_store *store,
                                            const char *key, const char *value);
grpc_credentials_md_store *grpc_credentials_md_store_ref(
    grpc_credentials_md_store *store);
void grpc_credentials_md_store_unref(grpc_credentials_md_store *store);

/* --- grpc_call_credentials. --- */

/* error_details must be NULL if status is GRPC_CREDENTIALS_OK. */
typedef void (*grpc_credentials_metadata_cb)(
    grpc_exec_ctx *exec_ctx, void *user_data, grpc_credentials_md *md_elems,
    size_t num_md, grpc_credentials_status status, const char *error_details);

typedef struct {
  void (*destruct)(grpc_call_credentials *c);
  void (*get_request_metadata)(grpc_exec_ctx *exec_ctx,
                               grpc_call_credentials *c,
                               grpc_polling_entity *pollent,
                               grpc_auth_metadata_context context,
                               grpc_credentials_metadata_cb cb,
                               void *user_data);
} grpc_call_credentials_vtable;

struct grpc_call_credentials {
  const grpc_call_credentials_vtable *vtable;
  const char *type;
  gpr_refcount refcount;
};

grpc_call_credentials *grpc_call_credentials_ref(grpc_call_credentials *creds);
void grpc_call_credentials_unref(grpc_call_credentials *creds);
void grpc_call_credentials_get_request_metadata(
    grpc_exec_ctx *exec_ctx, grpc_call_credentials *creds,
    grpc_polling_entity *pollent, grpc_auth_metadata_context context,
    grpc_credentials_metadata_cb cb, void *user_data);

/* Metadata-only credentials with the specified key and value where
   asynchronicity can be simulated for testing. */
grpc_call_credentials *grpc_md_only_test_credentials_create(
    const char *md_key, const char *md_value, int is_async);

/* --- grpc_server_credentials. --- */

typedef struct {
  void (*destruct)(grpc_server_credentials *c);
  grpc_security_status (*create_security_connector)(
      grpc_server_credentials *c, grpc_server_security_connector **sc);
} grpc_server_credentials_vtable;

struct grpc_server_credentials {
  const grpc_server_credentials_vtable *vtable;
  const char *type;
  gpr_refcount refcount;
  grpc_auth_metadata_processor processor;
};

grpc_security_status grpc_server_credentials_create_security_connector(
    grpc_server_credentials *creds, grpc_server_security_connector **sc);

grpc_server_credentials *grpc_server_credentials_ref(
    grpc_server_credentials *creds);

void grpc_server_credentials_unref(grpc_server_credentials *creds);

#define GRPC_SERVER_CREDENTIALS_ARG "grpc.server_credentials"

grpc_arg grpc_server_credentials_to_arg(grpc_server_credentials *c);
grpc_server_credentials *grpc_server_credentials_from_arg(const grpc_arg *arg);
grpc_server_credentials *grpc_find_server_credentials_in_args(
    const grpc_channel_args *args);

/* -- Credentials Metadata Request. -- */

typedef struct {
  grpc_call_credentials *creds;
  grpc_credentials_metadata_cb cb;
  grpc_http_response response;
  void *user_data;
} grpc_credentials_metadata_request;

grpc_credentials_metadata_request *grpc_credentials_metadata_request_create(
    grpc_call_credentials *creds, grpc_credentials_metadata_cb cb,
    void *user_data);

void grpc_credentials_metadata_request_destroy(
    grpc_credentials_metadata_request *r);

#endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_CREDENTIALS_H */