OLD | NEW |
1 /* crypto/asn1/a_object.c */ | 1 /* crypto/asn1/a_object.c */ |
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
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 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 if (num <= 0) break; | 132 if (num <= 0) break; |
133 num--; | 133 num--; |
134 c= *(p++); | 134 c= *(p++); |
135 if ((c == ' ') || (c == '.')) | 135 if ((c == ' ') || (c == '.')) |
136 break; | 136 break; |
137 if ((c < '0') || (c > '9')) | 137 if ((c < '0') || (c > '9')) |
138 { | 138 { |
139 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DI
GIT); | 139 ASN1err(ASN1_F_A2D_ASN1_OBJECT,ASN1_R_INVALID_DI
GIT); |
140 goto err; | 140 goto err; |
141 } | 141 } |
142 » » » if (!use_bn && l > (ULONG_MAX / 10L)) | 142 » » » if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) |
143 { | 143 { |
144 use_bn = 1; | 144 use_bn = 1; |
145 if (!bl) | 145 if (!bl) |
146 bl = BN_new(); | 146 bl = BN_new(); |
147 if (!bl || !BN_set_word(bl, l)) | 147 if (!bl || !BN_set_word(bl, l)) |
148 goto err; | 148 goto err; |
149 } | 149 } |
150 if (use_bn) | 150 if (use_bn) |
151 { | 151 { |
152 if (!BN_mul_word(bl, 10L) | 152 if (!BN_mul_word(bl, 10L) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 if (tag != V_ASN1_OBJECT) | 274 if (tag != V_ASN1_OBJECT) |
275 { | 275 { |
276 i=ASN1_R_EXPECTING_AN_OBJECT; | 276 i=ASN1_R_EXPECTING_AN_OBJECT; |
277 goto err; | 277 goto err; |
278 } | 278 } |
279 ret = c2i_ASN1_OBJECT(a, &p, len); | 279 ret = c2i_ASN1_OBJECT(a, &p, len); |
280 if(ret) *pp = p; | 280 if(ret) *pp = p; |
281 return ret; | 281 return ret; |
282 err: | 282 err: |
283 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i); | 283 ASN1err(ASN1_F_D2I_ASN1_OBJECT,i); |
284 if ((ret != NULL) && ((a == NULL) || (*a != ret))) | |
285 ASN1_OBJECT_free(ret); | |
286 return(NULL); | 284 return(NULL); |
287 } | 285 } |
288 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, | 286 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, |
289 long len) | 287 long len) |
290 { | 288 { |
291 ASN1_OBJECT *ret=NULL; | 289 ASN1_OBJECT *ret=NULL; |
292 const unsigned char *p; | 290 const unsigned char *p; |
| 291 unsigned char *data; |
293 int i; | 292 int i; |
294 /* Sanity check OID encoding: can't have leading 0x80 in | 293 /* Sanity check OID encoding: can't have leading 0x80 in |
295 * subidentifiers, see: X.690 8.19.2 | 294 * subidentifiers, see: X.690 8.19.2 |
296 */ | 295 */ |
297 » for (i = 0, p = *pp + 1; i < len - 1; i++, p++) | 296 » for (i = 0, p = *pp; i < len; i++, p++) |
298 { | 297 { |
299 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) | 298 if (*p == 0x80 && (!i || !(p[-1] & 0x80))) |
300 { | 299 { |
301 ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENC
ODING); | 300 ASN1err(ASN1_F_C2I_ASN1_OBJECT,ASN1_R_INVALID_OBJECT_ENC
ODING); |
302 return NULL; | 301 return NULL; |
303 } | 302 } |
304 } | 303 } |
305 | 304 |
306 /* only the ASN1_OBJECTs from the 'table' will have values | 305 /* only the ASN1_OBJECTs from the 'table' will have values |
307 * for ->sn or ->ln */ | 306 * for ->sn or ->ln */ |
308 if ((a == NULL) || ((*a) == NULL) || | 307 if ((a == NULL) || ((*a) == NULL) || |
309 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) | 308 !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) |
310 { | 309 { |
311 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL); | 310 if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL); |
312 } | 311 } |
313 else ret=(*a); | 312 else ret=(*a); |
314 | 313 |
315 p= *pp; | 314 p= *pp; |
316 » if ((ret->data == NULL) || (ret->length < len)) | 315 » /* detach data from object */ |
| 316 » data = (unsigned char *)ret->data; |
| 317 » ret->data = NULL; |
| 318 » /* once detached we can change it */ |
| 319 » if ((data == NULL) || (ret->length < len)) |
317 { | 320 { |
318 » » if (ret->data != NULL) OPENSSL_free(ret->data); | 321 » » ret->length=0; |
319 » » ret->data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1); | 322 » » if (data != NULL) OPENSSL_free(data); |
| 323 » » data=(unsigned char *)OPENSSL_malloc(len ? (int)len : 1); |
| 324 » » if (data == NULL) |
| 325 » » » { i=ERR_R_MALLOC_FAILURE; goto err; } |
320 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA; | 326 ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
321 if (ret->data == NULL) | |
322 { i=ERR_R_MALLOC_FAILURE; goto err; } | |
323 } | 327 } |
324 » memcpy(ret->data,p,(int)len); | 328 » memcpy(data,p,(int)len); |
| 329 » /* reattach data to object, after which it remains const */ |
| 330 » ret->data =data; |
325 ret->length=(int)len; | 331 ret->length=(int)len; |
326 ret->sn=NULL; | 332 ret->sn=NULL; |
327 ret->ln=NULL; | 333 ret->ln=NULL; |
328 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */ | 334 /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */ |
329 p+=len; | 335 p+=len; |
330 | 336 |
331 if (a != NULL) (*a)=ret; | 337 if (a != NULL) (*a)=ret; |
332 *pp=p; | 338 *pp=p; |
333 return(ret); | 339 return(ret); |
334 err: | 340 err: |
(...skipping 28 matching lines...) Expand all Loading... |
363 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) | 369 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) |
364 { | 370 { |
365 #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. D
oing this on a "real" compile will cause memory leaks */ | 371 #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. D
oing this on a "real" compile will cause memory leaks */ |
366 if (a->sn != NULL) OPENSSL_free((void *)a->sn); | 372 if (a->sn != NULL) OPENSSL_free((void *)a->sn); |
367 if (a->ln != NULL) OPENSSL_free((void *)a->ln); | 373 if (a->ln != NULL) OPENSSL_free((void *)a->ln); |
368 #endif | 374 #endif |
369 a->sn=a->ln=NULL; | 375 a->sn=a->ln=NULL; |
370 } | 376 } |
371 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) | 377 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) |
372 { | 378 { |
373 » » if (a->data != NULL) OPENSSL_free(a->data); | 379 » » if (a->data != NULL) OPENSSL_free((void *)a->data); |
374 a->data=NULL; | 380 a->data=NULL; |
375 a->length=0; | 381 a->length=0; |
376 } | 382 } |
377 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) | 383 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) |
378 OPENSSL_free(a); | 384 OPENSSL_free(a); |
379 } | 385 } |
380 | 386 |
381 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len, | 387 ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len, |
382 const char *sn, const char *ln) | 388 const char *sn, const char *ln) |
383 { | 389 { |
384 ASN1_OBJECT o; | 390 ASN1_OBJECT o; |
385 | 391 |
386 o.sn=sn; | 392 o.sn=sn; |
387 o.ln=ln; | 393 o.ln=ln; |
388 o.data=data; | 394 o.data=data; |
389 o.nid=nid; | 395 o.nid=nid; |
390 o.length=len; | 396 o.length=len; |
391 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS| | 397 o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS| |
392 ASN1_OBJECT_FLAG_DYNAMIC_DATA; | 398 ASN1_OBJECT_FLAG_DYNAMIC_DATA; |
393 return(OBJ_dup(&o)); | 399 return(OBJ_dup(&o)); |
394 } | 400 } |
395 | 401 |
396 IMPLEMENT_STACK_OF(ASN1_OBJECT) | 402 IMPLEMENT_STACK_OF(ASN1_OBJECT) |
397 IMPLEMENT_ASN1_SET_OF(ASN1_OBJECT) | 403 IMPLEMENT_ASN1_SET_OF(ASN1_OBJECT) |
OLD | NEW |