diff options
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | ossl_cipher.c | 132 | ||||
-rw-r--r-- | ossl_digest.c | 12 | ||||
-rw-r--r-- | ossl_hmac.c | 6 | ||||
-rw-r--r-- | ossl_pkcs7.c | 27 | ||||
-rw-r--r-- | ossl_x509cert.c | 2 |
6 files changed, 112 insertions, 77 deletions
@@ -1,3 +1,13 @@ +Sun, 20 Apr 2003 22:26:56 +0200 -- Michal Rokos <michal@rokos.homeip.net> + * ossl_pkcs7.c: Added #copy_object for Ruby1.8 dup© framework + +Sat, 19 Apr 2003 22:06:02 +0200 -- Michal Rokos <michal@rokos.homeip.net> + * ossl_cipher.c: Unwrap ossl struct - use pure EVP_CIPHER_CTX + * ossl_cipher.c: Fix #copy_object + +Sat, 19 Apr 2003 10:51:47 +0200 -- Michal Rokos <michal@rokos.homeip.net> + * ossl_hmac.c: Fix subclassing... + Mon, 14 Apr 2003 11:53:54 +0200 -- Michal Rokos <michal@rokos.homeip.net> * ossl_pkey_{rsa|dsa|dh}.c: method #params for inspection of private and public numbers diff --git a/ossl_cipher.c b/ossl_cipher.c index 7210583..45f7ff0 100644 --- a/ossl_cipher.c +++ b/ossl_cipher.c @@ -10,16 +10,17 @@ */ #include "ossl.h" -#define MakeCipher(obj, klass, ciphp) obj = Data_Make_Struct(klass, ossl_cipher, 0, ossl_cipher_free, ciphp) -#define GetCipher(obj, ciphp) do { \ - Data_Get_Struct(obj, ossl_cipher, ciphp); \ - if (!ciphp) { \ +#define MakeCipher(obj, klass, ctx) \ + obj = Data_Make_Struct(klass, EVP_CIPHER_CTX, 0, ossl_cipher_free, ctx) +#define GetCipher(obj, ctx) do { \ + Data_Get_Struct(obj, EVP_CIPHER_CTX, ctx); \ + if (!ctx) { \ ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \ } \ } while (0) -#define SafeGetCipher(obj, ciphp) do { \ +#define SafeGetCipher(obj, ctx) do { \ OSSL_Check_Kind(obj, cCipher); \ - GetCipher(obj, ciphp); \ + GetCipher(obj, ctx); \ } while (0) /* @@ -30,44 +31,37 @@ VALUE cCipher; VALUE eCipherError; /* - * Struct - */ -typedef struct ossl_cipher_st { - EVP_CIPHER_CTX ctx; -} ossl_cipher; - -static void -ossl_cipher_free(ossl_cipher *ciphp) -{ - if (ciphp) { - EVP_CIPHER_CTX_cleanup(&ciphp->ctx); - free(ciphp); - } -} - -/* * PUBLIC */ const EVP_CIPHER * ossl_cipher_get_EVP_CIPHER(VALUE obj) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; - SafeGetCipher(obj, ciphp); + SafeGetCipher(obj, ctx); - return EVP_CIPHER_CTX_cipher(&ciphp->ctx); + return EVP_CIPHER_CTX_cipher(ctx); } /* * PRIVATE */ +static void +ossl_cipher_free(EVP_CIPHER_CTX *ctx) +{ + if (ctx) { + EVP_CIPHER_CTX_cleanup(ctx); + free(ctx); + } +} + static VALUE ossl_cipher_alloc(VALUE klass) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; VALUE obj; - MakeCipher(obj, klass, ciphp); + MakeCipher(obj, klass, ctx); return obj; } @@ -76,19 +70,19 @@ DEFINE_ALLOC_WRAPPER(ossl_cipher_alloc) static VALUE ossl_cipher_initialize(VALUE self, VALUE str) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; const EVP_CIPHER *cipher; char *name; - GetCipher(self, ciphp); + GetCipher(self, ctx); name = StringValuePtr(str); if (!(cipher = EVP_get_cipherbyname(name))) { ossl_raise(rb_eRuntimeError, "Unsupported cipher algorithm (%s).", name); } - EVP_CIPHER_CTX_init(&ciphp->ctx); - if (EVP_CipherInit(&ciphp->ctx, cipher, NULL, NULL, -1) != 1) + EVP_CIPHER_CTX_init(ctx); + if (EVP_CipherInit(ctx, cipher, NULL, NULL, -1) != 1) ossl_raise(eCipherError, ""); return self; @@ -96,13 +90,15 @@ ossl_cipher_initialize(VALUE self, VALUE str) static VALUE ossl_cipher_copy_object(VALUE self, VALUE other) { - ossl_cipher *ciphp1, *ciphp2; + EVP_CIPHER_CTX *ctx1, *ctx2; rb_check_frozen(self); if (self == other) return self; - GetCipher(self, ciphp1); - SafeGetCipher(other, ciphp2); + GetCipher(self, ctx1); + SafeGetCipher(other, ctx2); + + memcpy(ctx1, ctx2, sizeof(EVP_CIPHER_CTX)); return self; } @@ -110,10 +106,10 @@ ossl_cipher_copy_object(VALUE self, VALUE other) static VALUE ossl_cipher_reset(VALUE self) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; - GetCipher(self, ciphp); - if (EVP_CipherInit(&ciphp->ctx, NULL, NULL, NULL, -1) != 1) + GetCipher(self, ctx); + if (EVP_CipherInit(ctx, NULL, NULL, NULL, -1) != 1) ossl_raise(eCipherError, ""); return self; @@ -122,11 +118,11 @@ ossl_cipher_reset(VALUE self) static VALUE ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH]; VALUE pass, init_v; - GetCipher(self, ciphp); + GetCipher(self, ctx); rb_scan_args(argc, argv, "02", &pass, &init_v); @@ -154,16 +150,16 @@ ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self) } } - if (EVP_CipherInit(&ciphp->ctx, NULL, NULL, NULL, 1) != 1) { + if (EVP_CipherInit(ctx, NULL, NULL, NULL, 1) != 1) { ossl_raise(eCipherError, ""); } if (!NIL_P(pass)) { StringValue(pass); - EVP_BytesToKey(EVP_CIPHER_CTX_cipher(&ciphp->ctx), EVP_md5(), iv, + EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv, RSTRING(pass)->ptr, RSTRING(pass)->len, 1, key, NULL); - if (EVP_CipherInit(&ciphp->ctx, NULL, key, iv, -1) != 1) { + if (EVP_CipherInit(ctx, NULL, key, iv, -1) != 1) { ossl_raise(eCipherError, ""); } } @@ -174,11 +170,11 @@ ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self) static VALUE ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH]; VALUE pass, init_v; - GetCipher(self, ciphp); + GetCipher(self, ctx); rb_scan_args(argc, argv, "11", &pass, &init_v); if (NIL_P(init_v)) { @@ -199,16 +195,16 @@ ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self) } } - if (EVP_CipherInit(&ciphp->ctx, NULL, NULL, NULL, 0) != 1) { + if (EVP_CipherInit(ctx, NULL, NULL, NULL, 0) != 1) { ossl_raise(eCipherError, ""); } if (!NIL_P(pass)) { StringValue(pass); - EVP_BytesToKey(EVP_CIPHER_CTX_cipher(&ciphp->ctx), EVP_md5(), iv, + EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv, RSTRING(pass)->ptr, RSTRING(pass)->len, 1, key, NULL); - if (EVP_CipherInit(&ciphp->ctx, NULL, key, iv, -1) != 1) { + if (EVP_CipherInit(ctx, NULL, key, iv, -1) != 1) { ossl_raise(eCipherError, ""); } } @@ -219,21 +215,21 @@ ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self) static VALUE ossl_cipher_update(VALUE self, VALUE data) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; char *in, *out; int in_len, out_len; VALUE str; - GetCipher(self, ciphp); + GetCipher(self, ctx); StringValue(data); in = RSTRING(data)->ptr; in_len = RSTRING(data)->len; - if (!(out = OPENSSL_malloc(in_len+EVP_CIPHER_CTX_block_size(&ciphp->ctx)))){ + if (!(out = OPENSSL_malloc(in_len+EVP_CIPHER_CTX_block_size(ctx)))){ ossl_raise(eCipherError, ""); } - if (!EVP_CipherUpdate(&ciphp->ctx, out, &out_len, in, in_len)) { + if (!EVP_CipherUpdate(ctx, out, &out_len, in, in_len)) { OPENSSL_free(out); ossl_raise(eCipherError, ""); } @@ -246,17 +242,17 @@ ossl_cipher_update(VALUE self, VALUE data) static VALUE ossl_cipher_final(VALUE self) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; char *out; int out_len; VALUE str; - GetCipher(self, ciphp); + GetCipher(self, ctx); - if (!(out = OPENSSL_malloc(EVP_CIPHER_CTX_block_size(&ciphp->ctx)))) { + if (!(out = OPENSSL_malloc(EVP_CIPHER_CTX_block_size(ctx)))) { ossl_raise(eCipherError, ""); } - if (!EVP_CipherFinal(&ciphp->ctx, out, &out_len)) { + if (!EVP_CipherFinal(ctx, out, &out_len)) { OPENSSL_free(out); ossl_raise(eCipherError, ""); } @@ -270,25 +266,25 @@ ossl_cipher_final(VALUE self) static VALUE ossl_cipher_name(VALUE self) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; - GetCipher(self, ciphp); + GetCipher(self, ctx); - return rb_str_new2(EVP_CIPHER_name(EVP_CIPHER_CTX_cipher(&ciphp->ctx))); + return rb_str_new2(EVP_CIPHER_name(EVP_CIPHER_CTX_cipher(ctx))); } static VALUE ossl_cipher_set_key(VALUE self, VALUE key) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; StringValue(key); - GetCipher(self, ciphp); + GetCipher(self, ctx); - if (RSTRING(key)->len < EVP_CIPHER_CTX_key_length(&ciphp->ctx)) + if (RSTRING(key)->len < EVP_CIPHER_CTX_key_length(ctx)) ossl_raise(eCipherError, "key length too short"); - if (EVP_CipherInit(&ciphp->ctx, NULL, RSTRING(key)->ptr, NULL, -1) != 1) + if (EVP_CipherInit(ctx, NULL, RSTRING(key)->ptr, NULL, -1) != 1) ossl_raise(eCipherError, ""); return Qnil; @@ -297,12 +293,12 @@ ossl_cipher_set_key(VALUE self, VALUE key) static VALUE ossl_cipher_set_iv(VALUE self, VALUE iv) { - ossl_cipher *ciphp; + EVP_CIPHER_CTX *ctx; StringValue(iv); - GetCipher(self, ciphp); + GetCipher(self, ctx); - if (EVP_CipherInit(&ciphp->ctx, NULL, NULL, RSTRING(iv)->ptr, -1) != 1) + if (EVP_CipherInit(ctx, NULL, NULL, RSTRING(iv)->ptr, -1) != 1) ossl_raise(eCipherError, ""); return Qnil; @@ -312,9 +308,9 @@ ossl_cipher_set_iv(VALUE self, VALUE iv) static VALUE \ ossl_cipher_##func(VALUE self) \ { \ - ossl_cipher *ciphp; \ - GetCipher(self, ciphp); \ - return INT2NUM(EVP_CIPHER_##func(EVP_CIPHER_CTX_cipher(&ciphp->ctx))); \ + EVP_CIPHER_CTX *ctx; \ + GetCipher(self, ctx); \ + return INT2NUM(EVP_CIPHER_##func(EVP_CIPHER_CTX_cipher(ctx))); \ } CIPHER_0ARG_INT(key_length) CIPHER_0ARG_INT(iv_length) diff --git a/ossl_digest.c b/ossl_digest.c index b91b838..acf978d 100644 --- a/ossl_digest.c +++ b/ossl_digest.c @@ -68,7 +68,9 @@ ossl_digest_initialize(VALUE self, VALUE str) GetDigest(self, ctx); name = StringValuePtr(str); - if (!(md = EVP_get_digestbyname(name))) { + + md = EVP_get_digestbyname(name); + if (!md) { ossl_raise(rb_eRuntimeError, "Unsupported digest algorithm (%s).", name); } EVP_DigestInit(ctx, md); @@ -96,12 +98,12 @@ ossl_digest_copy_object(VALUE self, VALUE other) static VALUE ossl_digest_reset(VALUE self) { - EVP_MD_CTX *ctx; + EVP_MD_CTX *ctx; - GetDigest(self, ctx); - EVP_DigestInit(ctx, EVP_MD_CTX_md(ctx)); + GetDigest(self, ctx); + EVP_DigestInit(ctx, EVP_MD_CTX_md(ctx)); - return self; + return self; } static VALUE diff --git a/ossl_hmac.c b/ossl_hmac.c index 720279a..2a76163 100644 --- a/ossl_hmac.c +++ b/ossl_hmac.c @@ -12,8 +12,8 @@ #include "ossl.h" -#define MakeHMAC(obj, ctx) \ - obj = Data_Make_Struct(cHMAC, HMAC_CTX, 0, ossl_hmac_free, ctx) +#define MakeHMAC(obj, klass, ctx) \ + obj = Data_Make_Struct(klass, HMAC_CTX, 0, ossl_hmac_free, ctx) #define GetHMAC(obj, ctx) do { \ Data_Get_Struct(obj, HMAC_CTX, ctx); \ if (!ctx) { \ @@ -51,7 +51,7 @@ ossl_hmac_alloc(VALUE klass) HMAC_CTX *ctx; VALUE obj; - MakeHMAC(obj, ctx); + MakeHMAC(obj, klass, ctx); return obj; } diff --git a/ossl_pkcs7.c b/ossl_pkcs7.c index 0f1744d..2d444eb 100644 --- a/ossl_pkcs7.c +++ b/ossl_pkcs7.c @@ -22,6 +22,11 @@ ossl_raise(rb_eRuntimeError, "PKCS7 wasn't initialized."); \ } \ } while (0) +#define SafeGetPKCS7(obj, pkcs7) do { \ + OSSL_Check_Kind(obj, cPKCS7); \ + GetPKCS7(obj, pkcs7); \ +} while (0) + #define WrapPKCS7si(klass, obj, p7si) do { \ if (!p7si) { \ ossl_raise(rb_eRuntimeError, "PKCS7si wasn't initialized."); \ @@ -174,6 +179,27 @@ ossl_pkcs7_initialize(VALUE self, VALUE arg) } static VALUE +ossl_pkcs7_copy_object(VALUE self, VALUE other) +{ + PKCS7 *a, *b, *pkcs7; + + rb_check_frozen(self); + if (self == other) return self; + + GetPKCS7(self, a); + SafeGetPKCS7(other, b); + + pkcs7 = PKCS7_dup(b); + if (!pkcs7) { + ossl_raise(ePKCS7Error, ""); + } + DATA_PTR(self) = pkcs7; + PKCS7_free(a); + + return self; +} + +static VALUE ossl_pkcs7_set_cipher(VALUE self, VALUE cipher) { PKCS7 *pkcs7; @@ -535,6 +561,7 @@ Init_ossl_pkcs7() */ rb_define_alloc_func(cPKCS7, ossl_pkcs7_alloc); rb_define_method(cPKCS7, "initialize", ossl_pkcs7_initialize, 1); + rb_define_method(cPKCS7, "copy_object", ossl_pkcs7_copy_object, 1); rb_define_method(cPKCS7, "add_signer", ossl_pkcs7_add_signer, 2); rb_define_method(cPKCS7, "signers", ossl_pkcs7_get_signer, 0); diff --git a/ossl_x509cert.c b/ossl_x509cert.c index b50d325..b35db50 100644 --- a/ossl_x509cert.c +++ b/ossl_x509cert.c @@ -176,8 +176,8 @@ ossl_x509_copy_object(VALUE self, VALUE other) if (!(x509 = X509_dup(b))) { ossl_raise(eX509CertError, ""); } - X509_free(a); DATA_PTR(self) = x509; + X509_free(a); return self; } |