Make parameters of CTLOG_get* const
[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 */
168     if (poison_ext_is_dup)
169         goto err;
170
171     /* If no poison extension, store encoding */
172     if (poison_idx == -1) {
173         /* presigner must have poison */
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 have precert scts extension */
183     idx = ct_x509_get_ext(cert, NID_ct_precert_scts, &sct_ext_is_dup);
184     /* Duplicate scts */
185     if (sct_ext_is_dup)
186         goto err;
187
188     if (idx >= 0) {
189         /* Can't have both poison and scts */
190         if (poison_idx >= 0)
191             goto err;
192     } else {
193         idx = poison_idx;
194     }
195
196     if (idx >= 0) {
197         X509_EXTENSION *ext;
198
199         /* Take a copy of certificate so we don't modify passed version */
200         pretmp = X509_dup(cert);
201         if (pretmp == NULL)
202             goto err;
203
204         ext = X509_delete_ext(pretmp, idx);
205         X509_EXTENSION_free(ext);
206
207         if (!ct_x509_cert_fixup(pretmp, presigner))
208             goto err;
209
210         prederlen = i2d_re_X509_tbs(pretmp, &preder);
211         if (prederlen <= 0)
212             goto err;
213     }
214
215     X509_free(pretmp);
216
217     OPENSSL_free(sctx->certder);
218     sctx->certder = certder;
219     sctx->certderlen = certderlen;
220
221     OPENSSL_free(sctx->preder);
222     sctx->preder = preder;
223     sctx->prederlen = prederlen;
224
225     return 1;
226 err:
227     OPENSSL_free(certder);
228     OPENSSL_free(preder);
229     X509_free(pretmp);
230     return 0;
231 }
232
233 __owur static int ct_public_key_hash(X509_PUBKEY *pkey, unsigned char **hash,
234                                      size_t *hash_len)
235 {
236     int ret = 0;
237     unsigned char *md = NULL, *der = NULL;
238     int der_len;
239     unsigned int md_len;
240
241     /* Reuse buffer if possible */
242     if (*hash != NULL && *hash_len >= SHA256_DIGEST_LENGTH) {
243         md = *hash;
244     } else {
245         md = OPENSSL_malloc(SHA256_DIGEST_LENGTH);
246         if (md == NULL)
247             goto err;
248     }
249
250     /* Calculate key hash */
251     der_len = i2d_X509_PUBKEY(pkey, &der);
252     if (der_len <= 0)
253         goto err;
254
255     if (!EVP_Digest(der, der_len, md, &md_len, EVP_sha256(), NULL))
256         goto err;
257
258     if (md != *hash) {
259         OPENSSL_free(*hash);
260         *hash = md;
261         *hash_len = SHA256_DIGEST_LENGTH;
262     }
263
264     md = NULL;
265     ret = 1;
266  err:
267     OPENSSL_free(md);
268     OPENSSL_free(der);
269     return ret;
270 }
271
272 int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer)
273 {
274     return SCT_CTX_set1_issuer_pubkey(sctx, X509_get_X509_PUBKEY(issuer));
275 }
276
277 int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
278 {
279     return ct_public_key_hash(pubkey, &sctx->ihash, &sctx->ihashlen);
280 }
281
282 int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
283 {
284     EVP_PKEY *pkey = X509_PUBKEY_get(pubkey);
285
286     if (pkey == NULL)
287         return 0;
288
289     if (!ct_public_key_hash(pubkey, &sctx->pkeyhash, &sctx->pkeyhashlen)) {
290         EVP_PKEY_free(pkey);
291         return 0;
292     }
293
294     EVP_PKEY_free(sctx->pkey);
295     sctx->pkey = pkey;
296     return 1;
297 }