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