diff options
Diffstat (limited to 'lib/dns/opensslrsa_link.c')
| -rw-r--r-- | lib/dns/opensslrsa_link.c | 474 |
1 files changed, 415 insertions, 59 deletions
diff --git a/lib/dns/opensslrsa_link.c b/lib/dns/opensslrsa_link.c index fa426b38..aee7d21a 100644 --- a/lib/dns/opensslrsa_link.c +++ b/lib/dns/opensslrsa_link.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC") + * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 2000-2003 Internet Software Consortium. * * Permission to use, copy, modify, and/or distribute this software for any @@ -17,9 +17,15 @@ /* * Principal Author: Brian Wellington - * $Id: opensslrsa_link.c,v 1.16 2007/06/19 23:47:16 tbox Exp $ + * $Id: opensslrsa_link.c,v 1.20 2008/09/24 02:46:22 marka Exp $ */ #ifdef OPENSSL +#ifndef USE_EVP +#define USE_EVP 1 +#endif +#if USE_EVP +#define USE_EVP_RSA 1 +#endif #include <config.h> @@ -42,6 +48,7 @@ #if OPENSSL_VERSION_NUMBER > 0x00908000L #include <openssl/bn.h> #endif +#include <openssl/engine.h> /* * We don't use configure for windows so enforce the OpenSSL version @@ -50,7 +57,7 @@ #ifdef WIN32 #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \ OPENSSL_VERSION_NUMBER < 0x00908000L) || \ - OPENSSL_VERSION_NUMBER >= 0x0090804fL) + OPENSSL_VERSION_NUMBER >= 0x0090804fL) #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater. #endif #endif @@ -97,14 +104,38 @@ } while (0) #endif +#define DST_RET(a) {ret = a; goto err;} + static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data); static isc_result_t opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) { +#if USE_EVP + EVP_MD_CTX *evp_md_ctx; + const EVP_MD *type; +#endif + UNUSED(key); REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 || - dctx->key->key_alg == DST_ALG_RSASHA1); + dctx->key->key_alg == DST_ALG_RSASHA1 || + dctx->key->key_alg == DST_ALG_NSEC3RSASHA1); +#if USE_EVP + evp_md_ctx = EVP_MD_CTX_create(); + if (evp_md_ctx == NULL) + return (ISC_R_NOMEMORY); + + if (dctx->key->key_alg == DST_ALG_RSAMD5) + type = EVP_md5(); /* MD5 + RSA */ + else + type = EVP_sha1(); /* SHA1 + RSA */ + + if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) { + EVP_MD_CTX_destroy(evp_md_ctx); + return (ISC_R_FAILURE); + } + dctx->ctxdata.evp_md_ctx = evp_md_ctx; +#else if (dctx->key->key_alg == DST_ALG_RSAMD5) { isc_md5_t *md5ctx; @@ -122,15 +153,27 @@ opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) { isc_sha1_init(sha1ctx); dctx->ctxdata.sha1ctx = sha1ctx; } +#endif return (ISC_R_SUCCESS); } static void opensslrsa_destroyctx(dst_context_t *dctx) { +#if USE_EVP + EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx; +#endif + REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 || - dctx->key->key_alg == DST_ALG_RSASHA1); + dctx->key->key_alg == DST_ALG_RSASHA1 || + dctx->key->key_alg == DST_ALG_NSEC3RSASHA1); +#if USE_EVP + if (evp_md_ctx != NULL) { + EVP_MD_CTX_destroy(evp_md_ctx); + dctx->ctxdata.evp_md_ctx = NULL; + } +#else if (dctx->key->key_alg == DST_ALG_RSAMD5) { isc_md5_t *md5ctx = dctx->ctxdata.md5ctx; @@ -148,13 +191,24 @@ opensslrsa_destroyctx(dst_context_t *dctx) { dctx->ctxdata.sha1ctx = NULL; } } +#endif } static isc_result_t opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) { +#if USE_EVP + EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx; +#endif + REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 || - dctx->key->key_alg == DST_ALG_RSASHA1); + dctx->key->key_alg == DST_ALG_RSASHA1 || + dctx->key->key_alg == DST_ALG_NSEC3RSASHA1); +#if USE_EVP + if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) { + return (ISC_R_FAILURE); + } +#else if (dctx->key->key_alg == DST_ALG_RSAMD5) { isc_md5_t *md5ctx = dctx->ctxdata.md5ctx; isc_md5_update(md5ctx, data->base, data->length); @@ -162,17 +216,22 @@ opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) { isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx; isc_sha1_update(sha1ctx, data->base, data->length); } +#endif return (ISC_R_SUCCESS); } static isc_result_t opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) { dst_key_t *key = dctx->key; - RSA *rsa = key->keydata.rsa; isc_region_t r; + unsigned int siglen = 0; +#if USE_EVP + EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx; + EVP_PKEY *pkey = key->keydata.pkey; +#else + RSA *rsa = key->keydata.rsa; /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */ unsigned char digest[ISC_SHA1_DIGESTLENGTH]; - unsigned int siglen = 0; int status; int type; unsigned int digestlen; @@ -180,12 +239,22 @@ opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) { unsigned long err; const char* file; int line; +#endif REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 || - dctx->key->key_alg == DST_ALG_RSASHA1); + dctx->key->key_alg == DST_ALG_RSASHA1 || + dctx->key->key_alg == DST_ALG_NSEC3RSASHA1); isc_buffer_availableregion(sig, &r); +#if USE_EVP + if (r.length < (unsigned int) EVP_PKEY_size(pkey)) + return (ISC_R_NOSPACE); + + if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) { + return (ISC_R_FAILURE); + } +#else if (r.length < (unsigned int) RSA_size(rsa)) return (ISC_R_NOSPACE); @@ -206,11 +275,10 @@ opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) { err = ERR_peek_error_line(&file, &line); if (err != 0U) { message = ERR_error_string(err, NULL); - fprintf(stderr, "%s:%s:%d\n", message, - file ? file : "", line); } return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); } +#endif isc_buffer_add(sig, siglen); @@ -220,16 +288,25 @@ opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) { static isc_result_t opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) { dst_key_t *key = dctx->key; - RSA *rsa = key->keydata.rsa; + int status = 0; +#if USE_EVP + EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx; + EVP_PKEY *pkey = key->keydata.pkey; +#else /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */ unsigned char digest[ISC_SHA1_DIGESTLENGTH]; - int status = 0; int type; unsigned int digestlen; + RSA *rsa = key->keydata.rsa; +#endif REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 || - dctx->key->key_alg == DST_ALG_RSASHA1); + dctx->key->key_alg == DST_ALG_RSASHA1 || + dctx->key->key_alg == DST_ALG_NSEC3RSASHA1); +#if USE_EVP + status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey); +#else if (dctx->key->key_alg == DST_ALG_RSAMD5) { isc_md5_t *md5ctx = dctx->ctxdata.md5ctx; isc_md5_final(md5ctx, digest); @@ -247,6 +324,7 @@ opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) { status = RSA_verify(type, digest, digestlen, sig->base, RSA_size(rsa), rsa); +#endif if (status == 0) return (dst__openssl_toresult(DST_R_VERIFYFAILURE)); @@ -256,10 +334,30 @@ opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) { static isc_boolean_t opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) { int status; - RSA *rsa1, *rsa2; + RSA *rsa1 = NULL, *rsa2 = NULL; +#if USE_EVP + EVP_PKEY *pkey1, *pkey2; +#endif +#if USE_EVP + pkey1 = key1->keydata.pkey; + pkey2 = key2->keydata.pkey; + /* + * The pkey reference will keep these around after + * the RSA_free() call. + */ + if (pkey1 != NULL) { + rsa1 = EVP_PKEY_get1_RSA(pkey1); + RSA_free(rsa1); + } + if (pkey2 != NULL) { + rsa2 = EVP_PKEY_get1_RSA(pkey2); + RSA_free(rsa2); + } +#else rsa1 = key1->keydata.rsa; rsa2 = key2->keydata.rsa; +#endif if (rsa1 == NULL && rsa2 == NULL) return (ISC_TRUE); @@ -272,6 +370,19 @@ opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) { if (status != 0) return (ISC_FALSE); +#if USE_EVP + if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 || + (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) { + if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 || + (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0) + return (ISC_FALSE); + /* + * Can't compare private parameters, BTW does it make sense? + */ + return (ISC_TRUE); + } +#endif + if (rsa1->d != NULL || rsa2->d != NULL) { if (rsa1->d == NULL || rsa2->d == NULL) return (ISC_FALSE); @@ -291,9 +402,18 @@ opensslrsa_generate(dst_key_t *key, int exp) { BN_GENCB cb; RSA *rsa = RSA_new(); BIGNUM *e = BN_new(); +#if USE_EVP + EVP_PKEY *pkey = EVP_PKEY_new(); +#endif if (rsa == NULL || e == NULL) goto err; +#if USE_EVP + if (pkey == NULL) + goto err; + if (!EVP_PKEY_set1_RSA(pkey, rsa)) + goto err; +#endif if (exp == 0) { /* RSA_F4 0x10001 */ @@ -310,11 +430,21 @@ opensslrsa_generate(dst_key_t *key, int exp) { if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) { BN_free(e); SET_FLAGS(rsa); +#if USE_EVP + key->keydata.pkey = pkey; + + RSA_free(rsa); +#else key->keydata.rsa = rsa; +#endif return (ISC_R_SUCCESS); } err: +#if USE_EVP + if (pkey != NULL) + EVP_PKEY_free(pkey); +#endif if (e != NULL) BN_free(e); if (rsa != NULL) @@ -323,16 +453,36 @@ err: #else RSA *rsa; unsigned long e; +#if USE_EVP + EVP_PKEY *pkey = EVP_PKEY_new(); + + if (pkey == NULL) + return (ISC_R_NOMEMORY); +#endif if (exp == 0) e = RSA_F4; else e = 0x40000003; rsa = RSA_generate_key(key->key_size, e, NULL, NULL); - if (rsa == NULL) - return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); + if (rsa == NULL) { +#if USE_EVP + EVP_PKEY_free(pkey); +#endif + return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); + } SET_FLAGS(rsa); +#if USE_EVP + if (!EVP_PKEY_set1_RSA(pkey, rsa)) { + EVP_PKEY_free(pkey); + RSA_free(rsa); + return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); + } + key->keydata.pkey = pkey; + RSA_free(rsa); +#else key->keydata.rsa = rsa; +#endif return (ISC_R_SUCCESS); #endif @@ -340,28 +490,58 @@ err: static isc_boolean_t opensslrsa_isprivate(const dst_key_t *key) { - RSA *rsa = (RSA *) key->keydata.rsa; +#if USE_EVP + RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey); + INSIST(rsa != NULL); + RSA_free(rsa); + /* key->keydata.pkey still has a reference so rsa is still valid. */ +#else + RSA *rsa = key->keydata.rsa; +#endif + if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0) + return (ISC_TRUE); return (ISC_TF(rsa != NULL && rsa->d != NULL)); } static void opensslrsa_destroy(dst_key_t *key) { +#if USE_EVP + EVP_PKEY *pkey = key->keydata.pkey; + EVP_PKEY_free(pkey); + key->keydata.pkey = NULL; +#else RSA *rsa = key->keydata.rsa; RSA_free(rsa); key->keydata.rsa = NULL; +#endif } static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) { - RSA *rsa; isc_region_t r; unsigned int e_bytes; unsigned int mod_bytes; + isc_result_t ret; + RSA *rsa; +#if USE_EVP + EVP_PKEY *pkey; +#endif +#if USE_EVP + REQUIRE(key->keydata.pkey != NULL); +#else REQUIRE(key->keydata.rsa != NULL); +#endif +#if USE_EVP + pkey = key->keydata.pkey; + rsa = EVP_PKEY_get1_RSA(pkey); + if (rsa == NULL) + return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); +#else rsa = key->keydata.rsa; +#endif isc_buffer_availableregion(data, &r); @@ -370,11 +550,11 @@ opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) { if (e_bytes < 256) { /*%< key exponent is <= 2040 bits */ if (r.length < 1) - return (ISC_R_NOSPACE); + DST_RET(ISC_R_NOSPACE); isc_buffer_putuint8(data, (isc_uint8_t) e_bytes); } else { if (r.length < 3) - return (ISC_R_NOSPACE); + DST_RET(ISC_R_NOSPACE); isc_buffer_putuint8(data, 0); isc_buffer_putuint16(data, (isc_uint16_t) e_bytes); } @@ -389,7 +569,13 @@ opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) { isc_buffer_add(data, e_bytes + mod_bytes); - return (ISC_R_SUCCESS); + ret = ISC_R_SUCCESS; + err: +#if USE_EVP + if (rsa != NULL) + RSA_free(rsa); +#endif + return (ret); } static isc_result_t @@ -397,6 +583,9 @@ opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) { RSA *rsa; isc_region_t r; unsigned int e_bytes; +#if USE_EVP + EVP_PKEY *pkey; +#endif isc_buffer_remainingregion(data, &r); if (r.length == 0) @@ -438,12 +627,26 @@ opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) { isc_buffer_forward(data, r.length); +#if USE_EVP + pkey = EVP_PKEY_new(); + if (pkey == NULL) { + RSA_free(rsa); + return (ISC_R_NOMEMORY); + } + if (!EVP_PKEY_set1_RSA(pkey, rsa)) { + EVP_PKEY_free(pkey); + RSA_free(rsa); + return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); + } + key->keydata.pkey = pkey; + RSA_free(rsa); +#else key->keydata.rsa = rsa; +#endif return (ISC_R_SUCCESS); } - static isc_result_t opensslrsa_tofile(const dst_key_t *key, const char *directory) { int i; @@ -452,10 +655,17 @@ opensslrsa_tofile(const dst_key_t *key, const char *directory) { unsigned char *bufs[8]; isc_result_t result; +#if USE_EVP + if (key->keydata.pkey == NULL) + return (DST_R_NULLKEY); + rsa = EVP_PKEY_get1_RSA(key->keydata.pkey); + if (rsa == NULL) + return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); +#else if (key->keydata.rsa == NULL) return (DST_R_NULLKEY); - rsa = key->keydata.rsa; +#endif for (i = 0; i < 8; i++) { bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n)); @@ -479,45 +689,74 @@ opensslrsa_tofile(const dst_key_t *key, const char *directory) { priv.elements[i].data = bufs[i]; i++; - priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT; - priv.elements[i].length = BN_num_bytes(rsa->d); - BN_bn2bin(rsa->d, bufs[i]); - priv.elements[i].data = bufs[i]; - i++; + if (rsa->d != NULL) { + priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT; + priv.elements[i].length = BN_num_bytes(rsa->d); + BN_bn2bin(rsa->d, bufs[i]); + priv.elements[i].data = bufs[i]; + i++; + } - priv.elements[i].tag = TAG_RSA_PRIME1; - priv.elements[i].length = BN_num_bytes(rsa->p); - BN_bn2bin(rsa->p, bufs[i]); - priv.elements[i].data = bufs[i]; - i++; + if (rsa->p != NULL) { + priv.elements[i].tag = TAG_RSA_PRIME1; + priv.elements[i].length = BN_num_bytes(rsa->p); + BN_bn2bin(rsa->p, bufs[i]); + priv.elements[i].data = bufs[i]; + i++; + } - priv.elements[i].tag = TAG_RSA_PRIME2; - priv.elements[i].length = BN_num_bytes(rsa->q); - BN_bn2bin(rsa->q, bufs[i]); - priv.elements[i].data = bufs[i]; - i++; + if (rsa->q != NULL) { + priv.elements[i].tag = TAG_RSA_PRIME2; + priv.elements[i].length = BN_num_bytes(rsa->q); + BN_bn2bin(rsa->q, bufs[i]); + priv.elements[i].data = bufs[i]; + i++; + } - priv.elements[i].tag = TAG_RSA_EXPONENT1; - priv.elements[i].length = BN_num_bytes(rsa->dmp1); - BN_bn2bin(rsa->dmp1, bufs[i]); - priv.elements[i].data = bufs[i]; - i++; + if (rsa->dmp1 != NULL) { + priv.elements[i].tag = TAG_RSA_EXPONENT1; + priv.elements[i].length = BN_num_bytes(rsa->dmp1); + BN_bn2bin(rsa->dmp1, bufs[i]); + priv.elements[i].data = bufs[i]; + i++; + } - priv.elements[i].tag = TAG_RSA_EXPONENT2; - priv.elements[i].length = BN_num_bytes(rsa->dmq1); - BN_bn2bin(rsa->dmq1, bufs[i]); - priv.elements[i].data = bufs[i]; - i++; + if (rsa->dmq1 != NULL) { + priv.elements[i].tag = TAG_RSA_EXPONENT2; + priv.elements[i].length = BN_num_bytes(rsa->dmq1); + BN_bn2bin(rsa->dmq1, bufs[i]); + priv.elements[i].data = bufs[i]; + i++; + } - priv.elements[i].tag = TAG_RSA_COEFFICIENT; - priv.elements[i].length = BN_num_bytes(rsa->iqmp); - BN_bn2bin(rsa->iqmp, bufs[i]); - priv.elements[i].data = bufs[i]; - i++; + if (rsa->iqmp != NULL) { + priv.elements[i].tag = TAG_RSA_COEFFICIENT; + priv.elements[i].length = BN_num_bytes(rsa->iqmp); + BN_bn2bin(rsa->iqmp, bufs[i]); + priv.elements[i].data = bufs[i]; + i++; + } + + if (key->engine != NULL) { + priv.elements[i].tag = TAG_RSA_ENGINE; + priv.elements[i].length = strlen(key->engine) + 1; + priv.elements[i].data = (unsigned char *)key->engine; + i++; + } + + if (key->label != NULL) { + priv.elements[i].tag = TAG_RSA_LABEL; + priv.elements[i].length = strlen(key->label) + 1; + priv.elements[i].data = (unsigned char *)key->label; + i++; + } priv.nelements = i; result = dst__privstruct_writefile(key, &priv, directory); fail: +#if USE_EVP + RSA_free(rsa); +#endif for (i = 0; i < 8; i++) { if (bufs[i] == NULL) break; @@ -532,26 +771,94 @@ opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer) { isc_result_t ret; int i; RSA *rsa = NULL; + ENGINE *e = NULL; isc_mem_t *mctx = key->mctx; -#define DST_RET(a) {ret = a; goto err;} + const char *name = NULL, *label = NULL; + EVP_PKEY *pkey = NULL; /* read private key file */ ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv); if (ret != ISC_R_SUCCESS) return (ret); + for (i = 0; i < priv.nelements; i++) { + switch (priv.elements[i].tag) { + case TAG_RSA_ENGINE: + name = (char *)priv.elements[i].data; + break; + case TAG_RSA_LABEL: + label = (char *)priv.elements[i].data; + break; + default: + break; + } + } + /* + * Is this key is stored in a HSM? + * See if we can fetch it. + */ + if (name != NULL || label != NULL) { + INSIST(name != NULL); + INSIST(label != NULL); + e = dst__openssl_getengine(name); + if (e == NULL) + DST_RET(DST_R_NOENGINE); + pkey = ENGINE_load_private_key(e, label, NULL, NULL); + if (pkey == NULL) { + ERR_print_errors_fp(stderr); + DST_RET(ISC_R_FAILURE); + } + key->engine = isc_mem_strdup(key->mctx, name); + if (key->engine == NULL) + DST_RET(ISC_R_NOMEMORY); + key->label = isc_mem_strdup(key->mctx, label); + if (key->label == NULL) + DST_RET(ISC_R_NOMEMORY); + key->key_size = EVP_PKEY_bits(pkey); +#if USE_EVP + key->keydata.pkey = pkey; +#else + key->keydata.rsa = EVP_PKEY_get1_RSA(pkey); + if (rsa == NULL) + DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE)); + EVP_PKEY_free(pkey); +#endif + dst__privstruct_free(&priv, mctx); + return (ISC_R_SUCCESS); + } + rsa = RSA_new(); if (rsa == NULL) DST_RET(ISC_R_NOMEMORY); SET_FLAGS(rsa); + +#if USE_EVP + pkey = EVP_PKEY_new(); + if (pkey == NULL) + DST_RET(ISC_R_NOMEMORY); + if (!EVP_PKEY_set1_RSA(pkey, rsa)) { + DST_RET(ISC_R_FAILURE); + } + key->keydata.pkey = pkey; +#else key->keydata.rsa = rsa; +#endif for (i = 0; i < priv.nelements; i++) { BIGNUM *bn; - bn = BN_bin2bn(priv.elements[i].data, - priv.elements[i].length, NULL); - if (bn == NULL) - DST_RET(ISC_R_NOMEMORY); + switch (priv.elements[i].tag) { + case TAG_RSA_ENGINE: + continue; + case TAG_RSA_LABEL: + continue; + case TAG_RSA_PIN: + continue; + default: + bn = BN_bin2bn(priv.elements[i].data, + priv.elements[i].length, NULL); + if (bn == NULL) + DST_RET(ISC_R_NOMEMORY); + } switch (priv.elements[i].tag) { case TAG_RSA_MODULUS: @@ -583,16 +890,64 @@ opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer) { dst__privstruct_free(&priv, mctx); key->key_size = BN_num_bits(rsa->n); +#if USE_EVP + RSA_free(rsa); +#endif return (ISC_R_SUCCESS); err: +#if USE_EVP + if (pkey != NULL) + EVP_PKEY_free(pkey); +#endif + if (rsa != NULL) + RSA_free(rsa); opensslrsa_destroy(key); dst__privstruct_free(&priv, mctx); memset(&priv, 0, sizeof(priv)); return (ret); } +static isc_result_t +opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label, + const char *pin) +{ + ENGINE *e = NULL; + isc_result_t ret; + EVP_PKEY *pkey = NULL; + + UNUSED(pin); + + e = dst__openssl_getengine(engine); + if (e == NULL) + DST_RET(DST_R_NOENGINE); + pkey = ENGINE_load_private_key(e, label, NULL, NULL); + if (pkey == NULL) + DST_RET(ISC_R_NOMEMORY); + key->engine = isc_mem_strdup(key->mctx, label); + if (key->engine == NULL) + DST_RET(ISC_R_NOMEMORY); + key->label = isc_mem_strdup(key->mctx, label); + if (key->label == NULL) + DST_RET(ISC_R_NOMEMORY); + key->key_size = EVP_PKEY_bits(pkey); +#if USE_EVP + key->keydata.pkey = pkey; +#else + key->keydata.rsa = EVP_PKEY_get1_RSA(pkey); + EVP_PKEY_free(pkey); + if (key->keydata.rsa == NULL) + return (dst__openssl_toresult(DST_R_OPENSSLFAILURE)); +#endif + return (ISC_R_SUCCESS); + + err: + if (pkey != NULL) + EVP_PKEY_free(pkey); + return (ret); +} + static dst_func_t opensslrsa_functions = { opensslrsa_createctx, opensslrsa_destroyctx, @@ -610,6 +965,7 @@ static dst_func_t opensslrsa_functions = { opensslrsa_tofile, opensslrsa_parse, NULL, /*%< cleanup */ + opensslrsa_fromlabel, }; isc_result_t |
