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