/src/openssl31/crypto/asn1/f_string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2020 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 | | |
16 | | int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type) |
17 | 11.2k | { |
18 | 11.2k | int i, n = 0; |
19 | 11.2k | static const char *h = "0123456789ABCDEF"; |
20 | 11.2k | char buf[2]; |
21 | | |
22 | 11.2k | if (a == NULL) |
23 | 0 | return 0; |
24 | | |
25 | 11.2k | if (a->length == 0) { |
26 | 8.84k | if (BIO_write(bp, "0", 1) != 1) |
27 | 0 | goto err; |
28 | 8.84k | n = 1; |
29 | 8.84k | } else { |
30 | 21.5M | for (i = 0; i < a->length; i++) { |
31 | 21.5M | if ((i != 0) && (i % 35 == 0)) { |
32 | 614k | if (BIO_write(bp, "\\\n", 2) != 2) |
33 | 0 | goto err; |
34 | 614k | n += 2; |
35 | 614k | } |
36 | 21.5M | buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; |
37 | 21.5M | buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; |
38 | 21.5M | if (BIO_write(bp, buf, 2) != 2) |
39 | 0 | goto err; |
40 | 21.5M | n += 2; |
41 | 21.5M | } |
42 | 2.44k | } |
43 | 11.2k | return n; |
44 | 0 | err: |
45 | 0 | return -1; |
46 | 11.2k | } |
47 | | |
48 | | int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size) |
49 | 0 | { |
50 | 0 | int i, j, k, m, n, again, bufsize; |
51 | 0 | unsigned char *s = NULL, *sp; |
52 | 0 | unsigned char *bufp; |
53 | 0 | int num = 0, slen = 0, first = 1; |
54 | |
|
55 | 0 | bufsize = BIO_gets(bp, buf, size); |
56 | 0 | for (;;) { |
57 | 0 | if (bufsize < 1) { |
58 | 0 | if (first) |
59 | 0 | break; |
60 | 0 | else |
61 | 0 | goto err; |
62 | 0 | } |
63 | 0 | first = 0; |
64 | |
|
65 | 0 | i = bufsize; |
66 | 0 | if (buf[i - 1] == '\n') |
67 | 0 | buf[--i] = '\0'; |
68 | 0 | if (i == 0) |
69 | 0 | goto err; |
70 | 0 | if (buf[i - 1] == '\r') |
71 | 0 | buf[--i] = '\0'; |
72 | 0 | if (i == 0) |
73 | 0 | goto err; |
74 | 0 | again = (buf[i - 1] == '\\'); |
75 | |
|
76 | 0 | for (j = i - 1; j > 0; j--) { |
77 | 0 | if (!ossl_isxdigit(buf[j])) { |
78 | 0 | i = j; |
79 | 0 | break; |
80 | 0 | } |
81 | 0 | } |
82 | 0 | buf[i] = '\0'; |
83 | | /* |
84 | | * We have now cleared all the crap off the end of the line |
85 | | */ |
86 | 0 | if (i < 2) |
87 | 0 | goto err; |
88 | | |
89 | 0 | bufp = (unsigned char *)buf; |
90 | |
|
91 | 0 | k = 0; |
92 | 0 | i -= again; |
93 | 0 | if (i % 2 != 0) { |
94 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ODD_NUMBER_OF_CHARS); |
95 | 0 | OPENSSL_free(s); |
96 | 0 | return 0; |
97 | 0 | } |
98 | 0 | i /= 2; |
99 | 0 | if (num + i > slen) { |
100 | 0 | sp = OPENSSL_realloc(s, (unsigned int)num + i * 2); |
101 | 0 | if (sp == NULL) { |
102 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
103 | 0 | OPENSSL_free(s); |
104 | 0 | return 0; |
105 | 0 | } |
106 | 0 | s = sp; |
107 | 0 | slen = num + i * 2; |
108 | 0 | } |
109 | 0 | for (j = 0; j < i; j++, k += 2) { |
110 | 0 | for (n = 0; n < 2; n++) { |
111 | 0 | m = OPENSSL_hexchar2int(bufp[k + n]); |
112 | 0 | if (m < 0) { |
113 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NON_HEX_CHARACTERS); |
114 | 0 | OPENSSL_free(s); |
115 | 0 | return 0; |
116 | 0 | } |
117 | 0 | s[num + j] <<= 4; |
118 | 0 | s[num + j] |= m; |
119 | 0 | } |
120 | 0 | } |
121 | 0 | num += i; |
122 | 0 | if (again) |
123 | 0 | bufsize = BIO_gets(bp, buf, size); |
124 | 0 | else |
125 | 0 | break; |
126 | 0 | } |
127 | 0 | bs->length = num; |
128 | 0 | bs->data = s; |
129 | 0 | return 1; |
130 | | |
131 | 0 | err: |
132 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_SHORT_LINE); |
133 | 0 | OPENSSL_free(s); |
134 | 0 | return 0; |
135 | 0 | } |