Suppress CT callback as appropriate
[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:
140          *   struct {
141          *     Version sct_version;     (1 byte)
142          *     log_id id;               (32 bytes)
143          *     uint64 timestamp;        (8 bytes)
144          *     CtExtensions extensions; (2 bytes + ?)
145          *   }
146          */
147         if (len < 43) {
148             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
149             goto err;
150         }
151         len -= 43;
152         p++;
153         sct->log_id = BUF_memdup(p, CT_V1_HASHLEN);
154         if (sct->log_id == NULL)
155             goto err;
156         sct->log_id_len = CT_V1_HASHLEN;
157         p += CT_V1_HASHLEN;
158
159         n2l8(p, sct->timestamp);
160
161         n2s(p, len2);
162         if (len < len2) {
163             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
164             goto err;
165         }
166         if (len2 > 0) {
167             sct->ext = BUF_memdup(p, len2);
168             if (sct->ext == NULL)
169                 goto err;
170         }
171         sct->ext_len = len2;
172         p += len2;
173         len -= len2;
174
175         sig_len = o2i_SCT_signature(sct, &p, len);
176         if (sig_len <= 0) {
177             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
178             goto err;
179         }
180         len -= sig_len;
181         *in = p + len;
182     } else {
183         /* If not V1 just cache encoding */
184         sct->sct = BUF_memdup(p, len);
185         if (sct->sct == NULL)
186             goto err;
187         sct->sct_len = len;
188         *in = p + len;
189     }
190
191     if (psct != NULL) {
192         SCT_free(*psct);
193         *psct = sct;
194     }
195
196     return sct;
197 err:
198     SCT_free(sct);
199     return NULL;
200 }
201
202 int i2o_SCT_signature(const SCT *sct, unsigned char **out)
203 {
204     size_t len;
205     unsigned char *p = NULL;
206
207     if (!SCT_signature_is_complete(sct)) {
208         CTerr(CT_F_I2O_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
209         goto err;
210     }
211
212     if (sct->version != SCT_VERSION_V1) {
213         CTerr(CT_F_I2O_SCT_SIGNATURE, CT_R_UNSUPPORTED_VERSION);
214         goto err;
215     }
216
217     /*
218     * (1 byte) Hash algorithm
219     * (1 byte) Signature algorithm
220     * (2 bytes + ?) Signature
221     */
222     len = 4 + sct->sig_len;
223
224     if (out != NULL) {
225         if (*out != NULL) {
226             p = *out;
227             *out += len;
228         } else {
229             p = OPENSSL_malloc(len);
230             if (p == NULL) {
231                 CTerr(CT_F_I2O_SCT_SIGNATURE, ERR_R_MALLOC_FAILURE);
232                 goto err;
233             }
234             *out = p;
235         }
236
237         *p++ = sct->hash_alg;
238         *p++ = sct->sig_alg;
239         s2n(sct->sig_len, p);
240         memcpy(p, sct->sig, sct->sig_len);
241     }
242
243     return len;
244 err:
245     OPENSSL_free(p);
246     return -1;
247 }
248
249 int i2o_SCT(const SCT *sct, unsigned char **out)
250 {
251     size_t len;
252     unsigned char *p = NULL;
253
254     if (!SCT_is_complete(sct)) {
255         CTerr(CT_F_I2O_SCT, CT_R_SCT_NOT_SET);
256         goto err;
257     }
258     /*
259      * Fixed-length header: struct { (1 byte) Version sct_version; (32 bytes)
260      * log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?) CtExtensions
261      * extensions; (1 byte) Hash algorithm (1 byte) Signature algorithm (2
262      * bytes + ?) Signature
263      */
264     if (sct->version == SCT_VERSION_V1)
265         len = 43 + sct->ext_len + 4 + sct->sig_len;
266     else
267         len = sct->sct_len;
268
269     if (out == NULL)
270         return len;
271
272     if (*out != NULL) {
273         p = *out;
274         *out += len;
275     } else {
276         p = OPENSSL_malloc(len);
277         if (p == NULL) {
278             CTerr(CT_F_I2O_SCT, ERR_R_MALLOC_FAILURE);
279             goto err;
280         }
281         *out = p;
282     }
283
284     if (sct->version == SCT_VERSION_V1) {
285         *p++ = sct->version;
286         memcpy(p, sct->log_id, CT_V1_HASHLEN);
287         p += CT_V1_HASHLEN;
288         l2n8(sct->timestamp, p);
289         s2n(sct->ext_len, p);
290         if (sct->ext_len > 0) {
291             memcpy(p, sct->ext, sct->ext_len);
292             p += sct->ext_len;
293         }
294         if (i2o_SCT_signature(sct, &p) <= 0)
295             goto err;
296     } else {
297         memcpy(p, sct->sct, len);
298     }
299
300     return len;
301 err:
302     OPENSSL_free(p);
303     return -1;
304 }
305
306 void SCT_LIST_free(STACK_OF(SCT) *a)
307 {
308     sk_SCT_pop_free(a, SCT_free);
309 }
310
311 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
312                             size_t len)
313 {
314     STACK_OF(SCT) *sk = NULL;
315     size_t list_len, sct_len;
316
317     if (len < 2 || len > MAX_SCT_LIST_SIZE) {
318         CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
319         return NULL;
320     }
321
322     n2s(*pp, list_len);
323     if (list_len != len - 2) {
324         CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
325         return NULL;
326     }
327
328     if (a == NULL || *a == NULL) {
329         sk = sk_SCT_new_null();
330         if (sk == NULL)
331             return NULL;
332     } else {
333         SCT *sct;
334
335         /* Use the given stack, but empty it first. */
336         sk = *a;
337         while ((sct = sk_SCT_pop(sk)) != NULL)
338             SCT_free(sct);
339     }
340
341     while (list_len > 0) {
342         SCT *sct;
343
344         if (list_len < 2) {
345             CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
346             goto err;
347         }
348         n2s(*pp, sct_len);
349         list_len -= 2;
350
351         if (sct_len == 0 || sct_len > list_len) {
352             CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
353             goto err;
354         }
355         list_len -= sct_len;
356
357         if ((sct = o2i_SCT(NULL, pp, sct_len)) == NULL)
358             goto err;
359         if (!sk_SCT_push(sk, sct)) {
360             SCT_free(sct);
361             goto err;
362         }
363     }
364
365     if (a != NULL && *a == NULL)
366         *a = sk;
367     return sk;
368
369  err:
370     if (a == NULL || *a == NULL)
371         SCT_LIST_free(sk);
372     return NULL;
373 }
374
375 int i2o_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **pp)
376 {
377     int len, sct_len, i, is_pp_new = 0;
378     size_t len2;
379     unsigned char *p = NULL, *p2;
380
381     if (pp != NULL) {
382         if (*pp == NULL) {
383             if ((len = i2o_SCT_LIST(a, NULL)) == -1) {
384                 CTerr(CT_F_I2O_SCT_LIST, CT_R_SCT_LIST_INVALID);
385                 return -1;
386             }
387             if ((*pp = OPENSSL_malloc(len)) == NULL) {
388                 CTerr(CT_F_I2O_SCT_LIST, ERR_R_MALLOC_FAILURE);
389                 return -1;
390             }
391             is_pp_new = 1;
392         }
393         p = *pp + 2;
394     }
395
396     len2 = 2;
397     for (i = 0; i < sk_SCT_num(a); i++) {
398         if (pp != NULL) {
399             p2 = p;
400             p += 2;
401             if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1)
402                 goto err;
403             s2n(sct_len, p2);
404         } else {
405           if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1)
406               goto err;
407         }
408         len2 += 2 + sct_len;
409     }
410
411     if (len2 > MAX_SCT_LIST_SIZE)
412         goto err;
413
414     if (pp != NULL) {
415         p = *pp;
416         s2n(len2 - 2, p);
417     }
418     if (!is_pp_new)
419         *pp += len2;
420     return len2;
421
422  err:
423     if (is_pp_new) {
424         OPENSSL_free(*pp);
425         *pp = NULL;
426     }
427     return -1;
428 }
429
430 STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
431                             long len)
432 {
433     ASN1_OCTET_STRING *oct = NULL;
434     STACK_OF(SCT) *sk = NULL;
435     const unsigned char *p;
436
437     p = *pp;
438     if (d2i_ASN1_OCTET_STRING(&oct, &p, len) == NULL)
439         return NULL;
440
441     p = oct->data;
442     if ((sk = o2i_SCT_LIST(a, &p, oct->length)) != NULL)
443         *pp += len;
444
445     ASN1_OCTET_STRING_free(oct);
446     return sk;
447 }
448
449 int i2d_SCT_LIST(const STACK_OF(SCT) *a, unsigned char **out)
450 {
451     ASN1_OCTET_STRING oct;
452     int len;
453
454     oct.data = NULL;
455     if ((oct.length = i2o_SCT_LIST(a, &oct.data)) == -1)
456         return -1;
457
458     len = i2d_ASN1_OCTET_STRING(&oct, out);
459     OPENSSL_free(oct.data);
460     return len;
461 }