]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
33388b44 | 2 | * Copyright 2004-2020 The OpenSSL Project Authors. All Rights Reserved. |
5d7c222d | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
5d7c222d DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | ||
b39fc560 | 12 | #include "internal/cryptlib.h" |
5d7c222d | 13 | #include <openssl/crypto.h> |
5d7c222d DSH |
14 | #include <openssl/buffer.h> |
15 | #include <openssl/x509.h> | |
16 | #include <openssl/x509v3.h> | |
25f2138b | 17 | #include "crypto/x509.h" |
5d7c222d | 18 | |
706457b7 | 19 | #include "x509_local.h" |
4a253652 | 20 | |
852c2ed2 RS |
21 | DEFINE_STACK_OF(ASN1_OBJECT) |
22 | DEFINE_STACK_OF(X509_VERIFY_PARAM) | |
852c2ed2 | 23 | |
5d7c222d DSH |
24 | /* X509_VERIFY_PARAM functions */ |
25 | ||
8abffa4a VD |
26 | #define SET_HOST 0 |
27 | #define ADD_HOST 1 | |
28 | ||
0f113f3e MC |
29 | static char *str_copy(const char *s) |
30 | { | |
31 | return OPENSSL_strdup(s); | |
32 | } | |
33 | ||
34 | static void str_free(char *s) | |
35 | { | |
36 | OPENSSL_free(s); | |
37 | } | |
8abffa4a | 38 | |
9689a6ae | 39 | static int int_x509_param_set_hosts(X509_VERIFY_PARAM *vpm, int mode, |
0f113f3e MC |
40 | const char *name, size_t namelen) |
41 | { | |
42 | char *copy; | |
43 | ||
44 | /* | |
45 | * Refuse names with embedded NUL bytes, except perhaps as final byte. | |
46 | * XXX: Do we need to push an error onto the error stack? | |
47 | */ | |
0982ecaa | 48 | if (namelen == 0 || name == NULL) |
0f113f3e MC |
49 | namelen = name ? strlen(name) : 0; |
50 | else if (name && memchr(name, '\0', namelen > 1 ? namelen - 1 : namelen)) | |
51 | return 0; | |
0982ecaa | 52 | if (namelen > 0 && name[namelen - 1] == '\0') |
0f113f3e MC |
53 | --namelen; |
54 | ||
25aaa98a | 55 | if (mode == SET_HOST) { |
9689a6ae DSH |
56 | sk_OPENSSL_STRING_pop_free(vpm->hosts, str_free); |
57 | vpm->hosts = NULL; | |
0f113f3e MC |
58 | } |
59 | if (name == NULL || namelen == 0) | |
60 | return 1; | |
61 | ||
7644a9ae | 62 | copy = OPENSSL_strndup(name, namelen); |
0f113f3e MC |
63 | if (copy == NULL) |
64 | return 0; | |
65 | ||
9689a6ae DSH |
66 | if (vpm->hosts == NULL && |
67 | (vpm->hosts = sk_OPENSSL_STRING_new_null()) == NULL) { | |
0f113f3e MC |
68 | OPENSSL_free(copy); |
69 | return 0; | |
70 | } | |
71 | ||
9689a6ae | 72 | if (!sk_OPENSSL_STRING_push(vpm->hosts, copy)) { |
0f113f3e | 73 | OPENSSL_free(copy); |
9689a6ae DSH |
74 | if (sk_OPENSSL_STRING_num(vpm->hosts) == 0) { |
75 | sk_OPENSSL_STRING_free(vpm->hosts); | |
76 | vpm->hosts = NULL; | |
0f113f3e MC |
77 | } |
78 | return 0; | |
79 | } | |
80 | ||
81 | return 1; | |
82 | } | |
8abffa4a | 83 | |
5d7c222d DSH |
84 | |
85 | X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void) | |
0f113f3e MC |
86 | { |
87 | X509_VERIFY_PARAM *param; | |
222561fe | 88 | |
b51bce94 | 89 | param = OPENSSL_zalloc(sizeof(*param)); |
f90bc6c5 RS |
90 | if (param == NULL) { |
91 | X509err(X509_F_X509_VERIFY_PARAM_NEW, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 92 | return NULL; |
f90bc6c5 | 93 | } |
234b8af4 | 94 | param->trust = X509_TRUST_DEFAULT; |
f90bc6c5 | 95 | /* param->inh_flags = X509_VP_FLAG_DEFAULT; */ |
234b8af4 F |
96 | param->depth = -1; |
97 | param->auth_level = -1; /* -1 means unset, 0 is explicit */ | |
0f113f3e MC |
98 | return param; |
99 | } | |
5d7c222d DSH |
100 | |
101 | void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param) | |
0f113f3e | 102 | { |
234b8af4 | 103 | if (param == NULL) |
f49baeff | 104 | return; |
234b8af4 F |
105 | sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); |
106 | sk_OPENSSL_STRING_pop_free(param->hosts, str_free); | |
107 | OPENSSL_free(param->peername); | |
108 | OPENSSL_free(param->email); | |
109 | OPENSSL_free(param->ip); | |
0f113f3e MC |
110 | OPENSSL_free(param); |
111 | } | |
5d7c222d | 112 | |
1d97c843 TH |
113 | /*- |
114 | * This function determines how parameters are "inherited" from one structure | |
5d7c222d DSH |
115 | * to another. There are several different ways this can happen. |
116 | * | |
117 | * 1. If a child structure needs to have its values initialized from a parent | |
118 | * they are simply copied across. For example SSL_CTX copied to SSL. | |
119 | * 2. If the structure should take on values only if they are currently unset. | |
120 | * For example the values in an SSL structure will take appropriate value | |
121 | * for SSL servers or clients but only if the application has not set new | |
122 | * ones. | |
123 | * | |
0f113f3e | 124 | * The "inh_flags" field determines how this function behaves. |
5d7c222d DSH |
125 | * |
126 | * Normally any values which are set in the default are not copied from the | |
127 | * destination and verify flags are ORed together. | |
128 | * | |
129 | * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied | |
130 | * to the destination. Effectively the values in "to" become default values | |
131 | * which will be used only if nothing new is set in "from". | |
132 | * | |
133 | * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether | |
134 | * they are set or not. Flags is still Ored though. | |
135 | * | |
136 | * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead | |
137 | * of ORed. | |
138 | * | |
139 | * If X509_VP_FLAG_LOCKED is set then no values are copied. | |
140 | * | |
141 | * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed | |
142 | * after the next call. | |
143 | */ | |
144 | ||
145 | /* Macro to test if a field should be copied from src to dest */ | |
146 | ||
147 | #define test_x509_verify_param_copy(field, def) \ | |
278260bf DDO |
148 | (to_overwrite \ |
149 | || ((src->field != def) && (to_default || (dest->field == def)))) | |
5d7c222d DSH |
150 | |
151 | /* Macro to test and copy a field if necessary */ | |
152 | ||
153 | #define x509_verify_param_copy(field, def) \ | |
278260bf DDO |
154 | if (test_x509_verify_param_copy(field, def)) \ |
155 | dest->field = src->field; | |
5d7c222d DSH |
156 | |
157 | int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, | |
0f113f3e MC |
158 | const X509_VERIFY_PARAM *src) |
159 | { | |
160 | unsigned long inh_flags; | |
161 | int to_default, to_overwrite; | |
0f113f3e MC |
162 | if (!src) |
163 | return 1; | |
0f113f3e MC |
164 | inh_flags = dest->inh_flags | src->inh_flags; |
165 | ||
166 | if (inh_flags & X509_VP_FLAG_ONCE) | |
167 | dest->inh_flags = 0; | |
168 | ||
169 | if (inh_flags & X509_VP_FLAG_LOCKED) | |
170 | return 1; | |
171 | ||
172 | if (inh_flags & X509_VP_FLAG_DEFAULT) | |
173 | to_default = 1; | |
174 | else | |
175 | to_default = 0; | |
176 | ||
177 | if (inh_flags & X509_VP_FLAG_OVERWRITE) | |
178 | to_overwrite = 1; | |
179 | else | |
180 | to_overwrite = 0; | |
181 | ||
182 | x509_verify_param_copy(purpose, 0); | |
0daccd4d | 183 | x509_verify_param_copy(trust, X509_TRUST_DEFAULT); |
0f113f3e | 184 | x509_verify_param_copy(depth, -1); |
fbb82a60 | 185 | x509_verify_param_copy(auth_level, -1); |
0f113f3e MC |
186 | |
187 | /* If overwrite or check time not set, copy across */ | |
188 | ||
189 | if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) { | |
190 | dest->check_time = src->check_time; | |
191 | dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME; | |
192 | /* Don't need to copy flag: that is done below */ | |
193 | } | |
194 | ||
195 | if (inh_flags & X509_VP_FLAG_RESET_FLAGS) | |
196 | dest->flags = 0; | |
197 | ||
198 | dest->flags |= src->flags; | |
199 | ||
200 | if (test_x509_verify_param_copy(policies, NULL)) { | |
201 | if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies)) | |
202 | return 0; | |
203 | } | |
204 | ||
205 | /* Copy the host flags if and only if we're copying the host list */ | |
9689a6ae DSH |
206 | if (test_x509_verify_param_copy(hosts, NULL)) { |
207 | sk_OPENSSL_STRING_pop_free(dest->hosts, str_free); | |
208 | dest->hosts = NULL; | |
209 | if (src->hosts) { | |
210 | dest->hosts = | |
211 | sk_OPENSSL_STRING_deep_copy(src->hosts, str_copy, str_free); | |
212 | if (dest->hosts == NULL) | |
0f113f3e | 213 | return 0; |
9689a6ae | 214 | dest->hostflags = src->hostflags; |
0f113f3e MC |
215 | } |
216 | } | |
217 | ||
9689a6ae DSH |
218 | if (test_x509_verify_param_copy(email, NULL)) { |
219 | if (!X509_VERIFY_PARAM_set1_email(dest, src->email, src->emaillen)) | |
0f113f3e MC |
220 | return 0; |
221 | } | |
222 | ||
9689a6ae DSH |
223 | if (test_x509_verify_param_copy(ip, NULL)) { |
224 | if (!X509_VERIFY_PARAM_set1_ip(dest, src->ip, src->iplen)) | |
0f113f3e MC |
225 | return 0; |
226 | } | |
227 | ||
228 | return 1; | |
229 | } | |
5d7c222d DSH |
230 | |
231 | int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, | |
0f113f3e MC |
232 | const X509_VERIFY_PARAM *from) |
233 | { | |
234 | unsigned long save_flags = to->inh_flags; | |
235 | int ret; | |
236 | to->inh_flags |= X509_VP_FLAG_DEFAULT; | |
237 | ret = X509_VERIFY_PARAM_inherit(to, from); | |
238 | to->inh_flags = save_flags; | |
239 | return ret; | |
240 | } | |
5d7c222d | 241 | |
297c67fc | 242 | static int int_x509_param_set1(char **pdest, size_t *pdestlen, |
0f113f3e MC |
243 | const char *src, size_t srclen) |
244 | { | |
278260bf | 245 | char *tmp; |
0f113f3e | 246 | if (src) { |
01d0e241 | 247 | if (srclen == 0) |
0f113f3e | 248 | srclen = strlen(src); |
01d0e241 | 249 | |
278260bf | 250 | tmp = OPENSSL_malloc(srclen + 1); |
01d0e241 | 251 | if (tmp == NULL) |
0f113f3e | 252 | return 0; |
278260bf DDO |
253 | memcpy(tmp, src, srclen); |
254 | tmp[srclen] = '\0'; /* enforce NUL termination */ | |
0f113f3e MC |
255 | } else { |
256 | tmp = NULL; | |
257 | srclen = 0; | |
258 | } | |
b548a1f1 | 259 | OPENSSL_free(*pdest); |
0f113f3e | 260 | *pdest = tmp; |
01d0e241 | 261 | if (pdestlen != NULL) |
0f113f3e MC |
262 | *pdestlen = srclen; |
263 | return 1; | |
264 | } | |
3bf15e29 | 265 | |
5d7c222d | 266 | int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name) |
0f113f3e | 267 | { |
b548a1f1 | 268 | OPENSSL_free(param->name); |
7644a9ae | 269 | param->name = OPENSSL_strdup(name); |
0f113f3e MC |
270 | if (param->name) |
271 | return 1; | |
272 | return 0; | |
273 | } | |
5d7c222d DSH |
274 | |
275 | int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags) | |
0f113f3e MC |
276 | { |
277 | param->flags |= flags; | |
278 | if (flags & X509_V_FLAG_POLICY_MASK) | |
279 | param->flags |= X509_V_FLAG_POLICY_CHECK; | |
280 | return 1; | |
281 | } | |
282 | ||
283 | int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, | |
284 | unsigned long flags) | |
285 | { | |
286 | param->flags &= ~flags; | |
287 | return 1; | |
288 | } | |
f022c177 | 289 | |
25d7cd1d | 290 | unsigned long X509_VERIFY_PARAM_get_flags(const X509_VERIFY_PARAM *param) |
0f113f3e MC |
291 | { |
292 | return param->flags; | |
293 | } | |
f022c177 | 294 | |
a47bc283 RS |
295 | uint32_t X509_VERIFY_PARAM_get_inh_flags(const X509_VERIFY_PARAM *param) |
296 | { | |
297 | return param->inh_flags; | |
298 | } | |
299 | ||
300 | int X509_VERIFY_PARAM_set_inh_flags(X509_VERIFY_PARAM *param, uint32_t flags) | |
301 | { | |
302 | param->inh_flags = flags; | |
303 | return 1; | |
304 | } | |
305 | ||
5d7c222d | 306 | int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose) |
0f113f3e MC |
307 | { |
308 | return X509_PURPOSE_set(¶m->purpose, purpose); | |
309 | } | |
5d7c222d DSH |
310 | |
311 | int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust) | |
0f113f3e MC |
312 | { |
313 | return X509_TRUST_set(¶m->trust, trust); | |
314 | } | |
5d7c222d DSH |
315 | |
316 | void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth) | |
0f113f3e MC |
317 | { |
318 | param->depth = depth; | |
319 | } | |
5d7c222d | 320 | |
fbb82a60 VD |
321 | void X509_VERIFY_PARAM_set_auth_level(X509_VERIFY_PARAM *param, int auth_level) |
322 | { | |
323 | param->auth_level = auth_level; | |
324 | } | |
325 | ||
329f2f4a RS |
326 | time_t X509_VERIFY_PARAM_get_time(const X509_VERIFY_PARAM *param) |
327 | { | |
328 | return param->check_time; | |
329 | } | |
330 | ||
5d7c222d | 331 | void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t) |
0f113f3e MC |
332 | { |
333 | param->check_time = t; | |
334 | param->flags |= X509_V_FLAG_USE_CHECK_TIME; | |
335 | } | |
336 | ||
337 | int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, | |
338 | ASN1_OBJECT *policy) | |
339 | { | |
12a765a5 | 340 | if (param->policies == NULL) { |
0f113f3e | 341 | param->policies = sk_ASN1_OBJECT_new_null(); |
12a765a5 | 342 | if (param->policies == NULL) |
0f113f3e MC |
343 | return 0; |
344 | } | |
345 | if (!sk_ASN1_OBJECT_push(param->policies, policy)) | |
346 | return 0; | |
347 | return 1; | |
348 | } | |
349 | ||
350 | int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param, | |
351 | STACK_OF(ASN1_OBJECT) *policies) | |
352 | { | |
353 | int i; | |
354 | ASN1_OBJECT *oid, *doid; | |
2ace7450 | 355 | |
12a765a5 | 356 | if (param == NULL) |
0f113f3e | 357 | return 0; |
2ace7450 | 358 | sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); |
0f113f3e | 359 | |
12a765a5 | 360 | if (policies == NULL) { |
0f113f3e MC |
361 | param->policies = NULL; |
362 | return 1; | |
363 | } | |
364 | ||
365 | param->policies = sk_ASN1_OBJECT_new_null(); | |
12a765a5 | 366 | if (param->policies == NULL) |
0f113f3e MC |
367 | return 0; |
368 | ||
369 | for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) { | |
370 | oid = sk_ASN1_OBJECT_value(policies, i); | |
371 | doid = OBJ_dup(oid); | |
372 | if (!doid) | |
373 | return 0; | |
374 | if (!sk_ASN1_OBJECT_push(param->policies, doid)) { | |
375 | ASN1_OBJECT_free(doid); | |
376 | return 0; | |
377 | } | |
378 | } | |
379 | param->flags |= X509_V_FLAG_POLICY_CHECK; | |
380 | return 1; | |
381 | } | |
5d7c222d | 382 | |
278260bf DDO |
383 | char *X509_VERIFY_PARAM_get0_host(X509_VERIFY_PARAM *param, int idx) |
384 | { | |
385 | return sk_OPENSSL_STRING_value(param->hosts, idx); | |
386 | } | |
387 | ||
3bf15e29 | 388 | int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param, |
0f113f3e MC |
389 | const char *name, size_t namelen) |
390 | { | |
9689a6ae | 391 | return int_x509_param_set_hosts(param, SET_HOST, name, namelen); |
0f113f3e | 392 | } |
8abffa4a VD |
393 | |
394 | int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param, | |
0f113f3e MC |
395 | const char *name, size_t namelen) |
396 | { | |
9689a6ae | 397 | return int_x509_param_set_hosts(param, ADD_HOST, name, namelen); |
0f113f3e | 398 | } |
3bf15e29 | 399 | |
397a8e74 | 400 | void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param, |
0f113f3e MC |
401 | unsigned int flags) |
402 | { | |
9689a6ae | 403 | param->hostflags = flags; |
0f113f3e | 404 | } |
397a8e74 | 405 | |
4db296d9 | 406 | unsigned int X509_VERIFY_PARAM_get_hostflags(const X509_VERIFY_PARAM *param) |
5b748dea MC |
407 | { |
408 | return param->hostflags; | |
409 | } | |
410 | ||
8cc86b81 | 411 | char *X509_VERIFY_PARAM_get0_peername(const X509_VERIFY_PARAM *param) |
0f113f3e | 412 | { |
9689a6ae | 413 | return param->peername; |
0f113f3e | 414 | } |
6e661d45 | 415 | |
919ba009 VD |
416 | /* |
417 | * Move peername from one param structure to another, freeing any name present | |
418 | * at the target. If the source is a NULL parameter structure, free and zero | |
419 | * the target peername. | |
420 | */ | |
421 | void X509_VERIFY_PARAM_move_peername(X509_VERIFY_PARAM *to, | |
422 | X509_VERIFY_PARAM *from) | |
423 | { | |
424 | char *peername = (from != NULL) ? from->peername : NULL; | |
425 | ||
426 | if (to->peername != peername) { | |
427 | OPENSSL_free(to->peername); | |
428 | to->peername = peername; | |
429 | } | |
430 | if (from) | |
431 | from->peername = NULL; | |
432 | } | |
433 | ||
278260bf DDO |
434 | char *X509_VERIFY_PARAM_get0_email(X509_VERIFY_PARAM *param) |
435 | { | |
436 | return param->email; | |
437 | } | |
438 | ||
3bf15e29 | 439 | int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param, |
0f113f3e MC |
440 | const char *email, size_t emaillen) |
441 | { | |
9689a6ae | 442 | return int_x509_param_set1(¶m->email, ¶m->emaillen, |
0f113f3e MC |
443 | email, emaillen); |
444 | } | |
3bf15e29 | 445 | |
278260bf DDO |
446 | static unsigned char |
447 | *int_X509_VERIFY_PARAM_get0_ip(X509_VERIFY_PARAM *param, size_t *plen) | |
448 | { | |
449 | if (param == NULL || param->ip == NULL) | |
450 | return NULL; | |
451 | if (plen != NULL) | |
452 | *plen = param->iplen; | |
453 | return param->ip; | |
454 | } | |
455 | ||
456 | char *X509_VERIFY_PARAM_get1_ip_asc(X509_VERIFY_PARAM *param) | |
457 | { | |
458 | size_t iplen; | |
459 | unsigned char *ip = int_X509_VERIFY_PARAM_get0_ip(param, &iplen); | |
460 | ||
461 | return ip == NULL ? NULL : ipaddr_to_asc(ip, iplen); | |
462 | } | |
463 | ||
3bf15e29 | 464 | int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param, |
0f113f3e MC |
465 | const unsigned char *ip, size_t iplen) |
466 | { | |
467 | if (iplen != 0 && iplen != 4 && iplen != 16) | |
468 | return 0; | |
9689a6ae | 469 | return int_x509_param_set1((char **)¶m->ip, ¶m->iplen, |
0f113f3e MC |
470 | (char *)ip, iplen); |
471 | } | |
3bf15e29 DSH |
472 | |
473 | int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc) | |
0f113f3e MC |
474 | { |
475 | unsigned char ipout[16]; | |
476 | size_t iplen; | |
297c67fc | 477 | |
0f113f3e MC |
478 | iplen = (size_t)a2i_ipadd(ipout, ipasc); |
479 | if (iplen == 0) | |
480 | return 0; | |
481 | return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen); | |
482 | } | |
3bf15e29 | 483 | |
5d7c222d | 484 | int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param) |
0f113f3e MC |
485 | { |
486 | return param->depth; | |
487 | } | |
5d7c222d | 488 | |
fbb82a60 VD |
489 | int X509_VERIFY_PARAM_get_auth_level(const X509_VERIFY_PARAM *param) |
490 | { | |
491 | return param->auth_level; | |
492 | } | |
493 | ||
9b3d7570 | 494 | const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param) |
0f113f3e MC |
495 | { |
496 | return param->name; | |
497 | } | |
9b3d7570 | 498 | |
9689a6ae | 499 | #define vpm_empty_id NULL, 0U, NULL, NULL, 0, NULL, 0 |
4a253652 | 500 | |
0f113f3e MC |
501 | /* |
502 | * Default verify parameters: these are used for various applications and can | |
503 | * be overridden by the user specified table. NB: the 'name' field *must* be | |
504 | * in alphabetical order because it will be searched using OBJ_search. | |
5d7c222d DSH |
505 | */ |
506 | ||
507 | static const X509_VERIFY_PARAM default_table[] = { | |
0f113f3e MC |
508 | { |
509 | "default", /* X509 default parameters */ | |
510 | 0, /* Check time */ | |
511 | 0, /* internal flags */ | |
0daccd4d | 512 | X509_V_FLAG_TRUSTED_FIRST, /* flags */ |
0f113f3e MC |
513 | 0, /* purpose */ |
514 | 0, /* trust */ | |
515 | 100, /* depth */ | |
fbb82a60 | 516 | -1, /* auth_level */ |
0f113f3e MC |
517 | NULL, /* policies */ |
518 | vpm_empty_id}, | |
519 | { | |
520 | "pkcs7", /* S/MIME sign parameters */ | |
521 | 0, /* Check time */ | |
522 | 0, /* internal flags */ | |
523 | 0, /* flags */ | |
524 | X509_PURPOSE_SMIME_SIGN, /* purpose */ | |
525 | X509_TRUST_EMAIL, /* trust */ | |
526 | -1, /* depth */ | |
fbb82a60 | 527 | -1, /* auth_level */ |
0f113f3e MC |
528 | NULL, /* policies */ |
529 | vpm_empty_id}, | |
530 | { | |
531 | "smime_sign", /* S/MIME sign parameters */ | |
532 | 0, /* Check time */ | |
533 | 0, /* internal flags */ | |
534 | 0, /* flags */ | |
535 | X509_PURPOSE_SMIME_SIGN, /* purpose */ | |
536 | X509_TRUST_EMAIL, /* trust */ | |
537 | -1, /* depth */ | |
fbb82a60 | 538 | -1, /* auth_level */ |
0f113f3e MC |
539 | NULL, /* policies */ |
540 | vpm_empty_id}, | |
541 | { | |
542 | "ssl_client", /* SSL/TLS client parameters */ | |
543 | 0, /* Check time */ | |
544 | 0, /* internal flags */ | |
545 | 0, /* flags */ | |
546 | X509_PURPOSE_SSL_CLIENT, /* purpose */ | |
547 | X509_TRUST_SSL_CLIENT, /* trust */ | |
548 | -1, /* depth */ | |
fbb82a60 | 549 | -1, /* auth_level */ |
0f113f3e MC |
550 | NULL, /* policies */ |
551 | vpm_empty_id}, | |
552 | { | |
553 | "ssl_server", /* SSL/TLS server parameters */ | |
554 | 0, /* Check time */ | |
555 | 0, /* internal flags */ | |
556 | 0, /* flags */ | |
557 | X509_PURPOSE_SSL_SERVER, /* purpose */ | |
558 | X509_TRUST_SSL_SERVER, /* trust */ | |
559 | -1, /* depth */ | |
fbb82a60 | 560 | -1, /* auth_level */ |
0f113f3e MC |
561 | NULL, /* policies */ |
562 | vpm_empty_id} | |
563 | }; | |
5d7c222d DSH |
564 | |
565 | static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL; | |
566 | ||
babb3798 | 567 | static int table_cmp(const X509_VERIFY_PARAM *a, const X509_VERIFY_PARAM *b) |
0f113f3e MC |
568 | { |
569 | return strcmp(a->name, b->name); | |
570 | } | |
babb3798 | 571 | |
0f113f3e MC |
572 | DECLARE_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table); |
573 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(X509_VERIFY_PARAM, X509_VERIFY_PARAM, table); | |
babb3798 | 574 | |
0f113f3e MC |
575 | static int param_cmp(const X509_VERIFY_PARAM *const *a, |
576 | const X509_VERIFY_PARAM *const *b) | |
577 | { | |
578 | return strcmp((*a)->name, (*b)->name); | |
579 | } | |
5d7c222d DSH |
580 | |
581 | int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param) | |
0f113f3e MC |
582 | { |
583 | int idx; | |
584 | X509_VERIFY_PARAM *ptmp; | |
90945fa3 | 585 | if (param_table == NULL) { |
0f113f3e | 586 | param_table = sk_X509_VERIFY_PARAM_new(param_cmp); |
90945fa3 | 587 | if (param_table == NULL) |
0f113f3e MC |
588 | return 0; |
589 | } else { | |
590 | idx = sk_X509_VERIFY_PARAM_find(param_table, param); | |
5b37fef0 AP |
591 | if (idx >= 0) { |
592 | ptmp = sk_X509_VERIFY_PARAM_delete(param_table, idx); | |
0f113f3e | 593 | X509_VERIFY_PARAM_free(ptmp); |
0f113f3e MC |
594 | } |
595 | } | |
596 | if (!sk_X509_VERIFY_PARAM_push(param_table, param)) | |
597 | return 0; | |
598 | return 1; | |
599 | } | |
5d7c222d | 600 | |
9b3d7570 | 601 | int X509_VERIFY_PARAM_get_count(void) |
0f113f3e | 602 | { |
b6eb9827 | 603 | int num = OSSL_NELEM(default_table); |
0f113f3e MC |
604 | if (param_table) |
605 | num += sk_X509_VERIFY_PARAM_num(param_table); | |
606 | return num; | |
607 | } | |
9b3d7570 DSH |
608 | |
609 | const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id) | |
0f113f3e | 610 | { |
b6eb9827 | 611 | int num = OSSL_NELEM(default_table); |
0f113f3e MC |
612 | if (id < num) |
613 | return default_table + id; | |
614 | return sk_X509_VERIFY_PARAM_value(param_table, id - num); | |
615 | } | |
9b3d7570 | 616 | |
5d7c222d | 617 | const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name) |
0f113f3e MC |
618 | { |
619 | int idx; | |
620 | X509_VERIFY_PARAM pm; | |
621 | ||
622 | pm.name = (char *)name; | |
5b37fef0 | 623 | if (param_table != NULL) { |
0f113f3e | 624 | idx = sk_X509_VERIFY_PARAM_find(param_table, &pm); |
5b37fef0 | 625 | if (idx >= 0) |
0f113f3e MC |
626 | return sk_X509_VERIFY_PARAM_value(param_table, idx); |
627 | } | |
b6eb9827 | 628 | return OBJ_bsearch_table(&pm, default_table, OSSL_NELEM(default_table)); |
0f113f3e | 629 | } |
5d7c222d DSH |
630 | |
631 | void X509_VERIFY_PARAM_table_cleanup(void) | |
0f113f3e | 632 | { |
222561fe | 633 | sk_X509_VERIFY_PARAM_pop_free(param_table, X509_VERIFY_PARAM_free); |
0f113f3e MC |
634 | param_table = NULL; |
635 | } |