From d22373455532e99c0adf1a1e5d0b3a864691e2f9 Mon Sep 17 00:00:00 2001 From: knu Date: Tue, 23 Feb 2010 17:33:39 +0000 Subject: Back out the series of changes pending a due discussion. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26743 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/lib/openssl/digest.rb | 1 + ext/openssl/ossl_digest.c | 19 +++------- ext/openssl/ossl_hmac.c | 77 ++++++++++++++++++++++++--------------- 3 files changed, 54 insertions(+), 43 deletions(-) (limited to 'ext/openssl') diff --git a/ext/openssl/lib/openssl/digest.rb b/ext/openssl/lib/openssl/digest.rb index 7da80f23cf..e603c41de4 100644 --- a/ext/openssl/lib/openssl/digest.rb +++ b/ext/openssl/lib/openssl/digest.rb @@ -43,6 +43,7 @@ module OpenSSL singleton = (class << klass; self; end) singleton.class_eval{ define_method(:digest){|data| Digest.digest(name, data) } + define_method(:hexdigest){|data| Digest.hexdigest(name, data) } } const_set(name, klass) } diff --git a/ext/openssl/ossl_digest.c b/ext/openssl/ossl_digest.c index 30478911ba..9b8ced5233 100644 --- a/ext/openssl/ossl_digest.c +++ b/ext/openssl/ossl_digest.c @@ -37,25 +37,18 @@ GetDigestPtr(VALUE obj) { const EVP_MD *md; - if (TYPE(obj) == T_CLASS) { - EVP_MD_CTX *ctx; - VALUE digest = rb_funcall(obj, rb_intern("new"), 0, 0); - - SafeGetDigest(digest, ctx); + if (TYPE(obj) == T_STRING) { + const char *name = StringValueCStr(obj); - md = EVP_MD_CTX_md(ctx); - } else if (rb_obj_is_kind_of(obj, cDigest)) { + md = EVP_get_digestbyname(name); + if (!md) + ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name); + } else { EVP_MD_CTX *ctx; SafeGetDigest(obj, ctx); md = EVP_MD_CTX_md(ctx); - } else { - const char *name = StringValueCStr(obj); - - md = EVP_get_digestbyname(name); - if (!md) - ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name); } return md; diff --git a/ext/openssl/ossl_hmac.c b/ext/openssl/ossl_hmac.c index 13d8d9d0cf..aa7644aa5c 100644 --- a/ext/openssl/ossl_hmac.c +++ b/ext/openssl/ossl_hmac.c @@ -125,11 +125,11 @@ hmac_final(HMAC_CTX *ctx, unsigned char **buf, unsigned int *buf_len) /* * call-seq: - * hmac.finish -> aString + * hmac.digest -> aString * */ static VALUE -ossl_hmac_finish(VALUE self) +ossl_hmac_digest(VALUE self) { HMAC_CTX *ctx; unsigned char *buf; @@ -145,72 +145,89 @@ ossl_hmac_finish(VALUE self) /* * call-seq: - * hmac.reset -> self + * hmac.hexdigest -> aString * */ static VALUE -ossl_hmac_reset(VALUE self) +ossl_hmac_hexdigest(VALUE self) { HMAC_CTX *ctx; - + unsigned char *buf; + char *hexbuf; + unsigned int buf_len; + VALUE hexdigest; + GetHMAC(self, ctx); - HMAC_Init_ex(ctx, NULL, 0, NULL, NULL); + hmac_final(ctx, &buf, &buf_len); + if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) { + OPENSSL_free(buf); + ossl_raise(eHMACError, "Memory alloc error"); + } + OPENSSL_free(buf); + hexdigest = ossl_buf2str(hexbuf, 2 * buf_len); - return self; + return hexdigest; } /* * call-seq: - * hmac.digest_length -> integer + * hmac.reset -> self * */ static VALUE -ossl_hmac_digest_length(VALUE self) +ossl_hmac_reset(VALUE self) { HMAC_CTX *ctx; GetHMAC(self, ctx); + HMAC_Init_ex(ctx, NULL, 0, NULL, NULL); - return INT2FIX(HMAC_size(ctx)); + return self; } /* * call-seq: - * hmac.block_length -> integer + * HMAC.digest(digest, key, data) -> aString * */ static VALUE -ossl_hmac_block_length(VALUE self) +ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data) { - HMAC_CTX *ctx; - - GetHMAC(self, ctx); + unsigned char *buf; + unsigned int buf_len; + + StringValue(key); + StringValue(data); + buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LEN(key), + (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len); - return INT2FIX(EVP_MD_block_size(ctx->md)); + return rb_str_new((const char *)buf, buf_len); } /* * call-seq: - * HMAC.digest(digest_class, key, data) -> aString - * HMAC.digest(digest_object, key, data) -> aString - * HMAC.digest(digest_name, key, data) -> aString + * HMAC.digest(digest, key, data) -> aString * - * The last three forms are still supported for backward compatibility, - * and HMAC.digest(data, key, digest_name) is _not_ supported for - * that reason. */ static VALUE -ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data) +ossl_hmac_s_hexdigest(VALUE klass, VALUE digest, VALUE key, VALUE data) { unsigned char *buf; + char *hexbuf; unsigned int buf_len; + VALUE hexdigest; StringValue(key); StringValue(data); + buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LEN(key), (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len); + if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) { + ossl_raise(eHMACError, "Cannot convert buf to hexbuf"); + } + hexdigest = ossl_buf2str(hexbuf, 2 * buf_len); - return rb_str_new((const char *)buf, buf_len); + return hexdigest; } /* @@ -219,18 +236,17 @@ ossl_hmac_s_digest(VALUE klass, VALUE digest, VALUE key, VALUE data) void Init_ossl_hmac() { - rb_require("digest"); - #if 0 /* let rdoc know about mOSSL */ mOSSL = rb_define_module("OpenSSL"); #endif eHMACError = rb_define_class_under(mOSSL, "HMACError", eOSSLError); - cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_path2class("Digest::Class")); + cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_cObject); rb_define_alloc_func(cHMAC, ossl_hmac_alloc); rb_define_singleton_method(cHMAC, "digest", ossl_hmac_s_digest, 3); + rb_define_singleton_method(cHMAC, "hexdigest", ossl_hmac_s_hexdigest, 3); rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, 2); rb_define_copy_func(cHMAC, ossl_hmac_copy); @@ -238,9 +254,10 @@ Init_ossl_hmac() rb_define_method(cHMAC, "reset", ossl_hmac_reset, 0); rb_define_method(cHMAC, "update", ossl_hmac_update, 1); rb_define_alias(cHMAC, "<<", "update"); - rb_define_private_method(cHMAC, "finish", ossl_hmac_finish, 0); - rb_define_method(cHMAC, "digest_length", ossl_hmac_digest_length, 0); - rb_define_method(cHMAC, "block_length", ossl_hmac_block_length, 0); + rb_define_method(cHMAC, "digest", ossl_hmac_digest, 0); + rb_define_method(cHMAC, "hexdigest", ossl_hmac_hexdigest, 0); + rb_define_alias(cHMAC, "inspect", "hexdigest"); + rb_define_alias(cHMAC, "to_s", "hexdigest"); } #else /* NO_HMAC */ -- cgit v1.2.3