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