x509_trs.c revision 1.29
1/* $OpenBSD: x509_trs.c,v 1.29 2022/11/17 00:42:12 beck 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_lcl.h" 66 67static int tr_cmp(const X509_TRUST * const *a, const X509_TRUST * const *b); 68static void trtable_free(X509_TRUST *p); 69 70static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags); 71static int trust_1oid(X509_TRUST *trust, X509 *x, int flags); 72static int trust_compat(X509_TRUST *trust, X509 *x, int flags); 73 74static int obj_trust(int id, X509 *x, int flags); 75static int (*default_trust)(int id, X509 *x, int flags) = obj_trust; 76 77/* WARNING: the following table should be kept in order of trust 78 * and without any gaps so we can just subtract the minimum trust 79 * value to get an index into the table 80 */ 81 82static X509_TRUST trstandard[] = { 83 {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL}, 84 {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth, NULL}, 85 {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth, NULL}, 86 {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect, NULL}, 87 {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign, NULL}, 88 {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, NULL}, 89 {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP, NULL}, 90 {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL} 91}; 92 93#define X509_TRUST_COUNT (sizeof(trstandard)/sizeof(X509_TRUST)) 94 95static STACK_OF(X509_TRUST) *trtable = NULL; 96 97static int 98tr_cmp(const X509_TRUST * const *a, const X509_TRUST * const *b) 99{ 100 return (*a)->trust - (*b)->trust; 101} 102 103int 104(*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int) 105{ 106 int (*oldtrust)(int , X509 *, int); 107 108 oldtrust = default_trust; 109 default_trust = trust; 110 return oldtrust; 111} 112LCRYPTO_ALIAS(X509_TRUST_set_default) 113 114int 115X509_check_trust(X509 *x, int id, int flags) 116{ 117 X509_TRUST *pt; 118 int idx; 119 120 if (id == -1) 121 return 1; 122 /* 123 * XXX beck/jsing This enables self signed certs to be trusted for 124 * an unspecified id/trust flag value (this is NOT the 125 * X509_TRUST_DEFAULT), which was the longstanding 126 * openssl behaviour. boringssl does not have this behaviour. 127 * 128 * This should be revisited, but changing the default "not default" 129 * may break things. 130 */ 131 if (id == 0) { 132 int rv; 133 rv = obj_trust(NID_anyExtendedKeyUsage, x, 0); 134 if (rv != X509_TRUST_UNTRUSTED) 135 return rv; 136 return trust_compat(NULL, x, 0); 137 } 138 idx = X509_TRUST_get_by_id(id); 139 if (idx == -1) 140 return default_trust(id, x, flags); 141 pt = X509_TRUST_get0(idx); 142 return pt->check_trust(pt, x, flags); 143} 144LCRYPTO_ALIAS(X509_check_trust) 145 146int 147X509_TRUST_get_count(void) 148{ 149 if (!trtable) 150 return X509_TRUST_COUNT; 151 return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT; 152} 153LCRYPTO_ALIAS(X509_TRUST_get_count) 154 155X509_TRUST * 156X509_TRUST_get0(int idx) 157{ 158 if (idx < 0) 159 return NULL; 160 if (idx < (int)X509_TRUST_COUNT) 161 return trstandard + idx; 162 return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT); 163} 164LCRYPTO_ALIAS(X509_TRUST_get0) 165 166int 167X509_TRUST_get_by_id(int id) 168{ 169 X509_TRUST tmp; 170 int idx; 171 172 if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX)) 173 return id - X509_TRUST_MIN; 174 tmp.trust = id; 175 if (!trtable) 176 return -1; 177 idx = sk_X509_TRUST_find(trtable, &tmp); 178 if (idx == -1) 179 return -1; 180 return idx + X509_TRUST_COUNT; 181} 182LCRYPTO_ALIAS(X509_TRUST_get_by_id) 183 184int 185X509_TRUST_set(int *t, int trust) 186{ 187 if (X509_TRUST_get_by_id(trust) == -1) { 188 X509error(X509_R_INVALID_TRUST); 189 return 0; 190 } 191 *t = trust; 192 return 1; 193} 194LCRYPTO_ALIAS(X509_TRUST_set) 195 196int 197X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int), 198 const char *name, int arg1, void *arg2) 199{ 200 int idx; 201 X509_TRUST *trtmp; 202 char *name_dup; 203 204 /* This is set according to what we change: application can't set it */ 205 flags &= ~X509_TRUST_DYNAMIC; 206 /* This will always be set for application modified trust entries */ 207 flags |= X509_TRUST_DYNAMIC_NAME; 208 /* Get existing entry if any */ 209 idx = X509_TRUST_get_by_id(id); 210 /* Need a new entry */ 211 if (idx == -1) { 212 if (!(trtmp = malloc(sizeof(X509_TRUST)))) { 213 X509error(ERR_R_MALLOC_FAILURE); 214 return 0; 215 } 216 trtmp->flags = X509_TRUST_DYNAMIC; 217 } else { 218 trtmp = X509_TRUST_get0(idx); 219 if (trtmp == NULL) { 220 X509error(X509_R_INVALID_TRUST); 221 return 0; 222 } 223 } 224 225 if ((name_dup = strdup(name)) == NULL) 226 goto err; 227 228 /* free existing name if dynamic */ 229 if (trtmp->flags & X509_TRUST_DYNAMIC_NAME) 230 free(trtmp->name); 231 /* dup supplied name */ 232 trtmp->name = name_dup; 233 /* Keep the dynamic flag of existing entry */ 234 trtmp->flags &= X509_TRUST_DYNAMIC; 235 /* Set all other flags */ 236 trtmp->flags |= flags; 237 238 trtmp->trust = id; 239 trtmp->check_trust = ck; 240 trtmp->arg1 = arg1; 241 trtmp->arg2 = arg2; 242 243 /* If it's a new entry, manage the dynamic table */ 244 if (idx == -1) { 245 if (trtable == NULL && 246 (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) 247 goto err; 248 if (sk_X509_TRUST_push(trtable, trtmp) == 0) 249 goto err; 250 } 251 return 1; 252 253err: 254 free(name_dup); 255 if (idx == -1) 256 free(trtmp); 257 X509error(ERR_R_MALLOC_FAILURE); 258 return 0; 259} 260LCRYPTO_ALIAS(X509_TRUST_add) 261 262static void 263trtable_free(X509_TRUST *p) 264{ 265 if (!p) 266 return; 267 if (p->flags & X509_TRUST_DYNAMIC) { 268 if (p->flags & X509_TRUST_DYNAMIC_NAME) 269 free(p->name); 270 free(p); 271 } 272} 273 274void 275X509_TRUST_cleanup(void) 276{ 277 sk_X509_TRUST_pop_free(trtable, trtable_free); 278 trtable = NULL; 279} 280LCRYPTO_ALIAS(X509_TRUST_cleanup) 281 282int 283X509_TRUST_get_flags(const X509_TRUST *xp) 284{ 285 return xp->flags; 286} 287LCRYPTO_ALIAS(X509_TRUST_get_flags) 288 289char * 290X509_TRUST_get0_name(const X509_TRUST *xp) 291{ 292 return xp->name; 293} 294LCRYPTO_ALIAS(X509_TRUST_get0_name) 295 296int 297X509_TRUST_get_trust(const X509_TRUST *xp) 298{ 299 return xp->trust; 300} 301LCRYPTO_ALIAS(X509_TRUST_get_trust) 302 303static int 304trust_1oidany(X509_TRUST *trust, X509 *x, int flags) 305{ 306 if (x->aux && (x->aux->trust || x->aux->reject)) 307 return obj_trust(trust->arg1, x, flags); 308 /* we don't have any trust settings: for compatibility 309 * we return trusted if it is self signed 310 */ 311 return trust_compat(trust, x, flags); 312} 313 314static int 315trust_1oid(X509_TRUST *trust, X509 *x, int flags) 316{ 317 if (x->aux) 318 return obj_trust(trust->arg1, x, flags); 319 return X509_TRUST_UNTRUSTED; 320} 321 322static int 323trust_compat(X509_TRUST *trust, X509 *x, int flags) 324{ 325 X509_check_purpose(x, -1, 0); 326 if (x->ex_flags & EXFLAG_SS) 327 return X509_TRUST_TRUSTED; 328 else 329 return X509_TRUST_UNTRUSTED; 330} 331 332static int 333obj_trust(int id, X509 *x, int flags) 334{ 335 ASN1_OBJECT *obj; 336 int i, nid; 337 X509_CERT_AUX *ax; 338 339 ax = x->aux; 340 if (!ax) 341 return X509_TRUST_UNTRUSTED; 342 if (ax->reject) { 343 for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) { 344 obj = sk_ASN1_OBJECT_value(ax->reject, i); 345 nid = OBJ_obj2nid(obj); 346 if (nid == id || nid == NID_anyExtendedKeyUsage) 347 return X509_TRUST_REJECTED; 348 } 349 } 350 if (ax->trust) { 351 for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) { 352 obj = sk_ASN1_OBJECT_value(ax->trust, i); 353 nid = OBJ_obj2nid(obj); 354 if (nid == id || nid == NID_anyExtendedKeyUsage) 355 return X509_TRUST_TRUSTED; 356 } 357 } 358 return X509_TRUST_UNTRUSTED; 359} 360