1 /* $Id: test-cert.c,v 1.15 2021/10/26 16:59:54 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 verbose; 36 37 int 38 main(int argc, char *argv[]) 39 { 40 int c, i, verb = 0, ta = 0; 41 X509 *xp = NULL; 42 struct cert *p; 43 44 ERR_load_crypto_strings(); 45 OpenSSL_add_all_ciphers(); 46 OpenSSL_add_all_digests(); 47 48 while ((c = getopt(argc, argv, "tv")) != -1) 49 switch (c) { 50 case 't': 51 ta = 1; 52 break; 53 case 'v': 54 verb++; 55 break; 56 default: 57 errx(1, "bad argument %c", c); 58 } 59 60 argv += optind; 61 argc -= optind; 62 63 if (argc == 0) 64 errx(1, "argument missing"); 65 66 if (ta) { 67 if (argc % 2) 68 errx(1, "need even number of arguments"); 69 70 for (i = 0; i < argc; i += 2) { 71 const char *cert_path = argv[i]; 72 const char *tal_path = argv[i + 1]; 73 char *buf; 74 size_t len; 75 struct tal *tal; 76 77 buf = load_file(tal_path, &len); 78 tal = tal_parse(tal_path, buf, len); 79 free(buf); 80 if (tal == NULL) 81 break; 82 83 buf = load_file(cert_path, &len); 84 p = ta_parse(&xp, cert_path, buf, len, 85 tal->pkey, tal->pkeysz); 86 free(buf); 87 tal_free(tal); 88 if (p == NULL) 89 break; 90 91 if (verb) 92 cert_print(p); 93 cert_free(p); 94 X509_free(xp); 95 } 96 } else { 97 for (i = 0; i < argc; i++) { 98 char *buf; 99 size_t len; 100 101 buf = load_file(argv[i], &len); 102 p = cert_parse(&xp, argv[i], buf, len); 103 if (p == NULL) 104 break; 105 if (verb) 106 cert_print(p); 107 free(buf); 108 cert_free(p); 109 X509_free(xp); 110 } 111 } 112 113 EVP_cleanup(); 114 CRYPTO_cleanup_all_ex_data(); 115 ERR_free_strings(); 116 117 if (i < argc) 118 errx(1, "test failed for %s", argv[i]); 119 120 printf("OK\n"); 121 return 0; 122 } 123