]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/a_int.c
Update copyright year
[thirdparty/openssl.git] / crypto / asn1 / a_int.c
1 /*
2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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_local.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 if (len != 0) {
83 /*
84 * if len == 0 then src/dst could be NULL, and this would be undefined
85 * behaviour.
86 */
87 dst += len;
88 src += len;
89 }
90 /* two's complement value: ~value + 1 */
91 while (len-- != 0) {
92 *(--dst) = (unsigned char)(carry += *(--src) ^ pad);
93 carry >>= 8;
94 }
95 }
96
97 static size_t i2c_ibuf(const unsigned char *b, size_t blen, int neg,
98 unsigned char **pp)
99 {
100 unsigned int pad = 0;
101 size_t ret, i;
102 unsigned char *p, pb = 0;
103
104 if (b != NULL && blen) {
105 ret = blen;
106 i = b[0];
107 if (!neg && (i > 127)) {
108 pad = 1;
109 pb = 0;
110 } else if (neg) {
111 pb = 0xFF;
112 if (i > 128) {
113 pad = 1;
114 } else if (i == 128) {
115 /*
116 * Special case [of minimal negative for given length]:
117 * if any other bytes non zero we pad, otherwise we don't.
118 */
119 for (pad = 0, i = 1; i < blen; i++)
120 pad |= b[i];
121 pb = pad != 0 ? 0xffU : 0;
122 pad = pb & 1;
123 }
124 }
125 ret += pad;
126 } else {
127 ret = 1;
128 blen = 0; /* reduce '(b == NULL || blen == 0)' to '(blen == 0)' */
129 }
130
131 if (pp == NULL || (p = *pp) == NULL)
132 return ret;
133
134 /*
135 * This magically handles all corner cases, such as '(b == NULL ||
136 * blen == 0)', non-negative value, "negative" zero, 0x80 followed
137 * by any number of zeros...
138 */
139 *p = pb;
140 p += pad; /* yes, p[0] can be written twice, but it's little
141 * price to pay for eliminated branches */
142 twos_complement(p, b, blen, pb);
143
144 *pp += ret;
145 return ret;
146 }
147
148 /*
149 * convert content octets into a big endian buffer. Returns the length
150 * of buffer or 0 on error: for malformed INTEGER. If output buffer is
151 * NULL just return length.
152 */
153
154 static size_t c2i_ibuf(unsigned char *b, int *pneg,
155 const unsigned char *p, size_t plen)
156 {
157 int neg, pad;
158 /* Zero content length is illegal */
159 if (plen == 0) {
160 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT);
161 return 0;
162 }
163 neg = p[0] & 0x80;
164 if (pneg)
165 *pneg = neg;
166 /* Handle common case where length is 1 octet separately */
167 if (plen == 1) {
168 if (b != NULL) {
169 if (neg)
170 b[0] = (p[0] ^ 0xFF) + 1;
171 else
172 b[0] = p[0];
173 }
174 return 1;
175 }
176
177 pad = 0;
178 if (p[0] == 0) {
179 pad = 1;
180 } else if (p[0] == 0xFF) {
181 size_t i;
182
183 /*
184 * Special case [of "one less minimal negative" for given length]:
185 * if any other bytes non zero it was padded, otherwise not.
186 */
187 for (pad = 0, i = 1; i < plen; i++)
188 pad |= p[i];
189 pad = pad != 0 ? 1 : 0;
190 }
191 /* reject illegal padding: first two octets MSB can't match */
192 if (pad && (neg == (p[1] & 0x80))) {
193 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING);
194 return 0;
195 }
196
197 /* skip over pad */
198 p += pad;
199 plen -= pad;
200
201 if (b != NULL)
202 twos_complement(b, p, plen, neg ? 0xffU : 0);
203
204 return plen;
205 }
206
207 int ossl_i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
208 {
209 return i2c_ibuf(a->data, a->length, a->type & V_ASN1_NEG, pp);
210 }
211
212 /* Convert big endian buffer into uint64_t, return 0 on error */
213 static int asn1_get_uint64(uint64_t *pr, const unsigned char *b, size_t blen)
214 {
215 size_t i;
216 uint64_t r;
217
218 if (blen > sizeof(*pr)) {
219 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
220 return 0;
221 }
222 if (b == NULL)
223 return 0;
224 for (r = 0, i = 0; i < blen; i++) {
225 r <<= 8;
226 r |= b[i];
227 }
228 *pr = r;
229 return 1;
230 }
231
232 /*
233 * Write uint64_t to big endian buffer and return offset to first
234 * written octet. In other words it returns offset in range from 0
235 * to 7, with 0 denoting 8 written octets and 7 - one.
236 */
237 static size_t asn1_put_uint64(unsigned char b[sizeof(uint64_t)], uint64_t r)
238 {
239 size_t off = sizeof(uint64_t);
240
241 do {
242 b[--off] = (unsigned char)r;
243 } while (r >>= 8);
244
245 return off;
246 }
247
248 /*
249 * Absolute value of INT64_MIN: we can't just use -INT64_MIN as gcc produces
250 * overflow warnings.
251 */
252 #define ABS_INT64_MIN ((uint64_t)INT64_MAX + (-(INT64_MIN + INT64_MAX)))
253
254 /* signed version of asn1_get_uint64 */
255 static int asn1_get_int64(int64_t *pr, const unsigned char *b, size_t blen,
256 int neg)
257 {
258 uint64_t r;
259 if (asn1_get_uint64(&r, b, blen) == 0)
260 return 0;
261 if (neg) {
262 if (r <= INT64_MAX) {
263 /* Most significant bit is guaranteed to be clear, negation
264 * is guaranteed to be meaningful in platform-neutral sense. */
265 *pr = -(int64_t)r;
266 } else if (r == ABS_INT64_MIN) {
267 /* This never happens if INT64_MAX == ABS_INT64_MIN, e.g.
268 * on ones'-complement system. */
269 *pr = (int64_t)(0 - r);
270 } else {
271 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
272 return 0;
273 }
274 } else {
275 if (r <= INT64_MAX) {
276 *pr = (int64_t)r;
277 } else {
278 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
279 return 0;
280 }
281 }
282 return 1;
283 }
284
285 /* Convert ASN1 INTEGER content octets to ASN1_INTEGER structure */
286 ASN1_INTEGER *ossl_c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
287 long len)
288 {
289 ASN1_INTEGER *ret = NULL;
290 size_t r;
291 int neg;
292
293 r = c2i_ibuf(NULL, NULL, *pp, len);
294
295 if (r == 0)
296 return NULL;
297
298 if ((a == NULL) || ((*a) == NULL)) {
299 ret = ASN1_INTEGER_new();
300 if (ret == NULL)
301 return NULL;
302 ret->type = V_ASN1_INTEGER;
303 } else
304 ret = *a;
305
306 if (ASN1_STRING_set(ret, NULL, r) == 0)
307 goto err;
308
309 c2i_ibuf(ret->data, &neg, *pp, len);
310
311 if (neg != 0)
312 ret->type |= V_ASN1_NEG;
313 else
314 ret->type &= ~V_ASN1_NEG;
315
316 *pp += len;
317 if (a != NULL)
318 (*a) = ret;
319 return ret;
320 err:
321 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
322 if (a == NULL || *a != ret)
323 ASN1_INTEGER_free(ret);
324 return NULL;
325 }
326
327 static int asn1_string_get_int64(int64_t *pr, const ASN1_STRING *a, int itype)
328 {
329 if (a == NULL) {
330 ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
331 return 0;
332 }
333 if ((a->type & ~V_ASN1_NEG) != itype) {
334 ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
335 return 0;
336 }
337 return asn1_get_int64(pr, a->data, a->length, a->type & V_ASN1_NEG);
338 }
339
340 static int asn1_string_set_int64(ASN1_STRING *a, int64_t r, int itype)
341 {
342 unsigned char tbuf[sizeof(r)];
343 size_t off;
344
345 a->type = itype;
346 if (r < 0) {
347 /* Most obvious '-r' triggers undefined behaviour for most
348 * common INT64_MIN. Even though below '0 - (uint64_t)r' can
349 * appear two's-complement centric, it does produce correct/
350 * expected result even on one's-complement. This is because
351 * cast to unsigned has to change bit pattern... */
352 off = asn1_put_uint64(tbuf, 0 - (uint64_t)r);
353 a->type |= V_ASN1_NEG;
354 } else {
355 off = asn1_put_uint64(tbuf, r);
356 a->type &= ~V_ASN1_NEG;
357 }
358 return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
359 }
360
361 static int asn1_string_get_uint64(uint64_t *pr, const ASN1_STRING *a,
362 int itype)
363 {
364 if (a == NULL) {
365 ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
366 return 0;
367 }
368 if ((a->type & ~V_ASN1_NEG) != itype) {
369 ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
370 return 0;
371 }
372 if (a->type & V_ASN1_NEG) {
373 ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
374 return 0;
375 }
376 return asn1_get_uint64(pr, a->data, a->length);
377 }
378
379 static int asn1_string_set_uint64(ASN1_STRING *a, uint64_t r, int itype)
380 {
381 unsigned char tbuf[sizeof(r)];
382 size_t off;
383
384 a->type = itype;
385 off = asn1_put_uint64(tbuf, r);
386 return ASN1_STRING_set(a, tbuf + off, sizeof(tbuf) - off);
387 }
388
389 /*
390 * This is a version of d2i_ASN1_INTEGER that ignores the sign bit of ASN1
391 * integers: some broken software can encode a positive INTEGER with its MSB
392 * set as negative (it doesn't add a padding zero).
393 */
394
395 ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
396 long length)
397 {
398 ASN1_INTEGER *ret = NULL;
399 const unsigned char *p;
400 unsigned char *s;
401 long len;
402 int inf, tag, xclass;
403 int i;
404
405 if ((a == NULL) || ((*a) == NULL)) {
406 if ((ret = ASN1_INTEGER_new()) == NULL)
407 return NULL;
408 ret->type = V_ASN1_INTEGER;
409 } else
410 ret = (*a);
411
412 p = *pp;
413 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
414 if (inf & 0x80) {
415 i = ASN1_R_BAD_OBJECT_HEADER;
416 goto err;
417 }
418
419 if (tag != V_ASN1_INTEGER) {
420 i = ASN1_R_EXPECTING_AN_INTEGER;
421 goto err;
422 }
423
424 /*
425 * We must OPENSSL_malloc stuff, even for 0 bytes otherwise it signifies
426 * a missing NULL parameter.
427 */
428 s = OPENSSL_malloc((int)len + 1);
429 if (s == NULL) {
430 i = ERR_R_MALLOC_FAILURE;
431 goto err;
432 }
433 ret->type = V_ASN1_INTEGER;
434 if (len) {
435 if ((*p == 0) && (len != 1)) {
436 p++;
437 len--;
438 }
439 memcpy(s, p, (int)len);
440 p += len;
441 }
442
443 OPENSSL_free(ret->data);
444 ret->data = s;
445 ret->length = (int)len;
446 if (a != NULL)
447 (*a) = ret;
448 *pp = p;
449 return ret;
450 err:
451 ERR_raise(ERR_LIB_ASN1, i);
452 if ((a == NULL) || (*a != ret))
453 ASN1_INTEGER_free(ret);
454 return NULL;
455 }
456
457 static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
458 int atype)
459 {
460 ASN1_INTEGER *ret;
461 int len;
462
463 if (ai == NULL) {
464 ret = ASN1_STRING_type_new(atype);
465 } else {
466 ret = ai;
467 ret->type = atype;
468 }
469
470 if (ret == NULL) {
471 ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
472 goto err;
473 }
474
475 if (BN_is_negative(bn) && !BN_is_zero(bn))
476 ret->type |= V_ASN1_NEG_INTEGER;
477
478 len = BN_num_bytes(bn);
479
480 if (len == 0)
481 len = 1;
482
483 if (ASN1_STRING_set(ret, NULL, len) == 0) {
484 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
485 goto err;
486 }
487
488 /* Correct zero case */
489 if (BN_is_zero(bn))
490 ret->data[0] = 0;
491 else
492 len = BN_bn2bin(bn, ret->data);
493 ret->length = len;
494 return ret;
495 err:
496 if (ret != ai)
497 ASN1_INTEGER_free(ret);
498 return NULL;
499 }
500
501 static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,
502 int itype)
503 {
504 BIGNUM *ret;
505
506 if ((ai->type & ~V_ASN1_NEG) != itype) {
507 ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_INTEGER_TYPE);
508 return NULL;
509 }
510
511 ret = BN_bin2bn(ai->data, ai->length, bn);
512 if (ret == NULL) {
513 ERR_raise(ERR_LIB_ASN1, ASN1_R_BN_LIB);
514 return NULL;
515 }
516 if (ai->type & V_ASN1_NEG)
517 BN_set_negative(ret, 1);
518 return ret;
519 }
520
521 int ASN1_INTEGER_get_int64(int64_t *pr, const ASN1_INTEGER *a)
522 {
523 return asn1_string_get_int64(pr, a, V_ASN1_INTEGER);
524 }
525
526 int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t r)
527 {
528 return asn1_string_set_int64(a, r, V_ASN1_INTEGER);
529 }
530
531 int ASN1_INTEGER_get_uint64(uint64_t *pr, const ASN1_INTEGER *a)
532 {
533 return asn1_string_get_uint64(pr, a, V_ASN1_INTEGER);
534 }
535
536 int ASN1_INTEGER_set_uint64(ASN1_INTEGER *a, uint64_t r)
537 {
538 return asn1_string_set_uint64(a, r, V_ASN1_INTEGER);
539 }
540
541 int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
542 {
543 return ASN1_INTEGER_set_int64(a, v);
544 }
545
546 long ASN1_INTEGER_get(const ASN1_INTEGER *a)
547 {
548 int i;
549 int64_t r;
550 if (a == NULL)
551 return 0;
552 i = ASN1_INTEGER_get_int64(&r, a);
553 if (i == 0)
554 return -1;
555 if (r > LONG_MAX || r < LONG_MIN)
556 return -1;
557 return (long)r;
558 }
559
560 ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
561 {
562 return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
563 }
564
565 BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
566 {
567 return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
568 }
569
570 int ASN1_ENUMERATED_get_int64(int64_t *pr, const ASN1_ENUMERATED *a)
571 {
572 return asn1_string_get_int64(pr, a, V_ASN1_ENUMERATED);
573 }
574
575 int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t r)
576 {
577 return asn1_string_set_int64(a, r, V_ASN1_ENUMERATED);
578 }
579
580 int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v)
581 {
582 return ASN1_ENUMERATED_set_int64(a, v);
583 }
584
585 long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a)
586 {
587 int i;
588 int64_t r;
589 if (a == NULL)
590 return 0;
591 if ((a->type & ~V_ASN1_NEG) != V_ASN1_ENUMERATED)
592 return -1;
593 if (a->length > (int)sizeof(long))
594 return 0xffffffffL;
595 i = ASN1_ENUMERATED_get_int64(&r, a);
596 if (i == 0)
597 return -1;
598 if (r > LONG_MAX || r < LONG_MIN)
599 return -1;
600 return (long)r;
601 }
602
603 ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
604 {
605 return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
606 }
607
608 BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
609 {
610 return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
611 }
612
613 /* Internal functions used by x_int64.c */
614 int ossl_c2i_uint64_int(uint64_t *ret, int *neg,
615 const unsigned char **pp, long len)
616 {
617 unsigned char buf[sizeof(uint64_t)];
618 size_t buflen;
619
620 buflen = c2i_ibuf(NULL, NULL, *pp, len);
621 if (buflen == 0)
622 return 0;
623 if (buflen > sizeof(uint64_t)) {
624 ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
625 return 0;
626 }
627 (void)c2i_ibuf(buf, neg, *pp, len);
628 return asn1_get_uint64(ret, buf, buflen);
629 }
630
631 int ossl_i2c_uint64_int(unsigned char *p, uint64_t r, int neg)
632 {
633 unsigned char buf[sizeof(uint64_t)];
634 size_t off;
635
636 off = asn1_put_uint64(buf, r);
637 return i2c_ibuf(buf + off, sizeof(buf) - off, neg, &p);
638 }
639