155714Skris/* apps/dsa.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296465Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59296465Sdelphij#include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */
60109998Smarkm#ifndef OPENSSL_NO_DSA
61296465Sdelphij# include <stdio.h>
62296465Sdelphij# include <stdlib.h>
63296465Sdelphij# include <string.h>
64296465Sdelphij# include <time.h>
65296465Sdelphij# include "apps.h"
66296465Sdelphij# include <openssl/bio.h>
67296465Sdelphij# include <openssl/err.h>
68296465Sdelphij# include <openssl/evp.h>
69296465Sdelphij# include <openssl/x509.h>
70296465Sdelphij# include <openssl/pem.h>
71296465Sdelphij# include <openssl/bn.h>
72296465Sdelphij# include <openssl/dsa.h>
7355714Skris
74296465Sdelphij# undef PROG
75296465Sdelphij# define PROG    dsa_main
7655714Skris
77296465Sdelphij/*-
78296465Sdelphij * -inform arg  - input format - default PEM (one of DER, NET or PEM)
7955714Skris * -outform arg - output format - default PEM
80296465Sdelphij * -in arg      - input file - default stdin
81296465Sdelphij * -out arg     - output file - default stdout
82296465Sdelphij * -des         - encrypt output if PEM format with DES in cbc mode
83296465Sdelphij * -des3        - encrypt output if PEM format
84296465Sdelphij * -idea        - encrypt output if PEM format
85296465Sdelphij * -aes128      - encrypt output if PEM format
86296465Sdelphij * -aes192      - encrypt output if PEM format
87296465Sdelphij * -aes256      - encrypt output if PEM format
88162911Ssimon * -camellia128 - encrypt output if PEM format
89162911Ssimon * -camellia192 - encrypt output if PEM format
90162911Ssimon * -camellia256 - encrypt output if PEM format
91194206Ssimon * -seed        - encrypt output if PEM format
92296465Sdelphij * -text        - print a text version
93296465Sdelphij * -modulus     - print the DSA public key
9455714Skris */
9555714Skris
9659191Skrisint MAIN(int, char **);
9759191Skris
9855714Skrisint MAIN(int argc, char **argv)
99296465Sdelphij{
100296465Sdelphij    ENGINE *e = NULL;
101296465Sdelphij    int ret = 1;
102296465Sdelphij    DSA *dsa = NULL;
103296465Sdelphij    int i, badops = 0;
104296465Sdelphij    const EVP_CIPHER *enc = NULL;
105296465Sdelphij    BIO *in = NULL, *out = NULL;
106296465Sdelphij    int informat, outformat, text = 0, noout = 0;
107296465Sdelphij    int pubin = 0, pubout = 0;
108296465Sdelphij    char *infile, *outfile, *prog;
109296465Sdelphij# ifndef OPENSSL_NO_ENGINE
110296465Sdelphij    char *engine;
111296465Sdelphij# endif
112296465Sdelphij    char *passargin = NULL, *passargout = NULL;
113296465Sdelphij    char *passin = NULL, *passout = NULL;
114296465Sdelphij    int modulus = 0;
11555714Skris
116296465Sdelphij    apps_startup();
11755714Skris
118296465Sdelphij    if (bio_err == NULL)
119296465Sdelphij        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
120296465Sdelphij            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
12155714Skris
122296465Sdelphij    if (!load_config(bio_err, NULL))
123296465Sdelphij        goto end;
124109998Smarkm
125296465Sdelphij# ifndef OPENSSL_NO_ENGINE
126296465Sdelphij    engine = NULL;
127296465Sdelphij# endif
128296465Sdelphij    infile = NULL;
129296465Sdelphij    outfile = NULL;
130296465Sdelphij    informat = FORMAT_PEM;
131296465Sdelphij    outformat = FORMAT_PEM;
13255714Skris
133296465Sdelphij    prog = argv[0];
134296465Sdelphij    argc--;
135296465Sdelphij    argv++;
136296465Sdelphij    while (argc >= 1) {
137296465Sdelphij        if (strcmp(*argv, "-inform") == 0) {
138296465Sdelphij            if (--argc < 1)
139296465Sdelphij                goto bad;
140296465Sdelphij            informat = str2fmt(*(++argv));
141296465Sdelphij        } else if (strcmp(*argv, "-outform") == 0) {
142296465Sdelphij            if (--argc < 1)
143296465Sdelphij                goto bad;
144296465Sdelphij            outformat = str2fmt(*(++argv));
145296465Sdelphij        } else if (strcmp(*argv, "-in") == 0) {
146296465Sdelphij            if (--argc < 1)
147296465Sdelphij                goto bad;
148296465Sdelphij            infile = *(++argv);
149296465Sdelphij        } else if (strcmp(*argv, "-out") == 0) {
150296465Sdelphij            if (--argc < 1)
151296465Sdelphij                goto bad;
152296465Sdelphij            outfile = *(++argv);
153296465Sdelphij        } else if (strcmp(*argv, "-passin") == 0) {
154296465Sdelphij            if (--argc < 1)
155296465Sdelphij                goto bad;
156296465Sdelphij            passargin = *(++argv);
157296465Sdelphij        } else if (strcmp(*argv, "-passout") == 0) {
158296465Sdelphij            if (--argc < 1)
159296465Sdelphij                goto bad;
160296465Sdelphij            passargout = *(++argv);
161296465Sdelphij        }
162296465Sdelphij# ifndef OPENSSL_NO_ENGINE
163296465Sdelphij        else if (strcmp(*argv, "-engine") == 0) {
164296465Sdelphij            if (--argc < 1)
165296465Sdelphij                goto bad;
166296465Sdelphij            engine = *(++argv);
167296465Sdelphij        }
168296465Sdelphij# endif
169296465Sdelphij        else if (strcmp(*argv, "-noout") == 0)
170296465Sdelphij            noout = 1;
171296465Sdelphij        else if (strcmp(*argv, "-text") == 0)
172296465Sdelphij            text = 1;
173296465Sdelphij        else if (strcmp(*argv, "-modulus") == 0)
174296465Sdelphij            modulus = 1;
175296465Sdelphij        else if (strcmp(*argv, "-pubin") == 0)
176296465Sdelphij            pubin = 1;
177296465Sdelphij        else if (strcmp(*argv, "-pubout") == 0)
178296465Sdelphij            pubout = 1;
179296465Sdelphij        else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
180296465Sdelphij            BIO_printf(bio_err, "unknown option %s\n", *argv);
181296465Sdelphij            badops = 1;
182296465Sdelphij            break;
183296465Sdelphij        }
184296465Sdelphij        argc--;
185296465Sdelphij        argv++;
186296465Sdelphij    }
18755714Skris
188296465Sdelphij    if (badops) {
189296465Sdelphij bad:
190296465Sdelphij        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
191296465Sdelphij        BIO_printf(bio_err, "where options are\n");
192296465Sdelphij        BIO_printf(bio_err, " -inform arg     input format - DER or PEM\n");
193296465Sdelphij        BIO_printf(bio_err, " -outform arg    output format - DER or PEM\n");
194296465Sdelphij        BIO_printf(bio_err, " -in arg         input file\n");
195296465Sdelphij        BIO_printf(bio_err,
196296465Sdelphij                   " -passin arg     input file pass phrase source\n");
197296465Sdelphij        BIO_printf(bio_err, " -out arg        output file\n");
198296465Sdelphij        BIO_printf(bio_err,
199296465Sdelphij                   " -passout arg    output file pass phrase source\n");
200296465Sdelphij# ifndef OPENSSL_NO_ENGINE
201296465Sdelphij        BIO_printf(bio_err,
202296465Sdelphij                   " -engine e       use engine e, possibly a hardware device.\n");
203296465Sdelphij# endif
204296465Sdelphij        BIO_printf(bio_err,
205296465Sdelphij                   " -des            encrypt PEM output with cbc des\n");
206296465Sdelphij        BIO_printf(bio_err,
207296465Sdelphij                   " -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
208296465Sdelphij# ifndef OPENSSL_NO_IDEA
209296465Sdelphij        BIO_printf(bio_err,
210296465Sdelphij                   " -idea           encrypt PEM output with cbc idea\n");
211296465Sdelphij# endif
212296465Sdelphij# ifndef OPENSSL_NO_AES
213296465Sdelphij        BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
214296465Sdelphij        BIO_printf(bio_err,
215296465Sdelphij                   "                 encrypt PEM output with cbc aes\n");
216296465Sdelphij# endif
217296465Sdelphij# ifndef OPENSSL_NO_CAMELLIA
218296465Sdelphij        BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
219296465Sdelphij        BIO_printf(bio_err,
220296465Sdelphij                   "                 encrypt PEM output with cbc camellia\n");
221296465Sdelphij# endif
222296465Sdelphij# ifndef OPENSSL_NO_SEED
223296465Sdelphij        BIO_printf(bio_err,
224296465Sdelphij                   " -seed           encrypt PEM output with cbc seed\n");
225296465Sdelphij# endif
226296465Sdelphij        BIO_printf(bio_err, " -text           print the key in text\n");
227296465Sdelphij        BIO_printf(bio_err, " -noout          don't print key out\n");
228296465Sdelphij        BIO_printf(bio_err, " -modulus        print the DSA public value\n");
229296465Sdelphij        goto end;
230296465Sdelphij    }
23155714Skris
232296465Sdelphij    ERR_load_crypto_strings();
23355714Skris
234296465Sdelphij# ifndef OPENSSL_NO_ENGINE
235296465Sdelphij    e = setup_engine(bio_err, engine, 0);
236296465Sdelphij# endif
237109998Smarkm
238296465Sdelphij    if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
239296465Sdelphij        BIO_printf(bio_err, "Error getting passwords\n");
240296465Sdelphij        goto end;
241296465Sdelphij    }
24259191Skris
243296465Sdelphij    out = BIO_new(BIO_s_file());
244296465Sdelphij    if (out == NULL) {
245296465Sdelphij        ERR_print_errors(bio_err);
246296465Sdelphij        goto end;
247296465Sdelphij    }
24855714Skris
249296465Sdelphij    BIO_printf(bio_err, "read DSA key\n");
250296465Sdelphij    {
251296465Sdelphij        EVP_PKEY *pkey;
252296465Sdelphij        if (pubin)
253296465Sdelphij            pkey = load_pubkey(bio_err, infile, informat, 1,
254296465Sdelphij                               passin, e, "Public Key");
255296465Sdelphij        else
256296465Sdelphij            pkey = load_key(bio_err, infile, informat, 1,
257296465Sdelphij                            passin, e, "Private Key");
25855714Skris
259296465Sdelphij        if (pkey != NULL)
260296465Sdelphij            dsa = pkey == NULL ? NULL : EVP_PKEY_get1_DSA(pkey);
261296465Sdelphij        EVP_PKEY_free(pkey);
262296465Sdelphij    }
263296465Sdelphij    if (dsa == NULL) {
264296465Sdelphij        BIO_printf(bio_err, "unable to load Key\n");
265296465Sdelphij        ERR_print_errors(bio_err);
266296465Sdelphij        goto end;
267296465Sdelphij    }
26855714Skris
269296465Sdelphij    if (outfile == NULL) {
270296465Sdelphij        BIO_set_fp(out, stdout, BIO_NOCLOSE);
271296465Sdelphij# ifdef OPENSSL_SYS_VMS
272296465Sdelphij        {
273296465Sdelphij            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
274296465Sdelphij            out = BIO_push(tmpbio, out);
275296465Sdelphij        }
276296465Sdelphij# endif
277296465Sdelphij    } else {
278296465Sdelphij        if (BIO_write_filename(out, outfile) <= 0) {
279296465Sdelphij            perror(outfile);
280296465Sdelphij            goto end;
281296465Sdelphij        }
282296465Sdelphij    }
28355714Skris
284296465Sdelphij    if (text)
285296465Sdelphij        if (!DSA_print(out, dsa, 0)) {
286296465Sdelphij            perror(outfile);
287296465Sdelphij            ERR_print_errors(bio_err);
288296465Sdelphij            goto end;
289296465Sdelphij        }
29055714Skris
291296465Sdelphij    if (modulus) {
292296465Sdelphij        fprintf(stdout, "Public Key=");
293296465Sdelphij        BN_print(out, dsa->pub_key);
294296465Sdelphij        fprintf(stdout, "\n");
295296465Sdelphij    }
29655714Skris
297296465Sdelphij    if (noout)
298296465Sdelphij        goto end;
299296465Sdelphij    BIO_printf(bio_err, "writing DSA key\n");
300296465Sdelphij    if (outformat == FORMAT_ASN1) {
301296465Sdelphij        if (pubin || pubout)
302296465Sdelphij            i = i2d_DSA_PUBKEY_bio(out, dsa);
303296465Sdelphij        else
304296465Sdelphij            i = i2d_DSAPrivateKey_bio(out, dsa);
305296465Sdelphij    } else if (outformat == FORMAT_PEM) {
306296465Sdelphij        if (pubin || pubout)
307296465Sdelphij            i = PEM_write_bio_DSA_PUBKEY(out, dsa);
308296465Sdelphij        else
309296465Sdelphij            i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
310296465Sdelphij                                            NULL, 0, NULL, passout);
311296465Sdelphij    } else {
312296465Sdelphij        BIO_printf(bio_err, "bad output format specified for outfile\n");
313296465Sdelphij        goto end;
314296465Sdelphij    }
315296465Sdelphij    if (!i) {
316296465Sdelphij        BIO_printf(bio_err, "unable to write private key\n");
317296465Sdelphij        ERR_print_errors(bio_err);
318296465Sdelphij    } else
319296465Sdelphij        ret = 0;
320296465Sdelphij end:
321296465Sdelphij    if (in != NULL)
322296465Sdelphij        BIO_free(in);
323296465Sdelphij    if (out != NULL)
324296465Sdelphij        BIO_free_all(out);
325296465Sdelphij    if (dsa != NULL)
326296465Sdelphij        DSA_free(dsa);
327296465Sdelphij    if (passin)
328296465Sdelphij        OPENSSL_free(passin);
329296465Sdelphij    if (passout)
330296465Sdelphij        OPENSSL_free(passout);
331296465Sdelphij    apps_shutdown();
332296465Sdelphij    OPENSSL_EXIT(ret);
333296465Sdelphij}
33455714Skris#endif
335