| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <assert.h> |
| 6 |
#include <limits.h> |
| 7 |
|
| 8 |
#include <openssl/err.h> |
| 9 |
#include <openssl/mem.h> |
| 10 |
#include <openssl/obj.h> |
| 11 |
#include <openssl/x509.h> |
| 12 |
|
| 13 |
#include "../internal.h" |
| 14 |
#include "internal.h" |
| 15 |
|
| 16 |
|
| 17 |
static int trust_1oidany(const X509_TRUST *trust, X509 *x); |
| 18 |
static int trust_compat(const X509_TRUST *trust, X509 *x); |
| 19 |
|
| 20 |
static int obj_trust(int id, X509 *x); |
| 21 |
|
| 22 |
static const X509_TRUST trstandard[] = { |
| 23 |
{X509_TRUST_COMPAT, 0, trust_compat, (char *)"compatible", 0, NULL}, |
| 24 |
{X509_TRUST_SSL_CLIENT, 0, trust_1oidany, (char *)"SSL Client", |
| 25 |
NID_client_auth, NULL}, |
| 26 |
{X509_TRUST_SSL_SERVER, 0, trust_1oidany, (char *)"SSL Server", |
| 27 |
NID_server_auth, NULL}, |
| 28 |
{X509_TRUST_EMAIL, 0, trust_1oidany, (char *)"S/MIME email", |
| 29 |
NID_email_protect, NULL}, |
| 30 |
{X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, (char *)"Object Signer", |
| 31 |
NID_code_sign, NULL}, |
| 32 |
{X509_TRUST_TSA, 0, trust_1oidany, (char *)"TSA server", NID_time_stamp, |
| 33 |
NULL}}; |
| 34 |
|
| 35 |
int X509_check_trust(X509 *x, int id, int flags) { |
| 36 |
if (id == -1) { |
| 37 |
return X509_TRUST_TRUSTED; |
| 38 |
} |
| 39 |
// We get this as a default value |
| 40 |
if (id == 0) { |
| 41 |
int rv = obj_trust(NID_anyExtendedKeyUsage, x); |
| 42 |
if (rv != X509_TRUST_UNTRUSTED) { |
| 43 |
return rv; |
| 44 |
} |
| 45 |
return trust_compat(NULL, x); |
| 46 |
} |
| 47 |
int idx = X509_TRUST_get_by_id(id); |
| 48 |
if (idx == -1) { |
| 49 |
return obj_trust(id, x); |
| 50 |
} |
| 51 |
const X509_TRUST *pt = X509_TRUST_get0(idx); |
| 52 |
return pt->check_trust(pt, x); |
| 53 |
} |
| 54 |
|
| 55 |
int X509_TRUST_get_count(void) { return OPENSSL_ARRAY_SIZE(trstandard); } |
| 56 |
|
| 57 |
const X509_TRUST *X509_TRUST_get0(int idx) { |
| 58 |
if (idx < 0 || (size_t)idx >= OPENSSL_ARRAY_SIZE(trstandard)) { |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
return trstandard + idx; |
| 62 |
} |
| 63 |
|
| 64 |
int X509_TRUST_get_by_id(int id) { |
| 65 |
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(trstandard); i++) { |
| 66 |
if (trstandard[i].trust == id) { |
| 67 |
OPENSSL_STATIC_ASSERT(OPENSSL_ARRAY_SIZE(trstandard) <= INT_MAX, |
| 68 |
indices_must_fit_in_int); |
| 69 |
return (int)i; |
| 70 |
} |
| 71 |
} |
| 72 |
return -1; |
| 73 |
} |
| 74 |
|
| 75 |
int X509_TRUST_set(int *t, int trust) { |
| 76 |
if (X509_TRUST_get_by_id(trust) == -1) { |
| 77 |
OPENSSL_PUT_ERROR(X509, X509_R_INVALID_TRUST); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
*t = trust; |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
int X509_TRUST_get_flags(const X509_TRUST *xp) { return xp->flags; } |
| 85 |
|
| 86 |
char *X509_TRUST_get0_name(const X509_TRUST *xp) { return xp->name; } |
| 87 |
|
| 88 |
int X509_TRUST_get_trust(const X509_TRUST *xp) { return xp->trust; } |
| 89 |
|
| 90 |
void X509_TRUST_cleanup(void) { |
| 91 |
// This is an intentional No-Op (no operation) function. |
| 92 |
// |
| 93 |
// Historical Context: |
| 94 |
// - This function existed in OpenSSL versions prior to 1.1.0 |
| 95 |
// - AWS-LC does not support static trust settings storage |
| 96 |
// |
| 97 |
// - Kept for API compatibility with older versions |
| 98 |
} |
| 99 |
|
| 100 |
static int trust_1oidany(const X509_TRUST *trust, X509 *x) { |
| 101 |
if (x->aux && (x->aux->trust || x->aux->reject)) { |
| 102 |
return obj_trust(trust->arg1, x); |
| 103 |
} |
| 104 |
// we don't have any trust settings: for compatibility we return trusted |
| 105 |
// if it is self signed |
| 106 |
return trust_compat(trust, x); |
| 107 |
} |
| 108 |
|
| 109 |
static int trust_compat(const X509_TRUST *trust, X509 *x) { |
| 110 |
if (!x509v3_cache_extensions(x)) { |
| 111 |
return X509_TRUST_UNTRUSTED; |
| 112 |
} |
| 113 |
if (x->ex_flags & EXFLAG_SS) { |
| 114 |
return X509_TRUST_TRUSTED; |
| 115 |
} else { |
| 116 |
return X509_TRUST_UNTRUSTED; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static int obj_trust(int id, X509 *x) { |
| 121 |
ASN1_OBJECT *obj; |
| 122 |
size_t i; |
| 123 |
X509_CERT_AUX *ax; |
| 124 |
ax = x->aux; |
| 125 |
if (!ax) { |
| 126 |
return X509_TRUST_UNTRUSTED; |
| 127 |
} |
| 128 |
if (ax->reject) { |
| 129 |
for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) { |
| 130 |
obj = sk_ASN1_OBJECT_value(ax->reject, i); |
| 131 |
if (OBJ_obj2nid(obj) == id) { |
| 132 |
return X509_TRUST_REJECTED; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
if (ax->trust) { |
| 137 |
for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) { |
| 138 |
obj = sk_ASN1_OBJECT_value(ax->trust, i); |
| 139 |
if (OBJ_obj2nid(obj) == id) { |
| 140 |
return X509_TRUST_TRUSTED; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
return X509_TRUST_UNTRUSTED; |
| 145 |
} |