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