/src/openssl111/crypto/x509/x_x509.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/evp.h> |
13 | | #include <openssl/asn1t.h> |
14 | | #include <openssl/x509.h> |
15 | | #include <openssl/x509v3.h> |
16 | | #include "crypto/x509.h" |
17 | | |
18 | | ASN1_SEQUENCE_enc(X509_CINF, enc, 0) = { |
19 | | ASN1_EXP_OPT(X509_CINF, version, ASN1_INTEGER, 0), |
20 | | ASN1_EMBED(X509_CINF, serialNumber, ASN1_INTEGER), |
21 | | ASN1_EMBED(X509_CINF, signature, X509_ALGOR), |
22 | | ASN1_SIMPLE(X509_CINF, issuer, X509_NAME), |
23 | | ASN1_EMBED(X509_CINF, validity, X509_VAL), |
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) |
29 | | } ASN1_SEQUENCE_END_enc(X509_CINF, X509_CINF) |
30 | | |
31 | | IMPLEMENT_ASN1_FUNCTIONS(X509_CINF) |
32 | | /* X509 top level structure needs a bit of customisation */ |
33 | | |
34 | | extern void policy_cache_free(X509_POLICY_CACHE *cache); |
35 | | |
36 | | static int x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
37 | | void *exarg) |
38 | 69.2k | { |
39 | 69.2k | X509 *ret = (X509 *)*pval; |
40 | | |
41 | 69.2k | switch (operation) { |
42 | | |
43 | 9.76k | case ASN1_OP_D2I_PRE: |
44 | 9.76k | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); |
45 | 9.76k | X509_CERT_AUX_free(ret->aux); |
46 | 9.76k | ASN1_OCTET_STRING_free(ret->skid); |
47 | 9.76k | AUTHORITY_KEYID_free(ret->akid); |
48 | 9.76k | CRL_DIST_POINTS_free(ret->crldp); |
49 | 9.76k | policy_cache_free(ret->policy_cache); |
50 | 9.76k | GENERAL_NAMES_free(ret->altname); |
51 | 9.76k | NAME_CONSTRAINTS_free(ret->nc); |
52 | 9.76k | #ifndef OPENSSL_NO_RFC3779 |
53 | 9.76k | sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); |
54 | 9.76k | ASIdentifiers_free(ret->rfc3779_asid); |
55 | 9.76k | #endif |
56 | | |
57 | | /* fall thru */ |
58 | | |
59 | 19.5k | case ASN1_OP_NEW_POST: |
60 | 19.5k | ret->ex_cached = 0; |
61 | 19.5k | ret->ex_kusage = 0; |
62 | 19.5k | ret->ex_xkusage = 0; |
63 | 19.5k | ret->ex_nscert = 0; |
64 | 19.5k | ret->ex_flags = 0; |
65 | 19.5k | ret->ex_pathlen = -1; |
66 | 19.5k | ret->ex_pcpathlen = -1; |
67 | 19.5k | ret->skid = NULL; |
68 | 19.5k | ret->akid = NULL; |
69 | 19.5k | ret->policy_cache = NULL; |
70 | 19.5k | ret->altname = NULL; |
71 | 19.5k | ret->nc = NULL; |
72 | 19.5k | #ifndef OPENSSL_NO_RFC3779 |
73 | 19.5k | ret->rfc3779_addr = NULL; |
74 | 19.5k | ret->rfc3779_asid = NULL; |
75 | 19.5k | #endif |
76 | 19.5k | ret->aux = NULL; |
77 | 19.5k | ret->crldp = NULL; |
78 | 19.5k | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data)) |
79 | 0 | return 0; |
80 | 19.5k | break; |
81 | | |
82 | 19.5k | case ASN1_OP_FREE_POST: |
83 | 9.76k | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data); |
84 | 9.76k | X509_CERT_AUX_free(ret->aux); |
85 | 9.76k | ASN1_OCTET_STRING_free(ret->skid); |
86 | 9.76k | AUTHORITY_KEYID_free(ret->akid); |
87 | 9.76k | CRL_DIST_POINTS_free(ret->crldp); |
88 | 9.76k | policy_cache_free(ret->policy_cache); |
89 | 9.76k | GENERAL_NAMES_free(ret->altname); |
90 | 9.76k | NAME_CONSTRAINTS_free(ret->nc); |
91 | 9.76k | #ifndef OPENSSL_NO_RFC3779 |
92 | 9.76k | sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free); |
93 | 9.76k | ASIdentifiers_free(ret->rfc3779_asid); |
94 | 9.76k | #endif |
95 | 9.76k | break; |
96 | | |
97 | 69.2k | } |
98 | | |
99 | 69.2k | return 1; |
100 | | |
101 | 69.2k | } |
102 | | |
103 | | ASN1_SEQUENCE_ref(X509, x509_cb) = { |
104 | | ASN1_EMBED(X509, cert_info, X509_CINF), |
105 | | ASN1_EMBED(X509, sig_alg, X509_ALGOR), |
106 | | ASN1_EMBED(X509, signature, ASN1_BIT_STRING) |
107 | | } ASN1_SEQUENCE_END_ref(X509, X509) |
108 | | |
109 | | IMPLEMENT_ASN1_FUNCTIONS(X509) |
110 | | |
111 | | IMPLEMENT_ASN1_DUP_FUNCTION(X509) |
112 | | |
113 | | int X509_set_ex_data(X509 *r, int idx, void *arg) |
114 | 0 | { |
115 | 0 | return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
116 | 0 | } |
117 | | |
118 | | void *X509_get_ex_data(X509 *r, int idx) |
119 | 0 | { |
120 | 0 | return CRYPTO_get_ex_data(&r->ex_data, idx); |
121 | 0 | } |
122 | | |
123 | | /* |
124 | | * X509_AUX ASN1 routines. X509_AUX is the name given to a certificate with |
125 | | * extra info tagged on the end. Since these functions set how a certificate |
126 | | * is trusted they should only be used when the certificate comes from a |
127 | | * reliable source such as local storage. |
128 | | */ |
129 | | |
130 | | X509 *d2i_X509_AUX(X509 **a, const unsigned char **pp, long length) |
131 | 0 | { |
132 | 0 | const unsigned char *q; |
133 | 0 | X509 *ret; |
134 | 0 | int freeret = 0; |
135 | | |
136 | | /* Save start position */ |
137 | 0 | q = *pp; |
138 | |
|
139 | 0 | if (a == NULL || *a == NULL) |
140 | 0 | freeret = 1; |
141 | 0 | ret = d2i_X509(a, &q, length); |
142 | | /* If certificate unreadable then forget it */ |
143 | 0 | if (ret == NULL) |
144 | 0 | return NULL; |
145 | | /* update length */ |
146 | 0 | length -= q - *pp; |
147 | 0 | if (length > 0 && !d2i_X509_CERT_AUX(&ret->aux, &q, length)) |
148 | 0 | goto err; |
149 | 0 | *pp = q; |
150 | 0 | return ret; |
151 | 0 | err: |
152 | 0 | if (freeret) { |
153 | 0 | X509_free(ret); |
154 | 0 | if (a) |
155 | 0 | *a = NULL; |
156 | 0 | } |
157 | 0 | return NULL; |
158 | 0 | } |
159 | | |
160 | | /* |
161 | | * Serialize trusted certificate to *pp or just return the required buffer |
162 | | * length if pp == NULL. We ultimately want to avoid modifying *pp in the |
163 | | * error path, but that depends on similar hygiene in lower-level functions. |
164 | | * Here we avoid compounding the problem. |
165 | | */ |
166 | | static int i2d_x509_aux_internal(X509 *a, unsigned char **pp) |
167 | 0 | { |
168 | 0 | int length, tmplen; |
169 | 0 | unsigned char *start = pp != NULL ? *pp : NULL; |
170 | | |
171 | | /* |
172 | | * This might perturb *pp on error, but fixing that belongs in i2d_X509() |
173 | | * not here. It should be that if a == NULL length is zero, but we check |
174 | | * both just in case. |
175 | | */ |
176 | 0 | length = i2d_X509(a, pp); |
177 | 0 | if (length <= 0 || a == NULL) |
178 | 0 | return length; |
179 | | |
180 | 0 | tmplen = i2d_X509_CERT_AUX(a->aux, pp); |
181 | 0 | if (tmplen < 0) { |
182 | 0 | if (start != NULL) |
183 | 0 | *pp = start; |
184 | 0 | return tmplen; |
185 | 0 | } |
186 | 0 | length += tmplen; |
187 | |
|
188 | 0 | return length; |
189 | 0 | } |
190 | | |
191 | | /* |
192 | | * Serialize trusted certificate to *pp, or just return the required buffer |
193 | | * length if pp == NULL. |
194 | | * |
195 | | * When pp is not NULL, but *pp == NULL, we allocate the buffer, but since |
196 | | * we're writing two ASN.1 objects back to back, we can't have i2d_X509() do |
197 | | * the allocation, nor can we allow i2d_X509_CERT_AUX() to increment the |
198 | | * allocated buffer. |
199 | | */ |
200 | | int i2d_X509_AUX(X509 *a, unsigned char **pp) |
201 | 0 | { |
202 | 0 | int length; |
203 | 0 | unsigned char *tmp; |
204 | | |
205 | | /* Buffer provided by caller */ |
206 | 0 | if (pp == NULL || *pp != NULL) |
207 | 0 | return i2d_x509_aux_internal(a, pp); |
208 | | |
209 | | /* Obtain the combined length */ |
210 | 0 | if ((length = i2d_x509_aux_internal(a, NULL)) <= 0) |
211 | 0 | return length; |
212 | | |
213 | | /* Allocate requisite combined storage */ |
214 | 0 | *pp = tmp = OPENSSL_malloc(length); |
215 | 0 | if (tmp == NULL) { |
216 | 0 | X509err(X509_F_I2D_X509_AUX, ERR_R_MALLOC_FAILURE); |
217 | 0 | return -1; |
218 | 0 | } |
219 | | |
220 | | /* Encode, but keep *pp at the originally malloced pointer */ |
221 | 0 | length = i2d_x509_aux_internal(a, &tmp); |
222 | 0 | if (length <= 0) { |
223 | 0 | OPENSSL_free(*pp); |
224 | 0 | *pp = NULL; |
225 | 0 | } |
226 | 0 | return length; |
227 | 0 | } |
228 | | |
229 | | int i2d_re_X509_tbs(X509 *x, unsigned char **pp) |
230 | 0 | { |
231 | 0 | x->cert_info.enc.modified = 1; |
232 | 0 | return i2d_X509_CINF(&x->cert_info, pp); |
233 | 0 | } |
234 | | |
235 | | void X509_get0_signature(const ASN1_BIT_STRING **psig, |
236 | | const X509_ALGOR **palg, const X509 *x) |
237 | 3.62k | { |
238 | 3.62k | if (psig) |
239 | 3.62k | *psig = &x->signature; |
240 | 3.62k | if (palg) |
241 | 3.62k | *palg = &x->sig_alg; |
242 | 3.62k | } |
243 | | |
244 | | int X509_get_signature_nid(const X509 *x) |
245 | 0 | { |
246 | 0 | return OBJ_obj2nid(x->sig_alg.algorithm); |
247 | 0 | } |