1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

openssl: wrapper object before alloc

* ext/openssl: make wrapper objects before allocating structs to
  get rid of potential memory leaks.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50673 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-05-29 05:55:02 +00:00
parent 5924f9a684
commit 451fe269e5
23 changed files with 226 additions and 124 deletions

View file

@ -11,11 +11,13 @@
/* modified by Michal Rokos <m.rokos@sh.cvut.cz> */ /* modified by Michal Rokos <m.rokos@sh.cvut.cz> */
#include "ossl.h" #include "ossl.h"
#define WrapBN(klass, obj, bn) do { \ #define NewBN(klass) \
TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
#define SetBN(obj, bn) do { \
if (!(bn)) { \ if (!(bn)) { \
ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "BN wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_bn_type, (bn)); \ RTYPEDDATA_DATA(obj) = (bn); \
} while (0) } while (0)
#define GetBN(obj, bn) do { \ #define GetBN(obj, bn) do { \
@ -71,11 +73,12 @@ ossl_bn_new(const BIGNUM *bn)
BIGNUM *newbn; BIGNUM *newbn;
VALUE obj; VALUE obj;
obj = NewBN(cBN);
newbn = bn ? BN_dup(bn) : BN_new(); newbn = bn ? BN_dup(bn) : BN_new();
if (!newbn) { if (!newbn) {
ossl_raise(eBNError, NULL); ossl_raise(eBNError, NULL);
} }
WrapBN(cBN, obj, newbn); SetBN(obj, newbn);
return obj; return obj;
} }
@ -84,6 +87,7 @@ BIGNUM *
GetBNPtr(VALUE obj) GetBNPtr(VALUE obj)
{ {
BIGNUM *bn = NULL; BIGNUM *bn = NULL;
VALUE newobj;
if (RTEST(rb_obj_is_kind_of(obj, cBN))) { if (RTEST(rb_obj_is_kind_of(obj, cBN))) {
GetBN(obj, bn); GetBN(obj, bn);
@ -91,10 +95,11 @@ GetBNPtr(VALUE obj)
case T_FIXNUM: case T_FIXNUM:
case T_BIGNUM: case T_BIGNUM:
obj = rb_String(obj); obj = rb_String(obj);
newobj = NewBN(cBN); /* GC bug */
if (!BN_dec2bn(&bn, StringValuePtr(obj))) { if (!BN_dec2bn(&bn, StringValuePtr(obj))) {
ossl_raise(eBNError, NULL); ossl_raise(eBNError, NULL);
} }
WrapBN(cBN, obj, bn); /* Handle potencial mem leaks */ SetBN(newobj, bn); /* Handle potencial mem leaks */
break; break;
case T_NIL: case T_NIL:
break; break;
@ -118,12 +123,12 @@ static VALUE
ossl_bn_alloc(VALUE klass) ossl_bn_alloc(VALUE klass)
{ {
BIGNUM *bn; BIGNUM *bn;
VALUE obj; VALUE obj = NewBN(klass);
if (!(bn = BN_new())) { if (!(bn = BN_new())) {
ossl_raise(eBNError, NULL); ossl_raise(eBNError, NULL);
} }
WrapBN(klass, obj, bn); SetBN(obj, bn);
return obj; return obj;
} }
@ -365,6 +370,7 @@ BIGNUM_BOOL1(is_odd)
BIGNUM *bn, *result; \ BIGNUM *bn, *result; \
VALUE obj; \ VALUE obj; \
GetBN(self, bn); \ GetBN(self, bn); \
obj = NewBN(CLASS_OF(self)); \
if (!(result = BN_new())) { \ if (!(result = BN_new())) { \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
@ -372,7 +378,7 @@ BIGNUM_BOOL1(is_odd)
BN_free(result); \ BN_free(result); \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
WrapBN(CLASS_OF(self), obj, result); \ SetBN(obj, result); \
return obj; \ return obj; \
} }
@ -389,6 +395,7 @@ BIGNUM_1c(sqr)
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
VALUE obj; \ VALUE obj; \
GetBN(self, bn1); \ GetBN(self, bn1); \
obj = NewBN(CLASS_OF(self)); \
if (!(result = BN_new())) { \ if (!(result = BN_new())) { \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
@ -396,7 +403,7 @@ BIGNUM_1c(sqr)
BN_free(result); \ BN_free(result); \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
WrapBN(CLASS_OF(self), obj, result); \ SetBN(obj, result); \
return obj; \ return obj; \
} }
@ -419,6 +426,7 @@ BIGNUM_2(sub)
BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \ BIGNUM *bn1, *bn2 = GetBNPtr(other), *result; \
VALUE obj; \ VALUE obj; \
GetBN(self, bn1); \ GetBN(self, bn1); \
obj = NewBN(CLASS_OF(self)); \
if (!(result = BN_new())) { \ if (!(result = BN_new())) { \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
@ -426,7 +434,7 @@ BIGNUM_2(sub)
BN_free(result); \ BN_free(result); \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
WrapBN(CLASS_OF(self), obj, result); \ SetBN(obj, result); \
return obj; \ return obj; \
} }
@ -480,6 +488,8 @@ ossl_bn_div(VALUE self, VALUE other)
GetBN(self, bn1); GetBN(self, bn1);
obj1 = NewBN(CLASS_OF(self));
obj2 = NewBN(CLASS_OF(self));
if (!(r1 = BN_new())) { if (!(r1 = BN_new())) {
ossl_raise(eBNError, NULL); ossl_raise(eBNError, NULL);
} }
@ -492,8 +502,8 @@ ossl_bn_div(VALUE self, VALUE other)
BN_free(r2); BN_free(r2);
ossl_raise(eBNError, NULL); ossl_raise(eBNError, NULL);
} }
WrapBN(CLASS_OF(self), obj1, r1); SetBN(obj1, r1);
WrapBN(CLASS_OF(self), obj2, r2); SetBN(obj2, r2);
return rb_ary_new3(2, obj1, obj2); return rb_ary_new3(2, obj1, obj2);
} }
@ -506,6 +516,7 @@ ossl_bn_div(VALUE self, VALUE other)
BIGNUM *bn3 = GetBNPtr(other2), *result; \ BIGNUM *bn3 = GetBNPtr(other2), *result; \
VALUE obj; \ VALUE obj; \
GetBN(self, bn1); \ GetBN(self, bn1); \
obj = NewBN(CLASS_OF(self)); \
if (!(result = BN_new())) { \ if (!(result = BN_new())) { \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
@ -513,7 +524,7 @@ ossl_bn_div(VALUE self, VALUE other)
BN_free(result); \ BN_free(result); \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
WrapBN(CLASS_OF(self), obj, result); \ SetBN(obj, result); \
return obj; \ return obj; \
} }
@ -602,6 +613,7 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit)
VALUE obj; \ VALUE obj; \
b = NUM2INT(bits); \ b = NUM2INT(bits); \
GetBN(self, bn); \ GetBN(self, bn); \
obj = NewBN(CLASS_OF(self)); \
if (!(result = BN_new())) { \ if (!(result = BN_new())) { \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
@ -609,7 +621,7 @@ ossl_bn_is_bit_set(VALUE self, VALUE bit)
BN_free(result); \ BN_free(result); \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
WrapBN(CLASS_OF(self), obj, result); \ SetBN(obj, result); \
return obj; \ return obj; \
} }
@ -668,6 +680,7 @@ BIGNUM_SELF_SHIFT(rshift)
top = NUM2INT(fill); \ top = NUM2INT(fill); \
} \ } \
b = NUM2INT(bits); \ b = NUM2INT(bits); \
obj = NewBN(klass); \
if (!(result = BN_new())) { \ if (!(result = BN_new())) { \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
@ -675,7 +688,7 @@ BIGNUM_SELF_SHIFT(rshift)
BN_free(result); \ BN_free(result); \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
WrapBN(klass, obj, result); \ SetBN(obj, result); \
return obj; \ return obj; \
} }
@ -696,7 +709,7 @@ BIGNUM_RAND(pseudo_rand)
ossl_bn_s_##func##_range(VALUE klass, VALUE range) \ ossl_bn_s_##func##_range(VALUE klass, VALUE range) \
{ \ { \
BIGNUM *bn = GetBNPtr(range), *result; \ BIGNUM *bn = GetBNPtr(range), *result; \
VALUE obj; \ VALUE obj = NewBN(klass); \
if (!(result = BN_new())) { \ if (!(result = BN_new())) { \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
@ -704,7 +717,7 @@ BIGNUM_RAND(pseudo_rand)
BN_free(result); \ BN_free(result); \
ossl_raise(eBNError, NULL); \ ossl_raise(eBNError, NULL); \
} \ } \
WrapBN(klass, obj, result); \ SetBN(obj, result); \
return obj; \ return obj; \
} }
@ -750,6 +763,7 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
add = GetBNPtr(vadd); add = GetBNPtr(vadd);
rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem); rem = NIL_P(vrem) ? NULL : GetBNPtr(vrem);
} }
obj = NewBN(klass);
if (!(result = BN_new())) { if (!(result = BN_new())) {
ossl_raise(eBNError, NULL); ossl_raise(eBNError, NULL);
} }
@ -757,7 +771,7 @@ ossl_bn_s_generate_prime(int argc, VALUE *argv, VALUE klass)
BN_free(result); BN_free(result);
ossl_raise(eBNError, NULL); ossl_raise(eBNError, NULL);
} }
WrapBN(klass, obj, result); SetBN(obj, result);
return obj; return obj;
} }

View file

@ -10,8 +10,8 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapCipher(obj, klass, ctx) \ #define NewCipher(klass) \
(obj) = TypedData_Wrap_Struct((klass), &ossl_cipher_type, (ctx)) TypedData_Wrap_Struct((klass), &ossl_cipher_type, 0)
#define MakeCipher(obj, klass, ctx) \ #define MakeCipher(obj, klass, ctx) \
(obj) = TypedData_Make_Struct((klass), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)) (obj) = TypedData_Make_Struct((klass), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx))
#define AllocCipher(obj, ctx) \ #define AllocCipher(obj, ctx) \
@ -98,11 +98,7 @@ ossl_cipher_memsize(const void *ptr)
static VALUE static VALUE
ossl_cipher_alloc(VALUE klass) ossl_cipher_alloc(VALUE klass)
{ {
VALUE obj; return NewCipher(klass);
WrapCipher(obj, klass, 0);
return obj;
} }
/* /*

View file

@ -95,13 +95,11 @@ ossl_digest_new(const EVP_MD *md)
static VALUE static VALUE
ossl_digest_alloc(VALUE klass) ossl_digest_alloc(VALUE klass)
{ {
EVP_MD_CTX *ctx; VALUE obj = TypedData_Wrap_Struct(klass, &ossl_digest_type, 0);
VALUE obj; EVP_MD_CTX *ctx = EVP_MD_CTX_create();
ctx = EVP_MD_CTX_create();
if (ctx == NULL) if (ctx == NULL)
ossl_raise(rb_eRuntimeError, "EVP_MD_CTX_create() failed"); ossl_raise(rb_eRuntimeError, "EVP_MD_CTX_create() failed");
obj = TypedData_Wrap_Struct(klass, &ossl_digest_type, ctx); RTYPEDDATA_DATA(obj) = ctx;
return obj; return obj;
} }

View file

@ -12,11 +12,13 @@
#if defined(OSSL_ENGINE_ENABLED) #if defined(OSSL_ENGINE_ENABLED)
#define WrapEngine(klass, obj, engine) do { \ #define NewEngine(klass) \
TypedData_Wrap_Struct((klass), &ossl_engine_type, 0)
#define SetEngine(obj, engine) do { \
if (!(engine)) { \ if (!(engine)) { \
ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \ ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_engine_type, (engine)); \ RTYPEDDATA_DATA(obj) = (engine); \
} while(0) } while(0)
#define GetEngine(obj, engine) do { \ #define GetEngine(obj, engine) do { \
TypedData_Get_Struct((obj), ENGINE, &ossl_engine_type, (engine)); \ TypedData_Get_Struct((obj), ENGINE, &ossl_engine_type, (engine)); \
@ -182,11 +184,12 @@ ossl_engine_s_engines(VALUE klass)
ary = rb_ary_new(); ary = rb_ary_new();
for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)){ for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)){
obj = NewEngine(klass);
/* Need a ref count of two here because of ENGINE_free being /* Need a ref count of two here because of ENGINE_free being
* called internally by OpenSSL when moving to the next ENGINE * called internally by OpenSSL when moving to the next ENGINE
* and by us when releasing the ENGINE reference */ * and by us when releasing the ENGINE reference */
ENGINE_up_ref(e); ENGINE_up_ref(e);
WrapEngine(klass, obj, e); SetEngine(obj, e);
rb_ary_push(ary, obj); rb_ary_push(ary, obj);
} }
@ -213,9 +216,10 @@ ossl_engine_s_by_id(VALUE klass, VALUE id)
StringValue(id); StringValue(id);
ossl_engine_s_load(1, &id, klass); ossl_engine_s_load(1, &id, klass);
obj = NewEngine(klass);
if(!(e = ENGINE_by_id(RSTRING_PTR(id)))) if(!(e = ENGINE_by_id(RSTRING_PTR(id))))
ossl_raise(eEngineError, NULL); ossl_raise(eEngineError, NULL);
WrapEngine(klass, obj, e); SetEngine(obj, e);
if(rb_block_given_p()) rb_yield(obj); if(rb_block_given_p()) rb_yield(obj);
if(!ENGINE_init(e)) if(!ENGINE_init(e))
ossl_raise(eEngineError, NULL); ossl_raise(eEngineError, NULL);
@ -232,10 +236,11 @@ ossl_engine_s_alloc(VALUE klass)
ENGINE *e; ENGINE *e;
VALUE obj; VALUE obj;
obj = NewEngine(klass);
if (!(e = ENGINE_new())) { if (!(e = ENGINE_new())) {
ossl_raise(eEngineError, NULL); ossl_raise(eEngineError, NULL);
} }
WrapEngine(klass, obj, e); SetEngine(obj, e);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapSPKI(klass, obj, spki) do { \ #define NewSPKI(klass) \
TypedData_Wrap_Struct((klass), &ossl_netscape_spki_type, 0)
#define SetSPKI(obj, spki) do { \
if (!(spki)) { \ if (!(spki)) { \
ossl_raise(rb_eRuntimeError, "SPKI wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "SPKI wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_netscape_spki_type, (spki)); \ RTYPEDDATA_DATA(obj) = (spki); \
} while (0) } while (0)
#define GetSPKI(obj, spki) do { \ #define GetSPKI(obj, spki) do { \
TypedData_Get_Struct((obj), NETSCAPE_SPKI, &ossl_netscape_spki_type, (spki)); \ TypedData_Get_Struct((obj), NETSCAPE_SPKI, &ossl_netscape_spki_type, (spki)); \
@ -58,10 +60,11 @@ ossl_spki_alloc(VALUE klass)
NETSCAPE_SPKI *spki; NETSCAPE_SPKI *spki;
VALUE obj; VALUE obj;
obj = NewSPKI(klass);
if (!(spki = NETSCAPE_SPKI_new())) { if (!(spki = NETSCAPE_SPKI_new())) {
ossl_raise(eSPKIError, NULL); ossl_raise(eSPKIError, NULL);
} }
WrapSPKI(klass, obj, spki); SetSPKI(obj, spki);
return obj; return obj;
} }

View file

@ -13,9 +13,11 @@
#if defined(OSSL_OCSP_ENABLED) #if defined(OSSL_OCSP_ENABLED)
#define WrapOCSPReq(klass, obj, req) do { \ #define NewOCSPReq(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_request_type, 0)
#define SetOCSPReq(obj, req) do { \
if(!(req)) ossl_raise(rb_eRuntimeError, "Request wasn't initialized!"); \ if(!(req)) ossl_raise(rb_eRuntimeError, "Request wasn't initialized!"); \
(obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_request_type, (req)); \ RTYPEDDATA_DATA(obj) = (req); \
} while (0) } while (0)
#define GetOCSPReq(obj, req) do { \ #define GetOCSPReq(obj, req) do { \
TypedData_Get_Struct((obj), OCSP_REQUEST, &ossl_ocsp_request_type, (req)); \ TypedData_Get_Struct((obj), OCSP_REQUEST, &ossl_ocsp_request_type, (req)); \
@ -26,9 +28,11 @@
GetOCSPReq((obj), (req)); \ GetOCSPReq((obj), (req)); \
} while (0) } while (0)
#define WrapOCSPRes(klass, obj, res) do { \ #define NewOCSPRes(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_response_type, 0)
#define SetOCSPRes(obj, res) do { \
if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \ if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \
(obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_response_type, (res)); \ RTYPEDDATA_DATA(obj) = (res); \
} while (0) } while (0)
#define GetOCSPRes(obj, res) do { \ #define GetOCSPRes(obj, res) do { \
TypedData_Get_Struct((obj), OCSP_RESPONSE, &ossl_ocsp_response_type, (res)); \ TypedData_Get_Struct((obj), OCSP_RESPONSE, &ossl_ocsp_response_type, (res)); \
@ -39,9 +43,11 @@
GetOCSPRes((obj), (res)); \ GetOCSPRes((obj), (res)); \
} while (0) } while (0)
#define WrapOCSPBasicRes(klass, obj, res) do { \ #define NewOCSPBasicRes(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_basicresp_type, 0)
#define SetOCSPBasicRes(obj, res) do { \
if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \ if(!(res)) ossl_raise(rb_eRuntimeError, "Response wasn't initialized!"); \
(obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_basicresp_type, (res)); \ RTYPEDDATA_DATA(obj) = (res); \
} while (0) } while (0)
#define GetOCSPBasicRes(obj, res) do { \ #define GetOCSPBasicRes(obj, res) do { \
TypedData_Get_Struct((obj), OCSP_BASICRESP, &ossl_ocsp_basicresp_type, (res)); \ TypedData_Get_Struct((obj), OCSP_BASICRESP, &ossl_ocsp_basicresp_type, (res)); \
@ -52,9 +58,11 @@
GetOCSPBasicRes((obj), (res)); \ GetOCSPBasicRes((obj), (res)); \
} while (0) } while (0)
#define WrapOCSPCertId(klass, obj, cid) do { \ #define NewOCSPCertId(klass) \
TypedData_Wrap_Struct((klass), &ossl_ocsp_certid_type, 0)
#define SetOCSPCertId(obj, cid) do { \
if(!(cid)) ossl_raise(rb_eRuntimeError, "Cert ID wasn't initialized!"); \ if(!(cid)) ossl_raise(rb_eRuntimeError, "Cert ID wasn't initialized!"); \
(obj) = TypedData_Wrap_Struct((klass), &ossl_ocsp_certid_type, (cid)); \ RTYPEDDATA_DATA(obj) = (cid); \
} while (0) } while (0)
#define GetOCSPCertId(obj, cid) do { \ #define GetOCSPCertId(obj, cid) do { \
TypedData_Get_Struct((obj), OCSP_CERTID, &ossl_ocsp_certid_type, (cid)); \ TypedData_Get_Struct((obj), OCSP_CERTID, &ossl_ocsp_certid_type, (cid)); \
@ -134,8 +142,8 @@ static const rb_data_type_t ossl_ocsp_certid_type = {
static VALUE static VALUE
ossl_ocspcertid_new(OCSP_CERTID *cid) ossl_ocspcertid_new(OCSP_CERTID *cid)
{ {
VALUE obj; VALUE obj = NewOCSPCertId(cOCSPCertId);
WrapOCSPCertId(cOCSPCertId, obj, cid); SetOCSPCertId(obj, cid);
return obj; return obj;
} }
@ -148,9 +156,10 @@ ossl_ocspreq_alloc(VALUE klass)
OCSP_REQUEST *req; OCSP_REQUEST *req;
VALUE obj; VALUE obj;
obj = NewOCSPReq(klass);
if (!(req = OCSP_REQUEST_new())) if (!(req = OCSP_REQUEST_new()))
ossl_raise(eOCSPError, NULL); ossl_raise(eOCSPError, NULL);
WrapOCSPReq(klass, obj, req); SetOCSPReq(obj, req);
return obj; return obj;
} }
@ -294,9 +303,10 @@ ossl_ocspreq_get_certid(VALUE self)
ary = (count > 0) ? rb_ary_new() : Qnil; ary = (count > 0) ? rb_ary_new() : Qnil;
for(i = 0; i < count; i++){ for(i = 0; i < count; i++){
one = OCSP_request_onereq_get0(req, i); one = OCSP_request_onereq_get0(req, i);
tmp = NewOCSPCertId(cOCSPCertId);
if(!(id = OCSP_CERTID_dup(OCSP_onereq_get0_id(one)))) if(!(id = OCSP_CERTID_dup(OCSP_onereq_get0_id(one))))
ossl_raise(eOCSPError, NULL); ossl_raise(eOCSPError, NULL);
WrapOCSPCertId(cOCSPCertId, tmp, id); SetOCSPCertId(tmp, id);
rb_ary_push(ary, tmp); rb_ary_push(ary, tmp);
} }
@ -415,9 +425,10 @@ ossl_ocspres_s_create(VALUE klass, VALUE status, VALUE basic_resp)
if(NIL_P(basic_resp)) bs = NULL; if(NIL_P(basic_resp)) bs = NULL;
else GetOCSPBasicRes(basic_resp, bs); /* NO NEED TO DUP */ else GetOCSPBasicRes(basic_resp, bs); /* NO NEED TO DUP */
obj = NewOCSPRes(klass);
if(!(res = OCSP_response_create(st, bs))) if(!(res = OCSP_response_create(st, bs)))
ossl_raise(eOCSPError, NULL); ossl_raise(eOCSPError, NULL);
WrapOCSPRes(klass, obj, res); SetOCSPRes(obj, res);
return obj; return obj;
} }
@ -428,9 +439,10 @@ ossl_ocspres_alloc(VALUE klass)
OCSP_RESPONSE *res; OCSP_RESPONSE *res;
VALUE obj; VALUE obj;
obj = NewOCSPRes(klass);
if(!(res = OCSP_RESPONSE_new())) if(!(res = OCSP_RESPONSE_new()))
ossl_raise(eOCSPError, NULL); ossl_raise(eOCSPError, NULL);
WrapOCSPRes(klass, obj, res); SetOCSPRes(obj, res);
return obj; return obj;
} }
@ -519,9 +531,10 @@ ossl_ocspres_get_basic(VALUE self)
VALUE ret; VALUE ret;
GetOCSPRes(self, res); GetOCSPRes(self, res);
ret = NewOCSPBasicRes(cOCSPBasicRes);
if(!(bs = OCSP_response_get1_basic(res))) if(!(bs = OCSP_response_get1_basic(res)))
return Qnil; return Qnil;
WrapOCSPBasicRes(cOCSPBasicRes, ret, bs); SetOCSPBasicRes(ret, bs);
return ret; return ret;
} }
@ -562,9 +575,10 @@ ossl_ocspbres_alloc(VALUE klass)
OCSP_BASICRESP *bs; OCSP_BASICRESP *bs;
VALUE obj; VALUE obj;
obj = NewOCSPBasicRes(klass);
if(!(bs = OCSP_BASICRESP_new())) if(!(bs = OCSP_BASICRESP_new()))
ossl_raise(eOCSPError, NULL); ossl_raise(eOCSPError, NULL);
WrapOCSPBasicRes(klass, obj, bs); SetOCSPBasicRes(obj, bs);
return obj; return obj;
} }
@ -851,9 +865,10 @@ ossl_ocspcid_alloc(VALUE klass)
OCSP_CERTID *id; OCSP_CERTID *id;
VALUE obj; VALUE obj;
obj = NewOCSPCertId(klass);
if(!(id = OCSP_CERTID_new())) if(!(id = OCSP_CERTID_new()))
ossl_raise(eOCSPError, NULL); ossl_raise(eOCSPError, NULL);
WrapOCSPCertId(klass, obj, id); SetOCSPCertId(obj, id);
return obj; return obj;
} }

View file

@ -5,9 +5,12 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapPKCS12(klass, obj, p12) do { \ #define NewPKCS12(klass) \
TypedData_Wrap_Struct((klass), &ossl_pkcs12_type, 0)
#define SetPKCS12(obj, p12) do { \
if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \ if(!(p12)) ossl_raise(rb_eRuntimeError, "PKCS12 wasn't initialized."); \
(obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs12_type, (p12)); \ RTYPEDDATA_DATA(obj) = (p12); \
} while (0) } while (0)
#define GetPKCS12(obj, p12) do { \ #define GetPKCS12(obj, p12) do { \
@ -56,8 +59,9 @@ ossl_pkcs12_s_allocate(VALUE klass)
PKCS12 *p12; PKCS12 *p12;
VALUE obj; VALUE obj;
obj = NewPKCS12(klass);
if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL); if(!(p12 = PKCS12_new())) ossl_raise(ePKCS12Error, NULL);
WrapPKCS12(klass, obj, p12); SetPKCS12(obj, p12);
return obj; return obj;
} }
@ -118,11 +122,12 @@ ossl_pkcs12_s_create(int argc, VALUE *argv, VALUE self)
if (!NIL_P(keytype)) if (!NIL_P(keytype))
ktype = NUM2INT(keytype); ktype = NUM2INT(keytype);
obj = NewPKCS12(cPKCS12);
p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s, p12 = PKCS12_create(passphrase, friendlyname, key, x509, x509s,
nkey, ncert, kiter, miter, ktype); nkey, ncert, kiter, miter, ktype);
sk_X509_pop_free(x509s, X509_free); sk_X509_pop_free(x509s, X509_free);
if(!p12) ossl_raise(ePKCS12Error, NULL); if(!p12) ossl_raise(ePKCS12Error, NULL);
WrapPKCS12(cPKCS12, obj, p12); SetPKCS12(obj, p12);
ossl_pkcs12_set_key(obj, pkey); ossl_pkcs12_set_key(obj, pkey);
ossl_pkcs12_set_cert(obj, cert); ossl_pkcs12_set_cert(obj, cert);

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapPKCS7(klass, obj, pkcs7) do { \ #define NewPKCS7(klass) \
TypedData_Wrap_Struct((klass), &ossl_pkcs7_type, 0)
#define SetPKCS7(obj, pkcs7) do { \
if (!(pkcs7)) { \ if (!(pkcs7)) { \
ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \ ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs7_type, (pkcs7)); \ RTYPEDDATA_DATA(obj) = (pkcs7); \
} while (0) } while (0)
#define GetPKCS7(obj, pkcs7) do { \ #define GetPKCS7(obj, pkcs7) do { \
TypedData_Get_Struct((obj), PKCS7, &ossl_pkcs7_type, (pkcs7)); \ TypedData_Get_Struct((obj), PKCS7, &ossl_pkcs7_type, (pkcs7)); \
@ -27,11 +29,13 @@
GetPKCS7((obj), (pkcs7)); \ GetPKCS7((obj), (pkcs7)); \
} while (0) } while (0)
#define WrapPKCS7si(klass, obj, p7si) do { \ #define NewPKCS7si(klass) \
TypedData_Wrap_Struct((klass), &ossl_pkcs7_signer_info_type, 0)
#define SetPKCS7si(obj, p7si) do { \
if (!(p7si)) { \ if (!(p7si)) { \
ossl_raise(rb_eRuntimeError, "PKCS7si wasn't initialized."); \ ossl_raise(rb_eRuntimeError, "PKCS7si wasn't initialized."); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs7_signer_info_type, (p7si)); \ RTYPEDDATA_DATA(obj) = (p7si); \
} while (0) } while (0)
#define GetPKCS7si(obj, p7si) do { \ #define GetPKCS7si(obj, p7si) do { \
TypedData_Get_Struct((obj), PKCS7_SIGNER_INFO, &ossl_pkcs7_signer_info_type, (p7si)); \ TypedData_Get_Struct((obj), PKCS7_SIGNER_INFO, &ossl_pkcs7_signer_info_type, (p7si)); \
@ -44,11 +48,13 @@
GetPKCS7si((obj), (p7si)); \ GetPKCS7si((obj), (p7si)); \
} while (0) } while (0)
#define WrapPKCS7ri(klass, obj, p7ri) do { \ #define NewPKCS7ri(klass) \
TypedData_Wrap_Struct((klass), &ossl_pkcs7_recip_info_type, 0)
#define SetPKCS7ri(obj, p7ri) do { \
if (!(p7ri)) { \ if (!(p7ri)) { \
ossl_raise(rb_eRuntimeError, "PKCS7ri wasn't initialized."); \ ossl_raise(rb_eRuntimeError, "PKCS7ri wasn't initialized."); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_pkcs7_recip_info_type, (p7ri)); \ RTYPEDDATA_DATA(obj) = (p7ri); \
} while (0) } while (0)
#define GetPKCS7ri(obj, p7ri) do { \ #define GetPKCS7ri(obj, p7ri) do { \
TypedData_Get_Struct((obj), PKCS7_RECIP_INFO, &ossl_pkcs7_recip_info_type, (p7ri)); \ TypedData_Get_Struct((obj), PKCS7_RECIP_INFO, &ossl_pkcs7_recip_info_type, (p7ri)); \
@ -128,9 +134,10 @@ ossl_pkcs7si_new(PKCS7_SIGNER_INFO *p7si)
PKCS7_SIGNER_INFO *pkcs7; PKCS7_SIGNER_INFO *pkcs7;
VALUE obj; VALUE obj;
obj = NewPKCS7si(cPKCS7Signer);
pkcs7 = p7si ? PKCS7_SIGNER_INFO_dup(p7si) : PKCS7_SIGNER_INFO_new(); pkcs7 = p7si ? PKCS7_SIGNER_INFO_dup(p7si) : PKCS7_SIGNER_INFO_new();
if (!pkcs7) ossl_raise(ePKCS7Error, NULL); if (!pkcs7) ossl_raise(ePKCS7Error, NULL);
WrapPKCS7si(cPKCS7Signer, obj, pkcs7); SetPKCS7si(obj, pkcs7);
return obj; return obj;
} }
@ -154,9 +161,10 @@ ossl_pkcs7ri_new(PKCS7_RECIP_INFO *p7ri)
PKCS7_RECIP_INFO *pkcs7; PKCS7_RECIP_INFO *pkcs7;
VALUE obj; VALUE obj;
obj = NewPKCS7ri(cPKCS7Recipient);
pkcs7 = p7ri ? PKCS7_RECIP_INFO_dup(p7ri) : PKCS7_RECIP_INFO_new(); pkcs7 = p7ri ? PKCS7_RECIP_INFO_dup(p7ri) : PKCS7_RECIP_INFO_new();
if (!pkcs7) ossl_raise(ePKCS7Error, NULL); if (!pkcs7) ossl_raise(ePKCS7Error, NULL);
WrapPKCS7ri(cPKCS7Recipient, obj, pkcs7); SetPKCS7ri(obj, pkcs7);
return obj; return obj;
} }
@ -185,13 +193,14 @@ ossl_pkcs7_s_read_smime(VALUE klass, VALUE arg)
PKCS7 *pkcs7; PKCS7 *pkcs7;
VALUE ret, data; VALUE ret, data;
ret = NewPKCS7(cPKCS7);
in = ossl_obj2bio(arg); in = ossl_obj2bio(arg);
out = NULL; out = NULL;
pkcs7 = SMIME_read_PKCS7(in, &out); pkcs7 = SMIME_read_PKCS7(in, &out);
BIO_free(in); BIO_free(in);
if(!pkcs7) ossl_raise(ePKCS7Error, NULL); if(!pkcs7) ossl_raise(ePKCS7Error, NULL);
data = out ? ossl_membio2str(out) : Qnil; data = out ? ossl_membio2str(out) : Qnil;
WrapPKCS7(cPKCS7, ret, pkcs7); SetPKCS7(ret, pkcs7);
ossl_pkcs7_set_data(ret, data); ossl_pkcs7_set_data(ret, data);
ossl_pkcs7_set_err_string(ret, Qnil); ossl_pkcs7_set_err_string(ret, Qnil);
@ -253,6 +262,7 @@ ossl_pkcs7_s_sign(int argc, VALUE *argv, VALUE klass)
x509 = GetX509CertPtr(cert); /* NO NEED TO DUP */ x509 = GetX509CertPtr(cert); /* NO NEED TO DUP */
pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */ pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
flg = NIL_P(flags) ? 0 : NUM2INT(flags); flg = NIL_P(flags) ? 0 : NUM2INT(flags);
ret = NewPKCS7(cPKCS7);
in = ossl_obj2bio(data); in = ossl_obj2bio(data);
if(NIL_P(certs)) x509s = NULL; if(NIL_P(certs)) x509s = NULL;
else{ else{
@ -267,7 +277,7 @@ ossl_pkcs7_s_sign(int argc, VALUE *argv, VALUE klass)
sk_X509_pop_free(x509s, X509_free); sk_X509_pop_free(x509s, X509_free);
ossl_raise(ePKCS7Error, NULL); ossl_raise(ePKCS7Error, NULL);
} }
WrapPKCS7(cPKCS7, ret, pkcs7); SetPKCS7(ret, pkcs7);
ossl_pkcs7_set_data(ret, data); ossl_pkcs7_set_data(ret, data);
ossl_pkcs7_set_err_string(ret, Qnil); ossl_pkcs7_set_err_string(ret, Qnil);
BIO_free(in); BIO_free(in);
@ -308,6 +318,7 @@ ossl_pkcs7_s_encrypt(int argc, VALUE *argv, VALUE klass)
} }
else ciph = GetCipherPtr(cipher); /* NO NEED TO DUP */ else ciph = GetCipherPtr(cipher); /* NO NEED TO DUP */
flg = NIL_P(flags) ? 0 : NUM2INT(flags); flg = NIL_P(flags) ? 0 : NUM2INT(flags);
ret = NewPKCS7(cPKCS7);
in = ossl_obj2bio(data); in = ossl_obj2bio(data);
x509s = ossl_protect_x509_ary2sk(certs, &status); x509s = ossl_protect_x509_ary2sk(certs, &status);
if(status){ if(status){
@ -320,7 +331,7 @@ ossl_pkcs7_s_encrypt(int argc, VALUE *argv, VALUE klass)
ossl_raise(ePKCS7Error, NULL); ossl_raise(ePKCS7Error, NULL);
} }
BIO_free(in); BIO_free(in);
WrapPKCS7(cPKCS7, ret, p7); SetPKCS7(ret, p7);
ossl_pkcs7_set_data(ret, data); ossl_pkcs7_set_data(ret, data);
sk_X509_pop_free(x509s, X509_free); sk_X509_pop_free(x509s, X509_free);
@ -333,10 +344,11 @@ ossl_pkcs7_alloc(VALUE klass)
PKCS7 *pkcs7; PKCS7 *pkcs7;
VALUE obj; VALUE obj;
obj = NewPKCS7(klass);
if (!(pkcs7 = PKCS7_new())) { if (!(pkcs7 = PKCS7_new())) {
ossl_raise(ePKCS7Error, NULL); ossl_raise(ePKCS7Error, NULL);
} }
WrapPKCS7(klass, obj, pkcs7); SetPKCS7(obj, pkcs7);
return obj; return obj;
} }
@ -886,10 +898,11 @@ ossl_pkcs7si_alloc(VALUE klass)
PKCS7_SIGNER_INFO *p7si; PKCS7_SIGNER_INFO *p7si;
VALUE obj; VALUE obj;
obj = NewPKCS7si(klass);
if (!(p7si = PKCS7_SIGNER_INFO_new())) { if (!(p7si = PKCS7_SIGNER_INFO_new())) {
ossl_raise(ePKCS7Error, NULL); ossl_raise(ePKCS7Error, NULL);
} }
WrapPKCS7si(klass, obj, p7si); SetPKCS7si(obj, p7si);
return obj; return obj;
} }
@ -965,10 +978,11 @@ ossl_pkcs7ri_alloc(VALUE klass)
PKCS7_RECIP_INFO *p7ri; PKCS7_RECIP_INFO *p7ri;
VALUE obj; VALUE obj;
obj = NewPKCS7ri(klass);
if (!(p7ri = PKCS7_RECIP_INFO_new())) { if (!(p7ri = PKCS7_RECIP_INFO_new())) {
ossl_raise(ePKCS7Error, NULL); ossl_raise(ePKCS7Error, NULL);
} }
WrapPKCS7ri(klass, obj, p7ri); SetPKCS7ri(obj, p7ri);
return obj; return obj;
} }

View file

@ -241,10 +241,11 @@ ossl_pkey_alloc(VALUE klass)
EVP_PKEY *pkey; EVP_PKEY *pkey;
VALUE obj; VALUE obj;
obj = NewPKey(klass);
if (!(pkey = EVP_PKEY_new())) { if (!(pkey = EVP_PKEY_new())) {
ossl_raise(ePKeyError, NULL); ossl_raise(ePKeyError, NULL);
} }
WrapPKey(klass, obj, pkey); SetPKey(obj, pkey);
return obj; return obj;
} }

View file

@ -21,11 +21,13 @@ extern const rb_data_type_t ossl_evp_pkey_type;
#define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse) #define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
#define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue) #define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
#define WrapPKey(klass, obj, pkey) do { \ #define NewPKey(klass) \
TypedData_Wrap_Struct((klass), &ossl_evp_pkey_type, 0)
#define SetPKey(obj, pkey) do { \
if (!(pkey)) { \ if (!(pkey)) { \
rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \ rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_evp_pkey_type, (pkey)); \ RTYPEDDATA_DATA(obj) = (pkey); \
OSSL_PKEY_SET_PUBLIC(obj); \ OSSL_PKEY_SET_PUBLIC(obj); \
} while (0) } while (0)
#define GetPKey(obj, pkey) do {\ #define GetPKey(obj, pkey) do {\

View file

@ -46,6 +46,7 @@ dh_instance(VALUE klass, DH *dh)
if (!dh) { if (!dh) {
return Qfalse; return Qfalse;
} }
obj = NewPKey(klass);
if (!(pkey = EVP_PKEY_new())) { if (!(pkey = EVP_PKEY_new())) {
return Qfalse; return Qfalse;
} }
@ -53,7 +54,7 @@ dh_instance(VALUE klass, DH *dh)
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
return Qfalse; return Qfalse;
} }
WrapPKey(klass, obj, pkey); SetPKey(obj, pkey);
return obj; return obj;
} }
@ -66,10 +67,11 @@ ossl_dh_new(EVP_PKEY *pkey)
if (!pkey) { if (!pkey) {
obj = dh_instance(cDH, DH_new()); obj = dh_instance(cDH, DH_new());
} else { } else {
obj = NewPKey(cDH);
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) { if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) {
ossl_raise(rb_eTypeError, "Not a DH key!"); ossl_raise(rb_eTypeError, "Not a DH key!");
} }
WrapPKey(cDH, obj, pkey); SetPKey(obj, pkey);
} }
if (obj == Qfalse) { if (obj == Qfalse) {
ossl_raise(eDHError, NULL); ossl_raise(eDHError, NULL);

View file

@ -40,6 +40,7 @@ dsa_instance(VALUE klass, DSA *dsa)
if (!dsa) { if (!dsa) {
return Qfalse; return Qfalse;
} }
obj = NewPKey(klass);
if (!(pkey = EVP_PKEY_new())) { if (!(pkey = EVP_PKEY_new())) {
return Qfalse; return Qfalse;
} }
@ -47,7 +48,7 @@ dsa_instance(VALUE klass, DSA *dsa)
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
return Qfalse; return Qfalse;
} }
WrapPKey(klass, obj, pkey); SetPKey(obj, pkey);
return obj; return obj;
} }
@ -60,10 +61,11 @@ ossl_dsa_new(EVP_PKEY *pkey)
if (!pkey) { if (!pkey) {
obj = dsa_instance(cDSA, DSA_new()); obj = dsa_instance(cDSA, DSA_new());
} else { } else {
obj = NewPKey(cDSA);
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DSA) { if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DSA) {
ossl_raise(rb_eTypeError, "Not a DSA key!"); ossl_raise(rb_eTypeError, "Not a DSA key!");
} }
WrapPKey(cDSA, obj, pkey); SetPKey(obj, pkey);
} }
if (obj == Qfalse) { if (obj == Qfalse) {
ossl_raise(eDSAError, NULL); ossl_raise(eDSAError, NULL);

View file

@ -116,6 +116,7 @@ static VALUE ec_instance(VALUE klass, EC_KEY *ec)
if (!ec) { if (!ec) {
return Qfalse; return Qfalse;
} }
obj = NewPKey(klass);
if (!(pkey = EVP_PKEY_new())) { if (!(pkey = EVP_PKEY_new())) {
return Qfalse; return Qfalse;
} }
@ -123,7 +124,7 @@ static VALUE ec_instance(VALUE klass, EC_KEY *ec)
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
return Qfalse; return Qfalse;
} }
WrapPKey(klass, obj, pkey); SetPKey(obj, pkey);
return obj; return obj;
} }
@ -135,10 +136,11 @@ VALUE ossl_ec_new(EVP_PKEY *pkey)
if (!pkey) { if (!pkey) {
obj = ec_instance(cEC, EC_KEY_new()); obj = ec_instance(cEC, EC_KEY_new());
} else { } else {
obj = NewPKey(cEC);
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_EC) { if (EVP_PKEY_type(pkey->type) != EVP_PKEY_EC) {
ossl_raise(rb_eTypeError, "Not a EC key!"); ossl_raise(rb_eTypeError, "Not a EC key!");
} }
WrapPKey(cEC, obj, pkey); SetPKey(obj, pkey);
} }
if (obj == Qfalse) { if (obj == Qfalse) {
ossl_raise(eECError, NULL); ossl_raise(eECError, NULL);

View file

@ -40,6 +40,7 @@ rsa_instance(VALUE klass, RSA *rsa)
if (!rsa) { if (!rsa) {
return Qfalse; return Qfalse;
} }
obj = NewPKey(klass);
if (!(pkey = EVP_PKEY_new())) { if (!(pkey = EVP_PKEY_new())) {
return Qfalse; return Qfalse;
} }
@ -47,7 +48,7 @@ rsa_instance(VALUE klass, RSA *rsa)
EVP_PKEY_free(pkey); EVP_PKEY_free(pkey);
return Qfalse; return Qfalse;
} }
WrapPKey(klass, obj, pkey); SetPKey(obj, pkey);
return obj; return obj;
} }
@ -61,10 +62,11 @@ ossl_rsa_new(EVP_PKEY *pkey)
obj = rsa_instance(cRSA, RSA_new()); obj = rsa_instance(cRSA, RSA_new());
} }
else { else {
obj = NewPKey(cRSA);
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA) { if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA) {
ossl_raise(rb_eTypeError, "Not a RSA key!"); ossl_raise(rb_eTypeError, "Not a RSA key!");
} }
WrapPKey(cRSA, obj, pkey); SetPKey(obj, pkey);
} }
if (obj == Qfalse) { if (obj == Qfalse) {
ossl_raise(eRSAError, NULL); ossl_raise(eRSAError, NULL);

View file

@ -175,17 +175,20 @@ ossl_sslctx_s_alloc(VALUE klass)
{ {
SSL_CTX *ctx; SSL_CTX *ctx;
long mode = SSL_MODE_ENABLE_PARTIAL_WRITE; long mode = SSL_MODE_ENABLE_PARTIAL_WRITE;
VALUE obj;
#ifdef SSL_MODE_RELEASE_BUFFERS #ifdef SSL_MODE_RELEASE_BUFFERS
mode |= SSL_MODE_RELEASE_BUFFERS; mode |= SSL_MODE_RELEASE_BUFFERS;
#endif #endif
obj = TypedData_Wrap_Struct(klass, &ossl_sslctx_type, 0);
ctx = SSL_CTX_new(SSLv23_method()); ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) { if (!ctx) {
ossl_raise(eSSLError, "SSL_CTX_new"); ossl_raise(eSSLError, "SSL_CTX_new");
} }
SSL_CTX_set_mode(ctx, mode); SSL_CTX_set_mode(ctx, mode);
return TypedData_Wrap_Struct(klass, &ossl_sslctx_type, ctx); RTYPEDDATA_DATA(obj) = ctx;
return obj;
} }
/* /*

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509Attr(klass, obj, attr) do { \ #define NewX509Attr(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509attr_type, 0)
#define SetX509Attr(obj, attr) do { \
if (!(attr)) { \ if (!(attr)) { \
ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509attr_type, (attr)); \ RTYPEDDATA_DATA(obj) = (attr); \
} while (0) } while (0)
#define GetX509Attr(obj, attr) do { \ #define GetX509Attr(obj, attr) do { \
TypedData_Get_Struct((obj), X509_ATTRIBUTE, &ossl_x509attr_type, (attr)); \ TypedData_Get_Struct((obj), X509_ATTRIBUTE, &ossl_x509attr_type, (attr)); \
@ -56,6 +58,7 @@ ossl_x509attr_new(X509_ATTRIBUTE *attr)
X509_ATTRIBUTE *new; X509_ATTRIBUTE *new;
VALUE obj; VALUE obj;
obj = NewX509Attr(cX509Attr);
if (!attr) { if (!attr) {
new = X509_ATTRIBUTE_new(); new = X509_ATTRIBUTE_new();
} else { } else {
@ -64,7 +67,7 @@ ossl_x509attr_new(X509_ATTRIBUTE *attr)
if (!new) { if (!new) {
ossl_raise(eX509AttrError, NULL); ossl_raise(eX509AttrError, NULL);
} }
WrapX509Attr(cX509Attr, obj, new); SetX509Attr(obj, new);
return obj; return obj;
} }
@ -91,9 +94,10 @@ ossl_x509attr_alloc(VALUE klass)
X509_ATTRIBUTE *attr; X509_ATTRIBUTE *attr;
VALUE obj; VALUE obj;
obj = NewX509Attr(klass);
if (!(attr = X509_ATTRIBUTE_new())) if (!(attr = X509_ATTRIBUTE_new()))
ossl_raise(eX509AttrError, NULL); ossl_raise(eX509AttrError, NULL);
WrapX509Attr(klass, obj, attr); SetX509Attr(obj, attr);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509(klass, obj, x509) do { \ #define NewX509(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509_type, 0)
#define SetX509(obj, x509) do { \
if (!(x509)) { \ if (!(x509)) { \
ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509_type, (x509)); \ RTYPEDDATA_DATA(obj) = (x509); \
} while (0) } while (0)
#define GetX509(obj, x509) do { \ #define GetX509(obj, x509) do { \
TypedData_Get_Struct((obj), X509, &ossl_x509_type, (x509)); \ TypedData_Get_Struct((obj), X509, &ossl_x509_type, (x509)); \
@ -56,6 +58,7 @@ ossl_x509_new(X509 *x509)
X509 *new; X509 *new;
VALUE obj; VALUE obj;
obj = NewX509(cX509Cert);
if (!x509) { if (!x509) {
new = X509_new(); new = X509_new();
} else { } else {
@ -64,7 +67,7 @@ ossl_x509_new(X509 *x509)
if (!new) { if (!new) {
ossl_raise(eX509CertError, NULL); ossl_raise(eX509CertError, NULL);
} }
WrapX509(cX509Cert, obj, new); SetX509(obj, new);
return obj; return obj;
} }
@ -77,6 +80,7 @@ ossl_x509_new_from_file(VALUE filename)
VALUE obj; VALUE obj;
SafeStringValue(filename); SafeStringValue(filename);
obj = NewX509(cX509Cert);
if (!(fp = fopen(RSTRING_PTR(filename), "r"))) { if (!(fp = fopen(RSTRING_PTR(filename), "r"))) {
ossl_raise(eX509CertError, "%s", strerror(errno)); ossl_raise(eX509CertError, "%s", strerror(errno));
} }
@ -97,7 +101,7 @@ ossl_x509_new_from_file(VALUE filename)
if (!x509) { if (!x509) {
ossl_raise(eX509CertError, NULL); ossl_raise(eX509CertError, NULL);
} }
WrapX509(cX509Cert, obj, x509); SetX509(obj, x509);
return obj; return obj;
} }
@ -133,10 +137,10 @@ ossl_x509_alloc(VALUE klass)
X509 *x509; X509 *x509;
VALUE obj; VALUE obj;
obj = NewX509(klass);
x509 = X509_new(); x509 = X509_new();
if (!x509) ossl_raise(eX509CertError, NULL); if (!x509) ossl_raise(eX509CertError, NULL);
SetX509(obj, x509);
WrapX509(klass, obj, x509);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509CRL(klass, obj, crl) do { \ #define NewX509CRL(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509crl_type, 0)
#define SetX509CRL(obj, crl) do { \
if (!(crl)) { \ if (!(crl)) { \
ossl_raise(rb_eRuntimeError, "CRL wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "CRL wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509crl_type, (crl)); \ RTYPEDDATA_DATA(obj) = (crl); \
} while (0) } while (0)
#define GetX509CRL(obj, crl) do { \ #define GetX509CRL(obj, crl) do { \
TypedData_Get_Struct((obj), X509_CRL, &ossl_x509crl_type, (crl)); \ TypedData_Get_Struct((obj), X509_CRL, &ossl_x509crl_type, (crl)); \
@ -77,9 +79,10 @@ ossl_x509crl_new(X509_CRL *crl)
X509_CRL *tmp; X509_CRL *tmp;
VALUE obj; VALUE obj;
obj = NewX509CRL(cX509CRL);
tmp = crl ? X509_CRL_dup(crl) : X509_CRL_new(); tmp = crl ? X509_CRL_dup(crl) : X509_CRL_new();
if(!tmp) ossl_raise(eX509CRLError, NULL); if(!tmp) ossl_raise(eX509CRLError, NULL);
WrapX509CRL(cX509CRL, obj, tmp); SetX509CRL(obj, tmp);
return obj; return obj;
} }
@ -93,10 +96,11 @@ ossl_x509crl_alloc(VALUE klass)
X509_CRL *crl; X509_CRL *crl;
VALUE obj; VALUE obj;
obj = NewX509CRL(klass);
if (!(crl = X509_CRL_new())) { if (!(crl = X509_CRL_new())) {
ossl_raise(eX509CRLError, NULL); ossl_raise(eX509CRLError, NULL);
} }
WrapX509CRL(klass, obj, crl); SetX509CRL(obj, crl);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509Ext(klass, obj, ext) do { \ #define NewX509Ext(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509ext_type, 0)
#define SetX509Ext(obj, ext) do { \
if (!(ext)) { \ if (!(ext)) { \
ossl_raise(rb_eRuntimeError, "EXT wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "EXT wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509ext_type, (ext)); \ RTYPEDDATA_DATA(obj) = (ext); \
} while (0) } while (0)
#define GetX509Ext(obj, ext) do { \ #define GetX509Ext(obj, ext) do { \
TypedData_Get_Struct((obj), X509_EXTENSION, &ossl_x509ext_type, (ext)); \ TypedData_Get_Struct((obj), X509_EXTENSION, &ossl_x509ext_type, (ext)); \
@ -27,10 +29,11 @@
GetX509Ext((obj), (ext)); \ GetX509Ext((obj), (ext)); \
} while (0) } while (0)
#define MakeX509ExtFactory(klass, obj, ctx) do { \ #define MakeX509ExtFactory(klass, obj, ctx) do { \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509extfactory_type, 0); \
if (!((ctx) = OPENSSL_malloc(sizeof(X509V3_CTX)))) \ if (!((ctx) = OPENSSL_malloc(sizeof(X509V3_CTX)))) \
ossl_raise(rb_eRuntimeError, "CTX wasn't allocated!"); \ ossl_raise(rb_eRuntimeError, "CTX wasn't allocated!"); \
X509V3_set_ctx((ctx), NULL, NULL, NULL, NULL, 0); \ X509V3_set_ctx((ctx), NULL, NULL, NULL, NULL, 0); \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509extfactory_type, (ctx)); \ RTYPEDDATA_DATA(obj) = (ctx); \
} while (0) } while (0)
#define GetX509ExtFactory(obj, ctx) do { \ #define GetX509ExtFactory(obj, ctx) do { \
TypedData_Get_Struct((obj), X509V3_CTX, &ossl_x509extfactory_type, (ctx)); \ TypedData_Get_Struct((obj), X509V3_CTX, &ossl_x509extfactory_type, (ctx)); \
@ -69,6 +72,7 @@ ossl_x509ext_new(X509_EXTENSION *ext)
X509_EXTENSION *new; X509_EXTENSION *new;
VALUE obj; VALUE obj;
obj = NewX509Ext(cX509Ext);
if (!ext) { if (!ext) {
new = X509_EXTENSION_new(); new = X509_EXTENSION_new();
} else { } else {
@ -77,7 +81,7 @@ ossl_x509ext_new(X509_EXTENSION *ext)
if (!new) { if (!new) {
ossl_raise(eX509ExtError, NULL); ossl_raise(eX509ExtError, NULL);
} }
WrapX509Ext(cX509Ext, obj, new); SetX509Ext(obj, new);
return obj; return obj;
} }
@ -258,6 +262,7 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
valstr = rb_str_new2(RTEST(critical) ? "critical," : ""); valstr = rb_str_new2(RTEST(critical) ? "critical," : "");
rb_str_append(valstr, value); rb_str_append(valstr, value);
GetX509ExtFactory(self, ctx); GetX509ExtFactory(self, ctx);
obj = NewX509Ext(cX509Ext);
#ifdef HAVE_X509V3_EXT_NCONF_NID #ifdef HAVE_X509V3_EXT_NCONF_NID
rconf = rb_iv_get(self, "@config"); rconf = rb_iv_get(self, "@config");
conf = NIL_P(rconf) ? NULL : GetConfigPtr(rconf); conf = NIL_P(rconf) ? NULL : GetConfigPtr(rconf);
@ -270,7 +275,7 @@ ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
ossl_raise(eX509ExtError, "%s = %s", ossl_raise(eX509ExtError, "%s = %s",
RSTRING_PTR(oid), RSTRING_PTR(value)); RSTRING_PTR(oid), RSTRING_PTR(value));
} }
WrapX509Ext(cX509Ext, obj, ext); SetX509Ext(obj, ext);
return obj; return obj;
} }
@ -284,10 +289,11 @@ ossl_x509ext_alloc(VALUE klass)
X509_EXTENSION *ext; X509_EXTENSION *ext;
VALUE obj; VALUE obj;
obj = NewX509Ext(klass);
if(!(ext = X509_EXTENSION_new())){ if(!(ext = X509_EXTENSION_new())){
ossl_raise(eX509ExtError, NULL); ossl_raise(eX509ExtError, NULL);
} }
WrapX509Ext(klass, obj, ext); SetX509Ext(obj, ext);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509Name(klass, obj, name) do { \ #define NewX509Name(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509name_type, 0)
#define SetX509Name(obj, name) do { \
if (!(name)) { \ if (!(name)) { \
ossl_raise(rb_eRuntimeError, "Name wasn't initialized."); \ ossl_raise(rb_eRuntimeError, "Name wasn't initialized."); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509name_type, (name)); \ RTYPEDDATA_DATA(obj) = (name); \
} while (0) } while (0)
#define GetX509Name(obj, name) do { \ #define GetX509Name(obj, name) do { \
TypedData_Get_Struct((obj), X509_NAME, &ossl_x509name_type, (name)); \ TypedData_Get_Struct((obj), X509_NAME, &ossl_x509name_type, (name)); \
@ -61,6 +63,7 @@ ossl_x509name_new(X509_NAME *name)
X509_NAME *new; X509_NAME *new;
VALUE obj; VALUE obj;
obj = NewX509Name(cX509Name);
if (!name) { if (!name) {
new = X509_NAME_new(); new = X509_NAME_new();
} else { } else {
@ -69,7 +72,7 @@ ossl_x509name_new(X509_NAME *name)
if (!new) { if (!new) {
ossl_raise(eX509NameError, NULL); ossl_raise(eX509NameError, NULL);
} }
WrapX509Name(cX509Name, obj, new); SetX509Name(obj, new);
return obj; return obj;
} }
@ -93,10 +96,11 @@ ossl_x509name_alloc(VALUE klass)
X509_NAME *name; X509_NAME *name;
VALUE obj; VALUE obj;
obj = NewX509Name(klass);
if (!(name = X509_NAME_new())) { if (!(name = X509_NAME_new())) {
ossl_raise(eX509NameError, NULL); ossl_raise(eX509NameError, NULL);
} }
WrapX509Name(klass, obj, name); SetX509Name(obj, name);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509Req(klass, obj, req) do { \ #define NewX509Req(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509req_type, 0)
#define SetX509Req(obj, req) do { \
if (!(req)) { \ if (!(req)) { \
ossl_raise(rb_eRuntimeError, "Req wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "Req wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509req_type, (req)); \ RTYPEDDATA_DATA(obj) = (req); \
} while (0) } while (0)
#define GetX509Req(obj, req) do { \ #define GetX509Req(obj, req) do { \
TypedData_Get_Struct((obj), X509_REQ, &ossl_x509req_type, (req)); \ TypedData_Get_Struct((obj), X509_REQ, &ossl_x509req_type, (req)); \
@ -56,6 +58,7 @@ ossl_x509req_new(X509_REQ *req)
X509_REQ *new; X509_REQ *new;
VALUE obj; VALUE obj;
obj = NewX509Req(cX509Req);
if (!req) { if (!req) {
new = X509_REQ_new(); new = X509_REQ_new();
} else { } else {
@ -64,7 +67,7 @@ ossl_x509req_new(X509_REQ *req)
if (!new) { if (!new) {
ossl_raise(eX509ReqError, NULL); ossl_raise(eX509ReqError, NULL);
} }
WrapX509Req(cX509Req, obj, new); SetX509Req(obj, new);
return obj; return obj;
} }
@ -101,10 +104,11 @@ ossl_x509req_alloc(VALUE klass)
X509_REQ *req; X509_REQ *req;
VALUE obj; VALUE obj;
obj = NewX509Req(klass);
if (!(req = X509_REQ_new())) { if (!(req = X509_REQ_new())) {
ossl_raise(eX509ReqError, NULL); ossl_raise(eX509ReqError, NULL);
} }
WrapX509Req(klass, obj, req); SetX509Req(obj, req);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509Rev(klass, obj, rev) do { \ #define NewX509Rev(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509rev_type, 0)
#define SetX509Rev(obj, rev) do { \
if (!(rev)) { \ if (!(rev)) { \
ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509rev_type, (rev)); \ RTYPEDDATA_DATA(obj) = (rev); \
} while (0) } while (0)
#define GetX509Rev(obj, rev) do { \ #define GetX509Rev(obj, rev) do { \
TypedData_Get_Struct((obj), X509_REVOKED, &ossl_x509rev_type, (rev)); \ TypedData_Get_Struct((obj), X509_REVOKED, &ossl_x509rev_type, (rev)); \
@ -56,6 +58,7 @@ ossl_x509revoked_new(X509_REVOKED *rev)
X509_REVOKED *new; X509_REVOKED *new;
VALUE obj; VALUE obj;
obj = NewX509Rev(cX509Rev);
if (!rev) { if (!rev) {
new = X509_REVOKED_new(); new = X509_REVOKED_new();
} else { } else {
@ -64,7 +67,7 @@ ossl_x509revoked_new(X509_REVOKED *rev)
if (!new) { if (!new) {
ossl_raise(eX509RevError, NULL); ossl_raise(eX509RevError, NULL);
} }
WrapX509Rev(cX509Rev, obj, new); SetX509Rev(obj, new);
return obj; return obj;
} }
@ -91,10 +94,11 @@ ossl_x509revoked_alloc(VALUE klass)
X509_REVOKED *rev; X509_REVOKED *rev;
VALUE obj; VALUE obj;
obj = NewX509Rev(klass);
if (!(rev = X509_REVOKED_new())) { if (!(rev = X509_REVOKED_new())) {
ossl_raise(eX509RevError, NULL); ossl_raise(eX509RevError, NULL);
} }
WrapX509Rev(klass, obj, rev); SetX509Rev(obj, rev);
return obj; return obj;
} }

View file

@ -10,11 +10,13 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapX509Store(klass, obj, st) do { \ #define NewX509Store(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509store_type, 0)
#define SetX509Store(obj, st) do { \
if (!(st)) { \ if (!(st)) { \
ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509store_type, (st)); \ RTYPEDDATA_DATA(obj) = (st); \
} while (0) } while (0)
#define GetX509Store(obj, st) do { \ #define GetX509Store(obj, st) do { \
TypedData_Get_Struct((obj), X509_STORE, &ossl_x509store_type, (st)); \ TypedData_Get_Struct((obj), X509_STORE, &ossl_x509store_type, (st)); \
@ -27,11 +29,13 @@
GetX509Store((obj), (st)); \ GetX509Store((obj), (st)); \
} while (0) } while (0)
#define WrapX509StCtx(klass, obj, ctx) do { \ #define NewX509StCtx(klass) \
TypedData_Wrap_Struct((klass), &ossl_x509stctx_type, 0)
#define SetX509StCtx(obj, ctx) do { \
if (!(ctx)) { \ if (!(ctx)) { \
ossl_raise(rb_eRuntimeError, "STORE_CTX wasn't initialized!"); \ ossl_raise(rb_eRuntimeError, "STORE_CTX wasn't initialized!"); \
} \ } \
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509stctx_type, (ctx)); \ RTYPEDDATA_DATA(obj) = (ctx); \
} while (0) } while (0)
#define GetX509StCtx(obj, ctx) do { \ #define GetX509StCtx(obj, ctx) do { \
TypedData_Get_Struct((obj), X509_STORE_CTX, &ossl_x509stctx_type, (ctx)); \ TypedData_Get_Struct((obj), X509_STORE_CTX, &ossl_x509stctx_type, (ctx)); \
@ -73,7 +77,8 @@ ossl_x509store_new(X509_STORE *store)
{ {
VALUE obj; VALUE obj;
WrapX509Store(cX509Store, obj, store); obj = NewX509Store(cX509Store);
SetX509Store(obj, store);
return obj; return obj;
} }
@ -108,10 +113,11 @@ ossl_x509store_alloc(VALUE klass)
X509_STORE *store; X509_STORE *store;
VALUE obj; VALUE obj;
obj = NewX509Store(klass);
if((store = X509_STORE_new()) == NULL){ if((store = X509_STORE_new()) == NULL){
ossl_raise(eX509StoreError, NULL); ossl_raise(eX509StoreError, NULL);
} }
WrapX509Store(klass, obj, store); SetX509Store(obj, store);
return obj; return obj;
} }
@ -373,7 +379,8 @@ ossl_x509stctx_new(X509_STORE_CTX *ctx)
{ {
VALUE obj; VALUE obj;
WrapX509StCtx(cX509StoreContext, obj, ctx); obj = NewX509StCtx(cX509StoreContext);
SetX509StCtx(obj, ctx);
return obj; return obj;
} }
@ -407,10 +414,11 @@ ossl_x509stctx_alloc(VALUE klass)
X509_STORE_CTX *ctx; X509_STORE_CTX *ctx;
VALUE obj; VALUE obj;
obj = NewX509StCtx(klass);
if((ctx = X509_STORE_CTX_new()) == NULL){ if((ctx = X509_STORE_CTX_new()) == NULL){
ossl_raise(eX509StoreError, NULL); ossl_raise(eX509StoreError, NULL);
} }
WrapX509StCtx(klass, obj, ctx); SetX509StCtx(obj, ctx);
return obj; return obj;
} }