2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* Internal tests for the asn1 module */
13 * RSA low level APIs are deprecated for public use, but still ok for
16 #include "internal/deprecated.h"
21 #include <openssl/asn1.h>
22 #include <openssl/evp.h>
23 #include <openssl/objects.h>
25 #include "internal/nelem.h"
27 /**********************************************************************
29 * Test of a_strnid's tbl_standard
33 #include "../crypto/asn1/tbl_standard.h"
35 static int test_tbl_standard(void)
37 const ASN1_STRING_TABLE *tmp;
41 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
42 if (tmp->nid < last_nid) {
49 if (TEST_int_ne(last_nid, 0)) {
50 TEST_info("asn1 tbl_standard: Table order OK");
54 TEST_info("asn1 tbl_standard: out of order");
55 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
56 TEST_note("asn1 tbl_standard: Index %zu, NID %d, Name=%s",
57 i, tmp->nid, OBJ_nid2ln(tmp->nid));
62 /**********************************************************************
64 * Test of ameth_lib's standard_methods
68 #include "crypto/asn1.h"
69 #include "../crypto/asn1/standard_methods.h"
71 static int test_standard_methods(void)
73 const EVP_PKEY_ASN1_METHOD **tmp;
74 int last_pkey_id = -1;
78 for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
80 if ((*tmp)->pkey_id < last_pkey_id) {
84 last_pkey_id = (*tmp)->pkey_id;
87 * One of the following must be true:
89 * pem_str == NULL AND ASN1_PKEY_ALIAS is set
90 * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
92 * Anything else is an error and may lead to a corrupt ASN1 method table
94 if (!TEST_true(((*tmp)->pem_str == NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) != 0)
95 || ((*tmp)->pem_str != NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
96 TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
97 i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
102 if (TEST_int_ne(last_pkey_id, 0)) {
103 TEST_info("asn1 standard methods: Table order OK");
107 TEST_note("asn1 standard methods: out of order");
108 for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
110 TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
111 i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
116 /**********************************************************************
118 * Test of that i2d fail on non-existing non-optional items
122 #include <openssl/rsa.h>
124 static int test_empty_nonoptional_content(void)
131 if (!TEST_ptr(rsa = RSA_new())
132 || !TEST_ptr(n = BN_new())
133 || !TEST_ptr(e = BN_new())
134 || !TEST_true(RSA_set0_key(rsa, n, e, NULL)))
137 n = e = NULL; /* They are now "owned" by |rsa| */
140 * This SHOULD fail, as we're trying to encode a public key as a private
141 * key. The private key bits MUST be present for a proper RSAPrivateKey.
143 if (TEST_int_le(i2d_RSAPrivateKey(rsa, NULL), 0))
153 /**********************************************************************
155 * Tests of the Unicode code point range
159 static int test_unicode(const unsigned char *univ, size_t len, int expected)
161 const unsigned char *end = univ + len;
164 for (; univ < end; univ += 4) {
165 if (!TEST_int_eq(ASN1_mbstring_copy(NULL, univ, 4, MBSTRING_UNIV,
173 static int test_unicode_range(void)
175 const unsigned char univ_ok[] = "\0\0\0\0"
179 const unsigned char univ_bad[] = "\0\0\xd8\x00"
186 if (!test_unicode(univ_ok, sizeof univ_ok - 1, V_ASN1_UTF8STRING))
188 if (!test_unicode(univ_bad, sizeof univ_bad - 1, -1))
193 int setup_tests(void)
195 ADD_TEST(test_tbl_standard);
196 ADD_TEST(test_standard_methods);
197 ADD_TEST(test_empty_nonoptional_content);
198 ADD_TEST(test_unicode_range);