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