1 /*
2 * Copyright 1999-2016 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 <openssl/err.h>
11 #include <openssl/mem.h>
12 #include <openssl/obj.h>
13 #include <openssl/x509.h>
14
15 #include "../internal.h"
16 #include "internal.h"
17
18
19 typedef struct x509_trust_st X509_TRUST;
20
21 struct x509_trust_st {
22 int trust;
23 int (*check_trust)(const X509_TRUST *, X509 *);
24 int nid;
25 } /* X509_TRUST */;
26
27 static int trust_1oidany(const X509_TRUST *trust, X509 *x);
28 static int trust_compat(const X509_TRUST *trust, X509 *x);
29
30 static int obj_trust(int id, X509 *x);
31
32 static const X509_TRUST trstandard[] = {
33 {X509_TRUST_COMPAT, trust_compat, 0},
34 {X509_TRUST_SSL_CLIENT, trust_1oidany, NID_client_auth},
35 {X509_TRUST_SSL_SERVER, trust_1oidany, NID_server_auth},
36 {X509_TRUST_EMAIL, trust_1oidany, NID_email_protect},
37 {X509_TRUST_OBJECT_SIGN, trust_1oidany, NID_code_sign},
38 {X509_TRUST_TSA, trust_1oidany, NID_time_stamp}};
39
X509_TRUST_get0(int id)40 static const X509_TRUST *X509_TRUST_get0(int id) {
41 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(trstandard); i++) {
42 if (trstandard[i].trust == id) {
43 return &trstandard[i];
44 }
45 }
46 return NULL;
47 }
48
X509_check_trust(X509 * x,int id,int flags)49 int X509_check_trust(X509 *x, int id, int flags) {
50 if (id == -1) {
51 return X509_TRUST_TRUSTED;
52 }
53 // We get this as a default value
54 if (id == 0) {
55 int rv = obj_trust(NID_anyExtendedKeyUsage, x);
56 if (rv != X509_TRUST_UNTRUSTED) {
57 return rv;
58 }
59 return trust_compat(NULL, x);
60 }
61 const X509_TRUST *pt = X509_TRUST_get0(id);
62 if (pt == NULL) {
63 // Unknown trust IDs are silently reintrepreted as NIDs. This is unreachable
64 // from the certificate verifier itself, but wpa_supplicant relies on it.
65 // Note this relies on commonly-used NIDs and trust IDs not colliding.
66 return obj_trust(id, x);
67 }
68 return pt->check_trust(pt, x);
69 }
70
X509_is_valid_trust_id(int trust)71 int X509_is_valid_trust_id(int trust) {
72 return X509_TRUST_get0(trust) != NULL;
73 }
74
trust_1oidany(const X509_TRUST * trust,X509 * x)75 static int trust_1oidany(const X509_TRUST *trust, X509 *x) {
76 if (x->aux && (x->aux->trust || x->aux->reject)) {
77 return obj_trust(trust->nid, x);
78 }
79 // we don't have any trust settings: for compatibility we return trusted
80 // if it is self signed
81 return trust_compat(trust, x);
82 }
83
trust_compat(const X509_TRUST * trust,X509 * x)84 static int trust_compat(const X509_TRUST *trust, X509 *x) {
85 if (!x509v3_cache_extensions(x)) {
86 return X509_TRUST_UNTRUSTED;
87 }
88 if (x->ex_flags & EXFLAG_SS) {
89 return X509_TRUST_TRUSTED;
90 } else {
91 return X509_TRUST_UNTRUSTED;
92 }
93 }
94
obj_trust(int id,X509 * x)95 static int obj_trust(int id, X509 *x) {
96 X509_CERT_AUX *ax = x->aux;
97 if (!ax) {
98 return X509_TRUST_UNTRUSTED;
99 }
100 for (size_t i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
101 const ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->reject, i);
102 if (OBJ_obj2nid(obj) == id) {
103 return X509_TRUST_REJECTED;
104 }
105 }
106 for (size_t i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
107 const ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(ax->trust, i);
108 if (OBJ_obj2nid(obj) == id) {
109 return X509_TRUST_TRUSTED;
110 }
111 }
112 return X509_TRUST_UNTRUSTED;
113 }
114