Skip to content

Instantly share code, notes, and snippets.

@viktorium
Created January 12, 2015 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viktorium/b466b72c83d2ab90182c to your computer and use it in GitHub Desktop.
Save viktorium/b466b72c83d2ab90182c to your computer and use it in GitHub Desktop.
Ruby OpenSSL RSA sign patch
diff --git ext/openssl/ossl_pkey.c ext/openssl/ossl_pkey.c
index f785e66..2384676 100644
--- ext/openssl/ossl_pkey.c
+++ ext/openssl/ossl_pkey.c
@@ -237,6 +237,7 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
EVP_MD_CTX ctx;
unsigned int buf_len;
VALUE str;
+ int result;
if (rb_funcall(self, id_private_q, 0, NULL) != Qtrue) {
ossl_raise(rb_eArgError, "Private key is needed.");
@@ -246,7 +247,9 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
StringValue(data);
EVP_SignUpdate(&ctx, RSTRING_PTR(data), RSTRING_LEN(data));
str = rb_str_new(0, EVP_PKEY_size(pkey)+16);
- if (!EVP_SignFinal(&ctx, (unsigned char *)RSTRING_PTR(str), &buf_len, pkey))
+ result = EVP_SignFinal(&ctx, (unsigned char *)RSTRING_PTR(str), &buf_len, pkey);
+ EVP_MD_CTX_cleanup(&ctx);
+ if (!result)
ossl_raise(ePKeyError, NULL);
assert((long)buf_len <= RSTRING_LEN(str));
rb_str_set_len(str, buf_len);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment