1ebfedea0SLionel Sambuc /* apps/pkeyparam.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 pkeyparam_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 char **args, *infile = NULL, *outfile = NULL;
73ebfedea0SLionel Sambuc BIO *in = NULL, *out = NULL;
74ebfedea0SLionel Sambuc int text = 0, noout = 0;
75ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
76ebfedea0SLionel Sambuc int badarg = 0;
77ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
78ebfedea0SLionel Sambuc char *engine = NULL;
79ebfedea0SLionel Sambuc #endif
80ebfedea0SLionel Sambuc int ret = 1;
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc if (bio_err == NULL)
83ebfedea0SLionel Sambuc bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
84ebfedea0SLionel Sambuc
85ebfedea0SLionel Sambuc if (!load_config(bio_err, NULL))
86ebfedea0SLionel Sambuc goto end;
87ebfedea0SLionel Sambuc
88ebfedea0SLionel Sambuc ERR_load_crypto_strings();
89ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms();
90ebfedea0SLionel Sambuc args = argv + 1;
91*0a6a1f1dSLionel Sambuc while (!badarg && *args && *args[0] == '-') {
92*0a6a1f1dSLionel Sambuc if (!strcmp(*args, "-in")) {
93*0a6a1f1dSLionel Sambuc if (args[1]) {
94ebfedea0SLionel Sambuc args++;
95ebfedea0SLionel Sambuc infile = *args;
96*0a6a1f1dSLionel Sambuc } else
97*0a6a1f1dSLionel Sambuc badarg = 1;
98*0a6a1f1dSLionel Sambuc } else if (!strcmp(*args, "-out")) {
99*0a6a1f1dSLionel Sambuc if (args[1]) {
100ebfedea0SLionel Sambuc args++;
101ebfedea0SLionel Sambuc outfile = *args;
102*0a6a1f1dSLionel Sambuc } else
103*0a6a1f1dSLionel Sambuc badarg = 1;
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
106*0a6a1f1dSLionel Sambuc else if (strcmp(*args, "-engine") == 0) {
107*0a6a1f1dSLionel Sambuc if (!args[1])
108*0a6a1f1dSLionel Sambuc goto bad;
109ebfedea0SLionel Sambuc engine = *(++args);
110ebfedea0SLionel Sambuc }
111ebfedea0SLionel Sambuc #endif
112ebfedea0SLionel Sambuc
113ebfedea0SLionel Sambuc else if (strcmp(*args, "-text") == 0)
114ebfedea0SLionel Sambuc text = 1;
115ebfedea0SLionel Sambuc else if (strcmp(*args, "-noout") == 0)
116ebfedea0SLionel Sambuc noout = 1;
117ebfedea0SLionel Sambuc args++;
118ebfedea0SLionel Sambuc }
119ebfedea0SLionel Sambuc
120*0a6a1f1dSLionel Sambuc if (badarg) {
121ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
122ebfedea0SLionel Sambuc bad:
123ebfedea0SLionel Sambuc #endif
124ebfedea0SLionel Sambuc BIO_printf(bio_err, "Usage pkeyparam [options]\n");
125ebfedea0SLionel Sambuc BIO_printf(bio_err, "where options are\n");
126ebfedea0SLionel Sambuc BIO_printf(bio_err, "-in file input file\n");
127ebfedea0SLionel Sambuc BIO_printf(bio_err, "-out file output file\n");
128ebfedea0SLionel Sambuc BIO_printf(bio_err, "-text print parameters as text\n");
129*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
130*0a6a1f1dSLionel Sambuc "-noout don't output encoded parameters\n");
131ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
132*0a6a1f1dSLionel Sambuc BIO_printf(bio_err,
133*0a6a1f1dSLionel Sambuc "-engine e use engine e, possibly a hardware device.\n");
134ebfedea0SLionel Sambuc #endif
135ebfedea0SLionel Sambuc return 1;
136ebfedea0SLionel Sambuc }
137ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
138ebfedea0SLionel Sambuc setup_engine(bio_err, engine, 0);
139ebfedea0SLionel Sambuc #endif
140ebfedea0SLionel Sambuc
141*0a6a1f1dSLionel Sambuc if (infile) {
142*0a6a1f1dSLionel Sambuc if (!(in = BIO_new_file(infile, "r"))) {
143*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Can't open input file %s\n", infile);
144ebfedea0SLionel Sambuc goto end;
145ebfedea0SLionel Sambuc }
146*0a6a1f1dSLionel Sambuc } else
147ebfedea0SLionel Sambuc in = BIO_new_fp(stdin, BIO_NOCLOSE);
148ebfedea0SLionel Sambuc
149*0a6a1f1dSLionel Sambuc if (outfile) {
150*0a6a1f1dSLionel Sambuc if (!(out = BIO_new_file(outfile, "w"))) {
151*0a6a1f1dSLionel Sambuc BIO_printf(bio_err, "Can't open output file %s\n", outfile);
152ebfedea0SLionel Sambuc goto end;
153ebfedea0SLionel Sambuc }
154*0a6a1f1dSLionel Sambuc } else {
155ebfedea0SLionel Sambuc out = BIO_new_fp(stdout, BIO_NOCLOSE);
156ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
157ebfedea0SLionel Sambuc {
158ebfedea0SLionel Sambuc BIO *tmpbio = BIO_new(BIO_f_linebuffer());
159ebfedea0SLionel Sambuc out = BIO_push(tmpbio, out);
160ebfedea0SLionel Sambuc }
161ebfedea0SLionel Sambuc #endif
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc pkey = PEM_read_bio_Parameters(in, NULL);
165*0a6a1f1dSLionel Sambuc if (!pkey) {
166ebfedea0SLionel Sambuc BIO_printf(bio_err, "Error reading parameters\n");
167ebfedea0SLionel Sambuc ERR_print_errors(bio_err);
168ebfedea0SLionel Sambuc goto end;
169ebfedea0SLionel Sambuc }
170ebfedea0SLionel Sambuc
171ebfedea0SLionel Sambuc if (!noout)
172ebfedea0SLionel Sambuc PEM_write_bio_Parameters(out, pkey);
173ebfedea0SLionel Sambuc
174ebfedea0SLionel Sambuc if (text)
175ebfedea0SLionel Sambuc EVP_PKEY_print_params(out, pkey, 0, NULL);
176ebfedea0SLionel Sambuc
177ebfedea0SLionel Sambuc ret = 0;
178ebfedea0SLionel Sambuc
179ebfedea0SLionel Sambuc end:
180ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
181ebfedea0SLionel Sambuc BIO_free_all(out);
182ebfedea0SLionel Sambuc BIO_free(in);
183ebfedea0SLionel Sambuc
184ebfedea0SLionel Sambuc return ret;
185ebfedea0SLionel Sambuc }
186