Debian Codegraph @

external/boringssl/crypto/x509/x509_vpm.c

raw
1 /*
2 * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
3 * 2004.
4 */
5 /* ====================================================================
6 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * [email protected].
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * ([email protected]). This product includes software written by Tim
55 * Hudson ([email protected]). */
56
57 #include <string.h>
58
59 #include <openssl/buf.h>
60 #include <openssl/lhash.h>
61 #include <openssl/mem.h>
62 #include <openssl/obj.h>
63 #include <openssl/stack.h>
64 #include <openssl/x509.h>
65 #include <openssl/x509v3.h>
66
67 #include "vpm_int.h"
68
69 /* X509_VERIFY_PARAM functions */
70
71 #define SET_HOST 0
72 #define ADD_HOST 1
73
74 static char *str_copy(char *s)
75 {
76 return OPENSSL_strdup(s);
77 }
78
79 static void str_free(char *s)
80 {
81 OPENSSL_free(s);
82 }
83
84 #define string_stack_free(sk) sk_OPENSSL_STRING_pop_free(sk, str_free)
85
86 static int int_x509_param_set_hosts(X509_VERIFY_PARAM_ID *id, int mode,
87 const char *name, size_t namelen)
88 {
89 char *copy;
90
91 /*
92 * Refuse names with embedded NUL bytes.
93 * XXX: Do we need to push an error onto the error stack?
94 */
95 if (name && memchr(name, '\0', namelen))
96 return 0;
97
98 if (mode == SET_HOST && id->hosts) {
99 string_stack_free(id->hosts);
100 id->hosts = NULL;
101 }
102 if (name == NULL)
103 return 1;
104
105 copy = BUF_strndup(name, namelen);
106 if (copy == NULL)
107 return 0;
108
109 if (id->hosts == NULL &&
110 (id->hosts = sk_OPENSSL_STRING_new_null()) == NULL) {
111 OPENSSL_free(copy);
112 return 0;
113 }
114
115 if (!sk_OPENSSL_STRING_push(id->hosts, copy)) {
116 OPENSSL_free(copy);
117 if (sk_OPENSSL_STRING_num(id->hosts) == 0) {
118 sk_OPENSSL_STRING_free(id->hosts);
119 id->hosts = NULL;
120 }
121 return 0;
122 }
123
124 return 1;
125 }
126
127 static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
128 {
129 X509_VERIFY_PARAM_ID *paramid;
130 if (!param)
131 return;
132 param->name = NULL;
133 param->purpose = 0;
134 param->trust = 0;
135 /*
136 * param->inh_flags = X509_VP_FLAG_DEFAULT;
137 */
138 param->inh_flags = 0;
139 param->flags = 0;
140 param->depth = -1;
141 if (param->policies) {
142 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
143 param->policies = NULL;
144 }
145 paramid = param->id;
146 if (paramid->hosts) {
147 string_stack_free(paramid->hosts);
148 paramid->hosts = NULL;
149 }
150 if (paramid->peername) {
151 OPENSSL_free(paramid->peername);
152 paramid->peername = NULL;
153 }
154 if (paramid->email) {
155 OPENSSL_free(paramid->email);
156 paramid->email = NULL;
157 paramid->emaillen = 0;
158 }
159 if (paramid->ip) {
160 OPENSSL_free(paramid->ip);
161 paramid->ip = NULL;
162 paramid->iplen = 0;
163 }
164
165 }
166
167 X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
168 {
169 X509_VERIFY_PARAM *param;
170 X509_VERIFY_PARAM_ID *paramid;
171 param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
172 if (!param)
173 return NULL;
174 paramid = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM_ID));
175 if (!paramid) {
176 OPENSSL_free(param);
177 return NULL;
178 }
179 memset(param, 0, sizeof(X509_VERIFY_PARAM));
180 memset(paramid, 0, sizeof(X509_VERIFY_PARAM_ID));
181 param->id = paramid;
182 x509_verify_param_zero(param);
183 return param;
184 }
185
186 void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
187 {
188 if (param == NULL)
189 return;
190 x509_verify_param_zero(param);
191 OPENSSL_free(param->id);
192 OPENSSL_free(param);
193 }
194
195 /*
196 * This function determines how parameters are "inherited" from one structure
197 * to another. There are several different ways this can happen. 1. If a
198 * child structure needs to have its values initialized from a parent they are
199 * simply copied across. For example SSL_CTX copied to SSL. 2. If the
200 * structure should take on values only if they are currently unset. For
201 * example the values in an SSL structure will take appropriate value for SSL
202 * servers or clients but only if the application has not set new ones. The
203 * "inh_flags" field determines how this function behaves. Normally any
204 * values which are set in the default are not copied from the destination and
205 * verify flags are ORed together. If X509_VP_FLAG_DEFAULT is set then
206 * anything set in the source is copied to the destination. Effectively the
207 * values in "to" become default values which will be used only if nothing new
208 * is set in "from". If X509_VP_FLAG_OVERWRITE is set then all value are
209 * copied across whether they are set or not. Flags is still Ored though. If
210 * X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead of
211 * ORed. If X509_VP_FLAG_LOCKED is set then no values are copied. If
212 * X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed after
213 * the next call.
214 */
215
216 /* Macro to test if a field should be copied from src to dest */
217
218 #define test_x509_verify_param_copy(field, def) \
219 (to_overwrite || \
220 ((src->field != def) && (to_default || (dest->field == def))))
221
222 /* As above but for ID fields */
223
224 #define test_x509_verify_param_copy_id(idf, def) \
225 test_x509_verify_param_copy(id->idf, def)
226
227 /* Macro to test and copy a field if necessary */
228
229 #define x509_verify_param_copy(field, def) \
230 if (test_x509_verify_param_copy(field, def)) \
231 dest->field = src->field
232
233 int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
234 const X509_VERIFY_PARAM *src)
235 {
236 unsigned long inh_flags;
237 int to_default, to_overwrite;
238 X509_VERIFY_PARAM_ID *id;
239 if (!src)
240 return 1;
241 id = src->id;
242 inh_flags = dest->inh_flags | src->inh_flags;
243
244 if (inh_flags & X509_VP_FLAG_ONCE)
245 dest->inh_flags = 0;
246
247 if (inh_flags & X509_VP_FLAG_LOCKED)
248 return 1;
249
250 if (inh_flags & X509_VP_FLAG_DEFAULT)
251 to_default = 1;
252 else
253 to_default = 0;
254
255 if (inh_flags & X509_VP_FLAG_OVERWRITE)
256 to_overwrite = 1;
257 else
258 to_overwrite = 0;
259
260 x509_verify_param_copy(purpose, 0);
261 x509_verify_param_copy(trust, 0);
262 x509_verify_param_copy(depth, -1);
263
264 /* If overwrite or check time not set, copy across */
265
266 if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
267 dest->check_time = src->check_time;
268 dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
269 /* Don't need to copy flag: that is done below */
270 }
271
272 if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
273 dest->flags = 0;
274
275 dest->flags |= src->flags;
276
277 if (test_x509_verify_param_copy(policies, NULL)) {
278 if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
279 return 0;
280 }
281
282 /* Copy the host flags if and only if we're copying the host list */
283 if (test_x509_verify_param_copy_id(hosts, NULL)) {
284 if (dest->id->hosts) {
285 string_stack_free(dest->id->hosts);
286 dest->id->hosts = NULL;
287 }
288 if (id->hosts) {
289 dest->id->hosts =
290 sk_OPENSSL_STRING_deep_copy(id->hosts, str_copy, str_free);
291 if (dest->id->hosts == NULL)
292 return 0;
293 dest->id->hostflags = id->hostflags;
294 }
295 }
296
297 if (test_x509_verify_param_copy_id(email, NULL)) {
298 if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen))
299 return 0;
300 }
301
302 if (test_x509_verify_param_copy_id(ip, NULL)) {
303 if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
304 return 0;
305 }
306
307 return 1;
308 }
309
310 int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
311 const X509_VERIFY_PARAM *from)
312 {
313 unsigned long save_flags = to->inh_flags;
314 int ret;
315 to->inh_flags |= X509_VP_FLAG_DEFAULT;
316 ret = X509_VERIFY_PARAM_inherit(to, from);
317 to->inh_flags = save_flags;
318 return ret;
319 }
320
321 static int int_x509_param_set1(char **pdest, size_t *pdestlen,
322 const char *src, size_t srclen)
323 {
324 void *tmp;
325 if (src) {
326 if (srclen == 0) {
327 tmp = BUF_strdup(src);
328 srclen = strlen(src);
329 } else
330 tmp = BUF_memdup(src, srclen);
331 if (!tmp)
332 return 0;
333 } else {
334 tmp = NULL;
335 srclen = 0;
336 }
337 if (*pdest)
338 OPENSSL_free(*pdest);
339 *pdest = tmp;
340 if (pdestlen)
341 *pdestlen = srclen;
342 return 1;
343 }
344
345 int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
346 {
347 if (param->name)
348 OPENSSL_free(param->name);
349 param->name = BUF_strdup(name);
350 if (param->name)
351 return 1;
352 return 0;
353 }
354
355 int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
356 {
357 param->flags |= flags;
358 if (flags & X509_V_FLAG_POLICY_MASK)
359 param->flags |= X509_V_FLAG_POLICY_CHECK;
360 return 1;
361 }
362
363 int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
364 unsigned long flags)
365 {
366 param->flags &= ~flags;
367 return 1;
368 }
369
370 unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
371 {
372 return param->flags;
373 }
374
375 int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
376 {
377 return X509_PURPOSE_set(&param->purpose, purpose);
378 }
379
380 int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
381 {
382 return X509_TRUST_set(&param->trust, trust);
383 }
384
385 void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
386 {
387 param->depth = depth;
388 }
389
390 void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
391 {
392 param->check_time = t;
393 param->flags |= X509_V_FLAG_USE_CHECK_TIME;
394 }
395
396 int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
397 ASN1_OBJECT *policy)
398 {
399 if (!param->policies) {
400 param->policies = sk_ASN1_OBJECT_new_null();
401 if (!param->policies)
402 return 0;
403 }
404 if (!sk_ASN1_OBJECT_push(param->policies, policy))
405 return 0;
406 return 1;
407 }
408
409 int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
410 STACK_OF(ASN1_OBJECT) *policies)
411 {
412 size_t i;
413 ASN1_OBJECT *oid, *doid;
414 if (!param)
415 return 0;
416 if (param->policies)
417 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
418
419 if (!policies) {
420 param->policies = NULL;
421 return 1;
422 }
423
424 param->policies = sk_ASN1_OBJECT_new_null();
425 if (!param->policies)
426 return 0;
427
428 for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
429 oid = sk_ASN1_OBJECT_value(policies, i);
430 doid = OBJ_dup(oid);
431 if (!doid)
432 return 0;
433 if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
434 ASN1_OBJECT_free(doid);
435 return 0;
436 }
437 }
438 param->flags |= X509_V_FLAG_POLICY_CHECK;
439 return 1;
440 }
441
442 int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
443 const char *name, size_t namelen)
444 {
445 return int_x509_param_set_hosts(param->id, SET_HOST, name, namelen);
446 }
447
448 int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
449 const char *name, size_t namelen)
450 {
451 return int_x509_param_set_hosts(param->id, ADD_HOST, name, namelen);
452 }
453
454 void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
455 unsigned int flags)
456 {
457 param->id->hostflags = flags;
458 }
459
460 char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param)
461 {
462 return param->id->peername;
463 }
464
465 int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
466 const char *email, size_t emaillen)
467 {
468 return int_x509_param_set1(&param->id->email, &param->id->emaillen,
469 email, emaillen);
470 }
471
472 int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
473 const unsigned char *ip, size_t iplen)
474 {
475 if (iplen != 0 && iplen != 4 && iplen != 16)
476 return 0;
477 return int_x509_param_set1((char **)&param->id->ip, &param->id->iplen,
478 (char *)ip, iplen);
479 }
480
481 int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
482 {
483 unsigned char ipout[16];
484 size_t iplen;
485
486 iplen = (size_t)a2i_ipadd(ipout, ipasc);
487 if (iplen == 0)
488 return 0;
489 return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
490 }
491
492 int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
493 {
494 return param->depth;
495 }
496
497 const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
498 {
499 return param->name;
500 }
501
502 static const X509_VERIFY_PARAM_ID _empty_id =
503 { NULL, 0U, NULL, NULL, 0, NULL, 0 };
504
505 #define vpm_empty_id (X509_VERIFY_PARAM_ID *)&_empty_id
506
507 /*
508 * Default verify parameters: these are used for various applications and can
509 * be overridden by the user specified table. NB: the 'name' field *must* be
510 * in alphabetical order because it will be searched using OBJ_search.
511 */
512
513 static const X509_VERIFY_PARAM default_table[] = {
514 {
515 (char *)"default", /* X509 default parameters */
516 0, /* Check time */
517 0, /* internal flags */
518 0, /* flags */
519 0, /* purpose */
520 0, /* trust */
521 100, /* depth */
522 NULL, /* policies */
523 vpm_empty_id},
524 {
525 (char *)"pkcs7", /* S/MIME sign parameters */
526 0, /* Check time */
527 0, /* internal flags */
528 0, /* flags */
529 X509_PURPOSE_SMIME_SIGN, /* purpose */
530 X509_TRUST_EMAIL, /* trust */
531 -1, /* depth */
532 NULL, /* policies */
533 vpm_empty_id},
534 {
535 (char *)"smime_sign", /* S/MIME sign parameters */
536 0, /* Check time */
537 0, /* internal flags */
538 0, /* flags */
539 X509_PURPOSE_SMIME_SIGN, /* purpose */
540 X509_TRUST_EMAIL, /* trust */
541 -1, /* depth */
542 NULL, /* policies */
543 vpm_empty_id},
544 {
545 (char *)"ssl_client", /* SSL/TLS client parameters */
546 0, /* Check time */
547 0, /* internal flags */
548 0, /* flags */
549 X509_PURPOSE_SSL_CLIENT, /* purpose */
550 X509_TRUST_SSL_CLIENT, /* trust */
551 -1, /* depth */
552 NULL, /* policies */
553 vpm_empty_id},
554 {
555 (char *)"ssl_server", /* SSL/TLS server parameters */
556 0, /* Check time */
557 0, /* internal flags */
558 0, /* flags */
559 X509_PURPOSE_SSL_SERVER, /* purpose */
560 X509_TRUST_SSL_SERVER, /* trust */
561 -1, /* depth */
562 NULL, /* policies */
563 vpm_empty_id}
564 };
565
566 static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
567
568 static int param_cmp(const X509_VERIFY_PARAM **a, const X509_VERIFY_PARAM **b)
569 {
570 return strcmp((*a)->name, (*b)->name);
571 }
572
573 int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
574 {
575 X509_VERIFY_PARAM *ptmp;
576 if (!param_table) {
577 param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
578 if (!param_table)
579 return 0;
580 } else {
581 size_t idx;
582
583 if (sk_X509_VERIFY_PARAM_find(param_table, &idx, param)) {
584 ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
585 X509_VERIFY_PARAM_free(ptmp);
586 (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
587 }
588 }
589 if (!sk_X509_VERIFY_PARAM_push(param_table, param))
590 return 0;
591 return 1;
592 }
593
594 int X509_VERIFY_PARAM_get_count(void)
595 {
596 int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
597 if (param_table)
598 num += sk_X509_VERIFY_PARAM_num(param_table);
599 return num;
600 }
601
602 const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
603 {
604 int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
605 if (id < num)
606 return default_table + id;
607 return sk_X509_VERIFY_PARAM_value(param_table, id - num);
608 }
609
610 const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
611 {
612 X509_VERIFY_PARAM pm;
613 unsigned i, limit;
614
615 pm.name = (char *)name;
616 if (param_table) {
617 size_t idx;
618 if (sk_X509_VERIFY_PARAM_find(param_table, &idx, &pm))
619 return sk_X509_VERIFY_PARAM_value(param_table, idx);
620 }
621
622 limit = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
623 for (i = 0; i < limit; i++) {
624 if (strcmp(default_table[i].name, name) == 0) {
625 return &default_table[i];
626 }
627 }
628 return NULL;
629 }
630
631 void X509_VERIFY_PARAM_table_cleanup(void)
632 {
633 if (param_table)
634 sk_X509_VERIFY_PARAM_pop_free(param_table, X509_VERIFY_PARAM_free);
635 param_table = NULL;
636 }