/src/openssl32/crypto/ct/ct_sct_ctx.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 | | #ifdef OPENSSL_NO_CT |
11 | | # error "CT is disabled" |
12 | | #endif |
13 | | |
14 | | #include <stddef.h> |
15 | | #include <string.h> |
16 | | |
17 | | #include <openssl/err.h> |
18 | | #include <openssl/obj_mac.h> |
19 | | #include <openssl/x509.h> |
20 | | |
21 | | #include "ct_local.h" |
22 | | |
23 | | SCT_CTX *SCT_CTX_new(OSSL_LIB_CTX *libctx, const char *propq) |
24 | 0 | { |
25 | 0 | SCT_CTX *sctx = OPENSSL_zalloc(sizeof(*sctx)); |
26 | |
|
27 | 0 | if (sctx == NULL) |
28 | 0 | return NULL; |
29 | | |
30 | 0 | sctx->libctx = libctx; |
31 | 0 | if (propq != NULL) { |
32 | 0 | sctx->propq = OPENSSL_strdup(propq); |
33 | 0 | if (sctx->propq == NULL) { |
34 | 0 | OPENSSL_free(sctx); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | 0 | return sctx; |
40 | 0 | } |
41 | | |
42 | | void SCT_CTX_free(SCT_CTX *sctx) |
43 | 0 | { |
44 | 0 | if (sctx == NULL) |
45 | 0 | return; |
46 | 0 | EVP_PKEY_free(sctx->pkey); |
47 | 0 | OPENSSL_free(sctx->pkeyhash); |
48 | 0 | OPENSSL_free(sctx->ihash); |
49 | 0 | OPENSSL_free(sctx->certder); |
50 | 0 | OPENSSL_free(sctx->preder); |
51 | 0 | OPENSSL_free(sctx->propq); |
52 | 0 | OPENSSL_free(sctx); |
53 | 0 | } |
54 | | |
55 | | /* |
56 | | * Finds the index of the first extension with the given NID in cert. |
57 | | * If there is more than one extension with that NID, *is_duplicated is set to |
58 | | * 1, otherwise 0 (unless it is NULL). |
59 | | */ |
60 | | static int ct_x509_get_ext(X509 *cert, int nid, int *is_duplicated) |
61 | 0 | { |
62 | 0 | int ret = X509_get_ext_by_NID(cert, nid, -1); |
63 | |
|
64 | 0 | if (is_duplicated != NULL) |
65 | 0 | *is_duplicated = ret >= 0 && X509_get_ext_by_NID(cert, nid, ret) >= 0; |
66 | |
|
67 | 0 | return ret; |
68 | 0 | } |
69 | | |
70 | | /* |
71 | | * Modifies a certificate by deleting extensions and copying the issuer and |
72 | | * AKID from the presigner certificate, if necessary. |
73 | | * Returns 1 on success, 0 otherwise. |
74 | | */ |
75 | | __owur static int ct_x509_cert_fixup(X509 *cert, X509 *presigner) |
76 | 0 | { |
77 | 0 | int preidx, certidx; |
78 | 0 | int pre_akid_ext_is_dup, cert_akid_ext_is_dup; |
79 | |
|
80 | 0 | if (presigner == NULL) |
81 | 0 | return 1; |
82 | | |
83 | 0 | preidx = ct_x509_get_ext(presigner, NID_authority_key_identifier, |
84 | 0 | &pre_akid_ext_is_dup); |
85 | 0 | certidx = ct_x509_get_ext(cert, NID_authority_key_identifier, |
86 | 0 | &cert_akid_ext_is_dup); |
87 | | |
88 | | /* An error occurred whilst searching for the extension */ |
89 | 0 | if (preidx < -1 || certidx < -1) |
90 | 0 | return 0; |
91 | | /* Invalid certificate if they contain duplicate extensions */ |
92 | 0 | if (pre_akid_ext_is_dup || cert_akid_ext_is_dup) |
93 | 0 | return 0; |
94 | | /* AKID must be present in both certificate or absent in both */ |
95 | 0 | if (preidx >= 0 && certidx == -1) |
96 | 0 | return 0; |
97 | 0 | if (preidx == -1 && certidx >= 0) |
98 | 0 | return 0; |
99 | | /* Copy issuer name */ |
100 | 0 | if (!X509_set_issuer_name(cert, X509_get_issuer_name(presigner))) |
101 | 0 | return 0; |
102 | 0 | if (preidx != -1) { |
103 | | /* Retrieve and copy AKID encoding */ |
104 | 0 | X509_EXTENSION *preext = X509_get_ext(presigner, preidx); |
105 | 0 | X509_EXTENSION *certext = X509_get_ext(cert, certidx); |
106 | 0 | ASN1_OCTET_STRING *preextdata; |
107 | | |
108 | | /* Should never happen */ |
109 | 0 | if (preext == NULL || certext == NULL) |
110 | 0 | return 0; |
111 | 0 | preextdata = X509_EXTENSION_get_data(preext); |
112 | 0 | if (preextdata == NULL || |
113 | 0 | !X509_EXTENSION_set_data(certext, preextdata)) |
114 | 0 | return 0; |
115 | 0 | } |
116 | 0 | return 1; |
117 | 0 | } |
118 | | |
119 | | int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner) |
120 | 0 | { |
121 | 0 | unsigned char *certder = NULL, *preder = NULL; |
122 | 0 | X509 *pretmp = NULL; |
123 | 0 | int certderlen = 0, prederlen = 0; |
124 | 0 | int idx = -1; |
125 | 0 | int poison_ext_is_dup, sct_ext_is_dup; |
126 | 0 | int poison_idx = ct_x509_get_ext(cert, NID_ct_precert_poison, &poison_ext_is_dup); |
127 | | |
128 | | /* Duplicate poison extensions are present - error */ |
129 | 0 | if (poison_ext_is_dup) |
130 | 0 | goto err; |
131 | | |
132 | | /* If *cert doesn't have a poison extension, it isn't a precert */ |
133 | 0 | if (poison_idx == -1) { |
134 | | /* cert isn't a precert, so we shouldn't have a presigner */ |
135 | 0 | if (presigner != NULL) |
136 | 0 | goto err; |
137 | | |
138 | 0 | certderlen = i2d_X509(cert, &certder); |
139 | 0 | if (certderlen < 0) |
140 | 0 | goto err; |
141 | 0 | } |
142 | | |
143 | | /* See if cert has a precert SCTs extension */ |
144 | 0 | idx = ct_x509_get_ext(cert, NID_ct_precert_scts, &sct_ext_is_dup); |
145 | | /* Duplicate SCT extensions are present - error */ |
146 | 0 | if (sct_ext_is_dup) |
147 | 0 | goto err; |
148 | | |
149 | 0 | if (idx >= 0 && poison_idx >= 0) { |
150 | | /* |
151 | | * cert can't both contain SCTs (i.e. have an SCT extension) and be a |
152 | | * precert (i.e. have a poison extension). |
153 | | */ |
154 | 0 | goto err; |
155 | 0 | } |
156 | | |
157 | 0 | if (idx == -1) { |
158 | 0 | idx = poison_idx; |
159 | 0 | } |
160 | | |
161 | | /* |
162 | | * If either a poison or SCT extension is present, remove it before encoding |
163 | | * cert. This, along with ct_x509_cert_fixup(), gets a TBSCertificate (see |
164 | | * RFC5280) from cert, which is what the CT log signed when it produced the |
165 | | * SCT. |
166 | | */ |
167 | 0 | if (idx >= 0) { |
168 | | /* Take a copy of certificate so we don't modify passed version */ |
169 | 0 | pretmp = X509_dup(cert); |
170 | 0 | if (pretmp == NULL) |
171 | 0 | goto err; |
172 | | |
173 | 0 | X509_EXTENSION_free(X509_delete_ext(pretmp, idx)); |
174 | |
|
175 | 0 | if (!ct_x509_cert_fixup(pretmp, presigner)) |
176 | 0 | goto err; |
177 | | |
178 | 0 | prederlen = i2d_re_X509_tbs(pretmp, &preder); |
179 | 0 | if (prederlen <= 0) |
180 | 0 | goto err; |
181 | 0 | } |
182 | | |
183 | 0 | X509_free(pretmp); |
184 | |
|
185 | 0 | OPENSSL_free(sctx->certder); |
186 | 0 | sctx->certder = certder; |
187 | 0 | sctx->certderlen = certderlen; |
188 | |
|
189 | 0 | OPENSSL_free(sctx->preder); |
190 | 0 | sctx->preder = preder; |
191 | 0 | sctx->prederlen = prederlen; |
192 | |
|
193 | 0 | return 1; |
194 | 0 | err: |
195 | 0 | OPENSSL_free(certder); |
196 | 0 | OPENSSL_free(preder); |
197 | 0 | X509_free(pretmp); |
198 | 0 | return 0; |
199 | 0 | } |
200 | | |
201 | | __owur static int ct_public_key_hash(SCT_CTX *sctx, X509_PUBKEY *pkey, |
202 | | unsigned char **hash, size_t *hash_len) |
203 | 0 | { |
204 | 0 | int ret = 0; |
205 | 0 | unsigned char *md = NULL, *der = NULL; |
206 | 0 | int der_len; |
207 | 0 | unsigned int md_len; |
208 | 0 | EVP_MD *sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq); |
209 | |
|
210 | 0 | if (sha256 == NULL) |
211 | 0 | goto err; |
212 | | |
213 | | /* Reuse buffer if possible */ |
214 | 0 | if (*hash != NULL && *hash_len >= SHA256_DIGEST_LENGTH) { |
215 | 0 | md = *hash; |
216 | 0 | } else { |
217 | 0 | md = OPENSSL_malloc(SHA256_DIGEST_LENGTH); |
218 | 0 | if (md == NULL) |
219 | 0 | goto err; |
220 | 0 | } |
221 | | |
222 | | /* Calculate key hash */ |
223 | 0 | der_len = i2d_X509_PUBKEY(pkey, &der); |
224 | 0 | if (der_len <= 0) |
225 | 0 | goto err; |
226 | | |
227 | 0 | if (!EVP_Digest(der, der_len, md, &md_len, sha256, NULL)) |
228 | 0 | goto err; |
229 | | |
230 | 0 | if (md != *hash) { |
231 | 0 | OPENSSL_free(*hash); |
232 | 0 | *hash = md; |
233 | 0 | *hash_len = SHA256_DIGEST_LENGTH; |
234 | 0 | } |
235 | |
|
236 | 0 | md = NULL; |
237 | 0 | ret = 1; |
238 | 0 | err: |
239 | 0 | EVP_MD_free(sha256); |
240 | 0 | OPENSSL_free(md); |
241 | 0 | OPENSSL_free(der); |
242 | 0 | return ret; |
243 | 0 | } |
244 | | |
245 | | int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer) |
246 | 0 | { |
247 | 0 | return SCT_CTX_set1_issuer_pubkey(sctx, X509_get_X509_PUBKEY(issuer)); |
248 | 0 | } |
249 | | |
250 | | int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey) |
251 | 0 | { |
252 | 0 | return ct_public_key_hash(sctx, pubkey, &sctx->ihash, &sctx->ihashlen); |
253 | 0 | } |
254 | | |
255 | | int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey) |
256 | 0 | { |
257 | 0 | EVP_PKEY *pkey = X509_PUBKEY_get(pubkey); |
258 | |
|
259 | 0 | if (pkey == NULL) |
260 | 0 | return 0; |
261 | | |
262 | 0 | if (!ct_public_key_hash(sctx, pubkey, &sctx->pkeyhash, &sctx->pkeyhashlen)) { |
263 | 0 | EVP_PKEY_free(pkey); |
264 | 0 | return 0; |
265 | 0 | } |
266 | | |
267 | 0 | EVP_PKEY_free(sctx->pkey); |
268 | 0 | sctx->pkey = pkey; |
269 | 0 | return 1; |
270 | 0 | } |
271 | | |
272 | | void SCT_CTX_set_time(SCT_CTX *sctx, uint64_t time_in_ms) |
273 | 0 | { |
274 | 0 | sctx->epoch_time_in_ms = time_in_ms; |
275 | 0 | } |