Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/x509_vpm.c

raw
1 // Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 2004.
2 // Copyright (c) 2004 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <string.h>
6
7 #include <openssl/mem.h>
8 #include <openssl/obj.h>
9 #include <openssl/stack.h>
10 #include <openssl/x509.h>
11
12 #include "../internal.h"
13 #include "internal.h"
14
15
16 // X509_VERIFY_PARAM functions
17
18 #define SET_HOST 0
19 #define ADD_HOST 1
20
21 static void str_free(char *s) { OPENSSL_free(s); }
22
23 static int int_x509_param_set_hosts(X509_VERIFY_PARAM *param, int mode,
24 const char *name, size_t namelen) {
25 char *copy;
26
27 // Setting 0 to automatically detect the length of |name| is an OpenSSL quirk
28 // that AWS-LC isn't keen on supporting. However, consumers often assume
29 // OpenSSL semantics from AWS-LC, so it's supported in this case.
30 if (name != NULL && namelen == 0) {
31 namelen = strlen(name);
32 }
33
34 // Refuse names with embedded NUL bytes.
35 // XXX: Do we need to push an error onto the error stack?
36 if (name && OPENSSL_memchr(name, '\0', namelen)) {
37 return 0;
38 }
39
40 if (mode == SET_HOST && param->hosts) {
41 sk_OPENSSL_STRING_pop_free(param->hosts, str_free);
42 param->hosts = NULL;
43 }
44 // OpenSSL returns 1 when trying to set or add an empty name. This is also a
45 // quirk that AWS-LC isn't keen on supporting, but we maintain for backwards
46 // compatibility.
47 if (name == NULL || namelen == 0) {
48 return 1;
49 }
50
51 copy = OPENSSL_strndup(name, namelen);
52 if (copy == NULL) {
53 return 0;
54 }
55
56 if (param->hosts == NULL &&
57 (param->hosts = sk_OPENSSL_STRING_new_null()) == NULL) {
58 OPENSSL_free(copy);
59 return 0;
60 }
61
62 if (!sk_OPENSSL_STRING_push(param->hosts, copy)) {
63 OPENSSL_free(copy);
64 if (sk_OPENSSL_STRING_num(param->hosts) == 0) {
65 sk_OPENSSL_STRING_free(param->hosts);
66 param->hosts = NULL;
67 }
68 return 0;
69 }
70
71 return 1;
72 }
73
74 X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void) {
75 X509_VERIFY_PARAM *param = OPENSSL_zalloc(sizeof(X509_VERIFY_PARAM));
76 if (!param) {
77 return NULL;
78 }
79 param->depth = -1;
80 return param;
81 }
82
83 void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param) {
84 if (param == NULL) {
85 return;
86 }
87 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
88 sk_OPENSSL_STRING_pop_free(param->hosts, str_free);
89 OPENSSL_free(param->email);
90 OPENSSL_free(param->ip);
91 OPENSSL_free(param);
92 }
93
94 static int should_copy(int dest_is_set, int src_is_set, int prefer_src) {
95 if (prefer_src) {
96 // We prefer the source, so as long as there is a value to copy, copy it.
97 return src_is_set;
98 }
99
100 // We prefer the destination, so only copy if the destination is unset.
101 return src_is_set && !dest_is_set;
102 }
103
104 static void copy_int_param(int *dest, const int *src, int default_val,
105 int prefer_src) {
106 if (should_copy(*dest != default_val, *src != default_val, prefer_src)) {
107 *dest = *src;
108 }
109 }
110
111 // x509_verify_param_copy copies fields from |src| to |dest|. If both |src| and
112 // |dest| have some field set, |prefer_src| determines whether |src| or |dest|'s
113 // version is used.
114 static int x509_verify_param_copy(X509_VERIFY_PARAM *dest,
115 const X509_VERIFY_PARAM *src,
116 int prefer_src) {
117 if (src == NULL) {
118 return 1;
119 }
120
121 copy_int_param(&dest->purpose, &src->purpose, /*default_val=*/0, prefer_src);
122 copy_int_param(&dest->trust, &src->trust, /*default_val=*/0, prefer_src);
123 copy_int_param(&dest->depth, &src->depth, /*default_val=*/-1, prefer_src);
124
125 // |check_time|, unlike all other parameters, does not honor |prefer_src|.
126 // This means |X509_VERIFY_PARAM_set1| will not overwrite it. This behavior
127 // comes from OpenSSL but may have been a bug.
128 if (!(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
129 dest->check_time = src->check_time;
130 // The source |X509_V_FLAG_USE_CHECK_TIME| flag, if set, is copied below.
131 }
132
133 dest->flags |= src->flags;
134 dest->awslc_flags |= src->awslc_flags;
135
136 if (should_copy(dest->policies != NULL, src->policies != NULL, prefer_src)) {
137 if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies)) {
138 return 0;
139 }
140 }
141
142 if (should_copy(dest->hosts != NULL, src->hosts != NULL, prefer_src)) {
143 sk_OPENSSL_STRING_pop_free(dest->hosts, str_free);
144 dest->hosts = NULL;
145 if (src->hosts) {
146 dest->hosts =
147 sk_OPENSSL_STRING_deep_copy(src->hosts, OPENSSL_strdup, str_free);
148 if (dest->hosts == NULL) {
149 return 0;
150 }
151 // Copy the host flags if and only if we're copying the host list. Note
152 // this means mechanisms like |X509_STORE_CTX_set_default| cannot be used
153 // to set host flags. E.g. we cannot change the defaults using
154 // |kDefaultParam| below.
155 dest->hostflags = src->hostflags;
156 }
157 }
158
159 if (should_copy(dest->email != NULL, src->email != NULL, prefer_src)) {
160 if (!X509_VERIFY_PARAM_set1_email(dest, src->email, src->emaillen)) {
161 return 0;
162 }
163 }
164
165 if (should_copy(dest->ip != NULL, src->ip != NULL, prefer_src)) {
166 if (!X509_VERIFY_PARAM_set1_ip(dest, src->ip, src->iplen)) {
167 return 0;
168 }
169 }
170
171 dest->poison = src->poison;
172 return 1;
173 }
174
175 int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
176 const X509_VERIFY_PARAM *src) {
177 // Prefer the destination. That is, this function only changes unset
178 // parameters in |dest|.
179 return x509_verify_param_copy(dest, src, /*prefer_src=*/0);
180 }
181
182 int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
183 const X509_VERIFY_PARAM *from) {
184 // Prefer the source. That is, values in |to| are only preserved if they were
185 // unset in |from|.
186 return x509_verify_param_copy(to, from, /*prefer_src=*/1);
187 }
188
189 static int int_x509_param_set1_email(char **pdest, size_t *pdestlen,
190 const char *src, size_t srclen) {
191 void *tmp;
192 if (src != NULL) {
193 // Setting |srclen| to 0 to automatically detect the length of |src| is an
194 // OpenSSL quirk that AWS-LC isn't keen on supporting. However, consumers
195 // often assume OpenSSL semantics from AWS-LC, so it's supported in this
196 // case.
197 if (srclen == 0) {
198 srclen = strlen(src);
199 }
200
201 tmp = OPENSSL_strndup(src, srclen);
202 if (tmp == NULL) {
203 return 0;
204 }
205 } else {
206 // This allows an empty string to disable previously configured checks.
207 // This is an OpenSSL quirk that AWS-LC isn't keen on supporting. However,
208 // consumers often assume OpenSSL semantics from AWS-LC, so it's supported
209 // in this case.
210 tmp = NULL;
211 srclen = 0;
212 }
213
214 if (*pdest != NULL) {
215 OPENSSL_free(*pdest);
216 }
217 *pdest = tmp;
218 if (pdestlen != NULL) {
219 *pdestlen = srclen;
220 }
221 return 1;
222 }
223
224 // IP addresses work slightly differently, so we use another function to
225 // differentiate from emails. |X509_VERIFY_PARAM_set1_ip| takes a const
226 // unsigned char*, instead of a const char*, so the same strlen logic that was
227 // being used is not quite suitable here.
228 // We keep the original behavior that BoringSSL left, but only for IP addresses.
229 // We can align the behavior with |int_x509_param_set1_email| like OpenSSL has
230 // been doing if needed.
231 static int int_x509_param_set1_ip(unsigned char **pdest, size_t *pdestlen,
232 const unsigned char *src, size_t srclen) {
233 void *tmp;
234 if (src == NULL || srclen == 0) {
235 // Unlike OpenSSL, we do not allow an empty string to disable previously
236 // configured checks.
237 return 0;
238 }
239
240 tmp = OPENSSL_memdup(src, srclen);
241 if (!tmp) {
242 return 0;
243 }
244
245 if (*pdest) {
246 OPENSSL_free(*pdest);
247 }
248 *pdest = tmp;
249 if (pdestlen) {
250 *pdestlen = srclen;
251 }
252 return 1;
253 }
254
255 int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags) {
256 param->flags |= flags;
257 if (flags & X509_V_FLAG_POLICY_MASK) {
258 param->flags |= X509_V_FLAG_POLICY_CHECK;
259 }
260 return 1;
261 }
262
263 int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
264 unsigned long flags) {
265 param->flags &= ~flags;
266 return 1;
267 }
268
269 unsigned long X509_VERIFY_PARAM_get_flags(const X509_VERIFY_PARAM *param) {
270 return param->flags;
271 }
272
273 int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose) {
274 return X509_PURPOSE_set(&param->purpose, purpose);
275 }
276
277 int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust) {
278 return X509_TRUST_set(&param->trust, trust);
279 }
280
281 void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth) {
282 param->depth = depth;
283 }
284
285 void X509_VERIFY_PARAM_set_time_posix(X509_VERIFY_PARAM *param, int64_t t) {
286 param->check_time = t;
287 param->flags |= X509_V_FLAG_USE_CHECK_TIME;
288 }
289
290 void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t) {
291 X509_VERIFY_PARAM_set_time_posix(param, t);
292 }
293
294 int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
295 ASN1_OBJECT *policy) {
296 if (!param->policies) {
297 param->policies = sk_ASN1_OBJECT_new_null();
298 if (!param->policies) {
299 return 0;
300 }
301 }
302 if (!sk_ASN1_OBJECT_push(param->policies, policy)) {
303 return 0;
304 }
305 // TODO(davidben): This does not set |X509_V_FLAG_POLICY_CHECK|, while
306 // |X509_VERIFY_PARAM_set1_policies| does. Is this a bug?
307 return 1;
308 }
309
310 int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
311 const STACK_OF(ASN1_OBJECT) *policies) {
312 if (!param) {
313 return 0;
314 }
315
316 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
317 if (!policies) {
318 param->policies = NULL;
319 return 1;
320 }
321
322 param->policies =
323 sk_ASN1_OBJECT_deep_copy(policies, OBJ_dup, ASN1_OBJECT_free);
324 if (!param->policies) {
325 return 0;
326 }
327
328 param->flags |= X509_V_FLAG_POLICY_CHECK;
329 return 1;
330 }
331
332 int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param, const char *name,
333 size_t namelen) {
334 if (!int_x509_param_set_hosts(param, SET_HOST, name, namelen)) {
335 param->poison = 1;
336 return 0;
337 }
338 return 1;
339 }
340
341 int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param, const char *name,
342 size_t namelen) {
343 if (!int_x509_param_set_hosts(param, ADD_HOST, name, namelen)) {
344 param->poison = 1;
345 return 0;
346 }
347 return 1;
348 }
349
350 void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
351 unsigned int flags) {
352 param->hostflags = flags;
353 }
354
355 unsigned int X509_VERIFY_PARAM_get_hostflags(const X509_VERIFY_PARAM *param) {
356 return param->hostflags;
357 }
358
359 int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param, const char *email,
360 size_t emaillen) {
361 if (OPENSSL_memchr(email, '\0', emaillen) != NULL ||
362 !int_x509_param_set1_email(&param->email, &param->emaillen, email,
363 emaillen)) {
364 param->poison = 1;
365 return 0;
366 }
367
368 return 1;
369 }
370
371 int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param, const unsigned char *ip,
372 size_t iplen) {
373 if ((iplen != 0 && iplen != 4 && iplen != 16) ||
374 !int_x509_param_set1_ip(&param->ip, &param->iplen, ip, iplen)) {
375 param->poison = 1;
376 return 0;
377 }
378
379 return 1;
380 }
381
382 int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc) {
383 unsigned char ipout[16];
384 size_t iplen;
385
386 iplen = (size_t)x509v3_a2i_ipadd(ipout, ipasc);
387 if (iplen == 0) {
388 return 0;
389 }
390 return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
391 }
392
393 int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param) {
394 return param->depth;
395 }
396
397 #define vpm_empty_id NULL, 0U, NULL, 0, NULL, 0, 0
398
399 static const X509_VERIFY_PARAM kDefaultParam = {
400 .flags = X509_V_FLAG_TRUSTED_FIRST,
401 .depth = 100,
402 };
403
404 static const X509_VERIFY_PARAM kSMIMESignParam = {
405 .purpose = X509_PURPOSE_SMIME_SIGN,
406 .trust = X509_TRUST_EMAIL,
407 .depth = -1,
408 };
409
410 static const X509_VERIFY_PARAM kSSLClientParam = {
411 .purpose = X509_PURPOSE_SSL_CLIENT,
412 .trust = X509_TRUST_SSL_CLIENT,
413 .depth = -1,
414 };
415
416 static const X509_VERIFY_PARAM kSSLServerParam = {
417 .purpose = X509_PURPOSE_SSL_SERVER,
418 .trust = X509_TRUST_SSL_SERVER,
419 .depth = -1,
420 };
421
422 const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name) {
423 if (strcmp(name, "default") == 0) {
424 return &kDefaultParam;
425 }
426 if (strcmp(name, "pkcs7") == 0) {
427 // PKCS#7 and S/MIME signing use the same defaults.
428 return &kSMIMESignParam;
429 }
430 if (strcmp(name, "smime_sign") == 0) {
431 return &kSMIMESignParam;
432 }
433 if (strcmp(name, "ssl_client") == 0) {
434 return &kSSLClientParam;
435 }
436 if (strcmp(name, "ssl_server") == 0) {
437 return &kSSLServerParam;
438 }
439 return NULL;
440 }
441
442 int X509_VERIFY_PARAM_enable_ec_key_explicit_params(X509_VERIFY_PARAM *param) {
443 GUARD_PTR(param);
444 param->awslc_flags |= AWSLC_V_ENABLE_EC_KEY_EXPLICIT_PARAMS;
445 return 1;
446 }
447
448 int X509_VERIFY_PARAM_disable_ec_key_explicit_params(X509_VERIFY_PARAM *param) {
449 GUARD_PTR(param);
450 param->awslc_flags &= ~AWSLC_V_ENABLE_EC_KEY_EXPLICIT_PARAMS;
451 return 1;
452 }
453