|
1 /* crypto/asn1/f_enum.c */ |
|
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
|
3 * All rights reserved. |
|
4 * |
|
5 * This package is an SSL implementation written |
|
6 * by Eric Young (eay@cryptsoft.com). |
|
7 * The implementation was written so as to conform with Netscapes SSL. |
|
8 * |
|
9 * This library is free for commercial and non-commercial use as long as |
|
10 * the following conditions are aheared to. The following conditions |
|
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 |
|
13 * included with this distribution is covered by the same copyright terms |
|
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
|
15 * |
|
16 * Copyright remains Eric Young's, and as such any Copyright notices in |
|
17 * the code are not to be removed. |
|
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. |
|
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. |
|
22 * |
|
23 * Redistribution and use in source and binary forms, with or without |
|
24 * modification, are permitted provided that the following conditions |
|
25 * are met: |
|
26 * 1. Redistributions of source code must retain the copyright |
|
27 * notice, this list of conditions and the following disclaimer. |
|
28 * 2. Redistributions in binary form must reproduce the above copyright |
|
29 * notice, this list of conditions and the following disclaimer in the |
|
30 * documentation and/or other materials provided with the distribution. |
|
31 * 3. All advertising materials mentioning features or use of this software |
|
32 * must display the following acknowledgement: |
|
33 * "This product includes cryptographic software written by |
|
34 * Eric Young (eay@cryptsoft.com)" |
|
35 * The word 'cryptographic' can be left out if the rouines from the library |
|
36 * being used are not cryptographic related :-). |
|
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: |
|
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
|
40 * |
|
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
|
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
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 |
|
51 * SUCH DAMAGE. |
|
52 * |
|
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 |
|
55 * copied and put under another distribution licence |
|
56 * [including the GNU Public Licence.] |
|
57 */ |
|
58 /* |
|
59 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
60 */ |
|
61 #include <stdio.h> |
|
62 #include "cryptlib.h" |
|
63 #include <openssl/buffer.h> |
|
64 #include <openssl/asn1.h> |
|
65 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) |
|
66 #include "libcrypto_wsd_macros.h" |
|
67 #include "libcrypto_wsd.h" |
|
68 #endif |
|
69 |
|
70 |
|
71 /* Based on a_int.c: equivalent ENUMERATED functions */ |
|
72 |
|
73 EXPORT_C int i2a_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *a) |
|
74 { |
|
75 int i,n=0; |
|
76 #ifndef EMULATOR |
|
77 static const char *h="0123456789ABCDEF"; |
|
78 #else |
|
79 static const char *const h="0123456789ABCDEF"; |
|
80 #endif |
|
81 char buf[2]; |
|
82 |
|
83 if (a == NULL) return(0); |
|
84 |
|
85 if (a->length == 0) |
|
86 { |
|
87 if (BIO_write(bp,"00",2) != 2) goto err; |
|
88 n=2; |
|
89 } |
|
90 else |
|
91 { |
|
92 for (i=0; i<a->length; i++) |
|
93 { |
|
94 if ((i != 0) && (i%35 == 0)) |
|
95 { |
|
96 if (BIO_write(bp,"\\\n",2) != 2) goto err; |
|
97 n+=2; |
|
98 } |
|
99 buf[0]=h[((unsigned char)a->data[i]>>4)&0x0f]; |
|
100 buf[1]=h[((unsigned char)a->data[i] )&0x0f]; |
|
101 if (BIO_write(bp,buf,2) != 2) goto err; |
|
102 n+=2; |
|
103 } |
|
104 } |
|
105 return(n); |
|
106 err: |
|
107 return(-1); |
|
108 } |
|
109 |
|
110 EXPORT_C int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size) |
|
111 { |
|
112 int ret=0; |
|
113 int i,j,k,m,n,again,bufsize; |
|
114 unsigned char *s=NULL,*sp; |
|
115 unsigned char *bufp; |
|
116 int num=0,slen=0,first=1; |
|
117 |
|
118 bs->type=V_ASN1_ENUMERATED; |
|
119 |
|
120 bufsize=BIO_gets(bp,buf,size); |
|
121 for (;;) |
|
122 { |
|
123 if (bufsize < 1) goto err_sl; |
|
124 i=bufsize; |
|
125 if (buf[i-1] == '\n') buf[--i]='\0'; |
|
126 if (i == 0) goto err_sl; |
|
127 if (buf[i-1] == '\r') buf[--i]='\0'; |
|
128 if (i == 0) goto err_sl; |
|
129 again=(buf[i-1] == '\\'); |
|
130 |
|
131 for (j=0; j<i; j++) |
|
132 { |
|
133 if (!( ((buf[j] >= '0') && (buf[j] <= '9')) || |
|
134 ((buf[j] >= 'a') && (buf[j] <= 'f')) || |
|
135 ((buf[j] >= 'A') && (buf[j] <= 'F')))) |
|
136 { |
|
137 i=j; |
|
138 break; |
|
139 } |
|
140 } |
|
141 buf[i]='\0'; |
|
142 /* We have now cleared all the crap off the end of the |
|
143 * line */ |
|
144 if (i < 2) goto err_sl; |
|
145 |
|
146 bufp=(unsigned char *)buf; |
|
147 if (first) |
|
148 { |
|
149 first=0; |
|
150 if ((bufp[0] == '0') && (buf[1] == '0')) |
|
151 { |
|
152 bufp+=2; |
|
153 i-=2; |
|
154 } |
|
155 } |
|
156 k=0; |
|
157 i-=again; |
|
158 if (i%2 != 0) |
|
159 { |
|
160 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_ODD_NUMBER_OF_CHARS); |
|
161 goto err; |
|
162 } |
|
163 i/=2; |
|
164 if (num+i > slen) |
|
165 { |
|
166 if (s == NULL) |
|
167 sp=(unsigned char *)OPENSSL_malloc( |
|
168 (unsigned int)num+i*2); |
|
169 else |
|
170 sp=(unsigned char *)OPENSSL_realloc(s, |
|
171 (unsigned int)num+i*2); |
|
172 if (sp == NULL) |
|
173 { |
|
174 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE); |
|
175 if (s != NULL) OPENSSL_free(s); |
|
176 goto err; |
|
177 } |
|
178 s=sp; |
|
179 slen=num+i*2; |
|
180 } |
|
181 for (j=0; j<i; j++,k+=2) |
|
182 { |
|
183 for (n=0; n<2; n++) |
|
184 { |
|
185 m=bufp[k+n]; |
|
186 if ((m >= '0') && (m <= '9')) |
|
187 m-='0'; |
|
188 else if ((m >= 'a') && (m <= 'f')) |
|
189 m=m-'a'+10; |
|
190 else if ((m >= 'A') && (m <= 'F')) |
|
191 m=m-'A'+10; |
|
192 else |
|
193 { |
|
194 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_NON_HEX_CHARACTERS); |
|
195 goto err; |
|
196 } |
|
197 s[num+j]<<=4; |
|
198 s[num+j]|=m; |
|
199 } |
|
200 } |
|
201 num+=i; |
|
202 if (again) |
|
203 bufsize=BIO_gets(bp,buf,size); |
|
204 else |
|
205 break; |
|
206 } |
|
207 bs->length=num; |
|
208 bs->data=s; |
|
209 ret=1; |
|
210 err: |
|
211 if (0) |
|
212 { |
|
213 err_sl: |
|
214 ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,ASN1_R_SHORT_LINE); |
|
215 } |
|
216 return(ret); |
|
217 } |
|
218 |