1238384Sjkim/* apps/pkeyparam.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 pkeyparam_main
67238384Sjkim
68238384Sjkimint MAIN(int, char **);
69238384Sjkim
70238384Sjkimint MAIN(int argc, char **argv)
71280304Sjkim{
72280304Sjkim    char **args, *infile = NULL, *outfile = NULL;
73280304Sjkim    BIO *in = NULL, *out = NULL;
74280304Sjkim    int text = 0, noout = 0;
75280304Sjkim    EVP_PKEY *pkey = NULL;
76280304Sjkim    int badarg = 0;
77238384Sjkim#ifndef OPENSSL_NO_ENGINE
78280304Sjkim    char *engine = NULL;
79238384Sjkim#endif
80280304Sjkim    int ret = 1;
81238384Sjkim
82280304Sjkim    if (bio_err == NULL)
83280304Sjkim        bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
84238384Sjkim
85280304Sjkim    if (!load_config(bio_err, NULL))
86280304Sjkim        goto end;
87238384Sjkim
88280304Sjkim    ERR_load_crypto_strings();
89280304Sjkim    OpenSSL_add_all_algorithms();
90280304Sjkim    args = argv + 1;
91280304Sjkim    while (!badarg && *args && *args[0] == '-') {
92280304Sjkim        if (!strcmp(*args, "-in")) {
93280304Sjkim            if (args[1]) {
94280304Sjkim                args++;
95280304Sjkim                infile = *args;
96280304Sjkim            } else
97280304Sjkim                badarg = 1;
98280304Sjkim        } else if (!strcmp(*args, "-out")) {
99280304Sjkim            if (args[1]) {
100280304Sjkim                args++;
101280304Sjkim                outfile = *args;
102280304Sjkim            } else
103280304Sjkim                badarg = 1;
104280304Sjkim        }
105238384Sjkim#ifndef OPENSSL_NO_ENGINE
106280304Sjkim        else if (strcmp(*args, "-engine") == 0) {
107280304Sjkim            if (!args[1])
108280304Sjkim                goto bad;
109280304Sjkim            engine = *(++args);
110280304Sjkim        }
111238384Sjkim#endif
112238384Sjkim
113280304Sjkim        else if (strcmp(*args, "-text") == 0)
114280304Sjkim            text = 1;
115280304Sjkim        else if (strcmp(*args, "-noout") == 0)
116280304Sjkim            noout = 1;
117280304Sjkim        args++;
118280304Sjkim    }
119238384Sjkim
120280304Sjkim    if (badarg) {
121238384Sjkim#ifndef OPENSSL_NO_ENGINE
122280304Sjkim bad:
123238384Sjkim#endif
124280304Sjkim        BIO_printf(bio_err, "Usage pkeyparam [options]\n");
125280304Sjkim        BIO_printf(bio_err, "where options are\n");
126280304Sjkim        BIO_printf(bio_err, "-in file        input file\n");
127280304Sjkim        BIO_printf(bio_err, "-out file       output file\n");
128280304Sjkim        BIO_printf(bio_err, "-text           print parameters as text\n");
129280304Sjkim        BIO_printf(bio_err,
130280304Sjkim                   "-noout          don't output encoded parameters\n");
131238384Sjkim#ifndef OPENSSL_NO_ENGINE
132280304Sjkim        BIO_printf(bio_err,
133280304Sjkim                   "-engine e       use engine e, possibly a hardware device.\n");
134238384Sjkim#endif
135280304Sjkim        return 1;
136280304Sjkim    }
137238384Sjkim#ifndef OPENSSL_NO_ENGINE
138280304Sjkim    setup_engine(bio_err, engine, 0);
139238384Sjkim#endif
140238384Sjkim
141280304Sjkim    if (infile) {
142280304Sjkim        if (!(in = BIO_new_file(infile, "r"))) {
143280304Sjkim            BIO_printf(bio_err, "Can't open input file %s\n", infile);
144280304Sjkim            goto end;
145280304Sjkim        }
146280304Sjkim    } else
147280304Sjkim        in = BIO_new_fp(stdin, BIO_NOCLOSE);
148238384Sjkim
149280304Sjkim    if (outfile) {
150280304Sjkim        if (!(out = BIO_new_file(outfile, "w"))) {
151280304Sjkim            BIO_printf(bio_err, "Can't open output file %s\n", outfile);
152280304Sjkim            goto end;
153280304Sjkim        }
154280304Sjkim    } else {
155280304Sjkim        out = BIO_new_fp(stdout, BIO_NOCLOSE);
156238384Sjkim#ifdef OPENSSL_SYS_VMS
157280304Sjkim        {
158280304Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
159280304Sjkim            out = BIO_push(tmpbio, out);
160280304Sjkim        }
161238384Sjkim#endif
162280304Sjkim    }
163238384Sjkim
164280304Sjkim    pkey = PEM_read_bio_Parameters(in, NULL);
165280304Sjkim    if (!pkey) {
166280304Sjkim        BIO_printf(bio_err, "Error reading parameters\n");
167280304Sjkim        ERR_print_errors(bio_err);
168280304Sjkim        goto end;
169280304Sjkim    }
170238384Sjkim
171280304Sjkim    if (!noout)
172280304Sjkim        PEM_write_bio_Parameters(out, pkey);
173238384Sjkim
174280304Sjkim    if (text)
175280304Sjkim        EVP_PKEY_print_params(out, pkey, 0, NULL);
176238384Sjkim
177280304Sjkim    ret = 0;
178238384Sjkim
179280304Sjkim end:
180280304Sjkim    EVP_PKEY_free(pkey);
181280304Sjkim    BIO_free_all(out);
182280304Sjkim    BIO_free(in);
183238384Sjkim
184280304Sjkim    return ret;
185280304Sjkim}
186