1 /* $Id: test-mft.c,v 1.17 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/types.h> 19 #include <netinet/in.h> 20 #include <assert.h> 21 #include <err.h> 22 #include <resolv.h> /* b64_ntop */ 23 #include <stdio.h> 24 #include <stdint.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/pem.h> 32 #include <openssl/x509v3.h> 33 34 #include "extern.h" 35 36 int verbose; 37 38 int 39 main(int argc, char *argv[]) 40 { 41 int c, i, ppem = 0, verb = 0; 42 struct mft *p; 43 BIO *bio_out = NULL; 44 X509 *xp = NULL; 45 unsigned char *buf; 46 size_t len; 47 48 ERR_load_crypto_strings(); 49 OpenSSL_add_all_ciphers(); 50 OpenSSL_add_all_digests(); 51 52 while (-1 != (c = getopt(argc, argv, "pv"))) 53 switch (c) { 54 case 'p': 55 if (ppem) 56 break; 57 ppem = 1; 58 if ((bio_out = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL) 59 errx(1, "BIO_new_fp"); 60 break; 61 case 'v': 62 verb++; 63 break; 64 default: 65 errx(1, "bad argument %c", c); 66 } 67 68 argv += optind; 69 argc -= optind; 70 71 if (argc == 0) 72 errx(1, "argument missing"); 73 74 for (i = 0; i < argc; i++) { 75 buf = load_file(argv[i], &len); 76 if ((p = mft_parse(&xp, argv[i], buf, len)) == NULL) { 77 free(buf); 78 continue; 79 } 80 if (verb) 81 mft_print(p); 82 if (ppem) { 83 if (!PEM_write_bio_X509(bio_out, xp)) 84 errx(1, 85 "PEM_write_bio_X509: unable to write cert"); 86 } 87 free(buf); 88 mft_free(p); 89 X509_free(xp); 90 } 91 92 BIO_free(bio_out); 93 EVP_cleanup(); 94 CRYPTO_cleanup_all_ex_data(); 95 ERR_free_strings(); 96 97 if (i < argc) 98 errx(1, "test failed for %s", argv[i]); 99 100 printf("OK\n"); 101 return 0; 102 } 103