summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/f_enum.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/f_enum.c')
-rw-r--r--src/lib/libcrypto/asn1/f_enum.c173
1 files changed, 95 insertions, 78 deletions
diff --git a/src/lib/libcrypto/asn1/f_enum.c b/src/lib/libcrypto/asn1/f_enum.c
index 4ccfbddf2a..e8736e5b72 100644
--- a/src/lib/libcrypto/asn1/f_enum.c
+++ b/src/lib/libcrypto/asn1/f_enum.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -63,128 +63,145 @@
63 63
64/* Based on a_int.c: equivalent ENUMERATED functions */ 64/* Based on a_int.c: equivalent ENUMERATED functions */
65 65
66int i2a_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *a) 66int
67i2a_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *a)
67{ 68{
68 int i,n=0; 69 int i, n = 0;
69 static const char *h="0123456789ABCDEF"; 70 static const char *h = "0123456789ABCDEF";
70 char buf[2]; 71 char buf[2];
71 72
72 if (a == NULL) return(0); 73 if (a == NULL)
74 return (0);
73 75
74 if (a->length == 0) { 76 if (a->length == 0) {
75 if (BIO_write(bp,"00",2) != 2) goto err; 77 if (BIO_write(bp, "00", 2) != 2)
76 n=2; 78 goto err;
79 n = 2;
77 } else { 80 } else {
78 for (i=0; i<a->length; i++) { 81 for (i = 0; i < a->length; i++) {
79 if ((i != 0) && (i%35 == 0)) { 82 if ((i != 0) && (i % 35 == 0)) {
80 if (BIO_write(bp,"\\\n",2) != 2) goto err; 83 if (BIO_write(bp, "\\\n", 2) != 2)
81 n+=2; 84 goto err;
85 n += 2;
82 } 86 }
83 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f]; 87 buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
84 buf[1]=h[((unsigned char)a->data[i] )&0x0f]; 88 buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
85 if (BIO_write(bp,buf,2) != 2) goto err; 89 if (BIO_write(bp, buf, 2) != 2)
86 n+=2; 90 goto err;
91 n += 2;
87 } 92 }
88 } 93 }
89 return(n); 94 return (n);
95
90err: 96err:
91 return(-1); 97 return (-1);
92} 98}
93 99
94int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size) 100int
101a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
95{ 102{
96 int ret=0; 103 int ret = 0;
97 int i,j,k,m,n,again,bufsize; 104 int i, j,k, m,n, again, bufsize;
98 unsigned char *s=NULL,*sp; 105 unsigned char *s = NULL, *sp;
99 unsigned char *bufp; 106 unsigned char *bufp;
100 int num=0,slen=0,first=1; 107 int num = 0, slen = 0, first = 1;
101 108
102 bs->type=V_ASN1_ENUMERATED; 109 bs->type = V_ASN1_ENUMERATED;
103 110
104 bufsize=BIO_gets(bp,buf,size); 111 bufsize = BIO_gets(bp, buf, size);
105 for (;;) { 112 for (;;) {
106 if (bufsize < 1) goto err_sl; 113 if (bufsize < 1)
107 i=bufsize; 114 goto err_sl;
108 if (buf[i-1] == '\n') buf[--i]='\0'; 115 i = bufsize;
109 if (i == 0) goto err_sl; 116 if (buf[i-1] == '\n')
110 if (buf[i-1] == '\r') buf[--i]='\0'; 117 buf[--i] = '\0';
111 if (i == 0) goto err_sl; 118 if (i == 0)
112 again=(buf[i-1] == '\\'); 119 goto err_sl;
120 if (buf[i-1] == '\r')
121 buf[--i] = '\0';
122 if (i == 0)
123 goto err_sl;
124 again = (buf[i - 1] == '\\');
113 125
114 for (j=0; j<i; j++) { 126 for (j = 0; j < i; j++) {
115 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) || 127 if (!(((buf[j] >= '0') && (buf[j] <= '9')) ||
116 ((buf[j] >= 'a') && (buf[j] <= 'f')) || 128 ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
117 ((buf[j] >= 'A') && (buf[j] <= 'F')))) { 129 ((buf[j] >= 'A') && (buf[j] <= 'F')))) {
118 i=j; 130 i = j;
119 break; 131 break;
120 } 132 }
121 } 133 }
122 buf[i]='\0'; 134 buf[i] = '\0';
123 /* We have now cleared all the crap off the end of the 135 /* We have now cleared all the crap off the end of the
124 * line */ 136 * line */
125 if (i < 2) goto err_sl; 137 if (i < 2)
138 goto err_sl;
126 139
127 bufp=(unsigned char *)buf; 140 bufp = (unsigned char *)buf;
128 if (first) { 141 if (first) {
129 first=0; 142 first = 0;
130 if ((bufp[0] == '0') && (buf[1] == '0')) { 143 if ((bufp[0] == '0') && (buf[1] == '0')) {
131 bufp+=2; 144 bufp += 2;
132 i-=2; 145 i -= 2;
133 } 146 }
134 } 147 }
135 k=0; 148 k = 0;
136 i-=again; 149 i -= again;
137 if (i%2 != 0) { 150 if (i % 2 != 0) {
138 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_ODD_NUMBER_OF_CHARS); 151 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,
152 ASN1_R_ODD_NUMBER_OF_CHARS);
139 goto err; 153 goto err;
140 } 154 }
141 i/=2; 155 i /= 2;
142 if (num+i > slen) { 156 if (num + i > slen) {
143 if (s == NULL) 157 if (s == NULL)
144 sp=(unsigned char *)malloc( 158 sp = (unsigned char *)malloc(
145 (unsigned int)num+i*2); 159 (unsigned int)num + i * 2);
146 else 160 else
147 sp=(unsigned char *)realloc(s, 161 sp = (unsigned char *)realloc(s,
148 (unsigned int)num+i*2); 162 (unsigned int)num + i * 2);
149 if (sp == NULL) { 163 if (sp == NULL) {
150 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE); 164 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,
151 if (s != NULL) free(s); 165 ERR_R_MALLOC_FAILURE);
166 if (s != NULL)
167 free(s);
152 goto err; 168 goto err;
153 } 169 }
154 s=sp; 170 s = sp;
155 slen=num+i*2; 171 slen = num + i * 2;
156 } 172 }
157 for (j=0; j<i; j++,k+=2) { 173 for (j = 0; j < i; j++, k += 2) {
158 for (n=0; n<2; n++) { 174 for (n = 0; n < 2; n++) {
159 m=bufp[k+n]; 175 m = bufp[k + n];
160 if ((m >= '0') && (m <= '9')) 176 if ((m >= '0') && (m <= '9'))
161 m-='0'; 177 m -= '0';
162 else if ((m >= 'a') && (m <= 'f')) 178 else if ((m >= 'a') && (m <= 'f'))
163 m=m-'a'+10; 179 m = m - 'a' + 10;
164 else if ((m >= 'A') && (m <= 'F')) 180 else if ((m >= 'A') && (m <= 'F'))
165 m=m-'A'+10; 181 m = m - 'A' + 10;
166 else { 182 else {
167 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_NON_HEX_CHARACTERS); 183 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,
184 ASN1_R_NON_HEX_CHARACTERS);
168 goto err; 185 goto err;
169 } 186 }
170 s[num+j]<<=4; 187 s[num + j] <<= 4;
171 s[num+j]|=m; 188 s[num + j] |= m;
172 } 189 }
173 } 190 }
174 num+=i; 191 num += i;
175 if (again) 192 if (again)
176 bufsize=BIO_gets(bp,buf,size); 193 bufsize = BIO_gets(bp, buf, size);
177 else 194 else
178 break; 195 break;
179 } 196 }
180 bs->length=num; 197 bs->length = num;
181 bs->data=s; 198 bs->data = s;
182 ret=1; 199 ret = 1;
200
183err: 201err:
184 if (0) { 202 if (0) {
185err_sl: 203err_sl:
186 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_SHORT_LINE); 204 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED, ASN1_R_SHORT_LINE);
187 } 205 }
188 return(ret); 206 return (ret);
189} 207}
190