diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-05-17 21:48:48 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-05-17 21:48:48 +0900 |
commit | 017496151f7e41a17913b267bc9de5ab8318b5e5 (patch) | |
tree | ce28e8e7d75bc0c659011b81fa1ae313ea01545a | |
parent | 384e12804a987ce1d0dcf64a009f2dd82a679509 (diff) | |
download | ruby-topic/openssl-doc.tar.gz |
fixup! ext/openssl: small cleanupstopic/openssl-doc
-rw-r--r-- | ext/openssl/ossl.c | 75 | ||||
-rw-r--r-- | ext/openssl/ossl_hmac.c | 12 |
2 files changed, 39 insertions, 48 deletions
diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c index 71ef09bc9f..ef03765be1 100644 --- a/ext/openssl/ossl.c +++ b/ext/openssl/ossl.c @@ -11,40 +11,6 @@ #include <stdarg.h> /* for ossl_raise */ /* - * String to HEXString conversion - */ -int -string2hex(const unsigned char *buf, int buf_len, char **hexbuf, int *hexbuf_len) -{ - static const char hex[]="0123456789abcdef"; - int i, len; - - if (buf_len < 0 || buf_len > INT_MAX / 2) { /* PARANOIA? */ - return -1; - } - len = 2 * buf_len; - if (!hexbuf) { /* if no buf, return calculated len */ - if (hexbuf_len) { - *hexbuf_len = len; - } - return len; - } - if (!(*hexbuf = OPENSSL_malloc(len + 1))) { - return -1; - } - for (i = 0; i < buf_len; i++) { - (*hexbuf)[2 * i] = hex[((unsigned char)buf[i]) >> 4]; - (*hexbuf)[2 * i + 1] = hex[buf[i] & 0x0f]; - } - (*hexbuf)[2 * i] = '\0'; - - if (hexbuf_len) { - *hexbuf_len = len; - } - return len; -} - -/* * Data Conversion */ #define OSSL_IMPL_ARY2SK(name, type, expected_class, dup) \ @@ -130,15 +96,21 @@ ossl_str_new(int size) return rb_str_new(0, size); } +static VALUE +ossl_str_allocate(int size, int *status) +{ + /* This is needed because rb_str_new() may raise. In that case, we can't + * free buf. */ + return rb_protect((VALUE(*)_((VALUE)))ossl_str_new, size, &status); +} + VALUE ossl_buf2str(char *buf, int len) { VALUE str; - int status = 0; + int status; - /* This is needed because rb_str_new() may raise. In that case, we can't - * free buf. */ - str = rb_protect((VALUE(*)_((VALUE)))ossl_str_new, len, &status); + str = ossl_str_allocate(len, &status); if(!NIL_P(str)) memcpy(RSTRING_PTR(str), buf, len); OPENSSL_free(buf); if(status) rb_jump_tag(status); @@ -146,6 +118,33 @@ ossl_buf2str(char *buf, int len) return str; } +VALUE +ossl_buf2hexstr(const unsigned char *buf, int len) +{ + static const char hex[] = "0123456789abcdef"; + int i, status; + char *p; + VALUE str; + + if (len < 0 || len > LONG_MAX / 2) + return Qnil; + + str = ossl_str_allocate(len * 2); + if (status) {/* nomem */ + OPENSSL_free(buf); + rb_jump_tag(status); + } + + for (i = 0, p = RSTRING_PTR(str); i < len; i++) { + unsigned char byte = buf[i]; + p[i * 2] = hex[byte >> 4]; + p[i * 2 + 1] = hex[byte & 0x0f]; + } + + OPENSSL_free(buf); + return str; +} + /* * our default PEM callback */ diff --git a/ext/openssl/ossl_hmac.c b/ext/openssl/ossl_hmac.c index 5513cb20de..59d60af791 100644 --- a/ext/openssl/ossl_hmac.c +++ b/ext/openssl/ossl_hmac.c @@ -215,20 +215,12 @@ ossl_hmac_hexdigest(VALUE self) { HMAC_CTX *ctx; unsigned char *buf; - char *hexbuf; unsigned int buf_len; - VALUE hexdigest; GetHMAC(self, ctx); hmac_final(ctx, &buf, &buf_len); - if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) { - OPENSSL_free(buf); - ossl_raise(eHMACError, "Memory alloc error"); - } - OPENSSL_free(buf); - hexdigest = ossl_buf2str(hexbuf, 2 * buf_len); - return hexdigest; + return ossl_buf2hexstr(buf, buf_len); } /* @@ -313,13 +305,13 @@ static VALUE 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 is static buffer */ buf = HMAC(GetDigestPtr(digest), RSTRING_PTR(key), RSTRING_LENINT(key), (unsigned char *)RSTRING_PTR(data), RSTRING_LEN(data), NULL, &buf_len); if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * (int)buf_len) { |