summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2026-05-21 13:14:57 +0000
committertb <>2026-05-21 13:14:57 +0000
commite99a1fd414153ce578bc57bf5bb1ed0a0968dabc (patch)
treeb184549b3cc094e5dff9e332fe81a0443c1a6ce6
parent73dc5c5d171f6cc40ff81af7988ff42243580ee2 (diff)
downloadopenbsd-e99a1fd414153ce578bc57bf5bb1ed0a0968dabc.tar.gz
openbsd-e99a1fd414153ce578bc57bf5bb1ed0a0968dabc.tar.bz2
openbsd-e99a1fd414153ce578bc57bf5bb1ed0a0968dabc.zip
rfc3779 test: exercise IPAddressFamily_cmp a bit more
This populates an IPAddrBlocks object with not all that sensible data and tests behavior of serialization and deserialization of this thing. Prior to x509_addr.c rev 1.96 this would call memcmp() on NULL.
-rw-r--r--src/regress/lib/libcrypto/x509/rfc3779/rfc3779.c169
1 files changed, 168 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/x509/rfc3779/rfc3779.c b/src/regress/lib/libcrypto/x509/rfc3779/rfc3779.c
index 61556f096f..f811749461 100644
--- a/src/regress/lib/libcrypto/x509/rfc3779/rfc3779.c
+++ b/src/regress/lib/libcrypto/x509/rfc3779/rfc3779.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rfc3779.c,v 1.12 2026/03/13 06:47:34 tb Exp $ */ 1/* $OpenBSD: rfc3779.c,v 1.13 2026/05/21 13:14:57 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2021 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2021 Theo Buehler <tb@openbsd.org>
4 * 4 *
@@ -2006,6 +2006,172 @@ run_ASIdentifiers_subset_test(void)
2006 return failed; 2006 return failed;
2007} 2007}
2008 2008
2009/*
2010 * IPAddressFamily_cmp() is well exercised with mostly valid data via canonize
2011 * and is_canonical. Squeeze invalid inheriting address families between valid
2012 * ones, then sort. This would previously call memcmp() on NULL.
2013 */
2014
2015static const uint8_t invalid_block[] = {
2016 0x30, 0x26,
2017 0x30, 0x04, 0x04, 0x00, 0x05, 0x00, /* Invalid empty AF. */
2018 0x30, 0x06, 0x04, 0x02, 0x00, 0x01, 0x05, 0x00, /* IPv4 */
2019 0x30, 0x06, 0x04, 0x02, 0x00, 0x02, 0x05, 0x00, /* IPv6 */
2020 0x30, 0x07, 0x04, 0x03, 0x00, 0x02, 0x04, 0x05, 0x00, /* IPv6 NLRI */
2021 0x30, 0x05, 0x04, 0x01, 0x01, 0x05, 0x00, /* invalid AF 0x01 */
2022};
2023static const size_t invalid_block_len = sizeof(invalid_block);
2024
2025static int
2026run_IPAddressFamily_cmp_ub_test(void)
2027{
2028 IPAddrBlocks *addrs;
2029 IPAddressFamily *af = NULL;
2030 const unsigned char *p;
2031 unsigned char *out = NULL;
2032 unsigned int nlri_safi = 4;
2033 int out_len = 0;
2034 int failed = 1;
2035
2036 if ((addrs = IPAddrBlocks_new()) == NULL) {
2037 fprintf(stderr, "%s: IPAddrBlocks_new\n", __func__);
2038 goto err;
2039 }
2040
2041 /*
2042 * Add IPv6 NLRI, inheriting
2043 */
2044 if (!X509v3_addr_add_inherit(addrs, IANA_AFI_IPV6, &nlri_safi)) {
2045 fprintf(stderr, "%s: X509v3_addr_add_inherit IPv6\n", __func__);
2046 goto err;
2047 }
2048
2049 /*
2050 * Add IPv6, inheriting
2051 */
2052 if (!X509v3_addr_add_inherit(addrs, IANA_AFI_IPV6, NULL)) {
2053 fprintf(stderr, "%s: X509v3_addr_add_inherit IPv6\n", __func__);
2054 goto err;
2055 }
2056
2057 /*
2058 * Add an inheriting IPAddressFamily with invalid empty addressFamily.
2059 */
2060
2061 if ((af = IPAddressFamily_new()) == NULL) {
2062 fprintf(stderr, "%s: IPAddressFamily_new\n", __func__);
2063 goto err;
2064 }
2065 if ((af->ipAddressChoice->u.inherit = ASN1_NULL_new()) == NULL) {
2066 fprintf(stderr, "%s: ASN1_NULL_new()\n", __func__);
2067 goto err;
2068 }
2069 af->ipAddressChoice->type = IPAddressChoice_inherit;
2070
2071 if (sk_IPAddressFamily_push(addrs, af) <= 0) {
2072 fprintf(stderr, "%s: sk_IPAddressFamily_push\n", __func__);
2073 goto err;
2074 }
2075 af = NULL;
2076
2077 /*
2078 * Add an inheriting IPAddressFamily with invalid addressFamily 0x01.
2079 */
2080
2081 if ((af = IPAddressFamily_new()) == NULL) {
2082 fprintf(stderr, "%s: IPAddressFamily_new\n", __func__);
2083 goto err;
2084 }
2085 if (!ASN1_OCTET_STRING_set(af->addressFamily, "\x01", 1)) {
2086 fprintf(stderr, "%s: ASN1_OCTET_STRING_set\n", __func__);
2087 goto err;
2088 }
2089 if ((af->ipAddressChoice->u.inherit = ASN1_NULL_new()) == NULL) {
2090 fprintf(stderr, "%s: ASN1_NULL_new()\n", __func__);
2091 goto err;
2092 }
2093 af->ipAddressChoice->type = IPAddressChoice_inherit;
2094
2095 if (sk_IPAddressFamily_push(addrs, af) <= 0) {
2096 fprintf(stderr, "%s: sk_IPAddressFamily_push\n", __func__);
2097 goto err;
2098 }
2099 af = NULL;
2100
2101 /*
2102 * Add IPv4, inheriting
2103 */
2104 if (!X509v3_addr_add_inherit(addrs, IANA_AFI_IPV4, NULL)) {
2105 fprintf(stderr, "%s: X509v3_addr_add_inherit IPv4\n", __func__);
2106 goto err;
2107 }
2108
2109 sk_IPAddressFamily_sort(addrs);
2110
2111 if (X509v3_addr_is_canonical(addrs)) {
2112 fprintf(stderr, "%s: X509v3_addr_is_canonical\n", __func__);
2113 goto err;
2114 }
2115
2116 if ((out_len = i2d_IPAddrBlocks(addrs, &out)) <= 0) {
2117 fprintf(stderr, "%s: i2d_IPAddrBlocks failed\n", __func__);
2118 goto err;
2119 }
2120
2121 if (invalid_block_len != (size_t)out_len) {
2122 fprintf(stderr, "%s: length: want %zu, got %d\n",
2123 __func__, invalid_block_len, out_len);
2124 goto err;
2125 }
2126
2127 if (memcmp(invalid_block, out, out_len) != 0) {
2128 report_hexdump(__func__, "invalid IPAddrBlock", "memcmp DER failed",
2129 invalid_block, invalid_block_len, out, out_len);
2130 goto err;
2131 }
2132
2133 /*
2134 * We can also parse this garbage.
2135 */
2136
2137 IPAddrBlocks_free(addrs);
2138 addrs = NULL;
2139
2140 p = invalid_block;
2141 if ((addrs = d2i_IPAddrBlocks(NULL, &p, invalid_block_len)) == NULL) {
2142 fprintf(stderr, "%s: d2i_IPAddrBlocks\n", __func__);
2143 goto err;
2144 }
2145
2146 freezero(out, out_len);
2147 out = NULL;
2148
2149 if ((out_len = i2d_IPAddrBlocks(addrs, &out)) <= 0) {
2150 fprintf(stderr, "%s: i2d_IPAddrBlocks (2) failed\n", __func__);
2151 goto err;
2152 }
2153
2154 if (invalid_block_len != (size_t)out_len) {
2155 fprintf(stderr, "%s: length (2): want %zu, got %d\n",
2156 __func__, invalid_block_len, out_len);
2157 goto err;
2158 }
2159
2160 if (memcmp(invalid_block, out, out_len) != 0) {
2161 report_hexdump(__func__, "invalid IPAddrBlock (2)", "memcmp DER failed",
2162 invalid_block, invalid_block_len, out, out_len);
2163 goto err;
2164 }
2165 failed = 0;
2166
2167 err:
2168 IPAddrBlocks_free(addrs);
2169 IPAddressFamily_free(af);
2170 freezero(out, out_len);
2171
2172 return failed;
2173}
2174
2009int 2175int
2010main(void) 2176main(void)
2011{ 2177{
@@ -2015,6 +2181,7 @@ main(void)
2015 failed |= run_IPAddrBlock_tests(); 2181 failed |= run_IPAddrBlock_tests();
2016 failed |= run_ASIdentifiers_build_test(); 2182 failed |= run_ASIdentifiers_build_test();
2017 failed |= run_ASIdentifiers_subset_test(); 2183 failed |= run_ASIdentifiers_subset_test();
2184 failed |= run_IPAddressFamily_cmp_ub_test();
2018 2185
2019 return failed; 2186 return failed;
2020} 2187}