Reset SCT validation_status if the SCT is modified
[openssl.git] / crypto / ct / ct_oct.c
1 /*
2  * Written by Rob Stradling (rob@comodo.com) and Stephen Henson
3  * (steve@openssl.org) for the OpenSSL project 2014.
4  */
5 /* ====================================================================
6  * Copyright (c) 2014 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #ifdef OPENSSL_NO_CT
60 # error "CT is disabled"
61 #endif
62
63 #include <limits.h>
64 #include <string.h>
65
66 #include <openssl/asn1.h>
67 #include <openssl/buffer.h>
68 #include <openssl/ct.h>
69 #include <openssl/err.h>
70
71 #include "ct_locl.h"
72
73 int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len)
74 {
75     size_t siglen;
76     size_t len_remaining = len;
77     const unsigned char *p;
78
79     if (sct->version != SCT_VERSION_V1) {
80         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_UNSUPPORTED_VERSION);
81         return -1;
82     }
83     /*
84      * digitally-signed struct header: (1 byte) Hash algorithm (1 byte)
85      * Signature algorithm (2 bytes + ?) Signature
86      *
87      * This explicitly rejects empty signatures: they're invalid for
88      * all supported algorithms.
89      */
90     if (len <= 4) {
91         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
92         return -1;
93     }
94
95     p = *in;
96     /* Get hash and signature algorithm */
97     sct->hash_alg = *p++;
98     sct->sig_alg = *p++;
99     if (SCT_get_signature_nid(sct) == NID_undef) {
100         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
101         return -1;
102     }
103     /* Retrieve signature and check it is consistent with the buffer length */
104     n2s(p, siglen);
105     len_remaining -= (p - *in);
106     if (siglen > len_remaining) {
107         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
108         return -1;
109     }
110
111     if (SCT_set1_signature(sct, p, siglen) != 1)
112         return -1;
113     len_remaining -= siglen;
114     *in = p + siglen;
115
116     return len - len_remaining;
117 }
118
119 SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len)
120 {
121     SCT *sct = NULL;
122     const unsigned char *p;
123
124     if (len == 0 || len > MAX_SCT_SIZE) {
125         CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
126         goto err;
127     }
128
129     if ((sct = SCT_new()) == NULL)
130         goto err;
131
132     p = *in;
133
134     sct->version = *p;
135     if (sct->version == SCT_VERSION_V1) {
136         int sig_len;
137         size_t len2;
138         /*
139          * Fixed-length header: struct { (1 byte) Version sct_version; (32
140          * bytes) log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?)
141          * CtExtensions extensions;
142          */
143         if (len < 43) {
144             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
145             goto err;
146         }
147         len -= 43;
148         p++;
149         sct->log_id = BUF_memdup(p, CT_V1_HASHLEN);
150         if (sct->log_id == NULL)
151             goto err;
152         sct->log_id_len = CT_V1_HASHLEN;
153         p += CT_V1_HASHLEN;
154
155         n2l8(p, sct->timestamp);
156
157         n2s(p, len2);
158         if (len < len2) {
159             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
160             goto err;
161         }
162         if (len2 > 0) {
163             sct->ext = BUF_memdup(p, len2);
164             if (sct->ext == NULL)
165                 goto err;
166         }
167         sct->ext_len = len2;
168         p += len2;
169         len -= len2;
170
171         sig_len = o2i_SCT_signature(sct, &p, len);
172         if (sig_len <= 0) {
173             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
174             goto err;
175         }
176         len -= sig_len;
177         *in = p + len;
178     } else {
179         /* If not V1 just cache encoding */
180         sct->sct = BUF_memdup(p, len);
181         if (sct->sct == NULL)
182             goto err;
183         sct->sct_len = len;
184         *in = p + len;
185     }
186
187     if (psct != NULL) {
188         SCT_free(*psct);
189         *psct = sct;
190     }
191
192     return sct;
193 err:
194     SCT_free(sct);
195     return NULL;
196 }
197
198 int i2o_SCT_signature(const SCT *sct, unsigned char **out)
199 {
200     size_t len;
201     unsigned char *p = NULL;
202
203     if (!SCT_signature_is_complete(sct)) {
204         CTerr(CT_F_I2O_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
205         goto err;
206     }
207
208     if (sct->version != SCT_VERSION_V1) {
209         CTerr(CT_F_I2O_SCT_SIGNATURE, CT_R_UNSUPPORTED_VERSION);
210         goto err;
211     }
212
213     /*
214     * (1 byte) Hash algorithm
215     * (1 byte) Signature algorithm
216     * (2 bytes + ?) Signature
217     */
218     len = 4 + sct->sig_len;
219
220     if (out != NULL) {
221         if (*out != NULL) {
222             p = *out;
223             *out += len;
224         } else {
225             p = OPENSSL_malloc(len);
226             if (p == NULL) {
227                 CTerr(CT_F_I2O_SCT_SIGNATURE, ERR_R_MALLOC_FAILURE);
228                 goto err;
229             }
230             *out = p;
231         }
232
233         *p++ = sct->hash_alg;
234         *p++ = sct->sig_alg;
235         s2n(sct->sig_len, p);
236         memcpy(p, sct->sig, sct->sig_len);
237     }
238
239     return len;
240 err:
241     OPENSSL_free(p);
242     return -1;
243 }
244
245 int i2o_SCT(const SCT *sct, unsigned char **out)
246 {
247     size_t len;
248     unsigned char *p = NULL;
249
250     if (!SCT_is_complete(sct)) {
251         CTerr(CT_F_I2O_SCT, CT_R_SCT_NOT_SET);
252         goto err;
253     }
254     /*
255      * Fixed-length header: struct { (1 byte) Version sct_version; (32 bytes)
256      * log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?) CtExtensions
257      * extensions; (1 byte) Hash algorithm (1 byte) Signature algorithm (2
258      * bytes + ?) Signature
259      */
260     if (sct->version == SCT_VERSION_V1)
261         len = 43 + sct->ext_len + 4 + sct->sig_len;
262     else
263         len = sct->sct_len;
264
265     if (out == NULL)
266         return len;
267
268     if (*out != NULL) {
269         p = *out;
270         *out += len;
271     } else {
272         p = OPENSSL_malloc(len);
273         if (p == NULL) {
274             CTerr(CT_F_I2O_SCT, ERR_R_MALLOC_FAILURE);
275             goto err;
276         }
277         *out = p;
278     }
279
280     if (sct->version == SCT_VERSION_V1) {
281         *p++ = sct->version;
282         memcpy(p, sct->log_id, CT_V1_HASHLEN);
283         p += CT_V1_HASHLEN;
284         l2n8(sct->timestamp, p);
285         s2n(sct->ext_len, p);
286         if (sct->ext_len > 0) {
287             memcpy(p, sct->ext, sct->ext_len);
288             p += sct->ext_len;
289         }
290         if (i2o_SCT_signature(sct, &p) <= 0)
291             goto err;
292     } else {
293         memcpy(p, sct->sct, len);
294     }
295
296     return len;
297 err:
298     OPENSSL_free(p);
299     return -1;
300 }
301
302 void SCT_LIST_free(STACK_OF(SCT) *a)
303 {
304     sk_SCT_pop_free(a, SCT_free);
305 }
306
307 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
308                             size_t len)
309 {
310     STACK_OF(SCT) *sk = NULL;
311     size_t list_len, sct_len;
312
313     if (len < 2 || len > MAX_SCT_LIST_SIZE) {
314         CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
315         return NULL;
316     }
317
318     n2s(*pp, list_len);
319     if (list_len != len - 2) {
320         CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
321         return NULL;
322     }
323
324     if (a == NULL || *a == NULL) {
325         sk = sk_SCT_new_null();
326         if (sk == NULL)
327             return NULL;
328     } else {
329         SCT *sct;
330
331         /* Use the given stack, but empty it first. */
332         sk = *a;
333         while ((sct = sk_SCT_pop(sk)) != NULL)
334             SCT_free(sct);
335     }
336
337     while (list_len > 0) {
338         SCT *sct;
339
340         if (list_len < 2) {
341             CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
342             goto err;
343         }
344         n2s(*pp, sct_len);
345         list_len -= 2;
346
347         if (sct_len == 0 || sct_len > list_len) {
348             CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
349             goto err;
350         }
351         list_len -= sct_len;
352
353         if ((sct = o2i_SCT(NULL, pp, sct_len)) == NULL)
354             goto err;
355         if (!sk_SCT_push(sk, sct)) {
356             SCT_free(sct);
357             goto err;
358         }
359     }
360
361     if (a != NULL && *a == NULL)
362         *a = sk;
363     return sk;
364
365  err:
366     if (a == NULL || *a == NULL)
367         SCT_LIST_free(sk);
368     return NULL;
369 }
370
371 int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp)
372 {
373     int len, sct_len, i, is_pp_new = 0;
374     size_t len2;
375     unsigned char *p = NULL, *p2;
376
377     if (pp != NULL) {
378         if (*pp == NULL) {
379             if ((len = i2o_SCT_LIST(a, NULL)) == -1) {
380                 CTerr(CT_F_I2O_SCT_LIST, CT_R_SCT_LIST_INVALID);
381                 return -1;
382             }
383             if ((*pp = OPENSSL_malloc(len)) == NULL) {
384                 CTerr(CT_F_I2O_SCT_LIST, ERR_R_MALLOC_FAILURE);
385                 return -1;
386             }
387             is_pp_new = 1;
388         }
389         p = *pp + 2;
390     }
391
392     len2 = 2;
393     for (i = 0; i < sk_SCT_num(a); i++) {
394         if (pp != NULL) {
395             p2 = p;
396             p += 2;
397             if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1)
398                 goto err;
399             s2n(sct_len, p2);
400         } else {
401           if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1)
402               goto err;
403         }
404         len2 += 2 + sct_len;
405     }
406
407     if (len2 > MAX_SCT_LIST_SIZE)
408         goto err;
409
410     if (pp != NULL) {
411         p = *pp;
412         s2n(len2 - 2, p);
413     }
414     if (!is_pp_new)
415         *pp += len2;
416     return len2;
417
418  err:
419     if (is_pp_new) {
420         OPENSSL_free(*pp);
421         *pp = NULL;
422     }
423     return -1;
424 }
425
426 STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
427                             long len)
428 {
429     ASN1_OCTET_STRING *oct = NULL;
430     STACK_OF(SCT) *sk = NULL;
431     const unsigned char *p;
432
433     p = *pp;
434     if (d2i_ASN1_OCTET_STRING(&oct, &p, len) == NULL)
435         return NULL;
436
437     p = oct->data;
438     if ((sk = o2i_SCT_LIST(a, &p, oct->length)) != NULL)
439         *pp += len;
440
441     ASN1_OCTET_STRING_free(oct);
442     return sk;
443 }
444
445 int i2d_SCT_LIST(STACK_OF(SCT) *a, unsigned char **out)
446 {
447     ASN1_OCTET_STRING oct;
448     int len;
449
450     oct.data = NULL;
451     if ((oct.length = i2o_SCT_LIST(a, &oct.data)) == -1)
452         return -1;
453
454     len = i2d_ASN1_OCTET_STRING(&oct, out);
455     OPENSSL_free(oct.data);
456     return len;
457 }