1 /* $Id: test-cert.c,v 1.24 2024/04/22 05:54:01 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 <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <openssl/err.h>
30 #include <openssl/evp.h>
31 #include <openssl/x509v3.h>
32
33 #include "extern.h"
34
35 int outformats;
36 int verbose;
37 int filemode;
38 int experimental;
39
40 int
main(int argc,char * argv[])41 main(int argc, char *argv[])
42 {
43 int c, i, verb = 0, ta = 0;
44 struct cert *p;
45
46 ERR_load_crypto_strings();
47 OpenSSL_add_all_ciphers();
48 OpenSSL_add_all_digests();
49 x509_init_oid();
50
51 while ((c = getopt(argc, argv, "tv")) != -1)
52 switch (c) {
53 case 't':
54 ta = 1;
55 break;
56 case 'v':
57 verb++;
58 break;
59 default:
60 errx(1, "bad argument %c", c);
61 }
62
63 argv += optind;
64 argc -= optind;
65
66 if (argc == 0)
67 errx(1, "argument missing");
68
69 if (ta) {
70 if (argc % 2)
71 errx(1, "need even number of arguments");
72
73 for (i = 0; i < argc; i += 2) {
74 const char *cert_path = argv[i];
75 const char *tal_path = argv[i + 1];
76 char *buf;
77 size_t len;
78 struct tal *tal;
79
80 buf = load_file(tal_path, &len);
81 tal = tal_parse(tal_path, buf, len);
82 free(buf);
83 if (tal == NULL)
84 break;
85
86 buf = load_file(cert_path, &len);
87 p = cert_parse_pre(cert_path, buf, len);
88 free(buf);
89 if (p == NULL)
90 break;
91 p = ta_parse(cert_path, p, tal->pkey, tal->pkeysz);
92 tal_free(tal);
93 if (p == NULL)
94 break;
95
96 if (verb)
97 cert_print(p);
98 cert_free(p);
99 }
100 } else {
101 for (i = 0; i < argc; i++) {
102 char *buf;
103 size_t len;
104
105 buf = load_file(argv[i], &len);
106 p = cert_parse_pre(argv[i], buf, len);
107 free(buf);
108 if (p == NULL)
109 break;
110 p = cert_parse(argv[i], p);
111 if (p == NULL)
112 break;
113 if (verb)
114 cert_print(p);
115 cert_free(p);
116 }
117 }
118
119 EVP_cleanup();
120 CRYPTO_cleanup_all_ex_data();
121 ERR_free_strings();
122
123 if (i < argc)
124 errx(1, "test failed for %s", argv[i]);
125
126 printf("OK\n");
127 return 0;
128 }
129
130 time_t
get_current_time(void)131 get_current_time(void)
132 {
133 return time(NULL);
134 }
135