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