2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/ct.h>
12 #include <openssl/evp.h>
13 #include <openssl/x509.h>
14 #include <openssl/x509v3.h>
15 #include <openssl/safestack.h>
18 * From RFC6962: opaque SerializedSCT<1..2^16-1>; struct { SerializedSCT
19 * sct_list <1..2^16-1>; } SignedCertificateTimestampList;
21 # define MAX_SCT_SIZE 65535
22 # define MAX_SCT_LIST_SIZE MAX_SCT_SIZE
25 * Macros to read and write integers in network-byte order.
28 #define n2s(c,s) ((s=(((unsigned int)((c)[0]))<< 8)| \
29 (((unsigned int)((c)[1])) )),c+=2)
31 #define s2n(s,c) ((c[0]=(unsigned char)(((s)>> 8)&0xff), \
32 c[1]=(unsigned char)(((s) )&0xff)),c+=2)
34 #define l2n3(l,c) ((c[0]=(unsigned char)(((l)>>16)&0xff), \
35 c[1]=(unsigned char)(((l)>> 8)&0xff), \
36 c[2]=(unsigned char)(((l) )&0xff)),c+=3)
38 #define n2l8(c,l) (l =((uint64_t)(*((c)++)))<<56, \
39 l|=((uint64_t)(*((c)++)))<<48, \
40 l|=((uint64_t)(*((c)++)))<<40, \
41 l|=((uint64_t)(*((c)++)))<<32, \
42 l|=((uint64_t)(*((c)++)))<<24, \
43 l|=((uint64_t)(*((c)++)))<<16, \
44 l|=((uint64_t)(*((c)++)))<< 8, \
45 l|=((uint64_t)(*((c)++))))
47 #define l2n8(l,c) (*((c)++)=(unsigned char)(((l)>>56)&0xff), \
48 *((c)++)=(unsigned char)(((l)>>48)&0xff), \
49 *((c)++)=(unsigned char)(((l)>>40)&0xff), \
50 *((c)++)=(unsigned char)(((l)>>32)&0xff), \
51 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
52 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
53 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
54 *((c)++)=(unsigned char)(((l) )&0xff))
56 /* Signed Certificate Timestamp */
58 sct_version_t version;
59 /* If version is not SCT_VERSION_V1, this contains the encoded SCT */
62 /* If version is SCT_VERSION_V1, fields below contain components of the SCT */
63 unsigned char *log_id;
66 * Note, we cannot distinguish between an unset timestamp, and one
67 * that is set to 0. However since CT didn't exist in 1970, no real
68 * SCT should ever be set as such.
73 unsigned char hash_alg;
74 unsigned char sig_alg;
78 ct_log_entry_type_t entry_type;
79 /* Where this SCT was found, e.g. certificate, OCSP response, etc. */
81 /* The result of the last attempt to validate this SCT. */
82 sct_validation_status_t validation_status;
85 /* Miscellaneous data that is useful when verifying an SCT */
89 /* Hash of public key */
90 unsigned char *pkeyhash;
92 /* For pre-certificate: issuer public key hash */
95 /* certificate encoding */
96 unsigned char *certder;
98 /* pre-certificate encoding */
99 unsigned char *preder;
103 /* Context when evaluating whether a Certificate Transparency policy is met */
104 struct ct_policy_eval_ctx_st {
107 CTLOG_STORE *log_store;
111 * Creates a new context for verifying an SCT.
113 SCT_CTX *SCT_CTX_new(void);
115 * Deletes an SCT verification context.
117 void SCT_CTX_free(SCT_CTX *sctx);
120 * Sets the certificate that the SCT was created for.
121 * If *cert does not have a poison extension, presigner must be NULL.
122 * If *cert does not have a poison extension, it may have a single SCT
123 * (NID_ct_precert_scts) extension.
124 * If either *cert or *presigner have an AKID (NID_authority_key_identifier)
125 * extension, both must have one.
126 * Returns 1 on success, 0 on failure.
128 __owur int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner);
131 * Sets the issuer of the certificate that the SCT was created for.
132 * This is just a convenience method to save extracting the public key and
133 * calling SCT_CTX_set1_issuer_pubkey().
134 * Issuer must not be NULL.
135 * Returns 1 on success, 0 on failure.
137 __owur int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer);
140 * Sets the public key of the issuer of the certificate that the SCT was created
142 * The public key must not be NULL.
143 * Returns 1 on success, 0 on failure.
145 __owur int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
148 * Sets the public key of the CT log that the SCT is from.
149 * Returns 1 on success, 0 on failure.
151 __owur int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
154 * Does this SCT have the minimum fields populated to be usable?
155 * Returns 1 if so, 0 otherwise.
157 __owur int SCT_is_complete(const SCT *sct);
160 * Does this SCT have the signature-related fields populated?
161 * Returns 1 if so, 0 otherwise.
162 * This checks that the signature and hash algorithms are set to supported
163 * values and that the signature field is set.
165 __owur int SCT_signature_is_complete(const SCT *sct);
169 * Handlers for Certificate Transparency X509v3/OCSP extensions
171 extern const X509V3_EXT_METHOD v3_ct_scts[3];