From 30103702c367c5cdaa18b6a622f3916cec6c701e Mon Sep 17 00:00:00 2001 From: gotoyuzo Date: Wed, 17 Sep 2003 09:05:02 +0000 Subject: * ext/openssl: all files are reviewed to simplify and avoid memory leak. * ext/openssl/extconf.rb: add check for assert.h. * ext/openssl/ossl.c (ossl_buf2str): new function to convert C buffer to String and free buffer. * ext/openssl/ossl.c (ossl_x509_ary2sk): new function to convert Array of OpenSSL::X509 to STACK_OF(X509) with exception safe. * ext/openssl/ossl.c (ossl_to_der, ossl_to_der_if_possible): new functions to convert object to DER string. * ext/openssl/ossl.h: ditto. * ext/openssl/ossl_bio.c (ossl_membio2str): new function to convert BIO to String object and free BIO. * ext/openssl/ossl_bio.h: ditto. * ext/openssl/ossl_pkcs7.c (ossl_pkcs7_to_der): add for "to_der". * ext/openssl/ossl_x509name.c (ossl_x509name_to_der): ditto. * ext/openssl/ossl_x509ext.c (ossl_x509ext_to_der): ditto. * ext/openssl/ossl_x509ext.c (create_ext_from_array): removed and reimplement in openssl/x509.rb. * ext/openssl/ossl_x509attr.c: reimplemented and disable some method temporarily. this class doesn't work fine without ASN.1 data support;-) I'll rewrite in near future. * ext/openssl/lib/openssl/x509.c (X509::Attribute): get rid off unused code. * ext/openssl/lib/openssl/x509.c (X509::ExtensionFactory): refine all. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4558 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/ossl_pkey.c | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) (limited to 'ext/openssl/ossl_pkey.c') diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index f9f8c0ad5e..dcae075ee8 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -149,23 +149,17 @@ ossl_pkey_to_der(VALUE self) { EVP_PKEY *pkey; VALUE str; - BIO *out; - BUF_MEM *buf; + long len; + unsigned char *p; GetPKey(self, pkey); - - out = BIO_new(BIO_s_mem()); - if (!out) ossl_raise(ePKeyError, NULL); - - if (!i2d_PUBKEY_bio(out, pkey)) { - BIO_free(out); + if((len = i2d_PUBKEY(pkey, NULL)) <= 0) ossl_raise(ePKeyError, NULL); - } - - BIO_get_mem_ptr(out, &buf); - str = rb_str_new(buf->data, buf->length); - - BIO_free(out); + str = rb_str_new(0, len); + p = RSTRING(str)->ptr; + if(len = i2d_PUBKEY(pkey, &p) <= 0) + ossl_raise(ePKeyError, NULL); + ossl_str_adjust(str, p); return str; } @@ -175,7 +169,6 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data) { EVP_PKEY *pkey; EVP_MD_CTX ctx; - char *buf; int buf_len; VALUE str; @@ -186,15 +179,12 @@ ossl_pkey_sign(VALUE self, VALUE digest, VALUE data) EVP_SignInit(&ctx, GetDigestPtr(digest)); StringValue(data); EVP_SignUpdate(&ctx, RSTRING(data)->ptr, RSTRING(data)->len); - if (!(buf = OPENSSL_malloc(EVP_PKEY_size(pkey) + 16))) { - ossl_raise(ePKeyError, NULL); - } - if (!EVP_SignFinal(&ctx, buf, &buf_len, pkey)) { - OPENSSL_free(buf); + str = rb_str_new(0, EVP_PKEY_size(pkey)+16); + if (!EVP_SignFinal(&ctx, RSTRING(str)->ptr, &buf_len, pkey)) ossl_raise(ePKeyError, NULL); - } - str = rb_str_new(buf, buf_len); - OPENSSL_free(buf); + assert(buf_len <= RSTRING(str)->len); + RSTRING(str)->len = buf_len; + RSTRING(str)->ptr[buf_len] = 0; return str; } -- cgit v1.2.3