Use EVP_PKEY_dup instead of manually incrementing the refcount.

Reference counting should be internal to the type, otherwise callers need to
know which lock to use.

Change-Id: If4d805876a321ef6dece115c805e605584ff311e
Reviewed-on: https://boringssl-review.googlesource.com/1414
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2014-08-05 21:46:37 -04:00 committed by Adam Langley
parent bf681a40d6
commit e6e15fc3a1
3 changed files with 8 additions and 14 deletions

View File

@ -119,8 +119,7 @@ static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) {
ret->operation = EVP_PKEY_OP_UNDEFINED; ret->operation = EVP_PKEY_OP_UNDEFINED;
if (pkey) { if (pkey) {
ret->pkey = pkey; ret->pkey = EVP_PKEY_dup(pkey);
CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
} }
if (pmeth->init) { if (pmeth->init) {
@ -176,14 +175,12 @@ EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx) {
rctx->operation = pctx->operation; rctx->operation = pctx->operation;
if (pctx->pkey) { if (pctx->pkey) {
CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); rctx->pkey = EVP_PKEY_dup(pctx->pkey);
} }
rctx->pkey = pctx->pkey;
if (pctx->peerkey) { if (pctx->peerkey) {
CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY); rctx->peerkey = EVP_PKEY_dup(pctx->peerkey);
} }
rctx->peerkey = pctx->peerkey;
if (pctx->pmeth->copy(rctx, pctx) > 0) { if (pctx->pmeth->copy(rctx, pctx) > 0) {
return rctx; return rctx;
@ -437,7 +434,7 @@ int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) {
return 0; return 0;
} }
CRYPTO_add(&peer->references, 1, CRYPTO_LOCK_EVP_PKEY); EVP_PKEY_dup(peer);
return 1; return 1;
} }

View File

@ -133,8 +133,7 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
if (key->pkey != NULL) if (key->pkey != NULL)
{ {
CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); return EVP_PKEY_dup(key->pkey);
return key->pkey;
} }
if (key->public_key == NULL) goto error; if (key->public_key == NULL) goto error;
@ -178,9 +177,8 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
key->pkey = ret; key->pkey = ret;
CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
} }
CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);
return ret; return EVP_PKEY_dup(ret);
error: error:
if (ret != NULL) if (ret != NULL)

View File

@ -226,9 +226,8 @@ static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
if (c->pkeys[i].privatekey != NULL) if (c->pkeys[i].privatekey != NULL)
EVP_PKEY_free(c->pkeys[i].privatekey); EVP_PKEY_free(c->pkeys[i].privatekey);
CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); c->pkeys[i].privatekey = EVP_PKEY_dup(pkey);
c->pkeys[i].privatekey=pkey; c->key = &(c->pkeys[i]);
c->key= &(c->pkeys[i]);
c->valid=0; c->valid=0;
return(1); return(1);