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