xref: /freebsd/crypto/openssl/apps/rsa.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
874664626SKris Kennaway  */
974664626SKris Kennaway 
10*b077aed3SPierre Pronchery /* Necessary for legacy RSA public key export */
11*b077aed3SPierre Pronchery #define OPENSSL_SUPPRESS_DEPRECATED
12*b077aed3SPierre Pronchery 
133b4e3dcbSSimon L. B. Nielsen #include <openssl/opensslconf.h>
14*b077aed3SPierre Pronchery 
1574664626SKris Kennaway #include <stdio.h>
1674664626SKris Kennaway #include <stdlib.h>
1774664626SKris Kennaway #include <string.h>
1874664626SKris Kennaway #include <time.h>
1974664626SKris Kennaway #include "apps.h"
20e71b7053SJung-uk Kim #include "progs.h"
2174664626SKris Kennaway #include <openssl/bio.h>
2274664626SKris Kennaway #include <openssl/err.h>
2374664626SKris Kennaway #include <openssl/rsa.h>
2474664626SKris Kennaway #include <openssl/evp.h>
2574664626SKris Kennaway #include <openssl/x509.h>
2674664626SKris Kennaway #include <openssl/pem.h>
273b4e3dcbSSimon L. B. Nielsen #include <openssl/bn.h>
28*b077aed3SPierre Pronchery #include <openssl/encoder.h>
29*b077aed3SPierre Pronchery 
30*b077aed3SPierre Pronchery /*
31*b077aed3SPierre Pronchery  * This include is to get OSSL_KEYMGMT_SELECT_*, which feels a bit
32*b077aed3SPierre Pronchery  * much just for those macros...  they might serve better as EVP macros.
33*b077aed3SPierre Pronchery  */
34*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
35*b077aed3SPierre Pronchery 
36*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_RC4
37*b077aed3SPierre Pronchery # define DEFAULT_PVK_ENCR_STRENGTH      2
38*b077aed3SPierre Pronchery #else
39*b077aed3SPierre Pronchery # define DEFAULT_PVK_ENCR_STRENGTH      0
40*b077aed3SPierre Pronchery #endif
4174664626SKris Kennaway 
42e71b7053SJung-uk Kim typedef enum OPTION_choice {
43*b077aed3SPierre Pronchery     OPT_COMMON,
44e71b7053SJung-uk Kim     OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
45e71b7053SJung-uk Kim     OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
46e71b7053SJung-uk Kim     OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
47e71b7053SJung-uk Kim     /* Do not change the order here; see case statements below */
48e71b7053SJung-uk Kim     OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
49*b077aed3SPierre Pronchery     OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER,
50*b077aed3SPierre Pronchery     OPT_PROV_ENUM, OPT_TRADITIONAL
51e71b7053SJung-uk Kim } OPTION_CHOICE;
5274664626SKris Kennaway 
53e71b7053SJung-uk Kim const OPTIONS rsa_options[] = {
54*b077aed3SPierre Pronchery     OPT_SECTION("General"),
55e71b7053SJung-uk Kim     {"help", OPT_HELP, '-', "Display this summary"},
56*b077aed3SPierre Pronchery     {"check", OPT_CHECK, '-', "Verify key consistency"},
57*b077aed3SPierre Pronchery     {"", OPT_CIPHER, '-', "Any supported cipher"},
58*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
59*b077aed3SPierre Pronchery     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
60*b077aed3SPierre Pronchery #endif
61*b077aed3SPierre Pronchery 
62*b077aed3SPierre Pronchery     OPT_SECTION("Input"),
63e71b7053SJung-uk Kim     {"in", OPT_IN, 's', "Input file"},
64*b077aed3SPierre Pronchery     {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE)"},
65e71b7053SJung-uk Kim     {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
66e71b7053SJung-uk Kim     {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
67*b077aed3SPierre Pronchery     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
68*b077aed3SPierre Pronchery 
69*b077aed3SPierre Pronchery     OPT_SECTION("Output"),
70*b077aed3SPierre Pronchery     {"out", OPT_OUT, '>', "Output file"},
71*b077aed3SPierre Pronchery     {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
72*b077aed3SPierre Pronchery     {"pubout", OPT_PUBOUT, '-', "Output a public key"},
73e71b7053SJung-uk Kim     {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
74*b077aed3SPierre Pronchery     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
75e71b7053SJung-uk Kim     {"noout", OPT_NOOUT, '-', "Don't print key out"},
76e71b7053SJung-uk Kim     {"text", OPT_TEXT, '-', "Print the key in text"},
77e71b7053SJung-uk Kim     {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
78*b077aed3SPierre Pronchery     {"traditional", OPT_TRADITIONAL, '-',
79*b077aed3SPierre Pronchery      "Use traditional format for private keys"},
80*b077aed3SPierre Pronchery 
81*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_RC4
82*b077aed3SPierre Pronchery     OPT_SECTION("PVK"),
83e71b7053SJung-uk Kim     {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
84e71b7053SJung-uk Kim     {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
85e71b7053SJung-uk Kim     {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
86e71b7053SJung-uk Kim #endif
87*b077aed3SPierre Pronchery 
88*b077aed3SPierre Pronchery     OPT_PROV_OPTIONS,
89e71b7053SJung-uk Kim     {NULL}
90e71b7053SJung-uk Kim };
9174664626SKris Kennaway 
try_legacy_encoding(EVP_PKEY * pkey,int outformat,int pubout,BIO * out)92*b077aed3SPierre Pronchery static int try_legacy_encoding(EVP_PKEY *pkey, int outformat, int pubout,
93*b077aed3SPierre Pronchery                                BIO *out)
94*b077aed3SPierre Pronchery {
95*b077aed3SPierre Pronchery     int ret = 0;
96*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DEPRECATED_3_0
97*b077aed3SPierre Pronchery     const RSA *rsa = EVP_PKEY_get0_RSA(pkey);
98*b077aed3SPierre Pronchery 
99*b077aed3SPierre Pronchery     if (rsa == NULL)
100*b077aed3SPierre Pronchery         return 0;
101*b077aed3SPierre Pronchery 
102*b077aed3SPierre Pronchery     if (outformat == FORMAT_ASN1) {
103*b077aed3SPierre Pronchery         if (pubout == 2)
104*b077aed3SPierre Pronchery             ret = i2d_RSAPublicKey_bio(out, rsa) > 0;
105*b077aed3SPierre Pronchery         else
106*b077aed3SPierre Pronchery             ret = i2d_RSA_PUBKEY_bio(out, rsa) > 0;
107*b077aed3SPierre Pronchery     } else if (outformat == FORMAT_PEM) {
108*b077aed3SPierre Pronchery         if (pubout == 2)
109*b077aed3SPierre Pronchery             ret = PEM_write_bio_RSAPublicKey(out, rsa) > 0;
110*b077aed3SPierre Pronchery         else
111*b077aed3SPierre Pronchery             ret = PEM_write_bio_RSA_PUBKEY(out, rsa) > 0;
112*b077aed3SPierre Pronchery # ifndef OPENSSL_NO_DSA
113*b077aed3SPierre Pronchery     } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
114*b077aed3SPierre Pronchery         ret = i2b_PublicKey_bio(out, pkey) > 0;
115*b077aed3SPierre Pronchery # endif
116*b077aed3SPierre Pronchery     }
117*b077aed3SPierre Pronchery #endif
118*b077aed3SPierre Pronchery 
119*b077aed3SPierre Pronchery     return ret;
120*b077aed3SPierre Pronchery }
121*b077aed3SPierre Pronchery 
rsa_main(int argc,char ** argv)122e71b7053SJung-uk Kim int rsa_main(int argc, char **argv)
12374664626SKris Kennaway {
1245c87c606SMark Murray     ENGINE *e = NULL;
1255c87c606SMark Murray     BIO *out = NULL;
126*b077aed3SPierre Pronchery     EVP_PKEY *pkey = NULL;
127*b077aed3SPierre Pronchery     EVP_PKEY_CTX *pctx;
128*b077aed3SPierre Pronchery     EVP_CIPHER *enc = NULL;
129*b077aed3SPierre Pronchery     char *infile = NULL, *outfile = NULL, *ciphername = NULL, *prog;
130e71b7053SJung-uk Kim     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
131*b077aed3SPierre Pronchery     int private = 0;
132*b077aed3SPierre Pronchery     int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, text = 0, check = 0;
133e71b7053SJung-uk Kim     int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
134*b077aed3SPierre Pronchery     int pvk_encr = DEFAULT_PVK_ENCR_STRENGTH;
135e71b7053SJung-uk Kim     OPTION_CHOICE o;
136*b077aed3SPierre Pronchery     int traditional = 0;
137*b077aed3SPierre Pronchery     const char *output_type = NULL;
138*b077aed3SPierre Pronchery     const char *output_structure = NULL;
139*b077aed3SPierre Pronchery     int selection = 0;
140*b077aed3SPierre Pronchery     OSSL_ENCODER_CTX *ectx = NULL;
141e71b7053SJung-uk Kim 
142e71b7053SJung-uk Kim     prog = opt_init(argc, argv, rsa_options);
143e71b7053SJung-uk Kim     while ((o = opt_next()) != OPT_EOF) {
144e71b7053SJung-uk Kim         switch (o) {
145e71b7053SJung-uk Kim         case OPT_EOF:
146e71b7053SJung-uk Kim         case OPT_ERR:
147e71b7053SJung-uk Kim  opthelp:
148e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
149e71b7053SJung-uk Kim             goto end;
150e71b7053SJung-uk Kim         case OPT_HELP:
151e71b7053SJung-uk Kim             opt_help(rsa_options);
152e71b7053SJung-uk Kim             ret = 0;
153e71b7053SJung-uk Kim             goto end;
154e71b7053SJung-uk Kim         case OPT_INFORM:
155e71b7053SJung-uk Kim             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
156e71b7053SJung-uk Kim                 goto opthelp;
157e71b7053SJung-uk Kim             break;
158e71b7053SJung-uk Kim         case OPT_IN:
159e71b7053SJung-uk Kim             infile = opt_arg();
160e71b7053SJung-uk Kim             break;
161e71b7053SJung-uk Kim         case OPT_OUTFORM:
162e71b7053SJung-uk Kim             if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
163e71b7053SJung-uk Kim                 goto opthelp;
164e71b7053SJung-uk Kim             break;
165e71b7053SJung-uk Kim         case OPT_OUT:
166e71b7053SJung-uk Kim             outfile = opt_arg();
167e71b7053SJung-uk Kim             break;
168e71b7053SJung-uk Kim         case OPT_PASSIN:
169e71b7053SJung-uk Kim             passinarg = opt_arg();
170e71b7053SJung-uk Kim             break;
171e71b7053SJung-uk Kim         case OPT_PASSOUT:
172e71b7053SJung-uk Kim             passoutarg = opt_arg();
173e71b7053SJung-uk Kim             break;
174e71b7053SJung-uk Kim         case OPT_ENGINE:
175e71b7053SJung-uk Kim             e = setup_engine(opt_arg(), 0);
176e71b7053SJung-uk Kim             break;
177e71b7053SJung-uk Kim         case OPT_PUBIN:
178f579bf8eSKris Kennaway             pubin = 1;
179e71b7053SJung-uk Kim             break;
180e71b7053SJung-uk Kim         case OPT_PUBOUT:
181f579bf8eSKris Kennaway             pubout = 1;
182e71b7053SJung-uk Kim             break;
183e71b7053SJung-uk Kim         case OPT_RSAPUBKEY_IN:
1841f13597dSJung-uk Kim             pubin = 2;
185e71b7053SJung-uk Kim             break;
186e71b7053SJung-uk Kim         case OPT_RSAPUBKEY_OUT:
1871f13597dSJung-uk Kim             pubout = 2;
188e71b7053SJung-uk Kim             break;
189e71b7053SJung-uk Kim         case OPT_PVK_STRONG:    /* pvk_encr:= 2 */
190e71b7053SJung-uk Kim         case OPT_PVK_WEAK:      /* pvk_encr:= 1 */
191e71b7053SJung-uk Kim         case OPT_PVK_NONE:      /* pvk_encr:= 0 */
192e71b7053SJung-uk Kim             pvk_encr = (o - OPT_PVK_NONE);
193e71b7053SJung-uk Kim             break;
194e71b7053SJung-uk Kim         case OPT_NOOUT:
19574664626SKris Kennaway             noout = 1;
196e71b7053SJung-uk Kim             break;
197e71b7053SJung-uk Kim         case OPT_TEXT:
19874664626SKris Kennaway             text = 1;
199e71b7053SJung-uk Kim             break;
200e71b7053SJung-uk Kim         case OPT_MODULUS:
20174664626SKris Kennaway             modulus = 1;
202e71b7053SJung-uk Kim             break;
203e71b7053SJung-uk Kim         case OPT_CHECK:
20474664626SKris Kennaway             check = 1;
205e71b7053SJung-uk Kim             break;
206e71b7053SJung-uk Kim         case OPT_CIPHER:
207*b077aed3SPierre Pronchery             ciphername = opt_unknown();
208*b077aed3SPierre Pronchery             break;
209*b077aed3SPierre Pronchery         case OPT_PROV_CASES:
210*b077aed3SPierre Pronchery             if (!opt_provider(o))
211*b077aed3SPierre Pronchery                 goto end;
212*b077aed3SPierre Pronchery             break;
213*b077aed3SPierre Pronchery         case OPT_TRADITIONAL:
214*b077aed3SPierre Pronchery             traditional = 1;
21574664626SKris Kennaway             break;
21674664626SKris Kennaway         }
21774664626SKris Kennaway     }
218*b077aed3SPierre Pronchery 
219*b077aed3SPierre Pronchery     /* No extra arguments. */
220e71b7053SJung-uk Kim     argc = opt_num_rest();
221e71b7053SJung-uk Kim     if (argc != 0)
222e71b7053SJung-uk Kim         goto opthelp;
22374664626SKris Kennaway 
224*b077aed3SPierre Pronchery     if (ciphername != NULL) {
225*b077aed3SPierre Pronchery         if (!opt_cipher(ciphername, &enc))
226*b077aed3SPierre Pronchery             goto opthelp;
227*b077aed3SPierre Pronchery     }
228e71b7053SJung-uk Kim     private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
22974664626SKris Kennaway 
230e71b7053SJung-uk Kim     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
231f579bf8eSKris Kennaway         BIO_printf(bio_err, "Error getting passwords\n");
232f579bf8eSKris Kennaway         goto end;
233f579bf8eSKris Kennaway     }
234f579bf8eSKris Kennaway     if (check && pubin) {
235f579bf8eSKris Kennaway         BIO_printf(bio_err, "Only private keys can be checked\n");
236f579bf8eSKris Kennaway         goto end;
237f579bf8eSKris Kennaway     }
238f579bf8eSKris Kennaway 
2396f9291ceSJung-uk Kim     if (pubin) {
240*b077aed3SPierre Pronchery         int tmpformat = FORMAT_UNDEF;
241*b077aed3SPierre Pronchery 
2426f9291ceSJung-uk Kim         if (pubin == 2) {
2431f13597dSJung-uk Kim             if (informat == FORMAT_PEM)
2441f13597dSJung-uk Kim                 tmpformat = FORMAT_PEMRSA;
2451f13597dSJung-uk Kim             else if (informat == FORMAT_ASN1)
2461f13597dSJung-uk Kim                 tmpformat = FORMAT_ASN1RSA;
247e71b7053SJung-uk Kim         } else {
2481f13597dSJung-uk Kim             tmpformat = informat;
249e71b7053SJung-uk Kim         }
2501f13597dSJung-uk Kim 
251*b077aed3SPierre Pronchery         pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
252e71b7053SJung-uk Kim     } else {
253*b077aed3SPierre Pronchery         pkey = load_key(infile, informat, 1, passin, e, "private key");
254e71b7053SJung-uk Kim     }
2555c87c606SMark Murray 
256*b077aed3SPierre Pronchery     if (pkey == NULL) {
25774664626SKris Kennaway         ERR_print_errors(bio_err);
25874664626SKris Kennaway         goto end;
25974664626SKris Kennaway     }
260*b077aed3SPierre Pronchery     if (!EVP_PKEY_is_a(pkey, "RSA") && !EVP_PKEY_is_a(pkey, "RSA-PSS")) {
261*b077aed3SPierre Pronchery         BIO_printf(bio_err, "Not an RSA key\n");
262*b077aed3SPierre Pronchery         goto end;
263*b077aed3SPierre Pronchery     }
26474664626SKris Kennaway 
265e71b7053SJung-uk Kim     out = bio_open_owner(outfile, outformat, private);
266e71b7053SJung-uk Kim     if (out == NULL)
26774664626SKris Kennaway         goto end;
26874664626SKris Kennaway 
269e71b7053SJung-uk Kim     if (text) {
270e71b7053SJung-uk Kim         assert(pubin || private);
271*b077aed3SPierre Pronchery         if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
272*b077aed3SPierre Pronchery             || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
27374664626SKris Kennaway             perror(outfile);
27474664626SKris Kennaway             ERR_print_errors(bio_err);
27574664626SKris Kennaway             goto end;
27674664626SKris Kennaway         }
277e71b7053SJung-uk Kim     }
27874664626SKris Kennaway 
2796f9291ceSJung-uk Kim     if (modulus) {
280*b077aed3SPierre Pronchery         BIGNUM *n = NULL;
281*b077aed3SPierre Pronchery 
282*b077aed3SPierre Pronchery         /* Every RSA key has an 'n' */
283*b077aed3SPierre Pronchery         EVP_PKEY_get_bn_param(pkey, "n", &n);
284f579bf8eSKris Kennaway         BIO_printf(out, "Modulus=");
285e71b7053SJung-uk Kim         BN_print(out, n);
286f579bf8eSKris Kennaway         BIO_printf(out, "\n");
287*b077aed3SPierre Pronchery         BN_free(n);
28874664626SKris Kennaway     }
28974664626SKris Kennaway 
2906f9291ceSJung-uk Kim     if (check) {
291*b077aed3SPierre Pronchery         int r;
292*b077aed3SPierre Pronchery 
293*b077aed3SPierre Pronchery         pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
294*b077aed3SPierre Pronchery         if (pctx == NULL) {
295*b077aed3SPierre Pronchery             BIO_printf(bio_err, "RSA unable to create PKEY context\n");
296*b077aed3SPierre Pronchery             ERR_print_errors(bio_err);
297*b077aed3SPierre Pronchery             goto end;
298*b077aed3SPierre Pronchery         }
299*b077aed3SPierre Pronchery         r = EVP_PKEY_check(pctx);
300*b077aed3SPierre Pronchery         EVP_PKEY_CTX_free(pctx);
30174664626SKris Kennaway 
302e71b7053SJung-uk Kim         if (r == 1) {
30374664626SKris Kennaway             BIO_printf(out, "RSA key ok\n");
304e71b7053SJung-uk Kim         } else if (r == 0) {
305*b077aed3SPierre Pronchery             BIO_printf(bio_err, "RSA key not ok\n");
306*b077aed3SPierre Pronchery             ERR_print_errors(bio_err);
307*b077aed3SPierre Pronchery         } else if (r < 0) {
30874664626SKris Kennaway             ERR_print_errors(bio_err);
30974664626SKris Kennaway             goto end;
31074664626SKris Kennaway         }
31174664626SKris Kennaway     }
31274664626SKris Kennaway 
3136f9291ceSJung-uk Kim     if (noout) {
314f579bf8eSKris Kennaway         ret = 0;
315f579bf8eSKris Kennaway         goto end;
316f579bf8eSKris Kennaway     }
317f579bf8eSKris Kennaway     BIO_printf(bio_err, "writing RSA key\n");
318c9cf7b5cSJung-uk Kim 
319*b077aed3SPierre Pronchery     /* Choose output type for the format */
320*b077aed3SPierre Pronchery     if (outformat == FORMAT_ASN1) {
321*b077aed3SPierre Pronchery         output_type = "DER";
322*b077aed3SPierre Pronchery     } else if (outformat == FORMAT_PEM) {
323*b077aed3SPierre Pronchery         output_type = "PEM";
324*b077aed3SPierre Pronchery     } else if (outformat == FORMAT_MSBLOB) {
325*b077aed3SPierre Pronchery         output_type = "MSBLOB";
326*b077aed3SPierre Pronchery     } else if (outformat == FORMAT_PVK) {
327e71b7053SJung-uk Kim         if (pubin) {
328e71b7053SJung-uk Kim             BIO_printf(bio_err, "PVK form impossible with public key input\n");
329e71b7053SJung-uk Kim             goto end;
330e71b7053SJung-uk Kim         }
331*b077aed3SPierre Pronchery         output_type = "PVK";
332f579bf8eSKris Kennaway     } else {
33374664626SKris Kennaway         BIO_printf(bio_err, "bad output format specified for outfile\n");
33474664626SKris Kennaway         goto end;
33574664626SKris Kennaway     }
336*b077aed3SPierre Pronchery 
337*b077aed3SPierre Pronchery     /* Select what you want in the output */
338*b077aed3SPierre Pronchery     if (pubout || pubin) {
339*b077aed3SPierre Pronchery         selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
340*b077aed3SPierre Pronchery     } else {
341*b077aed3SPierre Pronchery         assert(private);
342*b077aed3SPierre Pronchery         selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
343*b077aed3SPierre Pronchery                      | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
344*b077aed3SPierre Pronchery     }
345*b077aed3SPierre Pronchery 
346*b077aed3SPierre Pronchery     /* For DER based output, select the desired output structure */
347*b077aed3SPierre Pronchery     if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
348*b077aed3SPierre Pronchery         if (pubout || pubin) {
349*b077aed3SPierre Pronchery             if (pubout == 2)
350*b077aed3SPierre Pronchery                 output_structure = "pkcs1"; /* "type-specific" would work too */
351*b077aed3SPierre Pronchery             else
352*b077aed3SPierre Pronchery                 output_structure = "SubjectPublicKeyInfo";
353*b077aed3SPierre Pronchery         } else {
354*b077aed3SPierre Pronchery             assert(private);
355*b077aed3SPierre Pronchery             if (traditional)
356*b077aed3SPierre Pronchery                 output_structure = "pkcs1"; /* "type-specific" would work too */
357*b077aed3SPierre Pronchery             else
358*b077aed3SPierre Pronchery                 output_structure = "PrivateKeyInfo";
359*b077aed3SPierre Pronchery         }
360*b077aed3SPierre Pronchery     }
361*b077aed3SPierre Pronchery 
362*b077aed3SPierre Pronchery     /* Now, perform the encoding */
363*b077aed3SPierre Pronchery     ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
364*b077aed3SPierre Pronchery                                          output_type, output_structure,
365*b077aed3SPierre Pronchery                                          NULL);
366*b077aed3SPierre Pronchery     if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
367*b077aed3SPierre Pronchery         if ((!pubout && !pubin)
368*b077aed3SPierre Pronchery             || !try_legacy_encoding(pkey, outformat, pubout, out))
369*b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s format not supported\n", output_type);
370*b077aed3SPierre Pronchery         else
371*b077aed3SPierre Pronchery             ret = 0;
372*b077aed3SPierre Pronchery         goto end;
373*b077aed3SPierre Pronchery     }
374*b077aed3SPierre Pronchery 
375*b077aed3SPierre Pronchery     /* Passphrase setup */
376*b077aed3SPierre Pronchery     if (enc != NULL)
377*b077aed3SPierre Pronchery         OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
378*b077aed3SPierre Pronchery 
379*b077aed3SPierre Pronchery     /* Default passphrase prompter */
380*b077aed3SPierre Pronchery     if (enc != NULL || outformat == FORMAT_PVK) {
381*b077aed3SPierre Pronchery         OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
382*b077aed3SPierre Pronchery         if (passout != NULL)
383*b077aed3SPierre Pronchery             /* When passout given, override the passphrase prompter */
384*b077aed3SPierre Pronchery             OSSL_ENCODER_CTX_set_passphrase(ectx,
385*b077aed3SPierre Pronchery                                             (const unsigned char *)passout,
386*b077aed3SPierre Pronchery                                             strlen(passout));
387*b077aed3SPierre Pronchery     }
388*b077aed3SPierre Pronchery 
389*b077aed3SPierre Pronchery     /* PVK is a bit special... */
390*b077aed3SPierre Pronchery     if (outformat == FORMAT_PVK) {
391*b077aed3SPierre Pronchery         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
392*b077aed3SPierre Pronchery 
393*b077aed3SPierre Pronchery         params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
394*b077aed3SPierre Pronchery         if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
395*b077aed3SPierre Pronchery             BIO_printf(bio_err, "invalid PVK encryption level\n");
396*b077aed3SPierre Pronchery             goto end;
397*b077aed3SPierre Pronchery         }
398*b077aed3SPierre Pronchery     }
399*b077aed3SPierre Pronchery 
400*b077aed3SPierre Pronchery     if (!OSSL_ENCODER_to_bio(ectx, out)) {
401f579bf8eSKris Kennaway         BIO_printf(bio_err, "unable to write key\n");
40274664626SKris Kennaway         ERR_print_errors(bio_err);
403*b077aed3SPierre Pronchery         goto end;
404e71b7053SJung-uk Kim     }
405*b077aed3SPierre Pronchery     ret = 0;
40674664626SKris Kennaway  end:
407*b077aed3SPierre Pronchery     OSSL_ENCODER_CTX_free(ectx);
4086cf8931aSJung-uk Kim     release_engine(e);
4096f9291ceSJung-uk Kim     BIO_free_all(out);
410*b077aed3SPierre Pronchery     EVP_PKEY_free(pkey);
411*b077aed3SPierre Pronchery     EVP_CIPHER_free(enc);
4126f9291ceSJung-uk Kim     OPENSSL_free(passin);
4136f9291ceSJung-uk Kim     OPENSSL_free(passout);
414e71b7053SJung-uk Kim     return ret;
41574664626SKris Kennaway }
416