1 /*	$Id: test-roa.c,v 1.10 2021/03/29 15:47:34 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 <assert.h>
19 #include <err.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include <openssl/err.h>
27 #include <openssl/evp.h>
28 #include <openssl/pem.h>
29 #include <openssl/x509v3.h>
30 
31 #include "extern.h"
32 
33 #include "test-common.c"
34 
35 int verbose;
36 
37 static void
38 roa_print(const struct roa *p)
39 {
40 	char	 buf[128];
41 	size_t	 i;
42 
43 	assert(p != NULL);
44 
45 	printf("Subject key identifier: %s\n", pretty_key_id(p->ski));
46 	printf("Authority key identifier: %s\n", pretty_key_id(p->aki));
47 	printf("Authority info access: %s\n", p->aia);
48 	printf("asID: %" PRIu32 "\n", p->asid);
49 	for (i = 0; i < p->ipsz; i++) {
50 		ip_addr_print(&p->ips[i].addr,
51 			p->ips[i].afi, buf, sizeof(buf));
52 		printf("%5zu: %s (max: %zu)\n", i + 1,
53 			buf, p->ips[i].maxlength);
54 	}
55 }
56 
57 int
58 main(int argc, char *argv[])
59 {
60 	int		 c, i, ppem = 0, verb = 0;
61 	BIO		*bio_out = NULL;
62 	X509		*xp = NULL;
63 	struct roa	*p;
64 
65 
66 	ERR_load_crypto_strings();
67 	OpenSSL_add_all_ciphers();
68 	OpenSSL_add_all_digests();
69 
70 	while ((c = getopt(argc, argv, "pv")) != -1)
71 		switch (c) {
72 		case 'p':
73 			if (ppem)
74 				break;
75 			ppem = 1;
76 			if ((bio_out = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL)
77 				errx(1, "BIO_new_fp");
78 			break;
79 		case 'v':
80 			verb++;
81 			break;
82 		default:
83 			errx(1, "bad argument %c", c);
84 		}
85 
86 	argv += optind;
87 	argc -= optind;
88 
89 	if (argc == 0)
90 		errx(1, "argument missing");
91 
92 	for (i = 0; i < argc; i++) {
93 		if ((p = roa_parse(&xp, argv[i])) == NULL)
94 			break;
95 		if (verb)
96 			roa_print(p);
97 		if (ppem) {
98 			if (!PEM_write_bio_X509(bio_out, xp))
99 				errx(1,
100 				    "PEM_write_bio_X509: unable to write cert");
101 		}
102 		roa_free(p);
103 		X509_free(xp);
104 	}
105 
106 	BIO_free(bio_out);
107 	EVP_cleanup();
108 	CRYPTO_cleanup_all_ex_data();
109 	ERR_free_strings();
110 
111 	if (i < argc)
112 		errx(1, "test failed for %s", argv[i]);
113 
114 	printf("OK\n");
115 	return 0;
116 }
117