Skip to content

Commit

Permalink
bugfix: compatible with OPENSSL_VERSION_NUMBER < 0x10100000L
Browse files Browse the repository at this point in the history
  • Loading branch information
xjdrew committed Aug 7, 2018
1 parent b8b2c1c commit 1a7509a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
7 changes: 4 additions & 3 deletions src/lcrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <lua.h>

#include "lcrypto.h"
#include "lcrypto_compat_110.h"

LUACRYPTO_API int luaopen_crypto(lua_State* L);

Expand Down Expand Up @@ -599,7 +600,7 @@ static int hmac_update(lua_State* L)
hmac_context* c = (hmac_context*)luaL_checkudata(L, 1, LUACRYPTO_HMACNAME);
const char* s = luaL_checklstring(L, 2, &s_len);

if(HMAC_Update(c->ctx, (const unsigned char*)s, s_len) != 1) {
if (HMAC_Update(c->ctx, (const unsigned char*)s, s_len) != 1) {
return crypto_error(L);
}

Expand All @@ -621,7 +622,7 @@ static int hmac_final(lua_State* L)
HMAC_Update(c->ctx, (const unsigned char*)s, s_len);
}

if(HMAC_Final(c->ctx, digest, &written) != 1) {
if (HMAC_Final(c->ctx, digest, &written) != 1) {
return crypto_error(L);
}

Expand Down Expand Up @@ -1027,7 +1028,7 @@ static int pkey_generate(lua_State* L)
return 1;
} else {
DSA* dsa = DSA_new();
if(DSA_generate_parameters_ex(dsa, key_len, NULL, 0, NULL, NULL, NULL) != 1) {
if (DSA_generate_parameters_ex(dsa, key_len, NULL, 0, NULL, NULL, NULL) != 1) {
DSA_free(dsa);
return crypto_error(L);
}
Expand Down
55 changes: 25 additions & 30 deletions src/lcrypto_compat_110.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,19 @@ int DH_set_length(DH* dh, long length)

EVP_CIPHER_CTX* EVP_CIPHER_CTX_new()
{
EVP_CIPHER_CTX* ctx = OPENSSL_malloc(sizeof(*ctx));
EVP_CIPHER_CTX* ctx = OPENSSL_zalloc(sizeof(*ctx));
if (ctx != NULL) {
if (!EVP_CIPHER_CTX_init(ctx)) {
OPENSSL_free(ctx);
return NULL;
}
EVP_CIPHER_CTX_init(ctx);
}
return ctx;
}

void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX* ctx)
{
EVP_CIPHER_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}

const unsigned char* EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX* ctx)
{
return ctx->iv;
Expand All @@ -319,10 +319,7 @@ EVP_MD_CTX* EVP_MD_CTX_new(void)
{
EVP_MD_CTX* ctx = OPENSSL_zalloc(sizeof(EVP_MD_CTX));
if (ctx != NULL) {
if (!EVP_MD_CTX_init(ctx)) {
OPENSSL_free(ctx);
return NUll;
}
EVP_MD_CTX_init(ctx);
}

return ctx;
Expand All @@ -332,16 +329,18 @@ int EVP_MD_CTX_reset(EVP_MD_CTX* ctx)
{
int ret = EVP_MD_CTX_cleanup(ctx);
if (!ret) {
return ret
return ret;
}
EVP_MD_CTX_init(ctx);
return ret;
}

void EVP_MD_CTX_free(EVP_MD_CTX* ctx)
{
EVP_MD_CTX_cleanup(ctx);
OPENSSL_free(ctx);
if (ctx != NULL) {
EVP_MD_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}
}

RSA_METHOD* RSA_meth_dup(const RSA_METHOD* meth)
Expand Down Expand Up @@ -414,34 +413,30 @@ int RSA_bits(const RSA* r)
return (BN_num_bits(r->n));
}

RSA* EVP_PKEY_get0_RSA(EVP_PKEY* pkey)
{
if (pkey->type != EVP_PKEY_RSA) {
return NULL;
}
return pkey->pkey.rsa;
}

HMAC_CTX* HMAC_CTX_new(void)
{
HMAC_CTX* ctx = OPENSSL_malloc(sizeof(*ctx));
HMAC_CTX* ctx = OPENSSL_zalloc(sizeof(*ctx));
if (ctx != NULL) {
if (!HMAC_CTX_reset(ctx)) {
HMAC_CTX_free(ctx);
return NULL;
}
HMAC_CTX_init(ctx);
}
return ctx;
}

void HMAC_CTX_free(HMAC_CTX* ctx)
{
if (ctx != NULL) {
hmac_ctx_cleanup(ctx);
EVP_MD_CTX_free(ctx->i_ctx);
EVP_MD_CTX_free(ctx->o_ctx);
EVP_MD_CTX_free(ctx->md_ctx);
HMAC_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}
}
#endif

DSA* EVP_PKEY_get0_DSA(EVP_PKEY* pkey)
{
return pkey->pkey.dsa;
}

RSA* EVP_PKEY_get0_RSA(EVP_PKEY* pkey)
{
return pkey->pkey.rsa;
}
#endif

0 comments on commit 1a7509a

Please sign in to comment.