blob: 0b03361e1355f3e25785091a988387ec76608b0c [file] [log] [blame]
David Benjamin4969cc92016-04-22 15:02:23 -04001/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 2004.
4 */
Adam Langleyd9e397b2015-01-22 14:27:53 -08005/* ====================================================================
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
David Benjamin4969cc92016-04-22 15:02:23 -040013 * notice, this list of conditions and the following disclaimer.
Adam Langleyd9e397b2015-01-22 14:27:53 -080014 *
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#include <string.h>
58
59#include <openssl/buf.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080060#include <openssl/mem.h>
61#include <openssl/obj.h>
Adam Langleye9ada862015-05-11 17:20:37 -070062#include <openssl/stack.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080063#include <openssl/x509.h>
64#include <openssl/x509v3.h>
65
66#include "vpm_int.h"
Robert Sloan69939df2017-01-09 10:53:07 -080067#include "../internal.h"
68
Adam Langleyd9e397b2015-01-22 14:27:53 -080069
Adam Langleyd9e397b2015-01-22 14:27:53 -080070/* X509_VERIFY_PARAM functions */
71
Adam Langleye9ada862015-05-11 17:20:37 -070072#define SET_HOST 0
73#define ADD_HOST 1
74
David Benjamin4969cc92016-04-22 15:02:23 -040075static char *str_copy(char *s)
76{
77 return OPENSSL_strdup(s);
78}
79
80static void str_free(char *s)
81{
82 OPENSSL_free(s);
83}
Adam Langleye9ada862015-05-11 17:20:37 -070084
85#define string_stack_free(sk) sk_OPENSSL_STRING_pop_free(sk, str_free)
86
87static int int_x509_param_set_hosts(X509_VERIFY_PARAM_ID *id, int mode,
David Benjamin4969cc92016-04-22 15:02:23 -040088 const char *name, size_t namelen)
89{
90 char *copy;
Adam Langleye9ada862015-05-11 17:20:37 -070091
Robert Sloanab8b8882018-03-26 11:39:51 -070092 // This is an OpenSSL quirk that BoringSSL typically doesn't support.
93 // However, we didn't make this a fatal error at the time, which was a
94 // mistake. Because of that, given the risk that someone could assume the
95 // OpenSSL semantics from BoringSSL, it's supported in this case.
96 if (name != NULL && namelen == 0) {
97 namelen = strlen(name);
98 }
99
David Benjamin4969cc92016-04-22 15:02:23 -0400100 /*
101 * Refuse names with embedded NUL bytes.
102 * XXX: Do we need to push an error onto the error stack?
103 */
Robert Sloan69939df2017-01-09 10:53:07 -0800104 if (name && OPENSSL_memchr(name, '\0', namelen))
David Benjamin4969cc92016-04-22 15:02:23 -0400105 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700106
David Benjamin4969cc92016-04-22 15:02:23 -0400107 if (mode == SET_HOST && id->hosts) {
108 string_stack_free(id->hosts);
109 id->hosts = NULL;
110 }
111 if (name == NULL || namelen == 0)
112 return 1;
Adam Langleye9ada862015-05-11 17:20:37 -0700113
David Benjamin4969cc92016-04-22 15:02:23 -0400114 copy = BUF_strndup(name, namelen);
115 if (copy == NULL)
116 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700117
David Benjamin4969cc92016-04-22 15:02:23 -0400118 if (id->hosts == NULL &&
119 (id->hosts = sk_OPENSSL_STRING_new_null()) == NULL) {
120 OPENSSL_free(copy);
121 return 0;
122 }
Adam Langleye9ada862015-05-11 17:20:37 -0700123
David Benjamin4969cc92016-04-22 15:02:23 -0400124 if (!sk_OPENSSL_STRING_push(id->hosts, copy)) {
125 OPENSSL_free(copy);
126 if (sk_OPENSSL_STRING_num(id->hosts) == 0) {
127 sk_OPENSSL_STRING_free(id->hosts);
128 id->hosts = NULL;
129 }
130 return 0;
131 }
Adam Langleye9ada862015-05-11 17:20:37 -0700132
David Benjamin4969cc92016-04-22 15:02:23 -0400133 return 1;
134}
Adam Langleye9ada862015-05-11 17:20:37 -0700135
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136static void x509_verify_param_zero(X509_VERIFY_PARAM *param)
David Benjamin4969cc92016-04-22 15:02:23 -0400137{
138 X509_VERIFY_PARAM_ID *paramid;
139 if (!param)
140 return;
141 param->name = NULL;
142 param->purpose = 0;
143 param->trust = 0;
144 /*
145 * param->inh_flags = X509_VP_FLAG_DEFAULT;
146 */
147 param->inh_flags = 0;
148 param->flags = 0;
149 param->depth = -1;
150 if (param->policies) {
151 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
152 param->policies = NULL;
153 }
154 paramid = param->id;
155 if (paramid->hosts) {
156 string_stack_free(paramid->hosts);
157 paramid->hosts = NULL;
158 }
159 if (paramid->peername) {
160 OPENSSL_free(paramid->peername);
161 paramid->peername = NULL;
162 }
163 if (paramid->email) {
164 OPENSSL_free(paramid->email);
165 paramid->email = NULL;
166 paramid->emaillen = 0;
167 }
168 if (paramid->ip) {
169 OPENSSL_free(paramid->ip);
170 paramid->ip = NULL;
171 paramid->iplen = 0;
172 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800173
David Benjamin4969cc92016-04-22 15:02:23 -0400174}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175
176X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void)
David Benjamin4969cc92016-04-22 15:02:23 -0400177{
178 X509_VERIFY_PARAM *param;
179 X509_VERIFY_PARAM_ID *paramid;
180 param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM));
181 if (!param)
182 return NULL;
183 paramid = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM_ID));
184 if (!paramid) {
185 OPENSSL_free(param);
186 return NULL;
187 }
Robert Sloan69939df2017-01-09 10:53:07 -0800188 OPENSSL_memset(param, 0, sizeof(X509_VERIFY_PARAM));
189 OPENSSL_memset(paramid, 0, sizeof(X509_VERIFY_PARAM_ID));
David Benjamin4969cc92016-04-22 15:02:23 -0400190 param->id = paramid;
191 x509_verify_param_zero(param);
192 return param;
193}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194
195void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
David Benjamin4969cc92016-04-22 15:02:23 -0400196{
197 if (param == NULL)
198 return;
199 x509_verify_param_zero(param);
200 OPENSSL_free(param->id);
201 OPENSSL_free(param);
202}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400204/*-
David Benjamin4969cc92016-04-22 15:02:23 -0400205 * This function determines how parameters are "inherited" from one structure
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400206 * to another. There are several different ways this can happen.
207 *
208 * 1. If a child structure needs to have its values initialized from a parent
209 * they are simply copied across. For example SSL_CTX copied to SSL.
210 * 2. If the structure should take on values only if they are currently unset.
211 * For example the values in an SSL structure will take appropriate value
212 * for SSL servers or clients but only if the application has not set new
213 * ones.
214 *
215 * The "inh_flags" field determines how this function behaves.
216 *
217 * Normally any values which are set in the default are not copied from the
218 * destination and verify flags are ORed together.
219 *
220 * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied
221 * to the destination. Effectively the values in "to" become default values
222 * which will be used only if nothing new is set in "from".
223 *
224 * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether
225 * they are set or not. Flags is still Ored though.
226 *
227 * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead
228 * of ORed.
229 *
230 * If X509_VP_FLAG_LOCKED is set then no values are copied.
231 *
232 * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed
233 * after the next call.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800234 */
235
236/* Macro to test if a field should be copied from src to dest */
237
238#define test_x509_verify_param_copy(field, def) \
David Benjamin95add822016-10-19 01:09:12 -0400239 (to_overwrite || \
240 ((src->field != (def)) && (to_default || (dest->field == (def)))))
Adam Langleyd9e397b2015-01-22 14:27:53 -0800241
242/* As above but for ID fields */
243
244#define test_x509_verify_param_copy_id(idf, def) \
David Benjamin4969cc92016-04-22 15:02:23 -0400245 test_x509_verify_param_copy(id->idf, def)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246
247/* Macro to test and copy a field if necessary */
248
249#define x509_verify_param_copy(field, def) \
David Benjamin4969cc92016-04-22 15:02:23 -0400250 if (test_x509_verify_param_copy(field, def)) \
251 dest->field = src->field
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252
253int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest,
David Benjamin4969cc92016-04-22 15:02:23 -0400254 const X509_VERIFY_PARAM *src)
255{
256 unsigned long inh_flags;
257 int to_default, to_overwrite;
258 X509_VERIFY_PARAM_ID *id;
259 if (!src)
260 return 1;
261 id = src->id;
262 inh_flags = dest->inh_flags | src->inh_flags;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800263
David Benjamin4969cc92016-04-22 15:02:23 -0400264 if (inh_flags & X509_VP_FLAG_ONCE)
265 dest->inh_flags = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266
David Benjamin4969cc92016-04-22 15:02:23 -0400267 if (inh_flags & X509_VP_FLAG_LOCKED)
268 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269
David Benjamin4969cc92016-04-22 15:02:23 -0400270 if (inh_flags & X509_VP_FLAG_DEFAULT)
271 to_default = 1;
272 else
273 to_default = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274
David Benjamin4969cc92016-04-22 15:02:23 -0400275 if (inh_flags & X509_VP_FLAG_OVERWRITE)
276 to_overwrite = 1;
277 else
278 to_overwrite = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800279
David Benjamin4969cc92016-04-22 15:02:23 -0400280 x509_verify_param_copy(purpose, 0);
281 x509_verify_param_copy(trust, 0);
282 x509_verify_param_copy(depth, -1);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283
David Benjamin4969cc92016-04-22 15:02:23 -0400284 /* If overwrite or check time not set, copy across */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800285
David Benjamin4969cc92016-04-22 15:02:23 -0400286 if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
287 dest->check_time = src->check_time;
288 dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
289 /* Don't need to copy flag: that is done below */
290 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291
David Benjamin4969cc92016-04-22 15:02:23 -0400292 if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
293 dest->flags = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800294
David Benjamin4969cc92016-04-22 15:02:23 -0400295 dest->flags |= src->flags;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296
David Benjamin4969cc92016-04-22 15:02:23 -0400297 if (test_x509_verify_param_copy(policies, NULL)) {
298 if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
299 return 0;
300 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800301
David Benjamin4969cc92016-04-22 15:02:23 -0400302 /* Copy the host flags if and only if we're copying the host list */
303 if (test_x509_verify_param_copy_id(hosts, NULL)) {
304 if (dest->id->hosts) {
305 string_stack_free(dest->id->hosts);
306 dest->id->hosts = NULL;
307 }
308 if (id->hosts) {
309 dest->id->hosts =
310 sk_OPENSSL_STRING_deep_copy(id->hosts, str_copy, str_free);
311 if (dest->id->hosts == NULL)
312 return 0;
313 dest->id->hostflags = id->hostflags;
314 }
315 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800316
David Benjamin4969cc92016-04-22 15:02:23 -0400317 if (test_x509_verify_param_copy_id(email, NULL)) {
318 if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen))
319 return 0;
320 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800321
David Benjamin4969cc92016-04-22 15:02:23 -0400322 if (test_x509_verify_param_copy_id(ip, NULL)) {
323 if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
324 return 0;
325 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800326
David Benjamin4969cc92016-04-22 15:02:23 -0400327 return 1;
328}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329
330int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to,
David Benjamin4969cc92016-04-22 15:02:23 -0400331 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}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800340
Adam Langleye9ada862015-05-11 17:20:37 -0700341static int int_x509_param_set1(char **pdest, size_t *pdestlen,
David Benjamin4969cc92016-04-22 15:02:23 -0400342 const char *src, size_t srclen)
343{
344 void *tmp;
345 if (src) {
346 if (srclen == 0) {
347 tmp = BUF_strdup(src);
348 srclen = strlen(src);
349 } else
350 tmp = BUF_memdup(src, srclen);
351 if (!tmp)
352 return 0;
353 } else {
354 tmp = NULL;
355 srclen = 0;
356 }
357 if (*pdest)
358 OPENSSL_free(*pdest);
359 *pdest = tmp;
360 if (pdestlen)
361 *pdestlen = srclen;
362 return 1;
363}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364
365int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
David Benjamin4969cc92016-04-22 15:02:23 -0400366{
367 if (param->name)
368 OPENSSL_free(param->name);
369 param->name = BUF_strdup(name);
370 if (param->name)
371 return 1;
372 return 0;
373}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800374
375int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)
David Benjamin4969cc92016-04-22 15:02:23 -0400376{
377 param->flags |= flags;
378 if (flags & X509_V_FLAG_POLICY_MASK)
379 param->flags |= X509_V_FLAG_POLICY_CHECK;
380 return 1;
381}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382
David Benjamin4969cc92016-04-22 15:02:23 -0400383int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param,
384 unsigned long flags)
385{
386 param->flags &= ~flags;
387 return 1;
388}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800389
390unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
David Benjamin4969cc92016-04-22 15:02:23 -0400391{
392 return param->flags;
393}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800394
395int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
David Benjamin4969cc92016-04-22 15:02:23 -0400396{
397 return X509_PURPOSE_set(&param->purpose, purpose);
398}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800399
400int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
David Benjamin4969cc92016-04-22 15:02:23 -0400401{
402 return X509_TRUST_set(&param->trust, trust);
403}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800404
405void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth)
David Benjamin4969cc92016-04-22 15:02:23 -0400406{
407 param->depth = depth;
408}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409
410void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
David Benjamin4969cc92016-04-22 15:02:23 -0400411{
412 param->check_time = t;
413 param->flags |= X509_V_FLAG_USE_CHECK_TIME;
414}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800415
David Benjamin4969cc92016-04-22 15:02:23 -0400416int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param,
417 ASN1_OBJECT *policy)
418{
419 if (!param->policies) {
420 param->policies = sk_ASN1_OBJECT_new_null();
421 if (!param->policies)
422 return 0;
423 }
424 if (!sk_ASN1_OBJECT_push(param->policies, policy))
425 return 0;
426 return 1;
427}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800428
David Benjamin4969cc92016-04-22 15:02:23 -0400429int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
430 STACK_OF(ASN1_OBJECT) *policies)
431{
432 size_t i;
433 ASN1_OBJECT *oid, *doid;
434 if (!param)
435 return 0;
436 if (param->policies)
437 sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800438
David Benjamin4969cc92016-04-22 15:02:23 -0400439 if (!policies) {
440 param->policies = NULL;
441 return 1;
442 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800443
David Benjamin4969cc92016-04-22 15:02:23 -0400444 param->policies = sk_ASN1_OBJECT_new_null();
445 if (!param->policies)
446 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800447
David Benjamin4969cc92016-04-22 15:02:23 -0400448 for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
449 oid = sk_ASN1_OBJECT_value(policies, i);
450 doid = OBJ_dup(oid);
451 if (!doid)
452 return 0;
453 if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
454 ASN1_OBJECT_free(doid);
455 return 0;
456 }
457 }
458 param->flags |= X509_V_FLAG_POLICY_CHECK;
459 return 1;
460}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800461
462int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
David Benjamin4969cc92016-04-22 15:02:23 -0400463 const char *name, size_t namelen)
464{
465 return int_x509_param_set_hosts(param->id, SET_HOST, name, namelen);
466}
Adam Langleye9ada862015-05-11 17:20:37 -0700467
468int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
David Benjamin4969cc92016-04-22 15:02:23 -0400469 const char *name, size_t namelen)
470{
471 return int_x509_param_set_hosts(param->id, ADD_HOST, name, namelen);
472}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800473
474void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param,
David Benjamin4969cc92016-04-22 15:02:23 -0400475 unsigned int flags)
476{
477 param->id->hostflags = flags;
478}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800479
Adam Langleye9ada862015-05-11 17:20:37 -0700480char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param)
David Benjamin4969cc92016-04-22 15:02:23 -0400481{
482 return param->id->peername;
483}
Adam Langleye9ada862015-05-11 17:20:37 -0700484
Adam Langleyd9e397b2015-01-22 14:27:53 -0800485int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,
David Benjamin4969cc92016-04-22 15:02:23 -0400486 const char *email, size_t emaillen)
487{
488 return int_x509_param_set1(&param->id->email, &param->id->emaillen,
489 email, emaillen);
490}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800491
492int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param,
David Benjamin4969cc92016-04-22 15:02:23 -0400493 const unsigned char *ip, size_t iplen)
494{
495 if (iplen != 0 && iplen != 4 && iplen != 16)
496 return 0;
497 return int_x509_param_set1((char **)&param->id->ip, &param->id->iplen,
498 (char *)ip, iplen);
499}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800500
501int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
David Benjamin4969cc92016-04-22 15:02:23 -0400502{
503 unsigned char ipout[16];
504 size_t iplen;
Adam Langleye9ada862015-05-11 17:20:37 -0700505
David Benjamin4969cc92016-04-22 15:02:23 -0400506 iplen = (size_t)a2i_ipadd(ipout, ipasc);
507 if (iplen == 0)
508 return 0;
509 return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
510}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800511
512int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
David Benjamin4969cc92016-04-22 15:02:23 -0400513{
514 return param->depth;
515}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800516
517const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
David Benjamin4969cc92016-04-22 15:02:23 -0400518{
519 return param->name;
520}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800521
David Benjamin4969cc92016-04-22 15:02:23 -0400522static const X509_VERIFY_PARAM_ID _empty_id =
523 { NULL, 0U, NULL, NULL, 0, NULL, 0 };
Adam Langleyd9e397b2015-01-22 14:27:53 -0800524
David Benjamin95add822016-10-19 01:09:12 -0400525#define vpm_empty_id ((X509_VERIFY_PARAM_ID *)&_empty_id)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800526
David Benjamin4969cc92016-04-22 15:02:23 -0400527/*
528 * Default verify parameters: these are used for various applications and can
529 * be overridden by the user specified table. NB: the 'name' field *must* be
530 * in alphabetical order because it will be searched using OBJ_search.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800531 */
532
533static const X509_VERIFY_PARAM default_table[] = {
David Benjamin4969cc92016-04-22 15:02:23 -0400534 {
535 (char *)"default", /* X509 default parameters */
536 0, /* Check time */
537 0, /* internal flags */
538 0, /* flags */
539 0, /* purpose */
540 0, /* trust */
541 100, /* depth */
542 NULL, /* policies */
543 vpm_empty_id},
544 {
545 (char *)"pkcs7", /* S/MIME sign parameters */
546 0, /* Check time */
547 0, /* internal flags */
548 0, /* flags */
549 X509_PURPOSE_SMIME_SIGN, /* purpose */
550 X509_TRUST_EMAIL, /* trust */
551 -1, /* depth */
552 NULL, /* policies */
553 vpm_empty_id},
554 {
555 (char *)"smime_sign", /* S/MIME sign parameters */
556 0, /* Check time */
557 0, /* internal flags */
558 0, /* flags */
559 X509_PURPOSE_SMIME_SIGN, /* purpose */
560 X509_TRUST_EMAIL, /* trust */
561 -1, /* depth */
562 NULL, /* policies */
563 vpm_empty_id},
564 {
565 (char *)"ssl_client", /* SSL/TLS client parameters */
566 0, /* Check time */
567 0, /* internal flags */
568 0, /* flags */
569 X509_PURPOSE_SSL_CLIENT, /* purpose */
570 X509_TRUST_SSL_CLIENT, /* trust */
571 -1, /* depth */
572 NULL, /* policies */
573 vpm_empty_id},
574 {
575 (char *)"ssl_server", /* SSL/TLS server parameters */
576 0, /* Check time */
577 0, /* internal flags */
578 0, /* flags */
579 X509_PURPOSE_SSL_SERVER, /* purpose */
580 X509_TRUST_SSL_SERVER, /* trust */
581 -1, /* depth */
582 NULL, /* policies */
583 vpm_empty_id}
584};
Adam Langleyd9e397b2015-01-22 14:27:53 -0800585
586static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL;
587
David Benjamin4969cc92016-04-22 15:02:23 -0400588static int param_cmp(const X509_VERIFY_PARAM **a, const X509_VERIFY_PARAM **b)
589{
590 return strcmp((*a)->name, (*b)->name);
591}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800592
593int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
David Benjamin4969cc92016-04-22 15:02:23 -0400594{
595 X509_VERIFY_PARAM *ptmp;
596 if (!param_table) {
597 param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
598 if (!param_table)
599 return 0;
600 } else {
601 size_t idx;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800602
David Benjamin4969cc92016-04-22 15:02:23 -0400603 if (sk_X509_VERIFY_PARAM_find(param_table, &idx, param)) {
604 ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx);
605 X509_VERIFY_PARAM_free(ptmp);
606 (void)sk_X509_VERIFY_PARAM_delete(param_table, idx);
607 }
608 }
609 if (!sk_X509_VERIFY_PARAM_push(param_table, param))
610 return 0;
611 return 1;
612}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800613
614int X509_VERIFY_PARAM_get_count(void)
David Benjamin4969cc92016-04-22 15:02:23 -0400615{
616 int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
617 if (param_table)
618 num += sk_X509_VERIFY_PARAM_num(param_table);
619 return num;
620}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800621
622const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
David Benjamin4969cc92016-04-22 15:02:23 -0400623{
624 int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
625 if (id < num)
626 return default_table + id;
627 return sk_X509_VERIFY_PARAM_value(param_table, id - num);
628}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800629
630const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
David Benjamin4969cc92016-04-22 15:02:23 -0400631{
632 X509_VERIFY_PARAM pm;
633 unsigned i, limit;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800634
David Benjamin4969cc92016-04-22 15:02:23 -0400635 pm.name = (char *)name;
636 if (param_table) {
637 size_t idx;
638 if (sk_X509_VERIFY_PARAM_find(param_table, &idx, &pm))
639 return sk_X509_VERIFY_PARAM_value(param_table, idx);
640 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800641
David Benjamin4969cc92016-04-22 15:02:23 -0400642 limit = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
643 for (i = 0; i < limit; i++) {
644 if (strcmp(default_table[i].name, name) == 0) {
645 return &default_table[i];
646 }
647 }
648 return NULL;
649}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800650
651void X509_VERIFY_PARAM_table_cleanup(void)
David Benjamin4969cc92016-04-22 15:02:23 -0400652{
653 if (param_table)
654 sk_X509_VERIFY_PARAM_pop_free(param_table, X509_VERIFY_PARAM_free);
655 param_table = NULL;
656}