diff options
author | eric <> | 2021-01-26 12:51:22 +0000 |
---|---|---|
committer | eric <> | 2021-01-26 12:51:22 +0000 |
commit | 7602e79fb847dbe732cccb0b75a2591c2792d03e (patch) | |
tree | c2acdd11f57faaa054ed56bed51b54faf014d042 | |
parent | d5626382a55396240f1722a5e5fae881c70598d2 (diff) | |
download | openbsd-7602e79fb847dbe732cccb0b75a2591c2792d03e.tar.gz openbsd-7602e79fb847dbe732cccb0b75a2591c2792d03e.tar.bz2 openbsd-7602e79fb847dbe732cccb0b75a2591c2792d03e.zip |
Move private key setup to a helper function with proper error
checking. Only install the hash on the key if fake key is used,
and do it for EC keys too.
ok tb@ jsing@
-rw-r--r-- | src/lib/libtls/tls.c | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/src/lib/libtls/tls.c b/src/lib/libtls/tls.c index 5e02b5a427..f8f18b9fee 100644 --- a/src/lib/libtls/tls.c +++ b/src/lib/libtls/tls.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls.c,v 1.87 2021/01/21 22:02:17 eric Exp $ */ | 1 | /* $OpenBSD: tls.c,v 1.88 2021/01/26 12:51:22 eric Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -384,6 +384,50 @@ tls_keypair_to_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY **pke | |||
384 | return (ret); | 384 | return (ret); |
385 | } | 385 | } |
386 | 386 | ||
387 | static int | ||
388 | tls_keypair_setup_pkey(struct tls *ctx, struct tls_keypair *keypair, EVP_PKEY *pkey) | ||
389 | { | ||
390 | RSA *rsa = NULL; | ||
391 | EC_KEY *eckey = NULL; | ||
392 | int ret = -1; | ||
393 | |||
394 | /* Only install the pubkey hash if fake private keys are used. */ | ||
395 | if (!ctx->config->skip_private_key_check) | ||
396 | return (0); | ||
397 | |||
398 | if (keypair->pubkey_hash == NULL) { | ||
399 | tls_set_errorx(ctx, "public key hash not set"); | ||
400 | goto err; | ||
401 | } | ||
402 | |||
403 | switch (EVP_PKEY_id(pkey)) { | ||
404 | case EVP_PKEY_RSA: | ||
405 | if ((rsa = EVP_PKEY_get1_RSA(pkey)) == NULL || | ||
406 | RSA_set_ex_data(rsa, 0, keypair->pubkey_hash) == 0) { | ||
407 | tls_set_errorx(ctx, "failed to setup RSA key"); | ||
408 | goto err; | ||
409 | } | ||
410 | break; | ||
411 | case EVP_PKEY_EC: | ||
412 | if ((eckey = EVP_PKEY_get1_EC_KEY(pkey)) == NULL || | ||
413 | ECDSA_set_ex_data(eckey, 0, keypair->pubkey_hash) == 0) { | ||
414 | tls_set_errorx(ctx, "failed to setup EC key"); | ||
415 | goto err; | ||
416 | } | ||
417 | break; | ||
418 | default: | ||
419 | tls_set_errorx(ctx, "incorrect key type"); | ||
420 | goto err; | ||
421 | } | ||
422 | |||
423 | ret = 0; | ||
424 | |||
425 | err: | ||
426 | RSA_free(rsa); | ||
427 | EC_KEY_free(eckey); | ||
428 | return (ret); | ||
429 | } | ||
430 | |||
387 | int | 431 | int |
388 | tls_configure_ssl_keypair(struct tls *ctx, SSL_CTX *ssl_ctx, | 432 | tls_configure_ssl_keypair(struct tls *ctx, SSL_CTX *ssl_ctx, |
389 | struct tls_keypair *keypair, int required) | 433 | struct tls_keypair *keypair, int required) |
@@ -411,15 +455,8 @@ tls_configure_ssl_keypair(struct tls *ctx, SSL_CTX *ssl_ctx, | |||
411 | if (tls_keypair_to_pkey(ctx, keypair, &pkey) == -1) | 455 | if (tls_keypair_to_pkey(ctx, keypair, &pkey) == -1) |
412 | goto err; | 456 | goto err; |
413 | if (pkey != NULL) { | 457 | if (pkey != NULL) { |
414 | if (keypair->pubkey_hash != NULL) { | 458 | if (tls_keypair_setup_pkey(ctx, keypair, pkey) == -1) |
415 | RSA *rsa; | 459 | goto err; |
416 | /* XXX only RSA for now for relayd privsep */ | ||
417 | if ((rsa = EVP_PKEY_get1_RSA(pkey)) != NULL) { | ||
418 | RSA_set_ex_data(rsa, 0, keypair->pubkey_hash); | ||
419 | RSA_free(rsa); | ||
420 | } | ||
421 | } | ||
422 | |||
423 | if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1) { | 460 | if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1) { |
424 | tls_set_errorx(ctx, "failed to load private key"); | 461 | tls_set_errorx(ctx, "failed to load private key"); |
425 | goto err; | 462 | goto err; |