source: rtems-libbsd/freebsd/crypto/openssl/apps/pkey.c @ 0a699e7

5-freebsd-126-freebsd-12
Last change on this file since 0a699e7 was 0a699e7, checked in by Christian Mauderer <christian.mauderer@…>, on 03/28/19 at 06:13:59

bin/openssl: Import from FreeBSD.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
5 *
6 * Licensed under the OpenSSL license (the "License").  You may not use
7 * this file except in compliance with the License.  You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
10 */
11
12#include <stdio.h>
13#include <string.h>
14#include "apps.h"
15#include "progs.h"
16#include <openssl/pem.h>
17#include <openssl/err.h>
18#include <openssl/evp.h>
19
20typedef enum OPTION_choice {
21    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
22    OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
23    OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
24    OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK
25} OPTION_CHOICE;
26
27const OPTIONS pkey_options[] = {
28    {"help", OPT_HELP, '-', "Display this summary"},
29    {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
30    {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
31    {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
32    {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
33    {"in", OPT_IN, 's', "Input key"},
34    {"out", OPT_OUT, '>', "Output file"},
35    {"pubin", OPT_PUBIN, '-',
36     "Read public key from input (default is private key)"},
37    {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
38    {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
39    {"text", OPT_TEXT, '-', "Output in plaintext as well"},
40    {"noout", OPT_NOOUT, '-', "Don't output the key"},
41    {"", OPT_MD, '-', "Any supported cipher"},
42    {"traditional", OPT_TRADITIONAL, '-',
43     "Use traditional format for private keys"},
44#ifndef OPENSSL_NO_ENGINE
45    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
46#endif
47    {"check", OPT_CHECK, '-', "Check key consistency"},
48    {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
49    {NULL}
50};
51
52int pkey_main(int argc, char **argv)
53{
54    BIO *in = NULL, *out = NULL;
55    ENGINE *e = NULL;
56    EVP_PKEY *pkey = NULL;
57    const EVP_CIPHER *cipher = NULL;
58    char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
59    char *passinarg = NULL, *passoutarg = NULL, *prog;
60    OPTION_CHOICE o;
61    int informat = FORMAT_PEM, outformat = FORMAT_PEM;
62    int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
63    int private = 0, traditional = 0, check = 0, pub_check = 0;
64
65    prog = opt_init(argc, argv, pkey_options);
66    while ((o = opt_next()) != OPT_EOF) {
67        switch (o) {
68        case OPT_EOF:
69        case OPT_ERR:
70 opthelp:
71            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
72            goto end;
73        case OPT_HELP:
74            opt_help(pkey_options);
75            ret = 0;
76            goto end;
77        case OPT_INFORM:
78            if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
79                goto opthelp;
80            break;
81        case OPT_OUTFORM:
82            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
83                goto opthelp;
84            break;
85        case OPT_PASSIN:
86            passinarg = opt_arg();
87            break;
88        case OPT_PASSOUT:
89            passoutarg = opt_arg();
90            break;
91        case OPT_ENGINE:
92            e = setup_engine(opt_arg(), 0);
93            break;
94        case OPT_IN:
95            infile = opt_arg();
96            break;
97        case OPT_OUT:
98            outfile = opt_arg();
99            break;
100        case OPT_PUBIN:
101            pubin = pubout = pubtext = 1;
102            break;
103        case OPT_PUBOUT:
104            pubout = 1;
105            break;
106        case OPT_TEXT_PUB:
107            pubtext = text = 1;
108            break;
109        case OPT_TEXT:
110            text = 1;
111            break;
112        case OPT_NOOUT:
113            noout = 1;
114            break;
115        case OPT_TRADITIONAL:
116            traditional = 1;
117            break;
118        case OPT_CHECK:
119            check = 1;
120            break;
121        case OPT_PUB_CHECK:
122            pub_check = 1;
123            break;
124        case OPT_MD:
125            if (!opt_cipher(opt_unknown(), &cipher))
126                goto opthelp;
127        }
128    }
129    argc = opt_num_rest();
130    if (argc != 0)
131        goto opthelp;
132
133    private = !noout && !pubout ? 1 : 0;
134    if (text && !pubtext)
135        private = 1;
136
137    if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
138        BIO_printf(bio_err, "Error getting passwords\n");
139        goto end;
140    }
141
142    out = bio_open_owner(outfile, outformat, private);
143    if (out == NULL)
144        goto end;
145
146    if (pubin)
147        pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
148    else
149        pkey = load_key(infile, informat, 1, passin, e, "key");
150    if (pkey == NULL)
151        goto end;
152
153    if (check || pub_check) {
154        int r;
155        EVP_PKEY_CTX *ctx;
156
157        ctx = EVP_PKEY_CTX_new(pkey, e);
158        if (ctx == NULL) {
159            ERR_print_errors(bio_err);
160            goto end;
161        }
162
163        if (check)
164            r = EVP_PKEY_check(ctx);
165        else
166            r = EVP_PKEY_public_check(ctx);
167
168        if (r == 1) {
169            BIO_printf(out, "Key is valid\n");
170        } else {
171            /*
172             * Note: at least for RSA keys if this function returns
173             * -1, there will be no error reasons.
174             */
175            unsigned long err;
176
177            BIO_printf(out, "Key is invalid\n");
178
179            while ((err = ERR_peek_error()) != 0) {
180                BIO_printf(out, "Detailed error: %s\n",
181                           ERR_reason_error_string(err));
182                ERR_get_error(); /* remove err from error stack */
183            }
184        }
185        EVP_PKEY_CTX_free(ctx);
186    }
187
188    if (!noout) {
189        if (outformat == FORMAT_PEM) {
190            if (pubout) {
191                if (!PEM_write_bio_PUBKEY(out, pkey))
192                    goto end;
193            } else {
194                assert(private);
195                if (traditional) {
196                    if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
197                                                              NULL, 0, NULL,
198                                                              passout))
199                        goto end;
200                } else {
201                    if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
202                                                  NULL, 0, NULL, passout))
203                        goto end;
204                }
205            }
206        } else if (outformat == FORMAT_ASN1) {
207            if (pubout) {
208                if (!i2d_PUBKEY_bio(out, pkey))
209                    goto end;
210            } else {
211                assert(private);
212                if (!i2d_PrivateKey_bio(out, pkey))
213                    goto end;
214            }
215        } else {
216            BIO_printf(bio_err, "Bad format specified for key\n");
217            goto end;
218        }
219    }
220
221    if (text) {
222        if (pubtext) {
223            if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
224                goto end;
225        } else {
226            assert(private);
227            if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
228                goto end;
229        }
230    }
231
232    ret = 0;
233
234 end:
235    if (ret != 0)
236        ERR_print_errors(bio_err);
237    EVP_PKEY_free(pkey);
238    release_engine(e);
239    BIO_free_all(out);
240    BIO_free(in);
241    OPENSSL_free(passin);
242    OPENSSL_free(passout);
243
244    return ret;
245}
Note: See TracBrowser for help on using the repository browser.