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