]>
Commit | Line | Data |
---|---|---|
673b102c | 1 | /* v3_purp.c */ |
0f113f3e MC |
2 | /* |
3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
4 | * 2001. | |
673b102c DSH |
5 | */ |
6 | /* ==================================================================== | |
bc501570 | 7 | * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. |
673b102c DSH |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
12 | * | |
13 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 14 | * notice, this list of conditions and the following disclaimer. |
673b102c DSH |
15 | * |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in | |
18 | * the documentation and/or other materials provided with the | |
19 | * distribution. | |
20 | * | |
21 | * 3. All advertising materials mentioning features or use of this | |
22 | * software must display the following acknowledgment: | |
23 | * "This product includes software developed by the OpenSSL Project | |
24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
25 | * | |
26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
27 | * endorse or promote products derived from this software without | |
28 | * prior written permission. For written permission, please contact | |
29 | * licensing@OpenSSL.org. | |
30 | * | |
31 | * 5. Products derived from this software may not be called "OpenSSL" | |
32 | * nor may "OpenSSL" appear in their names without prior written | |
33 | * permission of the OpenSSL Project. | |
34 | * | |
35 | * 6. Redistributions of any form whatsoever must retain the following | |
36 | * acknowledgment: | |
37 | * "This product includes software developed by the OpenSSL Project | |
38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
39 | * | |
40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
52 | * ==================================================================== | |
53 | * | |
54 | * This product includes cryptographic software written by Eric Young | |
55 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
60 | #include <stdio.h> | |
61 | #include "cryptlib.h" | |
62 | #include <openssl/x509v3.h> | |
2f043896 | 63 | #include <openssl/x509_vfy.h> |
673b102c | 64 | |
ce1b4fe1 | 65 | static void x509v3_cache_extensions(X509 *x); |
673b102c | 66 | |
ccd86b68 | 67 | static int check_ssl_ca(const X509 *x); |
0f113f3e MC |
68 | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
69 | int ca); | |
70 | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, | |
71 | int ca); | |
72 | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, | |
73 | int ca); | |
ccd86b68 | 74 | static int purpose_smime(const X509 *x, int ca); |
0f113f3e MC |
75 | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
76 | int ca); | |
77 | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, | |
78 | int ca); | |
79 | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, | |
80 | int ca); | |
81 | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, | |
82 | int ca); | |
ccd86b68 | 83 | static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca); |
81f169e9 | 84 | static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca); |
ccd86b68 | 85 | |
0f113f3e | 86 | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b); |
d4cec6a1 | 87 | static void xptable_free(X509_PURPOSE *p); |
673b102c DSH |
88 | |
89 | static X509_PURPOSE xstandard[] = { | |
0f113f3e MC |
90 | {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, |
91 | check_purpose_ssl_client, "SSL client", "sslclient", NULL}, | |
92 | {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, | |
93 | check_purpose_ssl_server, "SSL server", "sslserver", NULL}, | |
94 | {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, | |
95 | check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL}, | |
96 | {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, | |
97 | "S/MIME signing", "smimesign", NULL}, | |
98 | {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, | |
99 | check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL}, | |
100 | {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, | |
101 | "CRL signing", "crlsign", NULL}, | |
102 | {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any", | |
103 | NULL}, | |
104 | {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, | |
105 | "OCSP helper", "ocsphelper", NULL}, | |
106 | {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, | |
107 | check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign", | |
108 | NULL}, | |
673b102c DSH |
109 | }; |
110 | ||
b6eb9827 | 111 | #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard) |
dd413410 | 112 | |
79875776 | 113 | static STACK_OF(X509_PURPOSE) *xptable = NULL; |
673b102c | 114 | |
0f113f3e | 115 | static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b) |
673b102c | 116 | { |
0f113f3e | 117 | return (*a)->purpose - (*b)->purpose; |
673b102c DSH |
118 | } |
119 | ||
0f113f3e MC |
120 | /* |
121 | * As much as I'd like to make X509_check_purpose use a "const" X509* I | |
122 | * really can't because it does recalculate hashes and do other non-const | |
123 | * things. | |
124 | */ | |
673b102c DSH |
125 | int X509_check_purpose(X509 *x, int id, int ca) |
126 | { | |
0f113f3e MC |
127 | int idx; |
128 | const X509_PURPOSE *pt; | |
129 | if (!(x->ex_flags & EXFLAG_SET)) { | |
130 | CRYPTO_w_lock(CRYPTO_LOCK_X509); | |
131 | x509v3_cache_extensions(x); | |
132 | CRYPTO_w_unlock(CRYPTO_LOCK_X509); | |
133 | } | |
134 | if (id == -1) | |
135 | return 1; | |
136 | idx = X509_PURPOSE_get_by_id(id); | |
137 | if (idx == -1) | |
138 | return -1; | |
139 | pt = X509_PURPOSE_get0(idx); | |
140 | return pt->check_purpose(pt, x, ca); | |
673b102c | 141 | } |
e947f396 | 142 | |
926a56bf DSH |
143 | int X509_PURPOSE_set(int *p, int purpose) |
144 | { | |
0f113f3e MC |
145 | if (X509_PURPOSE_get_by_id(purpose) == -1) { |
146 | X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE); | |
147 | return 0; | |
148 | } | |
149 | *p = purpose; | |
150 | return 1; | |
926a56bf DSH |
151 | } |
152 | ||
d4cec6a1 DSH |
153 | int X509_PURPOSE_get_count(void) |
154 | { | |
0f113f3e MC |
155 | if (!xptable) |
156 | return X509_PURPOSE_COUNT; | |
157 | return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT; | |
d4cec6a1 | 158 | } |
ce1b4fe1 | 159 | |
0f113f3e | 160 | X509_PURPOSE *X509_PURPOSE_get0(int idx) |
d4cec6a1 | 161 | { |
0f113f3e MC |
162 | if (idx < 0) |
163 | return NULL; | |
164 | if (idx < (int)X509_PURPOSE_COUNT) | |
165 | return xstandard + idx; | |
166 | return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT); | |
d4cec6a1 DSH |
167 | } |
168 | ||
169 | int X509_PURPOSE_get_by_sname(char *sname) | |
170 | { | |
0f113f3e MC |
171 | int i; |
172 | X509_PURPOSE *xptmp; | |
173 | for (i = 0; i < X509_PURPOSE_get_count(); i++) { | |
174 | xptmp = X509_PURPOSE_get0(i); | |
175 | if (!strcmp(xptmp->sname, sname)) | |
176 | return i; | |
177 | } | |
178 | return -1; | |
d4cec6a1 | 179 | } |
673b102c | 180 | |
13938ace | 181 | int X509_PURPOSE_get_by_id(int purpose) |
673b102c | 182 | { |
0f113f3e MC |
183 | X509_PURPOSE tmp; |
184 | int idx; | |
185 | if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX)) | |
186 | return purpose - X509_PURPOSE_MIN; | |
187 | tmp.purpose = purpose; | |
188 | if (!xptable) | |
189 | return -1; | |
190 | idx = sk_X509_PURPOSE_find(xptable, &tmp); | |
191 | if (idx == -1) | |
192 | return -1; | |
193 | return idx + X509_PURPOSE_COUNT; | |
673b102c DSH |
194 | } |
195 | ||
dd413410 | 196 | int X509_PURPOSE_add(int id, int trust, int flags, |
0f113f3e MC |
197 | int (*ck) (const X509_PURPOSE *, const X509 *, int), |
198 | char *name, char *sname, void *arg) | |
199 | { | |
200 | int idx; | |
201 | X509_PURPOSE *ptmp; | |
202 | /* | |
203 | * This is set according to what we change: application can't set it | |
204 | */ | |
205 | flags &= ~X509_PURPOSE_DYNAMIC; | |
206 | /* This will always be set for application modified trust entries */ | |
207 | flags |= X509_PURPOSE_DYNAMIC_NAME; | |
208 | /* Get existing entry if any */ | |
209 | idx = X509_PURPOSE_get_by_id(id); | |
210 | /* Need a new entry */ | |
211 | if (idx == -1) { | |
b4faea50 | 212 | if (!(ptmp = OPENSSL_malloc(sizeof(*ptmp)))) { |
0f113f3e MC |
213 | X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE); |
214 | return 0; | |
215 | } | |
216 | ptmp->flags = X509_PURPOSE_DYNAMIC; | |
217 | } else | |
218 | ptmp = X509_PURPOSE_get0(idx); | |
219 | ||
220 | /* OPENSSL_free existing name if dynamic */ | |
221 | if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) { | |
222 | OPENSSL_free(ptmp->name); | |
223 | OPENSSL_free(ptmp->sname); | |
224 | } | |
225 | /* dup supplied name */ | |
226 | ptmp->name = BUF_strdup(name); | |
227 | ptmp->sname = BUF_strdup(sname); | |
228 | if (!ptmp->name || !ptmp->sname) { | |
229 | X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE); | |
230 | return 0; | |
231 | } | |
232 | /* Keep the dynamic flag of existing entry */ | |
233 | ptmp->flags &= X509_PURPOSE_DYNAMIC; | |
234 | /* Set all other flags */ | |
235 | ptmp->flags |= flags; | |
236 | ||
237 | ptmp->purpose = id; | |
238 | ptmp->trust = trust; | |
239 | ptmp->check_purpose = ck; | |
240 | ptmp->usr_data = arg; | |
241 | ||
242 | /* If its a new entry manage the dynamic table */ | |
243 | if (idx == -1) { | |
244 | if (!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) { | |
245 | X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE); | |
246 | return 0; | |
247 | } | |
248 | if (!sk_X509_PURPOSE_push(xptable, ptmp)) { | |
249 | X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE); | |
250 | return 0; | |
251 | } | |
252 | } | |
253 | return 1; | |
673b102c DSH |
254 | } |
255 | ||
79875776 | 256 | static void xptable_free(X509_PURPOSE *p) |
0f113f3e MC |
257 | { |
258 | if (!p) | |
259 | return; | |
260 | if (p->flags & X509_PURPOSE_DYNAMIC) { | |
261 | if (p->flags & X509_PURPOSE_DYNAMIC_NAME) { | |
262 | OPENSSL_free(p->name); | |
263 | OPENSSL_free(p->sname); | |
264 | } | |
265 | OPENSSL_free(p); | |
266 | } | |
267 | } | |
79875776 BM |
268 | |
269 | void X509_PURPOSE_cleanup(void) | |
270 | { | |
0f113f3e MC |
271 | unsigned int i; |
272 | sk_X509_PURPOSE_pop_free(xptable, xptable_free); | |
273 | for (i = 0; i < X509_PURPOSE_COUNT; i++) | |
274 | xptable_free(xstandard + i); | |
275 | xptable = NULL; | |
79875776 BM |
276 | } |
277 | ||
d4cec6a1 | 278 | int X509_PURPOSE_get_id(X509_PURPOSE *xp) |
673b102c | 279 | { |
0f113f3e | 280 | return xp->purpose; |
673b102c DSH |
281 | } |
282 | ||
c7cb16a8 | 283 | char *X509_PURPOSE_get0_name(X509_PURPOSE *xp) |
d4cec6a1 | 284 | { |
0f113f3e | 285 | return xp->name; |
d4cec6a1 | 286 | } |
673b102c | 287 | |
c7cb16a8 | 288 | char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp) |
673b102c | 289 | { |
0f113f3e | 290 | return xp->sname; |
673b102c DSH |
291 | } |
292 | ||
d4cec6a1 | 293 | int X509_PURPOSE_get_trust(X509_PURPOSE *xp) |
673b102c | 294 | { |
0f113f3e | 295 | return xp->trust; |
673b102c DSH |
296 | } |
297 | ||
babb3798 | 298 | static int nid_cmp(const int *a, const int *b) |
0f113f3e MC |
299 | { |
300 | return *a - *b; | |
301 | } | |
f1558bb4 | 302 | |
e19106f5 DSH |
303 | DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid); |
304 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid); | |
babb3798 | 305 | |
f1558bb4 | 306 | int X509_supported_extension(X509_EXTENSION *ex) |
0f113f3e MC |
307 | { |
308 | /* | |
309 | * This table is a list of the NIDs of supported extensions: that is | |
310 | * those which are used by the verify process. If an extension is | |
311 | * critical and doesn't appear in this list then the verify process will | |
312 | * normally reject the certificate. The list must be kept in numerical | |
313 | * order because it will be searched using bsearch. | |
314 | */ | |
315 | ||
316 | static const int supported_nids[] = { | |
317 | NID_netscape_cert_type, /* 71 */ | |
318 | NID_key_usage, /* 83 */ | |
319 | NID_subject_alt_name, /* 85 */ | |
320 | NID_basic_constraints, /* 87 */ | |
321 | NID_certificate_policies, /* 89 */ | |
322 | NID_ext_key_usage, /* 126 */ | |
0f113f3e MC |
323 | NID_sbgp_ipAddrBlock, /* 290 */ |
324 | NID_sbgp_autonomousSysNum, /* 291 */ | |
0f113f3e MC |
325 | NID_policy_constraints, /* 401 */ |
326 | NID_proxyCertInfo, /* 663 */ | |
327 | NID_name_constraints, /* 666 */ | |
328 | NID_policy_mappings, /* 747 */ | |
329 | NID_inhibit_any_policy /* 748 */ | |
330 | }; | |
331 | ||
332 | int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); | |
333 | ||
334 | if (ex_nid == NID_undef) | |
335 | return 0; | |
336 | ||
b6eb9827 | 337 | if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids))) |
0f113f3e MC |
338 | return 1; |
339 | return 0; | |
340 | } | |
3e727a3b DSH |
341 | |
342 | static void setup_dp(X509 *x, DIST_POINT *dp) | |
0f113f3e MC |
343 | { |
344 | X509_NAME *iname = NULL; | |
345 | int i; | |
346 | if (dp->reasons) { | |
347 | if (dp->reasons->length > 0) | |
348 | dp->dp_reasons = dp->reasons->data[0]; | |
349 | if (dp->reasons->length > 1) | |
350 | dp->dp_reasons |= (dp->reasons->data[1] << 8); | |
351 | dp->dp_reasons &= CRLDP_ALL_REASONS; | |
352 | } else | |
353 | dp->dp_reasons = CRLDP_ALL_REASONS; | |
354 | if (!dp->distpoint || (dp->distpoint->type != 1)) | |
355 | return; | |
356 | for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { | |
357 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); | |
358 | if (gen->type == GEN_DIRNAME) { | |
359 | iname = gen->d.directoryName; | |
360 | break; | |
361 | } | |
362 | } | |
363 | if (!iname) | |
364 | iname = X509_get_issuer_name(x); | |
365 | ||
366 | DIST_POINT_set_dpname(dp->distpoint, iname); | |
367 | ||
368 | } | |
3e727a3b DSH |
369 | |
370 | static void setup_crldp(X509 *x) | |
0f113f3e MC |
371 | { |
372 | int i; | |
373 | x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL); | |
374 | for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) | |
375 | setup_dp(x, sk_DIST_POINT_value(x->crldp, i)); | |
376 | } | |
f1558bb4 | 377 | |
ce1b4fe1 | 378 | static void x509v3_cache_extensions(X509 *x) |
673b102c | 379 | { |
0f113f3e MC |
380 | BASIC_CONSTRAINTS *bs; |
381 | PROXY_CERT_INFO_EXTENSION *pci; | |
382 | ASN1_BIT_STRING *usage; | |
383 | ASN1_BIT_STRING *ns; | |
384 | EXTENDED_KEY_USAGE *extusage; | |
385 | X509_EXTENSION *ex; | |
386 | ||
387 | int i; | |
388 | if (x->ex_flags & EXFLAG_SET) | |
389 | return; | |
0f113f3e | 390 | X509_digest(x, EVP_sha1(), x->sha1_hash, NULL); |
0f113f3e MC |
391 | /* V1 should mean no extensions ... */ |
392 | if (!X509_get_version(x)) | |
393 | x->ex_flags |= EXFLAG_V1; | |
394 | /* Handle basic constraints */ | |
395 | if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) { | |
396 | if (bs->ca) | |
397 | x->ex_flags |= EXFLAG_CA; | |
398 | if (bs->pathlen) { | |
399 | if ((bs->pathlen->type == V_ASN1_NEG_INTEGER) | |
400 | || !bs->ca) { | |
401 | x->ex_flags |= EXFLAG_INVALID; | |
402 | x->ex_pathlen = 0; | |
403 | } else | |
404 | x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen); | |
405 | } else | |
406 | x->ex_pathlen = -1; | |
407 | BASIC_CONSTRAINTS_free(bs); | |
408 | x->ex_flags |= EXFLAG_BCONS; | |
409 | } | |
410 | /* Handle proxy certificates */ | |
411 | if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) { | |
412 | if (x->ex_flags & EXFLAG_CA | |
413 | || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0 | |
414 | || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) { | |
415 | x->ex_flags |= EXFLAG_INVALID; | |
416 | } | |
417 | if (pci->pcPathLengthConstraint) { | |
418 | x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint); | |
419 | } else | |
420 | x->ex_pcpathlen = -1; | |
421 | PROXY_CERT_INFO_EXTENSION_free(pci); | |
422 | x->ex_flags |= EXFLAG_PROXY; | |
423 | } | |
424 | /* Handle key usage */ | |
425 | if ((usage = X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) { | |
426 | if (usage->length > 0) { | |
427 | x->ex_kusage = usage->data[0]; | |
428 | if (usage->length > 1) | |
429 | x->ex_kusage |= usage->data[1] << 8; | |
430 | } else | |
431 | x->ex_kusage = 0; | |
432 | x->ex_flags |= EXFLAG_KUSAGE; | |
433 | ASN1_BIT_STRING_free(usage); | |
434 | } | |
435 | x->ex_xkusage = 0; | |
436 | if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) { | |
437 | x->ex_flags |= EXFLAG_XKUSAGE; | |
438 | for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { | |
439 | switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { | |
440 | case NID_server_auth: | |
441 | x->ex_xkusage |= XKU_SSL_SERVER; | |
442 | break; | |
443 | ||
444 | case NID_client_auth: | |
445 | x->ex_xkusage |= XKU_SSL_CLIENT; | |
446 | break; | |
447 | ||
448 | case NID_email_protect: | |
449 | x->ex_xkusage |= XKU_SMIME; | |
450 | break; | |
451 | ||
452 | case NID_code_sign: | |
453 | x->ex_xkusage |= XKU_CODE_SIGN; | |
454 | break; | |
455 | ||
456 | case NID_ms_sgc: | |
457 | case NID_ns_sgc: | |
458 | x->ex_xkusage |= XKU_SGC; | |
459 | break; | |
460 | ||
461 | case NID_OCSP_sign: | |
462 | x->ex_xkusage |= XKU_OCSP_SIGN; | |
463 | break; | |
464 | ||
465 | case NID_time_stamp: | |
466 | x->ex_xkusage |= XKU_TIMESTAMP; | |
467 | break; | |
468 | ||
469 | case NID_dvcs: | |
470 | x->ex_xkusage |= XKU_DVCS; | |
471 | break; | |
472 | ||
473 | case NID_anyExtendedKeyUsage: | |
474 | x->ex_xkusage |= XKU_ANYEKU; | |
475 | break; | |
476 | } | |
477 | } | |
478 | sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); | |
479 | } | |
480 | ||
481 | if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) { | |
482 | if (ns->length > 0) | |
483 | x->ex_nscert = ns->data[0]; | |
484 | else | |
485 | x->ex_nscert = 0; | |
486 | x->ex_flags |= EXFLAG_NSCERT; | |
487 | ASN1_BIT_STRING_free(ns); | |
488 | } | |
489 | x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL); | |
490 | x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL); | |
491 | /* Does subject name match issuer ? */ | |
492 | if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) { | |
493 | x->ex_flags |= EXFLAG_SI; | |
494 | /* If SKID matches AKID also indicate self signed */ | |
495 | if (X509_check_akid(x, x->akid) == X509_V_OK) | |
496 | x->ex_flags |= EXFLAG_SS; | |
497 | } | |
498 | x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); | |
499 | x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL); | |
500 | if (!x->nc && (i != -1)) | |
501 | x->ex_flags |= EXFLAG_INVALID; | |
502 | setup_crldp(x); | |
d43c4497 | 503 | |
0f113f3e MC |
504 | x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, NULL, NULL); |
505 | x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, | |
506 | NULL, NULL); | |
0f113f3e MC |
507 | for (i = 0; i < X509_get_ext_count(x); i++) { |
508 | ex = X509_get_ext(x, i); | |
509 | if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) | |
510 | == NID_freshest_crl) | |
511 | x->ex_flags |= EXFLAG_FRESHEST; | |
512 | if (!X509_EXTENSION_get_critical(ex)) | |
513 | continue; | |
514 | if (!X509_supported_extension(ex)) { | |
515 | x->ex_flags |= EXFLAG_CRITICAL; | |
516 | break; | |
517 | } | |
518 | } | |
519 | x->ex_flags |= EXFLAG_SET; | |
673b102c DSH |
520 | } |
521 | ||
1d97c843 TH |
522 | /*- |
523 | * CA checks common to all purposes | |
673b102c DSH |
524 | * return codes: |
525 | * 0 not a CA | |
526 | * 1 is a CA | |
527 | * 2 basicConstraints absent so "maybe" a CA | |
528 | * 3 basicConstraints absent but self signed V1. | |
bc501570 | 529 | * 4 basicConstraints absent but keyUsage present and keyCertSign asserted. |
673b102c DSH |
530 | */ |
531 | ||
532 | #define V1_ROOT (EXFLAG_V1|EXFLAG_SS) | |
533 | #define ku_reject(x, usage) \ | |
0f113f3e | 534 | (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) |
673b102c | 535 | #define xku_reject(x, usage) \ |
0f113f3e | 536 | (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage))) |
673b102c | 537 | #define ns_reject(x, usage) \ |
0f113f3e | 538 | (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage))) |
673b102c | 539 | |
5073ff03 | 540 | static int check_ca(const X509 *x) |
673b102c | 541 | { |
0f113f3e MC |
542 | /* keyUsage if present should allow cert signing */ |
543 | if (ku_reject(x, KU_KEY_CERT_SIGN)) | |
544 | return 0; | |
545 | if (x->ex_flags & EXFLAG_BCONS) { | |
546 | if (x->ex_flags & EXFLAG_CA) | |
547 | return 1; | |
548 | /* If basicConstraints says not a CA then say so */ | |
549 | else | |
550 | return 0; | |
551 | } else { | |
552 | /* we support V1 roots for... uh, I don't really know why. */ | |
553 | if ((x->ex_flags & V1_ROOT) == V1_ROOT) | |
554 | return 3; | |
555 | /* | |
556 | * If key usage present it must have certSign so tolerate it | |
557 | */ | |
558 | else if (x->ex_flags & EXFLAG_KUSAGE) | |
559 | return 4; | |
560 | /* Older certificates could have Netscape-specific CA types */ | |
561 | else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA) | |
562 | return 5; | |
563 | /* can this still be regarded a CA certificate? I doubt it */ | |
564 | return 0; | |
565 | } | |
673b102c DSH |
566 | } |
567 | ||
5073ff03 RL |
568 | int X509_check_ca(X509 *x) |
569 | { | |
0f113f3e MC |
570 | if (!(x->ex_flags & EXFLAG_SET)) { |
571 | CRYPTO_w_lock(CRYPTO_LOCK_X509); | |
572 | x509v3_cache_extensions(x); | |
573 | CRYPTO_w_unlock(CRYPTO_LOCK_X509); | |
574 | } | |
5073ff03 | 575 | |
0f113f3e | 576 | return check_ca(x); |
5073ff03 RL |
577 | } |
578 | ||
0cb957a6 | 579 | /* Check SSL CA: common checks for SSL client and server */ |
ccd86b68 | 580 | static int check_ssl_ca(const X509 *x) |
0cb957a6 | 581 | { |
0f113f3e MC |
582 | int ca_ret; |
583 | ca_ret = check_ca(x); | |
584 | if (!ca_ret) | |
585 | return 0; | |
586 | /* check nsCertType if present */ | |
587 | if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA) | |
588 | return ca_ret; | |
589 | else | |
590 | return 0; | |
0cb957a6 | 591 | } |
8cff6331 | 592 | |
0f113f3e MC |
593 | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
594 | int ca) | |
673b102c | 595 | { |
0f113f3e MC |
596 | if (xku_reject(x, XKU_SSL_CLIENT)) |
597 | return 0; | |
598 | if (ca) | |
599 | return check_ssl_ca(x); | |
600 | /* We need to do digital signatures or key agreement */ | |
601 | if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT)) | |
602 | return 0; | |
603 | /* nsCertType if present should allow SSL client use */ | |
604 | if (ns_reject(x, NS_SSL_CLIENT)) | |
605 | return 0; | |
606 | return 1; | |
673b102c | 607 | } |
0f113f3e MC |
608 | |
609 | /* | |
610 | * Key usage needed for TLS/SSL server: digital signature, encipherment or | |
7568d15a DSH |
611 | * key agreement. The ssl code can check this more thoroughly for individual |
612 | * key types. | |
613 | */ | |
614 | #define KU_TLS \ | |
0f113f3e | 615 | KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT |
673b102c | 616 | |
0f113f3e MC |
617 | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
618 | int ca) | |
673b102c | 619 | { |
0f113f3e MC |
620 | if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC)) |
621 | return 0; | |
622 | if (ca) | |
623 | return check_ssl_ca(x); | |
624 | ||
625 | if (ns_reject(x, NS_SSL_SERVER)) | |
626 | return 0; | |
627 | if (ku_reject(x, KU_TLS)) | |
628 | return 0; | |
673b102c | 629 | |
0f113f3e | 630 | return 1; |
673b102c DSH |
631 | |
632 | } | |
633 | ||
0f113f3e MC |
634 | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
635 | int ca) | |
673b102c | 636 | { |
0f113f3e MC |
637 | int ret; |
638 | ret = check_purpose_ssl_server(xp, x, ca); | |
639 | if (!ret || ca) | |
640 | return ret; | |
641 | /* We need to encipher or Netscape complains */ | |
642 | if (ku_reject(x, KU_KEY_ENCIPHERMENT)) | |
643 | return 0; | |
644 | return ret; | |
673b102c DSH |
645 | } |
646 | ||
647 | /* common S/MIME checks */ | |
ccd86b68 | 648 | static int purpose_smime(const X509 *x, int ca) |
673b102c | 649 | { |
0f113f3e MC |
650 | if (xku_reject(x, XKU_SMIME)) |
651 | return 0; | |
652 | if (ca) { | |
653 | int ca_ret; | |
654 | ca_ret = check_ca(x); | |
655 | if (!ca_ret) | |
656 | return 0; | |
657 | /* check nsCertType if present */ | |
658 | if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA) | |
659 | return ca_ret; | |
660 | else | |
661 | return 0; | |
662 | } | |
663 | if (x->ex_flags & EXFLAG_NSCERT) { | |
664 | if (x->ex_nscert & NS_SMIME) | |
665 | return 1; | |
666 | /* Workaround for some buggy certificates */ | |
667 | if (x->ex_nscert & NS_SSL_CLIENT) | |
668 | return 2; | |
669 | return 0; | |
670 | } | |
671 | return 1; | |
673b102c DSH |
672 | } |
673 | ||
0f113f3e MC |
674 | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
675 | int ca) | |
673b102c | 676 | { |
0f113f3e MC |
677 | int ret; |
678 | ret = purpose_smime(x, ca); | |
679 | if (!ret || ca) | |
680 | return ret; | |
681 | if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION)) | |
682 | return 0; | |
683 | return ret; | |
673b102c DSH |
684 | } |
685 | ||
0f113f3e MC |
686 | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
687 | int ca) | |
673b102c | 688 | { |
0f113f3e MC |
689 | int ret; |
690 | ret = purpose_smime(x, ca); | |
691 | if (!ret || ca) | |
692 | return ret; | |
693 | if (ku_reject(x, KU_KEY_ENCIPHERMENT)) | |
694 | return 0; | |
695 | return ret; | |
673b102c DSH |
696 | } |
697 | ||
0f113f3e MC |
698 | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
699 | int ca) | |
673b102c | 700 | { |
0f113f3e MC |
701 | if (ca) { |
702 | int ca_ret; | |
703 | if ((ca_ret = check_ca(x)) != 2) | |
704 | return ca_ret; | |
705 | else | |
706 | return 0; | |
707 | } | |
708 | if (ku_reject(x, KU_CRL_SIGN)) | |
709 | return 0; | |
710 | return 1; | |
673b102c | 711 | } |
068fdce8 | 712 | |
0f113f3e MC |
713 | /* |
714 | * OCSP helper: this is *not* a full OCSP check. It just checks that each CA | |
715 | * is valid. Additional checks must be made on the chain. | |
81f169e9 DSH |
716 | */ |
717 | ||
718 | static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca) | |
719 | { | |
0f113f3e MC |
720 | /* |
721 | * Must be a valid CA. Should we really support the "I don't know" value | |
722 | * (2)? | |
723 | */ | |
724 | if (ca) | |
725 | return check_ca(x); | |
726 | /* leaf certificate is checked in OCSP_verify() */ | |
727 | return 1; | |
81f169e9 DSH |
728 | } |
729 | ||
c7235be6 | 730 | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
0f113f3e | 731 | int ca) |
c7235be6 | 732 | { |
0f113f3e MC |
733 | int i_ext; |
734 | ||
735 | /* If ca is true we must return if this is a valid CA certificate. */ | |
736 | if (ca) | |
737 | return check_ca(x); | |
738 | ||
739 | /* | |
740 | * Check the optional key usage field: | |
741 | * if Key Usage is present, it must be one of digitalSignature | |
742 | * and/or nonRepudiation (other values are not consistent and shall | |
743 | * be rejected). | |
744 | */ | |
745 | if ((x->ex_flags & EXFLAG_KUSAGE) | |
746 | && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || | |
747 | !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) | |
748 | return 0; | |
749 | ||
750 | /* Only time stamp key usage is permitted and it's required. */ | |
751 | if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP) | |
752 | return 0; | |
753 | ||
754 | /* Extended Key Usage MUST be critical */ | |
755 | i_ext = X509_get_ext_by_NID((X509 *)x, NID_ext_key_usage, -1); | |
756 | if (i_ext >= 0) { | |
757 | X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext); | |
758 | if (!X509_EXTENSION_get_critical(ext)) | |
759 | return 0; | |
760 | } | |
761 | ||
762 | return 1; | |
c7235be6 UM |
763 | } |
764 | ||
ccd86b68 | 765 | static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca) |
068fdce8 | 766 | { |
0f113f3e | 767 | return 1; |
068fdce8 | 768 | } |
2f043896 | 769 | |
1d97c843 TH |
770 | /*- |
771 | * Various checks to see if one certificate issued the second. | |
2f043896 DSH |
772 | * This can be used to prune a set of possible issuer certificates |
773 | * which have been looked up using some simple method such as by | |
774 | * subject name. | |
775 | * These are: | |
776 | * 1. Check issuer_name(subject) == subject_name(issuer) | |
777 | * 2. If akid(subject) exists check it matches issuer | |
778 | * 3. If key_usage(issuer) exists check it supports certificate signing | |
779 | * returns 0 for OK, positive for reason for mismatch, reasons match | |
780 | * codes for X509_verify_cert() | |
781 | */ | |
782 | ||
783 | int X509_check_issued(X509 *issuer, X509 *subject) | |
784 | { | |
0f113f3e MC |
785 | if (X509_NAME_cmp(X509_get_subject_name(issuer), |
786 | X509_get_issuer_name(subject))) | |
787 | return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; | |
788 | x509v3_cache_extensions(issuer); | |
789 | x509v3_cache_extensions(subject); | |
790 | ||
791 | if (subject->akid) { | |
792 | int ret = X509_check_akid(issuer, subject->akid); | |
793 | if (ret != X509_V_OK) | |
794 | return ret; | |
795 | } | |
796 | ||
797 | if (subject->ex_flags & EXFLAG_PROXY) { | |
798 | if (ku_reject(issuer, KU_DIGITAL_SIGNATURE)) | |
799 | return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE; | |
800 | } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) | |
801 | return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; | |
802 | return X509_V_OK; | |
2f043896 DSH |
803 | } |
804 | ||
bc7535bc | 805 | int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid) |
0f113f3e | 806 | { |
bc7535bc | 807 | |
0f113f3e MC |
808 | if (!akid) |
809 | return X509_V_OK; | |
810 | ||
811 | /* Check key ids (if present) */ | |
812 | if (akid->keyid && issuer->skid && | |
813 | ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) | |
814 | return X509_V_ERR_AKID_SKID_MISMATCH; | |
815 | /* Check serial number */ | |
816 | if (akid->serial && | |
817 | ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial)) | |
818 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; | |
819 | /* Check issuer name */ | |
820 | if (akid->issuer) { | |
821 | /* | |
822 | * Ugh, for some peculiar reason AKID includes SEQUENCE OF | |
823 | * GeneralName. So look for a DirName. There may be more than one but | |
824 | * we only take any notice of the first. | |
825 | */ | |
826 | GENERAL_NAMES *gens; | |
827 | GENERAL_NAME *gen; | |
828 | X509_NAME *nm = NULL; | |
829 | int i; | |
830 | gens = akid->issuer; | |
831 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | |
832 | gen = sk_GENERAL_NAME_value(gens, i); | |
833 | if (gen->type == GEN_DIRNAME) { | |
834 | nm = gen->d.dirn; | |
835 | break; | |
836 | } | |
837 | } | |
838 | if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer))) | |
839 | return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; | |
840 | } | |
841 | return X509_V_OK; | |
842 | } |