summaryrefslogtreecommitdiff
path: root/src/regress
diff options
context:
space:
mode:
authorbeck <>2023-05-29 11:54:50 +0000
committerbeck <>2023-05-29 11:54:50 +0000
commit3244a31e011c814d3d65b8b757a2dd3d28c542e2 (patch)
tree3ee54daae8ea3ccd2187904d345b85cb009a53dd /src/regress
parent26e7d501966e054b9e377033e8b93db0c4e42412 (diff)
downloadopenbsd-3244a31e011c814d3d65b8b757a2dd3d28c542e2.tar.gz
openbsd-3244a31e011c814d3d65b8b757a2dd3d28c542e2.tar.bz2
openbsd-3244a31e011c814d3d65b8b757a2dd3d28c542e2.zip
Make X509_NAME_get_text_by[NID|OBJ] safer.
This is an un-revert with nits of the previously landed change to do this which broke libtls. libtls has now been changed to not use this function. This change ensures that if something is returned it is "text" (UTF-8) and a C string not containing a NUL byte. Historically callers to this function assume the result is text and a C string however the OpenSSL version simply hands them the bytes from an ASN1_STRING and expects them to know bad things can happen which they almost universally do not check for. Partly inspired by goings on in boringssl. ok jsing@ tb@
Diffstat (limited to 'src/regress')
-rw-r--r--src/regress/lib/libcrypto/x509/x509_asn1.c79
1 files changed, 77 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/x509/x509_asn1.c b/src/regress/lib/libcrypto/x509/x509_asn1.c
index d96a51880e..4c038c9bbf 100644
--- a/src/regress/lib/libcrypto/x509/x509_asn1.c
+++ b/src/regress/lib/libcrypto/x509/x509_asn1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: x509_asn1.c,v 1.18 2023/05/03 08:10:23 beck Exp $ */ 1/* $OpenBSD: x509_asn1.c,v 1.19 2023/05/29 11:54:50 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Job Snijders <job@openbsd.org> 3 * Copyright (c) 2023 Job Snijders <job@openbsd.org>
4 * 4 *
@@ -512,13 +512,88 @@ test_x509_req_setters(void)
512 return failed; 512 return failed;
513} 513}
514 514
515int main(void) 515static const struct testcase {
516 char *data;
517 int len;
518 int len_to_pass;
519 int encode_type;
520 int expected_result;
521 char *expected_string;
522} testCases[] = {
523 /* should work */
524 {"fozzie", 6, 80, MBSTRING_ASC, 6, "fozzie"},
525 /* should work */
526 {"fozzie", 6, -1, MBSTRING_ASC, 6, ""},
527 /* should fail, truncation */
528 {"muppet", 6, 5, MBSTRING_ASC, -1, ""},
529 /* should fail, contains 0 byte */
530 {"g\0nzo", 5, 80, MBSTRING_ASC, -1, ""},
531 /* should fail, can't encode as utf-8 */
532 {"\x30\x00", 2, 80, V_ASN1_SEQUENCE, -1, ""},
533};
534
535#define NUM_TEST_CASES (sizeof(testCases) / sizeof(testCases[0]))
536
537static int
538test_x509_name_get(void)
539{
540 int failed = 0;
541 size_t i;
542
543 for (i = 0; i < NUM_TEST_CASES; i++) {
544 const struct testcase *test = testCases + i;
545 X509_NAME_ENTRY *entry = NULL;
546 X509_NAME *name = NULL;
547 char textbuf[80];
548 int result;
549
550 textbuf[0] = '\0';
551 if ((name = X509_NAME_new()) == NULL)
552 err(1, "X509_NAME_new");
553 if ((entry = X509_NAME_ENTRY_new()) == NULL)
554 err(1, "X509_NAME_ENTRY_new");
555 if (!X509_NAME_ENTRY_set_object(entry,
556 OBJ_nid2obj(NID_commonName)))
557 err(1, "X509_NAME_ENTRY_set_object");
558 if (!X509_NAME_ENTRY_set_data(entry, test->encode_type,
559 test->data, test->len))
560 err(1, "X509_NAME_ENTRY_set_data");
561 if (!X509_NAME_add_entry(name, entry, -1, 0))
562 err(1, "X509_NAME_add_entry");
563 if (test->len_to_pass == -1)
564 result = X509_NAME_get_text_by_NID(name, NID_commonName,
565 NULL, 0);
566 else
567 result = X509_NAME_get_text_by_NID(name, NID_commonName,
568 textbuf, test->len_to_pass);
569 if (result != test->expected_result) {
570 fprintf(stderr,
571 "Test %zu X509_GET_text_by_NID returned %d,"
572 "expected %d\n", i, result, test->expected_result);
573 failed++;
574 }
575 if (result != -1 &&
576 strcmp(test->expected_string, textbuf) != 0) {
577 fprintf(stderr,
578 "Test %zu, X509_GET_text_by_NID returned bytes do"
579 "not match \n", i);
580 failed++;
581 }
582 X509_NAME_ENTRY_free(entry);
583 X509_NAME_free(name);
584 }
585 return failed;
586}
587
588int
589main(void)
516{ 590{
517 int failed = 0; 591 int failed = 0;
518 592
519 failed |= test_x509_setters(); 593 failed |= test_x509_setters();
520 /* failed |= */ test_x509_crl_setters(); 594 /* failed |= */ test_x509_crl_setters();
521 /* failed |= */ test_x509_req_setters(); 595 /* failed |= */ test_x509_req_setters();
596 failed |= test_x509_name_get();
522 597
523 OPENSSL_cleanup(); 598 OPENSSL_cleanup();
524 599