]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ct/ct_locl.h
0af4b7f83070a073ab7bb8d6039eeba8e5e31bd6
[thirdparty/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 #include <stddef.h>
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>
59
60 /*
61 * From RFC6962: opaque SerializedSCT<1..2^16-1>; struct { SerializedSCT
62 * sct_list <1..2^16-1>; } SignedCertificateTimestampList;
63 */
64 # define MAX_SCT_SIZE 65535
65 # define MAX_SCT_LIST_SIZE MAX_SCT_SIZE
66
67 /*
68 * Macros to read and write integers in network-byte order.
69 */
70
71 #define n2s(c,s) ((s=(((unsigned int)((c)[0]))<< 8)| \
72 (((unsigned int)((c)[1])) )),c+=2)
73
74 #define s2n(s,c) ((c[0]=(unsigned char)(((s)>> 8)&0xff), \
75 c[1]=(unsigned char)(((s) )&0xff)),c+=2)
76
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)
80
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)++))))
89
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))
98
99 /* Signed Certificate Timestamp */
100 struct sct_st {
101 sct_version_t version;
102 /* If version is not SCT_VERSION_V1, this contains the encoded SCT */
103 unsigned char *sct;
104 size_t sct_len;
105 /* If version is SCT_VERSION_V1, fields below contain components of the SCT */
106 unsigned char *log_id;
107 size_t log_id_len;
108 /*
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.
112 */
113 uint64_t timestamp;
114 unsigned char *ext;
115 size_t ext_len;
116 unsigned char hash_alg;
117 unsigned char sig_alg;
118 unsigned char *sig;
119 size_t sig_len;
120 /* Log entry type */
121 ct_log_entry_type_t entry_type;
122 /* Where this SCT was found, e.g. certificate, OCSP response, etc. */
123 sct_source_t source;
124 /* The result of the last attempt to validate this SCT. */
125 sct_validation_status_t validation_status;
126 };
127
128 /* Miscellaneous data that is useful when verifying an SCT */
129 struct sct_ctx_st {
130 /* Public key */
131 EVP_PKEY *pkey;
132 /* Hash of public key */
133 unsigned char *pkeyhash;
134 size_t pkeyhashlen;
135 /* For pre-certificate: issuer public key hash */
136 unsigned char *ihash;
137 size_t ihashlen;
138 /* certificate encoding */
139 unsigned char *certder;
140 size_t certderlen;
141 /* pre-certificate encoding */
142 unsigned char *preder;
143 size_t prederlen;
144 };
145
146 /* Context when evaluating whether a Certificate Transparency policy is met */
147 struct ct_policy_eval_ctx_st {
148 X509 *cert;
149 X509 *issuer;
150 CTLOG_STORE *log_store;
151 STACK_OF(SCT) *good_scts;
152 STACK_OF(SCT) *bad_scts;
153 };
154
155 /*
156 * Creates a new context for verifying an SCT.
157 */
158 SCT_CTX *SCT_CTX_new(void);
159 /*
160 * Deletes an SCT verification context.
161 */
162 void SCT_CTX_free(SCT_CTX *sctx);
163
164 /*
165 * Sets the certificate that the SCT was created for.
166 * If *cert does not have a poison extension, presigner must be NULL.
167 * If *cert does not have a poison extension, it may have a single SCT
168 * (NID_ct_precert_scts) extension.
169 * If either *cert or *presigner have an AKID (NID_authority_key_identifier)
170 * extension, both must have one.
171 * Returns 1 on success, 0 on failure.
172 */
173 __owur int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner);
174
175 /*
176 * Sets the issuer of the certificate that the SCT was created for.
177 * This is just a convenience method to save extracting the public key and
178 * calling SCT_CTX_set1_issuer_pubkey().
179 * Issuer must not be NULL.
180 * Returns 1 on success, 0 on failure.
181 */
182 __owur int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer);
183
184 /*
185 * Sets the public key of the issuer of the certificate that the SCT was created
186 * for.
187 * The public key must not be NULL.
188 * Returns 1 on success, 0 on failure.
189 */
190 __owur int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
191
192 /*
193 * Sets the public key of the CT log that the SCT is from.
194 * Returns 1 on success, 0 on failure.
195 */
196 __owur int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
197
198 /*
199 * Does this SCT have the minimum fields populated to be usable?
200 * Returns 1 if so, 0 otherwise.
201 */
202 __owur int SCT_is_complete(const SCT *sct);
203
204 /*
205 * Does this SCT have the signature-related fields populated?
206 * Returns 1 if so, 0 otherwise.
207 * This checks that the signature and hash algorithms are set to supported
208 * values and that the signature field is set.
209 */
210 __owur int SCT_signature_is_complete(const SCT *sct);
211
212
213 /*
214 * Handlers for Certificate Transparency X509v3/OCSP extensions
215 */
216 extern const X509V3_EXT_METHOD v3_ct_scts[];