asn1/a_int.c: don't write result if returning error.
[openssl.git] / crypto / asn1 / a_int.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "internal/numbers.h"
13 #include <limits.h>
14 #include <openssl/asn1.h>
15 #include <openssl/bn.h>
16 #include "asn1_locl.h"
17
18 ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
19 {
20     return ASN1_STRING_dup(x);
21 }
22
23 int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
24 {
25     int neg, ret;
26     /* Compare signs */
27     neg = x->type & V_ASN1_NEG;
28     if (neg != (y->type & V_ASN1_NEG)) {
29         if (neg)
30             return -1;
31         else
32             return 1;
33     }
34
35     ret = ASN1_STRING_cmp(x, y);
36
37     if (neg)
38         return -ret;
39     else
40         return ret;
41 }
42
43 /*-
44  * This converts a big endian buffer and sign into its content encoding.
45  * This is used for INTEGER and ENUMERATED types.
46  * The internal representation is an ASN1_STRING whose data is a big endian
47  * representation of the value, ignoring the sign. The sign is determined by
48  * the type: if type & V_ASN1_NEG is true it is negative, otherwise positive.
49  *
50  * Positive integers are no problem: they are almost the same as the DER
51  * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
52  *
53  * Negative integers are a bit trickier...
54  * The DER representation of negative integers is in 2s complement form.
55  * The internal form is converted by complementing each octet and finally
56  * adding one to the result. This can be done less messily with a little trick.
57  * If the internal form has trailing zeroes then they will become FF by the
58  * complement and 0 by the add one (due to carry) so just copy as many trailing
59  * zeros to the destination as there are in the source. The carry will add one
60  * to the last none zero octet: so complement this octet and add one and finally
61  * complement any left over until you get to the start of the string.
62  *
63  * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
64  * with 0xff. However if the first byte is 0x80 and one of the following bytes
65  * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
66  * followed by optional zeros isn't padded.
67  */
68
69 /*
70  * If |pad| is zero, the operation is effectively reduced to memcpy,
71  * and if |pad| is 0xff, then it performs two's complement, ~dst + 1.
72  * Note that in latter case sequence of zeros yields itself, and so
73  * does 0x80 followed by any number of zeros. These properties are
74  * used elsewhere below...
75  */
76 static void twos_complement(unsigned char *dst, const unsigned char *src,
77                             size_t len, unsigned char pad)
78 {
79     unsigned int carry = pad & 1;
80
81     /* Begin at the end of the encoding */
82     dst += len;
83     src += len;
84     /* two's complement value: ~value + 1 */
85     while (len-- != 0) {
86         *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
87         carry >>= 8;
88     }
89 }
90
91 static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
92                        unsigned char **pp)
93 {
94     unsigned int pad = 0;
95     size_t ret, i;
96     unsigned char *p, pb = 0;
97
98     if (b != NULL && blen) {
99         ret = blen;
100         i = b[0];
101         if (!neg && (i > 127)) {
102             pad = 1;
103             pb = 0;
104         } else if (neg) {
105             pb = 0xFF;
106             if (i > 128) {
107                 pad = 1;
108             } else if (i == 128) {
109                 /*
110                  * Special case [of minimal negative for given length]:
111                  * if any other bytes non zero we pad, otherwise we don't.
112                  */
113                 for (pad = 0, i = 1; i < blen; i++)
114                     pad |= b[i];
115                 pb = pad != 0 ? 0xffU : 0;
116                 pad = pb & 1;
117             }
118         }
119         ret += pad;
120     } else {
121         ret = 1;
122         blen = 0;   /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
123     }
124
125     if (pp == NULL || (p = *pp) == NULL)
126         return ret;
127
128     /*
129      * This magically handles all corner cases, such as '(b == NULL ||
130      * blen == 0)', non-negative value, "negative" zero, 0x80 followed
131      * by any number of zeros...
132      */
133     *p = pb;
134     p += pad;       /* yes, p[0] can be written twice, but it's little
135                      * price to pay for eliminated branches */
136     twos_complement(p, b, blen, pb);
137
138     *pp += ret;
139     return ret;
140 }
141
142 /*
143  * convert content octets into a big endian buffer. Returns the length
144  * of buffer or 0 on error: for malformed INTEGER. If output buffer is
145  * NULL just return length.
146  */
147
148 static size_t c2i_ibuf(unsigned char *b, int *pneg,
149                        const unsigned char *p, size_t plen)
150 {
151     int neg, pad;
152     /* Zero content length is illegal */
153     if (plen == 0) {
154         ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_ZERO_CONTENT);
155         return 0;
156     }
157     neg = p[0] & 0x80;
158     if (pneg)
159         *pneg = neg;
160     /* Handle common case where length is 1 octet separately */
161     if (plen == 1) {
162         if (b != NULL) {
163             if (neg)
164                 b[0] = (p[0] ^ 0xFF) + 1;
165             else
166                 b[0] = p[0];
167         }
168         return 1;
169     }
170     if (p[0] == 0 || p[0] == 0xFF)
171         pad = 1;
172     else
173         pad = 0;
174     /* reject illegal padding: first two octets MSB can't match */
175     if (pad && (neg == (p[1] & 0x80))) {
176         ASN1err(ASN1_F_C2I_IBUF, ASN1_R_ILLEGAL_PADDING);
177         return 0;
178     }
179
180     /* skip over pad */
181     p += pad;
182     plen -= pad;
183
184     if (b != NULL)
185         twos_complement(b, p, plen, neg ? 0xffU : 0);
186
187     return plen;
188 }
189
190 int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
191 {
192     return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
193 }
194
195 /* Convert big endian buffer into uint64_t, return 0 on error */
196 static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
197 {
198     size_t i;
199     uint64_t r;
200
201     if (blen > sizeof(*pr)) {
202         ASN1err(ASN1_F_ASN1_GET_UINT64, ASN1_R_TOO_LARGE);
203         return 0;
204     }
205     if (b == NULL)
206         return 0;
207     for (r = 0, i = 0; i < blen; i++) {
208         r <<= 8;
209         r |= b[i];
210     }
211     *pr = r;
212     return 1;
213 }
214
215 /*
216  * Write uint64_t to big endian buffer and return offset to first
217  * written octet. In other words it returns offset in range from 0
218  * to 7, with 0 denoting 8 written octets and 7 - one.
219  */
220 static size_t asn1_put_uint64(unsigned char b[sizeof(uint64_t)], uint64_t r)
221 {
222     size_t off = sizeof(uint64_t);
223
224     do {
225         b[--off] = (unsigned char)r;
226     } while (r >>= 8);
227
228     return off;
229 }
230
231 /*
232  * Absolute value of INT64_MIN: we can't just use -INT64_MIN as it produces
233  * overflow warnings.
234  */
235
236 #define ABS_INT64_MIN \
237     ((uint64_t)INT64_MAX + (uint64_t)(-(INT64_MIN + INT64_MAX)))
238
239 /* signed version of asn1_get_uint64 */
240 static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
241                           int neg)
242 {
243     uint64_t r;
244     if (asn1_get_uint64(&r, b, blen) == 0)
245         return 0;
246     if (neg) {
247         if (r > ABS_INT64_MIN) {
248             ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_SMALL);
249             return 0;
250         }
251         *pr = 0 - (uint64_t)r;
252     } else {
253         if (r > INT64_MAX) {
254             ASN1err(ASN1_F_ASN1_GET_INT64, ASN1_R_TOO_LARGE);
255             return 0;
256         }
257         *pr = (int64_t)r;
258     }
259     return 1;
260 }
261
262 /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
263 ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
264                                long len)
265 {
266     ASN1_INTEGER *ret = NULL;
267     size_t r;
268     int neg;
269
270     r = c2i_ibuf(NULL, NULL, *pp, len);
271
272     if (r == 0)
273         return NULL;
274
275     if ((a == NULL) || ((*a) == NULL)) {
276         ret = ASN1_INTEGER_new();
277         if (ret == NULL)
278             return NULL;
279         ret->type = V_ASN1_INTEGER;
280     } else
281         ret = *a;
282
283     if (ASN1_STRING_set(ret, NULL, r) == 0)
284         goto err;
285
286     c2i_ibuf(ret->data, &neg, *pp, len);
287
288     if (neg)
289         ret->type |= V_ASN1_NEG;
290
291     *pp += len;
292     if (a != NULL)
293         (*a) = ret;
294     return ret;
295  err:
296     ASN1err(ASN1_F_C2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
297     if ((a == NULL) || (*a != ret))
298         ASN1_INTEGER_free(ret);
299     return NULL;
300 }
301
302 static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
303 {
304     if (a == NULL) {
305         ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ERR_R_PASSED_NULL_PARAMETER);
306         return 0;
307     }
308     if ((a->type & ~V_ASN1_NEG) != itype) {
309         ASN1err(ASN1_F_ASN1_STRING_GET_INT64, ASN1_R_WRONG_INTEGER_TYPE);
310         return 0;
311     }
312     return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
313 }
314
315 static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
316 {
317     unsigned char tbuf[sizeof(r)];
318     size_t off;
319
320     a->type = itype;
321     if (r < 0) {
322         off = asn1_put_uint64(tbuf, -r);
323         a->type |= V_ASN1_NEG;
324     } else {
325         off = asn1_put_uint64(tbuf, r);
326         a->type &= ~V_ASN1_NEG;
327     }
328     return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
329 }
330
331 static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
332                                   int itype)
333 {
334     if (a == NULL) {
335         ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ERR_R_PASSED_NULL_PARAMETER);
336         return 0;
337     }
338     if ((a->type & ~V_ASN1_NEG) != itype) {
339         ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_WRONG_INTEGER_TYPE);
340         return 0;
341     }
342     if (a->type & V_ASN1_NEG) {
343         ASN1err(ASN1_F_ASN1_STRING_GET_UINT64, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
344         return 0;
345     }
346     return asn1_get_uint64(pr, a->data, a->length);
347 }
348
349 static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
350 {
351     unsigned char tbuf[sizeof(r)];
352     size_t off;
353
354     a->type = itype;
355     off = asn1_put_uint64(tbuf, r);
356     return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
357 }
358
359 /*
360  * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
361  * integers: some broken software can encode a positive INTEGER with its MSB
362  * set as negative (it doesn't add a padding zero).
363  */
364
365 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
366                                 long length)
367 {
368     ASN1_INTEGER *ret = NULL;
369     const unsigned char *p;
370     unsigned char *s;
371     long len;
372     int inf, tag, xclass;
373     int i;
374
375     if ((a == NULL) || ((*a) == NULL)) {
376         if ((ret = ASN1_INTEGER_new()) == NULL)
377             return (NULL);
378         ret->type = V_ASN1_INTEGER;
379     } else
380         ret = (*a);
381
382     p = *pp;
383     inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
384     if (inf & 0x80) {
385         i = ASN1_R_BAD_OBJECT_HEADER;
386         goto err;
387     }
388
389     if (tag != V_ASN1_INTEGER) {
390         i = ASN1_R_EXPECTING_AN_INTEGER;
391         goto err;
392     }
393
394     /*
395      * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
396      * a missing NULL parameter.
397      */
398     s = OPENSSL_malloc((int)len + 1);
399     if (s == NULL) {
400         i = ERR_R_MALLOC_FAILURE;
401         goto err;
402     }
403     ret->type = V_ASN1_INTEGER;
404     if (len) {
405         if ((*p == 0) && (len != 1)) {
406             p++;
407             len--;
408         }
409         memcpy(s, p, (int)len);
410         p += len;
411     }
412
413     OPENSSL_free(ret->data);
414     ret->data = s;
415     ret->length = (int)len;
416     if (a != NULL)
417         (*a) = ret;
418     *pp = p;
419     return (ret);
420  err:
421     ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
422     if ((a == NULL) || (*a != ret))
423         ASN1_INTEGER_free(ret);
424     return (NULL);
425 }
426
427 static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
428                                       int atype)
429 {
430     ASN1_INTEGER *ret;
431     int len;
432
433     if (ai == NULL) {
434         ret = ASN1_STRING_type_new(atype);
435     } else {
436         ret = ai;
437         ret->type = atype;
438     }
439
440     if (ret == NULL) {
441         ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_NESTED_ASN1_ERROR);
442         goto err;
443     }
444
445     if (BN_is_negative(bn) && !BN_is_zero(bn))
446         ret->type |= V_ASN1_NEG_INTEGER;
447
448     len = BN_num_bytes(bn);
449
450     if (len == 0)
451         len = 1;
452
453     if (ASN1_STRING_set(ret, NULL, len) == 0) {
454         ASN1err(ASN1_F_BN_TO_ASN1_STRING, ERR_R_MALLOC_FAILURE);
455         goto err;
456     }
457
458     /* Correct zero case */
459     if (BN_is_zero(bn))
460         ret->data[0] = 0;
461     else
462         len = BN_bn2bin(bn, ret->data);
463     ret->length = len;
464     return ret;
465  err:
466     if (ret != ai)
467         ASN1_INTEGER_free(ret);
468     return (NULL);
469 }
470
471 static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
472                                  int itype)
473 {
474     BIGNUM *ret;
475
476     if ((ai->type & ~V_ASN1_NEG) != itype) {
477         ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);
478         return NULL;
479     }
480
481     ret = BN_bin2bn(ai->data, ai->length, bn);
482     if (ret == NULL) {
483         ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);
484         return NULL;
485     }
486     if (ai->type & V_ASN1_NEG)
487         BN_set_negative(ret, 1);
488     return ret;
489 }
490
491 int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
492 {
493     return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
494 }
495
496 int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
497 {
498     return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
499 }
500
501 int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
502 {
503     return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
504 }
505
506 int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
507 {
508     return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
509 }
510
511 int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
512 {
513     return ASN1_INTEGER_set_int64(a, v);
514 }
515
516 long ASN1_INTEGER_get(const ASN1_INTEGER *a)
517 {
518     int i;
519     int64_t r;
520     if (a == NULL)
521         return 0;
522     i = ASN1_INTEGER_get_int64(&r, a);
523     if (i == 0)
524         return -1;
525     if (r > LONG_MAX || r < LONG_MIN)
526         return -1;
527     return (long)r;
528 }
529
530 ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
531 {
532     return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
533 }
534
535 BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
536 {
537     return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
538 }
539
540 int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
541 {
542     return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
543 }
544
545 int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
546 {
547     return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
548 }
549
550 int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
551 {
552     return ASN1_ENUMERATED_set_int64(a, v);
553 }
554
555 long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
556 {
557     int i;
558     int64_t r;
559     if (a == NULL)
560         return 0;
561     if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
562         return -1;
563     if (a->length > (int)sizeof(long))
564         return 0xffffffffL;
565     i = ASN1_ENUMERATED_get_int64(&r, a);
566     if (i == 0)
567         return -1;
568     if (r > LONG_MAX || r < LONG_MIN)
569         return -1;
570     return (long)r;
571 }
572
573 ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
574 {
575     return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
576 }
577
578 BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
579 {
580     return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
581 }
582
583 /* Internal functions used by x_int64.c */
584 int c2i_uint64_int(uint64_t *ret, int *neg, const unsigned char **pp, long len)
585 {
586     unsigned char buf[sizeof(uint64_t)];
587     size_t buflen;
588
589     buflen = c2i_ibuf(NULL, NULL, *pp, len);
590     if (buflen == 0)
591         return 0;
592     if (buflen > sizeof(uint64_t)) {
593         ASN1err(ASN1_F_C2I_UINT64_INT, ASN1_R_TOO_LARGE);
594         return 0;
595     }
596     (void)c2i_ibuf(buf, neg, *pp, len);
597     return asn1_get_uint64(ret, buf, buflen);
598 }
599
600 int i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
601 {
602     unsigned char buf[sizeof(uint64_t)];
603     size_t off;
604
605     off = asn1_put_uint64(buf, r);
606     return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
607 }
608