1 /* $Id: test-cert.c,v 1.9 2021/02/16 08:53:53 job 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 static void 38 cert_print(const struct cert *p) 39 { 40 size_t i; 41 char buf1[64], buf2[64]; 42 int sockt; 43 44 assert(p != NULL); 45 46 printf("Manifest: %s\n", p->mft); 47 printf("caRepository: %s\n", p->repo); 48 if (p->notify != NULL) 49 printf("Notify URL: %s\n", p->notify); 50 if (p->crl != NULL) 51 printf("Revocation list: %s\n", p->crl); 52 printf("Subject key identifier: %s\n", p->ski); 53 if (p->aki != NULL) 54 printf("Authority key identifier: %s\n", p->aki); 55 if (p->aia != NULL) 56 printf("Authority info access: %s\n", p->aia); 57 58 for (i = 0; i < p->asz; i++) 59 switch (p->as[i].type) { 60 case CERT_AS_ID: 61 printf("%5zu: AS: %" 62 PRIu32 "\n", i + 1, p->as[i].id); 63 break; 64 case CERT_AS_INHERIT: 65 printf("%5zu: AS: inherit\n", i + 1); 66 break; 67 case CERT_AS_RANGE: 68 printf("%5zu: AS: %" 69 PRIu32 "--%" PRIu32 "\n", i + 1, 70 p->as[i].range.min, p->as[i].range.max); 71 break; 72 } 73 74 for (i = 0; i < p->ipsz; i++) 75 switch (p->ips[i].type) { 76 case CERT_IP_INHERIT: 77 printf("%5zu: IP: inherit\n", i + 1); 78 break; 79 case CERT_IP_ADDR: 80 ip_addr_print(&p->ips[i].ip, 81 p->ips[i].afi, buf1, sizeof(buf1)); 82 printf("%5zu: IP: %s\n", i + 1, buf1); 83 break; 84 case CERT_IP_RANGE: 85 sockt = (p->ips[i].afi == AFI_IPV4) ? 86 AF_INET : AF_INET6; 87 inet_ntop(sockt, p->ips[i].min, buf1, sizeof(buf1)); 88 inet_ntop(sockt, p->ips[i].max, buf2, sizeof(buf2)); 89 printf("%5zu: IP: %s--%s\n", i + 1, buf1, buf2); 90 break; 91 } 92 } 93 94 int 95 main(int argc, char *argv[]) 96 { 97 int c, i, verb = 0, ta = 0; 98 X509 *xp = NULL; 99 struct cert *p; 100 101 ERR_load_crypto_strings(); 102 OpenSSL_add_all_ciphers(); 103 OpenSSL_add_all_digests(); 104 105 while ((c = getopt(argc, argv, "tv")) != -1) 106 switch (c) { 107 case 't': 108 ta = 1; 109 break; 110 case 'v': 111 verb++; 112 break; 113 default: 114 errx(1, "bad argument %c", c); 115 } 116 117 argv += optind; 118 argc -= optind; 119 120 if (argc == 0) 121 errx(1, "argument missing"); 122 123 if (ta) { 124 if (argc % 2) 125 errx(1, "need even number of arguments"); 126 127 for (i = 0; i < argc; i += 2) { 128 const char *cert_path = argv[i]; 129 const char *tal_path = argv[i + 1]; 130 char *buf; 131 struct tal *tal; 132 133 buf = tal_read_file(tal_path); 134 tal = tal_parse(tal_path, buf); 135 free(buf); 136 if (tal == NULL) 137 break; 138 139 p = ta_parse(&xp, cert_path, tal->pkey, tal->pkeysz); 140 tal_free(tal); 141 if (p == NULL) 142 break; 143 144 if (verb) 145 cert_print(p); 146 cert_free(p); 147 X509_free(xp); 148 } 149 } else { 150 for (i = 0; i < argc; i++) { 151 p = cert_parse(&xp, argv[i]); 152 if (p == NULL) 153 break; 154 if (verb) 155 cert_print(p); 156 cert_free(p); 157 X509_free(xp); 158 } 159 } 160 161 EVP_cleanup(); 162 CRYPTO_cleanup_all_ex_data(); 163 ERR_free_strings(); 164 165 if (i < argc) 166 errx(1, "test failed for %s", argv[i]); 167 168 printf("OK\n"); 169 return 0; 170 } 171