1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /* Internal tests for the x509 and x509v3 modules */
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubert #include <stdio.h>
13*e0c4386eSCy Schubert #include <string.h>
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert #include <openssl/x509.h>
16*e0c4386eSCy Schubert #include <openssl/x509v3.h>
17*e0c4386eSCy Schubert #include "testutil.h"
18*e0c4386eSCy Schubert #include "internal/nelem.h"
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubert /**********************************************************************
21*e0c4386eSCy Schubert *
22*e0c4386eSCy Schubert * Test of x509v3
23*e0c4386eSCy Schubert *
24*e0c4386eSCy Schubert ***/
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubert #include "../crypto/x509/ext_dat.h"
27*e0c4386eSCy Schubert #include "../crypto/x509/standard_exts.h"
28*e0c4386eSCy Schubert
test_standard_exts(void)29*e0c4386eSCy Schubert static int test_standard_exts(void)
30*e0c4386eSCy Schubert {
31*e0c4386eSCy Schubert size_t i;
32*e0c4386eSCy Schubert int prev = -1, good = 1;
33*e0c4386eSCy Schubert const X509V3_EXT_METHOD **tmp;
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert tmp = standard_exts;
36*e0c4386eSCy Schubert for (i = 0; i < OSSL_NELEM(standard_exts); i++, tmp++) {
37*e0c4386eSCy Schubert if ((*tmp)->ext_nid < prev)
38*e0c4386eSCy Schubert good = 0;
39*e0c4386eSCy Schubert prev = (*tmp)->ext_nid;
40*e0c4386eSCy Schubert
41*e0c4386eSCy Schubert }
42*e0c4386eSCy Schubert if (!good) {
43*e0c4386eSCy Schubert tmp = standard_exts;
44*e0c4386eSCy Schubert TEST_error("Extensions out of order!");
45*e0c4386eSCy Schubert for (i = 0; i < STANDARD_EXTENSION_COUNT; i++, tmp++)
46*e0c4386eSCy Schubert TEST_note("%d : %s", (*tmp)->ext_nid, OBJ_nid2sn((*tmp)->ext_nid));
47*e0c4386eSCy Schubert }
48*e0c4386eSCy Schubert return good;
49*e0c4386eSCy Schubert }
50*e0c4386eSCy Schubert
51*e0c4386eSCy Schubert typedef struct {
52*e0c4386eSCy Schubert const char *ipasc;
53*e0c4386eSCy Schubert const char *data;
54*e0c4386eSCy Schubert int length;
55*e0c4386eSCy Schubert } IP_TESTDATA;
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert static IP_TESTDATA a2i_ipaddress_tests[] = {
58*e0c4386eSCy Schubert {"127.0.0.1", "\x7f\x00\x00\x01", 4},
59*e0c4386eSCy Schubert {"1.2.3.4", "\x01\x02\x03\x04", 4},
60*e0c4386eSCy Schubert {"1.2.3.255", "\x01\x02\x03\xff", 4},
61*e0c4386eSCy Schubert {"1.2.3", NULL, 0},
62*e0c4386eSCy Schubert {"1.2.3 .4", NULL, 0},
63*e0c4386eSCy Schubert
64*e0c4386eSCy Schubert {"::1", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16},
65*e0c4386eSCy Schubert {"1:1:1:1:1:1:1:1", "\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01", 16},
66*e0c4386eSCy Schubert {"2001:db8::ff00:42:8329", "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\xff\x00\x00\x42\x83\x29", 16},
67*e0c4386eSCy Schubert {"1:1:1:1:1:1:1:1.test", NULL, 0},
68*e0c4386eSCy Schubert {":::1", NULL, 0},
69*e0c4386eSCy Schubert {"2001::123g", NULL, 0},
70*e0c4386eSCy Schubert
71*e0c4386eSCy Schubert {"example.test", NULL, 0},
72*e0c4386eSCy Schubert {"", NULL, 0},
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert {"1.2.3.4 ", "\x01\x02\x03\x04", 4},
75*e0c4386eSCy Schubert {" 1.2.3.4", "\x01\x02\x03\x04", 4},
76*e0c4386eSCy Schubert {" 1.2.3.4 ", "\x01\x02\x03\x04", 4},
77*e0c4386eSCy Schubert {"1.2.3.4.example.test", NULL, 0},
78*e0c4386eSCy Schubert };
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert
test_a2i_ipaddress(int idx)81*e0c4386eSCy Schubert static int test_a2i_ipaddress(int idx)
82*e0c4386eSCy Schubert {
83*e0c4386eSCy Schubert int good = 1;
84*e0c4386eSCy Schubert ASN1_OCTET_STRING *ip;
85*e0c4386eSCy Schubert int len = a2i_ipaddress_tests[idx].length;
86*e0c4386eSCy Schubert
87*e0c4386eSCy Schubert ip = a2i_IPADDRESS(a2i_ipaddress_tests[idx].ipasc);
88*e0c4386eSCy Schubert if (len == 0) {
89*e0c4386eSCy Schubert if (!TEST_ptr_null(ip)) {
90*e0c4386eSCy Schubert good = 0;
91*e0c4386eSCy Schubert TEST_note("'%s' should not be parsed as IP address", a2i_ipaddress_tests[idx].ipasc);
92*e0c4386eSCy Schubert }
93*e0c4386eSCy Schubert } else {
94*e0c4386eSCy Schubert if (!TEST_ptr(ip)
95*e0c4386eSCy Schubert || !TEST_int_eq(ASN1_STRING_length(ip), len)
96*e0c4386eSCy Schubert || !TEST_mem_eq(ASN1_STRING_get0_data(ip), len,
97*e0c4386eSCy Schubert a2i_ipaddress_tests[idx].data, len)) {
98*e0c4386eSCy Schubert good = 0;
99*e0c4386eSCy Schubert }
100*e0c4386eSCy Schubert }
101*e0c4386eSCy Schubert ASN1_OCTET_STRING_free(ip);
102*e0c4386eSCy Schubert return good;
103*e0c4386eSCy Schubert }
104*e0c4386eSCy Schubert
setup_tests(void)105*e0c4386eSCy Schubert int setup_tests(void)
106*e0c4386eSCy Schubert {
107*e0c4386eSCy Schubert ADD_TEST(test_standard_exts);
108*e0c4386eSCy Schubert ADD_ALL_TESTS(test_a2i_ipaddress, OSSL_NELEM(a2i_ipaddress_tests));
109*e0c4386eSCy Schubert return 1;
110*e0c4386eSCy Schubert }
111