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