Improved documentation of SCT_CTX_* functions
[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(*sctx));
75
76     if (sctx == NULL)
77         CTerr(CT_F_SCT_CTX_NEW, ERR_R_MALLOC_FAILURE);
78
79     return sctx;
80 }
81
82 void SCT_CTX_free(SCT_CTX *sctx)
83 {
84     if (sctx == NULL)
85         return;
86     EVP_PKEY_free(sctx->pkey);
87     OPENSSL_free(sctx->pkeyhash);
88     OPENSSL_free(sctx->ihash);
89     OPENSSL_free(sctx->certder);
90     OPENSSL_free(sctx->preder);
91     OPENSSL_free(sctx);
92 }
93
94 /*
95  * Finds the index of the first extension with the given NID in cert.
96  * If there is more than one extension with that NID, *is_duplicated is set to
97  * 1, otherwise 0 (unless it is NULL).
98  */
99 static int ct_x509_get_ext(X509 *cert, int nid, int *is_duplicated)
100 {
101     int ret = X509_get_ext_by_NID(cert, nid, -1);
102
103     if (is_duplicated != NULL)
104         *is_duplicated = ret >= 0 && X509_get_ext_by_NID(cert, nid, ret) >= 0;
105
106     return ret;
107 }
108
109 /*
110  * Modifies a certificate by deleting extensions and copying the issuer and
111  * AKID from the presigner certificate, if necessary.
112  * Returns 1 on success, 0 otherwise.
113  */
114 __owur static int ct_x509_cert_fixup(X509 *cert, X509 *presigner)
115 {
116     int preidx, certidx;
117     int pre_akid_ext_is_dup, cert_akid_ext_is_dup;
118
119     if (presigner == NULL)
120         return 1;
121
122     preidx = ct_x509_get_ext(presigner, NID_authority_key_identifier,
123                              &pre_akid_ext_is_dup);
124     certidx = ct_x509_get_ext(cert, NID_authority_key_identifier,
125                               &cert_akid_ext_is_dup);
126
127     /* An error occurred whilst searching for the extension */
128     if (preidx < -1 || certidx < -1)
129         return 0;
130     /* Invalid certificate if they contain duplicate extensions */
131     if (pre_akid_ext_is_dup || cert_akid_ext_is_dup)
132         return 0;
133     /* AKID must be present in both certificate or absent in both */
134     if (preidx >= 0 && certidx == -1)
135         return 0;
136     if (preidx == -1 && certidx >= 0)
137         return 0;
138     /* Copy issuer name */
139     if (!X509_set_issuer_name(cert, X509_get_issuer_name(presigner)))
140         return 0;
141     if (preidx != -1) {
142         /* Retrieve and copy AKID encoding */
143         X509_EXTENSION *preext = X509_get_ext(presigner, preidx);
144         X509_EXTENSION *certext = X509_get_ext(cert, certidx);
145         ASN1_OCTET_STRING *preextdata;
146
147         /* Should never happen */
148         if (preext == NULL || certext == NULL)
149             return 0;
150         preextdata = X509_EXTENSION_get_data(preext);
151         if (preextdata == NULL ||
152             !X509_EXTENSION_set_data(certext, preextdata))
153             return 0;
154     }
155     return 1;
156 }
157
158 int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner)
159 {
160     unsigned char *certder = NULL, *preder = NULL;
161     X509 *pretmp = NULL;
162     int certderlen = 0, prederlen = 0;
163     int idx = -1;
164     int poison_ext_is_dup, sct_ext_is_dup;
165     int poison_idx = ct_x509_get_ext(cert, NID_ct_precert_poison, &poison_ext_is_dup);
166
167     /* Duplicate poison extensions are present - error */
168     if (poison_ext_is_dup)
169         goto err;
170
171     /* If *cert doesn't have a poison extension, it isn't a precert */
172     if (poison_idx == -1) {
173         /* cert isn't a precert, so we shouldn't have a presigner */
174         if (presigner != NULL)
175             goto err;
176
177         certderlen = i2d_X509(cert, &certder);
178         if (certderlen < 0)
179             goto err;
180     }
181
182     /* See if cert has a precert SCTs extension */
183     idx = ct_x509_get_ext(cert, NID_ct_precert_scts, &sct_ext_is_dup);
184     /* Duplicate SCT extensions are present - error */
185     if (sct_ext_is_dup)
186         goto err;
187
188     if (idx >= 0 && poison_idx >= 0) {
189         /*
190          * cert can't both contain SCTs (i.e. have an SCT extension) and be a
191          * precert (i.e. have a poison extension).
192          */
193         goto err;
194     }
195
196     if (idx == -1) {
197         idx = poison_idx;
198     }
199
200     /*
201      * If either a poison or SCT extension is present, remove it before encoding
202      * cert. This, along with ct_x509_cert_fixup(), gets a TBSCertificate (see
203      * RFC5280) from cert, which is what the CT log signed when it produced the
204      * SCT.
205      */
206     if (idx >= 0) {
207         X509_EXTENSION *ext;
208
209         /* Take a copy of certificate so we don't modify passed version */
210         pretmp = X509_dup(cert);
211         if (pretmp == NULL)
212             goto err;
213
214         ext = X509_delete_ext(pretmp, idx);
215         X509_EXTENSION_free(ext);
216
217         if (!ct_x509_cert_fixup(pretmp, presigner))
218             goto err;
219
220         prederlen = i2d_re_X509_tbs(pretmp, &preder);
221         if (prederlen <= 0)
222             goto err;
223     }
224
225     X509_free(pretmp);
226
227     OPENSSL_free(sctx->certder);
228     sctx->certder = certder;
229     sctx->certderlen = certderlen;
230
231     OPENSSL_free(sctx->preder);
232     sctx->preder = preder;
233     sctx->prederlen = prederlen;
234
235     return 1;
236 err:
237     OPENSSL_free(certder);
238     OPENSSL_free(preder);
239     X509_free(pretmp);
240     return 0;
241 }
242
243 __owur static int ct_public_key_hash(X509_PUBKEY *pkey, unsigned char **hash,
244                                      size_t *hash_len)
245 {
246     int ret = 0;
247     unsigned char *md = NULL, *der = NULL;
248     int der_len;
249     unsigned int md_len;
250
251     /* Reuse buffer if possible */
252     if (*hash != NULL && *hash_len >= SHA256_DIGEST_LENGTH) {
253         md = *hash;
254     } else {
255         md = OPENSSL_malloc(SHA256_DIGEST_LENGTH);
256         if (md == NULL)
257             goto err;
258     }
259
260     /* Calculate key hash */
261     der_len = i2d_X509_PUBKEY(pkey, &der);
262     if (der_len <= 0)
263         goto err;
264
265     if (!EVP_Digest(der, der_len, md, &md_len, EVP_sha256(), NULL))
266         goto err;
267
268     if (md != *hash) {
269         OPENSSL_free(*hash);
270         *hash = md;
271         *hash_len = SHA256_DIGEST_LENGTH;
272     }
273
274     md = NULL;
275     ret = 1;
276  err:
277     OPENSSL_free(md);
278     OPENSSL_free(der);
279     return ret;
280 }
281
282 int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer)
283 {
284     return SCT_CTX_set1_issuer_pubkey(sctx, X509_get_X509_PUBKEY(issuer));
285 }
286
287 int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
288 {
289     return ct_public_key_hash(pubkey, &sctx->ihash, &sctx->ihashlen);
290 }
291
292 int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
293 {
294     EVP_PKEY *pkey = X509_PUBKEY_get(pubkey);
295
296     if (pkey == NULL)
297         return 0;
298
299     if (!ct_public_key_hash(pubkey, &sctx->pkeyhash, &sctx->pkeyhashlen)) {
300         EVP_PKEY_free(pkey);
301         return 0;
302     }
303
304     EVP_PKEY_free(sctx->pkey);
305     sctx->pkey = pkey;
306     return 1;
307 }