blob: cf9bed01250dd09e6e3d0388b899ae9e1b008e2a [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;
236 }
237
238 if (test_x509_verify_param_copy_id(email, NULL))
239 {
240 if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen))
241 return 0;
242 }
243
244 if (test_x509_verify_param_copy_id(ip, NULL))
245 {
246 if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
247 return 0;
248 }
249
250 return 1;
251 }
252
253int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
254 const X509_VERIFY_PARAM *from)
255 {
256 unsigned long save_flags = to->inh_flags;
257 int ret;
258 to->inh_flags |= X509_VP_FLAG_DEFAULT;
259 ret = X509_VERIFY_PARAM_inherit(to, from);
260 to->inh_flags = save_flags;
261 return ret;
262 }
263
264static int int_x509_param_set1(unsigned char **pdest, size_t *pdestlen,
265 const unsigned char *src, size_t srclen)
266 {
267 void *tmp;
268 if (src)
269 {
270 if (srclen == 0)
271 {
272 tmp = BUF_strdup((char *)src);
273 srclen = strlen((char *)src);
274 }
275 else
276 tmp = BUF_memdup(src, srclen);
277 if (!tmp)
278 return 0;
279 }
280 else
281 {
282 tmp = NULL;
283 srclen = 0;
284 }
285 if (*pdest)
286 OPENSSL_free(*pdest);
287 *pdest = tmp;
288 if (pdestlen)
289 *pdestlen = srclen;
290 return 1;
291 }
292
293int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
294 {
295 if (param->name)
296 OPENSSL_free(param->name);
297 param->name = BUF_strdup(name);
298 if (param->name)
299 return 1;
300 return 0;
301 }
302
303int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
304 {
305 param->flags |= flags;
306 if (flags & X509_V_FLAG_POLICY_MASK)
307 param->flags |= X509_V_FLAG_POLICY_CHECK;
308 return 1;
309 }
310
311int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
312 {
313 param->flags &= ~flags;
314 return 1;
315 }
316
317unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
318 {
319 return param->flags;
320 }
321
322int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
323 {
324 return X509_PURPOSE_set(&param->purpose, purpose);
325 }
326
327int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
328 {
329 return X509_TRUST_set(&param->trust, trust);
330 }
331
332void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
333 {
334 param->depth = depth;
335 }
336
337void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
338 {
339 param->check_time = t;
340 param->flags |= X509_V_FLAG_USE_CHECK_TIME;
341 }
342
343int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
344 {
345 if (!param->policies)
346 {
347 param->policies = sk_ASN1_OBJECT_new_null();
348 if (!param->policies)
349 return 0;
350 }
351 if (!sk_ASN1_OBJECT_push(param->policies, policy))
352 return 0;
353 return 1;
354 }
355
356int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
357 STACK_OF(ASN1_OBJECT) *policies)
358 {
359 size_t i;
360 ASN1_OBJECT *oid, *doid;
361 if (!param)
362 return 0;
363 if (param->policies)
364 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
365
366 if (!policies)
367 {
368 param->policies = NULL;
369 return 1;
370 }
371
372 param->policies = sk_ASN1_OBJECT_new_null();
373 if (!param->policies)
374 return 0;
375
376 for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++)
377 {
378 oid = sk_ASN1_OBJECT_value(policies, i);
379 doid = OBJ_dup(oid);
380 if (!doid)
381 return 0;
382 if (!sk_ASN1_OBJECT_push(param->policies, doid))
383 {
384 ASN1_OBJECT_free(doid);
385 return 0;
386 }
387 }
388 param->flags |= X509_V_FLAG_POLICY_CHECK;
389 return 1;
390 }
391
392int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
393 const unsigned char *name, size_t namelen)
394 {
395 return int_x509_param_set1(&param->id->host, &param->id->hostlen,
396 name, namelen);
397 }
398
399int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
400 const unsigned char *email, size_t emaillen)
401 {
402 return int_x509_param_set1(&param->id->email, &param->id->emaillen,
403 email, emaillen);
404 }
405
406int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
407 const unsigned char *ip, size_t iplen)
408 {
409 if (iplen != 0 && iplen != 4 && iplen != 16)
410 return 0;
411 return int_x509_param_set1(&param->id->ip, &param->id->iplen, ip, iplen);
412 }
413
414int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
415 {
416 unsigned char ipout[16];
417 int iplen;
418 iplen = a2i_ipadd(ipout, ipasc);
419 if (iplen == 0)
420 return 0;
421 return X509_VERIFY_PARAM_set1_ip(param, ipout, (size_t)iplen);
422 }
423
424int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
425 {
426 return param->depth;
427 }
428
429const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
430 {
431 return param->name;
432 }
433
434static X509_VERIFY_PARAM_ID _empty_id = {NULL, 0, NULL, 0, NULL, 0};
435
436#define vpm_empty_id (X509_VERIFY_PARAM_ID *)&_empty_id
437
438/* Default verify parameters: these are used for various
439 * applications and can be overridden by the user specified table.
440 * NB: the 'name' field *must* be in alphabetical order because it
441 * will be searched using OBJ_search.
442 */
443
444static const X509_VERIFY_PARAM default_table[] = {
445 {
Adam Langley73510762014-06-20 12:00:00 -0700446 (char *) "default", /* X509 default parameters */
Adam Langley95c29f32014-06-20 12:00:00 -0700447 0, /* Check time */
448 0, /* internal flags */
449 0, /* flags */
450 0, /* purpose */
451 0, /* trust */
452 100, /* depth */
453 NULL, /* policies */
454 vpm_empty_id
455 },
456 {
Adam Langley73510762014-06-20 12:00:00 -0700457 (char *) "pkcs7", /* S/MIME sign parameters */
Adam Langley95c29f32014-06-20 12:00:00 -0700458 0, /* Check time */
459 0, /* internal flags */
460 0, /* flags */
461 X509_PURPOSE_SMIME_SIGN, /* purpose */
462 X509_TRUST_EMAIL, /* trust */
463 -1, /* depth */
464 NULL, /* policies */
465 vpm_empty_id
466 },
467 {
Adam Langley73510762014-06-20 12:00:00 -0700468 (char *) "smime_sign", /* S/MIME sign parameters */
Adam Langley95c29f32014-06-20 12:00:00 -0700469 0, /* Check time */
470 0, /* internal flags */
471 0, /* flags */
472 X509_PURPOSE_SMIME_SIGN, /* purpose */
473 X509_TRUST_EMAIL, /* trust */
474 -1, /* depth */
475 NULL, /* policies */
476 vpm_empty_id
477 },
478 {
Adam Langley73510762014-06-20 12:00:00 -0700479 (char *) "ssl_client", /* SSL/TLS client parameters */
Adam Langley95c29f32014-06-20 12:00:00 -0700480 0, /* Check time */
481 0, /* internal flags */
482 0, /* flags */
483 X509_PURPOSE_SSL_CLIENT, /* purpose */
484 X509_TRUST_SSL_CLIENT, /* trust */
485 -1, /* depth */
486 NULL, /* policies */
487 vpm_empty_id
488 },
489 {
Adam Langley73510762014-06-20 12:00:00 -0700490 (char *) "ssl_server", /* SSL/TLS server parameters */
Adam Langley95c29f32014-06-20 12:00:00 -0700491 0, /* Check time */
492 0, /* internal flags */
493 0, /* flags */
494 X509_PURPOSE_SSL_SERVER, /* purpose */
495 X509_TRUST_SSL_SERVER, /* trust */
496 -1, /* depth */
497 NULL, /* policies */
498 vpm_empty_id
499 }};
500
501static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
502
503static int param_cmp(const X509_VERIFY_PARAM **a,
504 const X509_VERIFY_PARAM **b)
505 {
506 return strcmp((*a)->name, (*b)->name);
507 }
508
509int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
510 {
511 X509_VERIFY_PARAM *ptmp;
512 if (!param_table)
513 {
514 param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
515 if (!param_table)
516 return 0;
517 }
518 else
519 {
520 size_t idx;
521
522 if (sk_X509_VERIFY_PARAM_find(param_table, &idx, param))
523 {
524 ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
525 X509_VERIFY_PARAM_free(ptmp);
526 (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
527 }
528 }
529 if (!sk_X509_VERIFY_PARAM_push(param_table, param))
530 return 0;
531 return 1;
532 }
533
534int X509_VERIFY_PARAM_get_count(void)
535 {
536 int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
537 if (param_table)
538 num += sk_X509_VERIFY_PARAM_num(param_table);
539 return num;
540 }
541
542const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
543 {
544 int num = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
545 if (id < num)
546 return default_table + id;
547 return sk_X509_VERIFY_PARAM_value(param_table, id - num);
548 }
549
550const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
551 {
552 X509_VERIFY_PARAM pm;
553 unsigned i, limit;
554
555 pm.name = (char *)name;
556 if (param_table)
557 {
558 size_t idx;
559 if (sk_X509_VERIFY_PARAM_find(param_table, &idx, &pm))
560 return sk_X509_VERIFY_PARAM_value(param_table, idx);
561 }
562
563 limit = sizeof(default_table)/sizeof(X509_VERIFY_PARAM);
564 for (i = 0; i < limit; i++) {
565 if (strcmp(default_table[i].name, name) == 0) {
566 return &default_table[i];
567 }
568 }
569 return NULL;
570 }
571
572void X509_VERIFY_PARAM_table_cleanup(void)
573 {
574 if (param_table)
575 sk_X509_VERIFY_PARAM_pop_free(param_table,
576 X509_VERIFY_PARAM_free);
577 param_table = NULL;
578 }