]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/pkeyutl.c
sha/asm/sha512p8-ppc.pl: optimize epilogue.
[thirdparty/openssl.git] / apps / pkeyutl.c
CommitLineData
0f113f3e 1/*
6738bf14 2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
9e4d0f0b 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (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
9e4d0f0b
DSH
8 */
9
9e4d0f0b 10#include "apps.h"
dab2cd68 11#include "progs.h"
9e4d0f0b
DSH
12#include <string.h>
13#include <openssl/err.h>
14#include <openssl/pem.h>
15#include <openssl/evp.h>
16
924ec89a 17#define KEY_NONE 0
0f113f3e
MC
18#define KEY_PRIVKEY 1
19#define KEY_PUBKEY 2
20#define KEY_CERT 3
9e4d0f0b 21
924ec89a 22static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
0c20802c 23 const char *keyfile, int keyform, int key_type,
9880236e
M
24 char *passinarg, int pkey_op, ENGINE *e,
25 const int impl);
ffb1ac67 26
0c20802c
VD
27static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
28 ENGINE *e);
9e4d0f0b 29
b010b7c4 30static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
0f113f3e 31 unsigned char *out, size_t *poutlen,
cc696296 32 const unsigned char *in, size_t inlen);
b010b7c4 33
7e1b7485
RS
34typedef enum OPTION_choice {
35 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
9880236e 36 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
7e1b7485
RS
37 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
38 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
39 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
3ee1eac2
RS
40 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_KDF, OPT_KDFLEN,
41 OPT_R_ENUM
7e1b7485
RS
42} OPTION_CHOICE;
43
44c83ebd 44const OPTIONS pkeyutl_options[] = {
7e1b7485 45 {"help", OPT_HELP, '-', "Display this summary"},
a173a7ee
RS
46 {"in", OPT_IN, '<', "Input file - default stdin"},
47 {"out", OPT_OUT, '>', "Output file - default stdout"},
7e1b7485
RS
48 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
49 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
0c20802c 50 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
7e1b7485 51 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
a173a7ee 52 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
7e1b7485
RS
53 {"verify", OPT_VERIFY, '-', "Verify with public key"},
54 {"verifyrecover", OPT_VERIFYRECOVER, '-',
55 "Verify with public key, recover original data"},
a173a7ee
RS
56 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
57 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
58 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
7e1b7485 59 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
924ec89a
DSH
60 {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
61 {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
7e1b7485 62 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
a173a7ee 63 {"inkey", OPT_INKEY, 's', "Input private key file"},
0c20802c 64 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
16e1b281 65 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
0c20802c
VD
66 {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
67 {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
7e1b7485 68 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
3ee1eac2 69 OPT_R_OPTIONS,
7e1b7485
RS
70#ifndef OPENSSL_NO_ENGINE
71 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
a173a7ee
RS
72 {"engine_impl", OPT_ENGINE_IMPL, '-',
73 "Also use engine given by -engine for crypto operations"},
7e1b7485
RS
74#endif
75 {NULL}
76};
9e4d0f0b 77
7e1b7485 78int pkeyutl_main(int argc, char **argv)
9e4d0f0b 79{
0f113f3e 80 BIO *in = NULL, *out = NULL;
0f113f3e 81 ENGINE *e = NULL;
0f113f3e 82 EVP_PKEY_CTX *ctx = NULL;
7e1b7485
RS
83 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
84 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
0f113f3e 85 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
7e1b7485 86 OPTION_CHOICE o;
f6add6ac 87 int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
7e1b7485 88 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
9880236e 89 int engine_impl = 0;
0f113f3e 90 int ret = 1, rv = -1;
7e1b7485 91 size_t buf_outlen;
0c20802c
VD
92 const char *inkey = NULL;
93 const char *peerkey = NULL;
924ec89a
DSH
94 const char *kdfalg = NULL;
95 int kdflen = 0;
0c20802c 96 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
0f113f3e 97
7e1b7485
RS
98 prog = opt_init(argc, argv, pkeyutl_options);
99 while ((o = opt_next()) != OPT_EOF) {
100 switch (o) {
101 case OPT_EOF:
102 case OPT_ERR:
103 opthelp:
104 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
105 goto end;
106 case OPT_HELP:
107 opt_help(pkeyutl_options);
108 ret = 0;
109 goto end;
110 case OPT_IN:
111 infile = opt_arg();
112 break;
113 case OPT_OUT:
114 outfile = opt_arg();
115 break;
116 case OPT_SIGFILE:
117 sigfile = opt_arg();
118 break;
9880236e
M
119 case OPT_ENGINE_IMPL:
120 engine_impl = 1;
121 break;
7e1b7485 122 case OPT_INKEY:
0c20802c 123 inkey = opt_arg();
7e1b7485
RS
124 break;
125 case OPT_PEERKEY:
0c20802c 126 peerkey = opt_arg();
7e1b7485
RS
127 break;
128 case OPT_PASSIN:
129 passinarg = opt_arg();
130 break;
131 case OPT_PEERFORM:
0c20802c 132 if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
7e1b7485
RS
133 goto opthelp;
134 break;
135 case OPT_KEYFORM:
0c20802c 136 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
7e1b7485
RS
137 goto opthelp;
138 break;
3ee1eac2
RS
139 case OPT_R_CASES:
140 if (!opt_rand(o))
141 goto end;
142 break;
7e1b7485
RS
143 case OPT_ENGINE:
144 e = setup_engine(opt_arg(), 0);
145 break;
7e1b7485 146 case OPT_PUBIN:
0f113f3e 147 key_type = KEY_PUBKEY;
7e1b7485
RS
148 break;
149 case OPT_CERTIN:
0f113f3e 150 key_type = KEY_CERT;
7e1b7485
RS
151 break;
152 case OPT_ASN1PARSE:
0f113f3e 153 asn1parse = 1;
7e1b7485
RS
154 break;
155 case OPT_HEXDUMP:
0f113f3e 156 hexdump = 1;
7e1b7485
RS
157 break;
158 case OPT_SIGN:
0f113f3e 159 pkey_op = EVP_PKEY_OP_SIGN;
7e1b7485
RS
160 break;
161 case OPT_VERIFY:
0f113f3e 162 pkey_op = EVP_PKEY_OP_VERIFY;
7e1b7485
RS
163 break;
164 case OPT_VERIFYRECOVER:
0f113f3e 165 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
7e1b7485 166 break;
7e1b7485 167 case OPT_ENCRYPT:
0f113f3e 168 pkey_op = EVP_PKEY_OP_ENCRYPT;
7e1b7485
RS
169 break;
170 case OPT_DECRYPT:
0f113f3e 171 pkey_op = EVP_PKEY_OP_DECRYPT;
7e1b7485
RS
172 break;
173 case OPT_DERIVE:
0f113f3e 174 pkey_op = EVP_PKEY_OP_DERIVE;
7e1b7485 175 break;
924ec89a
DSH
176 case OPT_KDF:
177 pkey_op = EVP_PKEY_OP_DERIVE;
178 key_type = KEY_NONE;
179 kdfalg = opt_arg();
180 break;
181 case OPT_KDFLEN:
182 kdflen = atoi(opt_arg());
183 break;
0c20802c
VD
184 case OPT_REV:
185 rev = 1;
186 break;
7e1b7485 187 case OPT_PKEYOPT:
0c20802c
VD
188 if ((pkeyopts == NULL &&
189 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
78524149 190 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
0c20802c 191 BIO_puts(bio_err, "out of memory\n");
0f113f3e
MC
192 goto end;
193 }
7e1b7485 194 break;
0f113f3e 195 }
0f113f3e 196 }
7e1b7485 197 argc = opt_num_rest();
03358517
KR
198 if (argc != 0)
199 goto opthelp;
0f113f3e 200
924ec89a 201 if (kdfalg != NULL) {
f6add6ac
JB
202 if (kdflen == 0) {
203 BIO_printf(bio_err,
204 "%s: no KDF length given (-kdflen parameter).\n", prog);
924ec89a 205 goto opthelp;
f6add6ac
JB
206 }
207 } else if (inkey == NULL) {
208 BIO_printf(bio_err,
209 "%s: no private key given (-inkey parameter).\n", prog);
210 goto opthelp;
211 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
212 BIO_printf(bio_err,
213 "%s: no peer key given (-peerkey parameter).\n", prog);
7e1b7485 214 goto opthelp;
924ec89a
DSH
215 }
216 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
9880236e 217 passinarg, pkey_op, e, engine_impl);
0c20802c
VD
218 if (ctx == NULL) {
219 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
220 ERR_print_errors(bio_err);
221 goto end;
222 }
223 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
224 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
225 ERR_print_errors(bio_err);
226 goto end;
227 }
228 if (pkeyopts != NULL) {
229 int num = sk_OPENSSL_STRING_num(pkeyopts);
230 int i;
231
232 for (i = 0; i < num; ++i) {
233 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
234
235 if (pkey_ctrl_string(ctx, opt) <= 0) {
f6add6ac
JB
236 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
237 prog, opt);
0c20802c
VD
238 ERR_print_errors(bio_err);
239 goto end;
240 }
241 }
242 }
243
2234212c 244 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
7e1b7485
RS
245 BIO_printf(bio_err,
246 "%s: Signature file specified for non verify\n", prog);
0f113f3e
MC
247 goto end;
248 }
249
2234212c 250 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
7e1b7485
RS
251 BIO_printf(bio_err,
252 "%s: No signature file specified for verify\n", prog);
0f113f3e
MC
253 goto end;
254 }
a9164153 255
0f113f3e 256 if (pkey_op != EVP_PKEY_OP_DERIVE) {
bdd58d98 257 in = bio_open_default(infile, 'r', FORMAT_BINARY);
7e1b7485 258 if (in == NULL)
0f113f3e 259 goto end;
0f113f3e 260 }
bdd58d98 261 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
7e1b7485
RS
262 if (out == NULL)
263 goto end;
0f113f3e 264
2234212c 265 if (sigfile != NULL) {
0f113f3e 266 BIO *sigbio = BIO_new_file(sigfile, "rb");
2234212c
PY
267
268 if (sigbio == NULL) {
0f113f3e
MC
269 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
270 goto end;
271 }
272 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
273 BIO_free(sigbio);
0c20802c 274 if (siglen < 0) {
0f113f3e
MC
275 BIO_printf(bio_err, "Error reading signature data\n");
276 goto end;
277 }
278 }
279
2234212c 280 if (in != NULL) {
0f113f3e
MC
281 /* Read the input data */
282 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
0c20802c 283 if (buf_inlen < 0) {
0f113f3e 284 BIO_printf(bio_err, "Error reading input Data\n");
a0abb6a1 285 goto end;
0f113f3e
MC
286 }
287 if (rev) {
288 size_t i;
289 unsigned char ctmp;
290 size_t l = (size_t)buf_inlen;
291 for (i = 0; i < l / 2; i++) {
292 ctmp = buf_in[i];
293 buf_in[i] = buf_in[l - 1 - i];
294 buf_in[l - 1 - i] = ctmp;
295 }
296 }
297 }
298
a0abb6a1
MC
299 /* Sanity check the input */
300 if (buf_inlen > EVP_MAX_MD_SIZE
301 && (pkey_op == EVP_PKEY_OP_SIGN
302 || pkey_op == EVP_PKEY_OP_VERIFY
303 || pkey_op == EVP_PKEY_OP_VERIFYRECOVER)) {
304 BIO_printf(bio_err,
305 "Error: The input data looks too long to be a hash\n");
306 goto end;
307 }
308
0f113f3e
MC
309 if (pkey_op == EVP_PKEY_OP_VERIFY) {
310 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
311 buf_in, (size_t)buf_inlen);
7e1b7485 312 if (rv == 1) {
0f113f3e 313 BIO_puts(out, "Signature Verified Successfully\n");
7e1b7485 314 ret = 0;
2234212c 315 } else {
7e1b7485 316 BIO_puts(out, "Signature Verification Failure\n");
2234212c 317 }
7e1b7485
RS
318 goto end;
319 }
924ec89a
DSH
320 if (kdflen != 0) {
321 buf_outlen = kdflen;
322 rv = 1;
323 } else {
324 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
325 buf_in, (size_t)buf_inlen);
326 }
0c20802c 327 if (rv > 0 && buf_outlen != 0) {
68dc6824
RS
328 buf_out = app_malloc(buf_outlen, "buffer output");
329 rv = do_keyop(ctx, pkey_op,
330 buf_out, (size_t *)&buf_outlen,
331 buf_in, (size_t)buf_inlen);
0f113f3e 332 }
78524149 333 if (rv <= 0) {
f6add6ac
JB
334 if (pkey_op != EVP_PKEY_OP_DERIVE) {
335 BIO_puts(bio_err, "Public Key operation error\n");
336 } else {
337 BIO_puts(bio_err, "Key derivation failed\n");
338 }
0f113f3e
MC
339 ERR_print_errors(bio_err);
340 goto end;
341 }
342 ret = 0;
7e1b7485 343
0f113f3e
MC
344 if (asn1parse) {
345 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
346 ERR_print_errors(bio_err);
2234212c 347 } else if (hexdump) {
0f113f3e 348 BIO_dump(out, (char *)buf_out, buf_outlen);
2234212c 349 } else {
0f113f3e 350 BIO_write(out, buf_out, buf_outlen);
2234212c 351 }
0f113f3e
MC
352
353 end:
c5ba2d99 354 EVP_PKEY_CTX_free(ctx);
dd1abd44 355 release_engine(e);
0f113f3e
MC
356 BIO_free(in);
357 BIO_free_all(out);
b548a1f1
RS
358 OPENSSL_free(buf_in);
359 OPENSSL_free(buf_out);
360 OPENSSL_free(sig);
0c20802c 361 sk_OPENSSL_STRING_free(pkeyopts);
0f113f3e 362 return ret;
9e4d0f0b
DSH
363}
364
924ec89a 365static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
0c20802c 366 const char *keyfile, int keyform, int key_type,
9880236e
M
367 char *passinarg, int pkey_op, ENGINE *e,
368 const int engine_impl)
0f113f3e
MC
369{
370 EVP_PKEY *pkey = NULL;
371 EVP_PKEY_CTX *ctx = NULL;
9880236e 372 ENGINE *impl = NULL;
0f113f3e
MC
373 char *passin = NULL;
374 int rv = -1;
375 X509 *x;
376 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
377 || (pkey_op == EVP_PKEY_OP_DERIVE))
924ec89a 378 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
0f113f3e
MC
379 BIO_printf(bio_err, "A private key is needed for this operation\n");
380 goto end;
381 }
7e1b7485 382 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
0f113f3e
MC
383 BIO_printf(bio_err, "Error getting password\n");
384 goto end;
385 }
386 switch (key_type) {
387 case KEY_PRIVKEY:
7e1b7485 388 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
0f113f3e
MC
389 break;
390
391 case KEY_PUBKEY:
7e1b7485 392 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
0f113f3e
MC
393 break;
394
395 case KEY_CERT:
a773b52a 396 x = load_cert(keyfile, keyform, "Certificate");
0f113f3e
MC
397 if (x) {
398 pkey = X509_get_pubkey(x);
399 X509_free(x);
400 }
401 break;
402
924ec89a
DSH
403 case KEY_NONE:
404 break;
0f113f3e 405
924ec89a 406 }
0f113f3e 407
9880236e
M
408#ifndef OPENSSL_NO_ENGINE
409 if (engine_impl)
410 impl = e;
411#endif
0f113f3e 412
2234212c 413 if (kdfalg != NULL) {
924ec89a 414 int kdfnid = OBJ_sn2nid(kdfalg);
b15d5ab6
DSH
415
416 if (kdfnid == NID_undef) {
417 kdfnid = OBJ_ln2nid(kdfalg);
f6add6ac
JB
418 if (kdfnid == NID_undef) {
419 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
420 kdfalg);
b15d5ab6 421 goto end;
f6add6ac 422 }
b15d5ab6 423 }
924ec89a
DSH
424 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
425 } else {
426 if (pkey == NULL)
427 goto end;
428 *pkeysize = EVP_PKEY_size(pkey);
429 ctx = EVP_PKEY_CTX_new(pkey, impl);
430 EVP_PKEY_free(pkey);
431 }
0f113f3e 432
96487cdd 433 if (ctx == NULL)
0f113f3e
MC
434 goto end;
435
436 switch (pkey_op) {
437 case EVP_PKEY_OP_SIGN:
438 rv = EVP_PKEY_sign_init(ctx);
439 break;
440
441 case EVP_PKEY_OP_VERIFY:
442 rv = EVP_PKEY_verify_init(ctx);
443 break;
444
445 case EVP_PKEY_OP_VERIFYRECOVER:
446 rv = EVP_PKEY_verify_recover_init(ctx);
447 break;
448
449 case EVP_PKEY_OP_ENCRYPT:
450 rv = EVP_PKEY_encrypt_init(ctx);
451 break;
452
453 case EVP_PKEY_OP_DECRYPT:
454 rv = EVP_PKEY_decrypt_init(ctx);
455 break;
456
457 case EVP_PKEY_OP_DERIVE:
458 rv = EVP_PKEY_derive_init(ctx);
459 break;
460 }
461
462 if (rv <= 0) {
463 EVP_PKEY_CTX_free(ctx);
464 ctx = NULL;
465 }
466
467 end:
b548a1f1 468 OPENSSL_free(passin);
0f113f3e
MC
469 return ctx;
470
471}
9e4d0f0b 472
0c20802c 473static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
f6add6ac 474 ENGINE *e)
0f113f3e
MC
475{
476 EVP_PKEY *peer = NULL;
f6add6ac 477 ENGINE *engine = NULL;
0f113f3e 478 int ret;
0f113f3e 479
0c20802c
VD
480 if (peerform == FORMAT_ENGINE)
481 engine = e;
482 peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
2234212c 483 if (peer == NULL) {
0f113f3e 484 BIO_printf(bio_err, "Error reading peer key %s\n", file);
7e1b7485 485 ERR_print_errors(bio_err);
0f113f3e
MC
486 return 0;
487 }
488
489 ret = EVP_PKEY_derive_set_peer(ctx, peer);
490
491 EVP_PKEY_free(peer);
492 if (ret <= 0)
7e1b7485 493 ERR_print_errors(bio_err);
0f113f3e
MC
494 return ret;
495}
b010b7c4
DSH
496
497static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
0f113f3e 498 unsigned char *out, size_t *poutlen,
cc696296 499 const unsigned char *in, size_t inlen)
0f113f3e
MC
500{
501 int rv = 0;
502 switch (pkey_op) {
503 case EVP_PKEY_OP_VERIFYRECOVER:
504 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
505 break;
506
507 case EVP_PKEY_OP_SIGN:
508 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
509 break;
510
511 case EVP_PKEY_OP_ENCRYPT:
512 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
513 break;
514
515 case EVP_PKEY_OP_DECRYPT:
516 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
517 break;
518
519 case EVP_PKEY_OP_DERIVE:
520 rv = EVP_PKEY_derive(ctx, out, poutlen);
521 break;
522
523 }
524 return rv;
525}