Verify SCT signatures
[openssl.git] / crypto / ct / ct_sct_ctx.c
1 /*
2  * Written by Rob Stradling (rob@comodo.com) and Stephen Henson
3  * (steve@openssl.org) for the OpenSSL project 2014.
4  */
5 /* ====================================================================
6  * Copyright (c) 2014 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #ifdef OPENSSL_NO_CT
60 # error "CT is disabled"
61 #endif
62
63 #include <stddef.h>
64 #include <string.h>
65
66 #include <openssl/err.h>
67 #include <openssl/obj_mac.h>
68 #include <openssl/x509.h>
69
70 #include "ct_locl.h"
71
72 SCT_CTX *SCT_CTX_new(void)
73 {
74     SCT_CTX *sctx = OPENSSL_zalloc(sizeof(SCT_CTX));
75     if (sctx == NULL)
76         CTerr(CT_F_SCT_CTX_NEW, ERR_R_MALLOC_FAILURE);
77     return sctx;
78 }
79
80 void SCT_CTX_free(SCT_CTX *sctx)
81 {
82     if (sctx == NULL)
83         return;
84     EVP_PKEY_free(sctx->pkey);
85     OPENSSL_free(sctx->pkeyhash);
86     OPENSSL_free(sctx->ihash);
87     OPENSSL_free(sctx->certder);
88     OPENSSL_free(sctx->preder);
89     OPENSSL_free(sctx);
90 }
91
92 /* retrieve extension index checking for duplicates */
93 static int sct_get_ext(X509 *cert, int nid)
94 {
95     int rv = X509_get_ext_by_NID(cert, nid, -1);
96     if (rv >= 0 && X509_get_ext_by_NID(cert, nid, rv) >= 0)
97         return -2;
98     return rv;
99 }
100
101 /*
102  * modify certificate by deleting extensions, copying issuer
103  * and AKID if necessary.
104  */
105 static int sct_cert_fixup(X509 *cert, X509 *presigner)
106 {
107     int preidx, certidx;
108     if (presigner == NULL)
109         return 1;
110     preidx = sct_get_ext(presigner, NID_authority_key_identifier);
111     certidx = sct_get_ext(cert, NID_authority_key_identifier);
112     /* Invalid certificate if duplicate */
113     if (preidx == -2 || certidx == -2)
114         return 0;
115     /* AKID must be present in both certificate or absent in both */
116     if (preidx >= 0 && certidx == -1)
117         return 0;
118     if (preidx == -1 && certidx >= 0)
119         return 0;
120     /* Copy issuer name */
121     if (!X509_set_issuer_name(cert, X509_get_issuer_name(presigner)))
122         return 0;
123     if (preidx != -1) {
124         /* Retrieve and copy AKID encoding */
125         X509_EXTENSION *preext = X509_get_ext(presigner, preidx);
126         X509_EXTENSION *certext = X509_get_ext(cert, certidx);
127         ASN1_OCTET_STRING *preextdata;
128         /* Should never happen */
129         if (preext == NULL || certext == NULL)
130             return 0;
131         preextdata = X509_EXTENSION_get_data(preext);
132         if (preextdata == NULL || !X509_EXTENSION_set_data(certext, preextdata))
133             return 0;
134     }
135     return 1;
136 }
137
138 int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner)
139 {
140     unsigned char *certder = NULL, *preder = NULL;
141     X509 *pretmp = NULL;
142     int certderlen = 0, prederlen = 0;
143     int idx = -1, idxp = -1;
144     idxp = sct_get_ext(cert, NID_ct_precert_poison);
145     /* Duplicate poison */
146     if (idxp == -2)
147         goto err;
148     /* If no poison store encoding */
149     if (idxp == -1) {
150         /* If presigner must have poison */
151         if (presigner)
152             goto err;
153         certderlen = i2d_X509(cert, &certder);
154         if (certderlen < 0)
155             goto err;
156     }
157     /* See if have precert scts extension */
158     idx = X509_get_ext_by_NID(cert, NID_ct_precert_scts, -1);
159     /* Duplicate scts */
160     if (idx == -2)
161         goto err;
162     if (idx >= 0) {
163         /* Can't have both poison and scts */
164         if (idxp >= 0)
165             goto err;
166     } else
167         idx = idxp;
168     if (idx >= 0) {
169         X509_EXTENSION *ext;
170         /*
171          * Take a copy of certificate so we don't modify passed version
172          */
173         pretmp = X509_dup(cert);
174         if (pretmp == NULL)
175             goto err;
176         ext = X509_delete_ext(pretmp, idx);
177         X509_EXTENSION_free(ext);
178         if (!sct_cert_fixup(pretmp, presigner))
179             goto err;
180
181         prederlen = i2d_re_X509_tbs(pretmp, &preder);
182         if (prederlen <= 0)
183             goto err;
184     }
185
186     X509_free(pretmp);
187
188     OPENSSL_free(sctx->certder);
189     sctx->certder = certder;
190     sctx->certderlen = certderlen;
191
192     OPENSSL_free(sctx->preder);
193     sctx->preder = preder;
194     sctx->prederlen = prederlen;
195
196     return 1;
197
198  err:
199     OPENSSL_free(certder);
200     OPENSSL_free(preder);
201     X509_free(pretmp);
202     return 0;
203 }
204
205 static int CT_public_key_hash(X509_PUBKEY *pkey, unsigned char **hash,
206                               size_t *hash_len)
207 {
208     int ret = -1;
209     unsigned char *md = NULL, *der = NULL;
210     int der_len;
211     unsigned int md_len;
212     if (pkey == NULL)
213         goto err;
214     /* Reuse buffer if possible */
215     if (*hash != NULL && *hash_len >= SHA256_DIGEST_LENGTH) {
216         md = *hash;
217     } else {
218         md = OPENSSL_malloc(SHA256_DIGEST_LENGTH);
219         if (md == NULL)
220           goto err;
221     }
222
223     /* Calculate key hash */
224     der_len = i2d_X509_PUBKEY(pkey, &der);
225     if (der_len <= 0)
226         goto err;
227     if (!EVP_Digest(der, der_len, md, &md_len, EVP_sha256(), NULL))
228         goto err;
229     if (md != *hash) {
230         OPENSSL_free(*hash);
231         *hash = md;
232         *hash_len = SHA256_DIGEST_LENGTH;
233     }
234     md = NULL;
235     ret = 1;
236  err:
237     OPENSSL_free(md);
238     OPENSSL_free(der);
239     return ret;
240 }
241
242 int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer)
243 {
244     return CT_public_key_hash(X509_get_X509_PUBKEY(issuer), &sctx->ihash,
245                               &sctx->ihashlen);
246 }
247
248 int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
249 {
250     return CT_public_key_hash(pubkey, &sctx->ihash, &sctx->ihashlen);
251 }
252
253 int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
254 {
255     EVP_PKEY *pkey = X509_PUBKEY_get(pubkey);
256     if (pkey == NULL)
257         return 0;
258
259     if (!CT_public_key_hash(pubkey, &sctx->pkeyhash, &sctx->pkeyhashlen)) {
260         EVP_PKEY_free(pkey);
261         return 0;
262     }
263
264     EVP_PKEY_free(sctx->pkey);
265     sctx->pkey = pkey;
266     return 1;
267 }