source: rtems-libbsd/freebsd/crypto/openssl/apps/pkcs7.c @ d1dac78

5
Last change on this file since d1dac78 was d1dac78, checked in by Christian Mauderer <christian.mauderer@…>, on 03/26/19 at 10:08:47

bin/openssl: Port to RTEMS.

  • Property mode set to 100644
File size: 5.6 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2#ifdef __rtems__
3#include <machine/rtems-bsd-program.h>
4#include "rtems-bsd-openssl-namespace.h"
5#endif /* __rtems__ */
6
7/*
8 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
9 *
10 * Licensed under the OpenSSL license (the "License").  You may not use
11 * this file except in compliance with the License.  You can obtain a copy
12 * in the file LICENSE in the source distribution or at
13 * https://www.openssl.org/source/license.html
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20#include "apps.h"
21#include "progs.h"
22#include <openssl/err.h>
23#include <openssl/objects.h>
24#include <openssl/evp.h>
25#include <openssl/x509.h>
26#include <openssl/pkcs7.h>
27#include <openssl/pem.h>
28
29typedef enum OPTION_choice {
30    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
31    OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
32    OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE
33} OPTION_CHOICE;
34
35const OPTIONS pkcs7_options[] = {
36    {"help", OPT_HELP, '-', "Display this summary"},
37    {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
38    {"in", OPT_IN, '<', "Input file"},
39    {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
40    {"out", OPT_OUT, '>', "Output file"},
41    {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
42    {"text", OPT_TEXT, '-', "Print full details of certificates"},
43    {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"},
44    {"print_certs", OPT_PRINT_CERTS, '-',
45     "Print_certs  print any certs or crl in the input"},
46#ifndef OPENSSL_NO_ENGINE
47    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
48#endif
49    {NULL}
50};
51
52int pkcs7_main(int argc, char **argv)
53{
54    ENGINE *e = NULL;
55    PKCS7 *p7 = NULL;
56    BIO *in = NULL, *out = NULL;
57    int informat = FORMAT_PEM, outformat = FORMAT_PEM;
58    char *infile = NULL, *outfile = NULL, *prog;
59    int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
60    OPTION_CHOICE o;
61
62    prog = opt_init(argc, argv, pkcs7_options);
63    while ((o = opt_next()) != OPT_EOF) {
64        switch (o) {
65        case OPT_EOF:
66        case OPT_ERR:
67 opthelp:
68            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
69            goto end;
70        case OPT_HELP:
71            opt_help(pkcs7_options);
72            ret = 0;
73            goto end;
74        case OPT_INFORM:
75            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
76                goto opthelp;
77            break;
78        case OPT_OUTFORM:
79            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
80                goto opthelp;
81            break;
82        case OPT_IN:
83            infile = opt_arg();
84            break;
85        case OPT_OUT:
86            outfile = opt_arg();
87            break;
88        case OPT_NOOUT:
89            noout = 1;
90            break;
91        case OPT_TEXT:
92            text = 1;
93            break;
94        case OPT_PRINT:
95            p7_print = 1;
96            break;
97        case OPT_PRINT_CERTS:
98            print_certs = 1;
99            break;
100        case OPT_ENGINE:
101            e = setup_engine(opt_arg(), 0);
102            break;
103        }
104    }
105    argc = opt_num_rest();
106    if (argc != 0)
107        goto opthelp;
108
109    in = bio_open_default(infile, 'r', informat);
110    if (in == NULL)
111        goto end;
112
113    if (informat == FORMAT_ASN1)
114        p7 = d2i_PKCS7_bio(in, NULL);
115    else
116        p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
117    if (p7 == NULL) {
118        BIO_printf(bio_err, "unable to load PKCS7 object\n");
119        ERR_print_errors(bio_err);
120        goto end;
121    }
122
123    out = bio_open_default(outfile, 'w', outformat);
124    if (out == NULL)
125        goto end;
126
127    if (p7_print)
128        PKCS7_print_ctx(out, p7, 0, NULL);
129
130    if (print_certs) {
131        STACK_OF(X509) *certs = NULL;
132        STACK_OF(X509_CRL) *crls = NULL;
133
134        i = OBJ_obj2nid(p7->type);
135        switch (i) {
136        case NID_pkcs7_signed:
137            if (p7->d.sign != NULL) {
138                certs = p7->d.sign->cert;
139                crls = p7->d.sign->crl;
140            }
141            break;
142        case NID_pkcs7_signedAndEnveloped:
143            if (p7->d.signed_and_enveloped != NULL) {
144                certs = p7->d.signed_and_enveloped->cert;
145                crls = p7->d.signed_and_enveloped->crl;
146            }
147            break;
148        default:
149            break;
150        }
151
152        if (certs != NULL) {
153            X509 *x;
154
155            for (i = 0; i < sk_X509_num(certs); i++) {
156                x = sk_X509_value(certs, i);
157                if (text)
158                    X509_print(out, x);
159                else
160                    dump_cert_text(out, x);
161
162                if (!noout)
163                    PEM_write_bio_X509(out, x);
164                BIO_puts(out, "\n");
165            }
166        }
167        if (crls != NULL) {
168            X509_CRL *crl;
169
170            for (i = 0; i < sk_X509_CRL_num(crls); i++) {
171                crl = sk_X509_CRL_value(crls, i);
172
173                X509_CRL_print_ex(out, crl, get_nameopt());
174
175                if (!noout)
176                    PEM_write_bio_X509_CRL(out, crl);
177                BIO_puts(out, "\n");
178            }
179        }
180
181        ret = 0;
182        goto end;
183    }
184
185    if (!noout) {
186        if (outformat == FORMAT_ASN1)
187            i = i2d_PKCS7_bio(out, p7);
188        else
189            i = PEM_write_bio_PKCS7(out, p7);
190
191        if (!i) {
192            BIO_printf(bio_err, "unable to write pkcs7 object\n");
193            ERR_print_errors(bio_err);
194            goto end;
195        }
196    }
197    ret = 0;
198 end:
199    PKCS7_free(p7);
200    release_engine(e);
201    BIO_free(in);
202    BIO_free_all(out);
203    return ret;
204}
205#ifdef __rtems__
206#include "rtems-bsd-openssl-pkcs7-data.h"
207#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.