174664626SKris Kennaway /* apps/rsa.c */ 274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 374664626SKris Kennaway * All rights reserved. 474664626SKris Kennaway * 574664626SKris Kennaway * This package is an SSL implementation written 674664626SKris Kennaway * by Eric Young (eay@cryptsoft.com). 774664626SKris Kennaway * The implementation was written so as to conform with Netscapes SSL. 874664626SKris Kennaway * 974664626SKris Kennaway * This library is free for commercial and non-commercial use as long as 1074664626SKris Kennaway * the following conditions are aheared to. The following conditions 1174664626SKris Kennaway * apply to all code found in this distribution, be it the RC4, RSA, 1274664626SKris Kennaway * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1374664626SKris Kennaway * included with this distribution is covered by the same copyright terms 1474664626SKris Kennaway * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1574664626SKris Kennaway * 1674664626SKris Kennaway * Copyright remains Eric Young's, and as such any Copyright notices in 1774664626SKris Kennaway * the code are not to be removed. 1874664626SKris Kennaway * If this package is used in a product, Eric Young should be given attribution 1974664626SKris Kennaway * as the author of the parts of the library used. 2074664626SKris Kennaway * This can be in the form of a textual message at program startup or 2174664626SKris Kennaway * in documentation (online or textual) provided with the package. 2274664626SKris Kennaway * 2374664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 2474664626SKris Kennaway * modification, are permitted provided that the following conditions 2574664626SKris Kennaway * are met: 2674664626SKris Kennaway * 1. Redistributions of source code must retain the copyright 2774664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 2874664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 2974664626SKris Kennaway * notice, this list of conditions and the following disclaimer in the 3074664626SKris Kennaway * documentation and/or other materials provided with the distribution. 3174664626SKris Kennaway * 3. All advertising materials mentioning features or use of this software 3274664626SKris Kennaway * must display the following acknowledgement: 3374664626SKris Kennaway * "This product includes cryptographic software written by 3474664626SKris Kennaway * Eric Young (eay@cryptsoft.com)" 3574664626SKris Kennaway * The word 'cryptographic' can be left out if the rouines from the library 3674664626SKris Kennaway * being used are not cryptographic related :-). 3774664626SKris Kennaway * 4. If you include any Windows specific code (or a derivative thereof) from 3874664626SKris Kennaway * the apps directory (application code) you must include an acknowledgement: 3974664626SKris Kennaway * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4074664626SKris Kennaway * 4174664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4274664626SKris Kennaway * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4374664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4474664626SKris Kennaway * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4574664626SKris Kennaway * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4674664626SKris Kennaway * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4774664626SKris Kennaway * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4874664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4974664626SKris Kennaway * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5074664626SKris Kennaway * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5174664626SKris Kennaway * SUCH DAMAGE. 5274664626SKris Kennaway * 5374664626SKris Kennaway * The licence and distribution terms for any publically available version or 5474664626SKris Kennaway * derivative of this code cannot be changed. i.e. this code cannot simply be 5574664626SKris Kennaway * copied and put under another distribution licence 5674664626SKris Kennaway * [including the GNU Public Licence.] 5774664626SKris Kennaway */ 5874664626SKris Kennaway 593b4e3dcbSSimon L. B. Nielsen #include <openssl/opensslconf.h> 605c87c606SMark Murray #ifndef OPENSSL_NO_RSA 6174664626SKris Kennaway # include <stdio.h> 6274664626SKris Kennaway # include <stdlib.h> 6374664626SKris Kennaway # include <string.h> 6474664626SKris Kennaway # include <time.h> 6574664626SKris Kennaway # include "apps.h" 6674664626SKris Kennaway # include <openssl/bio.h> 6774664626SKris Kennaway # include <openssl/err.h> 6874664626SKris Kennaway # include <openssl/rsa.h> 6974664626SKris Kennaway # include <openssl/evp.h> 7074664626SKris Kennaway # include <openssl/x509.h> 7174664626SKris Kennaway # include <openssl/pem.h> 723b4e3dcbSSimon L. B. Nielsen # include <openssl/bn.h> 7374664626SKris Kennaway 7474664626SKris Kennaway # undef PROG 7574664626SKris Kennaway # define PROG rsa_main 7674664626SKris Kennaway 776f9291ceSJung-uk Kim /*- 786f9291ceSJung-uk Kim * -inform arg - input format - default PEM (one of DER, NET or PEM) 7974664626SKris Kennaway * -outform arg - output format - default PEM 8074664626SKris Kennaway * -in arg - input file - default stdin 8174664626SKris Kennaway * -out arg - output file - default stdout 8274664626SKris Kennaway * -des - encrypt output if PEM format with DES in cbc mode 8374664626SKris Kennaway * -des3 - encrypt output if PEM format 8474664626SKris Kennaway * -idea - encrypt output if PEM format 85db522d3aSSimon L. B. Nielsen * -seed - encrypt output if PEM format 865c87c606SMark Murray * -aes128 - encrypt output if PEM format 875c87c606SMark Murray * -aes192 - encrypt output if PEM format 885c87c606SMark Murray * -aes256 - encrypt output if PEM format 89ed5d4f9aSSimon L. B. Nielsen * -camellia128 - encrypt output if PEM format 90ed5d4f9aSSimon L. B. Nielsen * -camellia192 - encrypt output if PEM format 91ed5d4f9aSSimon L. B. Nielsen * -camellia256 - encrypt output if PEM format 9274664626SKris Kennaway * -text - print a text version 9374664626SKris Kennaway * -modulus - print the RSA key modulus 9474664626SKris Kennaway * -check - verify key consistency 95f579bf8eSKris Kennaway * -pubin - Expect a public key in input file. 96f579bf8eSKris Kennaway * -pubout - Output a public key. 9774664626SKris Kennaway */ 9874664626SKris Kennaway 99f579bf8eSKris Kennaway int MAIN(int, char **); 100f579bf8eSKris Kennaway 10174664626SKris Kennaway int MAIN(int argc, char **argv) 10274664626SKris Kennaway { 1035c87c606SMark Murray ENGINE *e = NULL; 10474664626SKris Kennaway int ret = 1; 10574664626SKris Kennaway RSA *rsa = NULL; 106ddd58736SKris Kennaway int i, badops = 0, sgckey = 0; 10774664626SKris Kennaway const EVP_CIPHER *enc = NULL; 1085c87c606SMark Murray BIO *out = NULL; 10974664626SKris Kennaway int informat, outformat, text = 0, check = 0, noout = 0; 110f579bf8eSKris Kennaway int pubin = 0, pubout = 0; 11174664626SKris Kennaway char *infile, *outfile, *prog; 112f579bf8eSKris Kennaway char *passargin = NULL, *passargout = NULL; 113f579bf8eSKris Kennaway char *passin = NULL, *passout = NULL; 1145c87c606SMark Murray char *engine = NULL; 11574664626SKris Kennaway int modulus = 0; 11674664626SKris Kennaway 1171f13597dSJung-uk Kim int pvk_encr = 2; 1181f13597dSJung-uk Kim 11974664626SKris Kennaway apps_startup(); 12074664626SKris Kennaway 12174664626SKris Kennaway if (bio_err == NULL) 12274664626SKris Kennaway if ((bio_err = BIO_new(BIO_s_file())) != NULL) 12374664626SKris Kennaway BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 12474664626SKris Kennaway 1255c87c606SMark Murray if (!load_config(bio_err, NULL)) 1265c87c606SMark Murray goto end; 1275c87c606SMark Murray 12874664626SKris Kennaway infile = NULL; 12974664626SKris Kennaway outfile = NULL; 13074664626SKris Kennaway informat = FORMAT_PEM; 13174664626SKris Kennaway outformat = FORMAT_PEM; 13274664626SKris Kennaway 13374664626SKris Kennaway prog = argv[0]; 13474664626SKris Kennaway argc--; 13574664626SKris Kennaway argv++; 1366f9291ceSJung-uk Kim while (argc >= 1) { 1376f9291ceSJung-uk Kim if (strcmp(*argv, "-inform") == 0) { 1386f9291ceSJung-uk Kim if (--argc < 1) 1396f9291ceSJung-uk Kim goto bad; 14074664626SKris Kennaway informat = str2fmt(*(++argv)); 1416f9291ceSJung-uk Kim } else if (strcmp(*argv, "-outform") == 0) { 1426f9291ceSJung-uk Kim if (--argc < 1) 1436f9291ceSJung-uk Kim goto bad; 14474664626SKris Kennaway outformat = str2fmt(*(++argv)); 1456f9291ceSJung-uk Kim } else if (strcmp(*argv, "-in") == 0) { 1466f9291ceSJung-uk Kim if (--argc < 1) 1476f9291ceSJung-uk Kim goto bad; 14874664626SKris Kennaway infile = *(++argv); 1496f9291ceSJung-uk Kim } else if (strcmp(*argv, "-out") == 0) { 1506f9291ceSJung-uk Kim if (--argc < 1) 1516f9291ceSJung-uk Kim goto bad; 15274664626SKris Kennaway outfile = *(++argv); 1536f9291ceSJung-uk Kim } else if (strcmp(*argv, "-passin") == 0) { 1546f9291ceSJung-uk Kim if (--argc < 1) 1556f9291ceSJung-uk Kim goto bad; 156f579bf8eSKris Kennaway passargin = *(++argv); 1576f9291ceSJung-uk Kim } else if (strcmp(*argv, "-passout") == 0) { 1586f9291ceSJung-uk Kim if (--argc < 1) 1596f9291ceSJung-uk Kim goto bad; 160f579bf8eSKris Kennaway passargout = *(++argv); 161f579bf8eSKris Kennaway } 162fceca8a3SJacques Vidrine # ifndef OPENSSL_NO_ENGINE 1636f9291ceSJung-uk Kim else if (strcmp(*argv, "-engine") == 0) { 1646f9291ceSJung-uk Kim if (--argc < 1) 1656f9291ceSJung-uk Kim goto bad; 1665c87c606SMark Murray engine = *(++argv); 1675c87c606SMark Murray } 168fceca8a3SJacques Vidrine # endif 169ddd58736SKris Kennaway else if (strcmp(*argv, "-sgckey") == 0) 170ddd58736SKris Kennaway sgckey = 1; 171f579bf8eSKris Kennaway else if (strcmp(*argv, "-pubin") == 0) 172f579bf8eSKris Kennaway pubin = 1; 173f579bf8eSKris Kennaway else if (strcmp(*argv, "-pubout") == 0) 174f579bf8eSKris Kennaway pubout = 1; 1751f13597dSJung-uk Kim else if (strcmp(*argv, "-RSAPublicKey_in") == 0) 1761f13597dSJung-uk Kim pubin = 2; 1771f13597dSJung-uk Kim else if (strcmp(*argv, "-RSAPublicKey_out") == 0) 1781f13597dSJung-uk Kim pubout = 2; 1791f13597dSJung-uk Kim else if (strcmp(*argv, "-pvk-strong") == 0) 1801f13597dSJung-uk Kim pvk_encr = 2; 1811f13597dSJung-uk Kim else if (strcmp(*argv, "-pvk-weak") == 0) 1821f13597dSJung-uk Kim pvk_encr = 1; 1831f13597dSJung-uk Kim else if (strcmp(*argv, "-pvk-none") == 0) 1841f13597dSJung-uk Kim pvk_encr = 0; 18574664626SKris Kennaway else if (strcmp(*argv, "-noout") == 0) 18674664626SKris Kennaway noout = 1; 18774664626SKris Kennaway else if (strcmp(*argv, "-text") == 0) 18874664626SKris Kennaway text = 1; 18974664626SKris Kennaway else if (strcmp(*argv, "-modulus") == 0) 19074664626SKris Kennaway modulus = 1; 19174664626SKris Kennaway else if (strcmp(*argv, "-check") == 0) 19274664626SKris Kennaway check = 1; 1936f9291ceSJung-uk Kim else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) { 19474664626SKris Kennaway BIO_printf(bio_err, "unknown option %s\n", *argv); 19574664626SKris Kennaway badops = 1; 19674664626SKris Kennaway break; 19774664626SKris Kennaway } 19874664626SKris Kennaway argc--; 19974664626SKris Kennaway argv++; 20074664626SKris Kennaway } 20174664626SKris Kennaway 2026f9291ceSJung-uk Kim if (badops) { 20374664626SKris Kennaway bad: 20474664626SKris Kennaway BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog); 20574664626SKris Kennaway BIO_printf(bio_err, "where options are\n"); 2066f9291ceSJung-uk Kim BIO_printf(bio_err, 2076f9291ceSJung-uk Kim " -inform arg input format - one of DER NET PEM\n"); 2086f9291ceSJung-uk Kim BIO_printf(bio_err, 2096f9291ceSJung-uk Kim " -outform arg output format - one of DER NET PEM\n"); 21074664626SKris Kennaway BIO_printf(bio_err, " -in arg input file\n"); 211ddd58736SKris Kennaway BIO_printf(bio_err, " -sgckey Use IIS SGC key format\n"); 2126f9291ceSJung-uk Kim BIO_printf(bio_err, 2136f9291ceSJung-uk Kim " -passin arg input file pass phrase source\n"); 21474664626SKris Kennaway BIO_printf(bio_err, " -out arg output file\n"); 2156f9291ceSJung-uk Kim BIO_printf(bio_err, 2166f9291ceSJung-uk Kim " -passout arg output file pass phrase source\n"); 2176f9291ceSJung-uk Kim BIO_printf(bio_err, 2186f9291ceSJung-uk Kim " -des encrypt PEM output with cbc des\n"); 2196f9291ceSJung-uk Kim BIO_printf(bio_err, 2206f9291ceSJung-uk Kim " -des3 encrypt PEM output with ede cbc des using 168 bit key\n"); 2215c87c606SMark Murray # ifndef OPENSSL_NO_IDEA 2226f9291ceSJung-uk Kim BIO_printf(bio_err, 2236f9291ceSJung-uk Kim " -idea encrypt PEM output with cbc idea\n"); 22474664626SKris Kennaway # endif 225db522d3aSSimon L. B. Nielsen # ifndef OPENSSL_NO_SEED 2266f9291ceSJung-uk Kim BIO_printf(bio_err, 2276f9291ceSJung-uk Kim " -seed encrypt PEM output with cbc seed\n"); 228db522d3aSSimon L. B. Nielsen # endif 2295c87c606SMark Murray # ifndef OPENSSL_NO_AES 2305c87c606SMark Murray BIO_printf(bio_err, " -aes128, -aes192, -aes256\n"); 2316f9291ceSJung-uk Kim BIO_printf(bio_err, 2326f9291ceSJung-uk Kim " encrypt PEM output with cbc aes\n"); 2335c87c606SMark Murray # endif 234ed5d4f9aSSimon L. B. Nielsen # ifndef OPENSSL_NO_CAMELLIA 235ed5d4f9aSSimon L. B. Nielsen BIO_printf(bio_err, " -camellia128, -camellia192, -camellia256\n"); 2366f9291ceSJung-uk Kim BIO_printf(bio_err, 2376f9291ceSJung-uk Kim " encrypt PEM output with cbc camellia\n"); 238ed5d4f9aSSimon L. B. Nielsen # endif 23974664626SKris Kennaway BIO_printf(bio_err, " -text print the key in text\n"); 24074664626SKris Kennaway BIO_printf(bio_err, " -noout don't print key out\n"); 24174664626SKris Kennaway BIO_printf(bio_err, " -modulus print the RSA key modulus\n"); 24274664626SKris Kennaway BIO_printf(bio_err, " -check verify key consistency\n"); 2436f9291ceSJung-uk Kim BIO_printf(bio_err, 2446f9291ceSJung-uk Kim " -pubin expect a public key in input file\n"); 245f579bf8eSKris Kennaway BIO_printf(bio_err, " -pubout output a public key\n"); 246fceca8a3SJacques Vidrine # ifndef OPENSSL_NO_ENGINE 2476f9291ceSJung-uk Kim BIO_printf(bio_err, 2486f9291ceSJung-uk Kim " -engine e use engine e, possibly a hardware device.\n"); 249fceca8a3SJacques Vidrine # endif 25074664626SKris Kennaway goto end; 25174664626SKris Kennaway } 25274664626SKris Kennaway 25374664626SKris Kennaway ERR_load_crypto_strings(); 25474664626SKris Kennaway 2555c87c606SMark Murray e = setup_engine(bio_err, engine, 0); 2565c87c606SMark Murray 257f579bf8eSKris Kennaway if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { 258f579bf8eSKris Kennaway BIO_printf(bio_err, "Error getting passwords\n"); 259f579bf8eSKris Kennaway goto end; 260f579bf8eSKris Kennaway } 261f579bf8eSKris Kennaway 262f579bf8eSKris Kennaway if (check && pubin) { 263f579bf8eSKris Kennaway BIO_printf(bio_err, "Only private keys can be checked\n"); 264f579bf8eSKris Kennaway goto end; 265f579bf8eSKris Kennaway } 266f579bf8eSKris Kennaway 26774664626SKris Kennaway out = BIO_new(BIO_s_file()); 26874664626SKris Kennaway 2695c87c606SMark Murray { 2705c87c606SMark Murray EVP_PKEY *pkey; 2715c87c606SMark Murray 2726f9291ceSJung-uk Kim if (pubin) { 2731f13597dSJung-uk Kim int tmpformat = -1; 2746f9291ceSJung-uk Kim if (pubin == 2) { 2751f13597dSJung-uk Kim if (informat == FORMAT_PEM) 2761f13597dSJung-uk Kim tmpformat = FORMAT_PEMRSA; 2771f13597dSJung-uk Kim else if (informat == FORMAT_ASN1) 2781f13597dSJung-uk Kim tmpformat = FORMAT_ASN1RSA; 2796f9291ceSJung-uk Kim } else if (informat == FORMAT_NETSCAPE && sgckey) 2801f13597dSJung-uk Kim tmpformat = FORMAT_IISSGC; 2811f13597dSJung-uk Kim else 2821f13597dSJung-uk Kim tmpformat = informat; 2831f13597dSJung-uk Kim 2841f13597dSJung-uk Kim pkey = load_pubkey(bio_err, infile, tmpformat, 1, 2855c87c606SMark Murray passin, e, "Public Key"); 2866f9291ceSJung-uk Kim } else 2875c87c606SMark Murray pkey = load_key(bio_err, infile, 2885c87c606SMark Murray (informat == FORMAT_NETSCAPE && sgckey ? 2895c87c606SMark Murray FORMAT_IISSGC : informat), 1, 2905c87c606SMark Murray passin, e, "Private Key"); 2915c87c606SMark Murray 2925c87c606SMark Murray if (pkey != NULL) 2931f13597dSJung-uk Kim rsa = EVP_PKEY_get1_RSA(pkey); 2945c87c606SMark Murray EVP_PKEY_free(pkey); 29574664626SKris Kennaway } 29674664626SKris Kennaway 2976f9291ceSJung-uk Kim if (rsa == NULL) { 29874664626SKris Kennaway ERR_print_errors(bio_err); 29974664626SKris Kennaway goto end; 30074664626SKris Kennaway } 30174664626SKris Kennaway 3026f9291ceSJung-uk Kim if (outfile == NULL) { 30374664626SKris Kennaway BIO_set_fp(out, stdout, BIO_NOCLOSE); 3045c87c606SMark Murray # ifdef OPENSSL_SYS_VMS 305ddd58736SKris Kennaway { 306ddd58736SKris Kennaway BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 307ddd58736SKris Kennaway out = BIO_push(tmpbio, out); 308ddd58736SKris Kennaway } 309ddd58736SKris Kennaway # endif 3106f9291ceSJung-uk Kim } else { 3116f9291ceSJung-uk Kim if (BIO_write_filename(out, outfile) <= 0) { 31274664626SKris Kennaway perror(outfile); 31374664626SKris Kennaway goto end; 31474664626SKris Kennaway } 31574664626SKris Kennaway } 31674664626SKris Kennaway 31774664626SKris Kennaway if (text) 3186f9291ceSJung-uk Kim if (!RSA_print(out, rsa, 0)) { 31974664626SKris Kennaway perror(outfile); 32074664626SKris Kennaway ERR_print_errors(bio_err); 32174664626SKris Kennaway goto end; 32274664626SKris Kennaway } 32374664626SKris Kennaway 3246f9291ceSJung-uk Kim if (modulus) { 325f579bf8eSKris Kennaway BIO_printf(out, "Modulus="); 32674664626SKris Kennaway BN_print(out, rsa->n); 327f579bf8eSKris Kennaway BIO_printf(out, "\n"); 32874664626SKris Kennaway } 32974664626SKris Kennaway 3306f9291ceSJung-uk Kim if (check) { 33174664626SKris Kennaway int r = RSA_check_key(rsa); 33274664626SKris Kennaway 33374664626SKris Kennaway if (r == 1) 33474664626SKris Kennaway BIO_printf(out, "RSA key ok\n"); 3356f9291ceSJung-uk Kim else if (r == 0) { 3363b4e3dcbSSimon L. B. Nielsen unsigned long err; 33774664626SKris Kennaway 3385c87c606SMark Murray while ((err = ERR_peek_error()) != 0 && 3395c87c606SMark Murray ERR_GET_LIB(err) == ERR_LIB_RSA && 3405c87c606SMark Murray ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY && 3416f9291ceSJung-uk Kim ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) { 3426f9291ceSJung-uk Kim BIO_printf(out, "RSA key error: %s\n", 3436f9291ceSJung-uk Kim ERR_reason_error_string(err)); 34474664626SKris Kennaway ERR_get_error(); /* remove e from error stack */ 34574664626SKris Kennaway } 34674664626SKris Kennaway } 34774664626SKris Kennaway 3486f9291ceSJung-uk Kim if (r == -1 || ERR_peek_error() != 0) { /* should happen only if r == 3496f9291ceSJung-uk Kim * -1 */ 35074664626SKris Kennaway ERR_print_errors(bio_err); 35174664626SKris Kennaway goto end; 35274664626SKris Kennaway } 35374664626SKris Kennaway } 35474664626SKris Kennaway 3556f9291ceSJung-uk Kim if (noout) { 356f579bf8eSKris Kennaway ret = 0; 357f579bf8eSKris Kennaway goto end; 358f579bf8eSKris Kennaway } 359f579bf8eSKris Kennaway BIO_printf(bio_err, "writing RSA key\n"); 360f579bf8eSKris Kennaway if (outformat == FORMAT_ASN1) { 3616f9291ceSJung-uk Kim if (pubout || pubin) { 3621f13597dSJung-uk Kim if (pubout == 2) 3631f13597dSJung-uk Kim i = i2d_RSAPublicKey_bio(out, rsa); 3641f13597dSJung-uk Kim else 3651f13597dSJung-uk Kim i = i2d_RSA_PUBKEY_bio(out, rsa); 3666f9291ceSJung-uk Kim } else 3676f9291ceSJung-uk Kim i = i2d_RSAPrivateKey_bio(out, rsa); 368f579bf8eSKris Kennaway } 3695c87c606SMark Murray # ifndef OPENSSL_NO_RC4 3706f9291ceSJung-uk Kim else if (outformat == FORMAT_NETSCAPE) { 37174664626SKris Kennaway unsigned char *p, *pp; 37274664626SKris Kennaway int size; 37374664626SKris Kennaway 37474664626SKris Kennaway i = 1; 375ddd58736SKris Kennaway size = i2d_RSA_NET(rsa, NULL, NULL, sgckey); 3766f9291ceSJung-uk Kim if ((p = (unsigned char *)OPENSSL_malloc(size)) == NULL) { 377ddd58736SKris Kennaway BIO_printf(bio_err, "Memory allocation failure\n"); 37874664626SKris Kennaway goto end; 37974664626SKris Kennaway } 38074664626SKris Kennaway pp = p; 381ddd58736SKris Kennaway i2d_RSA_NET(rsa, &p, NULL, sgckey); 38274664626SKris Kennaway BIO_write(out, (char *)pp, size); 383ddd58736SKris Kennaway OPENSSL_free(pp); 38474664626SKris Kennaway } 38574664626SKris Kennaway # endif 386f579bf8eSKris Kennaway else if (outformat == FORMAT_PEM) { 3876f9291ceSJung-uk Kim if (pubout || pubin) { 3881f13597dSJung-uk Kim if (pubout == 2) 3891f13597dSJung-uk Kim i = PEM_write_bio_RSAPublicKey(out, rsa); 3901f13597dSJung-uk Kim else 391f579bf8eSKris Kennaway i = PEM_write_bio_RSA_PUBKEY(out, rsa); 3926f9291ceSJung-uk Kim } else 3936f9291ceSJung-uk Kim i = PEM_write_bio_RSAPrivateKey(out, rsa, 394f579bf8eSKris Kennaway enc, NULL, 0, NULL, passout); 3951f13597dSJung-uk Kim # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4) 3961f13597dSJung-uk Kim } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) { 3971f13597dSJung-uk Kim EVP_PKEY *pk; 3981f13597dSJung-uk Kim pk = EVP_PKEY_new(); 3991f13597dSJung-uk Kim EVP_PKEY_set1_RSA(pk, rsa); 4001f13597dSJung-uk Kim if (outformat == FORMAT_PVK) 4011f13597dSJung-uk Kim i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout); 4021f13597dSJung-uk Kim else if (pubin || pubout) 4031f13597dSJung-uk Kim i = i2b_PublicKey_bio(out, pk); 4041f13597dSJung-uk Kim else 4051f13597dSJung-uk Kim i = i2b_PrivateKey_bio(out, pk); 4061f13597dSJung-uk Kim EVP_PKEY_free(pk); 4071f13597dSJung-uk Kim # endif 408f579bf8eSKris Kennaway } else { 40974664626SKris Kennaway BIO_printf(bio_err, "bad output format specified for outfile\n"); 41074664626SKris Kennaway goto end; 41174664626SKris Kennaway } 4126f9291ceSJung-uk Kim if (i <= 0) { 413f579bf8eSKris Kennaway BIO_printf(bio_err, "unable to write key\n"); 41474664626SKris Kennaway ERR_print_errors(bio_err); 4156f9291ceSJung-uk Kim } else 41674664626SKris Kennaway ret = 0; 41774664626SKris Kennaway end: 418*6cf8931aSJung-uk Kim release_engine(e); 4196f9291ceSJung-uk Kim if (out != NULL) 4206f9291ceSJung-uk Kim BIO_free_all(out); 4216f9291ceSJung-uk Kim if (rsa != NULL) 4226f9291ceSJung-uk Kim RSA_free(rsa); 4236f9291ceSJung-uk Kim if (passin) 4246f9291ceSJung-uk Kim OPENSSL_free(passin); 4256f9291ceSJung-uk Kim if (passout) 4266f9291ceSJung-uk Kim OPENSSL_free(passout); 4275c87c606SMark Murray apps_shutdown(); 4285c87c606SMark Murray OPENSSL_EXIT(ret); 42974664626SKris Kennaway } 4305c87c606SMark Murray #else /* !OPENSSL_NO_RSA */ 431f579bf8eSKris Kennaway 432f579bf8eSKris Kennaway # if PEDANTIC 433f579bf8eSKris Kennaway static void *dummy = &dummy; 434f579bf8eSKris Kennaway # endif 435f579bf8eSKris Kennaway 43674664626SKris Kennaway #endif 437