Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/f_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 <openssl/bio.h>
7 #include <openssl/err.h>
8
9 int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size) {
10 int i = 0, j = 0, k = 0, n = 0, again = 0, bufsize = 0;
11 unsigned char *s = NULL, *sp = NULL;
12 char *bufp = NULL;
13 int num = 0, slen = 0, first = 1;
14
15 bs->type = V_ASN1_INTEGER;
16
17 bufsize = BIO_gets(bp, buf, size);
18 for (;;) {
19 if (bufsize < 1) {
20 goto err;
21 }
22 i = bufsize;
23 if (buf[i - 1] == '\n') {
24 buf[--i] = '\0';
25 }
26 if (i == 0) {
27 goto err;
28 }
29 if (buf[i - 1] == '\r') {
30 buf[--i] = '\0';
31 }
32 if (i == 0) {
33 goto err;
34 }
35 again = (buf[i - 1] == '\\');
36
37 for (j = 0; j < i; j++) {
38 if (!OPENSSL_isxdigit(buf[j])) {
39 i = j;
40 break;
41 }
42 }
43 buf[i] = '\0';
44
45 if (i < 2) {
46 goto err;
47 }
48
49 bufp = buf;
50 if (first) {
51 first = 0;
52 if ((bufp[0] == '0') && (bufp[1] == '0')) {
53 bufp += 2;
54 i -= 2;
55 }
56 }
57 k = 0;
58 if (i % 2 != 0) {
59 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ODD_NUMBER_OF_CHARS);
60 OPENSSL_free(s);
61 return 0;
62 }
63 i /= 2;
64 if (num + i > slen) {
65 sp = OPENSSL_realloc(s, num + i);
66 if (sp == NULL) {
67 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
68 OPENSSL_free(s);
69 return 0;
70 }
71 s = sp;
72 slen = num + i;
73 }
74 for (j = 0; j < i; j++, k += 2) {
75 for (n = 0; n < 2; n++) {
76 uint8_t hex = 0;
77 if (!OPENSSL_fromxdigit(&hex, bufp[k + n])) {
78 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NON_HEX_CHARACTERS);
79 goto err;
80 }
81 s[num + j] <<= 4;
82 s[num + j] |= hex;
83 }
84 }
85 num += i;
86 if (again) {
87 bufsize = BIO_gets(bp, buf, size);
88 } else {
89 break;
90 }
91 }
92 bs->length = num;
93 bs->data = s;
94 return 1;
95 err:
96 OPENSSL_PUT_ERROR(ASN1, ASN1_R_SHORT_LINE);
97 OPENSSL_free(s);
98 return 0;
99 }
100
101 int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) {
102 int i, n = 0;
103 static const char *h = "0123456789ABCDEF";
104 char buf[2];
105
106 if (a == NULL) {
107 return 0;
108 }
109
110 if (a->type & V_ASN1_NEG) {
111 if (BIO_write(bp, "-", 1) != 1) {
112 goto err;
113 }
114 n = 1;
115 }
116
117 if (a->length == 0) {
118 if (BIO_write(bp, "00", 2) != 2) {
119 goto err;
120 }
121 n += 2;
122 } else {
123 for (i = 0; i < a->length; i++) {
124 if ((i != 0) && (i % 35 == 0)) {
125 if (BIO_write(bp, "\\\n", 2) != 2) {
126 goto err;
127 }
128 n += 2;
129 }
130 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
131 buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
132 if (BIO_write(bp, buf, 2) != 2) {
133 goto err;
134 }
135 n += 2;
136 }
137 }
138 return n;
139 err:
140 return -1;
141 }
142
143 int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a) {
144 return i2a_ASN1_INTEGER(bp, a);
145 }