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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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
59280304Sjkim#include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */
60109998Smarkm#ifndef OPENSSL_NO_DSA
61280304Sjkim# include <stdio.h>
62280304Sjkim# include <stdlib.h>
63280304Sjkim# include <string.h>
64280304Sjkim# include <time.h>
65280304Sjkim# include "apps.h"
66280304Sjkim# include <openssl/bio.h>
67280304Sjkim# include <openssl/err.h>
68280304Sjkim# include <openssl/dsa.h>
69280304Sjkim# include <openssl/evp.h>
70280304Sjkim# include <openssl/x509.h>
71280304Sjkim# include <openssl/pem.h>
72280304Sjkim# include <openssl/bn.h>
7355714Skris
74280304Sjkim# undef PROG
75280304Sjkim# define PROG    dsa_main
7655714Skris
77280304Sjkim/*-
78280304Sjkim * -inform arg  - input format - default PEM (one of DER, NET or PEM)
7955714Skris * -outform arg - output format - default PEM
80280304Sjkim * -in arg      - input file - default stdin
81280304Sjkim * -out arg     - output file - default stdout
82280304Sjkim * -des         - encrypt output if PEM format with DES in cbc mode
83280304Sjkim * -des3        - encrypt output if PEM format
84280304Sjkim * -idea        - encrypt output if PEM format
85280304Sjkim * -aes128      - encrypt output if PEM format
86280304Sjkim * -aes192      - encrypt output if PEM format
87280304Sjkim * -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
92280304Sjkim * -text        - print a text version
93280304Sjkim * -modulus     - print the DSA public key
9455714Skris */
9555714Skris
9659191Skrisint MAIN(int, char **);
9759191Skris
9855714Skrisint MAIN(int argc, char **argv)
99280304Sjkim{
100280304Sjkim    ENGINE *e = NULL;
101280304Sjkim    int ret = 1;
102280304Sjkim    DSA *dsa = NULL;
103280304Sjkim    int i, badops = 0;
104280304Sjkim    const EVP_CIPHER *enc = NULL;
105280304Sjkim    BIO *in = NULL, *out = NULL;
106280304Sjkim    int informat, outformat, text = 0, noout = 0;
107280304Sjkim    int pubin = 0, pubout = 0;
108280304Sjkim    char *infile, *outfile, *prog;
109280304Sjkim# ifndef OPENSSL_NO_ENGINE
110280304Sjkim    char *engine;
111280304Sjkim# endif
112280304Sjkim    char *passargin = NULL, *passargout = NULL;
113280304Sjkim    char *passin = NULL, *passout = NULL;
114280304Sjkim    int modulus = 0;
11555714Skris
116280304Sjkim    int pvk_encr = 2;
117238405Sjkim
118280304Sjkim    apps_startup();
11955714Skris
120280304Sjkim    if (bio_err == NULL)
121280304Sjkim        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
122280304Sjkim            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
12355714Skris
124280304Sjkim    if (!load_config(bio_err, NULL))
125280304Sjkim        goto end;
126109998Smarkm
127280304Sjkim# ifndef OPENSSL_NO_ENGINE
128280304Sjkim    engine = NULL;
129280304Sjkim# endif
130280304Sjkim    infile = NULL;
131280304Sjkim    outfile = NULL;
132280304Sjkim    informat = FORMAT_PEM;
133280304Sjkim    outformat = FORMAT_PEM;
13455714Skris
135280304Sjkim    prog = argv[0];
136280304Sjkim    argc--;
137280304Sjkim    argv++;
138280304Sjkim    while (argc >= 1) {
139280304Sjkim        if (strcmp(*argv, "-inform") == 0) {
140280304Sjkim            if (--argc < 1)
141280304Sjkim                goto bad;
142280304Sjkim            informat = str2fmt(*(++argv));
143280304Sjkim        } else if (strcmp(*argv, "-outform") == 0) {
144280304Sjkim            if (--argc < 1)
145280304Sjkim                goto bad;
146280304Sjkim            outformat = str2fmt(*(++argv));
147280304Sjkim        } else if (strcmp(*argv, "-in") == 0) {
148280304Sjkim            if (--argc < 1)
149280304Sjkim                goto bad;
150280304Sjkim            infile = *(++argv);
151280304Sjkim        } else if (strcmp(*argv, "-out") == 0) {
152280304Sjkim            if (--argc < 1)
153280304Sjkim                goto bad;
154280304Sjkim            outfile = *(++argv);
155280304Sjkim        } else if (strcmp(*argv, "-passin") == 0) {
156280304Sjkim            if (--argc < 1)
157280304Sjkim                goto bad;
158280304Sjkim            passargin = *(++argv);
159280304Sjkim        } else if (strcmp(*argv, "-passout") == 0) {
160280304Sjkim            if (--argc < 1)
161280304Sjkim                goto bad;
162280304Sjkim            passargout = *(++argv);
163280304Sjkim        }
164280304Sjkim# ifndef OPENSSL_NO_ENGINE
165280304Sjkim        else if (strcmp(*argv, "-engine") == 0) {
166280304Sjkim            if (--argc < 1)
167280304Sjkim                goto bad;
168280304Sjkim            engine = *(++argv);
169280304Sjkim        }
170280304Sjkim# endif
171280304Sjkim        else if (strcmp(*argv, "-pvk-strong") == 0)
172280304Sjkim            pvk_encr = 2;
173280304Sjkim        else if (strcmp(*argv, "-pvk-weak") == 0)
174280304Sjkim            pvk_encr = 1;
175280304Sjkim        else if (strcmp(*argv, "-pvk-none") == 0)
176280304Sjkim            pvk_encr = 0;
177280304Sjkim        else if (strcmp(*argv, "-noout") == 0)
178280304Sjkim            noout = 1;
179280304Sjkim        else if (strcmp(*argv, "-text") == 0)
180280304Sjkim            text = 1;
181280304Sjkim        else if (strcmp(*argv, "-modulus") == 0)
182280304Sjkim            modulus = 1;
183280304Sjkim        else if (strcmp(*argv, "-pubin") == 0)
184280304Sjkim            pubin = 1;
185280304Sjkim        else if (strcmp(*argv, "-pubout") == 0)
186280304Sjkim            pubout = 1;
187280304Sjkim        else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
188280304Sjkim            BIO_printf(bio_err, "unknown option %s\n", *argv);
189280304Sjkim            badops = 1;
190280304Sjkim            break;
191280304Sjkim        }
192280304Sjkim        argc--;
193280304Sjkim        argv++;
194280304Sjkim    }
19555714Skris
196280304Sjkim    if (badops) {
197280304Sjkim bad:
198280304Sjkim        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
199280304Sjkim        BIO_printf(bio_err, "where options are\n");
200280304Sjkim        BIO_printf(bio_err, " -inform arg     input format - DER or PEM\n");
201280304Sjkim        BIO_printf(bio_err, " -outform arg    output format - DER or PEM\n");
202280304Sjkim        BIO_printf(bio_err, " -in arg         input file\n");
203280304Sjkim        BIO_printf(bio_err,
204280304Sjkim                   " -passin arg     input file pass phrase source\n");
205280304Sjkim        BIO_printf(bio_err, " -out arg        output file\n");
206280304Sjkim        BIO_printf(bio_err,
207280304Sjkim                   " -passout arg    output file pass phrase source\n");
208280304Sjkim# ifndef OPENSSL_NO_ENGINE
209280304Sjkim        BIO_printf(bio_err,
210280304Sjkim                   " -engine e       use engine e, possibly a hardware device.\n");
211280304Sjkim# endif
212280304Sjkim        BIO_printf(bio_err,
213280304Sjkim                   " -des            encrypt PEM output with cbc des\n");
214280304Sjkim        BIO_printf(bio_err,
215280304Sjkim                   " -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
216280304Sjkim# ifndef OPENSSL_NO_IDEA
217280304Sjkim        BIO_printf(bio_err,
218280304Sjkim                   " -idea           encrypt PEM output with cbc idea\n");
219280304Sjkim# endif
220280304Sjkim# ifndef OPENSSL_NO_AES
221280304Sjkim        BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
222280304Sjkim        BIO_printf(bio_err,
223280304Sjkim                   "                 encrypt PEM output with cbc aes\n");
224280304Sjkim# endif
225280304Sjkim# ifndef OPENSSL_NO_CAMELLIA
226280304Sjkim        BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
227280304Sjkim        BIO_printf(bio_err,
228280304Sjkim                   "                 encrypt PEM output with cbc camellia\n");
229280304Sjkim# endif
230280304Sjkim# ifndef OPENSSL_NO_SEED
231280304Sjkim        BIO_printf(bio_err,
232280304Sjkim                   " -seed           encrypt PEM output with cbc seed\n");
233280304Sjkim# endif
234280304Sjkim        BIO_printf(bio_err, " -text           print the key in text\n");
235280304Sjkim        BIO_printf(bio_err, " -noout          don't print key out\n");
236280304Sjkim        BIO_printf(bio_err, " -modulus        print the DSA public value\n");
237280304Sjkim        goto end;
238280304Sjkim    }
23955714Skris
240280304Sjkim    ERR_load_crypto_strings();
24155714Skris
242280304Sjkim# ifndef OPENSSL_NO_ENGINE
243280304Sjkim    e = setup_engine(bio_err, engine, 0);
244280304Sjkim# endif
245109998Smarkm
246280304Sjkim    if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
247280304Sjkim        BIO_printf(bio_err, "Error getting passwords\n");
248280304Sjkim        goto end;
249280304Sjkim    }
25059191Skris
251280304Sjkim    in = BIO_new(BIO_s_file());
252280304Sjkim    out = BIO_new(BIO_s_file());
253280304Sjkim    if ((in == NULL) || (out == NULL)) {
254280304Sjkim        ERR_print_errors(bio_err);
255280304Sjkim        goto end;
256280304Sjkim    }
25755714Skris
258280304Sjkim    if (infile == NULL)
259280304Sjkim        BIO_set_fp(in, stdin, BIO_NOCLOSE);
260280304Sjkim    else {
261280304Sjkim        if (BIO_read_filename(in, infile) <= 0) {
262280304Sjkim            perror(infile);
263280304Sjkim            goto end;
264280304Sjkim        }
265280304Sjkim    }
266238405Sjkim
267280304Sjkim    BIO_printf(bio_err, "read DSA key\n");
268238405Sjkim
269280304Sjkim    {
270280304Sjkim        EVP_PKEY *pkey;
271238405Sjkim
272280304Sjkim        if (pubin)
273280304Sjkim            pkey = load_pubkey(bio_err, infile, informat, 1,
274280304Sjkim                               passin, e, "Public Key");
275280304Sjkim        else
276280304Sjkim            pkey = load_key(bio_err, infile, informat, 1,
277280304Sjkim                            passin, e, "Private Key");
27855714Skris
279280304Sjkim        if (pkey) {
280280304Sjkim            dsa = EVP_PKEY_get1_DSA(pkey);
281280304Sjkim            EVP_PKEY_free(pkey);
282280304Sjkim        }
283280304Sjkim    }
284280304Sjkim    if (dsa == NULL) {
285280304Sjkim        BIO_printf(bio_err, "unable to load Key\n");
286280304Sjkim        ERR_print_errors(bio_err);
287280304Sjkim        goto end;
288280304Sjkim    }
28955714Skris
290280304Sjkim    if (outfile == NULL) {
291280304Sjkim        BIO_set_fp(out, stdout, BIO_NOCLOSE);
292280304Sjkim# ifdef OPENSSL_SYS_VMS
293280304Sjkim        {
294280304Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
295280304Sjkim            out = BIO_push(tmpbio, out);
296280304Sjkim        }
297280304Sjkim# endif
298280304Sjkim    } else {
299280304Sjkim        if (BIO_write_filename(out, outfile) <= 0) {
300280304Sjkim            perror(outfile);
301280304Sjkim            goto end;
302280304Sjkim        }
303280304Sjkim    }
30455714Skris
305280304Sjkim    if (text)
306280304Sjkim        if (!DSA_print(out, dsa, 0)) {
307280304Sjkim            perror(outfile);
308280304Sjkim            ERR_print_errors(bio_err);
309280304Sjkim            goto end;
310280304Sjkim        }
31155714Skris
312280304Sjkim    if (modulus) {
313280304Sjkim        fprintf(stdout, "Public Key=");
314280304Sjkim        BN_print(out, dsa->pub_key);
315280304Sjkim        fprintf(stdout, "\n");
316280304Sjkim    }
31755714Skris
318280304Sjkim    if (noout)
319280304Sjkim        goto end;
320280304Sjkim    BIO_printf(bio_err, "writing DSA key\n");
321280304Sjkim    if (outformat == FORMAT_ASN1) {
322280304Sjkim        if (pubin || pubout)
323280304Sjkim            i = i2d_DSA_PUBKEY_bio(out, dsa);
324280304Sjkim        else
325280304Sjkim            i = i2d_DSAPrivateKey_bio(out, dsa);
326280304Sjkim    } else if (outformat == FORMAT_PEM) {
327280304Sjkim        if (pubin || pubout)
328280304Sjkim            i = PEM_write_bio_DSA_PUBKEY(out, dsa);
329280304Sjkim        else
330280304Sjkim            i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
331280304Sjkim                                            NULL, 0, NULL, passout);
332280304Sjkim# if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_RC4)
333280304Sjkim    } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
334280304Sjkim        EVP_PKEY *pk;
335280304Sjkim        pk = EVP_PKEY_new();
336280304Sjkim        EVP_PKEY_set1_DSA(pk, dsa);
337280304Sjkim        if (outformat == FORMAT_PVK)
338280304Sjkim            i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
339280304Sjkim        else if (pubin || pubout)
340280304Sjkim            i = i2b_PublicKey_bio(out, pk);
341280304Sjkim        else
342280304Sjkim            i = i2b_PrivateKey_bio(out, pk);
343280304Sjkim        EVP_PKEY_free(pk);
344280304Sjkim# endif
345280304Sjkim    } else {
346280304Sjkim        BIO_printf(bio_err, "bad output format specified for outfile\n");
347280304Sjkim        goto end;
348280304Sjkim    }
349280304Sjkim    if (i <= 0) {
350280304Sjkim        BIO_printf(bio_err, "unable to write private key\n");
351280304Sjkim        ERR_print_errors(bio_err);
352280304Sjkim    } else
353280304Sjkim        ret = 0;
354280304Sjkim end:
355280304Sjkim    if (in != NULL)
356280304Sjkim        BIO_free(in);
357280304Sjkim    if (out != NULL)
358280304Sjkim        BIO_free_all(out);
359280304Sjkim    if (dsa != NULL)
360280304Sjkim        DSA_free(dsa);
361280304Sjkim    if (passin)
362280304Sjkim        OPENSSL_free(passin);
363280304Sjkim    if (passout)
364280304Sjkim        OPENSSL_free(passout);
365280304Sjkim    apps_shutdown();
366280304Sjkim    OPENSSL_EXIT(ret);
367280304Sjkim}
368280304Sjkim#else                           /* !OPENSSL_NO_DSA */
369238405Sjkim
370238405Sjkim# if PEDANTIC
371280304Sjkimstatic void *dummy = &dummy;
372238405Sjkim# endif
373238405Sjkim
37455714Skris#endif
375