Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions ext/openssl/ossl_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
#include "ossl.h"

#define NewHMAC(klass) \
TypedData_Wrap_Struct((klass), &ossl_hmac_type, 0)
#define GetHMAC(obj, ctx) do { \
TypedData_Get_Struct((obj), EVP_MD_CTX, &ossl_hmac_type, (ctx)); \
if (!(ctx)) { \
Expand Down Expand Up @@ -49,16 +47,7 @@ static const rb_data_type_t ossl_hmac_type = {
static VALUE
ossl_hmac_alloc(VALUE klass)
{
VALUE obj;
EVP_MD_CTX *ctx;

obj = NewHMAC(klass);
ctx = EVP_MD_CTX_new();
if (!ctx)
ossl_raise(eHMACError, "EVP_MD_CTX");
RTYPEDDATA_DATA(obj) = ctx;

return obj;
return TypedData_Wrap_Struct(klass, &ossl_hmac_type, NULL);
}


Expand Down Expand Up @@ -98,18 +87,25 @@ ossl_hmac_initialize(VALUE self, VALUE key, VALUE digest)
const EVP_MD *md;
VALUE md_holder;

GetHMAC(self, ctx);
ossl_want_uninitialized(self, &ossl_hmac_type);
StringValue(key);
md = ossl_evp_md_fetch(digest, &md_holder);
pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
(unsigned char *)RSTRING_PTR(key),
RSTRING_LENINT(key));
if (!pkey)
ossl_raise(eHMACError, "EVP_PKEY_new_raw_private_key");
ctx = EVP_MD_CTX_new();
if (!ctx) {
EVP_PKEY_free(pkey);
ossl_raise(eHMACError, "EVP_MD_CTX_new");
}
if (EVP_DigestSignInit(ctx, NULL, md, NULL, pkey) != 1) {
EVP_MD_CTX_free(ctx);
EVP_PKEY_free(pkey);
ossl_raise(eHMACError, "EVP_DigestSignInit");
}
RTYPEDDATA_DATA(self) = ctx;
rb_ivar_set(self, id_md_holder, md_holder);
/* Decrement reference counter; EVP_MD_CTX still keeps it */
EVP_PKEY_free(pkey);
Expand All @@ -123,13 +119,18 @@ ossl_hmac_copy(VALUE self, VALUE other)
{
EVP_MD_CTX *ctx1, *ctx2;

rb_check_frozen(self);
if (self == other) return self;

GetHMAC(self, ctx1);
ossl_want_uninitialized(self, &ossl_hmac_type);
GetHMAC(other, ctx2);
if (EVP_MD_CTX_copy(ctx1, ctx2) != 1)

ctx1 = EVP_MD_CTX_new();
if (!ctx1)
ossl_raise(eHMACError, "EVP_MD_CTX_new");
if (EVP_MD_CTX_copy(ctx1, ctx2) != 1) {
EVP_MD_CTX_free(ctx1);
ossl_raise(eHMACError, "EVP_MD_CTX_copy");
}
RTYPEDDATA_DATA(self) = ctx1;

return self;
}

Expand Down
1 change: 1 addition & 0 deletions ext/openssl/ossl_ocsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ ossl_ocspbres_initialize(int argc, VALUE *argv, VALUE self)
const unsigned char *p;

rb_scan_args(argc, argv, "01", &arg);
ossl_want_uninitialized(self, &ossl_ocsp_basicresp_type);
if (NIL_P(arg)) {
bs_new = OCSP_BASICRESP_new();
if (!bs_new)
Expand Down
6 changes: 6 additions & 0 deletions test/openssl/test_hmac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_hmac_sha224
assert_equal "iW+xEoq73xloMhB81J3zP0e0sRaZErpPU2hLIg==", hmac.base64digest
end

def test_initialize
hmac = OpenSSL::HMAC.new("a"*32, "SHA256")
assert_raise(TypeError) { hmac.__send__(:initialize, "b"*32, "SHA512") }
assert_raise(RuntimeError) { OpenSSL::HMAC.allocate.reset }
end

def test_dup
h1 = OpenSSL::HMAC.new("KEY"*32, "SHA256")
h1.update("DATA")
Expand Down
Loading