diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/a_int.c')
-rw-r--r-- | src/lib/libcrypto/asn1/a_int.c | 131 |
1 files changed, 130 insertions, 1 deletions
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c index d14bd7959b..314bd2b369 100644 --- a/src/lib/libcrypto/asn1/a_int.c +++ b/src/lib/libcrypto/asn1/a_int.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: a_int.c,v 1.34 2019/04/28 05:03:56 tb Exp $ */ | 1 | /* $OpenBSD: a_int.c,v 1.35 2021/12/15 18:00:31 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -62,6 +62,7 @@ | |||
62 | 62 | ||
63 | #include <openssl/asn1.h> | 63 | #include <openssl/asn1.h> |
64 | #include <openssl/bn.h> | 64 | #include <openssl/bn.h> |
65 | #include <openssl/buffer.h> | ||
65 | #include <openssl/err.h> | 66 | #include <openssl/err.h> |
66 | 67 | ||
67 | static int | 68 | static int |
@@ -101,6 +102,134 @@ ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y) | |||
101 | return ret; | 102 | return ret; |
102 | } | 103 | } |
103 | 104 | ||
105 | int | ||
106 | i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a) | ||
107 | { | ||
108 | int i, n = 0; | ||
109 | static const char h[] = "0123456789ABCDEF"; | ||
110 | char buf[2]; | ||
111 | |||
112 | if (a == NULL) | ||
113 | return (0); | ||
114 | |||
115 | if (a->type & V_ASN1_NEG) { | ||
116 | if (BIO_write(bp, "-", 1) != 1) | ||
117 | goto err; | ||
118 | n = 1; | ||
119 | } | ||
120 | |||
121 | if (a->length == 0) { | ||
122 | if (BIO_write(bp, "00", 2) != 2) | ||
123 | goto err; | ||
124 | n += 2; | ||
125 | } else { | ||
126 | for (i = 0; i < a->length; i++) { | ||
127 | if ((i != 0) && (i % 35 == 0)) { | ||
128 | if (BIO_write(bp, "\\\n", 2) != 2) | ||
129 | goto err; | ||
130 | n += 2; | ||
131 | } | ||
132 | buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; | ||
133 | buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; | ||
134 | if (BIO_write(bp, buf, 2) != 2) | ||
135 | goto err; | ||
136 | n += 2; | ||
137 | } | ||
138 | } | ||
139 | return (n); | ||
140 | |||
141 | err: | ||
142 | return (-1); | ||
143 | } | ||
144 | |||
145 | int | ||
146 | a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size) | ||
147 | { | ||
148 | int ret = 0; | ||
149 | int i, j,k, m,n, again, bufsize; | ||
150 | unsigned char *s = NULL, *sp; | ||
151 | unsigned char *bufp; | ||
152 | int num = 0, slen = 0, first = 1; | ||
153 | |||
154 | bs->type = V_ASN1_INTEGER; | ||
155 | |||
156 | bufsize = BIO_gets(bp, buf, size); | ||
157 | for (;;) { | ||
158 | if (bufsize < 1) | ||
159 | goto err_sl; | ||
160 | i = bufsize; | ||
161 | if (buf[i - 1] == '\n') | ||
162 | buf[--i] = '\0'; | ||
163 | if (i == 0) | ||
164 | goto err_sl; | ||
165 | if (buf[i - 1] == '\r') | ||
166 | buf[--i] = '\0'; | ||
167 | if (i == 0) | ||
168 | goto err_sl; | ||
169 | if (buf[i - 1] == '\\') { | ||
170 | i--; | ||
171 | again = 1; | ||
172 | } else | ||
173 | again = 0; | ||
174 | buf[i] = '\0'; | ||
175 | if (i < 2) | ||
176 | goto err_sl; | ||
177 | |||
178 | bufp = (unsigned char *)buf; | ||
179 | if (first) { | ||
180 | first = 0; | ||
181 | if ((bufp[0] == '0') && (buf[1] == '0')) { | ||
182 | bufp += 2; | ||
183 | i -= 2; | ||
184 | } | ||
185 | } | ||
186 | k = 0; | ||
187 | if (i % 2 != 0) { | ||
188 | ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS); | ||
189 | goto err; | ||
190 | } | ||
191 | i /= 2; | ||
192 | if (num + i > slen) { | ||
193 | if ((sp = recallocarray(s, slen, num + i, 1)) == NULL) { | ||
194 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
195 | goto err; | ||
196 | } | ||
197 | s = sp; | ||
198 | slen = num + i; | ||
199 | } | ||
200 | for (j = 0; j < i; j++, k += 2) { | ||
201 | for (n = 0; n < 2; n++) { | ||
202 | m = bufp[k + n]; | ||
203 | if ((m >= '0') && (m <= '9')) | ||
204 | m -= '0'; | ||
205 | else if ((m >= 'a') && (m <= 'f')) | ||
206 | m = m - 'a' + 10; | ||
207 | else if ((m >= 'A') && (m <= 'F')) | ||
208 | m = m - 'A' + 10; | ||
209 | else { | ||
210 | ASN1error(ASN1_R_NON_HEX_CHARACTERS); | ||
211 | goto err; | ||
212 | } | ||
213 | s[num + j] <<= 4; | ||
214 | s[num + j] |= m; | ||
215 | } | ||
216 | } | ||
217 | num += i; | ||
218 | if (again) | ||
219 | bufsize = BIO_gets(bp, buf, size); | ||
220 | else | ||
221 | break; | ||
222 | } | ||
223 | bs->length = num; | ||
224 | bs->data = s; | ||
225 | return (1); | ||
226 | |||
227 | err_sl: | ||
228 | ASN1error(ASN1_R_SHORT_LINE); | ||
229 | err: | ||
230 | free(s); | ||
231 | return (ret); | ||
232 | } | ||
104 | 233 | ||
105 | /* | 234 | /* |
106 | * This converts an ASN1 INTEGER into its content encoding. | 235 | * This converts an ASN1 INTEGER into its content encoding. |