summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_enum.c
diff options
context:
space:
mode:
authorjsing <>2021-12-15 18:00:32 +0000
committerjsing <>2021-12-15 18:00:32 +0000
commite61e8eab0ed72cba26134860e9976f836728d877 (patch)
tree582bd7e91dc07b37e1a88c57120d362994f5a80b /src/lib/libcrypto/asn1/a_enum.c
parentd97abc08cae4df58901d8ea1f6fe74e35d142843 (diff)
downloadopenbsd-e61e8eab0ed72cba26134860e9976f836728d877.tar.gz
openbsd-e61e8eab0ed72cba26134860e9976f836728d877.tar.bz2
openbsd-e61e8eab0ed72cba26134860e9976f836728d877.zip
Consolidate various ASN.1 code.
Rather than having multiple files per type (with minimal code per file), use one file per type (a_<type>.c). No functional change. Discussed with tb@
Diffstat (limited to 'src/lib/libcrypto/asn1/a_enum.c')
-rw-r--r--src/lib/libcrypto/asn1/a_enum.c130
1 files changed, 129 insertions, 1 deletions
diff --git a/src/lib/libcrypto/asn1/a_enum.c b/src/lib/libcrypto/asn1/a_enum.c
index 0952e049db..e0e64f0a81 100644
--- a/src/lib/libcrypto/asn1/a_enum.c
+++ b/src/lib/libcrypto/asn1/a_enum.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_enum.c,v 1.20 2019/04/28 05:05:56 tb Exp $ */ 1/* $OpenBSD: a_enum.c,v 1.21 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 *
@@ -61,6 +61,7 @@
61 61
62#include <openssl/asn1.h> 62#include <openssl/asn1.h>
63#include <openssl/bn.h> 63#include <openssl/bn.h>
64#include <openssl/buffer.h>
64#include <openssl/err.h> 65#include <openssl/err.h>
65 66
66/* 67/*
@@ -192,3 +193,130 @@ ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
192 BN_set_negative(ret, 1); 193 BN_set_negative(ret, 1);
193 return (ret); 194 return (ret);
194} 195}
196
197/* Based on a_int.c: equivalent ENUMERATED functions */
198
199int
200i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
201{
202 int i, n = 0;
203 static const char h[] = "0123456789ABCDEF";
204 char buf[2];
205
206 if (a == NULL)
207 return (0);
208
209 if (a->length == 0) {
210 if (BIO_write(bp, "00", 2) != 2)
211 goto err;
212 n = 2;
213 } else {
214 for (i = 0; i < a->length; i++) {
215 if ((i != 0) && (i % 35 == 0)) {
216 if (BIO_write(bp, "\\\n", 2) != 2)
217 goto err;
218 n += 2;
219 }
220 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
221 buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
222 if (BIO_write(bp, buf, 2) != 2)
223 goto err;
224 n += 2;
225 }
226 }
227 return (n);
228
229err:
230 return (-1);
231}
232
233int
234a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
235{
236 int ret = 0;
237 int i, j,k, m,n, again, bufsize;
238 unsigned char *s = NULL, *sp;
239 unsigned char *bufp;
240 int first = 1;
241 size_t num = 0, slen = 0;
242
243 bs->type = V_ASN1_ENUMERATED;
244
245 bufsize = BIO_gets(bp, buf, size);
246 for (;;) {
247 if (bufsize < 1)
248 goto err_sl;
249 i = bufsize;
250 if (buf[i-1] == '\n')
251 buf[--i] = '\0';
252 if (i == 0)
253 goto err_sl;
254 if (buf[i-1] == '\r')
255 buf[--i] = '\0';
256 if (i == 0)
257 goto err_sl;
258 if (buf[i - 1] == '\\') {
259 i--;
260 again = 1;
261 } else
262 again = 0;
263 buf[i] = '\0';
264 if (i < 2)
265 goto err_sl;
266
267 bufp = (unsigned char *)buf;
268 if (first) {
269 first = 0;
270 if ((bufp[0] == '0') && (buf[1] == '0')) {
271 bufp += 2;
272 i -= 2;
273 }
274 }
275 k = 0;
276 if (i % 2 != 0) {
277 ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS);
278 goto err;
279 }
280 i /= 2;
281 if (num + i > slen) {
282 sp = realloc(s, num + i);
283 if (sp == NULL) {
284 ASN1error(ERR_R_MALLOC_FAILURE);
285 goto err;
286 }
287 s = sp;
288 slen = num + i;
289 }
290 for (j = 0; j < i; j++, k += 2) {
291 for (n = 0; n < 2; n++) {
292 m = bufp[k + n];
293 if ((m >= '0') && (m <= '9'))
294 m -= '0';
295 else if ((m >= 'a') && (m <= 'f'))
296 m = m - 'a' + 10;
297 else if ((m >= 'A') && (m <= 'F'))
298 m = m - 'A' + 10;
299 else {
300 ASN1error(ASN1_R_NON_HEX_CHARACTERS);
301 goto err;
302 }
303 s[num + j] <<= 4;
304 s[num + j] |= m;
305 }
306 }
307 num += i;
308 if (again)
309 bufsize = BIO_gets(bp, buf, size);
310 else
311 break;
312 }
313 bs->length = num;
314 bs->data = s;
315 return (1);
316
317err_sl:
318 ASN1error(ASN1_R_SHORT_LINE);
319err:
320 free(s);
321 return (ret);
322}