source: rtems-libbsd/freebsd/crypto/openssl/apps/rsa.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: 9.4 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Copyright 1995-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 <openssl/opensslconf.h>
13#ifdef OPENSSL_NO_RSA
14NON_EMPTY_TRANSLATION_UNIT
15#else
16
17# include <stdio.h>
18# include <stdlib.h>
19# include <string.h>
20# include <time.h>
21# include "apps.h"
22# include "progs.h"
23# include <openssl/bio.h>
24# include <openssl/err.h>
25# include <openssl/rsa.h>
26# include <openssl/evp.h>
27# include <openssl/x509.h>
28# include <openssl/pem.h>
29# include <openssl/bn.h>
30
31typedef enum OPTION_choice {
32    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
33    OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
34    OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
35    OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
36    /* Do not change the order here; see case statements below */
37    OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
38    OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
39} OPTION_CHOICE;
40
41const OPTIONS rsa_options[] = {
42    {"help", OPT_HELP, '-', "Display this summary"},
43    {"inform", OPT_INFORM, 'f', "Input format, one of DER PEM"},
44    {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
45    {"in", OPT_IN, 's', "Input file"},
46    {"out", OPT_OUT, '>', "Output file"},
47    {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
48    {"pubout", OPT_PUBOUT, '-', "Output a public key"},
49    {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
50    {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
51    {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
52    {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
53    {"noout", OPT_NOOUT, '-', "Don't print key out"},
54    {"text", OPT_TEXT, '-', "Print the key in text"},
55    {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
56    {"check", OPT_CHECK, '-', "Verify key consistency"},
57    {"", OPT_CIPHER, '-', "Any supported cipher"},
58# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
59    {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
60    {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
61    {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
62# endif
63# ifndef OPENSSL_NO_ENGINE
64    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
65# endif
66    {NULL}
67};
68
69int rsa_main(int argc, char **argv)
70{
71    ENGINE *e = NULL;
72    BIO *out = NULL;
73    RSA *rsa = NULL;
74    const EVP_CIPHER *enc = NULL;
75    char *infile = NULL, *outfile = NULL, *prog;
76    char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
77    int i, private = 0;
78    int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
79    int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
80# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
81    int pvk_encr = 2;
82# endif
83    OPTION_CHOICE o;
84
85    prog = opt_init(argc, argv, rsa_options);
86    while ((o = opt_next()) != OPT_EOF) {
87        switch (o) {
88        case OPT_EOF:
89        case OPT_ERR:
90 opthelp:
91            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
92            goto end;
93        case OPT_HELP:
94            opt_help(rsa_options);
95            ret = 0;
96            goto end;
97        case OPT_INFORM:
98            if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
99                goto opthelp;
100            break;
101        case OPT_IN:
102            infile = opt_arg();
103            break;
104        case OPT_OUTFORM:
105            if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
106                goto opthelp;
107            break;
108        case OPT_OUT:
109            outfile = opt_arg();
110            break;
111        case OPT_PASSIN:
112            passinarg = opt_arg();
113            break;
114        case OPT_PASSOUT:
115            passoutarg = opt_arg();
116            break;
117        case OPT_ENGINE:
118            e = setup_engine(opt_arg(), 0);
119            break;
120        case OPT_PUBIN:
121            pubin = 1;
122            break;
123        case OPT_PUBOUT:
124            pubout = 1;
125            break;
126        case OPT_RSAPUBKEY_IN:
127            pubin = 2;
128            break;
129        case OPT_RSAPUBKEY_OUT:
130            pubout = 2;
131            break;
132        case OPT_PVK_STRONG:    /* pvk_encr:= 2 */
133        case OPT_PVK_WEAK:      /* pvk_encr:= 1 */
134        case OPT_PVK_NONE:      /* pvk_encr:= 0 */
135# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
136            pvk_encr = (o - OPT_PVK_NONE);
137# endif
138            break;
139        case OPT_NOOUT:
140            noout = 1;
141            break;
142        case OPT_TEXT:
143            text = 1;
144            break;
145        case OPT_MODULUS:
146            modulus = 1;
147            break;
148        case OPT_CHECK:
149            check = 1;
150            break;
151        case OPT_CIPHER:
152            if (!opt_cipher(opt_unknown(), &enc))
153                goto opthelp;
154            break;
155        }
156    }
157    argc = opt_num_rest();
158    if (argc != 0)
159        goto opthelp;
160
161    private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
162
163    if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
164        BIO_printf(bio_err, "Error getting passwords\n");
165        goto end;
166    }
167    if (check && pubin) {
168        BIO_printf(bio_err, "Only private keys can be checked\n");
169        goto end;
170    }
171
172    {
173        EVP_PKEY *pkey;
174
175        if (pubin) {
176            int tmpformat = -1;
177            if (pubin == 2) {
178                if (informat == FORMAT_PEM)
179                    tmpformat = FORMAT_PEMRSA;
180                else if (informat == FORMAT_ASN1)
181                    tmpformat = FORMAT_ASN1RSA;
182            } else {
183                tmpformat = informat;
184            }
185
186            pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
187        } else {
188            pkey = load_key(infile, informat, 1, passin, e, "Private Key");
189        }
190
191        if (pkey != NULL)
192            rsa = EVP_PKEY_get1_RSA(pkey);
193        EVP_PKEY_free(pkey);
194    }
195
196    if (rsa == NULL) {
197        ERR_print_errors(bio_err);
198        goto end;
199    }
200
201    out = bio_open_owner(outfile, outformat, private);
202    if (out == NULL)
203        goto end;
204
205    if (text) {
206        assert(pubin || private);
207        if (!RSA_print(out, rsa, 0)) {
208            perror(outfile);
209            ERR_print_errors(bio_err);
210            goto end;
211        }
212    }
213
214    if (modulus) {
215        const BIGNUM *n;
216        RSA_get0_key(rsa, &n, NULL, NULL);
217        BIO_printf(out, "Modulus=");
218        BN_print(out, n);
219        BIO_printf(out, "\n");
220    }
221
222    if (check) {
223        int r = RSA_check_key_ex(rsa, NULL);
224
225        if (r == 1) {
226            BIO_printf(out, "RSA key ok\n");
227        } else if (r == 0) {
228            unsigned long err;
229
230            while ((err = ERR_peek_error()) != 0 &&
231                   ERR_GET_LIB(err) == ERR_LIB_RSA &&
232                   ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY_EX &&
233                   ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
234                BIO_printf(out, "RSA key error: %s\n",
235                           ERR_reason_error_string(err));
236                ERR_get_error(); /* remove err from error stack */
237            }
238        } else if (r == -1) {
239            ERR_print_errors(bio_err);
240            goto end;
241        }
242    }
243
244    if (noout) {
245        ret = 0;
246        goto end;
247    }
248    BIO_printf(bio_err, "writing RSA key\n");
249    if (outformat == FORMAT_ASN1) {
250        if (pubout || pubin) {
251            if (pubout == 2)
252                i = i2d_RSAPublicKey_bio(out, rsa);
253            else
254                i = i2d_RSA_PUBKEY_bio(out, rsa);
255        } else {
256            assert(private);
257            i = i2d_RSAPrivateKey_bio(out, rsa);
258        }
259    } else if (outformat == FORMAT_PEM) {
260        if (pubout || pubin) {
261            if (pubout == 2)
262                i = PEM_write_bio_RSAPublicKey(out, rsa);
263            else
264                i = PEM_write_bio_RSA_PUBKEY(out, rsa);
265        } else {
266            assert(private);
267            i = PEM_write_bio_RSAPrivateKey(out, rsa,
268                                            enc, NULL, 0, NULL, passout);
269        }
270# ifndef OPENSSL_NO_DSA
271    } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
272        EVP_PKEY *pk;
273        pk = EVP_PKEY_new();
274        if (pk == NULL)
275            goto end;
276
277        EVP_PKEY_set1_RSA(pk, rsa);
278        if (outformat == FORMAT_PVK) {
279            if (pubin) {
280                BIO_printf(bio_err, "PVK form impossible with public key input\n");
281                EVP_PKEY_free(pk);
282                goto end;
283            }
284            assert(private);
285#  ifdef OPENSSL_NO_RC4
286            BIO_printf(bio_err, "PVK format not supported\n");
287            EVP_PKEY_free(pk);
288            goto end;
289#  else
290            i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
291#  endif
292        } else if (pubin || pubout) {
293            i = i2b_PublicKey_bio(out, pk);
294        } else {
295            assert(private);
296            i = i2b_PrivateKey_bio(out, pk);
297        }
298        EVP_PKEY_free(pk);
299# endif
300    } else {
301        BIO_printf(bio_err, "bad output format specified for outfile\n");
302        goto end;
303    }
304    if (i <= 0) {
305        BIO_printf(bio_err, "unable to write key\n");
306        ERR_print_errors(bio_err);
307    } else {
308        ret = 0;
309    }
310 end:
311    release_engine(e);
312    BIO_free_all(out);
313    RSA_free(rsa);
314    OPENSSL_free(passin);
315    OPENSSL_free(passout);
316    return ret;
317}
318#endif
Note: See TracBrowser for help on using the repository browser.