summaryrefslogtreecommitdiff
path: root/platform/src/key_keeper.cpp
diff options
context:
space:
mode:
authorzhengchao <[email protected]>2018-11-29 19:02:07 +0800
committerzhengchao <[email protected]>2018-11-29 19:02:07 +0800
commita5ca97d91edb7aedc39417f5c901affbf509cdea (patch)
tree4f0724a4d738eef1322e301990ed7aaf2ae04771 /platform/src/key_keeper.cpp
parentcc5420d15841f0e406173fcc85a7eb788312d864 (diff)
重构key keeper创建keyring的代码。修复除 #97 外的valgrind definitely lost。v3.0.5-20181130
Diffstat (limited to 'platform/src/key_keeper.cpp')
-rw-r--r--platform/src/key_keeper.cpp112
1 files changed, 21 insertions, 91 deletions
diff --git a/platform/src/key_keeper.cpp b/platform/src/key_keeper.cpp
index 8804c27..70ec421 100644
--- a/platform/src/key_keeper.cpp
+++ b/platform/src/key_keeper.cpp
@@ -73,17 +73,13 @@ static void key_keeper_promise_free_ctx(void* ctx)
free(_ctx);
}
-static struct keyring_private* keyring_new(void)
+static struct keyring_private* keyring_new(X509 *cert, EVP_PKEY *key, STACK_OF(X509) *chain)
{
- struct keyring_private *kyr;
- if (!(kyr = (struct keyring_private *)ALLOC(struct keyring_private, 1)))
- {
- return NULL;
- }
- if (pthread_mutex_init(&kyr->mutex, NULL)) {
- free(kyr);
- return NULL;
- }
+ struct keyring_private *kyr=ALLOC(struct keyring_private, 1);
+ pthread_mutex_init(&(kyr->mutex), NULL);
+ kyr->head.cert = cert;
+ kyr->head.key = key;
+ kyr->head.chain = chain;
kyr->references = 1;
return kyr;
}
@@ -120,64 +116,9 @@ static struct keyring* keyring_new3(EVP_PKEY *key, X509 *cert, STACK_OF(X509) *c
// Increment reference count.
static void keyring_ref_inc(struct keyring_private* kyr)
{
- pthread_mutex_lock(&kyr->mutex);
+ pthread_mutex_lock(&(kyr->mutex));
kyr->references++;
- pthread_mutex_unlock(&kyr->mutex);
-}
-
-/*
- * Thread-safe setter functions; they copy the value (refcounts are inc'd).
- */
-static void keyring_set_key(struct keyring_private* kyr, EVP_PKEY *key)
-{
- pthread_mutex_lock(&kyr->mutex);
- if ((kyr->head).key)
- {
- EVP_PKEY_free((kyr->head).key);
- }
- (kyr->head).key = key;
- if (key)
- {
- ssl_key_refcount_inc((kyr->head).key);
- }
- pthread_mutex_unlock(&kyr->mutex);
-}
-
-static void keyring_set_cert(struct keyring_private* kry, X509 *cert)
-{
- pthread_mutex_lock(&kry->mutex);
- if ((kry->head).cert)
- {
- X509_free((kry->head).cert);
- }
- (kry->head).cert = cert;
- if (cert)
- {
- ssl_x509_refcount_inc((kry->head).cert);
- }
- pthread_mutex_unlock(&kry->mutex);
-}
-
-static void keyring_set_chain(struct keyring_private* kyr, STACK_OF(X509) *chain)
-{
- pthread_mutex_lock(&kyr->mutex);
- if ((kyr->head).chain)
- {
- sk_X509_pop_free((kyr->head).chain, X509_free);
- }
- if (chain)
- {
- (kyr->head).chain = sk_X509_dup(chain);
- int i = 0;
- for (i = 0; i < sk_X509_num((kyr->head).chain); i++)
- {
- ssl_x509_refcount_inc(sk_X509_value((kyr->head).chain, i));
- }
- } else
- {
- (kyr->head).chain = NULL;
- }
- pthread_mutex_unlock(&kyr->mutex);
+ pthread_mutex_unlock(&(kyr->mutex));
}
/*
@@ -195,20 +136,20 @@ void key_keeper_free_keyring(struct keyring *kyr)
}
pthread_mutex_unlock(&_kyr->mutex);
pthread_mutex_destroy(&_kyr->mutex);
- if ((_kyr->head).key)
+ if (_kyr->head.key)
{
EVP_PKEY_free((_kyr->head).key);
- (_kyr->head).key=NULL;
+ _kyr->head.key=NULL;
}
- if ((_kyr->head).cert)
+ if (_kyr->head.cert)
{
- X509_free((_kyr->head).cert);
- (_kyr->head).cert=NULL;
+ X509_free(_kyr->head.cert);
+ _kyr->head.cert=NULL;
}
- if ((_kyr->head).chain)
+ if (_kyr->head.chain)
{
sk_X509_pop_free((_kyr->head).chain, X509_free);
- (_kyr->head).chain=NULL;
+ _kyr->head.chain=NULL;
}
free(_kyr);
}
@@ -336,15 +277,10 @@ static struct keyring_private* get_keyring_from_response(const char* data)
goto error_out;
}
sk_X509_push(chain, chain_cert);
- ssl_x509_refcount_inc(chain_cert);
- }
- _kyr= keyring_new();
- keyring_set_cert(_kyr, cert);
- keyring_set_key(_kyr, key);
- keyring_set_chain(_kyr, chain);
- X509_free(cert);
- EVP_PKEY_free(key);
- sk_X509_pop_free(chain, X509_free);
+// ssl_x509_refcount_inc(chain_cert);
+ }
+ _kyr= keyring_new(cert, key, chain);
+
cJSON_Delete(data_json);
return _kyr;
@@ -383,14 +319,8 @@ static struct keyring_private* generate_x509_keyring(X509* origin_cert, X509* ca
sk_X509_push(chain, forge_cert);
ssl_x509_refcount_inc(ca);
ssl_x509_refcount_inc(forge_cert);
- struct keyring_private* _kyr= keyring_new();
- keyring_set_key(_kyr, forge_key);
- keyring_set_cert(_kyr, forge_cert);
- keyring_set_chain(_kyr, chain);
-
- X509_free(forge_cert);
- EVP_PKEY_free(forge_key);
- sk_X509_pop_free(chain, X509_free);
+ struct keyring_private* _kyr= keyring_new(forge_cert, forge_key, chain);
+
return _kyr;
}