source: rtems-libbsd/freebsd/crypto/openssl/apps/genrsa.c @ 0fecf49

5
Last change on this file since 0fecf49 was 0fecf49, checked in by Christian Mauderer <christian.mauderer@…>, on 03/26/19 at 09:19:22

bin/openssl: Import from FreeBSD.

  • Property mode set to 100644
File size: 5.5 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 <string.h>
19# include <sys/types.h>
20# include <sys/stat.h>
21# include "apps.h"
22# include "progs.h"
23# include <openssl/bio.h>
24# include <openssl/err.h>
25# include <openssl/bn.h>
26# include <openssl/rsa.h>
27# include <openssl/evp.h>
28# include <openssl/x509.h>
29# include <openssl/pem.h>
30# include <openssl/rand.h>
31
32# define DEFBITS 2048
33# define DEFPRIMES 2
34
35static int genrsa_cb(int p, int n, BN_GENCB *cb);
36
37typedef enum OPTION_choice {
38    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
39    OPT_3, OPT_F4, OPT_ENGINE,
40    OPT_OUT, OPT_PASSOUT, OPT_CIPHER, OPT_PRIMES,
41    OPT_R_ENUM
42} OPTION_CHOICE;
43
44const OPTIONS genrsa_options[] = {
45    {"help", OPT_HELP, '-', "Display this summary"},
46    {"3", OPT_3, '-', "Use 3 for the E value"},
47    {"F4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
48    {"f4", OPT_F4, '-', "Use F4 (0x10001) for the E value"},
49    {"out", OPT_OUT, '>', "Output the key to specified file"},
50    OPT_R_OPTIONS,
51    {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
52    {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
53# ifndef OPENSSL_NO_ENGINE
54    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
55# endif
56    {"primes", OPT_PRIMES, 'p', "Specify number of primes"},
57    {NULL}
58};
59
60int genrsa_main(int argc, char **argv)
61{
62    BN_GENCB *cb = BN_GENCB_new();
63    PW_CB_DATA cb_data;
64    ENGINE *eng = NULL;
65    BIGNUM *bn = BN_new();
66    BIO *out = NULL;
67    const BIGNUM *e;
68    RSA *rsa = NULL;
69    const EVP_CIPHER *enc = NULL;
70    int ret = 1, num = DEFBITS, private = 0, primes = DEFPRIMES;
71    unsigned long f4 = RSA_F4;
72    char *outfile = NULL, *passoutarg = NULL, *passout = NULL;
73    char *prog, *hexe, *dece;
74    OPTION_CHOICE o;
75
76    if (bn == NULL || cb == NULL)
77        goto end;
78
79    BN_GENCB_set(cb, genrsa_cb, bio_err);
80
81    prog = opt_init(argc, argv, genrsa_options);
82    while ((o = opt_next()) != OPT_EOF) {
83        switch (o) {
84        case OPT_EOF:
85        case OPT_ERR:
86opthelp:
87            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
88            goto end;
89        case OPT_HELP:
90            ret = 0;
91            opt_help(genrsa_options);
92            goto end;
93        case OPT_3:
94            f4 = 3;
95            break;
96        case OPT_F4:
97            f4 = RSA_F4;
98            break;
99        case OPT_OUT:
100            outfile = opt_arg();
101            break;
102        case OPT_ENGINE:
103            eng = setup_engine(opt_arg(), 0);
104            break;
105        case OPT_R_CASES:
106            if (!opt_rand(o))
107                goto end;
108            break;
109        case OPT_PASSOUT:
110            passoutarg = opt_arg();
111            break;
112        case OPT_CIPHER:
113            if (!opt_cipher(opt_unknown(), &enc))
114                goto end;
115            break;
116        case OPT_PRIMES:
117            if (!opt_int(opt_arg(), &primes))
118                goto end;
119            break;
120        }
121    }
122    argc = opt_num_rest();
123    argv = opt_rest();
124
125    if (argc == 1) {
126        if (!opt_int(argv[0], &num) || num <= 0)
127            goto end;
128        if (num > OPENSSL_RSA_MAX_MODULUS_BITS)
129            BIO_printf(bio_err,
130                       "Warning: It is not recommended to use more than %d bit for RSA keys.\n"
131                       "         Your key size is %d! Larger key size may behave not as expected.\n",
132                       OPENSSL_RSA_MAX_MODULUS_BITS, num);
133    } else if (argc > 0) {
134        BIO_printf(bio_err, "Extra arguments given.\n");
135        goto opthelp;
136    }
137
138    private = 1;
139    if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
140        BIO_printf(bio_err, "Error getting password\n");
141        goto end;
142    }
143
144    out = bio_open_owner(outfile, FORMAT_PEM, private);
145    if (out == NULL)
146        goto end;
147
148    BIO_printf(bio_err, "Generating RSA private key, %d bit long modulus (%d primes)\n",
149               num, primes);
150    rsa = eng ? RSA_new_method(eng) : RSA_new();
151    if (rsa == NULL)
152        goto end;
153
154    if (!BN_set_word(bn, f4)
155        || !RSA_generate_multi_prime_key(rsa, num, primes, bn, cb))
156        goto end;
157
158    RSA_get0_key(rsa, NULL, &e, NULL);
159    hexe = BN_bn2hex(e);
160    dece = BN_bn2dec(e);
161    if (hexe && dece) {
162        BIO_printf(bio_err, "e is %s (0x%s)\n", dece, hexe);
163    }
164    OPENSSL_free(hexe);
165    OPENSSL_free(dece);
166    cb_data.password = passout;
167    cb_data.prompt_info = outfile;
168    assert(private);
169    if (!PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0,
170                                     (pem_password_cb *)password_callback,
171                                     &cb_data))
172        goto end;
173
174    ret = 0;
175 end:
176    BN_free(bn);
177    BN_GENCB_free(cb);
178    RSA_free(rsa);
179    BIO_free_all(out);
180    release_engine(eng);
181    OPENSSL_free(passout);
182    if (ret != 0)
183        ERR_print_errors(bio_err);
184    return ret;
185}
186
187static int genrsa_cb(int p, int n, BN_GENCB *cb)
188{
189    char c = '*';
190
191    if (p == 0)
192        c = '.';
193    if (p == 1)
194        c = '+';
195    if (p == 2)
196        c = '*';
197    if (p == 3)
198        c = '\n';
199    BIO_write(BN_GENCB_get_arg(cb), &c, 1);
200    (void)BIO_flush(BN_GENCB_get_arg(cb));
201    return 1;
202}
203#endif
Note: See TracBrowser for help on using the repository browser.