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