Add more CT utility routines to be used as part of larger patch.
[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 #ifndef OPENSSL_NO_CT
60
61 # include <limits.h>
62 # include "internal/cryptlib.h"
63 # include <openssl/asn1.h>
64 # include <openssl/evp.h>
65 # include <openssl/x509.h>
66 # include <openssl/x509v3.h>
67 # include "crypto/include/internal/ct_int.h"
68
69 # define n2s(c,s)        ((s=(((unsigned int)((c)[0]))<< 8)| \
70                              (((unsigned int)((c)[1]))    )),c+=2)
71
72 # define s2n(s,c)        ((c[0]=(unsigned char)(((s)>> 8)&0xff), \
73                            c[1]=(unsigned char)(((s)    )&0xff)),c+=2)
74
75 # define n2l8(c,l)       (l =((uint64_t)(*((c)++)))<<56, \
76                           l|=((uint64_t)(*((c)++)))<<48, \
77                           l|=((uint64_t)(*((c)++)))<<40, \
78                           l|=((uint64_t)(*((c)++)))<<32, \
79                           l|=((uint64_t)(*((c)++)))<<24, \
80                           l|=((uint64_t)(*((c)++)))<<16, \
81                           l|=((uint64_t)(*((c)++)))<< 8, \
82                           l|=((uint64_t)(*((c)++))))
83
84 # define l2n8(l,c)       (*((c)++)=(unsigned char)(((l)>>56)&0xff), \
85                           *((c)++)=(unsigned char)(((l)>>48)&0xff), \
86                           *((c)++)=(unsigned char)(((l)>>40)&0xff), \
87                           *((c)++)=(unsigned char)(((l)>>32)&0xff), \
88                           *((c)++)=(unsigned char)(((l)>>24)&0xff), \
89                           *((c)++)=(unsigned char)(((l)>>16)&0xff), \
90                           *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
91                           *((c)++)=(unsigned char)(((l)    )&0xff))
92
93 static STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a,
94                                    const unsigned char **pp, int len);
95 static int i2d_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp);
96 static int i2r_SCT_LIST(X509V3_EXT_METHOD *method, STACK_OF(SCT) *sct_list,
97                         BIO *out, int indent);
98
99 static char *i2s_poison(const X509V3_EXT_METHOD *method, void *val)
100 {
101     return OPENSSL_strdup("NULL");
102 }
103
104 const X509V3_EXT_METHOD v3_ct_scts[] = {
105     { NID_ct_precert_scts, 0, NULL,
106     0, (X509V3_EXT_FREE)SCT_LIST_free,
107     (X509V3_EXT_D2I)d2i_SCT_LIST, (X509V3_EXT_I2D)i2d_SCT_LIST,
108     0, 0, 0, 0,
109     (X509V3_EXT_I2R)i2r_SCT_LIST, 0,
110     NULL },
111
112     { NID_ct_precert_poison, 0, ASN1_ITEM_ref(ASN1_NULL),
113     0, 0, 0, 0, i2s_poison, 0,
114     0, 0, 0, 0, NULL },
115
116     { NID_ct_cert_scts, 0, NULL,
117     0, (X509V3_EXT_FREE)SCT_LIST_free,
118     (X509V3_EXT_D2I)d2i_SCT_LIST, (X509V3_EXT_I2D)i2d_SCT_LIST,
119     0, 0, 0, 0,
120     (X509V3_EXT_I2R)i2r_SCT_LIST, 0,
121     NULL },
122 };
123
124 int SCT_signature_is_valid(const SCT *sct)
125 {
126     if (SCT_get_signature_nid(sct) == NID_undef ||
127         sct->sig_len == 0 || sct->sig == NULL) {
128         return 0;
129     }
130
131     return 1;
132 }
133
134
135 int o2i_SCT_signature(SCT *sct, const unsigned char **in, size_t len)
136 {
137     size_t siglen;
138     size_t len_remaining = len;
139     const unsigned char *p = *in;
140
141     if (sct->version != SCT_V1) {
142         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_UNSUPPORTED_VERSION);
143         return -1;
144     }
145     /*
146      * digitally-signed struct header: (1 byte) Hash algorithm (1 byte)
147      * Signature algorithm (2 bytes + ?) Signature
148      *
149      * This explicitly rejects empty signatures: they're invalid for
150      * all supported algorithms.
151      */
152     if (len <= 4) {
153         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
154         return -1;
155     }
156
157     /* Get hash and signature algorithm */
158     sct->hash_alg = *p++;
159     sct->sig_alg = *p++;
160     if (SCT_get_signature_nid(sct) == NID_undef) {
161         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
162         return -1;
163     }
164     /* Retrieve signature and check it is consistent with the buffer length */
165     n2s(p, siglen);
166     len_remaining -= (p - *in);
167     if (siglen > len_remaining) {
168         CTerr(CT_F_O2I_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
169         return -1;
170     }
171
172     if (SCT_set1_signature(sct, p, siglen) != 1)
173         return -1;
174     len_remaining -= siglen;
175     *in = p + siglen;
176
177     return len - len_remaining;
178 }
179
180 SCT *o2i_SCT(SCT **psct, const unsigned char **in, size_t len)
181 {
182     SCT *sct = NULL;
183     const unsigned char *p;
184
185     if (len == 0 || len > MAX_SCT_SIZE) {
186         CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
187         goto err;
188     }
189
190     if ((sct = SCT_new()) == NULL)
191         goto err;
192
193     p = *in;
194
195     sct->version = *p;
196     if (sct->version == SCT_V1) {
197         int sig_len;
198         size_t len2;
199         /*
200          * Fixed-length header: struct { (1 byte) Version sct_version; (32
201          * bytes) log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?)
202          * CtExtensions extensions;
203          */
204         if (len < 43) {
205             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
206             goto err;
207         }
208         len -= 43;
209         p++;
210         sct->log_id = BUF_memdup(p, SCT_V1_HASHLEN);
211         if (sct->log_id == NULL)
212             goto err;
213         sct->log_id_len = SCT_V1_HASHLEN;
214         p += SCT_V1_HASHLEN;
215
216         n2l8(p, sct->timestamp);
217
218         n2s(p, len2);
219         if (len < len2) {
220             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
221             goto err;
222         }
223         if (len2 > 0) {
224             sct->ext = BUF_memdup(p, len2);
225             if (sct->ext == NULL)
226                 goto err;
227         }
228         sct->ext_len = len2;
229         p += len2;
230         len -= len2;
231
232         sig_len = o2i_SCT_signature(sct, &p, len);
233         if (sig_len <= 0) {
234             CTerr(CT_F_O2I_SCT, CT_R_SCT_INVALID);
235             goto err;
236         }
237         len -= sig_len;
238         *in = p + len;
239     } else {
240         /* If not V1 just cache encoding */
241         sct->sct = BUF_memdup(p, len);
242         if (sct->sct == NULL)
243             goto err;
244         sct->sct_len = len;
245         *in = p + len;
246     }
247
248     if (psct != NULL) {
249         SCT_free(*psct);
250         *psct = sct;
251     }
252
253     return sct;
254 err:
255     SCT_free(sct);
256     return NULL;
257 }
258
259 int i2o_SCT_signature(const SCT *sct, unsigned char **out)
260 {
261     size_t len;
262     unsigned char *p = NULL;
263
264     if (!SCT_signature_is_valid(sct)) {
265         CTerr(CT_F_I2O_SCT_SIGNATURE, CT_R_SCT_INVALID_SIGNATURE);
266         goto err;
267     }
268
269     if (sct->version != SCT_V1) {
270         CTerr(CT_F_I2O_SCT_SIGNATURE, CT_R_UNSUPPORTED_VERSION);
271         goto err;
272     }
273
274     /*
275     * (1 byte) Hash algorithm
276     * (1 byte) Signature algorithm
277     * (2 bytes + ?) Signature
278     */
279     len = 4 + sct->sig_len;
280
281     if (out != NULL) {
282         if (*out != NULL) {
283             p = *out;
284             *out += len;
285         } else {
286             p = OPENSSL_malloc(len);
287             if (p == NULL) {
288                 CTerr(CT_F_I2O_SCT_SIGNATURE, ERR_R_MALLOC_FAILURE);
289                 goto err;
290             }
291             *out = p;
292         }
293
294         *p++ = sct->hash_alg;
295         *p++ = sct->sig_alg;
296         s2n(sct->sig_len, p);
297         memcpy(p, sct->sig, sct->sig_len);
298     }
299
300     return len;
301 err:
302     OPENSSL_free(p);
303     return -1;
304 }
305
306 int i2o_SCT(const SCT *sct, unsigned char **out)
307 {
308     size_t len;
309     unsigned char *p = NULL;
310
311     if (!SCT_is_valid(sct)) {
312         CTerr(CT_F_I2O_SCT, CT_R_SCT_NOT_SET);
313         goto err;
314     }
315     /*
316      * Fixed-length header: struct { (1 byte) Version sct_version; (32 bytes)
317      * log_id id; (8 bytes) uint64 timestamp; (2 bytes + ?) CtExtensions
318      * extensions; (1 byte) Hash algorithm (1 byte) Signature algorithm (2
319      * bytes + ?) Signature
320      */
321     if (sct->version == SCT_V1)
322         len = 43 + sct->ext_len + 4 + sct->sig_len;
323     else
324         len = sct->sct_len;
325
326     if (out == NULL)
327         return len;
328
329     if (*out != NULL) {
330         p = *out;
331         *out += len;
332     } else {
333         p = OPENSSL_malloc(len);
334         if (p == NULL) {
335             CTerr(CT_F_I2O_SCT, ERR_R_MALLOC_FAILURE);
336             goto err;
337         }
338         *out = p;
339     }
340
341     if (sct->version == SCT_V1) {
342         *p++ = sct->version;
343         memcpy(p, sct->log_id, SCT_V1_HASHLEN);
344         p += SCT_V1_HASHLEN;
345         l2n8(sct->timestamp, p);
346         s2n(sct->ext_len, p);
347         if (sct->ext_len > 0) {
348             memcpy(p, sct->ext, sct->ext_len);
349             p += sct->ext_len;
350         }
351         if (i2o_SCT_signature(sct, &p) <= 0)
352             goto err;
353     } else {
354         memcpy(p, sct->sct, len);
355     }
356
357     return len;
358 err:
359     OPENSSL_free(p);
360     return -1;
361 }
362
363 void SCT_LIST_free(STACK_OF(SCT) *a)
364 {
365     sk_SCT_pop_free(a, SCT_free);
366 }
367
368 STACK_OF(SCT) *o2i_SCT_LIST(STACK_OF(SCT) **a, const unsigned char **pp,
369                             size_t len)
370 {
371     STACK_OF(SCT) *sk = NULL;
372     size_t list_len, sct_len;
373
374     if (len < 2 || len > MAX_SCT_LIST_SIZE) {
375         CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
376         return NULL;
377     }
378
379     n2s(*pp, list_len);
380     if (list_len != len - 2) {
381         CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
382         return NULL;
383     }
384
385     if (a == NULL || *a == NULL) {
386         sk = sk_SCT_new_null();
387         if (sk == NULL)
388             return NULL;
389     } else {
390         SCT *sct;
391
392         /* Use the given stack, but empty it first. */
393         sk = *a;
394         while ((sct = sk_SCT_pop(sk)) != NULL)
395             SCT_free(sct);
396     }
397
398     while (list_len > 0) {
399         SCT *sct;
400
401         if (list_len < 2) {
402             CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
403             goto err;
404         }
405         n2s(*pp, sct_len);
406         list_len -= 2;
407
408         if (sct_len == 0 || sct_len > list_len) {
409             CTerr(CT_F_O2I_SCT_LIST, CT_R_SCT_LIST_INVALID);
410             goto err;
411         }
412         list_len -= sct_len;
413
414         if ((sct = o2i_SCT(NULL, pp, sct_len)) == NULL)
415             goto err;
416         if (!sk_SCT_push(sk, sct)) {
417             SCT_free(sct);
418             goto err;
419         }
420     }
421
422     if (a != NULL && *a == NULL)
423         *a = sk;
424     return sk;
425
426  err:
427     if (a == NULL || *a == NULL)
428         SCT_LIST_free(sk);
429     return NULL;
430 }
431
432 int i2o_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp)
433 {
434     int len, sct_len, i, is_pp_new = 0;
435     size_t len2;
436     unsigned char *p = NULL, *p2;
437
438     if (pp != NULL) {
439         if (*pp == NULL) {
440             if ((len = i2o_SCT_LIST(a, NULL)) == -1) {
441                 CTerr(CT_F_I2O_SCT_LIST, CT_R_SCT_LIST_INVALID);
442                 return -1;
443             }
444             if ((*pp = OPENSSL_malloc(len)) == NULL) {
445                 CTerr(CT_F_I2O_SCT_LIST, ERR_R_MALLOC_FAILURE);
446                 return -1;
447             }
448             is_pp_new = 1;
449         }
450         p = *pp + 2;
451     }
452
453     len2 = 2;
454     for (i = 0; i < sk_SCT_num(a); i++) {
455         if (pp != NULL) {
456             p2 = p;
457             p += 2;
458             if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1)
459                 goto err;
460             s2n(sct_len, p2);
461         } else {
462           if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1)
463               goto err;
464         }
465         len2 += 2 + sct_len;
466     }
467
468     if (len2 > MAX_SCT_LIST_SIZE)
469         goto err;
470
471     if (pp != NULL) {
472         p = *pp;
473         s2n(len2 - 2, p);
474     }
475     if (!is_pp_new)
476         *pp += len2;
477     return len2;
478
479  err:
480     if (is_pp_new) {
481         OPENSSL_free(*pp);
482         *pp = NULL;
483     }
484     return -1;
485 }
486
487 static STACK_OF(SCT) *d2i_SCT_LIST(STACK_OF(SCT) **a,
488                                    const unsigned char **pp, int len)
489 {
490     ASN1_OCTET_STRING *oct = NULL;
491     STACK_OF(SCT) *sk = NULL;
492     const unsigned char *p;
493
494     p = *pp;
495     if (d2i_ASN1_OCTET_STRING(&oct, &p, len) == NULL)
496         return NULL;
497
498     p = oct->data;
499     if ((sk = o2i_SCT_LIST(a, &p, oct->length)) != NULL)
500         *pp += len;
501
502     ASN1_OCTET_STRING_free(oct);
503     return sk;
504 }
505
506 static int i2d_SCT_LIST(STACK_OF(SCT) *a, unsigned char **out)
507 {
508     ASN1_OCTET_STRING oct;
509     int len;
510
511     oct.data = NULL;
512     if ((oct.length = i2o_SCT_LIST(a, &oct.data)) == -1)
513         return -1;
514
515     len = i2d_ASN1_OCTET_STRING(&oct, out);
516     OPENSSL_free(oct.data);
517     return len;
518 }
519
520 static int i2r_SCT_LIST(X509V3_EXT_METHOD *method, STACK_OF(SCT) *sct_list,
521                         BIO *out, int indent)
522 {
523     int i;
524
525     for (i = 0; i < sk_SCT_num(sct_list); ++i) {
526         SCT *sct = sk_SCT_value(sct_list, i);
527         SCT_print(sct, out, indent);
528         if (i < sk_SCT_num(sct_list) - 1)
529             BIO_printf(out, "\n");
530     }
531
532     return 1;
533 }
534
535 #endif