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 # error CT is disabled.
59 #include <openssl/ct.h>
60 #include <openssl/evp.h>
61 #include <openssl/x509.h>
62 #include <openssl/safestack.h>
65 * From RFC6962: opaque SerializedSCT<1..2^16-1>; struct { SerializedSCT
66 * sct_list <1..2^16-1>; } SignedCertificateTimestampList;
68 # define MAX_SCT_SIZE 65535
69 # define MAX_SCT_LIST_SIZE MAX_SCT_SIZE
72 * Macros to read and write integers in network-byte order.
75 #define n2s(c,s) ((s=(((unsigned int)((c)[0]))<< 8)| \
76 (((unsigned int)((c)[1])) )),c+=2)
78 #define s2n(s,c) ((c[0]=(unsigned char)(((s)>> 8)&0xff), \
79 c[1]=(unsigned char)(((s) )&0xff)),c+=2)
81 #define l2n3(l,c) ((c[0]=(unsigned char)(((l)>>16)&0xff), \
82 c[1]=(unsigned char)(((l)>> 8)&0xff), \
83 c[2]=(unsigned char)(((l) )&0xff)),c+=3)
85 #define n2l8(c,l) (l =((uint64_t)(*((c)++)))<<56, \
86 l|=((uint64_t)(*((c)++)))<<48, \
87 l|=((uint64_t)(*((c)++)))<<40, \
88 l|=((uint64_t)(*((c)++)))<<32, \
89 l|=((uint64_t)(*((c)++)))<<24, \
90 l|=((uint64_t)(*((c)++)))<<16, \
91 l|=((uint64_t)(*((c)++)))<< 8, \
92 l|=((uint64_t)(*((c)++))))
94 #define l2n8(l,c) (*((c)++)=(unsigned char)(((l)>>56)&0xff), \
95 *((c)++)=(unsigned char)(((l)>>48)&0xff), \
96 *((c)++)=(unsigned char)(((l)>>40)&0xff), \
97 *((c)++)=(unsigned char)(((l)>>32)&0xff), \
98 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
99 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
100 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
101 *((c)++)=(unsigned char)(((l) )&0xff))
103 /* Signed Certificate Timestamp */
105 sct_version_t version;
106 /* If version is not SCT_VERSION_V1, this contains the encoded SCT */
109 /* If version is SCT_VERSION_V1, fields below contain components of the SCT */
110 unsigned char *log_id;
113 * Note, we cannot distinguish between an unset timestamp, and one
114 * that is set to 0. However since CT didn't exist in 1970, no real
115 * SCT should ever be set as such.
120 unsigned char hash_alg;
121 unsigned char sig_alg;
125 ct_log_entry_type_t entry_type;
126 /* Where this SCT was found, e.g. certificate, OCSP response, etc. */
128 /* The result of the last attempt to validate this SCT. */
129 sct_validation_status_t validation_status;
132 /* Miscellaneous data that is useful when verifying an SCT */
136 /* Hash of public key */
137 unsigned char *pkeyhash;
139 /* For pre-certificate: issuer public key hash */
140 unsigned char *ihash;
142 /* certificate encoding */
143 unsigned char *certder;
145 /* pre-certificate encoding */
146 unsigned char *preder;
150 /* Context when evaluating whether a Certificate Transparency policy is met */
151 struct ct_policy_eval_ctx_st {
154 CTLOG_STORE *log_store;
155 STACK_OF(SCT) *good_scts;
156 STACK_OF(SCT) *bad_scts;
160 * Creates a new context for verifying an SCT.
162 SCT_CTX *SCT_CTX_new(void);
164 * Deletes an SCT verification context.
166 void SCT_CTX_free(SCT_CTX *sctx);
169 * Sets the certificate that the SCT was created for.
170 * If *cert does not have a poison extension, presigner must be NULL.
171 * If *cert does not have a poison extension, it may have a single SCT
172 * (NID_ct_precert_scts) extension.
173 * If either *cert or *presigner have an AKID (NID_authority_key_identifier)
174 * extension, both must have one.
175 * Returns 1 on success, 0 on failure.
177 __owur int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner);
180 * Sets the issuer of the certificate that the SCT was created for.
181 * This is just a convenience method to save extracting the public key and
182 * calling SCT_CTX_set1_issuer_pubkey().
183 * Issuer must not be NULL.
184 * Returns 1 on success, 0 on failure.
186 __owur int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer);
189 * Sets the public key of the issuer of the certificate that the SCT was created
191 * The public key must not be NULL.
192 * Returns 1 on success, 0 on failure.
194 __owur int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
197 * Sets the public key of the CT log that the SCT is from.
198 * Returns 1 on success, 0 on failure.
200 __owur int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
203 * Does this SCT have the minimum fields populated to be usable?
204 * Returns 1 if so, 0 otherwise.
206 __owur int SCT_is_complete(const SCT *sct);
209 * Does this SCT have the signature-related fields populated?
210 * Returns 1 if so, 0 otherwise.
211 * This checks that the signature and hash algorithms are set to supported
212 * values and that the signature field is set.
214 __owur int SCT_signature_is_complete(const SCT *sct);