]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
6ec5fce2 | 2 | * Copyright 1995-2018 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> |
94e84f5e | 16 | #include "internal/x509_int.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 | ||
4acc3e90 DSH |
34 | extern void policy_cache_free(X509_POLICY_CACHE *cache); |
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); | |
49 | policy_cache_free(ret->policy_cache); | |
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 | |
56 | ||
57 | /* fall thru */ | |
58 | ||
0f113f3e | 59 | case ASN1_OP_NEW_POST: |
53649022 BE |
60 | ret->ex_cached = 0; |
61 | ret->ex_kusage = 0; | |
62 | ret->ex_xkusage = 0; | |
63 | ret->ex_nscert = 0; | |
0f113f3e MC |
64 | ret->ex_flags = 0; |
65 | ret->ex_pathlen = -1; | |
f46c2597 | 66 | ret->ex_pcpathlen = -1; |
0f113f3e MC |
67 | ret->skid = NULL; |
68 | ret->akid = NULL; | |
53649022 BE |
69 | ret->policy_cache = NULL; |
70 | ret->altname = NULL; | |
71 | ret->nc = NULL; | |
47bbaa5b | 72 | #ifndef OPENSSL_NO_RFC3779 |
0f113f3e MC |
73 | ret->rfc3779_addr = NULL; |
74 | ret->rfc3779_asid = NULL; | |
47bbaa5b | 75 | #endif |
0f113f3e MC |
76 | ret->aux = NULL; |
77 | ret->crldp = NULL; | |
25a807bc F |
78 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data)) |
79 | return 0; | |
0f113f3e MC |
80 | break; |
81 | ||
0f113f3e MC |
82 | case ASN1_OP_FREE_POST: |
83 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); | |
84 | X509_CERT_AUX_free(ret->aux); | |
85 | ASN1_OCTET_STRING_free(ret->skid); | |
86 | AUTHORITY_KEYID_free(ret->akid); | |
87 | CRL_DIST_POINTS_free(ret->crldp); | |
88 | policy_cache_free(ret->policy_cache); | |
89 | GENERAL_NAMES_free(ret->altname); | |
90 | NAME_CONSTRAINTS_free(ret->nc); | |
47bbaa5b | 91 | #ifndef OPENSSL_NO_RFC3779 |
0f113f3e MC |
92 | sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); |
93 | ASIdentifiers_free(ret->rfc3779_asid); | |
47bbaa5b | 94 | #endif |
0f113f3e | 95 | break; |
d02b48c6 | 96 | |
0f113f3e | 97 | } |
d02b48c6 | 98 | |
0f113f3e | 99 | return 1; |
d02b48c6 | 100 | |
9d6b1ce6 | 101 | } |
d02b48c6 | 102 | |
c001ce33 | 103 | ASN1_SEQUENCE_ref(X509, x509_cb) = { |
5cf6abd8 | 104 | ASN1_EMBED(X509, cert_info, X509_CINF), |
6e63c142 | 105 | ASN1_EMBED(X509, sig_alg, X509_ALGOR), |
81e49438 | 106 | ASN1_EMBED(X509, signature, ASN1_BIT_STRING) |
d339187b | 107 | } ASN1_SEQUENCE_END_ref(X509, X509) |
d02b48c6 | 108 | |
9d6b1ce6 | 109 | IMPLEMENT_ASN1_FUNCTIONS(X509) |
1241126a | 110 | IMPLEMENT_ASN1_DUP_FUNCTION(X509) |
d02b48c6 | 111 | |
dd9d233e | 112 | int X509_set_ex_data(X509 *r, int idx, void *arg) |
0f113f3e | 113 | { |
26a7d938 | 114 | return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
0f113f3e | 115 | } |
56a3fec1 | 116 | |
dd9d233e | 117 | void *X509_get_ex_data(X509 *r, int idx) |
0f113f3e | 118 | { |
26a7d938 | 119 | return CRYPTO_get_ex_data(&r->ex_data, idx); |
0f113f3e MC |
120 | } |
121 | ||
122 | /* | |
123 | * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with | |
124 | * extra info tagged on the end. Since these functions set how a certificate | |
125 | * is trusted they should only be used when the certificate comes from a | |
126 | * reliable source such as local storage. | |
ce1b4fe1 DSH |
127 | */ |
128 | ||
875a644a | 129 | X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) |
ce1b4fe1 | 130 | { |
0f113f3e MC |
131 | const unsigned char *q; |
132 | X509 *ret; | |
5e5d53d3 MC |
133 | int freeret = 0; |
134 | ||
0f113f3e MC |
135 | /* Save start position */ |
136 | q = *pp; | |
5e5d53d3 | 137 | |
cc99bfa7 | 138 | if (a == NULL || *a == NULL) |
5e5d53d3 | 139 | freeret = 1; |
a46c9789 | 140 | ret = d2i_X509(a, &q, length); |
0f113f3e | 141 | /* If certificate unreadable then forget it */ |
cc99bfa7 | 142 | if (ret == NULL) |
0f113f3e MC |
143 | return NULL; |
144 | /* update length */ | |
a46c9789 | 145 | length -= q - *pp; |
605236f6 | 146 | if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length)) |
0f113f3e | 147 | goto err; |
a46c9789 | 148 | *pp = q; |
0f113f3e MC |
149 | return ret; |
150 | err: | |
61986d32 | 151 | if (freeret) { |
5e5d53d3 MC |
152 | X509_free(ret); |
153 | if (a) | |
154 | *a = NULL; | |
155 | } | |
0f113f3e | 156 | return NULL; |
ce1b4fe1 DSH |
157 | } |
158 | ||
fde2257f VD |
159 | /* |
160 | * Serialize trusted certificate to *pp or just return the required buffer | |
161 | * length if pp == NULL. We ultimately want to avoid modifying *pp in the | |
162 | * error path, but that depends on similar hygiene in lower-level functions. | |
163 | * Here we avoid compounding the problem. | |
164 | */ | |
9fdcc21f | 165 | static int i2d_x509_aux_internal(const X509 *a, unsigned char **pp) |
ce1b4fe1 | 166 | { |
446ba8de | 167 | int length, tmplen; |
05aef4bb | 168 | unsigned char *start = pp != NULL ? *pp : NULL; |
fde2257f | 169 | |
fde2257f VD |
170 | /* |
171 | * This might perturb *pp on error, but fixing that belongs in i2d_X509() | |
172 | * not here. It should be that if a == NULL length is zero, but we check | |
173 | * both just in case. | |
174 | */ | |
0f113f3e | 175 | length = i2d_X509(a, pp); |
fde2257f | 176 | if (length <= 0 || a == NULL) |
446ba8de MC |
177 | return length; |
178 | ||
179 | tmplen = i2d_X509_CERT_AUX(a->aux, pp); | |
180 | if (tmplen < 0) { | |
05aef4bb DSH |
181 | if (start != NULL) |
182 | *pp = start; | |
446ba8de MC |
183 | return tmplen; |
184 | } | |
185 | length += tmplen; | |
186 | ||
0f113f3e | 187 | return length; |
ce1b4fe1 | 188 | } |
dfcf48f4 | 189 | |
fde2257f VD |
190 | /* |
191 | * Serialize trusted certificate to *pp, or just return the required buffer | |
192 | * length if pp == NULL. | |
193 | * | |
194 | * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since | |
195 | * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do | |
196 | * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the | |
197 | * allocated buffer. | |
198 | */ | |
9fdcc21f | 199 | int i2d_X509_AUX(const X509 *a, unsigned char **pp) |
fde2257f VD |
200 | { |
201 | int length; | |
202 | unsigned char *tmp; | |
203 | ||
204 | /* Buffer provided by caller */ | |
205 | if (pp == NULL || *pp != NULL) | |
206 | return i2d_x509_aux_internal(a, pp); | |
207 | ||
208 | /* Obtain the combined length */ | |
209 | if ((length = i2d_x509_aux_internal(a, NULL)) <= 0) | |
210 | return length; | |
211 | ||
212 | /* Allocate requisite combined storage */ | |
213 | *pp = tmp = OPENSSL_malloc(length); | |
7fcdbd83 F |
214 | if (tmp == NULL) { |
215 | X509err(X509_F_I2D_X509_AUX, ERR_R_MALLOC_FAILURE); | |
216 | return -1; | |
217 | } | |
fde2257f VD |
218 | |
219 | /* Encode, but keep *pp at the originally malloced pointer */ | |
220 | length = i2d_x509_aux_internal(a, &tmp); | |
221 | if (length <= 0) { | |
222 | OPENSSL_free(*pp); | |
223 | *pp = NULL; | |
224 | } | |
225 | return length; | |
226 | } | |
227 | ||
95b1752c | 228 | int i2d_re_X509_tbs(X509 *x, unsigned char **pp) |
0f113f3e | 229 | { |
5cf6abd8 DSH |
230 | x->cert_info.enc.modified = 1; |
231 | return i2d_X509_CINF(&x->cert_info, pp); | |
0f113f3e | 232 | } |
95b1752c | 233 | |
8adc1cb8 DSH |
234 | void X509_get0_signature(const ASN1_BIT_STRING **psig, |
235 | const X509_ALGOR **palg, const X509 *x) | |
0f113f3e MC |
236 | { |
237 | if (psig) | |
81e49438 | 238 | *psig = &x->signature; |
0f113f3e | 239 | if (palg) |
6e63c142 | 240 | *palg = &x->sig_alg; |
0f113f3e | 241 | } |
dfcf48f4 DSH |
242 | |
243 | int X509_get_signature_nid(const X509 *x) | |
0f113f3e | 244 | { |
6e63c142 | 245 | return OBJ_obj2nid(x->sig_alg.algorithm); |
0f113f3e | 246 | } |
8267becb | 247 | |
248 | #ifndef OPENSSL_NO_SM2 | |
249 | void X509_set_sm2_id(X509 *x, ASN1_OCTET_STRING *sm2_id) | |
250 | { | |
251 | x->sm2_id = *sm2_id; | |
252 | } | |
253 | ||
254 | ASN1_OCTET_STRING *X509_get0_sm2_id(X509 *x) | |
255 | { | |
256 | return &x->sm2_id; | |
257 | } | |
258 | #endif |