]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/pkey.c
make errors
[thirdparty/openssl.git] / apps / pkey.c
CommitLineData
0f113f3e 1/*
846e33c7 2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3e84b6e1 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
3e84b6e1 8 */
846e33c7 9
3e84b6e1
DSH
10#include <stdio.h>
11#include <string.h>
12#include "apps.h"
13#include <openssl/pem.h>
14#include <openssl/err.h>
15#include <openssl/evp.h>
16
7e1b7485
RS
17typedef enum OPTION_choice {
18 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
19 OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
20 OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
05dba815 21 OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL
7e1b7485
RS
22} OPTION_CHOICE;
23
44c83ebd 24const OPTIONS pkey_options[] = {
7e1b7485 25 {"help", OPT_HELP, '-', "Display this summary"},
d18ba3cc 26 {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
7e1b7485
RS
27 {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
28 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
29 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
dd958974 30 {"in", OPT_IN, 's', "Input key"},
7e1b7485
RS
31 {"out", OPT_OUT, '>', "Output file"},
32 {"pubin", OPT_PUBIN, '-',
33 "Read public key from input (default is private key)"},
34 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
35 {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
36 {"text", OPT_TEXT, '-', "Output in plaintext as well"},
37 {"noout", OPT_NOOUT, '-', "Don't output the key"},
38 {"", OPT_MD, '-', "Any supported cipher"},
05dba815
DSH
39 {"traditional", OPT_TRADITIONAL, '-',
40 "Use traditional format for private keys"},
7e1b7485
RS
41#ifndef OPENSSL_NO_ENGINE
42 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
43#endif
44 {NULL}
45};
3e84b6e1 46
7e1b7485 47int pkey_main(int argc, char **argv)
0f113f3e 48{
0f113f3e 49 BIO *in = NULL, *out = NULL;
7e1b7485 50 ENGINE *e = NULL;
0f113f3e 51 EVP_PKEY *pkey = NULL;
7e1b7485
RS
52 const EVP_CIPHER *cipher = NULL;
53 char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
333b070e 54 char *passinarg = NULL, *passoutarg = NULL, *prog;
7e1b7485
RS
55 OPTION_CHOICE o;
56 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
57 int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
05dba815 58 int private = 0, traditional = 0;
7e1b7485
RS
59
60 prog = opt_init(argc, argv, pkey_options);
61 while ((o = opt_next()) != OPT_EOF) {
62 switch (o) {
63 case OPT_EOF:
64 case OPT_ERR:
65 opthelp:
66 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
67 goto end;
68 case OPT_HELP:
69 opt_help(pkey_options);
70 ret = 0;
71 goto end;
72 case OPT_INFORM:
dd958974 73 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
7e1b7485
RS
74 goto opthelp;
75 break;
76 case OPT_OUTFORM:
77 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
78 goto opthelp;
79 break;
80 case OPT_PASSIN:
81 passinarg = opt_arg();
82 break;
83 case OPT_PASSOUT:
84 passoutarg = opt_arg();
85 break;
86 case OPT_ENGINE:
333b070e 87 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
88 break;
89 case OPT_IN:
90 infile = opt_arg();
91 break;
92 case OPT_OUT:
93 outfile = opt_arg();
94 break;
95 case OPT_PUBIN:
96 pubin = pubout = pubtext = 1;
97 break;
98 case OPT_PUBOUT:
0f113f3e 99 pubout = 1;
7e1b7485
RS
100 break;
101 case OPT_TEXT_PUB:
102 pubtext = text = 1;
103 break;
104 case OPT_TEXT:
0f113f3e 105 text = 1;
7e1b7485
RS
106 break;
107 case OPT_NOOUT:
0f113f3e 108 noout = 1;
7e1b7485 109 break;
05dba815
DSH
110 case OPT_TRADITIONAL:
111 traditional = 1;
112 break;
7e1b7485
RS
113 case OPT_MD:
114 if (!opt_cipher(opt_unknown(), &cipher))
115 goto opthelp;
0f113f3e 116 }
0f113f3e 117 }
7e1b7485 118 argc = opt_num_rest();
03358517
KR
119 if (argc != 0)
120 goto opthelp;
121
3b061a00
RS
122 private = !noout && !pubout ? 1 : 0;
123 if (text && !pubtext)
124 private = 1;
0f113f3e 125
7e1b7485 126 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
127 BIO_printf(bio_err, "Error getting passwords\n");
128 goto end;
129 }
130
bdd58d98 131 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
132 if (out == NULL)
133 goto end;
0f113f3e
MC
134
135 if (pubin)
7e1b7485 136 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
0f113f3e 137 else
7e1b7485 138 pkey = load_key(infile, informat, 1, passin, e, "key");
0f113f3e
MC
139 if (!pkey)
140 goto end;
141
142 if (!noout) {
143 if (outformat == FORMAT_PEM) {
144 if (pubout)
145 PEM_write_bio_PUBKEY(out, pkey);
7eff6aa0
VD
146 else {
147 assert(private);
05dba815
DSH
148 if (traditional)
149 PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
150 NULL, 0, NULL,
151 passout);
152 else
153 PEM_write_bio_PrivateKey(out, pkey, cipher,
154 NULL, 0, NULL, passout);
7eff6aa0 155 }
0f113f3e
MC
156 } else if (outformat == FORMAT_ASN1) {
157 if (pubout)
158 i2d_PUBKEY_bio(out, pkey);
7eff6aa0
VD
159 else {
160 assert(private);
0f113f3e 161 i2d_PrivateKey_bio(out, pkey);
7eff6aa0 162 }
0f113f3e
MC
163 } else {
164 BIO_printf(bio_err, "Bad format specified for key\n");
165 goto end;
166 }
167
168 }
169
170 if (text) {
171 if (pubtext)
172 EVP_PKEY_print_public(out, pkey, 0, NULL);
3b061a00
RS
173 else {
174 assert(private);
0f113f3e 175 EVP_PKEY_print_private(out, pkey, 0, NULL);
3b061a00 176 }
0f113f3e
MC
177 }
178
179 ret = 0;
180
181 end:
182 EVP_PKEY_free(pkey);
dd1abd44 183 release_engine(e);
0f113f3e
MC
184 BIO_free_all(out);
185 BIO_free(in);
b548a1f1
RS
186 OPENSSL_free(passin);
187 OPENSSL_free(passout);
0f113f3e
MC
188
189 return ret;
190}