1*646e9a2fStb /* $OpenBSD: pkeyparam.c,v 1.19 2024/08/29 17:01:02 tb Exp $ */ 2dab3f910Sjsing /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3dab3f910Sjsing * project 2006 4dab3f910Sjsing */ 5dab3f910Sjsing /* ==================================================================== 6dab3f910Sjsing * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 7dab3f910Sjsing * 8dab3f910Sjsing * Redistribution and use in source and binary forms, with or without 9dab3f910Sjsing * modification, are permitted provided that the following conditions 10dab3f910Sjsing * are met: 11dab3f910Sjsing * 12dab3f910Sjsing * 1. Redistributions of source code must retain the above copyright 13dab3f910Sjsing * notice, this list of conditions and the following disclaimer. 14dab3f910Sjsing * 15dab3f910Sjsing * 2. Redistributions in binary form must reproduce the above copyright 16dab3f910Sjsing * notice, this list of conditions and the following disclaimer in 17dab3f910Sjsing * the documentation and/or other materials provided with the 18dab3f910Sjsing * distribution. 19dab3f910Sjsing * 20dab3f910Sjsing * 3. All advertising materials mentioning features or use of this 21dab3f910Sjsing * software must display the following acknowledgment: 22dab3f910Sjsing * "This product includes software developed by the OpenSSL Project 23dab3f910Sjsing * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24dab3f910Sjsing * 25dab3f910Sjsing * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26dab3f910Sjsing * endorse or promote products derived from this software without 27dab3f910Sjsing * prior written permission. For written permission, please contact 28dab3f910Sjsing * licensing@OpenSSL.org. 29dab3f910Sjsing * 30dab3f910Sjsing * 5. Products derived from this software may not be called "OpenSSL" 31dab3f910Sjsing * nor may "OpenSSL" appear in their names without prior written 32dab3f910Sjsing * permission of the OpenSSL Project. 33dab3f910Sjsing * 34dab3f910Sjsing * 6. Redistributions of any form whatsoever must retain the following 35dab3f910Sjsing * acknowledgment: 36dab3f910Sjsing * "This product includes software developed by the OpenSSL Project 37dab3f910Sjsing * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38dab3f910Sjsing * 39dab3f910Sjsing * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40dab3f910Sjsing * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41dab3f910Sjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42dab3f910Sjsing * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43dab3f910Sjsing * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44dab3f910Sjsing * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45dab3f910Sjsing * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46dab3f910Sjsing * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47dab3f910Sjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48dab3f910Sjsing * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49dab3f910Sjsing * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50dab3f910Sjsing * OF THE POSSIBILITY OF SUCH DAMAGE. 51dab3f910Sjsing * ==================================================================== 52dab3f910Sjsing * 53dab3f910Sjsing * This product includes cryptographic software written by Eric Young 54dab3f910Sjsing * (eay@cryptsoft.com). This product includes software written by Tim 55dab3f910Sjsing * Hudson (tjh@cryptsoft.com). 56dab3f910Sjsing * 57dab3f910Sjsing */ 58dab3f910Sjsing 59dab3f910Sjsing #include <stdio.h> 60dab3f910Sjsing #include <string.h> 61dab3f910Sjsing 62dab3f910Sjsing #include "apps.h" 63dab3f910Sjsing 64dab3f910Sjsing #include <openssl/err.h> 65dab3f910Sjsing #include <openssl/evp.h> 66dab3f910Sjsing #include <openssl/pem.h> 67dab3f910Sjsing 68efc7e65fStb static struct { 695c033291Sjsing char *infile; 705c033291Sjsing int noout; 715c033291Sjsing char *outfile; 725c033291Sjsing int text; 73e7718adaStb } cfg; 745c033291Sjsing 75ea149709Sguenther static const struct option pkeyparam_options[] = { 765c033291Sjsing { 775c033291Sjsing .name = "in", 785c033291Sjsing .argname = "file", 795c033291Sjsing .desc = "Input file (default stdin)", 805c033291Sjsing .type = OPTION_ARG, 81e7718adaStb .opt.arg = &cfg.infile, 825c033291Sjsing }, 835c033291Sjsing { 845c033291Sjsing .name = "noout", 855c033291Sjsing .desc = "Do not print encoded version of the parameters", 865c033291Sjsing .type = OPTION_FLAG, 87e7718adaStb .opt.flag = &cfg.noout, 885c033291Sjsing }, 895c033291Sjsing { 905c033291Sjsing .name = "out", 915c033291Sjsing .argname = "file", 925c033291Sjsing .desc = "Output file (default stdout)", 935c033291Sjsing .type = OPTION_ARG, 94e7718adaStb .opt.arg = &cfg.outfile, 955c033291Sjsing }, 965c033291Sjsing { 975c033291Sjsing .name = "text", 985c033291Sjsing .desc = "Print out the parameters in plain text", 995c033291Sjsing .type = OPTION_FLAG, 100e7718adaStb .opt.flag = &cfg.text, 1015c033291Sjsing }, 1025c033291Sjsing { NULL }, 1035c033291Sjsing }; 1045c033291Sjsing 1055c033291Sjsing static void 106440d1414Stb pkeyparam_usage(void) 1075c033291Sjsing { 1085c033291Sjsing fprintf(stderr, 109*646e9a2fStb "usage: pkeyparam [-in file] [-noout] [-out file] [-text]\n"); 1105c033291Sjsing options_usage(pkeyparam_options); 1115c033291Sjsing } 1125c033291Sjsing 113dab3f910Sjsing int 114dab3f910Sjsing pkeyparam_main(int argc, char **argv) 115dab3f910Sjsing { 116dab3f910Sjsing BIO *in = NULL, *out = NULL; 117dab3f910Sjsing EVP_PKEY *pkey = NULL; 118dab3f910Sjsing int ret = 1; 119dab3f910Sjsing 12051811eadSderaadt if (pledge("stdio cpath wpath rpath", NULL) == -1) { 1219bc487adSdoug perror("pledge"); 122e370f0eeSdoug exit(1); 123e370f0eeSdoug } 1249bc487adSdoug 125e7718adaStb memset(&cfg, 0, sizeof(cfg)); 126dab3f910Sjsing 1275c033291Sjsing if (options_parse(argc, argv, pkeyparam_options, NULL, NULL) != 0) { 1285c033291Sjsing pkeyparam_usage(); 1295c033291Sjsing return (1); 130dab3f910Sjsing } 131dab3f910Sjsing 132e7718adaStb if (cfg.infile) { 133e7718adaStb if (!(in = BIO_new_file(cfg.infile, "r"))) { 1345c033291Sjsing BIO_printf(bio_err, "Can't open input file %s\n", 135e7718adaStb cfg.infile); 136ab2cf38eSjsing goto end; 137dab3f910Sjsing } 138dab3f910Sjsing } else 139dab3f910Sjsing in = BIO_new_fp(stdin, BIO_NOCLOSE); 140dab3f910Sjsing 141e7718adaStb if (cfg.outfile) { 142e7718adaStb if (!(out = BIO_new_file(cfg.outfile, "w"))) { 1435c033291Sjsing BIO_printf(bio_err, "Can't open output file %s\n", 144e7718adaStb cfg.outfile); 145dab3f910Sjsing goto end; 146dab3f910Sjsing } 147dab3f910Sjsing } else { 148dab3f910Sjsing out = BIO_new_fp(stdout, BIO_NOCLOSE); 149dab3f910Sjsing } 150dab3f910Sjsing 151dab3f910Sjsing pkey = PEM_read_bio_Parameters(in, NULL); 152dab3f910Sjsing if (!pkey) { 153dab3f910Sjsing BIO_printf(bio_err, "Error reading parameters\n"); 154dab3f910Sjsing ERR_print_errors(bio_err); 155dab3f910Sjsing goto end; 156dab3f910Sjsing } 157d40b1e08Stb 158e7718adaStb if (!cfg.noout) 159dab3f910Sjsing PEM_write_bio_Parameters(out, pkey); 160dab3f910Sjsing 161e7718adaStb if (cfg.text) 162dab3f910Sjsing EVP_PKEY_print_params(out, pkey, 0, NULL); 163dab3f910Sjsing 164dab3f910Sjsing ret = 0; 165dab3f910Sjsing 166dab3f910Sjsing end: 167dab3f910Sjsing EVP_PKEY_free(pkey); 168dab3f910Sjsing BIO_free_all(out); 169dab3f910Sjsing BIO_free(in); 170dab3f910Sjsing 171dab3f910Sjsing return ret; 172dab3f910Sjsing } 173