Reformat progs.pl; add ARIA support
[openssl.git] / apps / pkeyparam.c
1 /*
2  * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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
17 typedef enum OPTION_choice {
18     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
19     OPT_IN, OPT_OUT, OPT_TEXT, OPT_NOOUT, OPT_ENGINE
20 } OPTION_CHOICE;
21
22 const OPTIONS pkeyparam_options[] = {
23     {"help", OPT_HELP, '-', "Display this summary"},
24     {"in", OPT_IN, '<', "Input file"},
25     {"out", OPT_OUT, '>', "Output file"},
26     {"text", OPT_TEXT, '-', "Print parameters as text"},
27     {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"},
28 #ifndef OPENSSL_NO_ENGINE
29     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
30 #endif
31     {NULL}
32 };
33
34 int pkeyparam_main(int argc, char **argv)
35 {
36     ENGINE *e = NULL;
37     BIO *in = NULL, *out = NULL;
38     EVP_PKEY *pkey = NULL;
39     int text = 0, noout = 0, ret = 1;
40     OPTION_CHOICE o;
41     char *infile = NULL, *outfile = NULL, *prog;
42
43     prog = opt_init(argc, argv, pkeyparam_options);
44     while ((o = opt_next()) != OPT_EOF) {
45         switch (o) {
46         case OPT_EOF:
47         case OPT_ERR:
48  opthelp:
49             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
50             goto end;
51         case OPT_HELP:
52             opt_help(pkeyparam_options);
53             ret = 0;
54             goto end;
55         case OPT_IN:
56             infile = opt_arg();
57             break;
58         case OPT_OUT:
59             outfile = opt_arg();
60             break;
61         case OPT_ENGINE:
62             e = setup_engine(opt_arg(), 0);
63             break;
64         case OPT_TEXT:
65             text = 1;
66             break;
67         case OPT_NOOUT:
68             noout = 1;
69             break;
70         }
71     }
72     argc = opt_num_rest();
73     if (argc != 0)
74         goto opthelp;
75
76     in = bio_open_default(infile, 'r', FORMAT_PEM);
77     if (in == NULL)
78         goto end;
79     out = bio_open_default(outfile, 'w', FORMAT_PEM);
80     if (out == NULL)
81         goto end;
82     pkey = PEM_read_bio_Parameters(in, NULL);
83     if (pkey == NULL) {
84         BIO_printf(bio_err, "Error reading parameters\n");
85         ERR_print_errors(bio_err);
86         goto end;
87     }
88
89     if (!noout)
90         PEM_write_bio_Parameters(out, pkey);
91
92     if (text)
93         EVP_PKEY_print_params(out, pkey, 0, NULL);
94
95     ret = 0;
96
97  end:
98     EVP_PKEY_free(pkey);
99     release_engine(e);
100     BIO_free_all(out);
101     BIO_free(in);
102
103     return ret;
104 }