1 /* $OpenBSD: v3_purp.c,v 1.29.4.1 2017/07/05 15:20:10 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
62 #include <openssl/opensslconf.h>
64 #include <openssl/err.h>
65 #include <openssl/x509v3.h>
66 #include <openssl/x509_vfy.h>
68 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
69 #define ku_reject(x, usage) \
70 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
71 #define xku_reject(x, usage) \
72 (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
73 #define ns_reject(x, usage) \
74 (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
76 static void x509v3_cache_extensions(X509
*x
);
78 static int check_ssl_ca(const X509
*x
);
79 static int check_purpose_ssl_client(const X509_PURPOSE
*xp
, const X509
*x
,
81 static int check_purpose_ssl_server(const X509_PURPOSE
*xp
, const X509
*x
,
83 static int check_purpose_ns_ssl_server(const X509_PURPOSE
*xp
, const X509
*x
,
85 static int purpose_smime(const X509
*x
, int ca
);
86 static int check_purpose_smime_sign(const X509_PURPOSE
*xp
, const X509
*x
,
88 static int check_purpose_smime_encrypt(const X509_PURPOSE
*xp
, const X509
*x
,
90 static int check_purpose_crl_sign(const X509_PURPOSE
*xp
, const X509
*x
,
92 static int check_purpose_timestamp_sign(const X509_PURPOSE
*xp
, const X509
*x
,
94 static int no_check(const X509_PURPOSE
*xp
, const X509
*x
, int ca
);
95 static int ocsp_helper(const X509_PURPOSE
*xp
, const X509
*x
, int ca
);
97 static int xp_cmp(const X509_PURPOSE
* const *a
, const X509_PURPOSE
* const *b
);
98 static void xptable_free(X509_PURPOSE
*p
);
100 static X509_PURPOSE xstandard
[] = {
101 {X509_PURPOSE_SSL_CLIENT
, X509_TRUST_SSL_CLIENT
, 0, check_purpose_ssl_client
, "SSL client", "sslclient", NULL
},
102 {X509_PURPOSE_SSL_SERVER
, X509_TRUST_SSL_SERVER
, 0, check_purpose_ssl_server
, "SSL server", "sslserver", NULL
},
103 {X509_PURPOSE_NS_SSL_SERVER
, X509_TRUST_SSL_SERVER
, 0, check_purpose_ns_ssl_server
, "Netscape SSL server", "nssslserver", NULL
},
104 {X509_PURPOSE_SMIME_SIGN
, X509_TRUST_EMAIL
, 0, check_purpose_smime_sign
, "S/MIME signing", "smimesign", NULL
},
105 {X509_PURPOSE_SMIME_ENCRYPT
, X509_TRUST_EMAIL
, 0, check_purpose_smime_encrypt
, "S/MIME encryption", "smimeencrypt", NULL
},
106 {X509_PURPOSE_CRL_SIGN
, X509_TRUST_COMPAT
, 0, check_purpose_crl_sign
, "CRL signing", "crlsign", NULL
},
107 {X509_PURPOSE_ANY
, X509_TRUST_DEFAULT
, 0, no_check
, "Any Purpose", "any", NULL
},
108 {X509_PURPOSE_OCSP_HELPER
, X509_TRUST_COMPAT
, 0, ocsp_helper
, "OCSP helper", "ocsphelper", NULL
},
109 {X509_PURPOSE_TIMESTAMP_SIGN
, X509_TRUST_TSA
, 0, check_purpose_timestamp_sign
, "Time Stamp signing", "timestampsign", NULL
},
112 #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
114 static STACK_OF(X509_PURPOSE
) *xptable
= NULL
;
117 xp_cmp(const X509_PURPOSE
* const *a
, const X509_PURPOSE
* const *b
)
119 return (*a
)->purpose
- (*b
)->purpose
;
122 /* As much as I'd like to make X509_check_purpose use a "const" X509*
123 * I really can't because it does recalculate hashes and do other non-const
126 X509_check_purpose(X509
*x
, int id
, int ca
)
129 const X509_PURPOSE
*pt
;
131 if (!(x
->ex_flags
& EXFLAG_SET
)) {
132 CRYPTO_w_lock(CRYPTO_LOCK_X509
);
133 x509v3_cache_extensions(x
);
134 CRYPTO_w_unlock(CRYPTO_LOCK_X509
);
138 idx
= X509_PURPOSE_get_by_id(id
);
141 pt
= X509_PURPOSE_get0(idx
);
142 return pt
->check_purpose(pt
, x
, ca
);
146 X509_PURPOSE_set(int *p
, int purpose
)
148 if (X509_PURPOSE_get_by_id(purpose
) == -1) {
149 X509V3error(X509V3_R_INVALID_PURPOSE
);
157 X509_PURPOSE_get_count(void)
160 return X509_PURPOSE_COUNT
;
161 return sk_X509_PURPOSE_num(xptable
) + X509_PURPOSE_COUNT
;
165 X509_PURPOSE_get0(int idx
)
169 if (idx
< (int)X509_PURPOSE_COUNT
)
170 return xstandard
+ idx
;
171 return sk_X509_PURPOSE_value(xptable
, idx
- X509_PURPOSE_COUNT
);
175 X509_PURPOSE_get_by_sname(char *sname
)
180 for (i
= 0; i
< X509_PURPOSE_get_count(); i
++) {
181 xptmp
= X509_PURPOSE_get0(i
);
182 if (!strcmp(xptmp
->sname
, sname
))
189 X509_PURPOSE_get_by_id(int purpose
)
194 if ((purpose
>= X509_PURPOSE_MIN
) && (purpose
<= X509_PURPOSE_MAX
))
195 return purpose
- X509_PURPOSE_MIN
;
196 tmp
.purpose
= purpose
;
199 idx
= sk_X509_PURPOSE_find(xptable
, &tmp
);
202 return idx
+ X509_PURPOSE_COUNT
;
206 X509_PURPOSE_add(int id
, int trust
, int flags
,
207 int (*ck
)(const X509_PURPOSE
*, const X509
*, int), char *name
,
208 char *sname
, void *arg
)
212 char *name_dup
, *sname_dup
;
214 name_dup
= sname_dup
= NULL
;
216 if (name
== NULL
|| sname
== NULL
) {
217 X509V3error(X509V3_R_INVALID_NULL_ARGUMENT
);
221 /* This is set according to what we change: application can't set it */
222 flags
&= ~X509_PURPOSE_DYNAMIC
;
223 /* This will always be set for application modified trust entries */
224 flags
|= X509_PURPOSE_DYNAMIC_NAME
;
225 /* Get existing entry if any */
226 idx
= X509_PURPOSE_get_by_id(id
);
227 /* Need a new entry */
229 if ((ptmp
= malloc(sizeof(X509_PURPOSE
))) == NULL
) {
230 X509V3error(ERR_R_MALLOC_FAILURE
);
233 ptmp
->flags
= X509_PURPOSE_DYNAMIC
;
235 ptmp
= X509_PURPOSE_get0(idx
);
237 if ((name_dup
= strdup(name
)) == NULL
)
239 if ((sname_dup
= strdup(sname
)) == NULL
)
242 /* free existing name if dynamic */
243 if (ptmp
->flags
& X509_PURPOSE_DYNAMIC_NAME
) {
247 /* dup supplied name */
248 ptmp
->name
= name_dup
;
249 ptmp
->sname
= sname_dup
;
250 /* Keep the dynamic flag of existing entry */
251 ptmp
->flags
&= X509_PURPOSE_DYNAMIC
;
252 /* Set all other flags */
253 ptmp
->flags
|= flags
;
257 ptmp
->check_purpose
= ck
;
258 ptmp
->usr_data
= arg
;
260 /* If its a new entry manage the dynamic table */
262 if (xptable
== NULL
&&
263 (xptable
= sk_X509_PURPOSE_new(xp_cmp
)) == NULL
)
265 if (sk_X509_PURPOSE_push(xptable
, ptmp
) == 0)
275 X509V3error(ERR_R_MALLOC_FAILURE
);
280 xptable_free(X509_PURPOSE
*p
)
284 if (p
->flags
& X509_PURPOSE_DYNAMIC
) {
285 if (p
->flags
& X509_PURPOSE_DYNAMIC_NAME
) {
294 X509_PURPOSE_cleanup(void)
298 sk_X509_PURPOSE_pop_free(xptable
, xptable_free
);
299 for(i
= 0; i
< X509_PURPOSE_COUNT
; i
++)
300 xptable_free(xstandard
+ i
);
305 X509_PURPOSE_get_id(X509_PURPOSE
*xp
)
311 X509_PURPOSE_get0_name(X509_PURPOSE
*xp
)
317 X509_PURPOSE_get0_sname(X509_PURPOSE
*xp
)
323 X509_PURPOSE_get_trust(X509_PURPOSE
*xp
)
329 nid_cmp(const int *a
, const int *b
)
334 static int nid_cmp_BSEARCH_CMP_FN(const void *, const void *);
335 static int nid_cmp(int const *, int const *);
336 static int *OBJ_bsearch_nid(int *key
, int const *base
, int num
);
339 nid_cmp_BSEARCH_CMP_FN(const void *a_
, const void *b_
)
343 return nid_cmp(a
, b
);
347 OBJ_bsearch_nid(int *key
, int const *base
, int num
)
349 return (int *)OBJ_bsearch_(key
, base
, num
, sizeof(int),
350 nid_cmp_BSEARCH_CMP_FN
);
354 X509_supported_extension(X509_EXTENSION
*ex
)
356 /* This table is a list of the NIDs of supported extensions:
357 * that is those which are used by the verify process. If
358 * an extension is critical and doesn't appear in this list
359 * then the verify process will normally reject the certificate.
360 * The list must be kept in numerical order because it will be
361 * searched using bsearch.
364 static const int supported_nids
[] = {
365 NID_netscape_cert_type
, /* 71 */
366 NID_key_usage
, /* 83 */
367 NID_subject_alt_name
, /* 85 */
368 NID_basic_constraints
, /* 87 */
369 NID_certificate_policies
, /* 89 */
370 NID_ext_key_usage
, /* 126 */
371 NID_policy_constraints
, /* 401 */
372 NID_proxyCertInfo
, /* 663 */
373 NID_name_constraints
, /* 666 */
374 NID_policy_mappings
, /* 747 */
375 NID_inhibit_any_policy
/* 748 */
378 int ex_nid
= OBJ_obj2nid(X509_EXTENSION_get_object(ex
));
380 if (ex_nid
== NID_undef
)
383 if (OBJ_bsearch_nid(&ex_nid
, supported_nids
,
384 sizeof(supported_nids
) / sizeof(int)))
390 setup_dp(X509
*x
, DIST_POINT
*dp
)
392 X509_NAME
*iname
= NULL
;
396 if (dp
->reasons
->length
> 0)
397 dp
->dp_reasons
= dp
->reasons
->data
[0];
398 if (dp
->reasons
->length
> 1)
399 dp
->dp_reasons
|= (dp
->reasons
->data
[1] << 8);
400 dp
->dp_reasons
&= CRLDP_ALL_REASONS
;
402 dp
->dp_reasons
= CRLDP_ALL_REASONS
;
403 if (!dp
->distpoint
|| (dp
->distpoint
->type
!= 1))
405 for (i
= 0; i
< sk_GENERAL_NAME_num(dp
->CRLissuer
); i
++) {
406 GENERAL_NAME
*gen
= sk_GENERAL_NAME_value(dp
->CRLissuer
, i
);
407 if (gen
->type
== GEN_DIRNAME
) {
408 iname
= gen
->d
.directoryName
;
413 iname
= X509_get_issuer_name(x
);
415 DIST_POINT_set_dpname(dp
->distpoint
, iname
);
424 x
->crldp
= X509_get_ext_d2i(x
, NID_crl_distribution_points
, NULL
, NULL
);
425 for (i
= 0; i
< sk_DIST_POINT_num(x
->crldp
); i
++)
426 setup_dp(x
, sk_DIST_POINT_value(x
->crldp
, i
));
430 x509v3_cache_extensions(X509
*x
)
432 BASIC_CONSTRAINTS
*bs
;
433 PROXY_CERT_INFO_EXTENSION
*pci
;
434 ASN1_BIT_STRING
*usage
;
436 EXTENDED_KEY_USAGE
*extusage
;
440 if (x
->ex_flags
& EXFLAG_SET
)
443 #ifndef OPENSSL_NO_SHA
444 X509_digest(x
, EVP_sha1(), x
->sha1_hash
, NULL
);
447 /* V1 should mean no extensions ... */
448 if (!X509_get_version(x
))
449 x
->ex_flags
|= EXFLAG_V1
;
451 /* Handle basic constraints */
452 if ((bs
= X509_get_ext_d2i(x
, NID_basic_constraints
, NULL
, NULL
))) {
454 x
->ex_flags
|= EXFLAG_CA
;
456 if ((bs
->pathlen
->type
== V_ASN1_NEG_INTEGER
) ||
458 x
->ex_flags
|= EXFLAG_INVALID
;
461 x
->ex_pathlen
= ASN1_INTEGER_get(bs
->pathlen
);
464 BASIC_CONSTRAINTS_free(bs
);
465 x
->ex_flags
|= EXFLAG_BCONS
;
468 /* Handle proxy certificates */
469 if ((pci
= X509_get_ext_d2i(x
, NID_proxyCertInfo
, NULL
, NULL
))) {
470 if (x
->ex_flags
& EXFLAG_CA
||
471 X509_get_ext_by_NID(x
, NID_subject_alt_name
, -1) >= 0 ||
472 X509_get_ext_by_NID(x
, NID_issuer_alt_name
, -1) >= 0) {
473 x
->ex_flags
|= EXFLAG_INVALID
;
475 if (pci
->pcPathLengthConstraint
) {
476 if (pci
->pcPathLengthConstraint
->type
==
477 V_ASN1_NEG_INTEGER
) {
478 x
->ex_flags
|= EXFLAG_INVALID
;
482 ASN1_INTEGER_get(pci
->
483 pcPathLengthConstraint
);
485 x
->ex_pcpathlen
= -1;
486 PROXY_CERT_INFO_EXTENSION_free(pci
);
487 x
->ex_flags
|= EXFLAG_PROXY
;
490 /* Handle key usage */
491 if ((usage
= X509_get_ext_d2i(x
, NID_key_usage
, NULL
, NULL
))) {
492 if (usage
->length
> 0) {
493 x
->ex_kusage
= usage
->data
[0];
494 if (usage
->length
> 1)
495 x
->ex_kusage
|= usage
->data
[1] << 8;
498 x
->ex_flags
|= EXFLAG_KUSAGE
;
499 ASN1_BIT_STRING_free(usage
);
502 if ((extusage
= X509_get_ext_d2i(x
, NID_ext_key_usage
, NULL
, NULL
))) {
503 x
->ex_flags
|= EXFLAG_XKUSAGE
;
504 for (i
= 0; i
< sk_ASN1_OBJECT_num(extusage
); i
++) {
505 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage
, i
))) {
506 case NID_server_auth
:
507 x
->ex_xkusage
|= XKU_SSL_SERVER
;
510 case NID_client_auth
:
511 x
->ex_xkusage
|= XKU_SSL_CLIENT
;
514 case NID_email_protect
:
515 x
->ex_xkusage
|= XKU_SMIME
;
519 x
->ex_xkusage
|= XKU_CODE_SIGN
;
524 x
->ex_xkusage
|= XKU_SGC
;
528 x
->ex_xkusage
|= XKU_OCSP_SIGN
;
532 x
->ex_xkusage
|= XKU_TIMESTAMP
;
536 x
->ex_xkusage
|= XKU_DVCS
;
540 sk_ASN1_OBJECT_pop_free(extusage
, ASN1_OBJECT_free
);
543 if ((ns
= X509_get_ext_d2i(x
, NID_netscape_cert_type
, NULL
, NULL
))) {
545 x
->ex_nscert
= ns
->data
[0];
548 x
->ex_flags
|= EXFLAG_NSCERT
;
549 ASN1_BIT_STRING_free(ns
);
552 x
->skid
= X509_get_ext_d2i(x
, NID_subject_key_identifier
, NULL
, NULL
);
553 x
->akid
= X509_get_ext_d2i(x
, NID_authority_key_identifier
, NULL
, NULL
);
555 /* Does subject name match issuer? */
556 if (!X509_NAME_cmp(X509_get_subject_name(x
), X509_get_issuer_name(x
))) {
557 x
->ex_flags
|= EXFLAG_SI
;
558 /* If SKID matches AKID also indicate self signed. */
559 if (X509_check_akid(x
, x
->akid
) == X509_V_OK
&&
560 !ku_reject(x
, KU_KEY_CERT_SIGN
))
561 x
->ex_flags
|= EXFLAG_SS
;
564 x
->altname
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
565 x
->nc
= X509_get_ext_d2i(x
, NID_name_constraints
, &i
, NULL
);
566 if (!x
->nc
&& (i
!= -1))
567 x
->ex_flags
|= EXFLAG_INVALID
;
570 for (i
= 0; i
< X509_get_ext_count(x
); i
++) {
571 ex
= X509_get_ext(x
, i
);
572 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex
)) ==
574 x
->ex_flags
|= EXFLAG_FRESHEST
;
575 if (!X509_EXTENSION_get_critical(ex
))
577 if (!X509_supported_extension(ex
)) {
578 x
->ex_flags
|= EXFLAG_CRITICAL
;
582 x
->ex_flags
|= EXFLAG_SET
;
585 /* CA checks common to all purposes
589 * 2 basicConstraints absent so "maybe" a CA
590 * 3 basicConstraints absent but self signed V1.
591 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
595 check_ca(const X509
*x
)
597 /* keyUsage if present should allow cert signing */
598 if (ku_reject(x
, KU_KEY_CERT_SIGN
))
600 if (x
->ex_flags
& EXFLAG_BCONS
) {
601 if (x
->ex_flags
& EXFLAG_CA
)
603 /* If basicConstraints says not a CA then say so */
607 /* we support V1 roots for... uh, I don't really know why. */
608 if ((x
->ex_flags
& V1_ROOT
) == V1_ROOT
)
610 /* If key usage present it must have certSign so tolerate it */
611 else if (x
->ex_flags
& EXFLAG_KUSAGE
)
613 /* Older certificates could have Netscape-specific CA types */
614 else if (x
->ex_flags
& EXFLAG_NSCERT
&&
615 x
->ex_nscert
& NS_ANY_CA
)
617 /* can this still be regarded a CA certificate? I doubt it */
623 X509_check_ca(X509
*x
)
625 if (!(x
->ex_flags
& EXFLAG_SET
)) {
626 CRYPTO_w_lock(CRYPTO_LOCK_X509
);
627 x509v3_cache_extensions(x
);
628 CRYPTO_w_unlock(CRYPTO_LOCK_X509
);
634 /* Check SSL CA: common checks for SSL client and server */
636 check_ssl_ca(const X509
*x
)
640 ca_ret
= check_ca(x
);
643 /* check nsCertType if present */
644 if (ca_ret
!= 5 || x
->ex_nscert
& NS_SSL_CA
)
651 check_purpose_ssl_client(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
653 if (xku_reject(x
, XKU_SSL_CLIENT
))
656 return check_ssl_ca(x
);
657 /* We need to do digital signatures with it */
658 if (ku_reject(x
, KU_DIGITAL_SIGNATURE
))
660 /* nsCertType if present should allow SSL client use */
661 if (ns_reject(x
, NS_SSL_CLIENT
))
667 check_purpose_ssl_server(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
669 if (xku_reject(x
, XKU_SSL_SERVER
|XKU_SGC
))
672 return check_ssl_ca(x
);
674 if (ns_reject(x
, NS_SSL_SERVER
))
676 /* Now as for keyUsage: we'll at least need to sign OR encipher */
677 if (ku_reject(x
, KU_DIGITAL_SIGNATURE
|KU_KEY_ENCIPHERMENT
))
684 check_purpose_ns_ssl_server(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
688 ret
= check_purpose_ssl_server(xp
, x
, ca
);
691 /* We need to encipher or Netscape complains */
692 if (ku_reject(x
, KU_KEY_ENCIPHERMENT
))
697 /* common S/MIME checks */
699 purpose_smime(const X509
*x
, int ca
)
701 if (xku_reject(x
, XKU_SMIME
))
705 ca_ret
= check_ca(x
);
708 /* check nsCertType if present */
709 if (ca_ret
!= 5 || x
->ex_nscert
& NS_SMIME_CA
)
714 if (x
->ex_flags
& EXFLAG_NSCERT
) {
715 if (x
->ex_nscert
& NS_SMIME
)
717 /* Workaround for some buggy certificates */
718 if (x
->ex_nscert
& NS_SSL_CLIENT
)
726 check_purpose_smime_sign(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
730 ret
= purpose_smime(x
, ca
);
733 if (ku_reject(x
, KU_DIGITAL_SIGNATURE
|KU_NON_REPUDIATION
))
739 check_purpose_smime_encrypt(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
743 ret
= purpose_smime(x
, ca
);
746 if (ku_reject(x
, KU_KEY_ENCIPHERMENT
))
752 check_purpose_crl_sign(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
756 if ((ca_ret
= check_ca(x
)) != 2)
761 if (ku_reject(x
, KU_CRL_SIGN
))
766 /* OCSP helper: this is *not* a full OCSP check. It just checks that
767 * each CA is valid. Additional checks must be made on the chain.
770 ocsp_helper(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
772 /* Must be a valid CA. Should we really support the "I don't know"
776 /* leaf certificate is checked in OCSP_verify() */
781 check_purpose_timestamp_sign(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
785 /* If ca is true we must return if this is a valid CA certificate. */
790 * Check the optional key usage field:
791 * if Key Usage is present, it must be one of digitalSignature
792 * and/or nonRepudiation (other values are not consistent and shall
795 if ((x
->ex_flags
& EXFLAG_KUSAGE
) &&
796 ((x
->ex_kusage
& ~(KU_NON_REPUDIATION
| KU_DIGITAL_SIGNATURE
)) ||
797 !(x
->ex_kusage
& (KU_NON_REPUDIATION
| KU_DIGITAL_SIGNATURE
))))
800 /* Only time stamp key usage is permitted and it's required. */
801 if (!(x
->ex_flags
& EXFLAG_XKUSAGE
) || x
->ex_xkusage
!= XKU_TIMESTAMP
)
804 /* Extended Key Usage MUST be critical */
805 i_ext
= X509_get_ext_by_NID((X509
*) x
, NID_ext_key_usage
, -1);
807 X509_EXTENSION
*ext
= X509_get_ext((X509
*) x
, i_ext
);
808 if (!X509_EXTENSION_get_critical(ext
))
816 no_check(const X509_PURPOSE
*xp
, const X509
*x
, int ca
)
821 /* Various checks to see if one certificate issued the second.
822 * This can be used to prune a set of possible issuer certificates
823 * which have been looked up using some simple method such as by
826 * 1. Check issuer_name(subject) == subject_name(issuer)
827 * 2. If akid(subject) exists check it matches issuer
828 * 3. If key_usage(issuer) exists check it supports certificate signing
829 * returns 0 for OK, positive for reason for mismatch, reasons match
830 * codes for X509_verify_cert()
834 X509_check_issued(X509
*issuer
, X509
*subject
)
836 if (X509_NAME_cmp(X509_get_subject_name(issuer
),
837 X509_get_issuer_name(subject
)))
838 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH
;
839 x509v3_cache_extensions(issuer
);
840 x509v3_cache_extensions(subject
);
843 int ret
= X509_check_akid(issuer
, subject
->akid
);
844 if (ret
!= X509_V_OK
)
848 if (subject
->ex_flags
& EXFLAG_PROXY
) {
849 if (ku_reject(issuer
, KU_DIGITAL_SIGNATURE
))
850 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE
;
851 } else if (ku_reject(issuer
, KU_KEY_CERT_SIGN
))
852 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN
;
857 X509_check_akid(X509
*issuer
, AUTHORITY_KEYID
*akid
)
862 /* Check key ids (if present) */
863 if (akid
->keyid
&& issuer
->skid
&&
864 ASN1_OCTET_STRING_cmp(akid
->keyid
, issuer
->skid
) )
865 return X509_V_ERR_AKID_SKID_MISMATCH
;
866 /* Check serial number */
868 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer
), akid
->serial
))
869 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH
;
870 /* Check issuer name */
872 /* Ugh, for some peculiar reason AKID includes
873 * SEQUENCE OF GeneralName. So look for a DirName.
874 * There may be more than one but we only take any
875 * notice of the first.
879 X509_NAME
*nm
= NULL
;
882 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
883 gen
= sk_GENERAL_NAME_value(gens
, i
);
884 if (gen
->type
== GEN_DIRNAME
) {
889 if (nm
&& X509_NAME_cmp(nm
, X509_get_issuer_name(issuer
)))
890 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH
;