]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
4333b89f | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
3e4b43b9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 | 12 | #include <openssl/evp.h> |
9d6b1ce6 | 13 | #include <openssl/asn1t.h> |
f0e8ae72 | 14 | #include <openssl/x509.h> |
2f043896 | 15 | #include <openssl/x509v3.h> |
25f2138b | 16 | #include "crypto/x509.h" |
d02b48c6 | 17 | |
39239280 | 18 | ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = { |
0f113f3e | 19 | ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0), |
81e49438 | 20 | ASN1_EMBED(X509_CINF, serialNumber, ASN1_INTEGER), |
6e63c142 | 21 | ASN1_EMBED(X509_CINF, signature, X509_ALGOR), |
0f113f3e | 22 | ASN1_SIMPLE(X509_CINF, issuer, X509_NAME), |
2869e79f | 23 | ASN1_EMBED(X509_CINF, validity, X509_VAL), |
0f113f3e MC |
24 | ASN1_SIMPLE(X509_CINF, subject, X509_NAME), |
25 | ASN1_SIMPLE(X509_CINF, key, X509_PUBKEY), | |
26 | ASN1_IMP_OPT(X509_CINF, issuerUID, ASN1_BIT_STRING, 1), | |
27 | ASN1_IMP_OPT(X509_CINF, subjectUID, ASN1_BIT_STRING, 2), | |
28 | ASN1_EXP_SEQUENCE_OF_OPT(X509_CINF, extensions, X509_EXTENSION, 3) | |
39239280 | 29 | } ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF) |
9d6b1ce6 DSH |
30 | |
31 | IMPLEMENT_ASN1_FUNCTIONS(X509_CINF) | |
32 | /* X509 top level structure needs a bit of customisation */ | |
33 | ||
b54cab31 | 34 | extern void ossl_policy_cache_free(X509_POLICY_CACHE *cache); |
4acc3e90 | 35 | |
24484759 | 36 | static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
0f113f3e | 37 | void *exarg) |
9d6b1ce6 | 38 | { |
0f113f3e | 39 | X509 *ret = (X509 *)*pval; |
9d6b1ce6 | 40 | |
0f113f3e | 41 | switch (operation) { |
9d6b1ce6 | 42 | |
53649022 BE |
43 | case ASN1_OP_D2I_PRE: |
44 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); | |
45 | X509_CERT_AUX_free(ret->aux); | |
46 | ASN1_OCTET_STRING_free(ret->skid); | |
47 | AUTHORITY_KEYID_free(ret->akid); | |
48 | CRL_DIST_POINTS_free(ret->crldp); | |
b54cab31 | 49 | ossl_policy_cache_free(ret->policy_cache); |
53649022 BE |
50 | GENERAL_NAMES_free(ret->altname); |
51 | NAME_CONSTRAINTS_free(ret->nc); | |
52 | #ifndef OPENSSL_NO_RFC3779 | |
53 | sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); | |
54 | ASIdentifiers_free(ret->rfc3779_asid); | |
55 | #endif | |
70a7dd6f | 56 | ASN1_OCTET_STRING_free(ret->distinguishing_id); |
53649022 BE |
57 | |
58 | /* fall thru */ | |
59 | ||
0f113f3e | 60 | case ASN1_OP_NEW_POST: |
53649022 BE |
61 | ret->ex_cached = 0; |
62 | ret->ex_kusage = 0; | |
63 | ret->ex_xkusage = 0; | |
64 | ret->ex_nscert = 0; | |
0f113f3e MC |
65 | ret->ex_flags = 0; |
66 | ret->ex_pathlen = -1; | |
f46c2597 | 67 | ret->ex_pcpathlen = -1; |
0f113f3e MC |
68 | ret->skid = NULL; |
69 | ret->akid = NULL; | |
53649022 BE |
70 | ret->policy_cache = NULL; |
71 | ret->altname = NULL; | |
72 | ret->nc = NULL; | |
47bbaa5b | 73 | #ifndef OPENSSL_NO_RFC3779 |
0f113f3e MC |
74 | ret->rfc3779_addr = NULL; |
75 | ret->rfc3779_asid = NULL; | |
ccf45361 | 76 | #endif |
70a7dd6f | 77 | ret->distinguishing_id = NULL; |
0f113f3e MC |
78 | ret->aux = NULL; |
79 | ret->crldp = NULL; | |
25a807bc F |
80 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data)) |
81 | return 0; | |
0f113f3e MC |
82 | break; |
83 | ||
0f113f3e MC |
84 | case ASN1_OP_FREE_POST: |
85 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); | |
86 | X509_CERT_AUX_free(ret->aux); | |
87 | ASN1_OCTET_STRING_free(ret->skid); | |
88 | AUTHORITY_KEYID_free(ret->akid); | |
89 | CRL_DIST_POINTS_free(ret->crldp); | |
b54cab31 | 90 | ossl_policy_cache_free(ret->policy_cache); |
0f113f3e MC |
91 | GENERAL_NAMES_free(ret->altname); |
92 | NAME_CONSTRAINTS_free(ret->nc); | |
47bbaa5b | 93 | #ifndef OPENSSL_NO_RFC3779 |
0f113f3e MC |
94 | sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); |
95 | ASIdentifiers_free(ret->rfc3779_asid); | |
ccf45361 | 96 | #endif |
70a7dd6f | 97 | ASN1_OCTET_STRING_free(ret->distinguishing_id); |
22b9230f | 98 | OPENSSL_free(ret->propq); |
0f113f3e | 99 | break; |
d02b48c6 | 100 | |
ee46dfbf DDO |
101 | case ASN1_OP_DUP_POST: |
102 | { | |
103 | X509 *old = exarg; | |
104 | ||
4669015d | 105 | if (!ossl_x509_set0_libctx(ret, old->libctx, old->propq)) |
22b9230f | 106 | return 0; |
ee46dfbf DDO |
107 | } |
108 | break; | |
ee46dfbf DDO |
109 | default: |
110 | break; | |
0f113f3e | 111 | } |
d02b48c6 | 112 | |
0f113f3e | 113 | return 1; |
9d6b1ce6 | 114 | } |
d02b48c6 | 115 | |
c001ce33 | 116 | ASN1_SEQUENCE_ref(X509, x509_cb) = { |
5cf6abd8 | 117 | ASN1_EMBED(X509, cert_info, X509_CINF), |
6e63c142 | 118 | ASN1_EMBED(X509, sig_alg, X509_ALGOR), |
81e49438 | 119 | ASN1_EMBED(X509, signature, ASN1_BIT_STRING) |
d339187b | 120 | } ASN1_SEQUENCE_END_ref(X509, X509) |
d02b48c6 | 121 | |
6725682d | 122 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(X509, X509, X509) |
1241126a | 123 | IMPLEMENT_ASN1_DUP_FUNCTION(X509) |
d02b48c6 | 124 | |
6725682d SL |
125 | X509 *d2i_X509(X509 **a, const unsigned char **in, long len) |
126 | { | |
127 | X509 *cert = NULL; | |
3339606a | 128 | int free_on_error = a != NULL && *a == NULL; |
6725682d SL |
129 | |
130 | cert = (X509 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, (X509_it())); | |
131 | /* Only cache the extensions if the cert object was passed in */ | |
1c0eede9 | 132 | if (cert != NULL && a != NULL) { /* then cert == *a */ |
4669015d | 133 | if (!ossl_x509v3_cache_extensions(cert)) { |
66ddc075 P |
134 | if (free_on_error) { |
135 | *a = NULL; | |
3339606a | 136 | X509_free(cert); |
66ddc075 | 137 | } |
6725682d | 138 | cert = NULL; |
3339606a | 139 | } |
6725682d SL |
140 | } |
141 | return cert; | |
142 | } | |
143 | int i2d_X509(const X509 *a, unsigned char **out) | |
144 | { | |
145 | return ASN1_item_i2d((const ASN1_VALUE *)a, out, (X509_it())); | |
146 | } | |
147 | ||
dcc679cd SL |
148 | /* |
149 | * This should only be used if the X509 object was embedded inside another | |
150 | * asn1 object and it needs a libctx to operate. | |
d8652be0 | 151 | * Use X509_new_ex() instead if possible. |
dcc679cd | 152 | */ |
4669015d | 153 | int ossl_x509_set0_libctx(X509 *x, OSSL_LIB_CTX *libctx, const char *propq) |
dcc679cd SL |
154 | { |
155 | if (x != NULL) { | |
156 | x->libctx = libctx; | |
22b9230f SL |
157 | OPENSSL_free(x->propq); |
158 | x->propq = NULL; | |
159 | if (propq != NULL) { | |
160 | x->propq = OPENSSL_strdup(propq); | |
161 | if (x->propq == NULL) | |
162 | return 0; | |
163 | } | |
dcc679cd SL |
164 | } |
165 | return 1; | |
166 | } | |
167 | ||
b4250010 | 168 | X509 *X509_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
6725682d SL |
169 | { |
170 | X509 *cert = NULL; | |
171 | ||
c8a9af97 | 172 | cert = (X509 *)ASN1_item_new_ex(X509_it(), libctx, propq); |
4669015d | 173 | if (!ossl_x509_set0_libctx(cert, libctx, propq)) { |
22b9230f SL |
174 | X509_free(cert); |
175 | cert = NULL; | |
176 | } | |
6725682d SL |
177 | return cert; |
178 | } | |
179 | ||
dd9d233e | 180 | int X509_set_ex_data(X509 *r, int idx, void *arg) |
0f113f3e | 181 | { |
26a7d938 | 182 | return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
0f113f3e | 183 | } |
56a3fec1 | 184 | |
8cc86b81 | 185 | void *X509_get_ex_data(const X509 *r, int idx) |
0f113f3e | 186 | { |
26a7d938 | 187 | return CRYPTO_get_ex_data(&r->ex_data, idx); |
0f113f3e MC |
188 | } |
189 | ||
190 | /* | |
191 | * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with | |
192 | * extra info tagged on the end. Since these functions set how a certificate | |
193 | * is trusted they should only be used when the certificate comes from a | |
194 | * reliable source such as local storage. | |
ce1b4fe1 DSH |
195 | */ |
196 | ||
875a644a | 197 | X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) |
ce1b4fe1 | 198 | { |
0f113f3e MC |
199 | const unsigned char *q; |
200 | X509 *ret; | |
5e5d53d3 MC |
201 | int freeret = 0; |
202 | ||
0f113f3e MC |
203 | /* Save start position */ |
204 | q = *pp; | |
5e5d53d3 | 205 | |
cc99bfa7 | 206 | if (a == NULL || *a == NULL) |
5e5d53d3 | 207 | freeret = 1; |
a46c9789 | 208 | ret = d2i_X509(a, &q, length); |
0f113f3e | 209 | /* If certificate unreadable then forget it */ |
cc99bfa7 | 210 | if (ret == NULL) |
0f113f3e MC |
211 | return NULL; |
212 | /* update length */ | |
a46c9789 | 213 | length -= q - *pp; |
605236f6 | 214 | if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length)) |
0f113f3e | 215 | goto err; |
a46c9789 | 216 | *pp = q; |
0f113f3e MC |
217 | return ret; |
218 | err: | |
61986d32 | 219 | if (freeret) { |
5e5d53d3 MC |
220 | X509_free(ret); |
221 | if (a) | |
222 | *a = NULL; | |
223 | } | |
0f113f3e | 224 | return NULL; |
ce1b4fe1 DSH |
225 | } |
226 | ||
fde2257f VD |
227 | /* |
228 | * Serialize trusted certificate to *pp or just return the required buffer | |
229 | * length if pp == NULL. We ultimately want to avoid modifying *pp in the | |
230 | * error path, but that depends on similar hygiene in lower-level functions. | |
231 | * Here we avoid compounding the problem. | |
232 | */ | |
9fdcc21f | 233 | static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp) |
ce1b4fe1 | 234 | { |
446ba8de | 235 | int length, tmplen; |
05aef4bb | 236 | unsigned char *start = pp != NULL ? *pp : NULL; |
fde2257f | 237 | |
fde2257f VD |
238 | /* |
239 | * This might perturb *pp on error, but fixing that belongs in i2d_X509() | |
240 | * not here. It should be that if a == NULL length is zero, but we check | |
241 | * both just in case. | |
242 | */ | |
0f113f3e | 243 | length = i2d_X509(a, pp); |
fde2257f | 244 | if (length <= 0 || a == NULL) |
446ba8de MC |
245 | return length; |
246 | ||
247 | tmplen = i2d_X509_CERT_AUX(a->aux, pp); | |
248 | if (tmplen < 0) { | |
05aef4bb DSH |
249 | if (start != NULL) |
250 | *pp = start; | |
446ba8de MC |
251 | return tmplen; |
252 | } | |
253 | length += tmplen; | |
254 | ||
0f113f3e | 255 | return length; |
ce1b4fe1 | 256 | } |
dfcf48f4 | 257 | |
fde2257f VD |
258 | /* |
259 | * Serialize trusted certificate to *pp, or just return the required buffer | |
260 | * length if pp == NULL. | |
261 | * | |
262 | * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since | |
263 | * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do | |
264 | * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the | |
265 | * allocated buffer. | |
266 | */ | |
9fdcc21f | 267 | int i2d_X509_AUX(const X509 *a, unsigned char **pp) |
fde2257f VD |
268 | { |
269 | int length; | |
270 | unsigned char *tmp; | |
271 | ||
272 | /* Buffer provided by caller */ | |
273 | if (pp == NULL || *pp != NULL) | |
274 | return i2d_x509_aux_internal(a, pp); | |
275 | ||
276 | /* Obtain the combined length */ | |
277 | if ((length = i2d_x509_aux_internal(a, NULL)) <= 0) | |
278 | return length; | |
279 | ||
280 | /* Allocate requisite combined storage */ | |
281 | *pp = tmp = OPENSSL_malloc(length); | |
7fcdbd83 | 282 | if (tmp == NULL) { |
9311d0c4 | 283 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
7fcdbd83 F |
284 | return -1; |
285 | } | |
fde2257f VD |
286 | |
287 | /* Encode, but keep *pp at the originally malloced pointer */ | |
288 | length = i2d_x509_aux_internal(a, &tmp); | |
289 | if (length <= 0) { | |
290 | OPENSSL_free(*pp); | |
291 | *pp = NULL; | |
292 | } | |
293 | return length; | |
294 | } | |
295 | ||
95b1752c | 296 | int i2d_re_X509_tbs(X509 *x, unsigned char **pp) |
0f113f3e | 297 | { |
5cf6abd8 DSH |
298 | x->cert_info.enc.modified = 1; |
299 | return i2d_X509_CINF(&x->cert_info, pp); | |
0f113f3e | 300 | } |
95b1752c | 301 | |
8adc1cb8 DSH |
302 | void X509_get0_signature(const ASN1_BIT_STRING **psig, |
303 | const X509_ALGOR **palg, const X509 *x) | |
0f113f3e MC |
304 | { |
305 | if (psig) | |
81e49438 | 306 | *psig = &x->signature; |
0f113f3e | 307 | if (palg) |
6e63c142 | 308 | *palg = &x->sig_alg; |
0f113f3e | 309 | } |
dfcf48f4 DSH |
310 | |
311 | int X509_get_signature_nid(const X509 *x) | |
0f113f3e | 312 | { |
6e63c142 | 313 | return OBJ_obj2nid(x->sig_alg.algorithm); |
0f113f3e | 314 | } |
8267becb | 315 | |
70a7dd6f | 316 | void X509_set0_distinguishing_id(X509 *x, ASN1_OCTET_STRING *d_id) |
8267becb | 317 | { |
70a7dd6f RL |
318 | ASN1_OCTET_STRING_free(x->distinguishing_id); |
319 | x->distinguishing_id = d_id; | |
8267becb | 320 | } |
321 | ||
70a7dd6f | 322 | ASN1_OCTET_STRING *X509_get0_distinguishing_id(X509 *x) |
8267becb | 323 | { |
70a7dd6f | 324 | return x->distinguishing_id; |
8267becb | 325 | } |