source: rtems-libbsd/freebsd/crypto/openssl/apps/gendsa.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: 4.0 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_DSA
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/dsa.h>
27# include <openssl/x509.h>
28# include <openssl/pem.h>
29
30typedef enum OPTION_choice {
31    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
32    OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER,
33    OPT_R_ENUM
34} OPTION_CHOICE;
35
36const OPTIONS gendsa_options[] = {
37    {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
38    {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
39    {"help", OPT_HELP, '-', "Display this summary"},
40    {"out", OPT_OUT, '>', "Output the key to the specified file"},
41    {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
42    OPT_R_OPTIONS,
43    {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
44# ifndef OPENSSL_NO_ENGINE
45    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
46# endif
47    {NULL}
48};
49
50int gendsa_main(int argc, char **argv)
51{
52    ENGINE *e = NULL;
53    BIO *out = NULL, *in = NULL;
54    DSA *dsa = NULL;
55    const EVP_CIPHER *enc = NULL;
56    char *dsaparams = NULL;
57    char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
58    OPTION_CHOICE o;
59    int ret = 1, private = 0;
60    const BIGNUM *p = NULL;
61
62    prog = opt_init(argc, argv, gendsa_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            ret = 0;
72            opt_help(gendsa_options);
73            goto end;
74        case OPT_OUT:
75            outfile = opt_arg();
76            break;
77        case OPT_PASSOUT:
78            passoutarg = opt_arg();
79            break;
80        case OPT_ENGINE:
81            e = setup_engine(opt_arg(), 0);
82            break;
83        case OPT_R_CASES:
84            if (!opt_rand(o))
85                goto end;
86            break;
87        case OPT_CIPHER:
88            if (!opt_cipher(opt_unknown(), &enc))
89                goto end;
90            break;
91        }
92    }
93    argc = opt_num_rest();
94    argv = opt_rest();
95    private = 1;
96
97    if (argc != 1)
98        goto opthelp;
99    dsaparams = *argv;
100
101    if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
102        BIO_printf(bio_err, "Error getting password\n");
103        goto end;
104    }
105
106    in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
107    if (in == NULL)
108        goto end2;
109
110    if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
111        BIO_printf(bio_err, "unable to load DSA parameter file\n");
112        goto end;
113    }
114    BIO_free(in);
115    in = NULL;
116
117    out = bio_open_owner(outfile, FORMAT_PEM, private);
118    if (out == NULL)
119        goto end2;
120
121    DSA_get0_pqg(dsa, &p, NULL, NULL);
122
123    if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
124        BIO_printf(bio_err,
125                   "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
126                   "         Your key size is %d! Larger key size may behave not as expected.\n",
127                   OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
128
129    BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
130    if (!DSA_generate_key(dsa))
131        goto end;
132
133    assert(private);
134    if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
135        goto end;
136    ret = 0;
137 end:
138    if (ret != 0)
139        ERR_print_errors(bio_err);
140 end2:
141    BIO_free(in);
142    BIO_free_all(out);
143    DSA_free(dsa);
144    release_engine(e);
145    OPENSSL_free(passout);
146    return ret;
147}
148#endif
Note: See TracBrowser for help on using the repository browser.