2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <sys/types.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509.h>
18 #include <openssl/pkcs7.h>
19 #include <openssl/pem.h>
20 #include <openssl/objects.h>
22 DEFINE_STACK_OF(X509_CRL)
24 DEFINE_STACK_OF(X509_INFO)
25 DEFINE_STACK_OF_STRING()
27 static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile);
29 typedef enum OPTION_choice {
30 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
31 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOCRL, OPT_CERTFILE,
35 const OPTIONS crl2pkcs7_options[] = {
36 OPT_SECTION("General"),
37 {"help", OPT_HELP, '-', "Display this summary"},
40 {"in", OPT_IN, '<', "Input file"},
41 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
42 {"nocrl", OPT_NOCRL, '-', "No crl to load, just certs from '-certfile'"},
43 {"certfile", OPT_CERTFILE, '<',
44 "File of chain of certs to a trusted CA; can be repeated"},
46 OPT_SECTION("Output"),
47 {"out", OPT_OUT, '>', "Output file"},
48 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
54 int crl2pkcs7_main(int argc, char **argv)
56 BIO *in = NULL, *out = NULL;
58 PKCS7_SIGNED *p7s = NULL;
59 STACK_OF(OPENSSL_STRING) *certflst = NULL;
60 STACK_OF(X509) *cert_stack = NULL;
61 STACK_OF(X509_CRL) *crl_stack = NULL;
63 char *infile = NULL, *outfile = NULL, *prog, *certfile;
64 int i = 0, informat = FORMAT_PEM, outformat = FORMAT_PEM, ret = 1, nocrl =
68 prog = opt_init(argc, argv, crl2pkcs7_options);
69 while ((o = opt_next()) != OPT_EOF) {
74 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
77 opt_help(crl2pkcs7_options);
81 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
85 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
98 if ((certflst == NULL)
99 && (certflst = sk_OPENSSL_STRING_new_null()) == NULL)
101 if (!sk_OPENSSL_STRING_push(certflst, opt_arg()))
105 if (!opt_provider(o))
110 argc = opt_num_rest();
115 in = bio_open_default(infile, 'r', informat);
119 if (informat == FORMAT_ASN1)
120 crl = d2i_X509_CRL_bio(in, NULL);
121 else if (informat == FORMAT_PEM)
122 crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
124 BIO_printf(bio_err, "unable to load CRL\n");
125 ERR_print_errors(bio_err);
130 if ((p7 = PKCS7_new()) == NULL)
132 if ((p7s = PKCS7_SIGNED_new()) == NULL)
134 p7->type = OBJ_nid2obj(NID_pkcs7_signed);
136 p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data);
138 if (!ASN1_INTEGER_set(p7s->version, 1))
140 if ((crl_stack = sk_X509_CRL_new_null()) == NULL)
142 p7s->crl = crl_stack;
144 sk_X509_CRL_push(crl_stack, crl);
145 crl = NULL; /* now part of p7 for OPENSSL_freeing */
148 if ((cert_stack = sk_X509_new_null()) == NULL)
150 p7s->cert = cert_stack;
152 if (certflst != NULL)
153 for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) {
154 certfile = sk_OPENSSL_STRING_value(certflst, i);
155 if (add_certs_from_file(cert_stack, certfile) < 0) {
156 BIO_printf(bio_err, "error loading certificates\n");
157 ERR_print_errors(bio_err);
162 out = bio_open_default(outfile, 'w', outformat);
166 if (outformat == FORMAT_ASN1)
167 i = i2d_PKCS7_bio(out, p7);
168 else if (outformat == FORMAT_PEM)
169 i = PEM_write_bio_PKCS7(out, p7);
171 BIO_printf(bio_err, "unable to write pkcs7 object\n");
172 ERR_print_errors(bio_err);
177 sk_OPENSSL_STRING_free(certflst);
187 *----------------------------------------------------------------------
188 * int add_certs_from_file
190 * Read a list of certificates to be checked from a file.
193 * number of certs added if successful, -1 if not.
194 *----------------------------------------------------------------------
196 static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
201 STACK_OF(X509_INFO) *sk = NULL;
204 in = BIO_new_file(certfile, "r");
206 BIO_printf(bio_err, "error opening the file, %s\n", certfile);
210 /* This loads from a file, a stack of x509/crl/pkey sets */
211 sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
213 BIO_printf(bio_err, "error reading the file, %s\n", certfile);
217 /* scan over it and pull out the CRL's */
218 while (sk_X509_INFO_num(sk)) {
219 xi = sk_X509_INFO_shift(sk);
220 if (xi->x509 != NULL) {
221 sk_X509_push(stack, xi->x509);
230 /* never need to OPENSSL_free x */
232 sk_X509_INFO_free(sk);