xref: /openbsd-src/regress/usr.sbin/rpki-client/test-aspa.c (revision 5af821209fa3336ed87da6b82b004b70a12d2594)
1*5af82120Sclaudio /*	$Id: test-aspa.c,v 1.8 2024/04/22 05:54:01 claudio Exp $ */
2f8535127Sjob /*
3f8535127Sjob  * Copyright (c) 2022 Job Snijders <job@fastly.com>
4f8535127Sjob  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
5f8535127Sjob  *
6f8535127Sjob  * Permission to use, copy, modify, and distribute this software for any
7f8535127Sjob  * purpose with or without fee is hereby granted, provided that the above
8f8535127Sjob  * copyright notice and this permission notice appear in all copies.
9f8535127Sjob  *
10f8535127Sjob  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11f8535127Sjob  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12f8535127Sjob  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13f8535127Sjob  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14f8535127Sjob  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15f8535127Sjob  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16f8535127Sjob  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17f8535127Sjob  */
18f8535127Sjob 
19f8535127Sjob #include <assert.h>
20f8535127Sjob #include <err.h>
21f8535127Sjob #include <inttypes.h>
22f8535127Sjob #include <stdio.h>
23f8535127Sjob #include <stdlib.h>
24f8535127Sjob #include <string.h>
25f8535127Sjob #include <unistd.h>
26f8535127Sjob 
27f8535127Sjob #include <openssl/err.h>
28f8535127Sjob #include <openssl/evp.h>
29f8535127Sjob #include <openssl/pem.h>
30f8535127Sjob #include <openssl/x509v3.h>
31f8535127Sjob 
32f8535127Sjob #include "extern.h"
33f8535127Sjob 
34f8535127Sjob int outformats;
35f8535127Sjob int verbose;
36229518c6Sjob int filemode = 1;
37*5af82120Sclaudio int experimental;
38f8535127Sjob 
39f8535127Sjob int
main(int argc,char * argv[])40f8535127Sjob main(int argc, char *argv[])
41f8535127Sjob {
42f8535127Sjob 	int		 c, i, ppem = 0, verb = 0;
43f8535127Sjob 	X509		*xp = NULL;
44f8535127Sjob 	struct aspa	*p;
45f8535127Sjob 	unsigned char	*buf;
46f8535127Sjob 	size_t		 len;
47f8535127Sjob 
48f8535127Sjob 	ERR_load_crypto_strings();
49f8535127Sjob 	OpenSSL_add_all_ciphers();
50f8535127Sjob 	OpenSSL_add_all_digests();
51f8535127Sjob 	x509_init_oid();
52f8535127Sjob 
53f8535127Sjob 	while ((c = getopt(argc, argv, "pv")) != -1)
54f8535127Sjob 		switch (c) {
55f8535127Sjob 		case 'p':
56f8535127Sjob 			if (ppem)
57f8535127Sjob 				break;
58f8535127Sjob 			ppem = 1;
59f8535127Sjob 			break;
60f8535127Sjob 		case 'v':
61f8535127Sjob 			verb++;
62f8535127Sjob 			break;
63f8535127Sjob 		default:
64f8535127Sjob 			errx(1, "bad argument %c", c);
65f8535127Sjob 		}
66f8535127Sjob 
67f8535127Sjob 	argv += optind;
68f8535127Sjob 	argc -= optind;
69f8535127Sjob 
70f8535127Sjob 	if (argc == 0)
71f8535127Sjob 		errx(1, "argument missing");
72f8535127Sjob 
73f8535127Sjob 	for (i = 0; i < argc; i++) {
74f8535127Sjob 		buf = load_file(argv[i], &len);
758e9035a6Stb 		if ((p = aspa_parse(&xp, argv[i], -1, buf, len)) == NULL) {
76f8535127Sjob 			free(buf);
77f8535127Sjob 			break;
78f8535127Sjob 		}
79f8535127Sjob 		if (verb)
80f8535127Sjob 			aspa_print(xp, p);
81f8535127Sjob 		if (ppem) {
82f8535127Sjob 			if (!PEM_write_X509(stdout, xp))
83f8535127Sjob 				errx(1, "PEM_write_X509: unable to write cert");
84f8535127Sjob 		}
85f8535127Sjob 		free(buf);
86f8535127Sjob 		aspa_free(p);
87f8535127Sjob 		X509_free(xp);
88f8535127Sjob 	}
89f8535127Sjob 
90f8535127Sjob 	EVP_cleanup();
91f8535127Sjob 	CRYPTO_cleanup_all_ex_data();
92f8535127Sjob 	ERR_free_strings();
93f8535127Sjob 
94f8535127Sjob 	if (i < argc)
95f8535127Sjob 		errx(1, "test failed for %s", argv[i]);
96f8535127Sjob 
97f8535127Sjob 	printf("OK\n");
98f8535127Sjob 	return 0;
99f8535127Sjob }
1000876134dSclaudio 
1010876134dSclaudio time_t
get_current_time(void)1020876134dSclaudio get_current_time(void)
1030876134dSclaudio {
1040876134dSclaudio 	return time(NULL);
1050876134dSclaudio }
106