xref: /minix3/crypto/external/bsd/openssl/dist/apps/pkey.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/pkey.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc  * 2006
5ebfedea0SLionel Sambuc  */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc  *    distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
23ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc  *
26ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc  *
35ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc  *    acknowledgment:
37ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc  * ====================================================================
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
56ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc  *
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <string.h>
61ebfedea0SLionel Sambuc #include "apps.h"
62ebfedea0SLionel Sambuc #include <openssl/pem.h>
63ebfedea0SLionel Sambuc #include <openssl/err.h>
64ebfedea0SLionel Sambuc #include <openssl/evp.h>
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc #define PROG pkey_main
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc int MAIN(int, char **);
69ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)70ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
71ebfedea0SLionel Sambuc {
72ebfedea0SLionel Sambuc     ENGINE *e = NULL;
73ebfedea0SLionel Sambuc     char **args, *infile = NULL, *outfile = NULL;
74ebfedea0SLionel Sambuc     char *passargin = NULL, *passargout = NULL;
75ebfedea0SLionel Sambuc     BIO *in = NULL, *out = NULL;
76ebfedea0SLionel Sambuc     const EVP_CIPHER *cipher = NULL;
77ebfedea0SLionel Sambuc     int informat, outformat;
78ebfedea0SLionel Sambuc     int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0;
79ebfedea0SLionel Sambuc     EVP_PKEY *pkey = NULL;
80ebfedea0SLionel Sambuc     char *passin = NULL, *passout = NULL;
81ebfedea0SLionel Sambuc     int badarg = 0;
82ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
83ebfedea0SLionel Sambuc     char *engine = NULL;
84ebfedea0SLionel Sambuc #endif
85ebfedea0SLionel Sambuc     int ret = 1;
86ebfedea0SLionel Sambuc 
87ebfedea0SLionel Sambuc     if (bio_err == NULL)
88ebfedea0SLionel Sambuc         bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
89ebfedea0SLionel Sambuc 
90ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
91ebfedea0SLionel Sambuc         goto end;
92ebfedea0SLionel Sambuc 
93ebfedea0SLionel Sambuc     informat = FORMAT_PEM;
94ebfedea0SLionel Sambuc     outformat = FORMAT_PEM;
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc     ERR_load_crypto_strings();
97ebfedea0SLionel Sambuc     OpenSSL_add_all_algorithms();
98ebfedea0SLionel Sambuc     args = argv + 1;
99*0a6a1f1dSLionel Sambuc     while (!badarg && *args && *args[0] == '-') {
100*0a6a1f1dSLionel Sambuc         if (!strcmp(*args, "-inform")) {
101*0a6a1f1dSLionel Sambuc             if (args[1]) {
102ebfedea0SLionel Sambuc                 args++;
103ebfedea0SLionel Sambuc                 informat = str2fmt(*args);
104*0a6a1f1dSLionel Sambuc             } else
105*0a6a1f1dSLionel Sambuc                 badarg = 1;
106*0a6a1f1dSLionel Sambuc         } else if (!strcmp(*args, "-outform")) {
107*0a6a1f1dSLionel Sambuc             if (args[1]) {
108ebfedea0SLionel Sambuc                 args++;
109ebfedea0SLionel Sambuc                 outformat = str2fmt(*args);
110*0a6a1f1dSLionel Sambuc             } else
111*0a6a1f1dSLionel Sambuc                 badarg = 1;
112*0a6a1f1dSLionel Sambuc         } else if (!strcmp(*args, "-passin")) {
113*0a6a1f1dSLionel Sambuc             if (!args[1])
114*0a6a1f1dSLionel Sambuc                 goto bad;
115ebfedea0SLionel Sambuc             passargin = *(++args);
116*0a6a1f1dSLionel Sambuc         } else if (!strcmp(*args, "-passout")) {
117*0a6a1f1dSLionel Sambuc             if (!args[1])
118*0a6a1f1dSLionel Sambuc                 goto bad;
119ebfedea0SLionel Sambuc             passargout = *(++args);
120ebfedea0SLionel Sambuc         }
121ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
122*0a6a1f1dSLionel Sambuc         else if (strcmp(*args, "-engine") == 0) {
123*0a6a1f1dSLionel Sambuc             if (!args[1])
124*0a6a1f1dSLionel Sambuc                 goto bad;
125ebfedea0SLionel Sambuc             engine = *(++args);
126ebfedea0SLionel Sambuc         }
127ebfedea0SLionel Sambuc #endif
128*0a6a1f1dSLionel Sambuc         else if (!strcmp(*args, "-in")) {
129*0a6a1f1dSLionel Sambuc             if (args[1]) {
130ebfedea0SLionel Sambuc                 args++;
131ebfedea0SLionel Sambuc                 infile = *args;
132*0a6a1f1dSLionel Sambuc             } else
133*0a6a1f1dSLionel Sambuc                 badarg = 1;
134*0a6a1f1dSLionel Sambuc         } else if (!strcmp(*args, "-out")) {
135*0a6a1f1dSLionel Sambuc             if (args[1]) {
136ebfedea0SLionel Sambuc                 args++;
137ebfedea0SLionel Sambuc                 outfile = *args;
138*0a6a1f1dSLionel Sambuc             } else
139*0a6a1f1dSLionel Sambuc                 badarg = 1;
140*0a6a1f1dSLionel Sambuc         } else if (strcmp(*args, "-pubin") == 0) {
141ebfedea0SLionel Sambuc             pubin = 1;
142ebfedea0SLionel Sambuc             pubout = 1;
143ebfedea0SLionel Sambuc             pubtext = 1;
144*0a6a1f1dSLionel Sambuc         } else if (strcmp(*args, "-pubout") == 0)
145ebfedea0SLionel Sambuc             pubout = 1;
146*0a6a1f1dSLionel Sambuc         else if (strcmp(*args, "-text_pub") == 0) {
147ebfedea0SLionel Sambuc             pubtext = 1;
148ebfedea0SLionel Sambuc             text = 1;
149*0a6a1f1dSLionel Sambuc         } else if (strcmp(*args, "-text") == 0)
150ebfedea0SLionel Sambuc             text = 1;
151ebfedea0SLionel Sambuc         else if (strcmp(*args, "-noout") == 0)
152ebfedea0SLionel Sambuc             noout = 1;
153*0a6a1f1dSLionel Sambuc         else {
154ebfedea0SLionel Sambuc             cipher = EVP_get_cipherbyname(*args + 1);
155*0a6a1f1dSLionel Sambuc             if (!cipher) {
156*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_err, "Unknown cipher %s\n", *args + 1);
157ebfedea0SLionel Sambuc                 badarg = 1;
158ebfedea0SLionel Sambuc             }
159ebfedea0SLionel Sambuc         }
160ebfedea0SLionel Sambuc         args++;
161ebfedea0SLionel Sambuc     }
162ebfedea0SLionel Sambuc 
163*0a6a1f1dSLionel Sambuc     if (badarg) {
164ebfedea0SLionel Sambuc  bad:
165ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Usage pkey [options]\n");
166ebfedea0SLionel Sambuc         BIO_printf(bio_err, "where options are\n");
167ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-in file        input file\n");
168ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-inform X       input format (DER or PEM)\n");
169*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
170*0a6a1f1dSLionel Sambuc                    "-passin arg     input file pass phrase source\n");
171ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-outform X      output format (DER or PEM)\n");
172ebfedea0SLionel Sambuc         BIO_printf(bio_err, "-out file       output file\n");
173*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
174*0a6a1f1dSLionel Sambuc                    "-passout arg    output file pass phrase source\n");
175ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
176*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
177*0a6a1f1dSLionel Sambuc                    "-engine e       use engine e, possibly a hardware device.\n");
178ebfedea0SLionel Sambuc #endif
179ebfedea0SLionel Sambuc         return 1;
180ebfedea0SLionel Sambuc     }
181ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
182ebfedea0SLionel Sambuc     e = setup_engine(bio_err, engine, 0);
183ebfedea0SLionel Sambuc #endif
184ebfedea0SLionel Sambuc 
185*0a6a1f1dSLionel Sambuc     if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
186ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Error getting passwords\n");
187ebfedea0SLionel Sambuc         goto end;
188ebfedea0SLionel Sambuc     }
189ebfedea0SLionel Sambuc 
190*0a6a1f1dSLionel Sambuc     if (outfile) {
191*0a6a1f1dSLionel Sambuc         if (!(out = BIO_new_file(outfile, "wb"))) {
192*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "Can't open output file %s\n", outfile);
193ebfedea0SLionel Sambuc             goto end;
194ebfedea0SLionel Sambuc         }
195*0a6a1f1dSLionel Sambuc     } else {
196ebfedea0SLionel Sambuc         out = BIO_new_fp(stdout, BIO_NOCLOSE);
197ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
198ebfedea0SLionel Sambuc         {
199ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
200ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
201ebfedea0SLionel Sambuc         }
202ebfedea0SLionel Sambuc #endif
203ebfedea0SLionel Sambuc     }
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc     if (pubin)
206ebfedea0SLionel Sambuc         pkey = load_pubkey(bio_err, infile, informat, 1,
207ebfedea0SLionel Sambuc                            passin, e, "Public Key");
208ebfedea0SLionel Sambuc     else
209*0a6a1f1dSLionel Sambuc         pkey = load_key(bio_err, infile, informat, 1, passin, e, "key");
210ebfedea0SLionel Sambuc     if (!pkey)
211ebfedea0SLionel Sambuc         goto end;
212ebfedea0SLionel Sambuc 
213*0a6a1f1dSLionel Sambuc     if (!noout) {
214*0a6a1f1dSLionel Sambuc         if (outformat == FORMAT_PEM) {
215ebfedea0SLionel Sambuc             if (pubout)
216ebfedea0SLionel Sambuc                 PEM_write_bio_PUBKEY(out, pkey);
217ebfedea0SLionel Sambuc             else
218ebfedea0SLionel Sambuc                 PEM_write_bio_PrivateKey(out, pkey, cipher,
219ebfedea0SLionel Sambuc                                          NULL, 0, NULL, passout);
220*0a6a1f1dSLionel Sambuc         } else if (outformat == FORMAT_ASN1) {
221ebfedea0SLionel Sambuc             if (pubout)
222ebfedea0SLionel Sambuc                 i2d_PUBKEY_bio(out, pkey);
223ebfedea0SLionel Sambuc             else
224ebfedea0SLionel Sambuc                 i2d_PrivateKey_bio(out, pkey);
225*0a6a1f1dSLionel Sambuc         } else {
226ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Bad format specified for key\n");
227ebfedea0SLionel Sambuc             goto end;
228ebfedea0SLionel Sambuc         }
229ebfedea0SLionel Sambuc 
230ebfedea0SLionel Sambuc     }
231ebfedea0SLionel Sambuc 
232*0a6a1f1dSLionel Sambuc     if (text) {
233ebfedea0SLionel Sambuc         if (pubtext)
234ebfedea0SLionel Sambuc             EVP_PKEY_print_public(out, pkey, 0, NULL);
235ebfedea0SLionel Sambuc         else
236ebfedea0SLionel Sambuc             EVP_PKEY_print_private(out, pkey, 0, NULL);
237ebfedea0SLionel Sambuc     }
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc     ret = 0;
240ebfedea0SLionel Sambuc 
241ebfedea0SLionel Sambuc  end:
242ebfedea0SLionel Sambuc     EVP_PKEY_free(pkey);
243ebfedea0SLionel Sambuc     BIO_free_all(out);
244ebfedea0SLionel Sambuc     BIO_free(in);
245ebfedea0SLionel Sambuc     if (passin)
246ebfedea0SLionel Sambuc         OPENSSL_free(passin);
247ebfedea0SLionel Sambuc     if (passout)
248ebfedea0SLionel Sambuc         OPENSSL_free(passout);
249ebfedea0SLionel Sambuc 
250ebfedea0SLionel Sambuc     return ret;
251ebfedea0SLionel Sambuc }
252