1 /*	$Id: test-ip.c,v 1.9 2023/05/30 12:14:48 claudio Exp $ */
2 /*
3  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 
21 #include <assert.h>
22 #include <err.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include <openssl/err.h>
29 #include <openssl/evp.h>
30 #include <openssl/x509v3.h>
31 
32 #include "extern.h"
33 
34 int outformats;
35 int verbose;
36 int filemode;
37 
38 static void
39 test(const char *res, uint16_t afiv, size_t sz, size_t unused, ...)
40 {
41 	va_list		 ap;
42 	struct ip_addr	 addr;
43 	char		 buf[64];
44 	size_t		 i;
45 	enum afi	 afi;
46 	struct cert_ip	 ip;
47 	int		 rc;
48 
49 	afi = (afiv == 1) ? AFI_IPV4 : AFI_IPV6;
50 
51 	memset(&addr, 0, sizeof(struct ip_addr));
52 
53 	va_start(ap, unused);
54 	for (i = 0; i < sz; i++)
55 		addr.addr[i] = (unsigned char)va_arg(ap, int);
56 	va_end(ap);
57 
58 	addr.prefixlen = sz * 8 - unused;
59 	ip_addr_print(&addr, afi, buf, sizeof(buf));
60 	if (res != NULL && strcmp(res, buf))
61 		errx(EXIT_FAILURE, "fail: %s != %s\n", res, buf);
62 	else if (res != NULL)
63 		warnx("pass: %s", buf);
64 	else
65 		warnx("check: %s", buf);
66 
67 	ip.afi = afi;
68 	ip.type = CERT_IP_ADDR;
69 	ip.ip = addr;
70 	rc = ip_cert_compose_ranges(&ip);
71 
72 	inet_ntop((afiv == 1) ? AF_INET : AF_INET6, ip.min, buf, sizeof(buf));
73 	warnx("minimum: %s", buf);
74 	inet_ntop((afiv == 1) ? AF_INET : AF_INET6, ip.max, buf, sizeof(buf));
75 	warnx("maximum: %s", buf);
76 	if (!rc)
77 		errx(EXIT_FAILURE, "fail: minimum > maximum");
78 }
79 
80 int
81 main(int argc, char *argv[])
82 {
83 	ERR_load_crypto_strings();
84 	OpenSSL_add_all_ciphers();
85 	OpenSSL_add_all_digests();
86 
87 	test("10.5.0.4/32",
88 	     1, 0x04, 0x00, 0x0a, 0x05, 0x00, 0x04);
89 
90 	test("10.5.0.0/23",
91 	     1, 0x03, 0x01, 0x0a, 0x05, 0x00);
92 
93 	test("2001:0:200:3::1/128",
94 	     2, 0x10, 0x00, 0x20, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x03,
95                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
96 
97 	test("2001:0:200::/39",
98 	     2, 0x05, 0x01, 0x20, 0x01, 0x00, 0x00, 0x02);
99 
100 	test("10.5.0.0/16",
101 	     1, 0x02, 0x00, 0x0a, 0x05);
102 
103 	test("10.5.0.0/23",
104 	     1, 0x03, 0x01, 0x0a, 0x05, 0x00);
105 
106 	test("2001:0:200::/39",
107 	     2, 0x05, 0x01, 0x20, 0x01, 0x00, 0x00, 0x02);
108 
109 	test("2001::/38",
110 	     2, 0x05, 0x02, 0x20, 0x01, 0x00, 0x00, 0x00);
111 
112 	test("0.0.0.0/0",
113 	     1, 0x00, 0x00);
114 
115 	test("10.64.0.0/12",
116 	     1, 0x02, 0x04, 0x0a, 0x40);
117 
118 	test("10.64.0.0/20",
119 	     1, 0x03, 0x04, 0x0a, 0x40, 0x00);
120 
121 	test("128.0.0.0/4",
122 	     1, 0x01, 0x04, 0x80);
123 	test("129.64.0.0/10",
124 	     1, 0x02, 0x06, 0x81, 0x40);
125 
126 	ERR_free_strings();
127 
128 	printf("OK\n");
129 	return 0;
130 }
131 
132 time_t
133 get_current_time(void)
134 {
135 	return time(NULL);
136 }
137