check reviewer --reviewer=emilia
[openssl.git] / crypto / ct / ct_locl.h
1 /*
2  * Written by Rob Percival (robpercival@google.com) for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 2016 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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
17  *    distribution.
18  *
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/)"
23  *
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.
28  *
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.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
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  * ====================================================================
51  */
52
53 #ifdef OPENSSL_NO_CT
54 # error CT is disabled.
55 #endif
56
57 #include <stddef.h>
58
59 #include <openssl/ct.h>
60 #include <openssl/evp.h>
61 #include <openssl/x509.h>
62 #include <openssl/safestack.h>
63
64 /*
65  * From RFC6962: opaque SerializedSCT<1..2^16-1>; struct { SerializedSCT
66  * sct_list <1..2^16-1>; } SignedCertificateTimestampList;
67  */
68 # define MAX_SCT_SIZE            65535
69 # define MAX_SCT_LIST_SIZE       MAX_SCT_SIZE
70
71 /*
72  * Macros to read and write integers in network-byte order.
73  */
74
75 #define n2s(c,s)        ((s=(((unsigned int)((c)[0]))<< 8)| \
76                             (((unsigned int)((c)[1]))    )),c+=2)
77
78 #define s2n(s,c)        ((c[0]=(unsigned char)(((s)>> 8)&0xff), \
79                           c[1]=(unsigned char)(((s)    )&0xff)),c+=2)
80
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)
84
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)++))))
93
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))
102
103 /* Signed Certificate Timestamp */
104 struct sct_st {
105     sct_version_t version;
106     /* If version is not SCT_VERSION_V1, this contains the encoded SCT */
107     unsigned char *sct;
108     size_t sct_len;
109     /* If version is SCT_VERSION_V1, fields below contain components of the SCT */
110     unsigned char *log_id;
111     size_t log_id_len;
112     /*
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.
116     */
117     uint64_t timestamp;
118     unsigned char *ext;
119     size_t ext_len;
120     unsigned char hash_alg;
121     unsigned char sig_alg;
122     unsigned char *sig;
123     size_t sig_len;
124     /* Log entry type */
125     ct_log_entry_type_t entry_type;
126     /* Where this SCT was found, e.g. certificate, OCSP response, etc. */
127     sct_source_t source;
128     /* The result of the last attempt to validate this SCT. */
129     sct_validation_status_t validation_status;
130 };
131
132 /* Miscellaneous data that is useful when verifying an SCT  */
133 struct sct_ctx_st {
134     /* Public key */
135     EVP_PKEY *pkey;
136     /* Hash of public key */
137     unsigned char *pkeyhash;
138     size_t pkeyhashlen;
139     /* For pre-certificate: issuer public key hash */
140     unsigned char *ihash;
141     size_t ihashlen;
142     /* certificate encoding */
143     unsigned char *certder;
144     size_t certderlen;
145     /* pre-certificate encoding */
146     unsigned char *preder;
147     size_t prederlen;
148 };
149
150 /* Context when evaluating whether a Certificate Transparency policy is met */
151 struct ct_policy_eval_ctx_st {
152     X509 *cert;
153     X509 *issuer;
154     CTLOG_STORE *log_store;
155     STACK_OF(SCT) *good_scts;
156     STACK_OF(SCT) *bad_scts;
157 };
158
159 /*
160  * Creates a new context for verifying an SCT.
161  */
162 SCT_CTX *SCT_CTX_new(void);
163 /*
164  * Deletes an SCT verification context.
165  */
166 void SCT_CTX_free(SCT_CTX *sctx);
167
168 /*
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.
176  */
177 __owur int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner);
178
179 /*
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.
185  */
186 __owur int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer);
187
188 /*
189  * Sets the public key of the issuer of the certificate that the SCT was created
190  * for.
191  * The public key must not be NULL.
192  * Returns 1 on success, 0 on failure.
193  */
194 __owur int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
195
196 /*
197  * Sets the public key of the CT log that the SCT is from.
198  * Returns 1 on success, 0 on failure.
199  */
200 __owur int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
201
202 /*
203  * Does this SCT have the minimum fields populated to be usable?
204  * Returns 1 if so, 0 otherwise.
205  */
206 __owur int SCT_is_complete(const SCT *sct);
207
208 /*
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.
213  */
214 __owur int SCT_signature_is_complete(const SCT *sct);
215
216