1 /*
2 * Copyright 1999-2026 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /* Internal tests for the asn1 module */
11
12 /*
13 * RSA low level APIs are deprecated for public use, but still ok for
14 * internal use.
15 */
16 #include "internal/deprecated.h"
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include <openssl/asn1.h>
22 #include <openssl/evp.h>
23 #include <openssl/pkcs12.h>
24 #include <openssl/objects.h>
25 #include "testutil.h"
26 #include "internal/nelem.h"
27
28 /**********************************************************************
29 *
30 * Test of a_strnid's tbl_standard
31 *
32 ***/
33
34 #include "../crypto/asn1/tbl_standard.h"
35
test_tbl_standard(void)36 static int test_tbl_standard(void)
37 {
38 const ASN1_STRING_TABLE *tmp;
39 int last_nid = -1;
40 size_t i;
41
42 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
43 if (tmp->nid < last_nid) {
44 last_nid = 0;
45 break;
46 }
47 last_nid = tmp->nid;
48 }
49
50 if (TEST_int_ne(last_nid, 0)) {
51 TEST_info("asn1 tbl_standard: Table order OK");
52 return 1;
53 }
54
55 TEST_info("asn1 tbl_standard: out of order");
56 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
57 TEST_note("asn1 tbl_standard: Index %zu, NID %d, Name=%s",
58 i, tmp->nid, OBJ_nid2ln(tmp->nid));
59
60 return 0;
61 }
62
63 /**********************************************************************
64 *
65 * Test of ameth_lib's standard_methods
66 *
67 ***/
68
69 #include "crypto/asn1.h"
70 #include "../crypto/asn1/standard_methods.h"
71
test_standard_methods(void)72 static int test_standard_methods(void)
73 {
74 const EVP_PKEY_ASN1_METHOD **tmp;
75 int last_pkey_id = -1;
76 size_t i;
77 int ok = 1;
78
79 for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
80 i++, tmp++) {
81 if ((*tmp)->pkey_id < last_pkey_id) {
82 last_pkey_id = 0;
83 break;
84 }
85 last_pkey_id = (*tmp)->pkey_id;
86
87 /*
88 * One of the following must be true:
89 *
90 * pem_str == NULL AND ASN1_PKEY_ALIAS is set
91 * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
92 *
93 * Anything else is an error and may lead to a corrupt ASN1 method table
94 */
95 if (!TEST_true(((*tmp)->pem_str == NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) != 0)
96 || ((*tmp)->pem_str != NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
97 TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
98 i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
99 ok = 0;
100 }
101 }
102
103 if (TEST_int_ne(last_pkey_id, 0)) {
104 TEST_info("asn1 standard methods: Table order OK");
105 return ok;
106 }
107
108 TEST_note("asn1 standard methods: out of order");
109 for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods);
110 i++, tmp++)
111 TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s",
112 i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id));
113
114 return 0;
115 }
116
117 /**********************************************************************
118 *
119 * Test of that i2d fail on non-existing non-optional items
120 *
121 ***/
122
123 #include <openssl/rsa.h>
124
test_empty_nonoptional_content(void)125 static int test_empty_nonoptional_content(void)
126 {
127 RSA *rsa = NULL;
128 BIGNUM *n = NULL;
129 BIGNUM *e = NULL;
130 int ok = 0;
131
132 if (!TEST_ptr(rsa = RSA_new())
133 || !TEST_ptr(n = BN_new())
134 || !TEST_ptr(e = BN_new())
135 || !TEST_true(RSA_set0_key(rsa, n, e, NULL)))
136 goto end;
137
138 n = e = NULL; /* They are now "owned" by |rsa| */
139
140 /*
141 * This SHOULD fail, as we're trying to encode a public key as a private
142 * key. The private key bits MUST be present for a proper RSAPrivateKey.
143 */
144 if (TEST_int_le(i2d_RSAPrivateKey(rsa, NULL), 0))
145 ok = 1;
146
147 end:
148 RSA_free(rsa);
149 BN_free(n);
150 BN_free(e);
151 return ok;
152 }
153
154 /**********************************************************************
155 *
156 * Tests of the Unicode code point range
157 *
158 ***/
159
test_unicode(const unsigned char * univ,size_t len,int expected)160 static int test_unicode(const unsigned char *univ, size_t len, int expected)
161 {
162 const unsigned char *end = univ + len;
163 int ok = 1;
164
165 for (; univ < end; univ += 4) {
166 if (!TEST_int_eq(ASN1_mbstring_copy(NULL, univ, 4, MBSTRING_UNIV,
167 B_ASN1_UTF8STRING),
168 expected))
169 ok = 0;
170 }
171 return ok;
172 }
173
test_unicode_range(void)174 static int test_unicode_range(void)
175 {
176 const unsigned char univ_ok[] = "\0\0\0\0"
177 "\0\0\xd7\xff"
178 "\0\0\xe0\x00"
179 "\0\x10\xff\xff";
180 const unsigned char univ_bad[] = "\0\0\xd8\x00"
181 "\0\0\xdf\xff"
182 "\0\x11\x00\x00"
183 "\x80\x00\x00\x00"
184 "\xff\xff\xff\xff";
185 int ok = 1;
186
187 if (!test_unicode(univ_ok, sizeof univ_ok - 1, V_ASN1_UTF8STRING))
188 ok = 0;
189 if (!test_unicode(univ_bad, sizeof univ_bad - 1, -1))
190 ok = 0;
191 return ok;
192 }
193
test_invalid_utf8(void)194 static int test_invalid_utf8(void)
195 {
196 const unsigned char inv_utf8[] = "\xF4\x90\x80\x80";
197 unsigned long val;
198
199 if (!TEST_int_lt(UTF8_getc(inv_utf8, sizeof(inv_utf8), &val), 0))
200 return 0;
201 return 1;
202 }
203
204 /**********************************************************************
205 *
206 * Tests of object creation
207 *
208 ***/
209
test_obj_create_once(const char * oid,const char * sn,const char * ln)210 static int test_obj_create_once(const char *oid, const char *sn, const char *ln)
211 {
212 int nid;
213
214 ERR_set_mark();
215
216 nid = OBJ_create(oid, sn, ln);
217
218 if (nid == NID_undef) {
219 unsigned long err = ERR_peek_last_error();
220 int l = ERR_GET_LIB(err);
221 int r = ERR_GET_REASON(err);
222
223 /* If it exists, that's fine, otherwise not */
224 if (l != ERR_LIB_OBJ || r != OBJ_R_OID_EXISTS) {
225 ERR_clear_last_mark();
226 return 0;
227 }
228 }
229 ERR_pop_to_mark();
230 return 1;
231 }
232
test_obj_create(void)233 static int test_obj_create(void)
234 {
235 /* Stolen from evp_extra_test.c */
236 #define arc "1.3.6.1.4.1.16604.998866."
237 #define broken_arc "25."
238 #define sn_prefix "custom"
239 #define ln_prefix "custom"
240
241 /* Try different combinations of correct object creation */
242 if (!TEST_true(test_obj_create_once(NULL, sn_prefix "1", NULL))
243 || !TEST_int_ne(OBJ_sn2nid(sn_prefix "1"), NID_undef)
244 || !TEST_true(test_obj_create_once(NULL, NULL, ln_prefix "2"))
245 || !TEST_int_ne(OBJ_ln2nid(ln_prefix "2"), NID_undef)
246 || !TEST_true(test_obj_create_once(NULL, sn_prefix "3", ln_prefix "3"))
247 || !TEST_int_ne(OBJ_sn2nid(sn_prefix "3"), NID_undef)
248 || !TEST_int_ne(OBJ_ln2nid(ln_prefix "3"), NID_undef)
249 || !TEST_true(test_obj_create_once(arc "4", NULL, NULL))
250 || !TEST_true(test_obj_create_once(arc "5", sn_prefix "5", NULL))
251 || !TEST_int_ne(OBJ_sn2nid(sn_prefix "5"), NID_undef)
252 || !TEST_true(test_obj_create_once(arc "6", NULL, ln_prefix "6"))
253 || !TEST_int_ne(OBJ_ln2nid(ln_prefix "6"), NID_undef)
254 || !TEST_true(test_obj_create_once(arc "7",
255 sn_prefix "7", ln_prefix "7"))
256 || !TEST_int_ne(OBJ_sn2nid(sn_prefix "7"), NID_undef)
257 || !TEST_int_ne(OBJ_ln2nid(ln_prefix "7"), NID_undef))
258 return 0;
259
260 if (!TEST_false(test_obj_create_once(NULL, NULL, NULL))
261 || !TEST_false(test_obj_create_once(broken_arc "8",
262 sn_prefix "8", ln_prefix "8")))
263 return 0;
264
265 return 1;
266 }
267
test_obj_nid_undef(void)268 static int test_obj_nid_undef(void)
269 {
270 if (!TEST_ptr(OBJ_nid2obj(NID_undef))
271 || !TEST_ptr(OBJ_nid2sn(NID_undef))
272 || !TEST_ptr(OBJ_nid2ln(NID_undef)))
273 return 0;
274
275 return 1;
276 }
277
test_mbstring_ncopy(void)278 static int test_mbstring_ncopy(void)
279 {
280 ASN1_STRING *str = NULL;
281 const unsigned char in[] = { 0xFF, 0xFE, 0xFF, 0xFE };
282 int inlen = 4;
283 int inform = MBSTRING_UNIV;
284
285 if (!TEST_int_eq(ASN1_mbstring_ncopy(&str, in, inlen, inform, B_ASN1_GENERALSTRING, 0, 0), -1)
286 || !TEST_int_eq(ASN1_mbstring_ncopy(&str, in, inlen, inform, B_ASN1_VISIBLESTRING, 0, 0), -1)
287 || !TEST_int_eq(ASN1_mbstring_ncopy(&str, in, inlen, inform, B_ASN1_VIDEOTEXSTRING, 0, 0), -1)
288 || !TEST_int_eq(ASN1_mbstring_ncopy(&str, in, inlen, inform, B_ASN1_GENERALIZEDTIME, 0, 0), -1))
289 return 0;
290
291 return 1;
292 }
293
test_ossl_uni2utf8(void)294 static int test_ossl_uni2utf8(void)
295 {
296 const unsigned char in[] = { 0x21, 0x92 }; /* unicode right arrow */
297 int inlen = 2;
298 char *out = NULL;
299 int ok = 0;
300
301 /* reproducer for CVE-2025-69419 */
302 out = OPENSSL_uni2utf8(in, inlen);
303 if (TEST_str_eq(out, "\xe2\x86\x92"))
304 ok = 1;
305
306 OPENSSL_free(out);
307 return ok;
308 }
309
setup_tests(void)310 int setup_tests(void)
311 {
312 ADD_TEST(test_tbl_standard);
313 ADD_TEST(test_standard_methods);
314 ADD_TEST(test_empty_nonoptional_content);
315 ADD_TEST(test_unicode_range);
316 ADD_TEST(test_invalid_utf8);
317 ADD_TEST(test_obj_create);
318 ADD_TEST(test_obj_nid_undef);
319 ADD_TEST(test_mbstring_ncopy);
320 ADD_TEST(test_ossl_uni2utf8);
321 return 1;
322 }
323