Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_int.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <limits.h>
7 #include <string.h>
8
9 #include <openssl/bytestring.h>
10 #include <openssl/err.h>
11 #include <openssl/mem.h>
12 #include <openssl/type_check.h>
13
14 #include "../internal.h"
15
16
17 ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x) {
18 return ASN1_STRING_dup(x);
19 }
20
21 int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) {
22 // Compare signs.
23 int neg = x->type & V_ASN1_NEG;
24 if (neg != (y->type & V_ASN1_NEG)) {
25 return neg ? -1 : 1;
26 }
27
28 int ret = ASN1_STRING_cmp(x, y);
29 if (neg) {
30 // This could be |-ret|, but |ASN1_STRING_cmp| is not forbidden from
31 // returning |INT_MIN|.
32 if (ret < 0) {
33 return 1;
34 } else if (ret > 0) {
35 return -1;
36 } else {
37 return 0;
38 }
39 }
40
41 return ret;
42 }
43
44 // negate_twos_complement negates |len| bytes from |buf| in-place, interpreted
45 // as a signed, big-endian two's complement value.
46 static void negate_twos_complement(uint8_t *buf, size_t len) {
47 uint8_t borrow = 0;
48 for (size_t i = len - 1; i < len; i--) {
49 uint8_t t = buf[i];
50 buf[i] = 0u - borrow - t;
51 borrow |= t != 0;
52 }
53 }
54
55 static int is_all_zeros(const uint8_t *in, size_t len) {
56 for (size_t i = 0; i < len; i++) {
57 if (in[i] != 0) {
58 return 0;
59 }
60 }
61 return 1;
62 }
63
64 int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
65 if (in == NULL) {
66 return 0;
67 }
68
69 // |ASN1_INTEGER|s should be represented minimally, but it is possible to
70 // construct invalid ones. Skip leading zeros so this does not produce an
71 // invalid encoding or break invariants.
72 CBS cbs;
73 CBS_init(&cbs, in->data, in->length);
74 while (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0) {
75 CBS_skip(&cbs, 1);
76 }
77
78 int is_negative = (in->type & V_ASN1_NEG) != 0;
79 size_t pad;
80 CBS copy = cbs;
81 uint8_t msb;
82 if (!CBS_get_u8(&copy, &msb)) {
83 // Zero is represented as a single byte.
84 is_negative = 0;
85 pad = 1;
86 } else if (is_negative) {
87 // 0x80...01 through 0xff...ff have a two's complement of 0x7f...ff
88 // through 0x00...01 and need an extra byte to be negative.
89 // 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff
90 // through 0x80...00 and can be negated as-is.
91 pad = msb > 0x80 ||
92 (msb == 0x80 && !is_all_zeros(CBS_data(&copy), CBS_len(&copy)));
93 } else {
94 // If the high bit is set, the signed representation needs an extra
95 // byte to be positive.
96 pad = (msb & 0x80) != 0;
97 }
98
99 if (CBS_len(&cbs) > INT_MAX - pad) {
100 OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
101 return 0;
102 }
103 int len = (int)(pad + CBS_len(&cbs));
104 assert(len > 0);
105 if (outp == NULL) {
106 return len;
107 }
108
109 if (pad) {
110 (*outp)[0] = 0;
111 }
112 OPENSSL_memcpy(*outp + pad, CBS_data(&cbs), CBS_len(&cbs));
113 if (is_negative) {
114 negate_twos_complement(*outp, len);
115 assert((*outp)[0] >= 0x80);
116 } else {
117 assert((*outp)[0] < 0x80);
118 }
119 *outp += len;
120 return len;
121 }
122
123 ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **out, const unsigned char **inp,
124 long len) {
125 // This function can handle lengths up to INT_MAX - 1, but the rest of the
126 // legacy ASN.1 code mixes integer types, so avoid exposing it to
127 // ASN1_INTEGERS with larger lengths.
128 if (len < 0 || len > INT_MAX / 2) {
129 OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
130 return NULL;
131 }
132
133 CBS cbs;
134 CBS_init(&cbs, *inp, (size_t)len);
135 int is_negative;
136 if (!CBS_is_valid_asn1_integer(&cbs, &is_negative)) {
137 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
138 return NULL;
139 }
140
141 ASN1_INTEGER *ret = NULL;
142 if (out == NULL || *out == NULL) {
143 ret = ASN1_INTEGER_new();
144 if (ret == NULL) {
145 return NULL;
146 }
147 } else {
148 ret = *out;
149 }
150
151 // Convert to |ASN1_INTEGER|'s sign-and-magnitude representation. First,
152 // determine the size needed for a minimal result.
153 if (is_negative) {
154 // 0xff00...01 through 0xff7f..ff have a two's complement of 0x00ff...ff
155 // through 0x000100...001 and need one leading zero removed. 0x8000...00
156 // through 0xff00...00 have a two's complement of 0x8000...00 through
157 // 0x0100...00 and will be minimally-encoded as-is.
158 if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0xff &&
159 !is_all_zeros(CBS_data(&cbs) + 1, CBS_len(&cbs) - 1)) {
160 CBS_skip(&cbs, 1);
161 }
162 } else {
163 // Remove the leading zero byte, if any.
164 if (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0x00) {
165 CBS_skip(&cbs, 1);
166 }
167 }
168
169 if (!ASN1_STRING_set(ret, CBS_data(&cbs), CBS_len(&cbs))) {
170 goto err;
171 }
172
173 if (is_negative) {
174 ret->type = V_ASN1_NEG_INTEGER;
175 negate_twos_complement(ret->data, ret->length);
176 } else {
177 ret->type = V_ASN1_INTEGER;
178 }
179
180 // The value should be minimally-encoded.
181 assert(ret->length == 0 || ret->data[0] != 0);
182 // Zero is not negative.
183 assert(!is_negative || ret->length > 0);
184
185 *inp += len;
186 if (out != NULL) {
187 *out = ret;
188 }
189 return ret;
190
191 err:
192 if (ret != NULL && (out == NULL || *out != ret)) {
193 ASN1_INTEGER_free(ret);
194 }
195 return NULL;
196 }
197
198 int ASN1_INTEGER_set_int64(ASN1_INTEGER *a, int64_t v) {
199 if (v >= 0) {
200 return ASN1_INTEGER_set_uint64(a, (uint64_t)v);
201 }
202
203 if (!ASN1_INTEGER_set_uint64(a, 0 - (uint64_t)v)) {
204 return 0;
205 }
206
207 a->type = V_ASN1_NEG_INTEGER;
208 return 1;
209 }
210
211 int ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *a, int64_t v) {
212 if (v >= 0) {
213 return ASN1_ENUMERATED_set_uint64(a, (uint64_t)v);
214 }
215
216 if (!ASN1_ENUMERATED_set_uint64(a, 0 - (uint64_t)v)) {
217 return 0;
218 }
219
220 a->type = V_ASN1_NEG_ENUMERATED;
221 return 1;
222 }
223
224 int ASN1_INTEGER_set(ASN1_INTEGER *a, long v) {
225 OPENSSL_STATIC_ASSERT(sizeof(long) <= sizeof(int64_t), long_fits_in_int64_t);
226 return ASN1_INTEGER_set_int64(a, v);
227 }
228
229 int ASN1_ENUMERATED_set(ASN1_ENUMERATED *a, long v) {
230 OPENSSL_STATIC_ASSERT(sizeof(long) <= sizeof(int64_t), long_fits_in_int64_t);
231 return ASN1_ENUMERATED_set_int64(a, v);
232 }
233
234 static int asn1_string_set_uint64(ASN1_STRING *out, uint64_t v, int type) {
235 uint8_t buf[sizeof(uint64_t)];
236 CRYPTO_store_u64_be(buf, v);
237 size_t leading_zeros;
238 for (leading_zeros = 0; leading_zeros < sizeof(buf); leading_zeros++) {
239 if (buf[leading_zeros] != 0) {
240 break;
241 }
242 }
243
244 if (!ASN1_STRING_set(out, buf + leading_zeros, sizeof(buf) - leading_zeros)) {
245 return 0;
246 }
247 out->type = type;
248 return 1;
249 }
250
251 int ASN1_INTEGER_set_uint64(ASN1_INTEGER *out, uint64_t v) {
252 return asn1_string_set_uint64(out, v, V_ASN1_INTEGER);
253 }
254
255 int ASN1_ENUMERATED_set_uint64(ASN1_ENUMERATED *out, uint64_t v) {
256 return asn1_string_set_uint64(out, v, V_ASN1_ENUMERATED);
257 }
258
259 static int asn1_string_get_abs_uint64(uint64_t *out, const ASN1_STRING *a,
260 int type) {
261 if ((a->type & ~V_ASN1_NEG) != type) {
262 OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
263 return 0;
264 }
265 uint8_t buf[sizeof(uint64_t)] = {0};
266 if (a->length > (int)sizeof(buf)) {
267 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
268 return 0;
269 }
270 OPENSSL_memcpy(buf + sizeof(buf) - a->length, a->data, a->length);
271 *out = CRYPTO_load_u64_be(buf);
272 return 1;
273 }
274
275 static int asn1_string_get_uint64(uint64_t *out, const ASN1_STRING *a,
276 int type) {
277 if (!asn1_string_get_abs_uint64(out, a, type)) {
278 return 0;
279 }
280 if (a->type & V_ASN1_NEG) {
281 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
282 return 0;
283 }
284 return 1;
285 }
286
287 int ASN1_INTEGER_get_uint64(uint64_t *out, const ASN1_INTEGER *a) {
288 return asn1_string_get_uint64(out, a, V_ASN1_INTEGER);
289 }
290
291 int ASN1_ENUMERATED_get_uint64(uint64_t *out, const ASN1_ENUMERATED *a) {
292 return asn1_string_get_uint64(out, a, V_ASN1_ENUMERATED);
293 }
294
295 static int asn1_string_get_int64(int64_t *out, const ASN1_STRING *a, int type) {
296 uint64_t v;
297 if (!asn1_string_get_abs_uint64(&v, a, type)) {
298 return 0;
299 }
300 int64_t i64;
301 int fits_in_i64;
302 // Check |v != 0| to handle manually-constructed negative zeros.
303 if ((a->type & V_ASN1_NEG) && v != 0) {
304 i64 = (int64_t)(0u - v);
305 fits_in_i64 = i64 < 0;
306 } else {
307 i64 = (int64_t)v;
308 fits_in_i64 = i64 >= 0;
309 }
310 if (!fits_in_i64) {
311 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_INTEGER);
312 return 0;
313 }
314 *out = i64;
315 return 1;
316 }
317
318 int ASN1_INTEGER_get_int64(int64_t *out, const ASN1_INTEGER *a) {
319 return asn1_string_get_int64(out, a, V_ASN1_INTEGER);
320 }
321
322 int ASN1_ENUMERATED_get_int64(int64_t *out, const ASN1_ENUMERATED *a) {
323 return asn1_string_get_int64(out, a, V_ASN1_ENUMERATED);
324 }
325
326 static long asn1_string_get_long(const ASN1_STRING *a, int type) {
327 if (a == NULL) {
328 return 0;
329 }
330
331 int64_t v;
332 if (!asn1_string_get_int64(&v, a, type) || //
333 v < LONG_MIN || v > LONG_MAX) {
334 // This function's return value does not distinguish overflow from -1.
335 ERR_clear_error();
336 return -1;
337 }
338
339 return (long)v;
340 }
341
342 long ASN1_INTEGER_get(const ASN1_INTEGER *a) {
343 return asn1_string_get_long(a, V_ASN1_INTEGER);
344 }
345
346 long ASN1_ENUMERATED_get(const ASN1_ENUMERATED *a) {
347 return asn1_string_get_long(a, V_ASN1_ENUMERATED);
348 }
349
350 static ASN1_STRING *bn_to_asn1_string(const BIGNUM *bn, ASN1_STRING *ai,
351 int type) {
352 ASN1_INTEGER *ret;
353 if (ai == NULL) {
354 ret = ASN1_STRING_type_new(type);
355 } else {
356 ret = ai;
357 }
358 if (ret == NULL) {
359 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
360 goto err;
361 }
362
363 if (BN_is_negative(bn) && !BN_is_zero(bn)) {
364 ret->type = type | V_ASN1_NEG;
365 } else {
366 ret->type = type;
367 }
368
369 int len = BN_num_bytes(bn);
370 if (!ASN1_STRING_set(ret, NULL, len) ||
371 !BN_bn2bin_padded(ret->data, len, bn)) {
372 goto err;
373 }
374 return ret;
375
376 err:
377 if (ret != ai) {
378 ASN1_STRING_free(ret);
379 }
380 return NULL;
381 }
382
383 ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai) {
384 return bn_to_asn1_string(bn, ai, V_ASN1_INTEGER);
385 }
386
387 ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) {
388 return bn_to_asn1_string(bn, ai, V_ASN1_ENUMERATED);
389 }
390
391 static BIGNUM *asn1_string_to_bn(const ASN1_STRING *ai, BIGNUM *bn, int type) {
392 if ((ai->type & ~V_ASN1_NEG) != type) {
393 OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_INTEGER_TYPE);
394 return NULL;
395 }
396
397 BIGNUM *ret;
398 if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) {
399 OPENSSL_PUT_ERROR(ASN1, ASN1_R_BN_LIB);
400 } else if (ai->type & V_ASN1_NEG) {
401 BN_set_negative(ret, 1);
402 }
403 return ret;
404 }
405
406 BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn) {
407 return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);
408 }
409
410 BIGNUM *ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn) {
411 return asn1_string_to_bn(ai, bn, V_ASN1_ENUMERATED);
412 }