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