xref: /openbsd/lib/libcrypto/x509/x509_trs.c (revision 8eeec99e)
1 /* $OpenBSD: x509_trs.c,v 1.55 2024/03/26 22:43:42 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 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  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/asn1.h>
62 #include <openssl/objects.h>
63 #include <openssl/x509.h>
64 #include <openssl/x509v3.h>
65 
66 #include "x509_internal.h"
67 #include "x509_local.h"
68 
69 static int
obj_trust(int id,const X509 * x)70 obj_trust(int id, const X509 *x)
71 {
72 	const X509_CERT_AUX *aux;
73 	ASN1_OBJECT *obj;
74 	int i, nid;
75 
76 	if ((aux = x->aux) == NULL)
77 		return X509_TRUST_UNTRUSTED;
78 
79 	for (i = 0; i < sk_ASN1_OBJECT_num(aux->reject); i++) {
80 		obj = sk_ASN1_OBJECT_value(aux->reject, i);
81 		nid = OBJ_obj2nid(obj);
82 		if (nid == id || nid == NID_anyExtendedKeyUsage)
83 			return X509_TRUST_REJECTED;
84 	}
85 
86 	for (i = 0; i < sk_ASN1_OBJECT_num(aux->trust); i++) {
87 		obj = sk_ASN1_OBJECT_value(aux->trust, i);
88 		nid = OBJ_obj2nid(obj);
89 		if (nid == id || nid == NID_anyExtendedKeyUsage)
90 			return X509_TRUST_TRUSTED;
91 	}
92 
93 	return X509_TRUST_UNTRUSTED;
94 }
95 
96 static int
trust_compat(int nid,const X509 * x)97 trust_compat(int nid, const X509 *x)
98 {
99 	/* Extensions already cached in X509_check_trust(). */
100 	if ((x->ex_flags & EXFLAG_SS) != 0)
101 		return X509_TRUST_TRUSTED;
102 
103 	return X509_TRUST_UNTRUSTED;
104 }
105 
106 static int
trust_1oidany(int nid,const X509 * x)107 trust_1oidany(int nid, const X509 *x)
108 {
109 	/* Inspect the certificate's trust settings if there are any. */
110 	if (x->aux != NULL && (x->aux->trust != NULL || x->aux->reject != NULL))
111 		return obj_trust(nid, x);
112 
113 	/* For compatibility we return trusted if the cert is self signed. */
114 	return trust_compat(NID_undef, x);
115 }
116 
117 static int
trust_1oid(int nid,const X509 * x)118 trust_1oid(int nid, const X509 *x)
119 {
120 	if (x->aux != NULL)
121 		return obj_trust(nid, x);
122 
123 	return X509_TRUST_UNTRUSTED;
124 }
125 
126 int
X509_check_trust(X509 * x,int trust_id,int flags)127 X509_check_trust(X509 *x, int trust_id, int flags)
128 {
129 	int rv;
130 
131 	if (trust_id == -1)
132 		return 1;
133 
134 	/* Call early so the trust handlers don't need to modify the certs. */
135 	if (!x509v3_cache_extensions(x))
136 		return X509_TRUST_UNTRUSTED;
137 
138 	switch (trust_id) {
139 	case 0:
140 		/*
141 		 * XXX beck/jsing This enables self signed certs to be trusted
142 		 * for an unspecified id/trust flag value (this is NOT the
143 		 * X509_TRUST_DEFAULT), which was the longstanding openssl
144 		 * behaviour. boringssl does not have this behaviour.
145 		 *
146 		 * This should be revisited, but changing the default
147 		 * "not default" may break things.
148 		 */
149 		rv = obj_trust(NID_anyExtendedKeyUsage, x);
150 		if (rv != X509_TRUST_UNTRUSTED)
151 			return rv;
152 		return trust_compat(NID_undef, x);
153 	case X509_TRUST_COMPAT:
154 		return trust_compat(NID_undef, x);
155 	case X509_TRUST_SSL_CLIENT:
156 		return trust_1oidany(NID_client_auth, x);
157 	case X509_TRUST_SSL_SERVER:
158 		return trust_1oidany(NID_server_auth, x);
159 	case X509_TRUST_EMAIL:
160 		return trust_1oidany(NID_email_protect, x);
161 	case X509_TRUST_OBJECT_SIGN:
162 		return trust_1oidany(NID_code_sign, x);
163 	case X509_TRUST_OCSP_SIGN:
164 		return trust_1oid(NID_OCSP_sign, x);
165 	case X509_TRUST_OCSP_REQUEST:
166 		return trust_1oid(NID_ad_OCSP, x);
167 	case X509_TRUST_TSA:
168 		return trust_1oidany(NID_time_stamp, x);
169 	default:
170 		return obj_trust(trust_id, x);
171 	}
172 }
173 LCRYPTO_ALIAS(X509_check_trust);
174