]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
846e33c7 | 2 | * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved. |
3e4585c8 | 3 | * |
846e33c7 RS |
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 | |
3e4585c8 | 8 | */ |
846e33c7 | 9 | |
3e4585c8 DSH |
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 | ||
7e1b7485 RS |
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 | ||
44c83ebd | 22 | const OPTIONS pkeyparam_options[] = { |
7e1b7485 RS |
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 | }; | |
3e4585c8 | 33 | |
7e1b7485 | 34 | int pkeyparam_main(int argc, char **argv) |
0f113f3e | 35 | { |
0f113f3e | 36 | BIO *in = NULL, *out = NULL; |
0f113f3e | 37 | EVP_PKEY *pkey = NULL; |
7e1b7485 RS |
38 | int text = 0, noout = 0, ret = 1; |
39 | OPTION_CHOICE o; | |
333b070e | 40 | char *infile = NULL, *outfile = NULL, *prog; |
7e1b7485 RS |
41 | |
42 | prog = opt_init(argc, argv, pkeyparam_options); | |
43 | while ((o = opt_next()) != OPT_EOF) { | |
44 | switch (o) { | |
45 | case OPT_EOF: | |
46 | case OPT_ERR: | |
03358517 | 47 | opthelp: |
7e1b7485 RS |
48 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
49 | goto end; | |
50 | case OPT_HELP: | |
51 | opt_help(pkeyparam_options); | |
52 | ret = 0; | |
53 | goto end; | |
54 | case OPT_IN: | |
55 | infile = opt_arg(); | |
56 | break; | |
57 | case OPT_OUT: | |
58 | outfile = opt_arg(); | |
59 | break; | |
60 | case OPT_ENGINE: | |
333b070e | 61 | (void)setup_engine(opt_arg(), 0); |
7e1b7485 RS |
62 | break; |
63 | case OPT_TEXT: | |
0f113f3e | 64 | text = 1; |
7e1b7485 RS |
65 | break; |
66 | case OPT_NOOUT: | |
0f113f3e | 67 | noout = 1; |
7e1b7485 RS |
68 | break; |
69 | } | |
0f113f3e | 70 | } |
7e1b7485 | 71 | argc = opt_num_rest(); |
03358517 KR |
72 | if (argc != 0) |
73 | goto opthelp; | |
3e4585c8 | 74 | |
bdd58d98 | 75 | in = bio_open_default(infile, 'r', FORMAT_PEM); |
7e1b7485 RS |
76 | if (in == NULL) |
77 | goto end; | |
bdd58d98 | 78 | out = bio_open_default(outfile, 'w', FORMAT_PEM); |
7e1b7485 RS |
79 | if (out == NULL) |
80 | goto end; | |
0f113f3e MC |
81 | pkey = PEM_read_bio_Parameters(in, NULL); |
82 | if (!pkey) { | |
83 | BIO_printf(bio_err, "Error reading parameters\n"); | |
84 | ERR_print_errors(bio_err); | |
85 | goto end; | |
86 | } | |
3e4585c8 | 87 | |
0f113f3e MC |
88 | if (!noout) |
89 | PEM_write_bio_Parameters(out, pkey); | |
3e4585c8 | 90 | |
0f113f3e MC |
91 | if (text) |
92 | EVP_PKEY_print_params(out, pkey, 0, NULL); | |
3e4585c8 | 93 | |
0f113f3e | 94 | ret = 0; |
3e4585c8 | 95 | |
0f113f3e MC |
96 | end: |
97 | EVP_PKEY_free(pkey); | |
98 | BIO_free_all(out); | |
99 | BIO_free(in); | |
3e4585c8 | 100 | |
0f113f3e MC |
101 | return ret; |
102 | } |