Fix a crash in X509v3_asid_subset()
[openssl.git] / test / v3ext.c
1 /*
2  * Copyright 2016-2020 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 #include <stdio.h>
11 #include <openssl/x509.h>
12 #include <openssl/x509v3.h>
13 #include <openssl/pem.h>
14 #include <openssl/err.h>
15
16 #include "testutil.h"
17
18 static const char *infile;
19
20 static int test_pathlen(void)
21 {
22     X509 *x = NULL;
23     BIO *b = NULL;
24     long pathlen;
25     int ret = 0;
26
27     if (!TEST_ptr(b = BIO_new_file(infile, "r"))
28             || !TEST_ptr(x = PEM_read_bio_X509(b, NULL, NULL, NULL))
29             || !TEST_int_eq(pathlen = X509_get_pathlen(x), 6))
30         goto end;
31
32     ret = 1;
33
34 end:
35     BIO_free(b);
36     X509_free(x);
37     return ret;
38 }
39
40 static int test_asid(void)
41 {
42     ASN1_INTEGER *val1 = NULL, *val2 = NULL;
43     ASIdentifiers *asid1 = ASIdentifiers_new(), *asid2 = ASIdentifiers_new(),
44                   *asid3 = ASIdentifiers_new(), *asid4 = ASIdentifiers_new();
45     int testresult = 0;
46
47     if (!TEST_ptr(asid1)
48             || !TEST_ptr(asid2)
49             || !TEST_ptr(asid3))
50         goto err;
51
52     if (!TEST_ptr(val1 = ASN1_INTEGER_new())
53             || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496)))
54         goto err;
55
56     if (!TEST_true(X509v3_asid_add_id_or_range(asid1, V3_ASID_ASNUM, val1, NULL)))
57         goto err;
58
59     val1 = NULL;
60     if (!TEST_ptr(val2 = ASN1_INTEGER_new())
61             || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
62         goto err;
63
64     if (!TEST_true(X509v3_asid_add_id_or_range(asid2, V3_ASID_ASNUM, val2, NULL)))
65         goto err;
66
67     val2 = NULL;
68     if (!TEST_ptr(val1 = ASN1_INTEGER_new())
69             || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496))
70             || !TEST_ptr(val2 = ASN1_INTEGER_new())
71             || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
72         goto err;
73
74     /*
75      * Just tests V3_ASID_ASNUM for now. Could be extended at some point to also
76      * test V3_ASID_RDI if we think it is worth it.
77      */
78     if (!TEST_true(X509v3_asid_add_id_or_range(asid3, V3_ASID_ASNUM, val1, val2)))
79         goto err;
80     val1 = val2 = NULL;
81
82     /* Actual subsets */
83     if (!TEST_true(X509v3_asid_subset(NULL, NULL))
84             || !TEST_true(X509v3_asid_subset(NULL, asid1))
85             || !TEST_true(X509v3_asid_subset(asid1, asid1))
86             || !TEST_true(X509v3_asid_subset(asid2, asid2))
87             || !TEST_true(X509v3_asid_subset(asid1, asid3))
88             || !TEST_true(X509v3_asid_subset(asid2, asid3))
89             || !TEST_true(X509v3_asid_subset(asid3, asid3))
90             || !TEST_true(X509v3_asid_subset(asid4, asid1))
91             || !TEST_true(X509v3_asid_subset(asid4, asid2))
92             || !TEST_true(X509v3_asid_subset(asid4, asid3)))
93         goto err;
94
95     /* Not subsets */
96     if (!TEST_false(X509v3_asid_subset(asid1, NULL))
97             || !TEST_false(X509v3_asid_subset(asid1, asid2))
98             || !TEST_false(X509v3_asid_subset(asid2, asid1))
99             || !TEST_false(X509v3_asid_subset(asid3, asid1))
100             || !TEST_false(X509v3_asid_subset(asid3, asid2))
101             || !TEST_false(X509v3_asid_subset(asid1, asid4))
102             || !TEST_false(X509v3_asid_subset(asid2, asid4))
103             || !TEST_false(X509v3_asid_subset(asid3, asid4)))
104         goto err;
105
106     testresult = 1;
107  err:
108     ASN1_INTEGER_free(val1);
109     ASN1_INTEGER_free(val2);
110     ASIdentifiers_free(asid1);
111     ASIdentifiers_free(asid2);
112     ASIdentifiers_free(asid3);
113     ASIdentifiers_free(asid4);
114     return testresult;
115 }
116
117 OPT_TEST_DECLARE_USAGE("cert.pem\n")
118
119 int setup_tests(void)
120 {
121     if (!test_skip_common_options()) {
122         TEST_error("Error parsing test options\n");
123         return 0;
124     }
125
126     if (!TEST_ptr(infile = test_get_argument(0)))
127         return 0;
128
129     ADD_TEST(test_pathlen);
130     ADD_TEST(test_asid);
131     return 1;
132 }