]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509/x509_vpm.c
6a6f5051b38e67c858e31f0ee9486cd18206b67f
[thirdparty/openssl.git] / crypto / x509 / x509_vpm.c
1 /* x509_vpm.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 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 * licensing@OpenSSL.org.
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 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59 #include <stdio.h>
60
61 #include "cryptlib.h"
62 #include <openssl/crypto.h>
63 #include <openssl/lhash.h>
64 #include <openssl/buffer.h>
65 #include <openssl/x509.h>
66 #include <openssl/x509v3.h>
67
68 #include "vpm_int.h"
69
70 /* X509_VERIFY_PARAM functions */
71
72 #define SET_HOST 0
73 #define ADD_HOST 1
74
75 static char *str_copy(const char *s) { return OPENSSL_strdup(s); }
76 static void str_free(char *s) { OPENSSL_free(s); }
77
78 #define string_stack_free(sk) sk_OPENSSL_STRING_pop_free(sk, str_free)
79
80 static int int_x509_param_set_hosts(X509_VERIFY_PARAM_ID *id, int mode,
81 const char *name, size_t namelen)
82 {
83 char *copy;
84
85 /*
86 * Refuse names with embedded NUL bytes, except perhaps as final byte.
87 * XXX: Do we need to push an error onto the error stack?
88 */
89 if (namelen == 0)
90 namelen = name ? strlen(name) : 0;
91 else if (name && memchr(name, '\0', namelen > 1 ? namelen-1 : namelen))
92 return 0;
93 if (name && name[namelen-1] == '\0')
94 --namelen;
95
96 if (mode == SET_HOST && id->hosts)
97 {
98 string_stack_free(id->hosts);
99 id->hosts = NULL;
100 }
101 if (name == NULL || namelen == 0)
102 return 1;
103
104 copy = BUF_strndup(name, namelen);
105 if (copy == NULL)
106 return 0;
107
108 if (id->hosts == NULL &&
109 (id->hosts = sk_OPENSSL_STRING_new_null()) == NULL)
110 {
111 OPENSSL_free(copy);
112 return 0;
113 }
114
115 if (!sk_OPENSSL_STRING_push(id->hosts, copy))
116 {
117 OPENSSL_free(copy);
118 if (sk_OPENSSL_STRING_num(id->hosts) == 0)
119 {
120 sk_OPENSSL_STRING_free(id->hosts);
121 id->hosts = NULL;
122 }
123 return 0;
124 }
125
126 return 1;
127 }
128
129 static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
130 {
131 X509_VERIFY_PARAM_ID *paramid;
132 if (!param)
133 return;
134 param->name = NULL;
135 param->purpose = 0;
136 param->trust = 0;
137 /*param->inh_flags = X509_VP_FLAG_DEFAULT;*/
138 param->inh_flags = 0;
139 param->flags = 0;
140 param->depth = -1;
141 if (param->policies)
142 {
143 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
144 param->policies = NULL;
145 }
146 paramid = param->id;
147 if (paramid->hosts)
148 {
149 string_stack_free(paramid->hosts);
150 paramid->hosts = NULL;
151 }
152 if (paramid->peername)
153 OPENSSL_free(paramid->peername);
154 if (paramid->email)
155 {
156 OPENSSL_free(paramid->email);
157 paramid->email = NULL;
158 paramid->emaillen = 0;
159 }
160 if (paramid->ip)
161 {
162 OPENSSL_free(paramid->ip);
163 paramid->ip = NULL;
164 paramid->iplen = 0;
165 }
166
167 }
168
169 X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
170 {
171 X509_VERIFY_PARAM *param;
172 X509_VERIFY_PARAM_ID *paramid;
173 param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
174 if (!param)
175 return NULL;
176 paramid = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
177 if (!paramid)
178 {
179 OPENSSL_free(param);
180 return NULL;
181 }
182 memset(param, 0, sizeof(X509_VERIFY_PARAM));
183 memset(paramid, 0, sizeof(X509_VERIFY_PARAM_ID));
184 param->id = paramid;
185 x509_verify_param_zero(param);
186 return param;
187 }
188
189 void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
190 {
191 x509_verify_param_zero(param);
192 OPENSSL_free(param->id);
193 OPENSSL_free(param);
194 }
195
196 /* This function determines how parameters are "inherited" from one structure
197 * to another. There are several different ways this can happen.
198 *
199 * 1. If a child structure needs to have its values initialized from a parent
200 * they are simply copied across. For example SSL_CTX copied to SSL.
201 * 2. If the structure should take on values only if they are currently unset.
202 * For example the values in an SSL structure will take appropriate value
203 * for SSL servers or clients but only if the application has not set new
204 * ones.
205 *
206 * The "inh_flags" field determines how this function behaves.
207 *
208 * Normally any values which are set in the default are not copied from the
209 * destination and verify flags are ORed together.
210 *
211 * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
212 * to the destination. Effectively the values in "to" become default values
213 * which will be used only if nothing new is set in "from".
214 *
215 * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
216 * they are set or not. Flags is still Ored though.
217 *
218 * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
219 * of ORed.
220 *
221 * If X509_VP_FLAG_LOCKED is set then no values are copied.
222 *
223 * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
224 * after the next call.
225 */
226
227 /* Macro to test if a field should be copied from src to dest */
228
229 #define test_x509_verify_param_copy(field, def) \
230 (to_overwrite || \
231 ((src->field != def) && (to_default || (dest->field == def))))
232
233 /* As above but for ID fields */
234
235 #define test_x509_verify_param_copy_id(idf, def) \
236 test_x509_verify_param_copy(id->idf, def)
237
238 /* Macro to test and copy a field if necessary */
239
240 #define x509_verify_param_copy(field, def) \
241 if (test_x509_verify_param_copy(field, def)) \
242 dest->field = src->field
243
244
245 int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
246 const X509_VERIFY_PARAM *src)
247 {
248 unsigned long inh_flags;
249 int to_default, to_overwrite;
250 X509_VERIFY_PARAM_ID *id;
251 if (!src)
252 return 1;
253 id = src->id;
254 inh_flags = dest->inh_flags | src->inh_flags;
255
256 if (inh_flags & X509_VP_FLAG_ONCE)
257 dest->inh_flags = 0;
258
259 if (inh_flags & X509_VP_FLAG_LOCKED)
260 return 1;
261
262 if (inh_flags & X509_VP_FLAG_DEFAULT)
263 to_default = 1;
264 else
265 to_default = 0;
266
267 if (inh_flags & X509_VP_FLAG_OVERWRITE)
268 to_overwrite = 1;
269 else
270 to_overwrite = 0;
271
272 x509_verify_param_copy(purpose, 0);
273 x509_verify_param_copy(trust, 0);
274 x509_verify_param_copy(depth, -1);
275
276 /* If overwrite or check time not set, copy across */
277
278 if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME))
279 {
280 dest->check_time = src->check_time;
281 dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
282 /* Don't need to copy flag: that is done below */
283 }
284
285 if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
286 dest->flags = 0;
287
288 dest->flags |= src->flags;
289
290 if (test_x509_verify_param_copy(policies, NULL))
291 {
292 if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
293 return 0;
294 }
295
296 /* Copy the host flags if and only if we're copying the host list */
297 if (test_x509_verify_param_copy_id(hosts, NULL))
298 {
299 if (dest->id->hosts)
300 {
301 string_stack_free(dest->id->hosts);
302 dest->id->hosts = NULL;
303 }
304 if (id->hosts)
305 {
306 dest->id->hosts =
307 sk_OPENSSL_STRING_deep_copy(id->hosts,
308 str_copy, str_free);
309 if (dest->id->hosts == NULL)
310 return 0;
311 dest->id->hostflags = id->hostflags;
312 }
313 }
314
315 if (test_x509_verify_param_copy_id(email, NULL))
316 {
317 if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen))
318 return 0;
319 }
320
321 if (test_x509_verify_param_copy_id(ip, NULL))
322 {
323 if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
324 return 0;
325 }
326
327 return 1;
328 }
329
330 int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
331 const X509_VERIFY_PARAM *from)
332 {
333 unsigned long save_flags = to->inh_flags;
334 int ret;
335 to->inh_flags |= X509_VP_FLAG_DEFAULT;
336 ret = X509_VERIFY_PARAM_inherit(to, from);
337 to->inh_flags = save_flags;
338 return ret;
339 }
340
341 static int int_x509_param_set1(char **pdest, size_t *pdestlen,
342 const char *src, size_t srclen)
343 {
344 void *tmp;
345 if (src)
346 {
347 if (srclen == 0)
348 {
349 tmp = BUF_strdup(src);
350 srclen = strlen(src);
351 }
352 else
353 tmp = BUF_memdup(src, srclen);
354 if (!tmp)
355 return 0;
356 }
357 else
358 {
359 tmp = NULL;
360 srclen = 0;
361 }
362 if (*pdest)
363 OPENSSL_free(*pdest);
364 *pdest = tmp;
365 if (pdestlen)
366 *pdestlen = srclen;
367 return 1;
368 }
369
370 int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
371 {
372 if (param->name)
373 OPENSSL_free(param->name);
374 param->name = BUF_strdup(name);
375 if (param->name)
376 return 1;
377 return 0;
378 }
379
380 int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
381 {
382 param->flags |= flags;
383 if (flags & X509_V_FLAG_POLICY_MASK)
384 param->flags |= X509_V_FLAG_POLICY_CHECK;
385 return 1;
386 }
387
388 int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
389 {
390 param->flags &= ~flags;
391 return 1;
392 }
393
394 unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
395 {
396 return param->flags;
397 }
398
399 int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
400 {
401 return X509_PURPOSE_set(&param->purpose, purpose);
402 }
403
404 int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
405 {
406 return X509_TRUST_set(&param->trust, trust);
407 }
408
409 void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
410 {
411 param->depth = depth;
412 }
413
414 void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
415 {
416 param->check_time = t;
417 param->flags |= X509_V_FLAG_USE_CHECK_TIME;
418 }
419
420 int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
421 {
422 if (!param->policies)
423 {
424 param->policies = sk_ASN1_OBJECT_new_null();
425 if (!param->policies)
426 return 0;
427 }
428 if (!sk_ASN1_OBJECT_push(param->policies, policy))
429 return 0;
430 return 1;
431 }
432
433 int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
434 STACK_OF(ASN1_OBJECT) *policies)
435 {
436 int i;
437 ASN1_OBJECT *oid, *doid;
438 if (!param)
439 return 0;
440 if (param->policies)
441 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
442
443 if (!policies)
444 {
445 param->policies = NULL;
446 return 1;
447 }
448
449 param->policies = sk_ASN1_OBJECT_new_null();
450 if (!param->policies)
451 return 0;
452
453 for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++)
454 {
455 oid = sk_ASN1_OBJECT_value(policies, i);
456 doid = OBJ_dup(oid);
457 if (!doid)
458 return 0;
459 if (!sk_ASN1_OBJECT_push(param->policies, doid))
460 {
461 ASN1_OBJECT_free(doid);
462 return 0;
463 }
464 }
465 param->flags |= X509_V_FLAG_POLICY_CHECK;
466 return 1;
467 }
468
469 int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
470 const char *name, size_t namelen)
471 {
472 return int_x509_param_set_hosts(param->id, SET_HOST, name, namelen);
473 }
474
475 int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
476 const char *name, size_t namelen)
477 {
478 return int_x509_param_set_hosts(param->id, ADD_HOST, name, namelen);
479 }
480
481 void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
482 unsigned int flags)
483 {
484 param->id->hostflags = flags;
485 }
486
487 char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param)
488 {
489 return param->id->peername;
490 }
491
492 int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
493 const char *email, size_t emaillen)
494 {
495 return int_x509_param_set1(&param->id->email, &param->id->emaillen,
496 email, emaillen);
497 }
498
499 int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
500 const unsigned char *ip, size_t iplen)
501 {
502 if (iplen != 0 && iplen != 4 && iplen != 16)
503 return 0;
504 return int_x509_param_set1((char **)&param->id->ip, &param->id->iplen,
505 (char *)ip, iplen);
506 }
507
508 int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
509 {
510 unsigned char ipout[16];
511 size_t iplen;
512
513 iplen = (size_t) a2i_ipadd(ipout, ipasc);
514 if (iplen == 0)
515 return 0;
516 return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
517 }
518
519 int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
520 {
521 return param->depth;
522 }
523
524 const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
525 {
526 return param->name;
527 }
528
529 static X509_VERIFY_PARAM_ID _empty_id = {NULL, 0U, NULL, NULL, 0, NULL, 0};
530
531 #define vpm_empty_id (X509_VERIFY_PARAM_ID *)&_empty_id
532
533 /* Default verify parameters: these are used for various
534 * applications and can be overridden by the user specified table.
535 * NB: the 'name' field *must* be in alphabetical order because it
536 * will be searched using OBJ_search.
537 */
538
539 static const X509_VERIFY_PARAM default_table[] = {
540 {
541 "default", /* X509 default parameters */
542 0, /* Check time */
543 0, /* internal flags */
544 0, /* flags */
545 0, /* purpose */
546 0, /* trust */
547 100, /* depth */
548 NULL, /* policies */
549 vpm_empty_id
550 },
551 {
552 "pkcs7", /* S/MIME sign parameters */
553 0, /* Check time */
554 0, /* internal flags */
555 0, /* flags */
556 X509_PURPOSE_SMIME_SIGN, /* purpose */
557 X509_TRUST_EMAIL, /* trust */
558 -1, /* depth */
559 NULL, /* policies */
560 vpm_empty_id
561 },
562 {
563 "smime_sign", /* S/MIME sign parameters */
564 0, /* Check time */
565 0, /* internal flags */
566 0, /* flags */
567 X509_PURPOSE_SMIME_SIGN, /* purpose */
568 X509_TRUST_EMAIL, /* trust */
569 -1, /* depth */
570 NULL, /* policies */
571 vpm_empty_id
572 },
573 {
574 "ssl_client", /* SSL/TLS client parameters */
575 0, /* Check time */
576 0, /* internal flags */
577 0, /* flags */
578 X509_PURPOSE_SSL_CLIENT, /* purpose */
579 X509_TRUST_SSL_CLIENT, /* trust */
580 -1, /* depth */
581 NULL, /* policies */
582 vpm_empty_id
583 },
584 {
585 "ssl_server", /* SSL/TLS server parameters */
586 0, /* Check time */
587 0, /* internal flags */
588 0, /* flags */
589 X509_PURPOSE_SSL_SERVER, /* purpose */
590 X509_TRUST_SSL_SERVER, /* trust */
591 -1, /* depth */
592 NULL, /* policies */
593 vpm_empty_id
594 }};
595
596 static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
597
598 static int table_cmp(const X509_VERIFY_PARAM *a, const X509_VERIFY_PARAM *b)
599
600 {
601 return strcmp(a->name, b->name);
602 }
603
604 DECLARE_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM,
605 table);
606 IMPLEMENT_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM,
607 table);
608
609 static int param_cmp(const X509_VERIFY_PARAM * const *a,
610 const X509_VERIFY_PARAM * const *b)
611 {
612 return strcmp((*a)->name, (*b)->name);
613 }
614
615 int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
616 {
617 int idx;
618 X509_VERIFY_PARAM *ptmp;
619 if (!param_table)
620 {
621 param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
622 if (!param_table)
623 return 0;
624 }
625 else
626 {
627 idx = sk_X509_VERIFY_PARAM_find(param_table, param);
628 if (idx != -1)
629 {
630 ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
631 X509_VERIFY_PARAM_free(ptmp);
632 (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
633 }
634 }
635 if (!sk_X509_VERIFY_PARAM_push(param_table, param))
636 return 0;
637 return 1;
638 }
639
640 int X509_VERIFY_PARAM_get_count(void)
641 {
642 int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
643 if (param_table)
644 num += sk_X509_VERIFY_PARAM_num(param_table);
645 return num;
646 }
647
648 const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
649 {
650 int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
651 if (id < num)
652 return default_table + id;
653 return sk_X509_VERIFY_PARAM_value(param_table, id - num);
654 }
655
656 const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
657 {
658 int idx;
659 X509_VERIFY_PARAM pm;
660
661 pm.name = (char *)name;
662 if (param_table)
663 {
664 idx = sk_X509_VERIFY_PARAM_find(param_table, &pm);
665 if (idx != -1)
666 return sk_X509_VERIFY_PARAM_value(param_table, idx);
667 }
668 return OBJ_bsearch_table(&pm, default_table,
669 sizeof(default_table)/sizeof(X509_VERIFY_PARAM));
670 }
671
672 void X509_VERIFY_PARAM_table_cleanup(void)
673 {
674 if (param_table)
675 sk_X509_VERIFY_PARAM_pop_free(param_table,
676 X509_VERIFY_PARAM_free);
677 param_table = NULL;
678 }