1180455e1a907612fbc035ae05e9bc159fdeff8c
[openssl.git] / crypto / ct / ct_locl.h
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stddef.h>
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>
16
17 /*
18  * From RFC6962: opaque SerializedSCT<1..2^16-1>; struct { SerializedSCT
19  * sct_list <1..2^16-1>; } SignedCertificateTimestampList;
20  */
21 # define MAX_SCT_SIZE            65535
22 # define MAX_SCT_LIST_SIZE       MAX_SCT_SIZE
23
24 /*
25  * Macros to read and write integers in network-byte order.
26  */
27
28 #define n2s(c,s)        ((s=(((unsigned int)((c)[0]))<< 8)| \
29                             (((unsigned int)((c)[1]))    )),c+=2)
30
31 #define s2n(s,c)        ((c[0]=(unsigned char)(((s)>> 8)&0xff), \
32                           c[1]=(unsigned char)(((s)    )&0xff)),c+=2)
33
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)
37
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)++))))
46
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))
55
56 /* Signed Certificate Timestamp */
57 struct sct_st {
58     sct_version_t version;
59     /* If version is not SCT_VERSION_V1, this contains the encoded SCT */
60     unsigned char *sct;
61     size_t sct_len;
62     /* If version is SCT_VERSION_V1, fields below contain components of the SCT */
63     unsigned char *log_id;
64     size_t log_id_len;
65     /*
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.
69     */
70     uint64_t timestamp;
71     unsigned char *ext;
72     size_t ext_len;
73     unsigned char hash_alg;
74     unsigned char sig_alg;
75     unsigned char *sig;
76     size_t sig_len;
77     /* Log entry type */
78     ct_log_entry_type_t entry_type;
79     /* Where this SCT was found, e.g. certificate, OCSP response, etc. */
80     sct_source_t source;
81     /* The result of the last attempt to validate this SCT. */
82     sct_validation_status_t validation_status;
83 };
84
85 /* Miscellaneous data that is useful when verifying an SCT  */
86 struct sct_ctx_st {
87     /* Public key */
88     EVP_PKEY *pkey;
89     /* Hash of public key */
90     unsigned char *pkeyhash;
91     size_t pkeyhashlen;
92     /* For pre-certificate: issuer public key hash */
93     unsigned char *ihash;
94     size_t ihashlen;
95     /* certificate encoding */
96     unsigned char *certder;
97     size_t certderlen;
98     /* pre-certificate encoding */
99     unsigned char *preder;
100     size_t prederlen;
101 };
102
103 /* Context when evaluating whether a Certificate Transparency policy is met */
104 struct ct_policy_eval_ctx_st {
105     X509 *cert;
106     X509 *issuer;
107     CTLOG_STORE *log_store;
108 };
109
110 /*
111  * Creates a new context for verifying an SCT.
112  */
113 SCT_CTX *SCT_CTX_new(void);
114 /*
115  * Deletes an SCT verification context.
116  */
117 void SCT_CTX_free(SCT_CTX *sctx);
118
119 /*
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.
127  */
128 __owur int SCT_CTX_set1_cert(SCT_CTX *sctx, X509 *cert, X509 *presigner);
129
130 /*
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.
136  */
137 __owur int SCT_CTX_set1_issuer(SCT_CTX *sctx, const X509 *issuer);
138
139 /*
140  * Sets the public key of the issuer of the certificate that the SCT was created
141  * for.
142  * The public key must not be NULL.
143  * Returns 1 on success, 0 on failure.
144  */
145 __owur int SCT_CTX_set1_issuer_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
146
147 /*
148  * Sets the public key of the CT log that the SCT is from.
149  * Returns 1 on success, 0 on failure.
150  */
151 __owur int SCT_CTX_set1_pubkey(SCT_CTX *sctx, X509_PUBKEY *pubkey);
152
153 /*
154  * Does this SCT have the minimum fields populated to be usable?
155  * Returns 1 if so, 0 otherwise.
156  */
157 __owur int SCT_is_complete(const SCT *sct);
158
159 /*
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.
164  */
165 __owur int SCT_signature_is_complete(const SCT *sct);
166
167
168 /*
169  * Handlers for Certificate Transparency X509v3/OCSP extensions
170  */
171 extern const X509V3_EXT_METHOD v3_ct_scts[3];