Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/x_x509.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <assert.h>
5 #include <limits.h>
6 #include <stdio.h>
7
8 #include <openssl/asn1t.h>
9 #include <openssl/evp.h>
10 #include <openssl/mem.h>
11 #include <openssl/obj.h>
12 #include <openssl/pool.h>
13 #include <openssl/thread.h>
14 #include <openssl/x509.h>
15
16 #include "../asn1/internal.h"
17 #include "../internal.h"
18 #include "internal.h"
19
20 static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
21
22 ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = {
23 ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0),
24 ASN1_SIMPLE(X509_CINF, serialNumber, ASN1_INTEGER),
25 ASN1_SIMPLE(X509_CINF, signature, X509_ALGOR),
26 ASN1_SIMPLE(X509_CINF, issuer, X509_NAME),
27 ASN1_SIMPLE(X509_CINF, validity, X509_VAL),
28 ASN1_SIMPLE(X509_CINF, subject, X509_NAME),
29 ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY),
30 ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1),
31 ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2),
32 ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3),
33 } ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF)
34
35 IMPLEMENT_ASN1_FUNCTIONS(X509_CINF)
36 // X509 top level structure needs a bit of customisation
37
38 static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
39 void *exarg) {
40 X509 *ret = (X509 *)*pval;
41
42 switch (operation) {
43 case ASN1_OP_NEW_POST:
44 ret->ex_flags = 0;
45 ret->ex_pathlen = -1;
46 ret->skid = NULL;
47 ret->akid = NULL;
48 ret->aux = NULL;
49 ret->crldp = NULL;
50 ret->buf = NULL;
51 CRYPTO_new_ex_data(&ret->ex_data);
52 CRYPTO_MUTEX_init(&ret->lock);
53 break;
54
55 case ASN1_OP_D2I_PRE:
56 CRYPTO_BUFFER_free(ret->buf);
57 ret->buf = NULL;
58 break;
59
60 case ASN1_OP_D2I_POST: {
61 // The version must be one of v1(0), v2(1), or v3(2).
62 long version = X509_VERSION_1;
63 if (ret->cert_info->version != NULL) {
64 version = ASN1_INTEGER_get(ret->cert_info->version);
65 // TODO(https://crbug.com/boringssl/364): |X509_VERSION_1| should
66 // also be rejected here. This means an explicitly-encoded X.509v1
67 // version. v1 is DEFAULT, so DER requires it be omitted.
68 if (version < X509_VERSION_1 || version > X509_VERSION_3) {
69 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
70 return 0;
71 }
72 }
73
74 // Per RFC 5280, section 4.1.2.8, these fields require v2 or v3.
75 if (version == X509_VERSION_1 && (ret->cert_info->issuerUID != NULL ||
76 ret->cert_info->subjectUID != NULL)) {
77 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_FOR_VERSION);
78 return 0;
79 }
80
81 // Per RFC 5280, section 4.1.2.9, extensions require v3.
82 if (version != X509_VERSION_3 && ret->cert_info->extensions != NULL) {
83 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_FOR_VERSION);
84 return 0;
85 }
86
87 break;
88 }
89
90 case ASN1_OP_FREE_POST:
91 CRYPTO_MUTEX_cleanup(&ret->lock);
92 CRYPTO_free_ex_data(&g_ex_data_class, ret, &ret->ex_data);
93 X509_CERT_AUX_free(ret->aux);
94 ASN1_OCTET_STRING_free(ret->skid);
95 AUTHORITY_KEYID_free(ret->akid);
96 CRL_DIST_POINTS_free(ret->crldp);
97 GENERAL_NAMES_free(ret->altname);
98 NAME_CONSTRAINTS_free(ret->nc);
99 CRYPTO_BUFFER_free(ret->buf);
100 break;
101 }
102
103 return 1;
104 }
105
106 ASN1_SEQUENCE_ref(X509, x509_cb) = {
107 ASN1_SIMPLE(X509, cert_info, X509_CINF),
108 ASN1_SIMPLE(X509, sig_alg, X509_ALGOR),
109 ASN1_SIMPLE(X509, signature, ASN1_BIT_STRING),
110 } ASN1_SEQUENCE_END_ref(X509, X509)
111
112 IMPLEMENT_ASN1_FUNCTIONS(X509)
113
114 IMPLEMENT_ASN1_DUP_FUNCTION(X509)
115
116 X509 *X509_parse_from_buffer(CRYPTO_BUFFER *buf) {
117 if (CRYPTO_BUFFER_len(buf) > LONG_MAX) {
118 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
119 return 0;
120 }
121
122 X509 *x509 = X509_new();
123 if (x509 == NULL) {
124 return NULL;
125 }
126
127 x509->cert_info->enc.alias_only_on_next_parse = 1;
128
129 const uint8_t *inp = CRYPTO_BUFFER_data(buf);
130 X509 *x509p = x509;
131 X509 *ret = d2i_X509(&x509p, &inp, CRYPTO_BUFFER_len(buf));
132 if (ret == NULL ||
133 inp - CRYPTO_BUFFER_data(buf) != (ptrdiff_t)CRYPTO_BUFFER_len(buf)) {
134 X509_free(x509p);
135 return NULL;
136 }
137 assert(x509p == x509);
138 assert(ret == x509);
139
140 CRYPTO_BUFFER_up_ref(buf);
141 ret->buf = buf;
142
143 return ret;
144 }
145
146 int X509_up_ref(X509 *x) {
147 if (x == NULL) {
148 return 0;
149 }
150 CRYPTO_refcount_inc(&x->references);
151 return 1;
152 }
153
154 int X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
155 CRYPTO_EX_dup *dup_unused,
156 CRYPTO_EX_free *free_func) {
157 int index;
158 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
159 free_func)) {
160 return -1;
161 }
162 return index;
163 }
164
165 int X509_set_ex_data(X509 *r, int idx, void *arg) {
166 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
167 }
168
169 void *X509_get_ex_data(X509 *r, int idx) {
170 return (CRYPTO_get_ex_data(&r->ex_data, idx));
171 }
172
173 // X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with
174 // extra info tagged on the end. Since these functions set how a certificate
175 // is trusted they should only be used when the certificate comes from a
176 // reliable source such as local storage.
177
178 X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) {
179 const unsigned char *q = *pp;
180 X509 *ret;
181 int freeret = 0;
182
183 if (!a || *a == NULL) {
184 freeret = 1;
185 }
186 ret = d2i_X509(a, &q, length);
187 // If certificate unreadable then forget it
188 if (!ret) {
189 return NULL;
190 }
191 // update length
192 length -= q - *pp;
193 // Parse auxiliary information if there is any.
194 if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length)) {
195 goto err;
196 }
197 *pp = q;
198 return ret;
199 err:
200 if (freeret) {
201 X509_free(ret);
202 if (a) {
203 *a = NULL;
204 }
205 }
206 return NULL;
207 }
208
209 // Serialize trusted certificate to *pp or just return the required buffer
210 // length if pp == NULL. We ultimately want to avoid modifying *pp in the
211 // error path, but that depends on similar hygiene in lower-level functions.
212 // Here we avoid compounding the problem.
213 static int i2d_x509_aux_internal(X509 *a, unsigned char **pp) {
214 int length, tmplen;
215 unsigned char *start = pp != NULL ? *pp : NULL;
216
217 assert(pp == NULL || *pp != NULL);
218
219 // This might perturb *pp on error, but fixing that belongs in i2d_X509()
220 // not here. It should be that if a == NULL length is zero, but we check
221 // both just in case.
222 length = i2d_X509(a, pp);
223 if (length <= 0 || a == NULL) {
224 return length;
225 }
226
227 if (a->aux != NULL) {
228 tmplen = i2d_X509_CERT_AUX(a->aux, pp);
229 if (tmplen < 0) {
230 if (start != NULL) {
231 *pp = start;
232 }
233 return tmplen;
234 }
235 length += tmplen;
236 }
237
238 return length;
239 }
240
241 // Serialize trusted certificate to *pp, or just return the required buffer
242 // length if pp == NULL.
243 //
244 // When pp is not NULL, but *pp == NULL, we allocate the buffer, but since
245 // we're writing two ASN.1 objects back to back, we can't have i2d_X509() do
246 // the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the
247 // allocated buffer.
248 int i2d_X509_AUX(X509 *a, unsigned char **pp) {
249 int length;
250 unsigned char *tmp;
251
252 // Buffer provided by caller
253 if (pp == NULL || *pp != NULL) {
254 return i2d_x509_aux_internal(a, pp);
255 }
256
257 // Obtain the combined length
258 if ((length = i2d_x509_aux_internal(a, NULL)) <= 0) {
259 return length;
260 }
261
262 // Allocate requisite combined storage
263 *pp = tmp = OPENSSL_malloc(length);
264 if (tmp == NULL) {
265 return -1; // Push error onto error stack?
266 }
267
268 // Encode, but keep *pp at the originally malloced pointer
269 length = i2d_x509_aux_internal(a, &tmp);
270 if (length <= 0) {
271 OPENSSL_free(*pp);
272 *pp = NULL;
273 }
274 return length;
275 }
276
277 int i2d_re_X509_tbs(X509 *x509, unsigned char **outp) {
278 asn1_encoding_clear(&x509->cert_info->enc);
279 return i2d_X509_CINF(x509->cert_info, outp);
280 }
281
282 int i2d_X509_tbs(X509 *x509, unsigned char **outp) {
283 return i2d_X509_CINF(x509->cert_info, outp);
284 }
285
286 int X509_set1_signature_algo(X509 *x509, const X509_ALGOR *algo) {
287 X509_ALGOR *copy1 = X509_ALGOR_dup(algo);
288 X509_ALGOR *copy2 = X509_ALGOR_dup(algo);
289 if (copy1 == NULL || copy2 == NULL) {
290 X509_ALGOR_free(copy1);
291 X509_ALGOR_free(copy2);
292 return 0;
293 }
294
295 X509_ALGOR_free(x509->sig_alg);
296 x509->sig_alg = copy1;
297 X509_ALGOR_free(x509->cert_info->signature);
298 x509->cert_info->signature = copy2;
299 return 1;
300 }
301
302 int X509_set1_signature_value(X509 *x509, const uint8_t *sig, size_t sig_len) {
303 if (!ASN1_STRING_set(x509->signature, sig, sig_len)) {
304 return 0;
305 }
306 x509->signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
307 x509->signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
308 return 1;
309 }
310
311 void X509_get0_signature(const ASN1_BIT_STRING **psig, const X509_ALGOR **palg,
312 const X509 *x) {
313 if (psig) {
314 *psig = x->signature;
315 }
316 if (palg) {
317 *palg = x->sig_alg;
318 }
319 }
320
321 int X509_get_signature_nid(const X509 *x) {
322 return OBJ_obj2nid(x->sig_alg->algorithm);
323 }