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