Convert asn1 selftests (a_strnid and ameth_lib) into internal test
authorRichard Levitte <levitte@openssl.org>
Wed, 26 Oct 2016 21:12:48 +0000 (23:12 +0200)
committerRichard Levitte <levitte@openssl.org>
Thu, 3 Nov 2016 12:13:31 +0000 (13:13 +0100)
Reviewed-by: Emilia Käsper <emilia@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/1789)

crypto/asn1/a_strnid.c
crypto/asn1/ameth_lib.c
crypto/asn1/standard_methods.h [new file with mode: 0644]
crypto/asn1/tbl_standard.h [new file with mode: 0644]
test/asn1_internal_test.c [new file with mode: 0644]
test/build.info

index 53832c8..ce8a646 100644 (file)
@@ -106,53 +106,7 @@ ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
  * Now the tables and helper functions for the string table:
  */
 
-/* size limits: this stuff is taken straight from RFC3280 */
-
-#define ub_name                         32768
-#define ub_common_name                  64
-#define ub_locality_name                128
-#define ub_state_name                   128
-#define ub_organization_name            64
-#define ub_organization_unit_name       64
-#define ub_title                        64
-#define ub_email_address                128
-#define ub_serial_number                64
-
-/* From RFC4524 */
-
-#define ub_rfc822_mailbox               256
-
-/* This table must be kept in NID order */
-
-static const ASN1_STRING_TABLE tbl_standard[] = {
-    {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
-    {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
-    {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
-    {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
-    {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
-    {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
-     0},
-    {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
-     STABLE_NO_MASK},
-    {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
-    {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
-    {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
-    {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
-    {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
-    {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
-    {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
-     STABLE_NO_MASK},
-    {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
-    {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
-    {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
-    {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
-    {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
-    {NID_rfc822Mailbox, 1, ub_rfc822_mailbox, B_ASN1_IA5STRING,
-     STABLE_NO_MASK},
-    {NID_INN, 1, 12, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
-    {NID_OGRN, 1, 13, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
-    {NID_SNILS, 1, 11, B_ASN1_NUMERICSTRING, STABLE_NO_MASK}
-};
+#include "tbl_standard.h"
 
 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
                         const ASN1_STRING_TABLE *const *b)
@@ -256,32 +210,3 @@ static void st_free(ASN1_STRING_TABLE *tbl)
     if (tbl->flags & STABLE_FLAGS_MALLOC)
         OPENSSL_free(tbl);
 }
-
-
-#ifdef STRING_TABLE_TEST
-
-main()
-{
-    ASN1_STRING_TABLE *tmp;
-    int i, last_nid = -1;
-
-    for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
-        if (tmp->nid < last_nid) {
-            last_nid = 0;
-            break;
-        }
-        last_nid = tmp->nid;
-    }
-
-    if (last_nid != 0) {
-        printf("Table order OK\n");
-        exit(0);
-    }
-
-    for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
-        printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
-               OBJ_nid2ln(tmp->nid));
-
-}
-
-#endif
index cfde49a..c677917 100644 (file)
 #include "internal/asn1_int.h"
 #include "internal/evp_int.h"
 
-/* Keep this sorted in type order !! */
-static const EVP_PKEY_ASN1_METHOD *standard_methods[] = {
-#ifndef OPENSSL_NO_RSA
-    &rsa_asn1_meths[0],
-    &rsa_asn1_meths[1],
-#endif
-#ifndef OPENSSL_NO_DH
-    &dh_asn1_meth,
-#endif
-#ifndef OPENSSL_NO_DSA
-    &dsa_asn1_meths[0],
-    &dsa_asn1_meths[1],
-    &dsa_asn1_meths[2],
-    &dsa_asn1_meths[3],
-    &dsa_asn1_meths[4],
-#endif
-#ifndef OPENSSL_NO_EC
-    &eckey_asn1_meth,
-#endif
-    &hmac_asn1_meth,
-#ifndef OPENSSL_NO_CMAC
-    &cmac_asn1_meth,
-#endif
-#ifndef OPENSSL_NO_DH
-    &dhx_asn1_meth,
-#endif
-#ifndef OPENSSL_NO_EC
-    &ecx25519_asn1_meth
-#endif
-};
+#include "standard_methods.h"
 
 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
 static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
 
-#ifdef TEST
-void main()
-{
-    int i;
-    for (i = 0; i < OSSL_NELEM(standard_methods); i++)
-        fprintf(stderr, "Number %d id=%d (%s)\n", i,
-                standard_methods[i]->pkey_id,
-                OBJ_nid2sn(standard_methods[i]->pkey_id));
-}
-#endif
-
 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
                            const EVP_PKEY_ASN1_METHOD *, ameth);
 
diff --git a/crypto/asn1/standard_methods.h b/crypto/asn1/standard_methods.h
new file mode 100644 (file)
index 0000000..5b8f9dd
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+/* Keep this sorted in type order !! */
+static const EVP_PKEY_ASN1_METHOD *standard_methods[] = {
+#ifndef OPENSSL_NO_RSA
+    &rsa_asn1_meths[0],
+    &rsa_asn1_meths[1],
+#endif
+#ifndef OPENSSL_NO_DH
+    &dh_asn1_meth,
+#endif
+#ifndef OPENSSL_NO_DSA
+    &dsa_asn1_meths[0],
+    &dsa_asn1_meths[1],
+    &dsa_asn1_meths[2],
+    &dsa_asn1_meths[3],
+    &dsa_asn1_meths[4],
+#endif
+#ifndef OPENSSL_NO_EC
+    &eckey_asn1_meth,
+#endif
+    &hmac_asn1_meth,
+#ifndef OPENSSL_NO_CMAC
+    &cmac_asn1_meth,
+#endif
+#ifndef OPENSSL_NO_DH
+    &dhx_asn1_meth,
+#endif
+#ifndef OPENSSL_NO_EC
+    &ecx25519_asn1_meth
+#endif
+};
+
diff --git a/crypto/asn1/tbl_standard.h b/crypto/asn1/tbl_standard.h
new file mode 100644 (file)
index 0000000..7fb04f8
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+/* size limits: this stuff is taken straight from RFC3280 */
+
+#define ub_name                         32768
+#define ub_common_name                  64
+#define ub_locality_name                128
+#define ub_state_name                   128
+#define ub_organization_name            64
+#define ub_organization_unit_name       64
+#define ub_title                        64
+#define ub_email_address                128
+#define ub_serial_number                64
+
+/* From RFC4524 */
+
+#define ub_rfc822_mailbox               256
+
+/* This table must be kept in NID order */
+
+static const ASN1_STRING_TABLE tbl_standard[] = {
+    {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
+    {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
+    {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
+    {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
+    {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
+    {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
+     0},
+    {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
+     STABLE_NO_MASK},
+    {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
+    {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
+    {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
+    {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
+    {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
+    {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
+    {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
+     STABLE_NO_MASK},
+    {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
+    {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
+    {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
+    {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
+    {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
+    {NID_rfc822Mailbox, 1, ub_rfc822_mailbox, B_ASN1_IA5STRING,
+     STABLE_NO_MASK},
+    {NID_INN, 1, 12, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
+    {NID_OGRN, 1, 13, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
+    {NID_SNILS, 1, 11, B_ASN1_NUMERICSTRING, STABLE_NO_MASK}
+};
+
diff --git a/test/asn1_internal_test.c b/test/asn1_internal_test.c
new file mode 100644 (file)
index 0000000..fc0ac20
--- /dev/null
@@ -0,0 +1,152 @@
+/*
+ * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+/* Internal tests for the asn1 module */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <openssl/asn1.h>
+#include <openssl/evp.h>
+#include <openssl/objects.h>
+#include "testutil.h"
+#include "e_os.h"
+
+typedef struct {
+    const char *test_case_name;
+    const char *test_section;
+} SIMPLE_FIXTURE;
+
+/**********************************************************************
+ *
+ * Test of a_strnid's tbl_standard
+ *
+ ***/
+
+static SIMPLE_FIXTURE setup_tbl_standard(const char *const test_case_name)
+{
+    SIMPLE_FIXTURE fixture;
+    fixture.test_case_name = test_case_name;
+    return fixture;
+}
+
+#include "../crypto/asn1/tbl_standard.h"
+
+static int execute_tbl_standard(SIMPLE_FIXTURE fixture)
+{
+    const ASN1_STRING_TABLE *tmp;
+    int last_nid = -1;
+    size_t i;
+
+    for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
+        if (tmp->nid < last_nid) {
+            last_nid = 0;
+            break;
+        }
+        last_nid = tmp->nid;
+    }
+
+    if (last_nid != 0) {
+        fprintf(stderr, "%s: Table order OK\n", fixture.test_section);
+        return 1;
+    }
+
+    for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
+        fprintf(stderr, "%s: Index %" OSSLzu ", NID %d, Name=%s\n",
+               fixture.test_section, i, tmp->nid, OBJ_nid2ln(tmp->nid));
+
+    return 0;
+}
+
+static void teardown_tbl_standard(SIMPLE_FIXTURE fixture)
+{
+    ERR_print_errors_fp(stderr);
+}
+
+/**********************************************************************
+ *
+ * Test of ameth_lib's standard_methods
+ *
+ ***/
+
+static SIMPLE_FIXTURE setup_standard_methods(const char *const test_case_name)
+{
+    SIMPLE_FIXTURE fixture;
+    fixture.test_case_name = test_case_name;
+    return fixture;
+}
+
+#include "internal/asn1_int.h"
+#include "../crypto/asn1/standard_methods.h"
+
+static int execute_standard_methods(SIMPLE_FIXTURE fixture)
+{
+    const EVP_PKEY_ASN1_METHOD **tmp;
+    int last_pkey_id = -1;
+    size_t i;
+
+    for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
+         i++, tmp++) {
+        if ((*tmp)->pkey_id < last_pkey_id) {
+            last_pkey_id = 0;
+            break;
+        }
+        last_pkey_id = (*tmp)->pkey_id;
+    }
+
+    if (last_pkey_id != 0) {
+        fprintf(stderr, "%s: Table order OK\n", fixture.test_section);
+        return 1;
+    }
+
+    for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
+         i++, tmp++)
+        fprintf(stderr, "%s: Index %" OSSLzu ", pkey ID %d, Name=%s\n",
+               fixture.test_section, i, (*tmp)->pkey_id,
+               OBJ_nid2sn((*tmp)->pkey_id));
+
+    return 0;
+}
+
+static void teardown_standard_methods(SIMPLE_FIXTURE fixture)
+{
+    ERR_print_errors_fp(stderr);
+}
+
+/**********************************************************************
+ *
+ * Test driver
+ *
+ ***/
+
+static struct {
+    const char *section;
+    SIMPLE_FIXTURE (*setup)(const char *const test_case_name);
+    int (*execute)(SIMPLE_FIXTURE);
+    void (*teardown)(SIMPLE_FIXTURE);
+} tests[] = {
+    {"asn1 tlb_standard", setup_tbl_standard, execute_tbl_standard,
+     teardown_tbl_standard},
+    {"asn1 standard_methods", setup_standard_methods, execute_standard_methods,
+     teardown_standard_methods}
+};
+
+static int drive_tests(int idx)
+{
+    SETUP_TEST_FIXTURE(SIMPLE_FIXTURE, tests[idx].setup);
+    fixture.test_section = tests[idx].section;
+    EXECUTE_TEST(tests[idx].execute, tests[idx].teardown);
+}
+
+int main(int argc, char **argv)
+{
+    ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
+
+    return run_tests(argv[0]);
+}
index 6f00865..266c3c3 100644 (file)
@@ -314,6 +314,7 @@ IF[{- !$disabled{tests} -}]
   # Note that when building with static libraries, none of those extra files
   # are needed, since all symbols are available anyway, regardless of what's
   # listed in util/*.num.
+  PROGRAMS_NO_INST=asn1_internal_test
   IF[{- !$disabled{poly1305} -}]
     PROGRAMS_NO_INST=poly1305_internal_test
   ENDIF
@@ -327,6 +328,17 @@ IF[{- !$disabled{tests} -}]
   ENDIF
   INCLUDE[poly1305_internal_test]=.. ../include ../crypto/include
   DEPEND[poly1305_internal_test]=../libcrypto
+
+  SOURCE[asn1_internal_test]=asn1_internal_test.c testutil.c
+  IF[{- !$disabled{shared} -}]
+    SOURCE[asn1_internal_test]= ../crypto/asn1/a_strnid.c \
+        ../crypto/rsa/rsa_ameth.c ../crypto/dsa/dsa_ameth.c \
+        ../crypto/dh/dh_ameth.c ../crypto/ec/ec_ameth.c \
+        ../crypto/hmac/hm_ameth.c ../crypto/cmac/cm_ameth.c \
+        ../crypto/ec/ecx_meth.c ../crypto/ec/curve25519.c
+  ENDIF
+  INCLUDE[asn1_internal_test]=.. ../include ../crypto/include
+  DEPEND[asn1_internal_test]=../libcrypto
 ENDIF
 
 {-