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