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