summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_bytes.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/asn1/a_bytes.c373
1 files changed, 184 insertions, 189 deletions
diff --git a/src/lib/libcrypto/asn1/a_bytes.c b/src/lib/libcrypto/asn1/a_bytes.c
index 8431d89edf..30647c97b5 100644
--- a/src/lib/libcrypto/asn1/a_bytes.c
+++ b/src/lib/libcrypto/asn1/a_bytes.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,252 +63,247 @@
63static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c); 63static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c);
64/* type is a 'bitmap' of acceptable string types. 64/* type is a 'bitmap' of acceptable string types.
65 */ 65 */
66ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp, 66ASN1_STRING *
67 long length, int type) 67d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
68 { 68 long length, int type)
69 ASN1_STRING *ret=NULL; 69{
70 ASN1_STRING *ret = NULL;
70 const unsigned char *p; 71 const unsigned char *p;
71 unsigned char *s; 72 unsigned char *s;
72 long len; 73 long len;
73 int inf,tag,xclass; 74 int inf, tag, xclass;
74 int i=0; 75 int i = 0;
75 76
76 p= *pp; 77 p = *pp;
77 inf=ASN1_get_object(&p,&len,&tag,&xclass,length); 78 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
78 if (inf & 0x80) goto err; 79 if (inf & 0x80)
80 goto err;
79 81
80 if (tag >= 32) 82 if (tag >= 32) {
81 { 83 i = ASN1_R_TAG_VALUE_TOO_HIGH;
82 i=ASN1_R_TAG_VALUE_TOO_HIGH;
83 goto err; 84 goto err;
84 } 85 }
85 if (!(ASN1_tag2bit(tag) & type)) 86 if (!(ASN1_tag2bit(tag) & type)) {
86 { 87 i = ASN1_R_WRONG_TYPE;
87 i=ASN1_R_WRONG_TYPE;
88 goto err; 88 goto err;
89 } 89 }
90 90
91 /* If a bit-string, exit early */ 91 /* If a bit-string, exit early */
92 if (tag == V_ASN1_BIT_STRING) 92 if (tag == V_ASN1_BIT_STRING)
93 return(d2i_ASN1_BIT_STRING(a,pp,length)); 93 return (d2i_ASN1_BIT_STRING(a, pp, length));
94 94
95 if ((a == NULL) || ((*a) == NULL)) 95 if ((a == NULL) || ((*a) == NULL)) {
96 { 96 if ((ret = ASN1_STRING_new()) == NULL)
97 if ((ret=ASN1_STRING_new()) == NULL) return(NULL); 97 return (NULL);
98 } 98 } else
99 else 99 ret = (*a);
100 ret=(*a); 100
101 101 if (len != 0) {
102 if (len != 0) 102 s = (unsigned char *)malloc((int)len + 1);
103 { 103 if (s == NULL) {
104 s=(unsigned char *)malloc((int)len+1); 104 i = ERR_R_MALLOC_FAILURE;
105 if (s == NULL)
106 {
107 i=ERR_R_MALLOC_FAILURE;
108 goto err; 105 goto err;
109 }
110 memcpy(s,p,(int)len);
111 s[len]='\0';
112 p+=len;
113 } 106 }
114 else 107 memcpy(s, p, (int)len);
115 s=NULL; 108 s[len]='\0';
116 109 p += len;
117 if (ret->data != NULL) free(ret->data); 110 } else
118 ret->length=(int)len; 111 s = NULL;
119 ret->data=s; 112
120 ret->type=tag; 113 if (ret->data != NULL)
121 if (a != NULL) (*a)=ret; 114 free(ret->data);
122 *pp=p; 115 ret->length = (int)len;
123 return(ret); 116 ret->data = s;
117 ret->type = tag;
118 if (a != NULL)
119 (*a) = ret;
120 *pp = p;
121 return (ret);
122
124err: 123err:
125 ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,i); 124 ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES, i);
126 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 125 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
127 ASN1_STRING_free(ret); 126 ASN1_STRING_free(ret);
128 return(NULL); 127 return (NULL);
129 } 128}
130 129
131int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass) 130int
132 { 131i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
133 int ret,r,constructed; 132{
133 int ret, r, constructed;
134 unsigned char *p; 134 unsigned char *p;
135 135
136 if (a == NULL) return(0); 136 if (a == NULL)
137 return (0);
137 138
138 if (tag == V_ASN1_BIT_STRING) 139 if (tag == V_ASN1_BIT_STRING)
139 return(i2d_ASN1_BIT_STRING(a,pp)); 140 return (i2d_ASN1_BIT_STRING(a, pp));
140 141
141 ret=a->length; 142 ret = a->length;
142 r=ASN1_object_size(0,ret,tag); 143 r = ASN1_object_size(0, ret, tag);
143 if (pp == NULL) return(r); 144 if (pp == NULL)
144 p= *pp; 145 return (r);
146 p = *pp;
145 147
146 if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET)) 148 if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
147 constructed=1; 149 constructed = 1;
148 else 150 else
149 constructed=0; 151 constructed = 0;
150 ASN1_put_object(&p,constructed,ret,tag,xclass); 152 ASN1_put_object(&p, constructed, ret, tag, xclass);
151 memcpy(p,a->data,a->length); 153 memcpy(p, a->data, a->length);
152 p+=a->length; 154 p += a->length;
153 *pp= p; 155 *pp = p;
154 return(r); 156 return (r);
155 } 157}
156 158
157ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp, 159ASN1_STRING *
158 long length, int Ptag, int Pclass) 160d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
159 { 161 long length, int Ptag, int Pclass)
160 ASN1_STRING *ret=NULL; 162{
163 ASN1_STRING *ret = NULL;
161 const unsigned char *p; 164 const unsigned char *p;
162 unsigned char *s; 165 unsigned char *s;
163 long len; 166 long len;
164 int inf,tag,xclass; 167 int inf, tag, xclass;
165 int i=0; 168 int i = 0;
166 169
167 if ((a == NULL) || ((*a) == NULL)) 170 if ((a == NULL) || ((*a) == NULL)) {
168 { 171 if ((ret = ASN1_STRING_new()) == NULL)
169 if ((ret=ASN1_STRING_new()) == NULL) return(NULL); 172 return (NULL);
170 } 173 } else
171 else 174 ret = (*a);
172 ret=(*a);
173 175
174 p= *pp; 176 p= *pp;
175 inf=ASN1_get_object(&p,&len,&tag,&xclass,length); 177 inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
176 if (inf & 0x80) 178 if (inf & 0x80) {
177 { 179 i = ASN1_R_BAD_OBJECT_HEADER;
178 i=ASN1_R_BAD_OBJECT_HEADER;
179 goto err; 180 goto err;
180 } 181 }
181 182
182 if (tag != Ptag) 183 if (tag != Ptag) {
183 { 184 i = ASN1_R_WRONG_TAG;
184 i=ASN1_R_WRONG_TAG;
185 goto err; 185 goto err;
186 } 186 }
187 187
188 if (inf & V_ASN1_CONSTRUCTED) 188 if (inf & V_ASN1_CONSTRUCTED) {
189 {
190 ASN1_const_CTX c; 189 ASN1_const_CTX c;
191 190
192 c.pp=pp; 191 c.pp = pp;
193 c.p=p; 192 c.p = p;
194 c.inf=inf; 193 c.inf = inf;
195 c.slen=len; 194 c.slen = len;
196 c.tag=Ptag; 195 c.tag = Ptag;
197 c.xclass=Pclass; 196 c.xclass = Pclass;
198 c.max=(length == 0)?0:(p+length); 197 c.max = (length == 0) ? 0 : (p + length);
199 if (!asn1_collate_primitive(ret,&c)) 198 if (!asn1_collate_primitive(ret, &c))
200 goto err; 199 goto err;
201 else 200 else {
202 { 201 p = c.p;
203 p=c.p;
204 }
205 } 202 }
206 else 203 } else {
207 { 204 if (len != 0) {
208 if (len != 0) 205 if ((ret->length < len) || (ret->data == NULL)) {
209 { 206 if (ret->data != NULL)
210 if ((ret->length < len) || (ret->data == NULL)) 207 free(ret->data);
211 { 208 s = (unsigned char *)malloc((int)len + 1);
212 if (ret->data != NULL) free(ret->data); 209 if (s == NULL) {
213 s=(unsigned char *)malloc((int)len + 1); 210 i = ERR_R_MALLOC_FAILURE;
214 if (s == NULL)
215 {
216 i=ERR_R_MALLOC_FAILURE;
217 goto err; 211 goto err;
218 }
219 } 212 }
220 else 213 } else
221 s=ret->data; 214 s = ret->data;
222 memcpy(s,p,(int)len); 215 memcpy(s, p, (int)len);
223 s[len] = '\0'; 216 s[len] = '\0';
224 p+=len; 217 p += len;
225 } 218 } else {
226 else 219 s = NULL;
227 { 220 if (ret->data != NULL)
228 s=NULL; 221 free(ret->data);
229 if (ret->data != NULL) free(ret->data);
230 }
231
232 ret->length=(int)len;
233 ret->data=s;
234 ret->type=Ptag;
235 } 222 }
236 223
237 if (a != NULL) (*a)=ret; 224 ret->length = (int)len;
238 *pp=p; 225 ret->data = s;
239 return(ret); 226 ret->type = Ptag;
227 }
228
229 if (a != NULL)
230 (*a) = ret;
231 *pp = p;
232 return (ret);
233
240err: 234err:
241 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 235 if ((ret != NULL) && ((a == NULL) || (*a != ret)))
242 ASN1_STRING_free(ret); 236 ASN1_STRING_free(ret);
243 ASN1err(ASN1_F_D2I_ASN1_BYTES,i); 237 ASN1err(ASN1_F_D2I_ASN1_BYTES, i);
244 return(NULL); 238 return (NULL);
245 } 239}
246 240
247 241
248/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse 242/* We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse
249 * them into the one structure that is then returned */ 243 * them into the one structure that is then returned */
250/* There have been a few bug fixes for this function from 244/* There have been a few bug fixes for this function from
251 * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */ 245 * Paul Keogh <paul.keogh@sse.ie>, many thanks to him */
252static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c) 246static int
253 { 247asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c)
254 ASN1_STRING *os=NULL; 248{
249 ASN1_STRING *os = NULL;
255 BUF_MEM b; 250 BUF_MEM b;
256 int num; 251 int num;
257 252
258 b.length=0; 253 b.length = 0;
259 b.max=0; 254 b.max = 0;
260 b.data=NULL; 255 b.data = NULL;
261 256
262 if (a == NULL) 257 if (a == NULL) {
263 { 258 c->error = ERR_R_PASSED_NULL_PARAMETER;
264 c->error=ERR_R_PASSED_NULL_PARAMETER;
265 goto err; 259 goto err;
260 }
261
262 num = 0;
263 for (;;) {
264 if (c->inf & 1) {
265 c->eos = ASN1_const_check_infinite_end(&c->p,
266 (long)(c->max - c->p));
267 if (c->eos)
268 break;
269 } else {
270 if (c->slen <= 0)
271 break;
266 } 272 }
267 273
268 num=0; 274 c->q = c->p;
269 for (;;) 275 if (d2i_ASN1_bytes(&os, &c->p, c->max - c->p, c->tag,
270 { 276 c->xclass) == NULL) {
271 if (c->inf & 1) 277 c->error = ERR_R_ASN1_LIB;
272 {
273 c->eos=ASN1_const_check_infinite_end(&c->p,
274 (long)(c->max-c->p));
275 if (c->eos) break;
276 }
277 else
278 {
279 if (c->slen <= 0) break;
280 }
281
282 c->q=c->p;
283 if (d2i_ASN1_bytes(&os,&c->p,c->max-c->p,c->tag,c->xclass)
284 == NULL)
285 {
286 c->error=ERR_R_ASN1_LIB;
287 goto err; 278 goto err;
288 } 279 }
289 280
290 if (!BUF_MEM_grow_clean(&b,num+os->length)) 281 if (!BUF_MEM_grow_clean(&b, num + os->length)) {
291 { 282 c->error = ERR_R_BUF_LIB;
292 c->error=ERR_R_BUF_LIB;
293 goto err; 283 goto err;
294 }
295 memcpy(&(b.data[num]),os->data,os->length);
296 if (!(c->inf & 1))
297 c->slen-=(c->p-c->q);
298 num+=os->length;
299 } 284 }
285 memcpy(&(b.data[num]), os->data, os->length);
286 if (!(c->inf & 1))
287 c->slen -= (c->p - c->q);
288 num += os->length;
289 }
290
291 if (!asn1_const_Finish(c))
292 goto err;
300 293
301 if (!asn1_const_Finish(c)) goto err; 294 a->length = num;
295 if (a->data != NULL)
296 free(a->data);
297 a->data = (unsigned char *)b.data;
298 if (os != NULL)
299 ASN1_STRING_free(os);
300 return (1);
302 301
303 a->length=num;
304 if (a->data != NULL) free(a->data);
305 a->data=(unsigned char *)b.data;
306 if (os != NULL) ASN1_STRING_free(os);
307 return(1);
308err: 302err:
309 ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE,c->error); 303 ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE, c->error);
310 if (os != NULL) ASN1_STRING_free(os); 304 if (os != NULL)
311 if (b.data != NULL) free(b.data); 305 ASN1_STRING_free(os);
312 return(0); 306 if (b.data != NULL)
313 } 307 free(b.data);
314 308 return (0);
309}