/src/openssl/crypto/asn1/f_string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "internal/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 | 0 | { |
18 | 0 | int i, n = 0; |
19 | 0 | static const char *h = "0123456789ABCDEF"; |
20 | 0 | char buf[2]; |
21 | 0 |
|
22 | 0 | if (a == NULL) |
23 | 0 | return 0; |
24 | 0 | |
25 | 0 | if (a->length == 0) { |
26 | 0 | if (BIO_write(bp, "0", 1) != 1) |
27 | 0 | goto err; |
28 | 0 | n = 1; |
29 | 0 | } else { |
30 | 0 | for (i = 0; i < a->length; i++) { |
31 | 0 | if ((i != 0) && (i % 35 == 0)) { |
32 | 0 | if (BIO_write(bp, "\\\n", 2) != 2) |
33 | 0 | goto err; |
34 | 0 | n += 2; |
35 | 0 | } |
36 | 0 | buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; |
37 | 0 | buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; |
38 | 0 | if (BIO_write(bp, buf, 2) != 2) |
39 | 0 | goto err; |
40 | 0 | n += 2; |
41 | 0 | } |
42 | 0 | } |
43 | 0 | return n; |
44 | 0 | err: |
45 | 0 | return -1; |
46 | 0 | } |
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 | 0 |
|
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 | 0 |
|
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 | 0 |
|
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 | 0 | /* |
84 | 0 | * We have now cleared all the crap off the end of the line |
85 | 0 | */ |
86 | 0 | if (i < 2) |
87 | 0 | goto err; |
88 | 0 | |
89 | 0 | bufp = (unsigned char *)buf; |
90 | 0 |
|
91 | 0 | k = 0; |
92 | 0 | i -= again; |
93 | 0 | if (i % 2 != 0) { |
94 | 0 | ASN1err(ASN1_F_A2I_ASN1_STRING, 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 | ASN1err(ASN1_F_A2I_ASN1_STRING, 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 | ASN1err(ASN1_F_A2I_ASN1_STRING, |
114 | 0 | ASN1_R_NON_HEX_CHARACTERS); |
115 | 0 | OPENSSL_free(s); |
116 | 0 | return 0; |
117 | 0 | } |
118 | 0 | s[num + j] <<= 4; |
119 | 0 | s[num + j] |= m; |
120 | 0 | } |
121 | 0 | } |
122 | 0 | num += i; |
123 | 0 | if (again) |
124 | 0 | bufsize = BIO_gets(bp, buf, size); |
125 | 0 | else |
126 | 0 | break; |
127 | 0 | } |
128 | 0 | bs->length = num; |
129 | 0 | bs->data = s; |
130 | 0 | return 1; |
131 | 0 | |
132 | 0 | err: |
133 | 0 | ASN1err(ASN1_F_A2I_ASN1_STRING, ASN1_R_SHORT_LINE); |
134 | 0 | OPENSSL_free(s); |
135 | 0 | return 0; |
136 | 0 | } |