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