x509_trs.c
Go to the documentation of this file.
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 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 #include <openssl/err.h>
58 #include <openssl/mem.h>
59 #include <openssl/obj.h>
60 #include <openssl/x509v3.h>
61 
62 #include "../x509v3/internal.h"
63 #include "internal.h"
64 
65 
66 static int tr_cmp(const X509_TRUST **a, const X509_TRUST **b);
67 static void trtable_free(X509_TRUST *p);
68 
69 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
70 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
71 static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
72 
73 static int obj_trust(int id, X509 *x, int flags);
74 static int (*default_trust) (int id, X509 *x, int flags) = obj_trust;
75 
76 /*
77  * WARNING: the following table should be kept in order of trust and without
78  * any gaps so we can just subtract the minimum trust value to get an index
79  * into the table
80  */
81 
82 static X509_TRUST trstandard[] = {
83  {X509_TRUST_COMPAT, 0, trust_compat, (char *)"compatible", 0, NULL},
84  {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, (char *)"SSL Client",
85  NID_client_auth, NULL},
86  {X509_TRUST_SSL_SERVER, 0, trust_1oidany, (char *)"SSL Server",
87  NID_server_auth, NULL},
88  {X509_TRUST_EMAIL, 0, trust_1oidany, (char *)"S/MIME email",
89  NID_email_protect, NULL},
90  {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, (char *)"Object Signer",
91  NID_code_sign, NULL},
92  {X509_TRUST_OCSP_SIGN, 0, trust_1oid, (char *)"OCSP responder",
93  NID_OCSP_sign, NULL},
94  {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, (char *)"OCSP request",
95  NID_ad_OCSP, NULL},
96  {X509_TRUST_TSA, 0, trust_1oidany, (char *)"TSA server", NID_time_stamp,
97  NULL}
98 };
99 
100 #define X509_TRUST_COUNT (sizeof(trstandard)/sizeof(X509_TRUST))
101 
102 static STACK_OF(X509_TRUST) *trtable = NULL;
103 
104 static int tr_cmp(const X509_TRUST **a, const X509_TRUST **b)
105 {
106  return (*a)->trust - (*b)->trust;
107 }
108 
109 int (*X509_TRUST_set_default(int (*trust) (int, X509 *, int))) (int, X509 *,
110  int) {
111  int (*oldtrust) (int, X509 *, int);
112  oldtrust = default_trust;
113  default_trust = trust;
114  return oldtrust;
115 }
116 
117 int X509_check_trust(X509 *x, int id, int flags)
118 {
119  X509_TRUST *pt;
120  int idx;
121  if (id == -1)
122  return 1;
123  /* We get this as a default value */
124  if (id == 0) {
125  int rv;
127  if (rv != X509_TRUST_UNTRUSTED)
128  return rv;
129  return trust_compat(NULL, x, 0);
130  }
132  if (idx == -1)
133  return default_trust(id, x, flags);
134  pt = X509_TRUST_get0(idx);
135  return pt->check_trust(pt, x, flags);
136 }
137 
139 {
140  if (!trtable)
141  return X509_TRUST_COUNT;
142  return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
143 }
144 
146 {
147  if (idx < 0)
148  return NULL;
149  if (idx < (int)X509_TRUST_COUNT)
150  return trstandard + idx;
151  return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
152 }
153 
155 {
156  X509_TRUST tmp;
157  size_t idx;
158 
159  if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
160  return id - X509_TRUST_MIN;
161  tmp.trust = id;
162  if (!trtable)
163  return -1;
164  sk_X509_TRUST_sort(trtable);
165  if (!sk_X509_TRUST_find(trtable, &idx, &tmp)) {
166  return -1;
167  }
168  return idx + X509_TRUST_COUNT;
169 }
170 
171 int X509_TRUST_set(int *t, int trust)
172 {
173  if (X509_TRUST_get_by_id(trust) == -1) {
175  return 0;
176  }
177  *t = trust;
178  return 1;
179 }
180 
181 int X509_TRUST_add(int id, int flags, int (*ck) (X509_TRUST *, X509 *, int),
182  char *name, int arg1, void *arg2)
183 {
184  int idx;
185  X509_TRUST *trtmp;
186  char *name_dup;
187 
188  /*
189  * This is set according to what we change: application can't set it
190  */
192  /* This will always be set for application modified trust entries */
194  /* Get existing entry if any */
196  /* Need a new entry */
197  if (idx == -1) {
198  if (!(trtmp = OPENSSL_malloc(sizeof(X509_TRUST)))) {
200  return 0;
201  }
202  trtmp->flags = X509_TRUST_DYNAMIC;
203  } else
204  trtmp = X509_TRUST_get0(idx);
205 
206  /* Duplicate the supplied name. */
207  name_dup = OPENSSL_strdup(name);
208  if (name_dup == NULL) {
210  if (idx == -1)
211  OPENSSL_free(trtmp);
212  return 0;
213  }
214 
215  /* OPENSSL_free existing name if dynamic */
216  if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
217  OPENSSL_free(trtmp->name);
218  trtmp->name = name_dup;
219  /* Keep the dynamic flag of existing entry */
220  trtmp->flags &= X509_TRUST_DYNAMIC;
221  /* Set all other flags */
222  trtmp->flags |= flags;
223 
224  trtmp->trust = id;
225  trtmp->check_trust = ck;
226  trtmp->arg1 = arg1;
227  trtmp->arg2 = arg2;
228 
229  /* If its a new entry manage the dynamic table */
230  if (idx == -1) {
231  if (!trtable && !(trtable = sk_X509_TRUST_new(tr_cmp))) {
233  trtable_free(trtmp);
234  return 0;
235  }
236  if (!sk_X509_TRUST_push(trtable, trtmp)) {
238  trtable_free(trtmp);
239  return 0;
240  }
241  }
242  return 1;
243 }
244 
245 static void trtable_free(X509_TRUST *p)
246 {
247  if (!p)
248  return;
249  if (p->flags & X509_TRUST_DYNAMIC) {
250  if (p->flags & X509_TRUST_DYNAMIC_NAME)
251  OPENSSL_free(p->name);
252  OPENSSL_free(p);
253  }
254 }
255 
257 {
258  unsigned int i;
259  for (i = 0; i < X509_TRUST_COUNT; i++)
261  sk_X509_TRUST_pop_free(trtable, trtable_free);
262  trtable = NULL;
263 }
264 
266 {
267  return xp->flags;
268 }
269 
271 {
272  return xp->name;
273 }
274 
276 {
277  return xp->trust;
278 }
279 
280 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
281 {
282  if (x->aux && (x->aux->trust || x->aux->reject))
283  return obj_trust(trust->arg1, x, flags);
284  /*
285  * we don't have any trust settings: for compatibility we return trusted
286  * if it is self signed
287  */
288  return trust_compat(trust, x, flags);
289 }
290 
291 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
292 {
293  if (x->aux)
294  return obj_trust(trust->arg1, x, flags);
295  return X509_TRUST_UNTRUSTED;
296 }
297 
298 static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
299 {
301  return X509_TRUST_UNTRUSTED;
302  if (x->ex_flags & EXFLAG_SS)
303  return X509_TRUST_TRUSTED;
304  else
305  return X509_TRUST_UNTRUSTED;
306 }
307 
308 static int obj_trust(int id, X509 *x, int flags)
309 {
310  ASN1_OBJECT *obj;
311  size_t i;
312  X509_CERT_AUX *ax;
313  ax = x->aux;
314  if (!ax)
315  return X509_TRUST_UNTRUSTED;
316  if (ax->reject) {
317  for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
318  obj = sk_ASN1_OBJECT_value(ax->reject, i);
319  if (OBJ_obj2nid(obj) == id)
320  return X509_TRUST_REJECTED;
321  }
322  }
323  if (ax->trust) {
324  for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
325  obj = sk_ASN1_OBJECT_value(ax->trust, i);
326  if (OBJ_obj2nid(obj) == id)
327  return X509_TRUST_TRUSTED;
328  }
329  }
330  return X509_TRUST_UNTRUSTED;
331 }
NID_server_auth
#define NID_server_auth
Definition: nid.h:662
X509_TRUST_set_default
int(*)(int, X509 *, int) X509_TRUST_set_default(int(*trust)(int, X509 *, int))
Definition: x509_trs.c:109
X509_TRUST_get_count
int X509_TRUST_get_count(void)
Definition: x509_trs.c:138
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
X509_TRUST_DYNAMIC
#define X509_TRUST_DYNAMIC
Definition: x509.h:176
X509_TRUST_get_trust
int X509_TRUST_get_trust(const X509_TRUST *xp)
Definition: x509_trs.c:275
X509_TRUST_OBJECT_SIGN
#define X509_TRUST_OBJECT_SIGN
Definition: x509.h:165
x509_trust_st::check_trust
int(* check_trust)(struct x509_trust_st *, X509 *, int)
Definition: x509.h:149
NID_anyExtendedKeyUsage
#define NID_anyExtendedKeyUsage
Definition: nid.h:4029
X509_TRUST_SSL_SERVER
#define X509_TRUST_SSL_SERVER
Definition: x509.h:163
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
X509_TRUST_get0
X509_TRUST * X509_TRUST_get0(int idx)
Definition: x509_trs.c:145
x509v3.h
X509_TRUST_OCSP_SIGN
#define X509_TRUST_OCSP_SIGN
Definition: x509.h:166
X509_TRUST_TSA
#define X509_TRUST_TSA
Definition: x509.h:168
setup.name
name
Definition: setup.py:542
tr_cmp
static int tr_cmp(const X509_TRUST **a, const X509_TRUST **b)
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
X509_TRUST_TRUSTED
#define X509_TRUST_TRUSTED
Definition: x509.h:181
xds_manager.p
p
Definition: xds_manager.py:60
asn1_object_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:102
X509_TRUST_cleanup
void X509_TRUST_cleanup(void)
Definition: x509_trs.c:256
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
X509_TRUST_add
int X509_TRUST_add(int id, int flags, int(*ck)(X509_TRUST *, X509 *, int), char *name, int arg1, void *arg2)
Definition: x509_trs.c:181
EXFLAG_SS
#define EXFLAG_SS
Definition: x509v3.h:404
x509_trust_st::name
char * name
Definition: x509.h:150
OBJ_obj2nid
#define OBJ_obj2nid
Definition: boringssl_prefix_symbols.h:1857
trust_1oid
static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
Definition: x509_trs.c:291
X509_TRUST_OCSP_REQUEST
#define X509_TRUST_OCSP_REQUEST
Definition: x509.h:167
xds_interop_client.int
int
Definition: xds_interop_client.py:113
x509v3_cache_extensions
#define x509v3_cache_extensions
Definition: boringssl_prefix_symbols.h:3459
x509_trust_st::flags
int flags
Definition: x509.h:148
X509_TRUST_MAX
#define X509_TRUST_MAX
Definition: x509.h:172
X509_R_INVALID_TRUST
#define X509_R_INVALID_TRUST
Definition: x509.h:2390
x509_trust_st::trust
int trust
Definition: x509.h:147
obj_trust
static int obj_trust(int id, X509 *x, int flags)
Definition: x509_trs.c:308
X509_TRUST_get_flags
int X509_TRUST_get_flags(const X509_TRUST *xp)
Definition: x509_trs.c:265
X509_TRUST_COUNT
#define X509_TRUST_COUNT
Definition: x509_trs.c:100
x509_trust_st
Definition: x509.h:146
x509_trust_st::arg1
int arg1
Definition: x509.h:151
NID_email_protect
#define NID_email_protect
Definition: nid.h:677
err.h
trstandard
static X509_TRUST trstandard[]
Definition: x509_trs.c:82
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
X509_TRUST_DYNAMIC_NAME
#define X509_TRUST_DYNAMIC_NAME
Definition: x509.h:177
internal.h
x509_cert_aux_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:109
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
NID_code_sign
#define NID_code_sign
Definition: nid.h:672
setup.idx
idx
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:197
X509_TRUST_get_by_id
int X509_TRUST_get_by_id(int id)
Definition: x509_trs.c:154
trust_1oidany
static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
Definition: x509_trs.c:280
X509_TRUST_MIN
#define X509_TRUST_MIN
Definition: x509.h:171
x509_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:139
OPENSSL_strdup
#define OPENSSL_strdup
Definition: boringssl_prefix_symbols.h:1891
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
trtable_free
static void trtable_free(X509_TRUST *p)
Definition: x509_trs.c:245
X509_TRUST_UNTRUSTED
#define X509_TRUST_UNTRUSTED
Definition: x509.h:183
NID_ad_OCSP
#define NID_ad_OCSP
Definition: nid.h:892
STACK_OF
static STACK_OF(X509_TRUST)
Definition: x509_trs.c:102
default_trust
static int(* default_trust)(int id, X509 *x, int flags)
Definition: x509_trs.c:74
X509_TRUST_EMAIL
#define X509_TRUST_EMAIL
Definition: x509.h:164
NID_client_auth
#define NID_client_auth
Definition: nid.h:667
X509_TRUST_get0_name
char * X509_TRUST_get0_name(const X509_TRUST *xp)
Definition: x509_trs.c:270
x509_trust_st::arg2
void * arg2
Definition: x509.h:152
X509_TRUST_set
int X509_TRUST_set(int *t, int trust)
Definition: x509_trs.c:171
obj.h
flags
uint32_t flags
Definition: retry_filter.cc:632
mem.h
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
X509_TRUST_COMPAT
#define X509_TRUST_COMPAT
Definition: x509.h:161
X509_TRUST_SSL_CLIENT
#define X509_TRUST_SSL_CLIENT
Definition: x509.h:162
NID_OCSP_sign
#define NID_OCSP_sign
Definition: nid.h:902
NID_time_stamp
#define NID_time_stamp
Definition: nid.h:682
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
trust_compat
static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
Definition: x509_trs.c:298
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371
X509_TRUST_REJECTED
#define X509_TRUST_REJECTED
Definition: x509.h:182
id
uint32_t id
Definition: flow_control_fuzzer.cc:70
X509_check_trust
int X509_check_trust(X509 *x, int id, int flags)
Definition: x509_trs.c:117


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:55