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