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