1 /*	$Id: test-gbr.c,v 1.3 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 gbr_print(const struct gbr *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("vcard:\n%s", p->vcard);
49 }
50 
51 int
52 main(int argc, char *argv[])
53 {
54 	int		 c, i, ppem = 0, verb = 0;
55 	BIO		*bio_out = NULL;
56 	X509		*xp = NULL;
57 	struct gbr	*p;
58 
59 
60 	ERR_load_crypto_strings();
61 	OpenSSL_add_all_ciphers();
62 	OpenSSL_add_all_digests();
63 
64 	while ((c = getopt(argc, argv, "pv")) != -1)
65 		switch (c) {
66 		case 'p':
67 			if (ppem)
68 				break;
69 			ppem = 1;
70 			if ((bio_out = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL)
71 				errx(1, "BIO_new_fp");
72 			break;
73 		case 'v':
74 			verb++;
75 			break;
76 		default:
77 			errx(1, "bad argument %c", c);
78 		}
79 
80 	argv += optind;
81 	argc -= optind;
82 
83 	if (argc == 0)
84 		errx(1, "argument missing");
85 
86 	for (i = 0; i < argc; i++) {
87 		if ((p = gbr_parse(&xp, argv[i])) == NULL)
88 			break;
89 		if (verb)
90 			gbr_print(p);
91 		if (ppem) {
92 			if (!PEM_write_bio_X509(bio_out, xp))
93 				errx(1,
94 				    "PEM_write_bio_X509: unable to write cert");
95 		}
96 		gbr_free(p);
97 		X509_free(xp);
98 	}
99 
100 	BIO_free(bio_out);
101 	EVP_cleanup();
102 	CRYPTO_cleanup_all_ex_data();
103 	ERR_free_strings();
104 
105 	if (i < argc)
106 		errx(1, "test failed for %s", argv[i]);
107 
108 	printf("OK\n");
109 	return 0;
110 }
111