apps/speed.c: initialize buffers
[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,
20     OPT_ENGINE, OPT_CHECK
21 } OPTION_CHOICE;
22
23 const OPTIONS pkeyparam_options[] = {
24     {"help", OPT_HELP, '-', "Display this summary"},
25     {"in", OPT_IN, '<', "Input file"},
26     {"out", OPT_OUT, '>', "Output file"},
27     {"text", OPT_TEXT, '-', "Print parameters as text"},
28     {"noout", OPT_NOOUT, '-', "Don't output encoded parameters"},
29 #ifndef OPENSSL_NO_ENGINE
30     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
31 #endif
32     {"check", OPT_CHECK, '-', "Check key param consistency"},
33     {NULL}
34 };
35
36 int pkeyparam_main(int argc, char **argv)
37 {
38     ENGINE *e = NULL;
39     BIO *in = NULL, *out = NULL;
40     EVP_PKEY *pkey = NULL;
41     int text = 0, noout = 0, ret = 1, check = 0;
42     OPTION_CHOICE o;
43     char *infile = NULL, *outfile = NULL, *prog;
44
45     prog = opt_init(argc, argv, pkeyparam_options);
46     while ((o = opt_next()) != OPT_EOF) {
47         switch (o) {
48         case OPT_EOF:
49         case OPT_ERR:
50  opthelp:
51             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
52             goto end;
53         case OPT_HELP:
54             opt_help(pkeyparam_options);
55             ret = 0;
56             goto end;
57         case OPT_IN:
58             infile = opt_arg();
59             break;
60         case OPT_OUT:
61             outfile = opt_arg();
62             break;
63         case OPT_ENGINE:
64             e = setup_engine(opt_arg(), 0);
65             break;
66         case OPT_TEXT:
67             text = 1;
68             break;
69         case OPT_NOOUT:
70             noout = 1;
71             break;
72         case OPT_CHECK:
73             check = 1;
74             break;
75         }
76     }
77     argc = opt_num_rest();
78     if (argc != 0)
79         goto opthelp;
80
81     in = bio_open_default(infile, 'r', FORMAT_PEM);
82     if (in == NULL)
83         goto end;
84     out = bio_open_default(outfile, 'w', FORMAT_PEM);
85     if (out == NULL)
86         goto end;
87     pkey = PEM_read_bio_Parameters(in, NULL);
88     if (pkey == NULL) {
89         BIO_printf(bio_err, "Error reading parameters\n");
90         ERR_print_errors(bio_err);
91         goto end;
92     }
93
94     if (check) {
95         int r;
96         EVP_PKEY_CTX *ctx;
97
98         ctx = EVP_PKEY_CTX_new(pkey, e);
99         if (ctx == NULL) {
100             ERR_print_errors(bio_err);
101             goto end;
102         }
103
104         r = EVP_PKEY_param_check(ctx);
105
106         if (r == 1) {
107             BIO_printf(out, "Parameters are valid\n");
108         } else {
109             /*
110              * Note: at least for RSA keys if this function returns
111              * -1, there will be no error reasons.
112              */
113             unsigned long err;
114
115             BIO_printf(out, "Parameters are invalid\n");
116
117             while ((err = ERR_peek_error()) != 0) {
118                 BIO_printf(out, "Detailed error: %s\n",
119                            ERR_reason_error_string(err));
120                 ERR_get_error(); /* remove err from error stack */
121             }
122         }
123         EVP_PKEY_CTX_free(ctx);
124     }
125
126     if (!noout)
127         PEM_write_bio_Parameters(out, pkey);
128
129     if (text)
130         EVP_PKEY_print_params(out, pkey, 0, NULL);
131
132     ret = 0;
133
134  end:
135     EVP_PKEY_free(pkey);
136     release_engine(e);
137     BIO_free_all(out);
138     BIO_free(in);
139
140     return ret;
141 }