1238384Sjkim/* apps/pkey.c */
2280304Sjkim/*
3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4280304Sjkim * 2006
5238384Sjkim */
6238384Sjkim/* ====================================================================
7238384Sjkim * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8238384Sjkim *
9238384Sjkim * Redistribution and use in source and binary forms, with or without
10238384Sjkim * modification, are permitted provided that the following conditions
11238384Sjkim * are met:
12238384Sjkim *
13238384Sjkim * 1. Redistributions of source code must retain the above copyright
14280304Sjkim *    notice, this list of conditions and the following disclaimer.
15238384Sjkim *
16238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
17238384Sjkim *    notice, this list of conditions and the following disclaimer in
18238384Sjkim *    the documentation and/or other materials provided with the
19238384Sjkim *    distribution.
20238384Sjkim *
21238384Sjkim * 3. All advertising materials mentioning features or use of this
22238384Sjkim *    software must display the following acknowledgment:
23238384Sjkim *    "This product includes software developed by the OpenSSL Project
24238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25238384Sjkim *
26238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27238384Sjkim *    endorse or promote products derived from this software without
28238384Sjkim *    prior written permission. For written permission, please contact
29238384Sjkim *    licensing@OpenSSL.org.
30238384Sjkim *
31238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
32238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
33238384Sjkim *    permission of the OpenSSL Project.
34238384Sjkim *
35238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
36238384Sjkim *    acknowledgment:
37238384Sjkim *    "This product includes software developed by the OpenSSL Project
38238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39238384Sjkim *
40238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
52238384Sjkim * ====================================================================
53238384Sjkim *
54238384Sjkim * This product includes cryptographic software written by Eric Young
55238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
56238384Sjkim * Hudson (tjh@cryptsoft.com).
57238384Sjkim *
58238384Sjkim */
59238384Sjkim#include <stdio.h>
60238384Sjkim#include <string.h>
61238384Sjkim#include "apps.h"
62238384Sjkim#include <openssl/pem.h>
63238384Sjkim#include <openssl/err.h>
64238384Sjkim#include <openssl/evp.h>
65238384Sjkim
66238384Sjkim#define PROG pkey_main
67238384Sjkim
68238384Sjkimint MAIN(int, char **);
69238384Sjkim
70238384Sjkimint MAIN(int argc, char **argv)
71280304Sjkim{
72280304Sjkim    ENGINE *e = NULL;
73280304Sjkim    char **args, *infile = NULL, *outfile = NULL;
74280304Sjkim    char *passargin = NULL, *passargout = NULL;
75280304Sjkim    BIO *in = NULL, *out = NULL;
76280304Sjkim    const EVP_CIPHER *cipher = NULL;
77280304Sjkim    int informat, outformat;
78280304Sjkim    int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0;
79280304Sjkim    EVP_PKEY *pkey = NULL;
80280304Sjkim    char *passin = NULL, *passout = NULL;
81280304Sjkim    int badarg = 0;
82238384Sjkim#ifndef OPENSSL_NO_ENGINE
83280304Sjkim    char *engine = NULL;
84238384Sjkim#endif
85280304Sjkim    int ret = 1;
86238384Sjkim
87280304Sjkim    if (bio_err == NULL)
88280304Sjkim        bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
89238384Sjkim
90280304Sjkim    if (!load_config(bio_err, NULL))
91280304Sjkim        goto end;
92238384Sjkim
93280304Sjkim    informat = FORMAT_PEM;
94280304Sjkim    outformat = FORMAT_PEM;
95238384Sjkim
96280304Sjkim    ERR_load_crypto_strings();
97280304Sjkim    OpenSSL_add_all_algorithms();
98280304Sjkim    args = argv + 1;
99280304Sjkim    while (!badarg && *args && *args[0] == '-') {
100280304Sjkim        if (!strcmp(*args, "-inform")) {
101280304Sjkim            if (args[1]) {
102280304Sjkim                args++;
103280304Sjkim                informat = str2fmt(*args);
104280304Sjkim            } else
105280304Sjkim                badarg = 1;
106280304Sjkim        } else if (!strcmp(*args, "-outform")) {
107280304Sjkim            if (args[1]) {
108280304Sjkim                args++;
109280304Sjkim                outformat = str2fmt(*args);
110280304Sjkim            } else
111280304Sjkim                badarg = 1;
112280304Sjkim        } else if (!strcmp(*args, "-passin")) {
113280304Sjkim            if (!args[1])
114280304Sjkim                goto bad;
115280304Sjkim            passargin = *(++args);
116280304Sjkim        } else if (!strcmp(*args, "-passout")) {
117280304Sjkim            if (!args[1])
118280304Sjkim                goto bad;
119280304Sjkim            passargout = *(++args);
120280304Sjkim        }
121238384Sjkim#ifndef OPENSSL_NO_ENGINE
122280304Sjkim        else if (strcmp(*args, "-engine") == 0) {
123280304Sjkim            if (!args[1])
124280304Sjkim                goto bad;
125280304Sjkim            engine = *(++args);
126280304Sjkim        }
127238384Sjkim#endif
128280304Sjkim        else if (!strcmp(*args, "-in")) {
129280304Sjkim            if (args[1]) {
130280304Sjkim                args++;
131280304Sjkim                infile = *args;
132280304Sjkim            } else
133280304Sjkim                badarg = 1;
134280304Sjkim        } else if (!strcmp(*args, "-out")) {
135280304Sjkim            if (args[1]) {
136280304Sjkim                args++;
137280304Sjkim                outfile = *args;
138280304Sjkim            } else
139280304Sjkim                badarg = 1;
140280304Sjkim        } else if (strcmp(*args, "-pubin") == 0) {
141280304Sjkim            pubin = 1;
142280304Sjkim            pubout = 1;
143280304Sjkim            pubtext = 1;
144280304Sjkim        } else if (strcmp(*args, "-pubout") == 0)
145280304Sjkim            pubout = 1;
146280304Sjkim        else if (strcmp(*args, "-text_pub") == 0) {
147280304Sjkim            pubtext = 1;
148280304Sjkim            text = 1;
149280304Sjkim        } else if (strcmp(*args, "-text") == 0)
150280304Sjkim            text = 1;
151280304Sjkim        else if (strcmp(*args, "-noout") == 0)
152280304Sjkim            noout = 1;
153280304Sjkim        else {
154280304Sjkim            cipher = EVP_get_cipherbyname(*args + 1);
155280304Sjkim            if (!cipher) {
156280304Sjkim                BIO_printf(bio_err, "Unknown cipher %s\n", *args + 1);
157280304Sjkim                badarg = 1;
158280304Sjkim            }
159280304Sjkim        }
160280304Sjkim        args++;
161280304Sjkim    }
162238384Sjkim
163280304Sjkim    if (badarg) {
164280304Sjkim bad:
165280304Sjkim        BIO_printf(bio_err, "Usage pkey [options]\n");
166280304Sjkim        BIO_printf(bio_err, "where options are\n");
167280304Sjkim        BIO_printf(bio_err, "-in file        input file\n");
168280304Sjkim        BIO_printf(bio_err, "-inform X       input format (DER or PEM)\n");
169280304Sjkim        BIO_printf(bio_err,
170280304Sjkim                   "-passin arg     input file pass phrase source\n");
171280304Sjkim        BIO_printf(bio_err, "-outform X      output format (DER or PEM)\n");
172280304Sjkim        BIO_printf(bio_err, "-out file       output file\n");
173280304Sjkim        BIO_printf(bio_err,
174280304Sjkim                   "-passout arg    output file pass phrase source\n");
175238384Sjkim#ifndef OPENSSL_NO_ENGINE
176280304Sjkim        BIO_printf(bio_err,
177280304Sjkim                   "-engine e       use engine e, possibly a hardware device.\n");
178238384Sjkim#endif
179280304Sjkim        return 1;
180280304Sjkim    }
181238384Sjkim#ifndef OPENSSL_NO_ENGINE
182280304Sjkim    e = setup_engine(bio_err, engine, 0);
183238384Sjkim#endif
184238384Sjkim
185280304Sjkim    if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
186280304Sjkim        BIO_printf(bio_err, "Error getting passwords\n");
187280304Sjkim        goto end;
188280304Sjkim    }
189238384Sjkim
190280304Sjkim    if (outfile) {
191280304Sjkim        if (!(out = BIO_new_file(outfile, "wb"))) {
192280304Sjkim            BIO_printf(bio_err, "Can't open output file %s\n", outfile);
193280304Sjkim            goto end;
194280304Sjkim        }
195280304Sjkim    } else {
196280304Sjkim        out = BIO_new_fp(stdout, BIO_NOCLOSE);
197238384Sjkim#ifdef OPENSSL_SYS_VMS
198280304Sjkim        {
199280304Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
200280304Sjkim            out = BIO_push(tmpbio, out);
201280304Sjkim        }
202238384Sjkim#endif
203280304Sjkim    }
204238384Sjkim
205280304Sjkim    if (pubin)
206280304Sjkim        pkey = load_pubkey(bio_err, infile, informat, 1,
207280304Sjkim                           passin, e, "Public Key");
208280304Sjkim    else
209280304Sjkim        pkey = load_key(bio_err, infile, informat, 1, passin, e, "key");
210280304Sjkim    if (!pkey)
211280304Sjkim        goto end;
212238384Sjkim
213280304Sjkim    if (!noout) {
214280304Sjkim        if (outformat == FORMAT_PEM) {
215280304Sjkim            if (pubout)
216280304Sjkim                PEM_write_bio_PUBKEY(out, pkey);
217280304Sjkim            else
218280304Sjkim                PEM_write_bio_PrivateKey(out, pkey, cipher,
219280304Sjkim                                         NULL, 0, NULL, passout);
220280304Sjkim        } else if (outformat == FORMAT_ASN1) {
221280304Sjkim            if (pubout)
222280304Sjkim                i2d_PUBKEY_bio(out, pkey);
223280304Sjkim            else
224280304Sjkim                i2d_PrivateKey_bio(out, pkey);
225280304Sjkim        } else {
226280304Sjkim            BIO_printf(bio_err, "Bad format specified for key\n");
227280304Sjkim            goto end;
228280304Sjkim        }
229238384Sjkim
230280304Sjkim    }
231238384Sjkim
232280304Sjkim    if (text) {
233280304Sjkim        if (pubtext)
234280304Sjkim            EVP_PKEY_print_public(out, pkey, 0, NULL);
235280304Sjkim        else
236280304Sjkim            EVP_PKEY_print_private(out, pkey, 0, NULL);
237280304Sjkim    }
238238384Sjkim
239280304Sjkim    ret = 0;
240238384Sjkim
241280304Sjkim end:
242280304Sjkim    EVP_PKEY_free(pkey);
243280304Sjkim    BIO_free_all(out);
244280304Sjkim    BIO_free(in);
245280304Sjkim    if (passin)
246280304Sjkim        OPENSSL_free(passin);
247280304Sjkim    if (passout)
248280304Sjkim        OPENSSL_free(passout);
249238384Sjkim
250280304Sjkim    return ret;
251280304Sjkim}
252