2 * Written by Rob Stradling (rob@comodo.com) and Stephen Henson
3 * (steve@openssl.org) for the OpenSSL project 2014.
5 /* ====================================================================
6 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
60 # error "CT is disabled"
66 #include <openssl/err.h>
67 #include <openssl/obj_mac.h>
68 #include <openssl/x509.h>
72 SCT_CTX *SCT_CTX_new(void)
74 SCT_CTX *sctx = OPENSSL_zalloc(sizeof(SCT_CTX));
76 CTerr(CT_F_SCT_CTX_NEW, ERR_R_MALLOC_FAILURE);
80 void SCT_CTX_free(SCT_CTX *sctx)
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);
92 /* retrieve extension index checking for duplicates */
93 static int sct_get_ext(X509 *cert, int nid)
95 int rv = X509_get_ext_by_NID(cert, nid, -1);
96 if (rv >= 0 && X509_get_ext_by_NID(cert, nid, rv) >= 0)
102 * modify certificate by deleting extensions, copying issuer
103 * and AKID if necessary.
105 static int sct_cert_fixup(X509 *cert, X509 *presigner)
108 if (presigner == NULL)
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)
115 /* AKID must be present in both certificate or absent in both */
116 if (preidx >= 0 && certidx == -1)
118 if (preidx == -1 && certidx >= 0)
120 /* Copy issuer name */
121 if (!X509_set_issuer_name(cert, X509_get_issuer_name(presigner)))
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)
131 preextdata = X509_EXTENSION_get_data(preext);
132 if (preextdata == NULL || !X509_EXTENSION_set_data(certext, preextdata))
138 int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner)
140 unsigned char *certder = NULL, *preder = 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 */
148 /* If no poison store encoding */
150 /* If presigner must have poison */
153 certderlen = i2d_X509(cert, &certder);
157 /* See if have precert scts extension */
158 idx = X509_get_ext_by_NID(cert, NID_ct_precert_scts, -1);
163 /* Can't have both poison and scts */
171 * Take a copy of certificate so we don't modify passed version
173 pretmp = X509_dup(cert);
176 ext = X509_delete_ext(pretmp, idx);
177 X509_EXTENSION_free(ext);
178 if (!sct_cert_fixup(pretmp, presigner))
181 prederlen = i2d_re_X509_tbs(pretmp, &preder);
188 OPENSSL_free(sctx->certder);
189 sctx->certder = certder;
190 sctx->certderlen = certderlen;
192 OPENSSL_free(sctx->preder);
193 sctx->preder = preder;
194 sctx->prederlen = prederlen;
199 OPENSSL_free(certder);
200 OPENSSL_free(preder);
205 static int CT_public_key_hash(X509_PUBKEY *pkey, unsigned char **hash,
209 unsigned char *md = NULL, *der = NULL;
214 /* Reuse buffer if possible */
215 if (*hash != NULL && *hash_len >= SHA256_DIGEST_LENGTH) {
218 md = OPENSSL_malloc(SHA256_DIGEST_LENGTH);
223 /* Calculate key hash */
224 der_len = i2d_X509_PUBKEY(pkey, &der);
227 if (!EVP_Digest(der, der_len, md, &md_len, EVP_sha256(), NULL))
232 *hash_len = SHA256_DIGEST_LENGTH;
242 int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer)
244 return CT_public_key_hash(X509_get_X509_PUBKEY(issuer), &sctx->ihash,
248 int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
250 return CT_public_key_hash(pubkey, &sctx->ihash, &sctx->ihashlen);
253 int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey)
255 EVP_PKEY *pkey = X509_PUBKEY_get(pubkey);
259 if (!CT_public_key_hash(pubkey, &sctx->pkeyhash, &sctx->pkeyhashlen)) {
264 EVP_PKEY_free(sctx->pkey);