1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
* Written by Rob Stradling (rob@comodo.com) and Stephen Henson
* (steve@openssl.org) for the OpenSSL project 2014.
*/
/* ====================================================================
* Copyright (c) 2014 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <string.h>
#include <openssl/ct.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include "ct_locl.h"
typedef enum sct_signature_type_t {
SIGNATURE_TYPE_NOT_SET = -1,
SIGNATURE_TYPE_CERT_TIMESTAMP,
SIGNATURE_TYPE_TREE_HASH
} SCT_SIGNATURE_TYPE;
int CT_verify_no_bad_scts(const CT_POLICY_EVAL_CTX *ctx,
const STACK_OF(SCT) *scts, void *arg)
{
int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
int i;
for (i = 0; i < sct_count; ++i) {
SCT *sct = sk_SCT_value(scts, i);
switch (SCT_get_validation_status(sct)) {
case SCT_VALIDATION_STATUS_INVALID:
return 0;
case SCT_VALIDATION_STATUS_NOT_SET:
CTerr(CT_F_CT_VERIFY_NO_BAD_SCTS,
CT_R_SCT_VALIDATION_STATUS_NOT_SET);
return -1;
default:
/* Ignore other validation statuses. */
break;
}
}
return 1;
}
int CT_verify_at_least_one_good_sct(const CT_POLICY_EVAL_CTX *ctx,
const STACK_OF(SCT) *scts, void *arg)
{
int sct_count = scts != NULL ? sk_SCT_num(scts) : 0;
int valid_scts = 0;
int i;
for (i = 0; i < sct_count; ++i) {
SCT *sct = sk_SCT_value(scts, i);
switch (SCT_get_validation_status(sct)) {
case SCT_VALIDATION_STATUS_VALID:
++valid_scts;
break;
case SCT_VALIDATION_STATUS_INVALID:
return 0;
case SCT_VALIDATION_STATUS_NOT_SET:
CTerr(CT_F_CT_VERIFY_AT_LEAST_ONE_GOOD_SCT,
CT_R_SCT_VALIDATION_STATUS_NOT_SET);
return -1;
default:
/* Ignore other validation statuses. */
break;
}
}
if (valid_scts == 0) {
CTerr(CT_F_CT_VERIFY_AT_LEAST_ONE_GOOD_SCT, CT_R_NOT_ENOUGH_SCTS);
return 0;
}
return 1;
}
/*
* Update encoding for SCT signature verification/generation to supplied
* EVP_MD_CTX.
*/
static int sct_ctx_update(EVP_MD_CTX *ctx, const SCT_CTX *sctx, const SCT *sct)
{
unsigned char tmpbuf[12];
unsigned char *p, *der;
size_t derlen;
/*+
* digitally-signed struct {
* (1 byte) Version sct_version;
* (1 byte) SignatureType signature_type = certificate_timestamp;
* (8 bytes) uint64 timestamp;
* (2 bytes) LogEntryType entry_type;
* (? bytes) select(entry_type) {
* case x509_entry: ASN.1Cert;
* case precert_entry: PreCert;
* } signed_entry;
* (2 bytes + sct->ext_len) CtExtensions extensions;
* }
*/
if (sct->entry_type == CT_LOG_ENTRY_TYPE_NOT_SET)
return 0;
if (sct->entry_type == CT_LOG_ENTRY_TYPE_PRECERT && sctx->ihash == NULL)
return 0;
p = tmpbuf;
*p++ = sct->version;
*p++ = SIGNATURE_TYPE_CERT_TIMESTAMP;
l2n8(sct->timestamp, p);
s2n(sct->entry_type, p);
if (!EVP_DigestUpdate(ctx, tmpbuf, p - tmpbuf))
return 0;
if (sct->entry_type == CT_LOG_ENTRY_TYPE_X509) {
der = sctx->certder;
derlen = sctx->certderlen;
} else {
if (!EVP_DigestUpdate(ctx, sctx->ihash, sctx->ihashlen))
return 0;
der = sctx->preder;
derlen = sctx->prederlen;
}
/* If no encoding available, fatal error */
if (der == NULL)
return 0;
/* Include length first */
p = tmpbuf;
l2n3(derlen, p);
if (!EVP_DigestUpdate(ctx, tmpbuf, 3))
return 0;
if (!EVP_DigestUpdate(ctx, der, derlen))
return 0;
/* Add any extensions */
p = tmpbuf;
s2n(sct->ext_len, p);
if (!EVP_DigestUpdate(ctx, tmpbuf, 2))
return 0;
if (sct->ext_len && !EVP_DigestUpdate(ctx, sct->ext, sct->ext_len))
return 0;
return 1;
}
int SCT_verify(const SCT_CTX *sctx, const SCT *sct)
{
EVP_MD_CTX *ctx = NULL;
int ret = -1;
if (!SCT_is_complete(sct) || sctx->pkey == NULL ||
sct->entry_type == CT_LOG_ENTRY_TYPE_NOT_SET ||
(sct->entry_type == CT_LOG_ENTRY_TYPE_PRECERT && sctx->ihash == NULL)) {
CTerr(CT_F_SCT_VERIFY, CT_R_SCT_NOT_SET);
return -1;
}
if (sct->version != SCT_VERSION_V1) {
CTerr(CT_F_SCT_VERIFY, CT_R_SCT_UNSUPPORTED_VERSION);
return 0;
}
if (sct->log_id_len != sctx->pkeyhashlen ||
memcmp(sct->log_id, sctx->pkeyhash, sctx->pkeyhashlen) != 0) {
CTerr(CT_F_SCT_VERIFY, CT_R_SCT_LOG_ID_MISMATCH);
return 0;
}
ctx = EVP_MD_CTX_new();
if (ctx == NULL)
goto end;
if (!EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, sctx->pkey))
goto end;
if (!sct_ctx_update(ctx, sctx, sct))
goto end;
/* Verify signature */
ret = EVP_DigestVerifyFinal(ctx, sct->sig, sct->sig_len);
/* If ret < 0 some other error: fall through without setting error */
if (ret == 0)
CTerr(CT_F_SCT_VERIFY, CT_R_SCT_INVALID_SIGNATURE);
end:
EVP_MD_CTX_free(ctx);
return ret;
}
int SCT_verify_v1(SCT *sct, X509 *cert, X509 *preissuer,
X509_PUBKEY *log_pubkey, X509 *issuer_cert)
{
int ret = 0;
SCT_CTX *sctx = NULL;
if (!SCT_is_complete(sct)) {
CTerr(CT_F_SCT_VERIFY_V1, CT_R_SCT_NOT_SET);
return -1;
}
if (sct->version != 0) {
CTerr(CT_F_SCT_VERIFY_V1, CT_R_SCT_UNSUPPORTED_VERSION);
return 0;
}
sctx = SCT_CTX_new();
if (sctx == NULL)
goto done;
ret = SCT_CTX_set1_pubkey(sctx, log_pubkey);
if (ret <= 0)
goto done;
ret = SCT_CTX_set1_cert(sctx, cert, preissuer);
if (ret <= 0)
goto done;
if (sct->entry_type == CT_LOG_ENTRY_TYPE_PRECERT) {
ret = SCT_CTX_set1_issuer(sctx, issuer_cert);
if (ret <= 0)
goto done;
}
ret = SCT_verify(sctx, sct);
done:
SCT_CTX_free(sctx);
return ret;
}
|