1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <stdio.h> 11e1051a39Sopenharmony_ci#include <string.h> 12e1051a39Sopenharmony_ci#include <sys/types.h> 13e1051a39Sopenharmony_ci#include "apps.h" 14e1051a39Sopenharmony_ci#include "progs.h" 15e1051a39Sopenharmony_ci#include <openssl/err.h> 16e1051a39Sopenharmony_ci#include <openssl/evp.h> 17e1051a39Sopenharmony_ci#include <openssl/x509.h> 18e1051a39Sopenharmony_ci#include <openssl/pkcs7.h> 19e1051a39Sopenharmony_ci#include <openssl/pem.h> 20e1051a39Sopenharmony_ci#include <openssl/objects.h> 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_cistatic int add_certs_from_file(STACK_OF(X509) *stack, char *certfile); 23e1051a39Sopenharmony_ci 24e1051a39Sopenharmony_citypedef enum OPTION_choice { 25e1051a39Sopenharmony_ci OPT_COMMON, 26e1051a39Sopenharmony_ci OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOCRL, OPT_CERTFILE, 27e1051a39Sopenharmony_ci OPT_PROV_ENUM 28e1051a39Sopenharmony_ci} OPTION_CHOICE; 29e1051a39Sopenharmony_ci 30e1051a39Sopenharmony_ciconst OPTIONS crl2pkcs7_options[] = { 31e1051a39Sopenharmony_ci OPT_SECTION("General"), 32e1051a39Sopenharmony_ci {"help", OPT_HELP, '-', "Display this summary"}, 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ci OPT_SECTION("Input"), 35e1051a39Sopenharmony_ci {"in", OPT_IN, '<', "Input file"}, 36e1051a39Sopenharmony_ci {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"}, 37e1051a39Sopenharmony_ci {"nocrl", OPT_NOCRL, '-', "No crl to load, just certs from '-certfile'"}, 38e1051a39Sopenharmony_ci {"certfile", OPT_CERTFILE, '<', 39e1051a39Sopenharmony_ci "File of chain of certs to a trusted CA; can be repeated"}, 40e1051a39Sopenharmony_ci 41e1051a39Sopenharmony_ci OPT_SECTION("Output"), 42e1051a39Sopenharmony_ci {"out", OPT_OUT, '>', "Output file"}, 43e1051a39Sopenharmony_ci {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"}, 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci OPT_PROV_OPTIONS, 46e1051a39Sopenharmony_ci {NULL} 47e1051a39Sopenharmony_ci}; 48e1051a39Sopenharmony_ci 49e1051a39Sopenharmony_ciint crl2pkcs7_main(int argc, char **argv) 50e1051a39Sopenharmony_ci{ 51e1051a39Sopenharmony_ci BIO *in = NULL, *out = NULL; 52e1051a39Sopenharmony_ci PKCS7 *p7 = NULL; 53e1051a39Sopenharmony_ci PKCS7_SIGNED *p7s = NULL; 54e1051a39Sopenharmony_ci STACK_OF(OPENSSL_STRING) *certflst = NULL; 55e1051a39Sopenharmony_ci STACK_OF(X509) *cert_stack = NULL; 56e1051a39Sopenharmony_ci STACK_OF(X509_CRL) *crl_stack = NULL; 57e1051a39Sopenharmony_ci X509_CRL *crl = NULL; 58e1051a39Sopenharmony_ci char *infile = NULL, *outfile = NULL, *prog, *certfile; 59e1051a39Sopenharmony_ci int i = 0, informat = FORMAT_PEM, outformat = FORMAT_PEM, ret = 1, nocrl = 60e1051a39Sopenharmony_ci 0; 61e1051a39Sopenharmony_ci OPTION_CHOICE o; 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_ci prog = opt_init(argc, argv, crl2pkcs7_options); 64e1051a39Sopenharmony_ci while ((o = opt_next()) != OPT_EOF) { 65e1051a39Sopenharmony_ci switch (o) { 66e1051a39Sopenharmony_ci case OPT_EOF: 67e1051a39Sopenharmony_ci case OPT_ERR: 68e1051a39Sopenharmony_ci opthelp: 69e1051a39Sopenharmony_ci BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 70e1051a39Sopenharmony_ci goto end; 71e1051a39Sopenharmony_ci case OPT_HELP: 72e1051a39Sopenharmony_ci opt_help(crl2pkcs7_options); 73e1051a39Sopenharmony_ci ret = 0; 74e1051a39Sopenharmony_ci goto end; 75e1051a39Sopenharmony_ci case OPT_INFORM: 76e1051a39Sopenharmony_ci if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) 77e1051a39Sopenharmony_ci goto opthelp; 78e1051a39Sopenharmony_ci break; 79e1051a39Sopenharmony_ci case OPT_OUTFORM: 80e1051a39Sopenharmony_ci if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) 81e1051a39Sopenharmony_ci goto opthelp; 82e1051a39Sopenharmony_ci break; 83e1051a39Sopenharmony_ci case OPT_IN: 84e1051a39Sopenharmony_ci infile = opt_arg(); 85e1051a39Sopenharmony_ci break; 86e1051a39Sopenharmony_ci case OPT_OUT: 87e1051a39Sopenharmony_ci outfile = opt_arg(); 88e1051a39Sopenharmony_ci break; 89e1051a39Sopenharmony_ci case OPT_NOCRL: 90e1051a39Sopenharmony_ci nocrl = 1; 91e1051a39Sopenharmony_ci break; 92e1051a39Sopenharmony_ci case OPT_CERTFILE: 93e1051a39Sopenharmony_ci if ((certflst == NULL) 94e1051a39Sopenharmony_ci && (certflst = sk_OPENSSL_STRING_new_null()) == NULL) 95e1051a39Sopenharmony_ci goto end; 96e1051a39Sopenharmony_ci if (!sk_OPENSSL_STRING_push(certflst, opt_arg())) 97e1051a39Sopenharmony_ci goto end; 98e1051a39Sopenharmony_ci break; 99e1051a39Sopenharmony_ci case OPT_PROV_CASES: 100e1051a39Sopenharmony_ci if (!opt_provider(o)) 101e1051a39Sopenharmony_ci goto end; 102e1051a39Sopenharmony_ci break; 103e1051a39Sopenharmony_ci } 104e1051a39Sopenharmony_ci } 105e1051a39Sopenharmony_ci 106e1051a39Sopenharmony_ci /* No remaining args. */ 107e1051a39Sopenharmony_ci argc = opt_num_rest(); 108e1051a39Sopenharmony_ci if (argc != 0) 109e1051a39Sopenharmony_ci goto opthelp; 110e1051a39Sopenharmony_ci 111e1051a39Sopenharmony_ci if (!nocrl) { 112e1051a39Sopenharmony_ci in = bio_open_default(infile, 'r', informat); 113e1051a39Sopenharmony_ci if (in == NULL) 114e1051a39Sopenharmony_ci goto end; 115e1051a39Sopenharmony_ci 116e1051a39Sopenharmony_ci if (informat == FORMAT_ASN1) 117e1051a39Sopenharmony_ci crl = d2i_X509_CRL_bio(in, NULL); 118e1051a39Sopenharmony_ci else if (informat == FORMAT_PEM) 119e1051a39Sopenharmony_ci crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); 120e1051a39Sopenharmony_ci if (crl == NULL) { 121e1051a39Sopenharmony_ci BIO_printf(bio_err, "unable to load CRL\n"); 122e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 123e1051a39Sopenharmony_ci goto end; 124e1051a39Sopenharmony_ci } 125e1051a39Sopenharmony_ci } 126e1051a39Sopenharmony_ci 127e1051a39Sopenharmony_ci if ((p7 = PKCS7_new()) == NULL) 128e1051a39Sopenharmony_ci goto end; 129e1051a39Sopenharmony_ci if ((p7s = PKCS7_SIGNED_new()) == NULL) 130e1051a39Sopenharmony_ci goto end; 131e1051a39Sopenharmony_ci p7->type = OBJ_nid2obj(NID_pkcs7_signed); 132e1051a39Sopenharmony_ci p7->d.sign = p7s; 133e1051a39Sopenharmony_ci p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data); 134e1051a39Sopenharmony_ci 135e1051a39Sopenharmony_ci if (!ASN1_INTEGER_set(p7s->version, 1)) 136e1051a39Sopenharmony_ci goto end; 137e1051a39Sopenharmony_ci 138e1051a39Sopenharmony_ci if (crl != NULL) { 139e1051a39Sopenharmony_ci if ((crl_stack = sk_X509_CRL_new_null()) == NULL) 140e1051a39Sopenharmony_ci goto end; 141e1051a39Sopenharmony_ci p7s->crl = crl_stack; 142e1051a39Sopenharmony_ci sk_X509_CRL_push(crl_stack, crl); 143e1051a39Sopenharmony_ci crl = NULL; /* now part of p7 for OPENSSL_freeing */ 144e1051a39Sopenharmony_ci } 145e1051a39Sopenharmony_ci 146e1051a39Sopenharmony_ci if (certflst != NULL) { 147e1051a39Sopenharmony_ci if ((cert_stack = sk_X509_new_null()) == NULL) 148e1051a39Sopenharmony_ci goto end; 149e1051a39Sopenharmony_ci p7s->cert = cert_stack; 150e1051a39Sopenharmony_ci 151e1051a39Sopenharmony_ci for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) { 152e1051a39Sopenharmony_ci certfile = sk_OPENSSL_STRING_value(certflst, i); 153e1051a39Sopenharmony_ci if (add_certs_from_file(cert_stack, certfile) < 0) { 154e1051a39Sopenharmony_ci BIO_printf(bio_err, "error loading certificates\n"); 155e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 156e1051a39Sopenharmony_ci goto end; 157e1051a39Sopenharmony_ci } 158e1051a39Sopenharmony_ci } 159e1051a39Sopenharmony_ci } 160e1051a39Sopenharmony_ci 161e1051a39Sopenharmony_ci out = bio_open_default(outfile, 'w', outformat); 162e1051a39Sopenharmony_ci if (out == NULL) 163e1051a39Sopenharmony_ci goto end; 164e1051a39Sopenharmony_ci 165e1051a39Sopenharmony_ci if (outformat == FORMAT_ASN1) 166e1051a39Sopenharmony_ci i = i2d_PKCS7_bio(out, p7); 167e1051a39Sopenharmony_ci else if (outformat == FORMAT_PEM) 168e1051a39Sopenharmony_ci i = PEM_write_bio_PKCS7(out, p7); 169e1051a39Sopenharmony_ci if (!i) { 170e1051a39Sopenharmony_ci BIO_printf(bio_err, "unable to write pkcs7 object\n"); 171e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 172e1051a39Sopenharmony_ci goto end; 173e1051a39Sopenharmony_ci } 174e1051a39Sopenharmony_ci ret = 0; 175e1051a39Sopenharmony_ci end: 176e1051a39Sopenharmony_ci sk_OPENSSL_STRING_free(certflst); 177e1051a39Sopenharmony_ci BIO_free(in); 178e1051a39Sopenharmony_ci BIO_free_all(out); 179e1051a39Sopenharmony_ci PKCS7_free(p7); 180e1051a39Sopenharmony_ci X509_CRL_free(crl); 181e1051a39Sopenharmony_ci 182e1051a39Sopenharmony_ci return ret; 183e1051a39Sopenharmony_ci} 184e1051a39Sopenharmony_ci 185e1051a39Sopenharmony_ci/*- 186e1051a39Sopenharmony_ci *---------------------------------------------------------------------- 187e1051a39Sopenharmony_ci * int add_certs_from_file 188e1051a39Sopenharmony_ci * 189e1051a39Sopenharmony_ci * Read a list of certificates to be checked from a file. 190e1051a39Sopenharmony_ci * 191e1051a39Sopenharmony_ci * Results: 192e1051a39Sopenharmony_ci * number of certs added if successful, -1 if not. 193e1051a39Sopenharmony_ci *---------------------------------------------------------------------- 194e1051a39Sopenharmony_ci */ 195e1051a39Sopenharmony_cistatic int add_certs_from_file(STACK_OF(X509) *stack, char *certfile) 196e1051a39Sopenharmony_ci{ 197e1051a39Sopenharmony_ci BIO *in = NULL; 198e1051a39Sopenharmony_ci int count = 0; 199e1051a39Sopenharmony_ci int ret = -1; 200e1051a39Sopenharmony_ci STACK_OF(X509_INFO) *sk = NULL; 201e1051a39Sopenharmony_ci X509_INFO *xi; 202e1051a39Sopenharmony_ci 203e1051a39Sopenharmony_ci in = BIO_new_file(certfile, "r"); 204e1051a39Sopenharmony_ci if (in == NULL) { 205e1051a39Sopenharmony_ci BIO_printf(bio_err, "error opening the file, %s\n", certfile); 206e1051a39Sopenharmony_ci goto end; 207e1051a39Sopenharmony_ci } 208e1051a39Sopenharmony_ci 209e1051a39Sopenharmony_ci /* This loads from a file, a stack of x509/crl/pkey sets */ 210e1051a39Sopenharmony_ci sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL); 211e1051a39Sopenharmony_ci if (sk == NULL) { 212e1051a39Sopenharmony_ci BIO_printf(bio_err, "error reading the file, %s\n", certfile); 213e1051a39Sopenharmony_ci goto end; 214e1051a39Sopenharmony_ci } 215e1051a39Sopenharmony_ci 216e1051a39Sopenharmony_ci /* scan over it and pull out the CRL's */ 217e1051a39Sopenharmony_ci while (sk_X509_INFO_num(sk)) { 218e1051a39Sopenharmony_ci xi = sk_X509_INFO_shift(sk); 219e1051a39Sopenharmony_ci if (xi->x509 != NULL) { 220e1051a39Sopenharmony_ci sk_X509_push(stack, xi->x509); 221e1051a39Sopenharmony_ci xi->x509 = NULL; 222e1051a39Sopenharmony_ci count++; 223e1051a39Sopenharmony_ci } 224e1051a39Sopenharmony_ci X509_INFO_free(xi); 225e1051a39Sopenharmony_ci } 226e1051a39Sopenharmony_ci 227e1051a39Sopenharmony_ci ret = count; 228e1051a39Sopenharmony_ci end: 229e1051a39Sopenharmony_ci /* never need to OPENSSL_free x */ 230e1051a39Sopenharmony_ci BIO_free(in); 231e1051a39Sopenharmony_ci sk_X509_INFO_free(sk); 232e1051a39Sopenharmony_ci return ret; 233e1051a39Sopenharmony_ci} 234