1 /*
2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 "crypto/ctype.h"
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/asn1.h>
15
i2a_ASN1_INTEGER(BIO * bp,const ASN1_INTEGER * a)16 int i2a_ASN1_INTEGER(BIO *bp, const ASN1_INTEGER *a)
17 {
18 int i, n = 0;
19 char buf[2];
20
21 if (a == NULL)
22 return 0;
23
24 if (a->type & V_ASN1_NEG) {
25 if (BIO_write(bp, "-", 1) != 1)
26 goto err;
27 n = 1;
28 }
29
30 if (a->length == 0) {
31 if (BIO_write(bp, "00", 2) != 2)
32 goto err;
33 n += 2;
34 } else {
35 for (i = 0; i < a->length; i++) {
36 if ((i != 0) && (i % 35 == 0)) {
37 if (BIO_write(bp, "\\\n", 2) != 2)
38 goto err;
39 n += 2;
40 }
41 ossl_to_hex(buf, a->data[i]);
42 if (BIO_write(bp, buf, 2) != 2)
43 goto err;
44 n += 2;
45 }
46 }
47 return n;
48 err:
49 return -1;
50 }
51
a2i_ASN1_INTEGER(BIO * bp,ASN1_INTEGER * bs,char * buf,int size)52 int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
53 {
54 int i, j, k, m, n, again, bufsize;
55 unsigned char *s = NULL, *sp;
56 unsigned char *bufp;
57 int num = 0, slen = 0, first = 1;
58
59 bs->type = V_ASN1_INTEGER;
60
61 bufsize = BIO_gets(bp, buf, size);
62 for (;;) {
63 if (bufsize < 1)
64 goto err;
65 i = bufsize;
66 if (buf[i - 1] == '\n')
67 buf[--i] = '\0';
68 if (i == 0)
69 goto err;
70 if (buf[i - 1] == '\r')
71 buf[--i] = '\0';
72 if (i == 0)
73 goto err;
74 again = (buf[i - 1] == '\\');
75
76 for (j = 0; j < i; j++) {
77 if (!ossl_isxdigit(buf[j])) {
78 i = j;
79 break;
80 }
81 }
82 buf[i] = '\0';
83 /*
84 * We have now cleared all the crap off the end of the line
85 */
86 if (i < 2)
87 goto err;
88
89 bufp = (unsigned char *)buf;
90 if (first) {
91 first = 0;
92 if ((bufp[0] == '0') && (bufp[1] == '0')) {
93 bufp += 2;
94 i -= 2;
95 }
96 }
97 k = 0;
98 i -= again;
99 if (i % 2 != 0) {
100 ERR_raise(ERR_LIB_ASN1, ASN1_R_ODD_NUMBER_OF_CHARS);
101 OPENSSL_free(s);
102 return 0;
103 }
104 i /= 2;
105 if (num + i > slen) {
106 sp = OPENSSL_clear_realloc(s, slen, num + i * 2);
107 if (sp == NULL) {
108 OPENSSL_free(s);
109 return 0;
110 }
111 s = sp;
112 slen = num + i * 2;
113 }
114 for (j = 0; j < i; j++, k += 2) {
115 for (n = 0; n < 2; n++) {
116 m = OPENSSL_hexchar2int(bufp[k + n]);
117 if (m < 0) {
118 ERR_raise(ERR_LIB_ASN1, ASN1_R_NON_HEX_CHARACTERS);
119 goto err;
120 }
121 s[num + j] <<= 4;
122 s[num + j] |= m;
123 }
124 }
125 num += i;
126 if (again)
127 bufsize = BIO_gets(bp, buf, size);
128 else
129 break;
130 }
131 bs->length = num;
132 bs->data = s;
133 return 1;
134 err:
135 ERR_raise(ERR_LIB_ASN1, ASN1_R_SHORT_LINE);
136 OPENSSL_free(s);
137 return 0;
138 }
139
i2a_ASN1_ENUMERATED(BIO * bp,const ASN1_ENUMERATED * a)140 int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
141 {
142 return i2a_ASN1_INTEGER(bp, a);
143 }
144
a2i_ASN1_ENUMERATED(BIO * bp,ASN1_ENUMERATED * bs,char * buf,int size)145 int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
146 {
147 int rv = a2i_ASN1_INTEGER(bp, bs, buf, size);
148 if (rv == 1)
149 bs->type = V_ASN1_INTEGER | (bs->type & V_ASN1_NEG);
150 return rv;
151 }
152