OpenSSL EVP: fix threaded use of structs

Make sure we don't clear or reset static structs after first init so
that they work fine even when used from multiple threads. Init the
structs in the global init.

Help and assistance by: John Engstrom

Fixes #229 (again)
This commit is contained in:
Daniel Stenberg 2011-09-29 22:17:03 +02:00
parent 24afd0fc72
commit ee07785a1e
4 changed files with 21 additions and 5 deletions

View File

@ -113,4 +113,6 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
const char *privatekey,
const char *passphrase);
void _libssh2_init_aes_ctr(void);
#endif

View File

@ -46,6 +46,7 @@ libssh2_init(int flags)
{
if (_libssh2_initialized == 0 && !(flags & LIBSSH2_INIT_NO_CRYPTO)) {
libssh2_crypto_init();
_libssh2_init_aes_ctr();
}
_libssh2_initialized++;

View File

@ -585,4 +585,8 @@ _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
libssh2_error */
}
void _libssh2_init_aes_ctr(void)
{
/* no implementation */
}
#endif /* LIBSSH2_LIBGCRYPT */

View File

@ -317,8 +317,6 @@ aes_ctr_cleanup(EVP_CIPHER_CTX *ctx) /* cleanup ctx */
static const EVP_CIPHER *
make_ctr_evp (size_t keylen, EVP_CIPHER *aes_ctr_cipher)
{
memset(aes_ctr_cipher, 0, sizeof(aes_ctr_cipher));
aes_ctr_cipher->block_size = 16;
aes_ctr_cipher->key_len = keylen;
aes_ctr_cipher->iv_len = 16;
@ -333,22 +331,33 @@ const EVP_CIPHER *
_libssh2_EVP_aes_128_ctr(void)
{
static EVP_CIPHER aes_ctr_cipher;
return make_ctr_evp (16, &aes_ctr_cipher);
return !aes_ctr_cipher.key_len?
make_ctr_evp (16, &aes_ctr_cipher) : &aes_ctr_cipher;
}
const EVP_CIPHER *
_libssh2_EVP_aes_192_ctr(void)
{
static EVP_CIPHER aes_ctr_cipher;
return make_ctr_evp (24, &aes_ctr_cipher);
return !aes_ctr_cipher.key_len?
make_ctr_evp (24, &aes_ctr_cipher) : &aes_ctr_cipher;
}
const EVP_CIPHER *
_libssh2_EVP_aes_256_ctr(void)
{
static EVP_CIPHER aes_ctr_cipher;
return make_ctr_evp (32, &aes_ctr_cipher);
return !aes_ctr_cipher.key_len?
make_ctr_evp (32, &aes_ctr_cipher) : &aes_ctr_cipher;
}
void _libssh2_init_aes_ctr(void)
{
_libssh2_EVP_aes_128_ctr();
_libssh2_EVP_aes_192_ctr();
_libssh2_EVP_aes_256_ctr();
}
#endif /* LIBSSH2_AES_CTR */
/* TODO: Optionally call a passphrase callback specified by the