blob: 3f175c959471d8ff4197e24c1c2391327349e016 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* v3_purp.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2001.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-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
13 * notice, this list of conditions and the following disclaimer.
14 *
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 <stdio.h>
58
Adam Langley2b2d66d2015-01-30 17:08:37 -080059#include <string.h>
60
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <openssl/buf.h>
62#include <openssl/err.h>
63#include <openssl/digest.h>
64#include <openssl/mem.h>
65#include <openssl/obj.h>
Brian Smith054e6822015-03-27 21:12:01 -100066#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070067#include <openssl/x509_vfy.h>
68#include <openssl/x509v3.h>
69
70
71static void x509v3_cache_extensions(X509 *x);
72
73static int check_ssl_ca(const X509 *x);
74static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca);
75static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
76static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
77static int purpose_smime(const X509 *x, int ca);
78static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
79static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca);
80static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
81static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
82static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
83static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
84
85static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b);
86static void xptable_free(X509_PURPOSE *p);
87
88static X509_PURPOSE xstandard[] = {
Adam Langley73510762014-06-20 12:00:00 -070089 {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, (char *) "SSL client", (char *) "sslclient", NULL},
90 {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, (char *) "SSL server", (char *) "sslserver", NULL},
91 {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, (char *) "Netscape SSL server", (char *) "nssslserver", NULL},
92 {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, (char *) "S/MIME signing", (char *) "smimesign", NULL},
93 {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, (char *) "S/MIME encryption", (char *) "smimeencrypt", NULL},
94 {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, (char *) "CRL signing", (char *) "crlsign", NULL},
95 {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, (char *) "Any Purpose", (char *) "any", NULL},
96 {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, (char *) "OCSP helper", (char *) "ocsphelper", NULL},
97 {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, check_purpose_timestamp_sign, (char *) "Time Stamp signing", (char *) "timestampsign", NULL},
Adam Langley95c29f32014-06-20 12:00:00 -070098};
99
100#define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
101
102static STACK_OF(X509_PURPOSE) *xptable = NULL;
103
104static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b)
105{
106 return (*a)->purpose - (*b)->purpose;
107}
108
109/* As much as I'd like to make X509_check_purpose use a "const" X509*
110 * I really can't because it does recalculate hashes and do other non-const
111 * things. */
112int X509_check_purpose(X509 *x, int id, int ca)
113{
114 int idx;
115 const X509_PURPOSE *pt;
116 if(!(x->ex_flags & EXFLAG_SET)) {
117 CRYPTO_w_lock(CRYPTO_LOCK_X509);
118 x509v3_cache_extensions(x);
119 CRYPTO_w_unlock(CRYPTO_LOCK_X509);
120 }
121 if(id == -1) return 1;
122 idx = X509_PURPOSE_get_by_id(id);
123 if(idx == -1) return -1;
124 pt = X509_PURPOSE_get0(idx);
125 return pt->check_purpose(pt, x, ca);
126}
127
128int X509_PURPOSE_set(int *p, int purpose)
129{
130 if(X509_PURPOSE_get_by_id(purpose) == -1) {
131 OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_set, X509V3_R_INVALID_PURPOSE);
132 return 0;
133 }
134 *p = purpose;
135 return 1;
136}
137
138int X509_PURPOSE_get_count(void)
139{
140 if(!xptable) return X509_PURPOSE_COUNT;
141 return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
142}
143
144X509_PURPOSE * X509_PURPOSE_get0(int idx)
145{
146 if(idx < 0) return NULL;
147 if(idx < (int)X509_PURPOSE_COUNT) return xstandard + idx;
148 return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
149}
150
151int X509_PURPOSE_get_by_sname(char *sname)
152{
153 int i;
154 X509_PURPOSE *xptmp;
155 for(i = 0; i < X509_PURPOSE_get_count(); i++) {
156 xptmp = X509_PURPOSE_get0(i);
157 if(!strcmp(xptmp->sname, sname)) return i;
158 }
159 return -1;
160}
161
162int X509_PURPOSE_get_by_id(int purpose)
163{
164 X509_PURPOSE tmp;
165 size_t idx;
166
167 if((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
168 return purpose - X509_PURPOSE_MIN;
169 tmp.purpose = purpose;
170 if(!xptable) return -1;
171
172 if (!sk_X509_PURPOSE_find(xptable, &idx, &tmp))
173 return -1;
174 return idx + X509_PURPOSE_COUNT;
175}
176
177int X509_PURPOSE_add(int id, int trust, int flags,
178 int (*ck)(const X509_PURPOSE *, const X509 *, int),
179 char *name, char *sname, void *arg)
180{
181 int idx;
182 X509_PURPOSE *ptmp;
David Benjamina85093f2014-11-02 22:22:22 -0500183 char *name_dup, *sname_dup;
184
Adam Langley95c29f32014-06-20 12:00:00 -0700185 /* This is set according to what we change: application can't set it */
186 flags &= ~X509_PURPOSE_DYNAMIC;
187 /* This will always be set for application modified trust entries */
188 flags |= X509_PURPOSE_DYNAMIC_NAME;
189 /* Get existing entry if any */
190 idx = X509_PURPOSE_get_by_id(id);
191 /* Need a new entry */
192 if(idx == -1) {
193 if(!(ptmp = OPENSSL_malloc(sizeof(X509_PURPOSE)))) {
194 OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
195 return 0;
196 }
197 ptmp->flags = X509_PURPOSE_DYNAMIC;
198 } else ptmp = X509_PURPOSE_get0(idx);
199
David Benjamina85093f2014-11-02 22:22:22 -0500200 /* Duplicate the supplied names. */
201 name_dup = BUF_strdup(name);
202 sname_dup = BUF_strdup(sname);
203 if (name_dup == NULL || sname_dup == NULL) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100204 OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
David Benjamina85093f2014-11-02 22:22:22 -0500205 if (name_dup != NULL)
206 OPENSSL_free(name_dup);
207 if (sname_dup != NULL)
208 OPENSSL_free(sname_dup);
209 if (idx == -1)
210 OPENSSL_free(ptmp);
211 return 0;
212 }
213
Adam Langley95c29f32014-06-20 12:00:00 -0700214 /* OPENSSL_free existing name if dynamic */
215 if(ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
216 OPENSSL_free(ptmp->name);
217 OPENSSL_free(ptmp->sname);
218 }
219 /* dup supplied name */
David Benjamina85093f2014-11-02 22:22:22 -0500220 ptmp->name = name_dup;
221 ptmp->sname = sname_dup;
Adam Langley95c29f32014-06-20 12:00:00 -0700222 /* Keep the dynamic flag of existing entry */
223 ptmp->flags &= X509_PURPOSE_DYNAMIC;
224 /* Set all other flags */
225 ptmp->flags |= flags;
226
227 ptmp->purpose = id;
228 ptmp->trust = trust;
229 ptmp->check_purpose = ck;
230 ptmp->usr_data = arg;
231
232 /* If its a new entry manage the dynamic table */
233 if(idx == -1) {
234 if(!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
235 OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
David Benjamina85093f2014-11-02 22:22:22 -0500236 xptable_free(ptmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700237 return 0;
238 }
239 if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
240 OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
David Benjamina85093f2014-11-02 22:22:22 -0500241 xptable_free(ptmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700242 return 0;
243 }
244 }
245 return 1;
246}
247
248static void xptable_free(X509_PURPOSE *p)
249 {
250 if(!p) return;
251 if (p->flags & X509_PURPOSE_DYNAMIC)
252 {
253 if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
254 OPENSSL_free(p->name);
255 OPENSSL_free(p->sname);
256 }
257 OPENSSL_free(p);
258 }
259 }
260
261void X509_PURPOSE_cleanup(void)
262{
263 unsigned int i;
264 sk_X509_PURPOSE_pop_free(xptable, xptable_free);
265 for(i = 0; i < X509_PURPOSE_COUNT; i++) xptable_free(xstandard + i);
266 xptable = NULL;
267}
268
269int X509_PURPOSE_get_id(X509_PURPOSE *xp)
270{
271 return xp->purpose;
272}
273
274char *X509_PURPOSE_get0_name(X509_PURPOSE *xp)
275{
276 return xp->name;
277}
278
279char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp)
280{
281 return xp->sname;
282}
283
284int X509_PURPOSE_get_trust(X509_PURPOSE *xp)
285{
286 return xp->trust;
287}
288
289static int nid_cmp(const void *void_a, const void *void_b)
290 {
291 const int *a = void_a, *b = void_b;
292
293 return *a - *b;
294 }
295
296int X509_supported_extension(X509_EXTENSION *ex)
297 {
298 /* This table is a list of the NIDs of supported extensions:
299 * that is those which are used by the verify process. If
300 * an extension is critical and doesn't appear in this list
301 * then the verify process will normally reject the certificate.
302 * The list must be kept in numerical order because it will be
303 * searched using bsearch.
304 */
305
306 static const int supported_nids[] = {
307 NID_netscape_cert_type, /* 71 */
308 NID_key_usage, /* 83 */
309 NID_subject_alt_name, /* 85 */
310 NID_basic_constraints, /* 87 */
311 NID_certificate_policies, /* 89 */
312 NID_ext_key_usage, /* 126 */
313 NID_policy_constraints, /* 401 */
314 NID_proxyCertInfo, /* 663 */
315 NID_name_constraints, /* 666 */
316 NID_policy_mappings, /* 747 */
317 NID_inhibit_any_policy /* 748 */
318 };
319
320 int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
321
322 if (ex_nid == NID_undef)
323 return 0;
324
325 if (bsearch(&ex_nid, supported_nids, sizeof(supported_nids)/sizeof(int), sizeof(int), nid_cmp) != NULL)
326 return 1;
327 return 0;
328 }
329
330static void setup_dp(X509 *x, DIST_POINT *dp)
331 {
332 X509_NAME *iname = NULL;
333 size_t i;
334 if (dp->reasons)
335 {
336 if (dp->reasons->length > 0)
337 dp->dp_reasons = dp->reasons->data[0];
338 if (dp->reasons->length > 1)
339 dp->dp_reasons |= (dp->reasons->data[1] << 8);
340 dp->dp_reasons &= CRLDP_ALL_REASONS;
341 }
342 else
343 dp->dp_reasons = CRLDP_ALL_REASONS;
344 if (!dp->distpoint || (dp->distpoint->type != 1))
345 return;
346 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++)
347 {
348 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
349 if (gen->type == GEN_DIRNAME)
350 {
351 iname = gen->d.directoryName;
352 break;
353 }
354 }
355 if (!iname)
356 iname = X509_get_issuer_name(x);
357
358 DIST_POINT_set_dpname(dp->distpoint, iname);
359
360 }
361
362static void setup_crldp(X509 *x)
363 {
364 size_t i;
365 x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL);
366 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
367 setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
368 }
369
370static void x509v3_cache_extensions(X509 *x)
371{
372 BASIC_CONSTRAINTS *bs;
373 PROXY_CERT_INFO_EXTENSION *pci;
374 ASN1_BIT_STRING *usage;
375 ASN1_BIT_STRING *ns;
376 EXTENDED_KEY_USAGE *extusage;
377 X509_EXTENSION *ex;
378 size_t i;
379 int j;
380 if(x->ex_flags & EXFLAG_SET) return;
Adam Langley95c29f32014-06-20 12:00:00 -0700381 X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700382 /* V1 should mean no extensions ... */
383 if(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;
384 /* Handle basic constraints */
385 if((bs=X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
386 if(bs->ca) x->ex_flags |= EXFLAG_CA;
387 if(bs->pathlen) {
388 if((bs->pathlen->type == V_ASN1_NEG_INTEGER)
389 || !bs->ca) {
390 x->ex_flags |= EXFLAG_INVALID;
391 x->ex_pathlen = 0;
392 } else x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
393 } else x->ex_pathlen = -1;
394 BASIC_CONSTRAINTS_free(bs);
395 x->ex_flags |= EXFLAG_BCONS;
396 }
397 /* Handle proxy certificates */
398 if((pci=X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) {
399 if (x->ex_flags & EXFLAG_CA
Adam Langley548523f2014-06-20 12:00:00 -0700400 || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
401 || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
Adam Langley95c29f32014-06-20 12:00:00 -0700402 x->ex_flags |= EXFLAG_INVALID;
403 }
404 if (pci->pcPathLengthConstraint) {
405 x->ex_pcpathlen =
406 ASN1_INTEGER_get(pci->pcPathLengthConstraint);
407 } else x->ex_pcpathlen = -1;
408 PROXY_CERT_INFO_EXTENSION_free(pci);
409 x->ex_flags |= EXFLAG_PROXY;
410 }
411 /* Handle key usage */
412 if((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
413 if(usage->length > 0) {
414 x->ex_kusage = usage->data[0];
415 if(usage->length > 1)
416 x->ex_kusage |= usage->data[1] << 8;
417 } else x->ex_kusage = 0;
418 x->ex_flags |= EXFLAG_KUSAGE;
419 ASN1_BIT_STRING_free(usage);
420 }
421 x->ex_xkusage = 0;
422 if((extusage=X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
423 x->ex_flags |= EXFLAG_XKUSAGE;
424 for(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
425 switch(OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage,i))) {
426 case NID_server_auth:
427 x->ex_xkusage |= XKU_SSL_SERVER;
428 break;
429
430 case NID_client_auth:
431 x->ex_xkusage |= XKU_SSL_CLIENT;
432 break;
433
434 case NID_email_protect:
435 x->ex_xkusage |= XKU_SMIME;
436 break;
437
438 case NID_code_sign:
439 x->ex_xkusage |= XKU_CODE_SIGN;
440 break;
441
442 case NID_ms_sgc:
443 case NID_ns_sgc:
444 x->ex_xkusage |= XKU_SGC;
445 break;
446
447 case NID_OCSP_sign:
448 x->ex_xkusage |= XKU_OCSP_SIGN;
449 break;
450
451 case NID_time_stamp:
452 x->ex_xkusage |= XKU_TIMESTAMP;
453 break;
454
455 case NID_dvcs:
456 x->ex_xkusage |= XKU_DVCS;
457 break;
458
459 case NID_anyExtendedKeyUsage:
460 x->ex_xkusage |= XKU_ANYEKU;
461 break;
462 }
463 }
464 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
465 }
466
467 if((ns=X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
468 if(ns->length > 0) x->ex_nscert = ns->data[0];
469 else x->ex_nscert = 0;
470 x->ex_flags |= EXFLAG_NSCERT;
471 ASN1_BIT_STRING_free(ns);
472 }
473 x->skid =X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);
474 x->akid =X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);
Adam Langleya993a702014-06-20 12:00:00 -0700475 /* Does subject name match issuer ? */
476 if(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))
477 {
478 x->ex_flags |= EXFLAG_SI;
479 /* If SKID matches AKID also indicate self signed */
480 if (X509_check_akid(x, x->akid) == X509_V_OK)
481 x->ex_flags |= EXFLAG_SS;
482 }
Adam Langley95c29f32014-06-20 12:00:00 -0700483 x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
484 x->nc = X509_get_ext_d2i(x, NID_name_constraints, &j, NULL);
485 if (!x->nc && (j != -1))
486 x->ex_flags |= EXFLAG_INVALID;
487 setup_crldp(x);
488
489 for (j = 0; j < X509_get_ext_count(x); j++)
490 {
491 ex = X509_get_ext(x, j);
492 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
493 == NID_freshest_crl)
494 x->ex_flags |= EXFLAG_FRESHEST;
495 if (!X509_EXTENSION_get_critical(ex))
496 continue;
497 if (!X509_supported_extension(ex))
498 {
499 x->ex_flags |= EXFLAG_CRITICAL;
500 break;
501 }
502 }
503 x->ex_flags |= EXFLAG_SET;
504}
505
506/* CA checks common to all purposes
507 * return codes:
508 * 0 not a CA
509 * 1 is a CA
510 * 2 basicConstraints absent so "maybe" a CA
511 * 3 basicConstraints absent but self signed V1.
512 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
513 */
514
515#define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
516#define ku_reject(x, usage) \
517 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
518#define xku_reject(x, usage) \
519 (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
520#define ns_reject(x, usage) \
521 (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
522
523static int check_ca(const X509 *x)
524{
525 /* keyUsage if present should allow cert signing */
526 if(ku_reject(x, KU_KEY_CERT_SIGN)) return 0;
527 if(x->ex_flags & EXFLAG_BCONS) {
528 if(x->ex_flags & EXFLAG_CA) return 1;
529 /* If basicConstraints says not a CA then say so */
530 else return 0;
531 } else {
532 /* we support V1 roots for... uh, I don't really know why. */
533 if((x->ex_flags & V1_ROOT) == V1_ROOT) return 3;
534 /* If key usage present it must have certSign so tolerate it */
535 else if (x->ex_flags & EXFLAG_KUSAGE) return 4;
536 /* Older certificates could have Netscape-specific CA types */
537 else if (x->ex_flags & EXFLAG_NSCERT
538 && x->ex_nscert & NS_ANY_CA) return 5;
539 /* can this still be regarded a CA certificate? I doubt it */
540 return 0;
541 }
542}
543
544int X509_check_ca(X509 *x)
545{
546 if(!(x->ex_flags & EXFLAG_SET)) {
547 CRYPTO_w_lock(CRYPTO_LOCK_X509);
548 x509v3_cache_extensions(x);
549 CRYPTO_w_unlock(CRYPTO_LOCK_X509);
550 }
551
552 return check_ca(x);
553}
554
555/* Check SSL CA: common checks for SSL client and server */
556static int check_ssl_ca(const X509 *x)
557{
558 int ca_ret;
559 ca_ret = check_ca(x);
560 if(!ca_ret) return 0;
561 /* check nsCertType if present */
562 if(ca_ret != 5 || x->ex_nscert & NS_SSL_CA) return ca_ret;
563 else return 0;
564}
565
566
567static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca)
568{
569 if(xku_reject(x,XKU_SSL_CLIENT)) return 0;
570 if(ca) return check_ssl_ca(x);
571 /* We need to do digital signatures or key agreement */
572 if(ku_reject(x,KU_DIGITAL_SIGNATURE|KU_KEY_AGREEMENT)) return 0;
573 /* nsCertType if present should allow SSL client use */
574 if(ns_reject(x, NS_SSL_CLIENT)) return 0;
575 return 1;
576}
577/* Key usage needed for TLS/SSL server: digital signature, encipherment or
578 * key agreement. The ssl code can check this more thoroughly for individual
579 * key types.
580 */
581#define KU_TLS \
582 KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
583
584static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
585{
586 if(xku_reject(x,XKU_SSL_SERVER|XKU_SGC)) return 0;
587 if(ca) return check_ssl_ca(x);
588
589 if(ns_reject(x, NS_SSL_SERVER)) return 0;
590 if(ku_reject(x, KU_TLS)) return 0;
591
592 return 1;
593
594}
595
596static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
597{
598 int ret;
599 ret = check_purpose_ssl_server(xp, x, ca);
600 if(!ret || ca) return ret;
601 /* We need to encipher or Netscape complains */
602 if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
603 return ret;
604}
605
606/* common S/MIME checks */
607static int purpose_smime(const X509 *x, int ca)
608{
609 if(xku_reject(x,XKU_SMIME)) return 0;
610 if(ca) {
611 int ca_ret;
612 ca_ret = check_ca(x);
613 if(!ca_ret) return 0;
614 /* check nsCertType if present */
615 if(ca_ret != 5 || x->ex_nscert & NS_SMIME_CA) return ca_ret;
616 else return 0;
617 }
618 if(x->ex_flags & EXFLAG_NSCERT) {
619 if(x->ex_nscert & NS_SMIME) return 1;
620 /* Workaround for some buggy certificates */
621 if(x->ex_nscert & NS_SSL_CLIENT) return 2;
622 return 0;
623 }
624 return 1;
625}
626
627static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
628{
629 int ret;
630 ret = purpose_smime(x, ca);
631 if(!ret || ca) return ret;
632 if(ku_reject(x, KU_DIGITAL_SIGNATURE|KU_NON_REPUDIATION)) return 0;
633 return ret;
634}
635
636static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca)
637{
638 int ret;
639 ret = purpose_smime(x, ca);
640 if(!ret || ca) return ret;
641 if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
642 return ret;
643}
644
645static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
646{
647 if(ca) {
648 int ca_ret;
649 if((ca_ret = check_ca(x)) != 2) return ca_ret;
650 else return 0;
651 }
652 if(ku_reject(x, KU_CRL_SIGN)) return 0;
653 return 1;
654}
655
656/* OCSP helper: this is *not* a full OCSP check. It just checks that
657 * each CA is valid. Additional checks must be made on the chain.
658 */
659
660static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
661{
662 /* Must be a valid CA. Should we really support the "I don't know"
663 value (2)? */
664 if(ca) return check_ca(x);
665 /* leaf certificate is checked in OCSP_verify() */
666 return 1;
667}
668
669static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
670 int ca)
671{
672 int i_ext;
673
674 /* If ca is true we must return if this is a valid CA certificate. */
675 if (ca) return check_ca(x);
676
677 /*
678 * Check the optional key usage field:
679 * if Key Usage is present, it must be one of digitalSignature
680 * and/or nonRepudiation (other values are not consistent and shall
681 * be rejected).
682 */
683 if ((x->ex_flags & EXFLAG_KUSAGE)
684 && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
685 !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
686 return 0;
687
688 /* Only time stamp key usage is permitted and it's required. */
689 if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
690 return 0;
691
692 /* Extended Key Usage MUST be critical */
Adam Langley548523f2014-06-20 12:00:00 -0700693 i_ext = X509_get_ext_by_NID((X509 *) x, NID_ext_key_usage, -1);
Adam Langley95c29f32014-06-20 12:00:00 -0700694 if (i_ext >= 0)
695 {
696 X509_EXTENSION *ext = X509_get_ext((X509 *) x, i_ext);
697 if (!X509_EXTENSION_get_critical(ext))
698 return 0;
699 }
700
701 return 1;
702}
703
704static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
705{
706 return 1;
707}
708
709/* Various checks to see if one certificate issued the second.
710 * This can be used to prune a set of possible issuer certificates
711 * which have been looked up using some simple method such as by
712 * subject name.
713 * These are:
714 * 1. Check issuer_name(subject) == subject_name(issuer)
715 * 2. If akid(subject) exists check it matches issuer
716 * 3. If key_usage(issuer) exists check it supports certificate signing
717 * returns 0 for OK, positive for reason for mismatch, reasons match
718 * codes for X509_verify_cert()
719 */
720
721int X509_check_issued(X509 *issuer, X509 *subject)
722{
723 if(X509_NAME_cmp(X509_get_subject_name(issuer),
724 X509_get_issuer_name(subject)))
725 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
726 x509v3_cache_extensions(issuer);
727 x509v3_cache_extensions(subject);
728
729 if(subject->akid)
730 {
731 int ret = X509_check_akid(issuer, subject->akid);
732 if (ret != X509_V_OK)
733 return ret;
734 }
735
736 if(subject->ex_flags & EXFLAG_PROXY)
737 {
738 if(ku_reject(issuer, KU_DIGITAL_SIGNATURE))
739 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
740 }
741 else if(ku_reject(issuer, KU_KEY_CERT_SIGN))
742 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
743 return X509_V_OK;
744}
745
746int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
747 {
748
749 if(!akid)
750 return X509_V_OK;
751
752 /* Check key ids (if present) */
753 if(akid->keyid && issuer->skid &&
754 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid) )
755 return X509_V_ERR_AKID_SKID_MISMATCH;
756 /* Check serial number */
757 if(akid->serial &&
758 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
759 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
760 /* Check issuer name */
761 if(akid->issuer)
762 {
763 /* Ugh, for some peculiar reason AKID includes
764 * SEQUENCE OF GeneralName. So look for a DirName.
765 * There may be more than one but we only take any
766 * notice of the first.
767 */
768 GENERAL_NAMES *gens;
769 GENERAL_NAME *gen;
770 X509_NAME *nm = NULL;
771 size_t i;
772 gens = akid->issuer;
773 for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
774 {
775 gen = sk_GENERAL_NAME_value(gens, i);
776 if(gen->type == GEN_DIRNAME)
777 {
778 nm = gen->d.dirn;
779 break;
780 }
781 }
782 if(nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
783 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
784 }
785 return X509_V_OK;
786 }
787