xref: /netbsd-src/crypto/external/bsd/openssl/dist/apps/rsa.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1c7da899bSchristos /*
2*b0d17251Schristos  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5c7da899bSchristos  * this file except in compliance with the License.  You can obtain a copy
6c7da899bSchristos  * in the file LICENSE in the source distribution or at
7c7da899bSchristos  * https://www.openssl.org/source/license.html
8a89c9211Schristos  */
9a89c9211Schristos 
10*b0d17251Schristos /* Necessary for legacy RSA public key export */
11*b0d17251Schristos #define OPENSSL_SUPPRESS_DEPRECATED
12*b0d17251Schristos 
13a89c9211Schristos #include <openssl/opensslconf.h>
14*b0d17251Schristos 
15a89c9211Schristos #include <stdio.h>
16a89c9211Schristos #include <stdlib.h>
17a89c9211Schristos #include <string.h>
18a89c9211Schristos #include <time.h>
19a89c9211Schristos #include "apps.h"
2013d40330Schristos #include "progs.h"
21a89c9211Schristos #include <openssl/bio.h>
22a89c9211Schristos #include <openssl/err.h>
23a89c9211Schristos #include <openssl/rsa.h>
24a89c9211Schristos #include <openssl/evp.h>
25a89c9211Schristos #include <openssl/x509.h>
26a89c9211Schristos #include <openssl/pem.h>
27a89c9211Schristos #include <openssl/bn.h>
28*b0d17251Schristos #include <openssl/encoder.h>
29*b0d17251Schristos 
30*b0d17251Schristos /*
31*b0d17251Schristos  * This include is to get OSSL_KEYMGMT_SELECT_*, which feels a bit
32*b0d17251Schristos  * much just for those macros...  they might serve better as EVP macros.
33*b0d17251Schristos  */
34*b0d17251Schristos #include <openssl/core_dispatch.h>
35*b0d17251Schristos 
36*b0d17251Schristos #ifndef OPENSSL_NO_RC4
37*b0d17251Schristos # define DEFAULT_PVK_ENCR_STRENGTH      2
38*b0d17251Schristos #else
39*b0d17251Schristos # define DEFAULT_PVK_ENCR_STRENGTH      0
40*b0d17251Schristos #endif
41a89c9211Schristos 
42c7da899bSchristos typedef enum OPTION_choice {
43*b0d17251Schristos     OPT_COMMON,
44c7da899bSchristos     OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
45c7da899bSchristos     OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
46c7da899bSchristos     OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
47c7da899bSchristos     /* Do not change the order here; see case statements below */
48c7da899bSchristos     OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
49*b0d17251Schristos     OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER,
50*b0d17251Schristos     OPT_PROV_ENUM, OPT_TRADITIONAL
51c7da899bSchristos } OPTION_CHOICE;
52a89c9211Schristos 
5313d40330Schristos const OPTIONS rsa_options[] = {
54*b0d17251Schristos     OPT_SECTION("General"),
55c7da899bSchristos     {"help", OPT_HELP, '-', "Display this summary"},
56*b0d17251Schristos     {"check", OPT_CHECK, '-', "Verify key consistency"},
57*b0d17251Schristos     {"", OPT_CIPHER, '-', "Any supported cipher"},
58*b0d17251Schristos #ifndef OPENSSL_NO_ENGINE
59*b0d17251Schristos     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
60*b0d17251Schristos #endif
61*b0d17251Schristos 
62*b0d17251Schristos     OPT_SECTION("Input"),
63c7da899bSchristos     {"in", OPT_IN, 's', "Input file"},
64*b0d17251Schristos     {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE)"},
65c7da899bSchristos     {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
66c7da899bSchristos     {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
67*b0d17251Schristos     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
68*b0d17251Schristos 
69*b0d17251Schristos     OPT_SECTION("Output"),
70*b0d17251Schristos     {"out", OPT_OUT, '>', "Output file"},
71*b0d17251Schristos     {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
72*b0d17251Schristos     {"pubout", OPT_PUBOUT, '-', "Output a public key"},
73c7da899bSchristos     {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
74*b0d17251Schristos     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
75c7da899bSchristos     {"noout", OPT_NOOUT, '-', "Don't print key out"},
76c7da899bSchristos     {"text", OPT_TEXT, '-', "Print the key in text"},
77c7da899bSchristos     {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
78*b0d17251Schristos     {"traditional", OPT_TRADITIONAL, '-',
79*b0d17251Schristos      "Use traditional format for private keys"},
80*b0d17251Schristos 
81*b0d17251Schristos #ifndef OPENSSL_NO_RC4
82*b0d17251Schristos     OPT_SECTION("PVK"),
83c7da899bSchristos     {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
84c7da899bSchristos     {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
85c7da899bSchristos     {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
86c7da899bSchristos #endif
87*b0d17251Schristos 
88*b0d17251Schristos     OPT_PROV_OPTIONS,
89c7da899bSchristos     {NULL}
90c7da899bSchristos };
91a89c9211Schristos 
try_legacy_encoding(EVP_PKEY * pkey,int outformat,int pubout,BIO * out)92*b0d17251Schristos static int try_legacy_encoding(EVP_PKEY *pkey, int outformat, int pubout,
93*b0d17251Schristos                                BIO *out)
94*b0d17251Schristos {
95*b0d17251Schristos     int ret = 0;
96*b0d17251Schristos #ifndef OPENSSL_NO_DEPRECATED_3_0
97*b0d17251Schristos     const RSA *rsa = EVP_PKEY_get0_RSA(pkey);
98*b0d17251Schristos 
99*b0d17251Schristos     if (rsa == NULL)
100*b0d17251Schristos         return 0;
101*b0d17251Schristos 
102*b0d17251Schristos     if (outformat == FORMAT_ASN1) {
103*b0d17251Schristos         if (pubout == 2)
104*b0d17251Schristos             ret = i2d_RSAPublicKey_bio(out, rsa) > 0;
105*b0d17251Schristos         else
106*b0d17251Schristos             ret = i2d_RSA_PUBKEY_bio(out, rsa) > 0;
107*b0d17251Schristos     } else if (outformat == FORMAT_PEM) {
108*b0d17251Schristos         if (pubout == 2)
109*b0d17251Schristos             ret = PEM_write_bio_RSAPublicKey(out, rsa) > 0;
110*b0d17251Schristos         else
111*b0d17251Schristos             ret = PEM_write_bio_RSA_PUBKEY(out, rsa) > 0;
112*b0d17251Schristos # ifndef OPENSSL_NO_DSA
113*b0d17251Schristos     } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
114*b0d17251Schristos         ret = i2b_PublicKey_bio(out, pkey) > 0;
115*b0d17251Schristos # endif
116*b0d17251Schristos     }
117*b0d17251Schristos #endif
118*b0d17251Schristos 
119*b0d17251Schristos     return ret;
120*b0d17251Schristos }
121*b0d17251Schristos 
rsa_main(int argc,char ** argv)122c7da899bSchristos int rsa_main(int argc, char **argv)
123a89c9211Schristos {
124a89c9211Schristos     ENGINE *e = NULL;
125a89c9211Schristos     BIO *out = NULL;
126*b0d17251Schristos     EVP_PKEY *pkey = NULL;
127*b0d17251Schristos     EVP_PKEY_CTX *pctx;
128*b0d17251Schristos     EVP_CIPHER *enc = NULL;
129*b0d17251Schristos     char *infile = NULL, *outfile = NULL, *ciphername = NULL, *prog;
130c7da899bSchristos     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
131*b0d17251Schristos     int private = 0;
132*b0d17251Schristos     int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, text = 0, check = 0;
133c7da899bSchristos     int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
134*b0d17251Schristos     int pvk_encr = DEFAULT_PVK_ENCR_STRENGTH;
135c7da899bSchristos     OPTION_CHOICE o;
136*b0d17251Schristos     int traditional = 0;
137*b0d17251Schristos     const char *output_type = NULL;
138*b0d17251Schristos     const char *output_structure = NULL;
139*b0d17251Schristos     int selection = 0;
140*b0d17251Schristos     OSSL_ENCODER_CTX *ectx = NULL;
141c7da899bSchristos 
142c7da899bSchristos     prog = opt_init(argc, argv, rsa_options);
143c7da899bSchristos     while ((o = opt_next()) != OPT_EOF) {
144c7da899bSchristos         switch (o) {
145c7da899bSchristos         case OPT_EOF:
146c7da899bSchristos         case OPT_ERR:
147c7da899bSchristos  opthelp:
148c7da899bSchristos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
149c7da899bSchristos             goto end;
150c7da899bSchristos         case OPT_HELP:
151c7da899bSchristos             opt_help(rsa_options);
152c7da899bSchristos             ret = 0;
153c7da899bSchristos             goto end;
154c7da899bSchristos         case OPT_INFORM:
155c7da899bSchristos             if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
156c7da899bSchristos                 goto opthelp;
157c7da899bSchristos             break;
158c7da899bSchristos         case OPT_IN:
159c7da899bSchristos             infile = opt_arg();
160c7da899bSchristos             break;
161c7da899bSchristos         case OPT_OUTFORM:
162c7da899bSchristos             if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
163c7da899bSchristos                 goto opthelp;
164c7da899bSchristos             break;
165c7da899bSchristos         case OPT_OUT:
166c7da899bSchristos             outfile = opt_arg();
167c7da899bSchristos             break;
168c7da899bSchristos         case OPT_PASSIN:
169c7da899bSchristos             passinarg = opt_arg();
170c7da899bSchristos             break;
171c7da899bSchristos         case OPT_PASSOUT:
172c7da899bSchristos             passoutarg = opt_arg();
173c7da899bSchristos             break;
174c7da899bSchristos         case OPT_ENGINE:
175c7da899bSchristos             e = setup_engine(opt_arg(), 0);
176c7da899bSchristos             break;
177c7da899bSchristos         case OPT_PUBIN:
178a89c9211Schristos             pubin = 1;
179c7da899bSchristos             break;
180c7da899bSchristos         case OPT_PUBOUT:
181a89c9211Schristos             pubout = 1;
182c7da899bSchristos             break;
183c7da899bSchristos         case OPT_RSAPUBKEY_IN:
184a89c9211Schristos             pubin = 2;
185c7da899bSchristos             break;
186c7da899bSchristos         case OPT_RSAPUBKEY_OUT:
187a89c9211Schristos             pubout = 2;
188c7da899bSchristos             break;
189c7da899bSchristos         case OPT_PVK_STRONG:    /* pvk_encr:= 2 */
190c7da899bSchristos         case OPT_PVK_WEAK:      /* pvk_encr:= 1 */
191c7da899bSchristos         case OPT_PVK_NONE:      /* pvk_encr:= 0 */
192c7da899bSchristos             pvk_encr = (o - OPT_PVK_NONE);
193c7da899bSchristos             break;
194c7da899bSchristos         case OPT_NOOUT:
195a89c9211Schristos             noout = 1;
196c7da899bSchristos             break;
197c7da899bSchristos         case OPT_TEXT:
198a89c9211Schristos             text = 1;
199c7da899bSchristos             break;
200c7da899bSchristos         case OPT_MODULUS:
201a89c9211Schristos             modulus = 1;
202c7da899bSchristos             break;
203c7da899bSchristos         case OPT_CHECK:
204a89c9211Schristos             check = 1;
205c7da899bSchristos             break;
206c7da899bSchristos         case OPT_CIPHER:
207*b0d17251Schristos             ciphername = opt_unknown();
208*b0d17251Schristos             break;
209*b0d17251Schristos         case OPT_PROV_CASES:
210*b0d17251Schristos             if (!opt_provider(o))
211*b0d17251Schristos                 goto end;
212*b0d17251Schristos             break;
213*b0d17251Schristos         case OPT_TRADITIONAL:
214*b0d17251Schristos             traditional = 1;
215a89c9211Schristos             break;
216a89c9211Schristos         }
217a89c9211Schristos     }
218*b0d17251Schristos 
219*b0d17251Schristos     /* No extra arguments. */
220c7da899bSchristos     argc = opt_num_rest();
221c7da899bSchristos     if (argc != 0)
222c7da899bSchristos         goto opthelp;
223a89c9211Schristos 
224*b0d17251Schristos     if (ciphername != NULL) {
225*b0d17251Schristos         if (!opt_cipher(ciphername, &enc))
226*b0d17251Schristos             goto opthelp;
227*b0d17251Schristos     }
228c7da899bSchristos     private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
229a89c9211Schristos 
230c7da899bSchristos     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
231a89c9211Schristos         BIO_printf(bio_err, "Error getting passwords\n");
232a89c9211Schristos         goto end;
233a89c9211Schristos     }
234a89c9211Schristos     if (check && pubin) {
235a89c9211Schristos         BIO_printf(bio_err, "Only private keys can be checked\n");
236a89c9211Schristos         goto end;
237a89c9211Schristos     }
238a89c9211Schristos 
239635165faSspz     if (pubin) {
240*b0d17251Schristos         int tmpformat = FORMAT_UNDEF;
241*b0d17251Schristos 
242635165faSspz         if (pubin == 2) {
243a89c9211Schristos             if (informat == FORMAT_PEM)
244a89c9211Schristos                 tmpformat = FORMAT_PEMRSA;
245a89c9211Schristos             else if (informat == FORMAT_ASN1)
246a89c9211Schristos                 tmpformat = FORMAT_ASN1RSA;
24713d40330Schristos         } else {
248a89c9211Schristos             tmpformat = informat;
24913d40330Schristos         }
250a89c9211Schristos 
251*b0d17251Schristos         pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
25213d40330Schristos     } else {
253*b0d17251Schristos         pkey = load_key(infile, informat, 1, passin, e, "private key");
25413d40330Schristos     }
255a89c9211Schristos 
256*b0d17251Schristos     if (pkey == NULL) {
257a89c9211Schristos         ERR_print_errors(bio_err);
258a89c9211Schristos         goto end;
259a89c9211Schristos     }
260*b0d17251Schristos     if (!EVP_PKEY_is_a(pkey, "RSA") && !EVP_PKEY_is_a(pkey, "RSA-PSS")) {
261*b0d17251Schristos         BIO_printf(bio_err, "Not an RSA key\n");
262*b0d17251Schristos         goto end;
263*b0d17251Schristos     }
264a89c9211Schristos 
265c7da899bSchristos     out = bio_open_owner(outfile, outformat, private);
266c7da899bSchristos     if (out == NULL)
267a89c9211Schristos         goto end;
268a89c9211Schristos 
269c7da899bSchristos     if (text) {
270c7da899bSchristos         assert(pubin || private);
271*b0d17251Schristos         if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
272*b0d17251Schristos             || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
273a89c9211Schristos             perror(outfile);
274a89c9211Schristos             ERR_print_errors(bio_err);
275a89c9211Schristos             goto end;
276a89c9211Schristos         }
277c7da899bSchristos     }
278a89c9211Schristos 
279635165faSspz     if (modulus) {
280*b0d17251Schristos         BIGNUM *n = NULL;
281*b0d17251Schristos 
282*b0d17251Schristos         /* Every RSA key has an 'n' */
283*b0d17251Schristos         EVP_PKEY_get_bn_param(pkey, "n", &n);
284a89c9211Schristos         BIO_printf(out, "Modulus=");
285c7da899bSchristos         BN_print(out, n);
286a89c9211Schristos         BIO_printf(out, "\n");
287*b0d17251Schristos         BN_free(n);
288a89c9211Schristos     }
289a89c9211Schristos 
290635165faSspz     if (check) {
291*b0d17251Schristos         int r;
292*b0d17251Schristos 
293*b0d17251Schristos         pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
294*b0d17251Schristos         if (pctx == NULL) {
295*b0d17251Schristos             BIO_printf(bio_err, "RSA unable to create PKEY context\n");
296*b0d17251Schristos             ERR_print_errors(bio_err);
297*b0d17251Schristos             goto end;
298*b0d17251Schristos         }
299*b0d17251Schristos         r = EVP_PKEY_check(pctx);
300*b0d17251Schristos         EVP_PKEY_CTX_free(pctx);
301a89c9211Schristos 
30213d40330Schristos         if (r == 1) {
303a89c9211Schristos             BIO_printf(out, "RSA key ok\n");
30413d40330Schristos         } else if (r == 0) {
305*b0d17251Schristos             BIO_printf(bio_err, "RSA key not ok\n");
306*b0d17251Schristos             ERR_print_errors(bio_err);
307*b0d17251Schristos         } else if (r < 0) {
308a89c9211Schristos             ERR_print_errors(bio_err);
309a89c9211Schristos             goto end;
310a89c9211Schristos         }
311a89c9211Schristos     }
312a89c9211Schristos 
313635165faSspz     if (noout) {
314a89c9211Schristos         ret = 0;
315a89c9211Schristos         goto end;
316a89c9211Schristos     }
317a89c9211Schristos     BIO_printf(bio_err, "writing RSA key\n");
318f4f044c4Schristos 
319*b0d17251Schristos     /* Choose output type for the format */
320*b0d17251Schristos     if (outformat == FORMAT_ASN1) {
321*b0d17251Schristos         output_type = "DER";
322*b0d17251Schristos     } else if (outformat == FORMAT_PEM) {
323*b0d17251Schristos         output_type = "PEM";
324*b0d17251Schristos     } else if (outformat == FORMAT_MSBLOB) {
325*b0d17251Schristos         output_type = "MSBLOB";
326*b0d17251Schristos     } else if (outformat == FORMAT_PVK) {
327c7da899bSchristos         if (pubin) {
328c7da899bSchristos             BIO_printf(bio_err, "PVK form impossible with public key input\n");
329c7da899bSchristos             goto end;
330c7da899bSchristos         }
331*b0d17251Schristos         output_type = "PVK";
332a89c9211Schristos     } else {
333a89c9211Schristos         BIO_printf(bio_err, "bad output format specified for outfile\n");
334a89c9211Schristos         goto end;
335a89c9211Schristos     }
336*b0d17251Schristos 
337*b0d17251Schristos     /* Select what you want in the output */
338*b0d17251Schristos     if (pubout || pubin) {
339*b0d17251Schristos         selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
340*b0d17251Schristos     } else {
341*b0d17251Schristos         assert(private);
342*b0d17251Schristos         selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
343*b0d17251Schristos                      | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
344*b0d17251Schristos     }
345*b0d17251Schristos 
346*b0d17251Schristos     /* For DER based output, select the desired output structure */
347*b0d17251Schristos     if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
348*b0d17251Schristos         if (pubout || pubin) {
349*b0d17251Schristos             if (pubout == 2)
350*b0d17251Schristos                 output_structure = "pkcs1"; /* "type-specific" would work too */
351*b0d17251Schristos             else
352*b0d17251Schristos                 output_structure = "SubjectPublicKeyInfo";
353*b0d17251Schristos         } else {
354*b0d17251Schristos             assert(private);
355*b0d17251Schristos             if (traditional)
356*b0d17251Schristos                 output_structure = "pkcs1"; /* "type-specific" would work too */
357*b0d17251Schristos             else
358*b0d17251Schristos                 output_structure = "PrivateKeyInfo";
359*b0d17251Schristos         }
360*b0d17251Schristos     }
361*b0d17251Schristos 
362*b0d17251Schristos     /* Now, perform the encoding */
363*b0d17251Schristos     ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
364*b0d17251Schristos                                          output_type, output_structure,
365*b0d17251Schristos                                          NULL);
366*b0d17251Schristos     if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
367*b0d17251Schristos         if ((!pubout && !pubin)
368*b0d17251Schristos             || !try_legacy_encoding(pkey, outformat, pubout, out))
369*b0d17251Schristos             BIO_printf(bio_err, "%s format not supported\n", output_type);
370*b0d17251Schristos         else
371*b0d17251Schristos             ret = 0;
372*b0d17251Schristos         goto end;
373*b0d17251Schristos     }
374*b0d17251Schristos 
375*b0d17251Schristos     /* Passphrase setup */
376*b0d17251Schristos     if (enc != NULL)
377*b0d17251Schristos         OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
378*b0d17251Schristos 
379*b0d17251Schristos     /* Default passphrase prompter */
380*b0d17251Schristos     if (enc != NULL || outformat == FORMAT_PVK) {
381*b0d17251Schristos         OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
382*b0d17251Schristos         if (passout != NULL)
383*b0d17251Schristos             /* When passout given, override the passphrase prompter */
384*b0d17251Schristos             OSSL_ENCODER_CTX_set_passphrase(ectx,
385*b0d17251Schristos                                             (const unsigned char *)passout,
386*b0d17251Schristos                                             strlen(passout));
387*b0d17251Schristos     }
388*b0d17251Schristos 
389*b0d17251Schristos     /* PVK is a bit special... */
390*b0d17251Schristos     if (outformat == FORMAT_PVK) {
391*b0d17251Schristos         OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
392*b0d17251Schristos 
393*b0d17251Schristos         params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
394*b0d17251Schristos         if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
395*b0d17251Schristos             BIO_printf(bio_err, "invalid PVK encryption level\n");
396*b0d17251Schristos             goto end;
397*b0d17251Schristos         }
398*b0d17251Schristos     }
399*b0d17251Schristos 
400*b0d17251Schristos     if (!OSSL_ENCODER_to_bio(ectx, out)) {
401a89c9211Schristos         BIO_printf(bio_err, "unable to write key\n");
402a89c9211Schristos         ERR_print_errors(bio_err);
403*b0d17251Schristos         goto end;
40413d40330Schristos     }
405*b0d17251Schristos     ret = 0;
406a89c9211Schristos  end:
407*b0d17251Schristos     OSSL_ENCODER_CTX_free(ectx);
40834505c60Sspz     release_engine(e);
409635165faSspz     BIO_free_all(out);
410*b0d17251Schristos     EVP_PKEY_free(pkey);
411*b0d17251Schristos     EVP_CIPHER_free(enc);
412635165faSspz     OPENSSL_free(passin);
413635165faSspz     OPENSSL_free(passout);
41413d40330Schristos     return ret;
415a89c9211Schristos }
416