2 * Written by Rob Percival (robpercival@google.com) for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2016 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
54 #include <openssl/ct.h>
55 #include <openssl/evp.h>
56 #include <openssl/x509.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/safestack.h>
61 * From RFC6962: opaque SerializedSCT<1..2^16-1>; struct { SerializedSCT
62 * sct_list <1..2^16-1>; } SignedCertificateTimestampList;
64 # define MAX_SCT_SIZE 65535
65 # define MAX_SCT_LIST_SIZE MAX_SCT_SIZE
68 * Macros to read and write integers in network-byte order.
71 #define n2s(c,s) ((s=(((unsigned int)((c)[0]))<< 8)| \
72 (((unsigned int)((c)[1])) )),c+=2)
74 #define s2n(s,c) ((c[0]=(unsigned char)(((s)>> 8)&0xff), \
75 c[1]=(unsigned char)(((s) )&0xff)),c+=2)
77 #define l2n3(l,c) ((c[0]=(unsigned char)(((l)>>16)&0xff), \
78 c[1]=(unsigned char)(((l)>> 8)&0xff), \
79 c[2]=(unsigned char)(((l) )&0xff)),c+=3)
81 #define n2l8(c,l) (l =((uint64_t)(*((c)++)))<<56, \
82 l|=((uint64_t)(*((c)++)))<<48, \
83 l|=((uint64_t)(*((c)++)))<<40, \
84 l|=((uint64_t)(*((c)++)))<<32, \
85 l|=((uint64_t)(*((c)++)))<<24, \
86 l|=((uint64_t)(*((c)++)))<<16, \
87 l|=((uint64_t)(*((c)++)))<< 8, \
88 l|=((uint64_t)(*((c)++))))
90 #define l2n8(l,c) (*((c)++)=(unsigned char)(((l)>>56)&0xff), \
91 *((c)++)=(unsigned char)(((l)>>48)&0xff), \
92 *((c)++)=(unsigned char)(((l)>>40)&0xff), \
93 *((c)++)=(unsigned char)(((l)>>32)&0xff), \
94 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
95 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
96 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
97 *((c)++)=(unsigned char)(((l) )&0xff))
99 /* Signed Certificate Timestamp */
101 sct_version_t version;
102 /* If version is not SCT_VERSION_V1, this contains the encoded SCT */
105 /* If version is SCT_VERSION_V1, fields below contain components of the SCT */
106 unsigned char *log_id;
109 * Note, we cannot distinguish between an unset timestamp, and one
110 * that is set to 0. However since CT didn't exist in 1970, no real
111 * SCT should ever be set as such.
116 unsigned char hash_alg;
117 unsigned char sig_alg;
121 ct_log_entry_type_t entry_type;
122 /* Where this SCT was found, e.g. certificate, OCSP response, etc. */
124 /* The result of the last attempt to validate this SCT. */
125 sct_validation_status_t validation_status;
128 /* Miscellaneous data that is useful when verifying an SCT */
132 /* Hash of public key */
133 unsigned char *pkeyhash;
135 /* For pre-certificate: issuer public key hash */
136 unsigned char *ihash;
138 /* certificate encoding */
139 unsigned char *certder;
141 /* pre-certificate encoding */
142 unsigned char *preder;
146 /* Context when evaluating whether a Certificate Transparency policy is met */
147 struct ct_policy_eval_ctx_st {
150 CTLOG_STORE *log_store;
154 * Creates a new context for verifying an SCT.
156 SCT_CTX *SCT_CTX_new(void);
158 * Deletes an SCT verification context.
160 void SCT_CTX_free(SCT_CTX *sctx);
163 * Sets the certificate that the SCT was created for.
164 * If *cert does not have a poison extension, presigner must be NULL.
165 * If *cert does not have a poison extension, it may have a single SCT
166 * (NID_ct_precert_scts) extension.
167 * If either *cert or *presigner have an AKID (NID_authority_key_identifier)
168 * extension, both must have one.
169 * Returns 1 on success, 0 on failure.
171 __owur int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner);
174 * Sets the issuer of the certificate that the SCT was created for.
175 * This is just a convenience method to save extracting the public key and
176 * calling SCT_CTX_set1_issuer_pubkey().
177 * Issuer must not be NULL.
178 * Returns 1 on success, 0 on failure.
180 __owur int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer);
183 * Sets the public key of the issuer of the certificate that the SCT was created
185 * The public key must not be NULL.
186 * Returns 1 on success, 0 on failure.
188 __owur int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
191 * Sets the public key of the CT log that the SCT is from.
192 * Returns 1 on success, 0 on failure.
194 __owur int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
197 * Does this SCT have the minimum fields populated to be usable?
198 * Returns 1 if so, 0 otherwise.
200 __owur int SCT_is_complete(const SCT *sct);
203 * Does this SCT have the signature-related fields populated?
204 * Returns 1 if so, 0 otherwise.
205 * This checks that the signature and hash algorithms are set to supported
206 * values and that the signature field is set.
208 __owur int SCT_signature_is_complete(const SCT *sct);
212 * Handlers for Certificate Transparency X509v3/OCSP extensions
214 extern const X509V3_EXT_METHOD v3_ct_scts[];