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