diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/x_name.c')
-rw-r--r-- | src/lib/libcrypto/asn1/x_name.c | 98 |
1 files changed, 40 insertions, 58 deletions
diff --git a/src/lib/libcrypto/asn1/x_name.c b/src/lib/libcrypto/asn1/x_name.c index dfd4363aa0..d10ac5acd1 100644 --- a/src/lib/libcrypto/asn1/x_name.c +++ b/src/lib/libcrypto/asn1/x_name.c | |||
@@ -145,8 +145,7 @@ static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) | |||
145 | 145 | ||
146 | memerr: | 146 | memerr: |
147 | ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE); | 147 | ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE); |
148 | if (ret) | 148 | if (ret) { |
149 | { | ||
150 | if (ret->entries) | 149 | if (ret->entries) |
151 | sk_X509_NAME_ENTRY_free(ret->entries); | 150 | sk_X509_NAME_ENTRY_free(ret->entries); |
152 | free(ret); | 151 | free(ret); |
@@ -242,14 +241,14 @@ static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_IT | |||
242 | } | 241 | } |
243 | 242 | ||
244 | static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) | 243 | static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) |
245 | { | 244 | { |
246 | sk_X509_NAME_ENTRY_free(ne); | 245 | sk_X509_NAME_ENTRY_free(ne); |
247 | } | 246 | } |
248 | 247 | ||
249 | static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) | 248 | static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) |
250 | { | 249 | { |
251 | sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); | 250 | sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); |
252 | } | 251 | } |
253 | 252 | ||
254 | static int x509_name_encode(X509_NAME *a) | 253 | static int x509_name_encode(X509_NAME *a) |
255 | { | 254 | { |
@@ -295,12 +294,12 @@ static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, | |||
295 | int indent, | 294 | int indent, |
296 | const char *fname, | 295 | const char *fname, |
297 | const ASN1_PCTX *pctx) | 296 | const ASN1_PCTX *pctx) |
298 | { | 297 | { |
299 | if (X509_NAME_print_ex(out, (X509_NAME *)*pval, | 298 | if (X509_NAME_print_ex(out, (X509_NAME *)*pval, |
300 | indent, pctx->nm_flags) <= 0) | 299 | indent, pctx->nm_flags) <= 0) |
301 | return 0; | 300 | return 0; |
302 | return 2; | 301 | return 2; |
303 | } | 302 | } |
304 | 303 | ||
305 | /* This function generates the canonical encoding of the Name structure. | 304 | /* This function generates the canonical encoding of the Name structure. |
306 | * In it all strings are converted to UTF8, leading, trailing and | 305 | * In it all strings are converted to UTF8, leading, trailing and |
@@ -316,39 +315,35 @@ static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, | |||
316 | */ | 315 | */ |
317 | 316 | ||
318 | static int x509_name_canon(X509_NAME *a) | 317 | static int x509_name_canon(X509_NAME *a) |
319 | { | 318 | { |
320 | unsigned char *p; | 319 | unsigned char *p; |
321 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL; | 320 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL; |
322 | STACK_OF(X509_NAME_ENTRY) *entries = NULL; | 321 | STACK_OF(X509_NAME_ENTRY) *entries = NULL; |
323 | X509_NAME_ENTRY *entry, *tmpentry = NULL; | 322 | X509_NAME_ENTRY *entry, *tmpentry = NULL; |
324 | int i, set = -1, ret = 0; | 323 | int i, set = -1, ret = 0; |
325 | 324 | ||
326 | if (a->canon_enc) | 325 | if (a->canon_enc) { |
327 | { | ||
328 | free(a->canon_enc); | 326 | free(a->canon_enc); |
329 | a->canon_enc = NULL; | 327 | a->canon_enc = NULL; |
330 | } | 328 | } |
331 | /* Special case: empty X509_NAME => null encoding */ | 329 | /* Special case: empty X509_NAME => null encoding */ |
332 | if (sk_X509_NAME_ENTRY_num(a->entries) == 0) | 330 | if (sk_X509_NAME_ENTRY_num(a->entries) == 0) { |
333 | { | ||
334 | a->canon_enclen = 0; | 331 | a->canon_enclen = 0; |
335 | return 1; | 332 | return 1; |
336 | } | 333 | } |
337 | intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); | 334 | intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); |
338 | if(!intname) | 335 | if(!intname) |
339 | goto err; | 336 | goto err; |
340 | for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) | 337 | for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
341 | { | ||
342 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); | 338 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); |
343 | if(entry->set != set) | 339 | if(entry->set != set) { |
344 | { | ||
345 | entries = sk_X509_NAME_ENTRY_new_null(); | 340 | entries = sk_X509_NAME_ENTRY_new_null(); |
346 | if(!entries) | 341 | if(!entries) |
347 | goto err; | 342 | goto err; |
348 | if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) | 343 | if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) |
349 | goto err; | 344 | goto err; |
350 | set = entry->set; | 345 | set = entry->set; |
351 | } | 346 | } |
352 | tmpentry = X509_NAME_ENTRY_new(); | 347 | tmpentry = X509_NAME_ENTRY_new(); |
353 | tmpentry->object = OBJ_dup(entry->object); | 348 | tmpentry->object = OBJ_dup(entry->object); |
354 | if (!asn1_string_canon(tmpentry->value, entry->value)) | 349 | if (!asn1_string_canon(tmpentry->value, entry->value)) |
@@ -356,7 +351,7 @@ static int x509_name_canon(X509_NAME *a) | |||
356 | if(!sk_X509_NAME_ENTRY_push(entries, tmpentry)) | 351 | if(!sk_X509_NAME_ENTRY_push(entries, tmpentry)) |
357 | goto err; | 352 | goto err; |
358 | tmpentry = NULL; | 353 | tmpentry = NULL; |
359 | } | 354 | } |
360 | 355 | ||
361 | /* Finally generate encoding */ | 356 | /* Finally generate encoding */ |
362 | 357 | ||
@@ -381,7 +376,7 @@ static int x509_name_canon(X509_NAME *a) | |||
381 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, | 376 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, |
382 | local_sk_X509_NAME_ENTRY_pop_free); | 377 | local_sk_X509_NAME_ENTRY_pop_free); |
383 | return ret; | 378 | return ret; |
384 | } | 379 | } |
385 | 380 | ||
386 | /* Bitmap of all the types of string that will be canonicalized. */ | 381 | /* Bitmap of all the types of string that will be canonicalized. */ |
387 | 382 | ||
@@ -392,17 +387,16 @@ static int x509_name_canon(X509_NAME *a) | |||
392 | 387 | ||
393 | 388 | ||
394 | static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | 389 | static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) |
395 | { | 390 | { |
396 | unsigned char *to, *from; | 391 | unsigned char *to, *from; |
397 | int len, i; | 392 | int len, i; |
398 | 393 | ||
399 | /* If type not in bitmask just copy string across */ | 394 | /* If type not in bitmask just copy string across */ |
400 | if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) | 395 | if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) { |
401 | { | ||
402 | if (!ASN1_STRING_copy(out, in)) | 396 | if (!ASN1_STRING_copy(out, in)) |
403 | return 0; | 397 | return 0; |
404 | return 1; | 398 | return 1; |
405 | } | 399 | } |
406 | 400 | ||
407 | out->type = V_ASN1_UTF8STRING; | 401 | out->type = V_ASN1_UTF8STRING; |
408 | out->length = ASN1_STRING_to_UTF8(&out->data, in); | 402 | out->length = ASN1_STRING_to_UTF8(&out->data, in); |
@@ -421,99 +415,87 @@ static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | |||
421 | */ | 415 | */ |
422 | 416 | ||
423 | /* Ignore leading spaces */ | 417 | /* Ignore leading spaces */ |
424 | while((len > 0) && !(*from & 0x80) && isspace(*from)) | 418 | while((len > 0) && !(*from & 0x80) && isspace(*from)) { |
425 | { | ||
426 | from++; | 419 | from++; |
427 | len--; | 420 | len--; |
428 | } | 421 | } |
429 | 422 | ||
430 | to = from + len - 1; | 423 | to = from + len - 1; |
431 | 424 | ||
432 | /* Ignore trailing spaces */ | 425 | /* Ignore trailing spaces */ |
433 | while ((len > 0) && !(*to & 0x80) && isspace(*to)) | 426 | while ((len > 0) && !(*to & 0x80) && isspace(*to)) { |
434 | { | ||
435 | to--; | 427 | to--; |
436 | len--; | 428 | len--; |
437 | } | 429 | } |
438 | 430 | ||
439 | to = out->data; | 431 | to = out->data; |
440 | 432 | ||
441 | i = 0; | 433 | i = 0; |
442 | while(i < len) | 434 | while(i < len) { |
443 | { | ||
444 | /* If MSB set just copy across */ | 435 | /* If MSB set just copy across */ |
445 | if (*from & 0x80) | 436 | if (*from & 0x80) { |
446 | { | ||
447 | *to++ = *from++; | 437 | *to++ = *from++; |
448 | i++; | 438 | i++; |
449 | } | 439 | } |
450 | /* Collapse multiple spaces */ | 440 | /* Collapse multiple spaces */ |
451 | else if (isspace(*from)) | 441 | else if (isspace(*from)) { |
452 | { | ||
453 | /* Copy one space across */ | 442 | /* Copy one space across */ |
454 | *to++ = ' '; | 443 | *to++ = ' '; |
455 | /* Ignore subsequent spaces. Note: don't need to | 444 | /* Ignore subsequent spaces. Note: don't need to |
456 | * check len here because we know the last | 445 | * check len here because we know the last |
457 | * character is a non-space so we can't overflow. | 446 | * character is a non-space so we can't overflow. |
458 | */ | 447 | */ |
459 | do | 448 | do { |
460 | { | ||
461 | from++; | 449 | from++; |
462 | i++; | 450 | i++; |
463 | } | 451 | } while(!(*from & 0x80) && isspace(*from)); |
464 | while(!(*from & 0x80) && isspace(*from)); | 452 | } else { |
465 | } | ||
466 | else | ||
467 | { | ||
468 | *to++ = tolower(*from); | 453 | *to++ = tolower(*from); |
469 | from++; | 454 | from++; |
470 | i++; | 455 | i++; |
471 | } | ||
472 | } | 456 | } |
457 | } | ||
473 | 458 | ||
474 | out->length = to - out->data; | 459 | out->length = to - out->data; |
475 | 460 | ||
476 | return 1; | 461 | return 1; |
477 | 462 | ||
478 | } | 463 | } |
479 | 464 | ||
480 | static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, | 465 | static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, |
481 | unsigned char **in) | 466 | unsigned char **in) |
482 | { | 467 | { |
483 | int i, len, ltmp; | 468 | int i, len, ltmp; |
484 | ASN1_VALUE *v; | 469 | ASN1_VALUE *v; |
485 | STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname; | 470 | STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname; |
486 | 471 | ||
487 | len = 0; | 472 | len = 0; |
488 | for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) | 473 | for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) { |
489 | { | ||
490 | v = sk_ASN1_VALUE_value(intname, i); | 474 | v = sk_ASN1_VALUE_value(intname, i); |
491 | ltmp = ASN1_item_ex_i2d(&v, in, | 475 | ltmp = ASN1_item_ex_i2d(&v, in, |
492 | ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1); | 476 | ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1); |
493 | if (ltmp < 0) | 477 | if (ltmp < 0) |
494 | return ltmp; | 478 | return ltmp; |
495 | len += ltmp; | 479 | len += ltmp; |
496 | } | ||
497 | return len; | ||
498 | } | 480 | } |
481 | return len; | ||
482 | } | ||
499 | 483 | ||
500 | int X509_NAME_set(X509_NAME **xn, X509_NAME *name) | 484 | int X509_NAME_set(X509_NAME **xn, X509_NAME *name) |
501 | { | 485 | { |
502 | X509_NAME *in; | 486 | X509_NAME *in; |
503 | 487 | ||
504 | if (!xn || !name) return(0); | 488 | if (!xn || !name) return(0); |
505 | 489 | ||
506 | if (*xn != name) | 490 | if (*xn != name) { |
507 | { | ||
508 | in=X509_NAME_dup(name); | 491 | in=X509_NAME_dup(name); |
509 | if (in != NULL) | 492 | if (in != NULL) { |
510 | { | ||
511 | X509_NAME_free(*xn); | 493 | X509_NAME_free(*xn); |
512 | *xn=in; | 494 | *xn=in; |
513 | } | ||
514 | } | 495 | } |
515 | return(*xn != NULL); | ||
516 | } | 496 | } |
497 | return(*xn != NULL); | ||
498 | } | ||
517 | 499 | ||
518 | IMPLEMENT_STACK_OF(X509_NAME_ENTRY) | 500 | IMPLEMENT_STACK_OF(X509_NAME_ENTRY) |
519 | IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY) | 501 | IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY) |