| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/x509/x509_trs.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* | - | ||||||||||||
| 2 | * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. | - | ||||||||||||
| 3 | * | - | ||||||||||||
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use | - | ||||||||||||
| 5 | * this file except in compliance with the License. You can obtain a copy | - | ||||||||||||
| 6 | * in the file LICENSE in the source distribution or at | - | ||||||||||||
| 7 | * https://www.openssl.org/source/license.html | - | ||||||||||||
| 8 | */ | - | ||||||||||||
| 9 | - | |||||||||||||
| 10 | #include <stdio.h> | - | ||||||||||||
| 11 | #include "internal/cryptlib.h" | - | ||||||||||||
| 12 | #include <openssl/x509v3.h> | - | ||||||||||||
| 13 | #include "internal/x509_int.h" | - | ||||||||||||
| 14 | - | |||||||||||||
| 15 | static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b); | - | ||||||||||||
| 16 | static void trtable_free(X509_TRUST *p); | - | ||||||||||||
| 17 | - | |||||||||||||
| 18 | static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags); | - | ||||||||||||
| 19 | static int trust_1oid(X509_TRUST *trust, X509 *x, int flags); | - | ||||||||||||
| 20 | static int trust_compat(X509_TRUST *trust, X509 *x, int flags); | - | ||||||||||||
| 21 | - | |||||||||||||
| 22 | static int obj_trust(int id, X509 *x, int flags); | - | ||||||||||||
| 23 | static int (*default_trust) (int id, X509 *x, int flags) = obj_trust; | - | ||||||||||||
| 24 | - | |||||||||||||
| 25 | /* | - | ||||||||||||
| 26 | * WARNING: the following table should be kept in order of trust and without | - | ||||||||||||
| 27 | * any gaps so we can just subtract the minimum trust value to get an index | - | ||||||||||||
| 28 | * into the table | - | ||||||||||||
| 29 | */ | - | ||||||||||||
| 30 | - | |||||||||||||
| 31 | static X509_TRUST trstandard[] = { | - | ||||||||||||
| 32 | {X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL}, | - | ||||||||||||
| 33 | {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth, | - | ||||||||||||
| 34 | NULL}, | - | ||||||||||||
| 35 | {X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth, | - | ||||||||||||
| 36 | NULL}, | - | ||||||||||||
| 37 | {X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect, | - | ||||||||||||
| 38 | NULL}, | - | ||||||||||||
| 39 | {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign, | - | ||||||||||||
| 40 | NULL}, | - | ||||||||||||
| 41 | {X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, | - | ||||||||||||
| 42 | NULL}, | - | ||||||||||||
| 43 | {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP, | - | ||||||||||||
| 44 | NULL}, | - | ||||||||||||
| 45 | {X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL} | - | ||||||||||||
| 46 | }; | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | #define X509_TRUST_COUNT OSSL_NELEM(trstandard) | - | ||||||||||||
| 49 | - | |||||||||||||
| 50 | static STACK_OF(X509_TRUST) *trtable = NULL; | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | static int tr_cmp(const X509_TRUST *const *a, const X509_TRUST *const *b) | - | ||||||||||||
| 53 | { | - | ||||||||||||
| 54 | return (*a)->trust - (*b)->trust; never executed: return (*a)->trust - (*b)->trust; | 0 | ||||||||||||
| 55 | } | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *, | - | ||||||||||||
| 58 | int) { | - | ||||||||||||
| 59 | int (*oldtrust) (int, X509 *, int); | - | ||||||||||||
| 60 | oldtrust = default_trust; | - | ||||||||||||
| 61 | default_trust = trust; | - | ||||||||||||
| 62 | return oldtrust; never executed: return oldtrust; | 0 | ||||||||||||
| 63 | } | - | ||||||||||||
| 64 | - | |||||||||||||
| 65 | int X509_check_trust(X509 *x, int id, int flags) | - | ||||||||||||
| 66 | { | - | ||||||||||||
| 67 | X509_TRUST *pt; | - | ||||||||||||
| 68 | int idx; | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | /* We get this as a default value */ | - | ||||||||||||
| 71 | if (id == X509_TRUST_DEFAULT)
| 22-2441 | ||||||||||||
| 72 | return obj_trust(NID_anyExtendedKeyUsage, x, executed 22 times by 1 test: return obj_trust(910, x, flags | (1U << 3));Executed by:
| 22 | ||||||||||||
| 73 | flags | X509_TRUST_DO_SS_COMPAT); executed 22 times by 1 test: return obj_trust(910, x, flags | (1U << 3));Executed by:
| 22 | ||||||||||||
| 74 | idx = X509_TRUST_get_by_id(id); | - | ||||||||||||
| 75 | if (idx == -1)
| 3-2438 | ||||||||||||
| 76 | return default_trust(id, x, flags); executed 3 times by 1 test: return default_trust(id, x, flags);Executed by:
| 3 | ||||||||||||
| 77 | pt = X509_TRUST_get0(idx); | - | ||||||||||||
| 78 | return pt->check_trust(pt, x, flags); executed 2438 times by 1 test: return pt->check_trust(pt, x, flags);Executed by:
| 2438 | ||||||||||||
| 79 | } | - | ||||||||||||
| 80 | - | |||||||||||||
| 81 | int X509_TRUST_get_count(void) | - | ||||||||||||
| 82 | { | - | ||||||||||||
| 83 | if (!trtable)
| 0 | ||||||||||||
| 84 | return X509_TRUST_COUNT; never executed: return (sizeof(trstandard)/sizeof((trstandard)[0])); | 0 | ||||||||||||
| 85 | return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT; never executed: return sk_X509_TRUST_num(trtable) + (sizeof(trstandard)/sizeof((trstandard)[0])); | 0 | ||||||||||||
| 86 | } | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | X509_TRUST *X509_TRUST_get0(int idx) | - | ||||||||||||
| 89 | { | - | ||||||||||||
| 90 | if (idx < 0)
| 0-2438 | ||||||||||||
| 91 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
| 92 | if (idx < (int)X509_TRUST_COUNT)
| 0-2438 | ||||||||||||
| 93 | return trstandard + idx; executed 2438 times by 1 test: return trstandard + idx;Executed by:
| 2438 | ||||||||||||
| 94 | return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT); never executed: return sk_X509_TRUST_value(trtable, idx - (sizeof(trstandard)/sizeof((trstandard)[0]))); | 0 | ||||||||||||
| 95 | } | - | ||||||||||||
| 96 | - | |||||||||||||
| 97 | int X509_TRUST_get_by_id(int id) | - | ||||||||||||
| 98 | { | - | ||||||||||||
| 99 | X509_TRUST tmp; | - | ||||||||||||
| 100 | int idx; | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
| 0-2466 | ||||||||||||
| 103 | return id - X509_TRUST_MIN; executed 2463 times by 1 test: return id - 1;Executed by:
| 2463 | ||||||||||||
| 104 | if (trtable == NULL)
| 0-3 | ||||||||||||
| 105 | return -1; executed 3 times by 1 test: return -1;Executed by:
| 3 | ||||||||||||
| 106 | tmp.trust = id; | - | ||||||||||||
| 107 | idx = sk_X509_TRUST_find(trtable, &tmp); | - | ||||||||||||
| 108 | if (idx < 0)
| 0 | ||||||||||||
| 109 | return -1; never executed: return -1; | 0 | ||||||||||||
| 110 | return idx + X509_TRUST_COUNT; never executed: return idx + (sizeof(trstandard)/sizeof((trstandard)[0])); | 0 | ||||||||||||
| 111 | } | - | ||||||||||||
| 112 | - | |||||||||||||
| 113 | int X509_TRUST_set(int *t, int trust) | - | ||||||||||||
| 114 | { | - | ||||||||||||
| 115 | if (X509_TRUST_get_by_id(trust) == -1) {
| 0 | ||||||||||||
| 116 | X509err(X509_F_X509_TRUST_SET, X509_R_INVALID_TRUST); | - | ||||||||||||
| 117 | return 0; never executed: return 0; | 0 | ||||||||||||
| 118 | } | - | ||||||||||||
| 119 | *t = trust; | - | ||||||||||||
| 120 | return 1; never executed: return 1; | 0 | ||||||||||||
| 121 | } | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int), | - | ||||||||||||
| 124 | const char *name, int arg1, void *arg2) | - | ||||||||||||
| 125 | { | - | ||||||||||||
| 126 | int idx; | - | ||||||||||||
| 127 | X509_TRUST *trtmp; | - | ||||||||||||
| 128 | /* | - | ||||||||||||
| 129 | * This is set according to what we change: application can't set it | - | ||||||||||||
| 130 | */ | - | ||||||||||||
| 131 | flags &= ~X509_TRUST_DYNAMIC; | - | ||||||||||||
| 132 | /* This will always be set for application modified trust entries */ | - | ||||||||||||
| 133 | flags |= X509_TRUST_DYNAMIC_NAME; | - | ||||||||||||
| 134 | /* Get existing entry if any */ | - | ||||||||||||
| 135 | idx = X509_TRUST_get_by_id(id); | - | ||||||||||||
| 136 | /* Need a new entry */ | - | ||||||||||||
| 137 | if (idx == -1) {
| 0 | ||||||||||||
| 138 | if ((trtmp = OPENSSL_malloc(sizeof(*trtmp))) == NULL) {
| 0 | ||||||||||||
| 139 | X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 140 | return 0; never executed: return 0; | 0 | ||||||||||||
| 141 | } | - | ||||||||||||
| 142 | trtmp->flags = X509_TRUST_DYNAMIC; | - | ||||||||||||
| 143 | } else never executed: end of block | 0 | ||||||||||||
| 144 | trtmp = X509_TRUST_get0(idx); never executed: trtmp = X509_TRUST_get0(idx); | 0 | ||||||||||||
| 145 | - | |||||||||||||
| 146 | /* OPENSSL_free existing name if dynamic */ | - | ||||||||||||
| 147 | if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
| 0 | ||||||||||||
| 148 | OPENSSL_free(trtmp->name); never executed: CRYPTO_free(trtmp->name, __FILE__, 148); | 0 | ||||||||||||
| 149 | /* dup supplied name */ | - | ||||||||||||
| 150 | if ((trtmp->name = OPENSSL_strdup(name)) == NULL) {
| 0 | ||||||||||||
| 151 | X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 152 | goto err; never executed: goto err; | 0 | ||||||||||||
| 153 | } | - | ||||||||||||
| 154 | /* Keep the dynamic flag of existing entry */ | - | ||||||||||||
| 155 | trtmp->flags &= X509_TRUST_DYNAMIC; | - | ||||||||||||
| 156 | /* Set all other flags */ | - | ||||||||||||
| 157 | trtmp->flags |= flags; | - | ||||||||||||
| 158 | - | |||||||||||||
| 159 | trtmp->trust = id; | - | ||||||||||||
| 160 | trtmp->check_trust = ck; | - | ||||||||||||
| 161 | trtmp->arg1 = arg1; | - | ||||||||||||
| 162 | trtmp->arg2 = arg2; | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | /* If its a new entry manage the dynamic table */ | - | ||||||||||||
| 165 | if (idx == -1) {
| 0 | ||||||||||||
| 166 | if (trtable == NULL
| 0 | ||||||||||||
| 167 | && (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL) {
| 0 | ||||||||||||
| 168 | X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 169 | goto err;; never executed: goto err; | 0 | ||||||||||||
| 170 | } | - | ||||||||||||
| 171 | if (!sk_X509_TRUST_push(trtable, trtmp)) {
| 0 | ||||||||||||
| 172 | X509err(X509_F_X509_TRUST_ADD, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
| 173 | goto err; never executed: goto err; | 0 | ||||||||||||
| 174 | } | - | ||||||||||||
| 175 | } never executed: end of block | 0 | ||||||||||||
| 176 | return 1; never executed: return 1; | 0 | ||||||||||||
| 177 | err: | - | ||||||||||||
| 178 | if (idx == -1) {
| 0 | ||||||||||||
| 179 | OPENSSL_free(trtmp->name); | - | ||||||||||||
| 180 | OPENSSL_free(trtmp); | - | ||||||||||||
| 181 | } never executed: end of block | 0 | ||||||||||||
| 182 | return 0; never executed: return 0; | 0 | ||||||||||||
| 183 | } | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | static void trtable_free(X509_TRUST *p) | - | ||||||||||||
| 186 | { | - | ||||||||||||
| 187 | if (!p)
| 0 | ||||||||||||
| 188 | return; never executed: return; | 0 | ||||||||||||
| 189 | if (p->flags & X509_TRUST_DYNAMIC) {
| 0 | ||||||||||||
| 190 | if (p->flags & X509_TRUST_DYNAMIC_NAME)
| 0 | ||||||||||||
| 191 | OPENSSL_free(p->name); never executed: CRYPTO_free(p->name, __FILE__, 191); | 0 | ||||||||||||
| 192 | OPENSSL_free(p); | - | ||||||||||||
| 193 | } never executed: end of block | 0 | ||||||||||||
| 194 | } never executed: end of block | 0 | ||||||||||||
| 195 | - | |||||||||||||
| 196 | void X509_TRUST_cleanup(void) | - | ||||||||||||
| 197 | { | - | ||||||||||||
| 198 | sk_X509_TRUST_pop_free(trtable, trtable_free); | - | ||||||||||||
| 199 | trtable = NULL; | - | ||||||||||||
| 200 | } never executed: end of block | 0 | ||||||||||||
| 201 | - | |||||||||||||
| 202 | int X509_TRUST_get_flags(const X509_TRUST *xp) | - | ||||||||||||
| 203 | { | - | ||||||||||||
| 204 | return xp->flags; never executed: return xp->flags; | 0 | ||||||||||||
| 205 | } | - | ||||||||||||
| 206 | - | |||||||||||||
| 207 | char *X509_TRUST_get0_name(const X509_TRUST *xp) | - | ||||||||||||
| 208 | { | - | ||||||||||||
| 209 | return xp->name; never executed: return xp->name; | 0 | ||||||||||||
| 210 | } | - | ||||||||||||
| 211 | - | |||||||||||||
| 212 | int X509_TRUST_get_trust(const X509_TRUST *xp) | - | ||||||||||||
| 213 | { | - | ||||||||||||
| 214 | return xp->trust; never executed: return xp->trust; | 0 | ||||||||||||
| 215 | } | - | ||||||||||||
| 216 | - | |||||||||||||
| 217 | static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags) | - | ||||||||||||
| 218 | { | - | ||||||||||||
| 219 | /* | - | ||||||||||||
| 220 | * Declare the chain verified if the desired trust OID is not rejected in | - | ||||||||||||
| 221 | * any auxiliary trust info for this certificate, and the OID is either | - | ||||||||||||
| 222 | * expressly trusted, or else either "anyEKU" is trusted, or the | - | ||||||||||||
| 223 | * certificate is self-signed. | - | ||||||||||||
| 224 | */ | - | ||||||||||||
| 225 | flags |= X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU; | - | ||||||||||||
| 226 | return obj_trust(trust->arg1, x, flags); executed 2394 times by 1 test: return obj_trust(trust->arg1, x, flags);Executed by:
| 2394 | ||||||||||||
| 227 | } | - | ||||||||||||
| 228 | - | |||||||||||||
| 229 | static int trust_1oid(X509_TRUST *trust, X509 *x, int flags) | - | ||||||||||||
| 230 | { | - | ||||||||||||
| 231 | /* | - | ||||||||||||
| 232 | * Declare the chain verified only if the desired trust OID is not | - | ||||||||||||
| 233 | * rejected and is expressly trusted. Neither "anyEKU" nor "compat" | - | ||||||||||||
| 234 | * trust in self-signed certificates apply. | - | ||||||||||||
| 235 | */ | - | ||||||||||||
| 236 | flags &= ~(X509_TRUST_DO_SS_COMPAT | X509_TRUST_OK_ANY_EKU); | - | ||||||||||||
| 237 | return obj_trust(trust->arg1, x, flags); never executed: return obj_trust(trust->arg1, x, flags); | 0 | ||||||||||||
| 238 | } | - | ||||||||||||
| 239 | - | |||||||||||||
| 240 | static int trust_compat(X509_TRUST *trust, X509 *x, int flags) | - | ||||||||||||
| 241 | { | - | ||||||||||||
| 242 | /* Call for side-effect of computing hash and caching extensions */ | - | ||||||||||||
| 243 | X509_check_purpose(x, -1, 0); | - | ||||||||||||
| 244 | if ((flags & X509_TRUST_NO_SS_COMPAT) == 0 && x->ex_flags & EXFLAG_SS)
| 24-1194 | ||||||||||||
| 245 | return X509_TRUST_TRUSTED; executed 1168 times by 1 test: return 1;Executed by:
| 1168 | ||||||||||||
| 246 | else | - | ||||||||||||
| 247 | return X509_TRUST_UNTRUSTED; executed 1218 times by 1 test: return 3;Executed by:
| 1218 | ||||||||||||
| 248 | } | - | ||||||||||||
| 249 | - | |||||||||||||
| 250 | static int obj_trust(int id, X509 *x, int flags) | - | ||||||||||||
| 251 | { | - | ||||||||||||
| 252 | X509_CERT_AUX *ax = x->aux; | - | ||||||||||||
| 253 | int i; | - | ||||||||||||
| 254 | - | |||||||||||||
| 255 | if (ax && ax->reject) {
| 27-2335 | ||||||||||||
| 256 | for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
| 10-27 | ||||||||||||
| 257 | ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i); | - | ||||||||||||
| 258 | int nid = OBJ_obj2nid(obj); | - | ||||||||||||
| 259 | - | |||||||||||||
| 260 | if (nid == id || (nid == NID_anyExtendedKeyUsage &&
| 7-17 | ||||||||||||
| 261 | (flags & X509_TRUST_OK_ANY_EKU)))
| 0-7 | ||||||||||||
| 262 | return X509_TRUST_REJECTED; executed 17 times by 1 test: return 2;Executed by:
| 17 | ||||||||||||
| 263 | } executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||||||||
| 264 | } executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||||||||
| 265 | - | |||||||||||||
| 266 | if (ax && ax->trust) {
| 10-2335 | ||||||||||||
| 267 | for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
| 9-57 | ||||||||||||
| 268 | ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i); | - | ||||||||||||
| 269 | int nid = OBJ_obj2nid(obj); | - | ||||||||||||
| 270 | - | |||||||||||||
| 271 | if (nid == id || (nid == NID_anyExtendedKeyUsage &&
| 9-29 | ||||||||||||
| 272 | (flags & X509_TRUST_OK_ANY_EKU)))
| 0-20 | ||||||||||||
| 273 | return X509_TRUST_TRUSTED; executed 48 times by 1 test: return 1;Executed by:
| 48 | ||||||||||||
| 274 | } executed 9 times by 1 test: end of blockExecuted by:
| 9 | ||||||||||||
| 275 | /* | - | ||||||||||||
| 276 | * Reject when explicit trust EKU are set and none match. | - | ||||||||||||
| 277 | * | - | ||||||||||||
| 278 | * Returning untrusted is enough for for full chains that end in | - | ||||||||||||
| 279 | * self-signed roots, because when explicit trust is specified it | - | ||||||||||||
| 280 | * suppresses the default blanket trust of self-signed objects. | - | ||||||||||||
| 281 | * | - | ||||||||||||
| 282 | * But for partial chains, this is not enough, because absent a similar | - | ||||||||||||
| 283 | * trust-self-signed policy, non matching EKUs are indistinguishable | - | ||||||||||||
| 284 | * from lack of EKU constraints. | - | ||||||||||||
| 285 | * | - | ||||||||||||
| 286 | * Therefore, failure to match any trusted purpose must trigger an | - | ||||||||||||
| 287 | * explicit reject. | - | ||||||||||||
| 288 | */ | - | ||||||||||||
| 289 | return X509_TRUST_REJECTED; executed 9 times by 1 test: return 2;Executed by:
| 9 | ||||||||||||
| 290 | } | - | ||||||||||||
| 291 | - | |||||||||||||
| 292 | if ((flags & X509_TRUST_DO_SS_COMPAT) == 0)
| 3-2342 | ||||||||||||
| 293 | return X509_TRUST_UNTRUSTED; executed 3 times by 1 test: return 3;Executed by:
| 3 | ||||||||||||
| 294 | - | |||||||||||||
| 295 | /* | - | ||||||||||||
| 296 | * Not rejected, and there is no list of accepted uses, try compat. | - | ||||||||||||
| 297 | */ | - | ||||||||||||
| 298 | return trust_compat(NULL, x, flags); executed 2342 times by 1 test: return trust_compat( ((void *)0) , x, flags);Executed by:
| 2342 | ||||||||||||
| 299 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |