source: rtems-libbsd/freebsd/crypto/openssl/apps/dsaparam.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.9 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 <stdlib.h>
19# include <time.h>
20# include <string.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
30static int dsa_cb(int p, int n, BN_GENCB *cb);
31
32typedef enum OPTION_choice {
33    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
34    OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
35    OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_R_ENUM
36} OPTION_CHOICE;
37
38const OPTIONS dsaparam_options[] = {
39    {"help", OPT_HELP, '-', "Display this summary"},
40    {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
41    {"in", OPT_IN, '<', "Input file"},
42    {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
43    {"out", OPT_OUT, '>', "Output file"},
44    {"text", OPT_TEXT, '-', "Print as text"},
45    {"C", OPT_C, '-', "Output C code"},
46    {"noout", OPT_NOOUT, '-', "No output"},
47    {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
48    OPT_R_OPTIONS,
49# ifndef OPENSSL_NO_ENGINE
50    {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
51# endif
52    {NULL}
53};
54
55int dsaparam_main(int argc, char **argv)
56{
57    ENGINE *e = NULL;
58    DSA *dsa = NULL;
59    BIO *in = NULL, *out = NULL;
60    BN_GENCB *cb = NULL;
61    int numbits = -1, num = 0, genkey = 0;
62    int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
63    int ret = 1, i, text = 0, private = 0;
64    char *infile = NULL, *outfile = NULL, *prog;
65    OPTION_CHOICE o;
66
67    prog = opt_init(argc, argv, dsaparam_options);
68    while ((o = opt_next()) != OPT_EOF) {
69        switch (o) {
70        case OPT_EOF:
71        case OPT_ERR:
72 opthelp:
73            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74            goto end;
75        case OPT_HELP:
76            opt_help(dsaparam_options);
77            ret = 0;
78            goto end;
79        case OPT_INFORM:
80            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
81                goto opthelp;
82            break;
83        case OPT_IN:
84            infile = opt_arg();
85            break;
86        case OPT_OUTFORM:
87            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
88                goto opthelp;
89            break;
90        case OPT_OUT:
91            outfile = opt_arg();
92            break;
93        case OPT_ENGINE:
94            e = setup_engine(opt_arg(), 0);
95            break;
96        case OPT_TEXT:
97            text = 1;
98            break;
99        case OPT_C:
100            C = 1;
101            break;
102        case OPT_GENKEY:
103            genkey = 1;
104            break;
105        case OPT_R_CASES:
106            if (!opt_rand(o))
107                goto end;
108            break;
109        case OPT_NOOUT:
110            noout = 1;
111            break;
112        }
113    }
114    argc = opt_num_rest();
115    argv = opt_rest();
116
117    if (argc == 1) {
118        if (!opt_int(argv[0], &num) || num < 0)
119            goto end;
120        /* generate a key */
121        numbits = num;
122    }
123    private = genkey ? 1 : 0;
124
125    in = bio_open_default(infile, 'r', informat);
126    if (in == NULL)
127        goto end;
128    out = bio_open_owner(outfile, outformat, private);
129    if (out == NULL)
130        goto end;
131
132    if (numbits > 0) {
133        if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
134            BIO_printf(bio_err,
135                       "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
136                       "         Your key size is %d! Larger key size may behave not as expected.\n",
137                       OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
138
139        cb = BN_GENCB_new();
140        if (cb == NULL) {
141            BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
142            goto end;
143        }
144        BN_GENCB_set(cb, dsa_cb, bio_err);
145        dsa = DSA_new();
146        if (dsa == NULL) {
147            BIO_printf(bio_err, "Error allocating DSA object\n");
148            goto end;
149        }
150        BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
151                   num);
152        BIO_printf(bio_err, "This could take some time\n");
153        if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
154            ERR_print_errors(bio_err);
155            BIO_printf(bio_err, "Error, DSA key generation failed\n");
156            goto end;
157        }
158    } else if (informat == FORMAT_ASN1) {
159        dsa = d2i_DSAparams_bio(in, NULL);
160    } else {
161        dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
162    }
163    if (dsa == NULL) {
164        BIO_printf(bio_err, "unable to load DSA parameters\n");
165        ERR_print_errors(bio_err);
166        goto end;
167    }
168
169    if (text) {
170        DSAparams_print(out, dsa);
171    }
172
173    if (C) {
174        const BIGNUM *p = NULL, *q = NULL, *g = NULL;
175        unsigned char *data;
176        int len, bits_p;
177
178        DSA_get0_pqg(dsa, &p, &q, &g);
179        len = BN_num_bytes(p);
180        bits_p = BN_num_bits(p);
181
182        data = app_malloc(len + 20, "BN space");
183
184        BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
185        print_bignum_var(bio_out, p, "dsap", bits_p, data);
186        print_bignum_var(bio_out, q, "dsaq", bits_p, data);
187        print_bignum_var(bio_out, g, "dsag", bits_p, data);
188        BIO_printf(bio_out, "    DSA *dsa = DSA_new();\n"
189                            "    BIGNUM *p, *q, *g;\n"
190                            "\n");
191        BIO_printf(bio_out, "    if (dsa == NULL)\n"
192                            "        return NULL;\n");
193        BIO_printf(bio_out, "    if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
194                   bits_p, bits_p);
195        BIO_printf(bio_out, "                           q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
196                   bits_p, bits_p);
197        BIO_printf(bio_out, "                           g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
198                   bits_p, bits_p);
199        BIO_printf(bio_out, "        DSA_free(dsa);\n"
200                            "        BN_free(p);\n"
201                            "        BN_free(q);\n"
202                            "        BN_free(g);\n"
203                            "        return NULL;\n"
204                            "    }\n"
205                            "    return dsa;\n}\n");
206        OPENSSL_free(data);
207    }
208
209    if (outformat == FORMAT_ASN1 && genkey)
210        noout = 1;
211
212    if (!noout) {
213        if (outformat == FORMAT_ASN1)
214            i = i2d_DSAparams_bio(out, dsa);
215        else
216            i = PEM_write_bio_DSAparams(out, dsa);
217        if (!i) {
218            BIO_printf(bio_err, "unable to write DSA parameters\n");
219            ERR_print_errors(bio_err);
220            goto end;
221        }
222    }
223    if (genkey) {
224        DSA *dsakey;
225
226        if ((dsakey = DSAparams_dup(dsa)) == NULL)
227            goto end;
228        if (!DSA_generate_key(dsakey)) {
229            ERR_print_errors(bio_err);
230            DSA_free(dsakey);
231            goto end;
232        }
233        assert(private);
234        if (outformat == FORMAT_ASN1)
235            i = i2d_DSAPrivateKey_bio(out, dsakey);
236        else
237            i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
238                                            NULL);
239        DSA_free(dsakey);
240    }
241    ret = 0;
242 end:
243    BN_GENCB_free(cb);
244    BIO_free(in);
245    BIO_free_all(out);
246    DSA_free(dsa);
247    release_engine(e);
248    return ret;
249}
250
251static int dsa_cb(int p, int n, BN_GENCB *cb)
252{
253    static const char symbols[] = ".+*\n";
254    char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
255
256    BIO_write(BN_GENCB_get_arg(cb), &c, 1);
257    (void)BIO_flush(BN_GENCB_get_arg(cb));
258    return 1;
259}
260#endif
Note: See TracBrowser for help on using the repository browser.