x509_trs.c revision 1.40
1/* $OpenBSD: x509_trs.c,v 1.40 2024/01/13 19:57:38 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/err.h> 63#include <openssl/objects.h> 64#include <openssl/x509.h> 65#include <openssl/x509v3.h> 66 67#include "crypto_internal.h" 68#include "x509_local.h" 69 70static int 71obj_trust(int id, X509 *x, int flags) 72{ 73 ASN1_OBJECT *obj; 74 int i, nid; 75 X509_CERT_AUX *ax; 76 77 ax = x->aux; 78 if (!ax) 79 return X509_TRUST_UNTRUSTED; 80 if (ax->reject) { 81 for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) { 82 obj = sk_ASN1_OBJECT_value(ax->reject, i); 83 nid = OBJ_obj2nid(obj); 84 if (nid == id || nid == NID_anyExtendedKeyUsage) 85 return X509_TRUST_REJECTED; 86 } 87 } 88 if (ax->trust) { 89 for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) { 90 obj = sk_ASN1_OBJECT_value(ax->trust, i); 91 nid = OBJ_obj2nid(obj); 92 if (nid == id || nid == NID_anyExtendedKeyUsage) 93 return X509_TRUST_TRUSTED; 94 } 95 } 96 return X509_TRUST_UNTRUSTED; 97} 98 99static int 100trust_compat(X509_TRUST *trust, X509 *x, int flags) 101{ 102 X509_check_purpose(x, -1, 0); 103 if (x->ex_flags & EXFLAG_SS) 104 return X509_TRUST_TRUSTED; 105 else 106 return X509_TRUST_UNTRUSTED; 107} 108 109static int 110trust_1oidany(X509_TRUST *trust, X509 *x, int flags) 111{ 112 if (x->aux && (x->aux->trust || x->aux->reject)) 113 return obj_trust(trust->arg1, x, flags); 114 /* we don't have any trust settings: for compatibility 115 * we return trusted if it is self signed 116 */ 117 return trust_compat(trust, x, flags); 118} 119 120static int 121trust_1oid(X509_TRUST *trust, X509 *x, int flags) 122{ 123 if (x->aux) 124 return obj_trust(trust->arg1, x, flags); 125 return X509_TRUST_UNTRUSTED; 126} 127 128/* WARNING: the following table should be kept in order of trust 129 * and without any gaps so we can just subtract the minimum trust 130 * value to get an index into the table 131 */ 132 133static const X509_TRUST trstandard[] = { 134 { 135 .trust = X509_TRUST_COMPAT, 136 .check_trust = trust_compat, 137 .name = "compatible", 138 }, 139 { 140 .trust = X509_TRUST_SSL_CLIENT, 141 .check_trust = trust_1oidany, 142 .name = "SSL Client", 143 .arg1 = NID_client_auth, 144 }, 145 { 146 .trust = X509_TRUST_SSL_SERVER, 147 .check_trust = trust_1oidany, 148 .name = "SSL Server", 149 .arg1 = NID_server_auth, 150 }, 151 { 152 .trust = X509_TRUST_EMAIL, 153 .check_trust = trust_1oidany, 154 .name = "S/MIME email", 155 .arg1 = NID_email_protect, 156 }, 157 { 158 .trust = X509_TRUST_OBJECT_SIGN, 159 .check_trust = trust_1oidany, 160 .name = "Object Signer", 161 .arg1 = NID_code_sign, 162 }, 163 { 164 .trust = X509_TRUST_OCSP_SIGN, 165 .check_trust = trust_1oid, 166 .name = "OCSP responder", 167 .arg1 = NID_OCSP_sign, 168 }, 169 { 170 .trust = X509_TRUST_OCSP_REQUEST, 171 .check_trust = trust_1oid, 172 .name = "OCSP request", 173 .arg1 = NID_ad_OCSP, 174 }, 175 { 176 .trust = X509_TRUST_TSA, 177 .check_trust = trust_1oidany, 178 .name = "TSA server", 179 .arg1 = NID_time_stamp, 180 }, 181}; 182 183#define X509_TRUST_COUNT (sizeof(trstandard) / sizeof(trstandard[0])) 184 185CTASSERT(X509_TRUST_MIN == 1 && X509_TRUST_MAX == X509_TRUST_COUNT); 186 187int 188X509_check_trust(X509 *x, int trust_id, int flags) 189{ 190 const X509_TRUST *trust; 191 int idx; 192 193 if (trust_id == -1) 194 return 1; 195 196 /* 197 * XXX beck/jsing This enables self signed certs to be trusted for 198 * an unspecified id/trust flag value (this is NOT the 199 * X509_TRUST_DEFAULT), which was the longstanding 200 * openssl behaviour. boringssl does not have this behaviour. 201 * 202 * This should be revisited, but changing the default "not default" 203 * may break things. 204 */ 205 if (trust_id == 0) { 206 int rv; 207 rv = obj_trust(NID_anyExtendedKeyUsage, x, 0); 208 if (rv != X509_TRUST_UNTRUSTED) 209 return rv; 210 return trust_compat(NULL, x, 0); 211 } 212 213 if (trust_id < X509_TRUST_MIN || trust_id > X509_TRUST_MAX) 214 return obj_trust(trust_id, x, flags); 215 216 idx = trust_id - X509_TRUST_MIN; 217 trust = &trstandard[idx]; 218 219 return trust->check_trust((X509_TRUST *)trust, x, flags); 220} 221LCRYPTO_ALIAS(X509_check_trust); 222 223/* 224 * Remove all the functions below in the next bump. 225 */ 226 227int 228(*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int) 229{ 230 X509error(ERR_R_DISABLED); 231 return NULL; 232} 233LCRYPTO_ALIAS(X509_TRUST_set_default); 234 235int 236X509_TRUST_get_count(void) 237{ 238 return X509_TRUST_COUNT; 239} 240LCRYPTO_ALIAS(X509_TRUST_get_count); 241 242X509_TRUST * 243X509_TRUST_get0(int idx) 244{ 245 X509error(ERR_R_DISABLED); 246 return NULL; 247} 248LCRYPTO_ALIAS(X509_TRUST_get0); 249 250int 251X509_TRUST_get_by_id(int id) 252{ 253 X509error(ERR_R_DISABLED); 254 return -1; 255} 256LCRYPTO_ALIAS(X509_TRUST_get_by_id); 257 258int 259X509_TRUST_set(int *t, int trust) 260{ 261 X509error(ERR_R_DISABLED); 262 return 0; 263} 264LCRYPTO_ALIAS(X509_TRUST_set); 265 266int 267X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int), 268 const char *name, int arg1, void *arg2) 269{ 270 X509error(ERR_R_DISABLED); 271 return 0; 272} 273LCRYPTO_ALIAS(X509_TRUST_add); 274 275void 276X509_TRUST_cleanup(void) 277{ 278} 279LCRYPTO_ALIAS(X509_TRUST_cleanup); 280 281int 282X509_TRUST_get_flags(const X509_TRUST *xp) 283{ 284 return xp->flags; 285} 286LCRYPTO_ALIAS(X509_TRUST_get_flags); 287 288char * 289X509_TRUST_get0_name(const X509_TRUST *xp) 290{ 291 return xp->name; 292} 293LCRYPTO_ALIAS(X509_TRUST_get0_name); 294 295int 296X509_TRUST_get_trust(const X509_TRUST *xp) 297{ 298 return xp->trust; 299} 300LCRYPTO_ALIAS(X509_TRUST_get_trust); 301