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