2 * Copyright 1999-2023 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 /**********************************************************************
195 * Tests of object creation
199 static int test_obj_create_once(const char *oid
, const char *sn
, const char *ln
)
205 nid
= OBJ_create(oid
, sn
, ln
);
207 if (nid
== NID_undef
) {
208 unsigned long err
= ERR_peek_last_error();
209 int l
= ERR_GET_LIB(err
);
210 int r
= ERR_GET_REASON(err
);
212 /* If it exists, that's fine, otherwise not */
213 if (l
!= ERR_LIB_OBJ
|| r
!= OBJ_R_OID_EXISTS
) {
214 ERR_clear_last_mark();
222 static int test_obj_create(void)
224 /* Stolen from evp_extra_test.c */
225 #define arc "1.3.6.1.4.1.16604.998866."
226 #define broken_arc "25."
227 #define sn_prefix "custom"
228 #define ln_prefix "custom"
230 /* Try different combinations of correct object creation */
231 if (!TEST_true(test_obj_create_once(NULL
, sn_prefix
"1", NULL
))
232 || !TEST_int_ne(OBJ_sn2nid(sn_prefix
"1"), NID_undef
)
233 || !TEST_true(test_obj_create_once(NULL
, NULL
, ln_prefix
"2"))
234 || !TEST_int_ne(OBJ_ln2nid(ln_prefix
"2"), NID_undef
)
235 || !TEST_true(test_obj_create_once(NULL
, sn_prefix
"3", ln_prefix
"3"))
236 || !TEST_int_ne(OBJ_sn2nid(sn_prefix
"3"), NID_undef
)
237 || !TEST_int_ne(OBJ_ln2nid(ln_prefix
"3"), NID_undef
)
238 || !TEST_true(test_obj_create_once(arc
"4", NULL
, NULL
))
239 || !TEST_true(test_obj_create_once(arc
"5", sn_prefix
"5", NULL
))
240 || !TEST_int_ne(OBJ_sn2nid(sn_prefix
"5"), NID_undef
)
241 || !TEST_true(test_obj_create_once(arc
"6", NULL
, ln_prefix
"6"))
242 || !TEST_int_ne(OBJ_ln2nid(ln_prefix
"6"), NID_undef
)
243 || !TEST_true(test_obj_create_once(arc
"7",
244 sn_prefix
"7", ln_prefix
"7"))
245 || !TEST_int_ne(OBJ_sn2nid(sn_prefix
"7"), NID_undef
)
246 || !TEST_int_ne(OBJ_ln2nid(ln_prefix
"7"), NID_undef
))
249 if (!TEST_false(test_obj_create_once(NULL
, NULL
, NULL
))
250 || !TEST_false(test_obj_create_once(broken_arc
"8",
251 sn_prefix
"8", ln_prefix
"8")))
257 static int test_obj_nid_undef(void)
259 if (!TEST_ptr(OBJ_nid2obj(NID_undef
))
260 || !TEST_ptr(OBJ_nid2sn(NID_undef
))
261 || !TEST_ptr(OBJ_nid2ln(NID_undef
)))
267 int setup_tests(void)
269 ADD_TEST(test_tbl_standard
);
270 ADD_TEST(test_standard_methods
);
271 ADD_TEST(test_empty_nonoptional_content
);
272 ADD_TEST(test_unicode_range
);
273 ADD_TEST(test_obj_create
);
274 ADD_TEST(test_obj_nid_undef
);