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