1*5af82120Sclaudio /*	$Id: test-geofeed.c,v 1.6 2024/04/22 05:54:01 claudio Exp $ */
29a2ff1ebStb /*
39a2ff1ebStb  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
49a2ff1ebStb  *
59a2ff1ebStb  * Permission to use, copy, modify, and distribute this software for any
69a2ff1ebStb  * purpose with or without fee is hereby granted, provided that the above
79a2ff1ebStb  * copyright notice and this permission notice appear in all copies.
89a2ff1ebStb  *
99a2ff1ebStb  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
109a2ff1ebStb  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119a2ff1ebStb  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
129a2ff1ebStb  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139a2ff1ebStb  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149a2ff1ebStb  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
159a2ff1ebStb  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
169a2ff1ebStb  */
179a2ff1ebStb 
189a2ff1ebStb #include <assert.h>
199a2ff1ebStb #include <err.h>
209a2ff1ebStb #include <inttypes.h>
219a2ff1ebStb #include <stdio.h>
229a2ff1ebStb #include <stdlib.h>
239a2ff1ebStb #include <string.h>
249a2ff1ebStb #include <unistd.h>
259a2ff1ebStb 
269a2ff1ebStb #include <openssl/err.h>
279a2ff1ebStb #include <openssl/evp.h>
289a2ff1ebStb #include <openssl/pem.h>
299a2ff1ebStb #include <openssl/x509v3.h>
309a2ff1ebStb 
319a2ff1ebStb #include "extern.h"
329a2ff1ebStb 
339a2ff1ebStb int outformats;
349a2ff1ebStb int verbose;
359a2ff1ebStb int filemode;
36*5af82120Sclaudio int experimental;
379a2ff1ebStb 
389a2ff1ebStb int
main(int argc,char * argv[])399a2ff1ebStb main(int argc, char *argv[])
409a2ff1ebStb {
419a2ff1ebStb 	int		 c, i, ppem = 0, verb = 0;
429a2ff1ebStb 	X509		*xp = NULL;
439a2ff1ebStb 	struct geofeed	*p;
449a2ff1ebStb 	unsigned char	*buf;
459a2ff1ebStb 	size_t		 len;
469a2ff1ebStb 
479a2ff1ebStb 
489a2ff1ebStb 	ERR_load_crypto_strings();
499a2ff1ebStb 	OpenSSL_add_all_ciphers();
509a2ff1ebStb 	OpenSSL_add_all_digests();
519a2ff1ebStb 	x509_init_oid();
529a2ff1ebStb 
539a2ff1ebStb 	while ((c = getopt(argc, argv, "pv")) != -1)
549a2ff1ebStb 		switch (c) {
559a2ff1ebStb 		case 'p':
569a2ff1ebStb 			if (ppem)
579a2ff1ebStb 				break;
589a2ff1ebStb 			ppem = 1;
599a2ff1ebStb 			break;
609a2ff1ebStb 		case 'v':
619a2ff1ebStb 			verb++;
629a2ff1ebStb 			break;
639a2ff1ebStb 		default:
649a2ff1ebStb 			errx(1, "bad argument %c", c);
659a2ff1ebStb 		}
669a2ff1ebStb 
679a2ff1ebStb 	argv += optind;
689a2ff1ebStb 	argc -= optind;
699a2ff1ebStb 
709a2ff1ebStb 	if (argc == 0)
719a2ff1ebStb 		errx(1, "argument missing");
729a2ff1ebStb 
739a2ff1ebStb 	for (i = 0; i < argc; i++) {
749a2ff1ebStb 		buf = load_file(argv[i], &len);
758e9035a6Stb 		if ((p = geofeed_parse(&xp, argv[i], -1, buf, len)) == NULL) {
769a2ff1ebStb 			free(buf);
779a2ff1ebStb 			break;
789a2ff1ebStb 		}
799a2ff1ebStb 		if (verb)
809a2ff1ebStb 			geofeed_print(xp, p);
819a2ff1ebStb 		if (ppem) {
829a2ff1ebStb 			if (!PEM_write_X509(stdout, xp))
839a2ff1ebStb 				errx(1, "PEM_write_X509: unable to write cert");
849a2ff1ebStb 		}
859a2ff1ebStb 		free(buf);
869a2ff1ebStb 		geofeed_free(p);
879a2ff1ebStb 		X509_free(xp);
889a2ff1ebStb 	}
899a2ff1ebStb 
909a2ff1ebStb 	EVP_cleanup();
919a2ff1ebStb 	CRYPTO_cleanup_all_ex_data();
929a2ff1ebStb 	ERR_free_strings();
939a2ff1ebStb 
949a2ff1ebStb 	if (i < argc)
959a2ff1ebStb 		errx(1, "test failed for %s", argv[i]);
969a2ff1ebStb 
979a2ff1ebStb 	printf("OK\n");
989a2ff1ebStb 	return 0;
999a2ff1ebStb }
1000876134dSclaudio 
1010876134dSclaudio time_t
get_current_time(void)1020876134dSclaudio get_current_time(void)
1030876134dSclaudio {
1040876134dSclaudio 	return time(NULL);
1050876134dSclaudio }
106