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