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