xref: /freebsd/crypto/openssl/test/x509_internal_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*
2  * Copyright 2016-2021 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 /* 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 #include "../crypto/x509/ext_dat.h"
27 #include "../crypto/x509/standard_exts.h"
28 
test_standard_exts(void)29 static int test_standard_exts(void)
30 {
31     size_t i;
32     int prev = -1, good = 1;
33     const X509V3_EXT_METHOD **tmp;
34 
35     tmp = standard_exts;
36     for (i = 0; i < OSSL_NELEM(standard_exts); i++, tmp++) {
37         if ((*tmp)->ext_nid < prev)
38             good = 0;
39         prev = (*tmp)->ext_nid;
40 
41     }
42     if (!good) {
43         tmp = standard_exts;
44         TEST_error("Extensions out of order!");
45         for (i = 0; i < STANDARD_EXTENSION_COUNT; i++, tmp++)
46             TEST_note("%d : %s", (*tmp)->ext_nid, OBJ_nid2sn((*tmp)->ext_nid));
47     }
48     return good;
49 }
50 
51 typedef struct {
52     const char *ipasc;
53     const char *data;
54     int length;
55 } IP_TESTDATA;
56 
57 static IP_TESTDATA a2i_ipaddress_tests[] = {
58     {"127.0.0.1", "\x7f\x00\x00\x01", 4},
59     {"1.2.3.4", "\x01\x02\x03\x04", 4},
60     {"1.2.3.255", "\x01\x02\x03\xff", 4},
61     {"1.2.3", NULL, 0},
62     {"1.2.3 .4", NULL, 0},
63 
64     {"::1", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16},
65     {"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     {"2001:db8::ff00:42:8329", "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\xff\x00\x00\x42\x83\x29", 16},
67     {"1:1:1:1:1:1:1:1.test", NULL, 0},
68     {":::1", NULL, 0},
69     {"2001::123g", NULL, 0},
70 
71     {"example.test", NULL, 0},
72     {"", NULL, 0},
73 
74     {"1.2.3.4 ", "\x01\x02\x03\x04", 4},
75     {" 1.2.3.4", "\x01\x02\x03\x04", 4},
76     {" 1.2.3.4 ", "\x01\x02\x03\x04", 4},
77     {"1.2.3.4.example.test", NULL, 0},
78 };
79 
80 
test_a2i_ipaddress(int idx)81 static int test_a2i_ipaddress(int idx)
82 {
83     int good = 1;
84     ASN1_OCTET_STRING *ip;
85     int len = a2i_ipaddress_tests[idx].length;
86 
87     ip = a2i_IPADDRESS(a2i_ipaddress_tests[idx].ipasc);
88     if (len == 0) {
89         if (!TEST_ptr_null(ip)) {
90             good = 0;
91             TEST_note("'%s' should not be parsed as IP address", a2i_ipaddress_tests[idx].ipasc);
92         }
93     } else {
94         if (!TEST_ptr(ip)
95             || !TEST_int_eq(ASN1_STRING_length(ip), len)
96             || !TEST_mem_eq(ASN1_STRING_get0_data(ip), len,
97                             a2i_ipaddress_tests[idx].data, len)) {
98             good = 0;
99         }
100     }
101     ASN1_OCTET_STRING_free(ip);
102     return good;
103 }
104 
setup_tests(void)105 int setup_tests(void)
106 {
107     ADD_TEST(test_standard_exts);
108     ADD_ALL_TESTS(test_a2i_ipaddress, OSSL_NELEM(a2i_ipaddress_tests));
109     return 1;
110 }
111