1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <string.h>
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#include <openssl/rand.h>
14e1051a39Sopenharmony_ci#include <openssl/asn1t.h>
15e1051a39Sopenharmony_ci#include <openssl/obj_mac.h>
16e1051a39Sopenharmony_ci#include "internal/numbers.h"
17e1051a39Sopenharmony_ci#include "testutil.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci#ifdef __GNUC__
20e1051a39Sopenharmony_ci# pragma GCC diagnostic ignored "-Wunused-function"
21e1051a39Sopenharmony_ci#endif
22e1051a39Sopenharmony_ci#ifdef __clang__
23e1051a39Sopenharmony_ci# pragma clang diagnostic ignored "-Wunused-function"
24e1051a39Sopenharmony_ci#endif
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci/* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
27e1051a39Sopenharmony_cistatic unsigned char t_invalid_zero[] = {
28e1051a39Sopenharmony_ci    0x30, 0x02,                  /* SEQUENCE tag + length */
29e1051a39Sopenharmony_ci    0x02, 0x00                   /* INTEGER tag + length */
30e1051a39Sopenharmony_ci};
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
33e1051a39Sopenharmony_ci/* LONG case ************************************************************* */
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_citypedef struct {
36e1051a39Sopenharmony_ci    long test_long;
37e1051a39Sopenharmony_ci} ASN1_LONG_DATA;
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ciASN1_SEQUENCE(ASN1_LONG_DATA) = {
40e1051a39Sopenharmony_ci    ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
41e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
44e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_cistatic int test_long(void)
47e1051a39Sopenharmony_ci{
48e1051a39Sopenharmony_ci    const unsigned char *p = t_invalid_zero;
49e1051a39Sopenharmony_ci    ASN1_LONG_DATA *dectst =
50e1051a39Sopenharmony_ci        d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci    if (dectst == NULL)
53e1051a39Sopenharmony_ci        return 0;                /* Fail */
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci    ASN1_LONG_DATA_free(dectst);
56e1051a39Sopenharmony_ci    return 1;
57e1051a39Sopenharmony_ci}
58e1051a39Sopenharmony_ci#endif
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci/* INT32 case ************************************************************* */
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_citypedef struct {
63e1051a39Sopenharmony_ci    int32_t test_int32;
64e1051a39Sopenharmony_ci} ASN1_INT32_DATA;
65e1051a39Sopenharmony_ci
66e1051a39Sopenharmony_ciASN1_SEQUENCE(ASN1_INT32_DATA) = {
67e1051a39Sopenharmony_ci    ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
68e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
71e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_cistatic int test_int32(void)
74e1051a39Sopenharmony_ci{
75e1051a39Sopenharmony_ci    const unsigned char *p = t_invalid_zero;
76e1051a39Sopenharmony_ci    ASN1_INT32_DATA *dectst =
77e1051a39Sopenharmony_ci        d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci    if (dectst == NULL)
80e1051a39Sopenharmony_ci        return 0;                /* Fail */
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci    ASN1_INT32_DATA_free(dectst);
83e1051a39Sopenharmony_ci    return 1;
84e1051a39Sopenharmony_ci}
85e1051a39Sopenharmony_ci
86e1051a39Sopenharmony_ci/* UINT32 case ************************************************************* */
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_citypedef struct {
89e1051a39Sopenharmony_ci    uint32_t test_uint32;
90e1051a39Sopenharmony_ci} ASN1_UINT32_DATA;
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ciASN1_SEQUENCE(ASN1_UINT32_DATA) = {
93e1051a39Sopenharmony_ci    ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
94e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
95e1051a39Sopenharmony_ci
96e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
97e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_cistatic int test_uint32(void)
100e1051a39Sopenharmony_ci{
101e1051a39Sopenharmony_ci    const unsigned char *p = t_invalid_zero;
102e1051a39Sopenharmony_ci    ASN1_UINT32_DATA *dectst =
103e1051a39Sopenharmony_ci        d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
104e1051a39Sopenharmony_ci
105e1051a39Sopenharmony_ci    if (dectst == NULL)
106e1051a39Sopenharmony_ci        return 0;                /* Fail */
107e1051a39Sopenharmony_ci
108e1051a39Sopenharmony_ci    ASN1_UINT32_DATA_free(dectst);
109e1051a39Sopenharmony_ci    return 1;
110e1051a39Sopenharmony_ci}
111e1051a39Sopenharmony_ci
112e1051a39Sopenharmony_ci/* INT64 case ************************************************************* */
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_citypedef struct {
115e1051a39Sopenharmony_ci    int64_t test_int64;
116e1051a39Sopenharmony_ci} ASN1_INT64_DATA;
117e1051a39Sopenharmony_ci
118e1051a39Sopenharmony_ciASN1_SEQUENCE(ASN1_INT64_DATA) = {
119e1051a39Sopenharmony_ci    ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
120e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
121e1051a39Sopenharmony_ci
122e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
123e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
124e1051a39Sopenharmony_ci
125e1051a39Sopenharmony_cistatic int test_int64(void)
126e1051a39Sopenharmony_ci{
127e1051a39Sopenharmony_ci    const unsigned char *p = t_invalid_zero;
128e1051a39Sopenharmony_ci    ASN1_INT64_DATA *dectst =
129e1051a39Sopenharmony_ci        d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
130e1051a39Sopenharmony_ci
131e1051a39Sopenharmony_ci    if (dectst == NULL)
132e1051a39Sopenharmony_ci        return 0;                /* Fail */
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_ci    ASN1_INT64_DATA_free(dectst);
135e1051a39Sopenharmony_ci    return 1;
136e1051a39Sopenharmony_ci}
137e1051a39Sopenharmony_ci
138e1051a39Sopenharmony_ci/* UINT64 case ************************************************************* */
139e1051a39Sopenharmony_ci
140e1051a39Sopenharmony_citypedef struct {
141e1051a39Sopenharmony_ci    uint64_t test_uint64;
142e1051a39Sopenharmony_ci} ASN1_UINT64_DATA;
143e1051a39Sopenharmony_ci
144e1051a39Sopenharmony_ciASN1_SEQUENCE(ASN1_UINT64_DATA) = {
145e1051a39Sopenharmony_ci    ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
146e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
147e1051a39Sopenharmony_ci
148e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
149e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
150e1051a39Sopenharmony_ci
151e1051a39Sopenharmony_cistatic int test_uint64(void)
152e1051a39Sopenharmony_ci{
153e1051a39Sopenharmony_ci    const unsigned char *p = t_invalid_zero;
154e1051a39Sopenharmony_ci    ASN1_UINT64_DATA *dectst =
155e1051a39Sopenharmony_ci        d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_ci    if (dectst == NULL)
158e1051a39Sopenharmony_ci        return 0;                /* Fail */
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_ci    ASN1_UINT64_DATA_free(dectst);
161e1051a39Sopenharmony_ci    return 1;
162e1051a39Sopenharmony_ci}
163e1051a39Sopenharmony_ci
164e1051a39Sopenharmony_citypedef struct {
165e1051a39Sopenharmony_ci    ASN1_STRING *invalidDirString;
166e1051a39Sopenharmony_ci} INVALIDTEMPLATE;
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_ciASN1_SEQUENCE(INVALIDTEMPLATE) = {
169e1051a39Sopenharmony_ci    /*
170e1051a39Sopenharmony_ci     * DirectoryString is a CHOICE type so it must use explicit tagging -
171e1051a39Sopenharmony_ci     * but we deliberately use implicit here, which makes this template invalid.
172e1051a39Sopenharmony_ci     */
173e1051a39Sopenharmony_ci    ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
174e1051a39Sopenharmony_ci} static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
175e1051a39Sopenharmony_ci
176e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
177e1051a39Sopenharmony_ciIMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
178e1051a39Sopenharmony_ci
179e1051a39Sopenharmony_ci/* Empty sequence for invalid template test */
180e1051a39Sopenharmony_cistatic unsigned char t_invalid_template[] = {
181e1051a39Sopenharmony_ci    0x30, 0x03,                  /* SEQUENCE tag + length */
182e1051a39Sopenharmony_ci    0x0c, 0x01, 0x41             /* UTF8String, length 1, "A" */
183e1051a39Sopenharmony_ci};
184e1051a39Sopenharmony_ci
185e1051a39Sopenharmony_cistatic int test_invalid_template(void)
186e1051a39Sopenharmony_ci{
187e1051a39Sopenharmony_ci    const unsigned char *p = t_invalid_template;
188e1051a39Sopenharmony_ci    INVALIDTEMPLATE *tmp = d2i_INVALIDTEMPLATE(NULL, &p,
189e1051a39Sopenharmony_ci                                               sizeof(t_invalid_template));
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_ci    /* We expect a NULL pointer return */
192e1051a39Sopenharmony_ci    if (TEST_ptr_null(tmp))
193e1051a39Sopenharmony_ci        return 1;
194e1051a39Sopenharmony_ci
195e1051a39Sopenharmony_ci    INVALIDTEMPLATE_free(tmp);
196e1051a39Sopenharmony_ci    return 0;
197e1051a39Sopenharmony_ci}
198e1051a39Sopenharmony_ci
199e1051a39Sopenharmony_cistatic int test_reuse_asn1_object(void)
200e1051a39Sopenharmony_ci{
201e1051a39Sopenharmony_ci    static unsigned char cn_der[] = { 0x06, 0x03, 0x55, 0x04, 0x06 };
202e1051a39Sopenharmony_ci    static unsigned char oid_der[] = {
203e1051a39Sopenharmony_ci        0x06, 0x06, 0x2a, 0x03, 0x04, 0x05, 0x06, 0x07
204e1051a39Sopenharmony_ci    };
205e1051a39Sopenharmony_ci    int ret = 0;
206e1051a39Sopenharmony_ci    ASN1_OBJECT *obj;
207e1051a39Sopenharmony_ci    unsigned char const *p = oid_der;
208e1051a39Sopenharmony_ci
209e1051a39Sopenharmony_ci    /* Create an object that owns dynamically allocated 'sn' and 'ln' fields */
210e1051a39Sopenharmony_ci
211e1051a39Sopenharmony_ci    if (!TEST_ptr(obj = ASN1_OBJECT_create(NID_undef, cn_der, sizeof(cn_der),
212e1051a39Sopenharmony_ci                                           "C", "countryName")))
213e1051a39Sopenharmony_ci        goto err;
214e1051a39Sopenharmony_ci    /* reuse obj - this should not leak sn and ln */
215e1051a39Sopenharmony_ci    if (!TEST_ptr(d2i_ASN1_OBJECT(&obj, &p, sizeof(oid_der))))
216e1051a39Sopenharmony_ci        goto err;
217e1051a39Sopenharmony_ci    ret = 1;
218e1051a39Sopenharmony_cierr:
219e1051a39Sopenharmony_ci    ASN1_OBJECT_free(obj);
220e1051a39Sopenharmony_ci    return ret;
221e1051a39Sopenharmony_ci}
222e1051a39Sopenharmony_ci
223e1051a39Sopenharmony_ciint setup_tests(void)
224e1051a39Sopenharmony_ci{
225e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
226e1051a39Sopenharmony_ci    ADD_TEST(test_long);
227e1051a39Sopenharmony_ci#endif
228e1051a39Sopenharmony_ci    ADD_TEST(test_int32);
229e1051a39Sopenharmony_ci    ADD_TEST(test_uint32);
230e1051a39Sopenharmony_ci    ADD_TEST(test_int64);
231e1051a39Sopenharmony_ci    ADD_TEST(test_uint64);
232e1051a39Sopenharmony_ci    ADD_TEST(test_invalid_template);
233e1051a39Sopenharmony_ci    ADD_TEST(test_reuse_asn1_object);
234e1051a39Sopenharmony_ci    return 1;
235e1051a39Sopenharmony_ci}
236