rsa.c revision 280297
155714Skris/* apps/rsa.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.
8280297Sjkim *
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).
15280297Sjkim *
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.
22280297Sjkim *
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 :-).
37280297Sjkim * 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)"
40280297Sjkim *
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.
52280297Sjkim *
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
59160814Ssimon#include <openssl/opensslconf.h>
60109998Smarkm#ifndef OPENSSL_NO_RSA
61280297Sjkim# include <stdio.h>
62280297Sjkim# include <stdlib.h>
63280297Sjkim# include <string.h>
64280297Sjkim# include <time.h>
65280297Sjkim# include "apps.h"
66280297Sjkim# include <openssl/bio.h>
67280297Sjkim# include <openssl/err.h>
68280297Sjkim# include <openssl/rsa.h>
69280297Sjkim# include <openssl/evp.h>
70280297Sjkim# include <openssl/x509.h>
71280297Sjkim# include <openssl/pem.h>
72280297Sjkim# include <openssl/bn.h>
7355714Skris
74280297Sjkim# undef PROG
75280297Sjkim# define PROG    rsa_main
7655714Skris
77280297Sjkim/*-
78280297Sjkim * -inform arg  - input format - default PEM (one of DER, NET or PEM)
7955714Skris * -outform arg - output format - default PEM
80280297Sjkim * -in arg      - input file - default stdin
81280297Sjkim * -out arg     - output file - default stdout
82280297Sjkim * -des         - encrypt output if PEM format with DES in cbc mode
83280297Sjkim * -des3        - encrypt output if PEM format
84280297Sjkim * -idea        - encrypt output if PEM format
85280297Sjkim * -seed        - encrypt output if PEM format
86280297Sjkim * -aes128      - encrypt output if PEM format
87280297Sjkim * -aes192      - encrypt output if PEM format
88280297Sjkim * -aes256      - encrypt output if PEM format
89162911Ssimon * -camellia128 - encrypt output if PEM format
90162911Ssimon * -camellia192 - encrypt output if PEM format
91162911Ssimon * -camellia256 - encrypt output if PEM format
92280297Sjkim * -text        - print a text version
93280297Sjkim * -modulus     - print the RSA key modulus
94280297Sjkim * -check       - verify key consistency
95280297Sjkim * -pubin       - Expect a public key in input file.
96280297Sjkim * -pubout      - Output a public key.
9755714Skris */
9855714Skris
9959191Skrisint MAIN(int, char **);
10059191Skris
10155714Skrisint MAIN(int argc, char **argv)
102280297Sjkim{
103280297Sjkim    ENGINE *e = NULL;
104280297Sjkim    int ret = 1;
105280297Sjkim    RSA *rsa = NULL;
106280297Sjkim    int i, badops = 0, sgckey = 0;
107280297Sjkim    const EVP_CIPHER *enc = NULL;
108280297Sjkim    BIO *out = NULL;
109280297Sjkim    int informat, outformat, text = 0, check = 0, noout = 0;
110280297Sjkim    int pubin = 0, pubout = 0;
111280297Sjkim    char *infile, *outfile, *prog;
112280297Sjkim    char *passargin = NULL, *passargout = NULL;
113280297Sjkim    char *passin = NULL, *passout = NULL;
114280297Sjkim# ifndef OPENSSL_NO_ENGINE
115280297Sjkim    char *engine = NULL;
116280297Sjkim# endif
117280297Sjkim    int modulus = 0;
11855714Skris
119280297Sjkim    int pvk_encr = 2;
120238405Sjkim
121280297Sjkim    apps_startup();
12255714Skris
123280297Sjkim    if (bio_err == NULL)
124280297Sjkim        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
125280297Sjkim            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
12655714Skris
127280297Sjkim    if (!load_config(bio_err, NULL))
128280297Sjkim        goto end;
129109998Smarkm
130280297Sjkim    infile = NULL;
131280297Sjkim    outfile = NULL;
132280297Sjkim    informat = FORMAT_PEM;
133280297Sjkim    outformat = FORMAT_PEM;
13455714Skris
135280297Sjkim    prog = argv[0];
136280297Sjkim    argc--;
137280297Sjkim    argv++;
138280297Sjkim    while (argc >= 1) {
139280297Sjkim        if (strcmp(*argv, "-inform") == 0) {
140280297Sjkim            if (--argc < 1)
141280297Sjkim                goto bad;
142280297Sjkim            informat = str2fmt(*(++argv));
143280297Sjkim        } else if (strcmp(*argv, "-outform") == 0) {
144280297Sjkim            if (--argc < 1)
145280297Sjkim                goto bad;
146280297Sjkim            outformat = str2fmt(*(++argv));
147280297Sjkim        } else if (strcmp(*argv, "-in") == 0) {
148280297Sjkim            if (--argc < 1)
149280297Sjkim                goto bad;
150280297Sjkim            infile = *(++argv);
151280297Sjkim        } else if (strcmp(*argv, "-out") == 0) {
152280297Sjkim            if (--argc < 1)
153280297Sjkim                goto bad;
154280297Sjkim            outfile = *(++argv);
155280297Sjkim        } else if (strcmp(*argv, "-passin") == 0) {
156280297Sjkim            if (--argc < 1)
157280297Sjkim                goto bad;
158280297Sjkim            passargin = *(++argv);
159280297Sjkim        } else if (strcmp(*argv, "-passout") == 0) {
160280297Sjkim            if (--argc < 1)
161280297Sjkim                goto bad;
162280297Sjkim            passargout = *(++argv);
163280297Sjkim        }
164280297Sjkim# ifndef OPENSSL_NO_ENGINE
165280297Sjkim        else if (strcmp(*argv, "-engine") == 0) {
166280297Sjkim            if (--argc < 1)
167280297Sjkim                goto bad;
168280297Sjkim            engine = *(++argv);
169280297Sjkim        }
170280297Sjkim# endif
171280297Sjkim        else if (strcmp(*argv, "-sgckey") == 0)
172280297Sjkim            sgckey = 1;
173280297Sjkim        else if (strcmp(*argv, "-pubin") == 0)
174280297Sjkim            pubin = 1;
175280297Sjkim        else if (strcmp(*argv, "-pubout") == 0)
176280297Sjkim            pubout = 1;
177280297Sjkim        else if (strcmp(*argv, "-RSAPublicKey_in") == 0)
178280297Sjkim            pubin = 2;
179280297Sjkim        else if (strcmp(*argv, "-RSAPublicKey_out") == 0)
180280297Sjkim            pubout = 2;
181280297Sjkim        else if (strcmp(*argv, "-pvk-strong") == 0)
182280297Sjkim            pvk_encr = 2;
183280297Sjkim        else if (strcmp(*argv, "-pvk-weak") == 0)
184280297Sjkim            pvk_encr = 1;
185280297Sjkim        else if (strcmp(*argv, "-pvk-none") == 0)
186280297Sjkim            pvk_encr = 0;
187280297Sjkim        else if (strcmp(*argv, "-noout") == 0)
188280297Sjkim            noout = 1;
189280297Sjkim        else if (strcmp(*argv, "-text") == 0)
190280297Sjkim            text = 1;
191280297Sjkim        else if (strcmp(*argv, "-modulus") == 0)
192280297Sjkim            modulus = 1;
193280297Sjkim        else if (strcmp(*argv, "-check") == 0)
194280297Sjkim            check = 1;
195280297Sjkim        else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
196280297Sjkim            BIO_printf(bio_err, "unknown option %s\n", *argv);
197280297Sjkim            badops = 1;
198280297Sjkim            break;
199280297Sjkim        }
200280297Sjkim        argc--;
201280297Sjkim        argv++;
202280297Sjkim    }
20355714Skris
204280297Sjkim    if (badops) {
205280297Sjkim bad:
206280297Sjkim        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
207280297Sjkim        BIO_printf(bio_err, "where options are\n");
208280297Sjkim        BIO_printf(bio_err,
209280297Sjkim                   " -inform arg     input format - one of DER NET PEM\n");
210280297Sjkim        BIO_printf(bio_err,
211280297Sjkim                   " -outform arg    output format - one of DER NET PEM\n");
212280297Sjkim        BIO_printf(bio_err, " -in arg         input file\n");
213280297Sjkim        BIO_printf(bio_err, " -sgckey         Use IIS SGC key format\n");
214280297Sjkim        BIO_printf(bio_err,
215280297Sjkim                   " -passin arg     input file pass phrase source\n");
216280297Sjkim        BIO_printf(bio_err, " -out arg        output file\n");
217280297Sjkim        BIO_printf(bio_err,
218280297Sjkim                   " -passout arg    output file pass phrase source\n");
219280297Sjkim        BIO_printf(bio_err,
220280297Sjkim                   " -des            encrypt PEM output with cbc des\n");
221280297Sjkim        BIO_printf(bio_err,
222280297Sjkim                   " -des3           encrypt PEM output with ede cbc des using 168 bit key\n");
223280297Sjkim# ifndef OPENSSL_NO_IDEA
224280297Sjkim        BIO_printf(bio_err,
225280297Sjkim                   " -idea           encrypt PEM output with cbc idea\n");
226280297Sjkim# endif
227280297Sjkim# ifndef OPENSSL_NO_SEED
228280297Sjkim        BIO_printf(bio_err,
229280297Sjkim                   " -seed           encrypt PEM output with cbc seed\n");
230280297Sjkim# endif
231280297Sjkim# ifndef OPENSSL_NO_AES
232280297Sjkim        BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
233280297Sjkim        BIO_printf(bio_err,
234280297Sjkim                   "                 encrypt PEM output with cbc aes\n");
235280297Sjkim# endif
236280297Sjkim# ifndef OPENSSL_NO_CAMELLIA
237280297Sjkim        BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n");
238280297Sjkim        BIO_printf(bio_err,
239280297Sjkim                   "                 encrypt PEM output with cbc camellia\n");
240280297Sjkim# endif
241280297Sjkim        BIO_printf(bio_err, " -text           print the key in text\n");
242280297Sjkim        BIO_printf(bio_err, " -noout          don't print key out\n");
243280297Sjkim        BIO_printf(bio_err, " -modulus        print the RSA key modulus\n");
244280297Sjkim        BIO_printf(bio_err, " -check          verify key consistency\n");
245280297Sjkim        BIO_printf(bio_err,
246280297Sjkim                   " -pubin          expect a public key in input file\n");
247280297Sjkim        BIO_printf(bio_err, " -pubout         output a public key\n");
248280297Sjkim# ifndef OPENSSL_NO_ENGINE
249280297Sjkim        BIO_printf(bio_err,
250280297Sjkim                   " -engine e       use engine e, possibly a hardware device.\n");
251280297Sjkim# endif
252280297Sjkim        goto end;
253280297Sjkim    }
25455714Skris
255280297Sjkim    ERR_load_crypto_strings();
25655714Skris
257280297Sjkim# ifndef OPENSSL_NO_ENGINE
258280297Sjkim    e = setup_engine(bio_err, engine, 0);
259280297Sjkim# endif
260109998Smarkm
261280297Sjkim    if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
262280297Sjkim        BIO_printf(bio_err, "Error getting passwords\n");
263280297Sjkim        goto end;
264280297Sjkim    }
26559191Skris
266280297Sjkim    if (check && pubin) {
267280297Sjkim        BIO_printf(bio_err, "Only private keys can be checked\n");
268280297Sjkim        goto end;
269280297Sjkim    }
27059191Skris
271280297Sjkim    out = BIO_new(BIO_s_file());
27255714Skris
273280297Sjkim    {
274280297Sjkim        EVP_PKEY *pkey;
27555714Skris
276280297Sjkim        if (pubin) {
277280297Sjkim            int tmpformat = -1;
278280297Sjkim            if (pubin == 2) {
279280297Sjkim                if (informat == FORMAT_PEM)
280280297Sjkim                    tmpformat = FORMAT_PEMRSA;
281280297Sjkim                else if (informat == FORMAT_ASN1)
282280297Sjkim                    tmpformat = FORMAT_ASN1RSA;
283280297Sjkim            } else if (informat == FORMAT_NETSCAPE && sgckey)
284280297Sjkim                tmpformat = FORMAT_IISSGC;
285280297Sjkim            else
286280297Sjkim                tmpformat = informat;
287109998Smarkm
288280297Sjkim            pkey = load_pubkey(bio_err, infile, tmpformat, 1,
289280297Sjkim                               passin, e, "Public Key");
290280297Sjkim        } else
291280297Sjkim            pkey = load_key(bio_err, infile,
292280297Sjkim                            (informat == FORMAT_NETSCAPE && sgckey ?
293280297Sjkim                             FORMAT_IISSGC : informat), 1,
294280297Sjkim                            passin, e, "Private Key");
29555714Skris
296280297Sjkim        if (pkey != NULL)
297280297Sjkim            rsa = EVP_PKEY_get1_RSA(pkey);
298280297Sjkim        EVP_PKEY_free(pkey);
299280297Sjkim    }
30055714Skris
301280297Sjkim    if (rsa == NULL) {
302280297Sjkim        ERR_print_errors(bio_err);
303280297Sjkim        goto end;
304280297Sjkim    }
30555714Skris
306280297Sjkim    if (outfile == NULL) {
307280297Sjkim        BIO_set_fp(out, stdout, BIO_NOCLOSE);
308280297Sjkim# ifdef OPENSSL_SYS_VMS
309280297Sjkim        {
310280297Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
311280297Sjkim            out = BIO_push(tmpbio, out);
312280297Sjkim        }
313280297Sjkim# endif
314280297Sjkim    } else {
315280297Sjkim        if (BIO_write_filename(out, outfile) <= 0) {
316280297Sjkim            perror(outfile);
317280297Sjkim            goto end;
318280297Sjkim        }
319280297Sjkim    }
32055714Skris
321280297Sjkim    if (text)
322280297Sjkim        if (!RSA_print(out, rsa, 0)) {
323280297Sjkim            perror(outfile);
324280297Sjkim            ERR_print_errors(bio_err);
325280297Sjkim            goto end;
326280297Sjkim        }
32755714Skris
328280297Sjkim    if (modulus) {
329280297Sjkim        BIO_printf(out, "Modulus=");
330280297Sjkim        BN_print(out, rsa->n);
331280297Sjkim        BIO_printf(out, "\n");
332280297Sjkim    }
33355714Skris
334280297Sjkim    if (check) {
335280297Sjkim        int r = RSA_check_key(rsa);
33655714Skris
337280297Sjkim        if (r == 1)
338280297Sjkim            BIO_printf(out, "RSA key ok\n");
339280297Sjkim        else if (r == 0) {
340280297Sjkim            unsigned long err;
34155714Skris
342280297Sjkim            while ((err = ERR_peek_error()) != 0 &&
343280297Sjkim                   ERR_GET_LIB(err) == ERR_LIB_RSA &&
344280297Sjkim                   ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
345280297Sjkim                   ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
346280297Sjkim                BIO_printf(out, "RSA key error: %s\n",
347280297Sjkim                           ERR_reason_error_string(err));
348280297Sjkim                ERR_get_error(); /* remove e from error stack */
349280297Sjkim            }
350280297Sjkim        }
35159191Skris
352280297Sjkim        if (r == -1 || ERR_peek_error() != 0) { /* should happen only if r ==
353280297Sjkim                                                 * -1 */
354280297Sjkim            ERR_print_errors(bio_err);
355280297Sjkim            goto end;
356280297Sjkim        }
357280297Sjkim    }
358280297Sjkim
359280297Sjkim    if (noout) {
360280297Sjkim        ret = 0;
361280297Sjkim        goto end;
362280297Sjkim    }
363280297Sjkim    BIO_printf(bio_err, "writing RSA key\n");
364280297Sjkim    if (outformat == FORMAT_ASN1) {
365280297Sjkim        if (pubout || pubin) {
366280297Sjkim            if (pubout == 2)
367280297Sjkim                i = i2d_RSAPublicKey_bio(out, rsa);
368280297Sjkim            else
369280297Sjkim                i = i2d_RSA_PUBKEY_bio(out, rsa);
370280297Sjkim        } else
371280297Sjkim            i = i2d_RSAPrivateKey_bio(out, rsa);
372280297Sjkim    }
373280297Sjkim# ifndef OPENSSL_NO_RC4
374280297Sjkim    else if (outformat == FORMAT_NETSCAPE) {
375280297Sjkim        unsigned char *p, *pp;
376280297Sjkim        int size;
377280297Sjkim
378280297Sjkim        i = 1;
379280297Sjkim        size = i2d_RSA_NET(rsa, NULL, NULL, sgckey);
380280297Sjkim        if ((p = (unsigned char *)OPENSSL_malloc(size)) == NULL) {
381280297Sjkim            BIO_printf(bio_err, "Memory allocation failure\n");
382280297Sjkim            goto end;
383280297Sjkim        }
384280297Sjkim        pp = p;
385280297Sjkim        i2d_RSA_NET(rsa, &p, NULL, sgckey);
386280297Sjkim        BIO_write(out, (char *)pp, size);
387280297Sjkim        OPENSSL_free(pp);
388280297Sjkim    }
389280297Sjkim# endif
390280297Sjkim    else if (outformat == FORMAT_PEM) {
391280297Sjkim        if (pubout || pubin) {
392280297Sjkim            if (pubout == 2)
393280297Sjkim                i = PEM_write_bio_RSAPublicKey(out, rsa);
394280297Sjkim            else
395280297Sjkim                i = PEM_write_bio_RSA_PUBKEY(out, rsa);
396280297Sjkim        } else
397280297Sjkim            i = PEM_write_bio_RSAPrivateKey(out, rsa,
398280297Sjkim                                            enc, NULL, 0, NULL, passout);
399280297Sjkim# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
400280297Sjkim    } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
401280297Sjkim        EVP_PKEY *pk;
402280297Sjkim        pk = EVP_PKEY_new();
403280297Sjkim        EVP_PKEY_set1_RSA(pk, rsa);
404280297Sjkim        if (outformat == FORMAT_PVK)
405280297Sjkim            i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
406280297Sjkim        else if (pubin || pubout)
407280297Sjkim            i = i2b_PublicKey_bio(out, pk);
408280297Sjkim        else
409280297Sjkim            i = i2b_PrivateKey_bio(out, pk);
410280297Sjkim        EVP_PKEY_free(pk);
411280297Sjkim# endif
412280297Sjkim    } else {
413280297Sjkim        BIO_printf(bio_err, "bad output format specified for outfile\n");
414280297Sjkim        goto end;
415280297Sjkim    }
416280297Sjkim    if (i <= 0) {
417280297Sjkim        BIO_printf(bio_err, "unable to write key\n");
418280297Sjkim        ERR_print_errors(bio_err);
419280297Sjkim    } else
420280297Sjkim        ret = 0;
421280297Sjkim end:
422280297Sjkim    if (out != NULL)
423280297Sjkim        BIO_free_all(out);
424280297Sjkim    if (rsa != NULL)
425280297Sjkim        RSA_free(rsa);
426280297Sjkim    if (passin)
427280297Sjkim        OPENSSL_free(passin);
428280297Sjkim    if (passout)
429280297Sjkim        OPENSSL_free(passout);
430280297Sjkim    apps_shutdown();
431280297Sjkim    OPENSSL_EXIT(ret);
432280297Sjkim}
433280297Sjkim#else                           /* !OPENSSL_NO_RSA */
434280297Sjkim
43559191Skris# if PEDANTIC
436280297Sjkimstatic void *dummy = &dummy;
43759191Skris# endif
43859191Skris
43955714Skris#endif
440