diff options
author | Michal Rokos <m.rokos@sh.cvut.cz> | 2001-11-20 19:39:37 +0000 |
---|---|---|
committer | Michal Rokos <m.rokos@sh.cvut.cz> | 2001-11-20 19:39:37 +0000 |
commit | e588321f8a5ae949ae246b480b1639f176be870a (patch) | |
tree | d04bf1b41dbb451360b0031c0e5a41f989e5e11f | |
parent | cceffafefe8d78e8b7b8d0c4563ec0d48f44eed3 (diff) | |
download | ruby-openssl-history-e588321f8a5ae949ae246b480b1639f176be870a.tar.gz |
- HMAC added
- Config came back
- API changed in PKCS7 (param. order)
- API changed in Cipher (can't add data in .new)
- indentation changed (only half of files, 2nd half to do)
- some API doc added to README
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | README | 32 | ||||
-rw-r--r-- | openssl_missing.h | 14 | ||||
-rw-r--r-- | ossl.c | 8 | ||||
-rw-r--r-- | ossl.h | 25 | ||||
-rw-r--r-- | ossl_cipher.c | 89 | ||||
-rw-r--r-- | ossl_config.c | 49 | ||||
-rw-r--r-- | ossl_digest.c | 82 | ||||
-rw-r--r-- | ossl_hmac.c | 191 | ||||
-rw-r--r-- | ossl_ns_spki.c | 36 | ||||
-rw-r--r-- | ossl_pkcs7.c | 75 | ||||
-rw-r--r-- | ossl_pkey.c | 43 | ||||
-rw-r--r-- | ossl_pkey_dsa.c | 56 | ||||
-rw-r--r-- | ossl_pkey_rsa.c | 70 | ||||
-rw-r--r-- | ossl_rand.c | 18 | ||||
-rw-r--r-- | ossl_version.h | 3 | ||||
-rwxr-xr-x | test/pkcs7.rb | 4 |
17 files changed, 573 insertions, 231 deletions
@@ -4,10 +4,17 @@ Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz> All rights reserved. $Log$ +Revision 1.7 2001/11/20 19:39:37 majkl +- HMAC added +- Config came back +- API changed in PKCS7 (param. order) +- API changed in Cipher (can't add data in .new) +- indentation changed (only half of files, 2nd half to do) +- some API doc added to README + Revision 1.6 2001/11/19 12:44:57 majkl added X509::Certificate .to_der - 2001/11/18 (ossl-0.0.9) * all project is moved to savannah.gnu.org * applied patch to make yielding optional (GOTOU Yuuzou - thanks!) @@ -7,7 +7,6 @@ This program is licenced under the same licence as Ruby. (See the file 'LICENCE'.) [Done] (but not fully tested) -= OpenSSL config file parser (part) --- TO BE DROPPED? (any idea?) = PKey:: RSA,DSA keys - new, load, export = X509::Certificate - generating new certs, load, looking inside = X509::CRL - load, new, looking inside @@ -24,6 +23,8 @@ This program is licenced under the same licence as Ruby. = basic PRNG functions (random generator) for OpenSSL module and class Random = SSLSocket (merged Gotou Yuuzou's SSLsocket-Ruby project) = PKCS7 (signing&data_verify is working, rest needs some testing) += HMAC += OpenSSL config file parser (part) [To-Do] = check for memory leaking :-)) @@ -41,7 +42,6 @@ This program is licenced under the same licence as Ruby. = safe BigNums = PKCS12 = PKCS8 -= HMAC = ASN.1 ??? = BIO ??? = compat tests for RSA/DSA sign/encrypt @@ -64,6 +64,20 @@ OpenSSL:: .load_random_file(Filename) => true|false .write_random_file(Filename) => true|false + Config + .new(String (filename)) => self + .get_string(String (section)|nil, String (key)) => String (value) + .get_number(String (section)|nil, String (key)) => String (value) + + HMAC + .new(String(key), Digest) => self + .update(String) => self + .<<(String) <=> .update(String) + .hmac() => String + .hexhmac() => String + .inspect() <=> .hexhmac() + .to_str() <=> .hexhmac() + Random .seed(String) => true|false .load_random_file(String (filename)) => true|false @@ -80,11 +94,11 @@ OpenSSL:: DES (ECB, EDE, EDE3, CFB, CFB:EDE, CFB:EDE3, OFB, OFB:EDE, OFB:EDE3, CBC, CBC:EDE, CBC:EDE3) Idea (ECB, CFB, OFB, CBC) RC2 (ECB, CBC, BIT40:CBC, BIT64:CBC, CFB, OFB) - RC4 (UNSPEC, BIT40) + RC4 (nil, UNSPEC, BIT40) RC5 (ECB, CFB, OFB, CBC) .new([mode|type] [,type|mode]) => self - .encrypt(Password [, Init_Vect] [, Data]) => self or String - .decrypt(Password [, Init_Vect] [, Data]) => self or String + .encrypt(Password [, Init_Vect]) => self + .decrypt(Password [, Init_Vect]) => self .update(String) => String .<<(String) <=> .update .cipher => String @@ -123,19 +137,19 @@ OpenSSL:: PKCS7 .new(CONST|String (PEM)) => self - .add_signer(PKey::Any (private), PKCS7::Signer) + .add_signer(PKCS7::Signer, PKey (his private)) .signers => Ary of PKCS7::Signer .cipher=(Cipher::Any) => Cipher .add_recipient(X509::Certificate) => self .add_certificate(X509::Certificate) => self .add_crl(X509::CRL) => self - .add_data(String (data), [true|false] (detached)) => self - .verify_data(X509::Store, [String] (detached)) => true|false, yields PKCS7::Signer + .add_data(String (data), [(true|false) (detached?)]) => self + .verify_data(X509::Store, [String (detached)]) => true|false, yields PKCS7::Signer .decode_data(PKey::Any (private), X509::Certificate) => String .to_pem => String (PEM) Signer - .new(PKey::Any (private), X509::Certificate, Digest::Any) => self + .new(X509::Certificate, PKey (his private), Digest::Any) => self .name => X509::Name .serial => FixNum .signed_time => Time diff --git a/openssl_missing.h b/openssl_missing.h index ec1ef8d..7bef30e 100644 --- a/openssl_missing.h +++ b/openssl_missing.h @@ -40,5 +40,19 @@ #define PKCS7_RECIP_INFO_dup(ri) (PKCS7_RECIP_INFO *)ASN1_dup((int (*)())i2d_PKCS7_RECIP_INFO, \ (char *(*)())d2i_PKCS7_RECIP_INFO,(char *)ri) +/* to hmac.[ch] */ +static inline int +HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in) +{ + if (in == NULL) { + /*HMACerr(HMAC_CTX_COPY,HMAC_R_INPUT_NOT_INITIALIZED);*/ + return 0; + } + + memcpy(out, in, sizeof(HMAC_CTX)); + + return 1; +} + #endif @@ -87,7 +87,7 @@ void Init_openssl() ERR_load_crypto_strings(); /* - * Universe of Module + * Universe of Modules */ mOSSL = rb_define_module("OpenSSL"); mX509 = rb_define_module_under(mOSSL, "X509"); @@ -103,10 +103,11 @@ void Init_openssl() */ rb_define_const(mOSSL, "VERSION", rb_str_new2(OSSL_VERSION)); rb_define_const(mOSSL, "OPENSSL_VERSION", rb_str_new2(OPENSSL_VERSION_TEXT)); + /* * Components */ - /* Init_ossl_config(mOSSL); TO BE DROPPED OUT??? */ + Init_ossl_config(mOSSL); Init_ossl_x509(mX509); Init_ossl_x509name(mX509); Init_ossl_x509revoked(mX509); @@ -121,6 +122,7 @@ void Init_openssl() Init_ossl_rand(mOSSL); Init_ossl_pkey(mPKey); Init_ssl(mSSL); - Init_PKCS7(mPKCS7); + Init_pkcs7(mPKCS7); + Init_hmac(mOSSL); } @@ -15,11 +15,17 @@ #include <openssl/asn1_mac.h> #include <openssl/x509v3.h> #include <openssl/ssl.h> +#include <openssl/hmac.h> + #include "openssl_missing.h" +#include "ossl_version.h" + +/* + * OpenSSL has defined RFILE and Ruby has defined RFILE - so undef it! + */ #if !defined(OSSL_DEBUG) && defined(RFILE) # undef RFILE #endif -#include "ossl_version.h" #include <ruby.h> /* @@ -83,6 +89,12 @@ EXTERN VALUE cDSA; EXTERN VALUE cPKCS7; EXTERN VALUE cPKCS7SignerInfo; EXTERN VALUE ePKCS7Error; +/* HMAC */ +EXTERN VALUE cHMAC; +EXTERN VALUE eHMACError; +/* Conf */ +EXTERN VALUE cConfig; +EXTERN VALUE eConfigError; /* * CheckTypes @@ -102,10 +114,8 @@ char *ossl_error(void); /* * Config - * TO BE DROPPED OUT - * -void Init_ossl_config(VALUE); */ +void Init_ossl_config(VALUE); /* * Digest @@ -224,7 +234,12 @@ void Init_ssl(VALUE); VALUE ossl_pkcs7si_new_null(void); VALUE ossl_pkcs7si_new(PKCS7_SIGNER_INFO *); PKCS7_SIGNER_INFO *ossl_pkcs7si_get_PKCS7_SIGNER_INFO(VALUE); -void Init_PKCS7(VALUE); +void Init_pkcs7(VALUE); + +/* + * HMAC + */ +void Init_hmac(VALUE); #endif diff --git a/ossl_cipher.c b/ossl_cipher.c index 33b3260..bef4539 100644 --- a/ossl_cipher.c +++ b/ossl_cipher.c @@ -13,10 +13,7 @@ #define MakeCipher(obj, klass, ciphp) {\ obj = Data_Make_Struct(klass, ossl_cipher, 0, ossl_cipher_free, ciphp);\ } - -#define GetCipher(obj, ciphp) {\ - Data_Get_Struct(obj, ossl_cipher, ciphp);\ -} +#define GetCipher(obj, ciphp) Data_Get_Struct(obj, ossl_cipher, ciphp) #define DefCipherConst(x) rb_define_const(mCipher, #x, INT2FIX(##x)) @@ -57,7 +54,8 @@ typedef struct ossl_cipher_st { EVP_CIPHER_CTX *ctx; } ossl_cipher; -static void ossl_cipher_free(ossl_cipher *ciphp) +static void +ossl_cipher_free(ossl_cipher *ciphp) { if (ciphp) { if (ciphp->ctx) OPENSSL_free(ciphp->ctx); @@ -68,7 +66,8 @@ static void ossl_cipher_free(ossl_cipher *ciphp) /* * PUBLIC */ -int ossl_cipher_get_NID(VALUE obj) +int +ossl_cipher_get_NID(VALUE obj) { ossl_cipher *ciphp = NULL; @@ -77,7 +76,8 @@ int ossl_cipher_get_NID(VALUE obj) return ciphp->nid; /*EVP_CIPHER_CTX_nid(ciphp->ctx);*/ } -const EVP_CIPHER *ossl_cipher_get_EVP_CIPHER(VALUE obj) +const EVP_CIPHER * +ossl_cipher_get_EVP_CIPHER(VALUE obj) { ossl_cipher *ciphp = NULL; @@ -89,13 +89,14 @@ const EVP_CIPHER *ossl_cipher_get_EVP_CIPHER(VALUE obj) /* * PRIVATE */ -static VALUE ossl_cipher_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_cipher_s_new(int argc, VALUE *argv, VALUE klass) { ossl_cipher *ciphp = NULL; VALUE obj; if (klass == cCipher) - rb_raise(rb_eNotImpError, "cannot do Cipher.new - Cipher is an abstract class"); + rb_raise(rb_eNotImpError, "cannot do Cipher::ANY.new - it is an abstract class"); MakeCipher(obj, klass, ciphp); if (!(ciphp->ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX)))) { @@ -107,41 +108,17 @@ static VALUE ossl_cipher_s_new(int argc, VALUE *argv, VALUE klass) return obj; } -static VALUE ossl_cipher_update(VALUE self, VALUE data) -{ - ossl_cipher *ciphp = NULL; - char *in = NULL, *out = NULL; - int in_len = 0, out_len = 0; - VALUE str; - - GetCipher(self, ciphp); - Check_SafeStr(data); - in = RSTRING(data)->ptr; - in_len = RSTRING(data)->len; - - if (!(out = OPENSSL_malloc(in_len + EVP_CIPHER_CTX_block_size(ciphp->ctx)))) { - rb_raise(eCipherError, "%s", ossl_error()); - } - if (!EVP_CipherUpdate(ciphp->ctx, out, &out_len, in, in_len)) { - rb_raise(eCipherError, "%s", ossl_error()); - } - - str = rb_str_new(out, out_len); - OPENSSL_free(out); - - return str; -} - -static VALUE ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self) { ossl_cipher *ciphp = NULL; const EVP_CIPHER *cipher = NULL; unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH]; - VALUE pass, init_v, data; + VALUE pass, init_v; GetCipher(self, ciphp); - rb_scan_args(argc, argv, "12", &pass, &init_v, &data); + rb_scan_args(argc, argv, "11", &pass, &init_v); Check_SafeStr(pass); if (NIL_P(init_v)) { @@ -169,22 +146,20 @@ static VALUE ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self) rb_raise(eCipherError, "%s", ossl_error()); } - if (!NIL_P(data)) { - return ossl_cipher_update(self, data); - } return self; } -static VALUE ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self) { ossl_cipher *ciphp = NULL; const EVP_CIPHER *cipher = NULL; unsigned char iv[EVP_MAX_IV_LENGTH], key[EVP_MAX_KEY_LENGTH]; - VALUE pass, init_v, data; + VALUE pass, init_v; GetCipher(self, ciphp); - rb_scan_args(argc, argv, "12", &pass, &init_v, &data); + rb_scan_args(argc, argv, "11", &pass, &init_v); Check_SafeStr(pass); if (NIL_P(init_v)) { @@ -207,12 +182,34 @@ static VALUE ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self) rb_raise(eCipherError, "%s", ossl_error()); } - if (!NIL_P(data)) { - return ossl_cipher_update(self, data); - } return self; } +static VALUE ossl_cipher_update(VALUE self, VALUE data) +{ + ossl_cipher *ciphp = NULL; + char *in = NULL, *out = NULL; + int in_len = 0, out_len = 0; + VALUE str; + + GetCipher(self, ciphp); + Check_SafeStr(data); + in = RSTRING(data)->ptr; + in_len = RSTRING(data)->len; + + if (!(out = OPENSSL_malloc(in_len + EVP_CIPHER_CTX_block_size(ciphp->ctx)))) { + rb_raise(eCipherError, "%s", ossl_error()); + } + if (!EVP_CipherUpdate(ciphp->ctx, out, &out_len, in, in_len)) { + rb_raise(eCipherError, "%s", ossl_error()); + } + + str = rb_str_new(out, out_len); + OPENSSL_free(out); + + return str; +} + static VALUE ossl_cipher_cipher(VALUE self) { ossl_cipher *ciphp = NULL; diff --git a/ossl_config.c b/ossl_config.c index 48936d8..3ae9545 100644 --- a/ossl_config.c +++ b/ossl_config.c @@ -8,23 +8,24 @@ * This program is licenced under the same licence as Ruby. * (See the file 'LICENCE'.) */ -/* - * WILL BE DROPPED OUT!?!??? - * #include "ossl.h" #define MakeConfig(obj, configp) {\ obj = Data_Make_Struct(cConfig, ossl_config, 0, ossl_config_free, configp);\ - configp->ossl_type = T_OSSL_CONFIG;\ } +#define GetConfig(obj, configp) Data_Get_Struct(obj, ossl_config, configp) -#define GetConfig(obj, configp) {\ - OSSL_Check_Type(obj, T_OSSL_CONFIG);\ - Data_Get_Struct(obj, ossl_config, configp);\ -} +/* + * Classes + */ +VALUE cConfig; +VALUE eConfigError; +/*VALUE cConfigSection;*/ +/* + * Structs + */ typedef struct ossl_config_st { - int ossl_type; LHASH *config; } ossl_config; @@ -35,29 +36,29 @@ typedef struct ossl_configsect_st { int ossl_type; STACK_OF(CONF_VALUE) *section; } ossl_configsect; - * + */ -VALUE cConfig; -VALUE eConfigError; -VALUE cConfigSection; static void ossl_config_free(ossl_config *configp) { if (configp) { - if (configp->config) CONF_free(configp->config); + if (configp->config) + CONF_free(configp->config); free(configp); } } /* + * It's not ready! + * static void ossl_config_section_free(ossl_configsect *sectp) { if (configp) { - if (configp->section) sk_CONF_VALUE_pop_free(configp-> + if (configp->section) sk_CONF_VALUE_pop_free(configp->...); free(sectp); } - * + */ static VALUE ossl_config_s_new(int argc, VALUE *argv, VALUE klass) @@ -79,11 +80,10 @@ ossl_config_initialize(int argc, VALUE* argv, VALUE self) VALUE path; GetConfig(self, configp); - rb_scan_args(argc, argv, "1", &path); + rb_scan_args(argc, argv, "10", &path); Check_Type(path, T_STRING); - configp->config = NULL; configp->config = CONF_load(configp->config, RSTRING(path)->ptr, &err_line); if (configp->config == NULL) { @@ -116,7 +116,7 @@ ossl_config_get_string(VALUE self, VALUE section, VALUE item) return rb_str_new2(string); } -static VALUE +static VALUE ossl_config_get_number(VALUE self, VALUE section, VALUE item) { ossl_config *configp = NULL; @@ -159,8 +159,11 @@ ossl_config_get_section(VALUE self, VALUE section) else return obj; } - * + */ +/* + * INIT + */ void Init_ossl_config(VALUE mOSSL) { @@ -177,10 +180,6 @@ Init_ossl_config(VALUE mOSSL) rb_define_method(cConfig, "get_section", ossl_config_get_section, 1); cConfigSection = rb_define_class_under(mOSSL, "ConfigSection", rb_cObject); rb_undef_method(CLASS_OF(cConfigSection), "new"); - * -} - * - * CONFIG... - * TO BE DROPPED OUT?? */ +} diff --git a/ossl_digest.c b/ossl_digest.c index 0f5cc23..db51a8f 100644 --- a/ossl_digest.c +++ b/ossl_digest.c @@ -13,10 +13,7 @@ #define MakeDigest(klass, obj, digestp) {\ obj = Data_Make_Struct(klass, ossl_digest, 0, ossl_digest_free, digestp);\ } - -#define GetDigest(obj, digestp) {\ - Data_Get_Struct(obj, ossl_digest, digestp);\ -} +#define GetDigest(obj, digestp) Data_Get_Struct(obj, ossl_digest, digestp) /* * Classes @@ -39,10 +36,11 @@ typedef struct ossl_digest_st { EVP_MD_CTX *md; } ossl_digest; -static void ossl_digest_free(ossl_digest *digestp) +static void +ossl_digest_free(ossl_digest *digestp) { if (digestp) { - if (digestp->md) free(digestp->md); + if (digestp->md) OPENSSL_free(digestp->md); free(digestp); } } @@ -50,7 +48,8 @@ static void ossl_digest_free(ossl_digest *digestp) /* * PUBLIC */ -int ossl_digest_get_NID(VALUE obj) +int +ossl_digest_get_NID(VALUE obj) { ossl_digest *digestp = NULL; @@ -59,7 +58,8 @@ int ossl_digest_get_NID(VALUE obj) return EVP_MD_CTX_type(digestp->md); /*== digestp->md->digest->type*/ } -const EVP_MD *ossl_digest_get_EVP_MD(VALUE obj) +const EVP_MD * +ossl_digest_get_EVP_MD(VALUE obj) { ossl_digest *digestp = NULL; @@ -71,13 +71,14 @@ const EVP_MD *ossl_digest_get_EVP_MD(VALUE obj) /* * PRIVATE */ -static VALUE ossl_digest_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_digest_s_new(int argc, VALUE *argv, VALUE klass) { ossl_digest *digestp = NULL; VALUE obj; if (klass == cDigest) - rb_raise(rb_eNotImpError, "cannot do Digest.new - Digest is an abstract class"); + rb_raise(rb_eNotImpError, "cannot do Digest::ANY.new - it is an abstract class"); MakeDigest(klass, obj, digestp); rb_obj_call_init(obj, argc, argv); @@ -85,7 +86,8 @@ static VALUE ossl_digest_s_new(int argc, VALUE *argv, VALUE klass) return obj; } -static VALUE ossl_digest_update(VALUE self, VALUE data) +static VALUE +ossl_digest_update(VALUE self, VALUE data) { ossl_digest *digestp = NULL; @@ -97,7 +99,8 @@ static VALUE ossl_digest_update(VALUE self, VALUE data) return self; } -static VALUE ossl_digest_digest(VALUE self) +static VALUE +ossl_digest_digest(VALUE self) { ossl_digest *digestp = NULL; char *digest_txt = NULL; @@ -111,7 +114,7 @@ static VALUE ossl_digest_digest(VALUE self) rb_raise(eDigestError, "%s", ossl_error()); } - if (!(digest_txt = malloc(EVP_MD_CTX_size(digestp->md)))) { + if (!(digest_txt = malloc(EVP_MD_CTX_size(&final)))) { rb_raise(eDigestError, "Cannot allocate memory for digest"); } EVP_DigestFinal(&final, digest_txt, &digest_len); @@ -125,7 +128,8 @@ static VALUE ossl_digest_digest(VALUE self) /* * RUBY attitude */ -static VALUE ossl_digest_hexdigest(VALUE self) +static VALUE +ossl_digest_hexdigest(VALUE self) { ossl_digest *digestp = NULL; static const char hex[]="0123456789abcdef"; @@ -140,7 +144,7 @@ static VALUE ossl_digest_hexdigest(VALUE self) rb_raise(eDigestError, "%s", ossl_error()); } - if (!(digest_txt = malloc(EVP_MD_CTX_size(digestp->md)))) { + if (!(digest_txt = malloc(EVP_MD_CTX_size(&final)))) { rb_raise(eDigestError, "Cannot allocate memory for digest"); } EVP_DigestFinal(&final, digest_txt, &digest_len); @@ -163,7 +167,8 @@ static VALUE ossl_digest_hexdigest(VALUE self) /* * OPENSSL attitude * -static VALUE ossl_digest_hexdigest(VALUE self) +static VALUE +ossl_digest_hexdigest(VALUE self) { ossl_digest *digestp = NULL; unsigned char *digest_txt = NULL, *hexdigest_txt = NULL; @@ -177,7 +182,7 @@ static VALUE ossl_digest_hexdigest(VALUE self) rb_raise(eDigestError, "%s", ossl_error()); } - if (!(digest_txt = malloc(EVP_MD_CTX_size(digestp->md)))) { + if (!(digest_txt = malloc(EVP_MD_CTX_size(&final)))) { rb_raise(eDigestError, "Cannot allocate memory for digest"); } EVP_DigestFinal(&final, digest_txt, &digest_len); @@ -194,13 +199,14 @@ static VALUE ossl_digest_hexdigest(VALUE self) /* * MD2 */ -static VALUE ossl_md2_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_md2_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_md2()); @@ -215,13 +221,14 @@ static VALUE ossl_md2_initialize(int argc, VALUE *argv, VALUE self) /* * MD5 */ -static VALUE ossl_md5_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_md5_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_md5()); @@ -236,13 +243,14 @@ static VALUE ossl_md5_initialize(int argc, VALUE *argv, VALUE self) /* * MDC2 */ -static VALUE ossl_mdc2_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_mdc2_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_mdc2()); @@ -257,13 +265,14 @@ static VALUE ossl_mdc2_initialize(int argc, VALUE *argv, VALUE self) /* * RIPEmd160 */ -static VALUE ossl_ripemd160_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_ripemd160_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_ripemd160()); @@ -278,13 +287,14 @@ static VALUE ossl_ripemd160_initialize(int argc, VALUE *argv, VALUE self) /* * SHA */ -static VALUE ossl_sha_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_sha_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_sha()); @@ -299,13 +309,14 @@ static VALUE ossl_sha_initialize(int argc, VALUE *argv, VALUE self) /* * SHA1 */ -static VALUE ossl_sha1_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_sha1_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_sha1()); @@ -317,13 +328,14 @@ static VALUE ossl_sha1_initialize(int argc, VALUE *argv, VALUE self) return self; } -static VALUE ossl_dss_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_dss_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_dss()); @@ -335,13 +347,14 @@ static VALUE ossl_dss_initialize(int argc, VALUE *argv, VALUE self) return self; } -static VALUE ossl_dss1_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_dss1_initialize(int argc, VALUE *argv, VALUE self) { ossl_digest *digestp = NULL; VALUE data; GetDigest(self, digestp); - if (!(digestp->md = malloc(sizeof(EVP_MD_CTX)))) { + if (!(digestp->md = OPENSSL_malloc(sizeof(EVP_MD_CTX)))) { rb_raise(eDigestError, "Cannot allocate memory for new digest"); } EVP_DigestInit(digestp->md, EVP_dss1()); @@ -356,7 +369,8 @@ static VALUE ossl_dss1_initialize(int argc, VALUE *argv, VALUE self) /* * INIT */ -void Init_ossl_digest(VALUE mDigest) +void +Init_ossl_digest(VALUE mDigest) { eDigestError = rb_define_class_under(mDigest, "Error", rb_eStandardError); diff --git a/ossl_hmac.c b/ossl_hmac.c new file mode 100644 index 0000000..bce9068 --- /dev/null +++ b/ossl_hmac.c @@ -0,0 +1,191 @@ +/* + * $Id$ + * 'OpenSSL for Ruby' project + * Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz> + * All rights reserved. + */ +/* + * This program is licenced under the same licence as Ruby. + * (See the file 'LICENCE'.) + */ +#ifndef NO_HMAC + +#include "ossl.h" + +#define MakeHMAC(obj, hmacp) {\ + obj = Data_Make_Struct(cHMAC, ossl_hmac, 0, ossl_hmac_free, hmacp);\ +} +#define GetHMAC(obj, hmacp) Data_Get_Struct(obj, ossl_hmac, hmacp) + +/* + * Classes + */ +VALUE cHMAC; +VALUE eHMACError; + +/* + * Struct + */ +typedef struct ossl_hmac_st { + HMAC_CTX *hmac; +} ossl_hmac; + +static void +ossl_hmac_free(ossl_hmac *hmacp) +{ + if (hmacp) { + if (hmacp->hmac) + OPENSSL_free(hmacp->hmac); + free(hmacp); + } +} + +/* + * PUBLIC + */ + +/* + * PRIVATE + */ +static VALUE +ossl_hmac_s_new(int argc, VALUE *argv, VALUE klass) +{ + ossl_hmac *hmacp = NULL; + VALUE obj; + + MakeHMAC(obj, hmacp); + rb_obj_call_init(obj, argc, argv); + + return obj; +} + +static VALUE +ossl_hmac_initialize(int argc, VALUE *argv, VALUE self) +{ + ossl_hmac *hmacp = NULL; + const EVP_MD *md = NULL; + VALUE key, digest; + + GetHMAC(self, hmacp); + + rb_scan_args(argc, argv, "20", &key, &digest); + + OSSL_Check_Type(digest, cDigest); + Check_Type(key, T_STRING); + Check_SafeStr(key); + + if (!(hmacp->hmac = OPENSSL_malloc(sizeof(HMAC_CTX)))) { + rb_raise(eHMACError, "%s", ossl_error()); + } + + md = ossl_digest_get_EVP_MD(digest); + + HMAC_Init(hmacp->hmac, RSTRING(key)->ptr, RSTRING(key)->len, md); + + return self; +} + +static VALUE +ossl_hmac_update(VALUE self, VALUE data) +{ + ossl_hmac *hmacp = NULL; + + GetHMAC(self, hmacp); + + Check_Type(data, T_STRING); + HMAC_Update(hmacp->hmac, RSTRING(data)->ptr, RSTRING(data)->len); + + return self; +} + +static VALUE +ossl_hmac_hmac(VALUE self) +{ + ossl_hmac *hmacp = NULL; + char *buf = NULL; + int buf_len = 0; + HMAC_CTX final; + VALUE str; + + GetHMAC(self, hmacp); + + if (!HMAC_CTX_copy(&final, hmacp->hmac)) { + rb_raise(eHMACError, "%s", ossl_error()); + } + + if (!(buf = OPENSSL_malloc(HMAC_size(&final)))) { + rb_raise(eHMACError, "Cannot allocate memory for hmac"); + } + HMAC_Final(&final, buf, &buf_len); + + str = rb_str_new(buf, buf_len); + OPENSSL_free(buf); + + return str; +} + +static VALUE +ossl_hmac_hexhmac(VALUE self) +{ + ossl_hmac *hmacp = NULL; + static const char hex[]="0123456789abcdef"; + char *buf = NULL, *hexbuf = NULL; + int i,buf_len = 0; + HMAC_CTX final; + VALUE str; + + GetHMAC(self, hmacp); + + if (!HMAC_CTX_copy(&final, hmacp->hmac)) { + rb_raise(eHMACError, "%s", ossl_error()); + } + + if (!(buf = OPENSSL_malloc(HMAC_size(&final)))) { + rb_raise(eHMACError, "Cannot allocate memory for hmac"); + } + HMAC_Final(&final, buf, &buf_len); + + if (!(hexbuf = OPENSSL_malloc(2*buf_len+1))) { + rb_raise(eHMACError, "Memory alloc error"); + } + for (i = 0; i < buf_len; i++) { + hexbuf[i + i] = hex[((unsigned char)buf[i]) >> 4]; + hexbuf[i + i + 1] = hex[buf[i] & 0x0f]; + } + hexbuf[i + i] = '\0'; + str = rb_str_new(hexbuf, 2*buf_len); + OPENSSL_free(buf); + OPENSSL_free(hexbuf); + + return str; +} + +/* + * INIT + */ +void +Init_hmac(VALUE mOSSL) +{ + eHMACError = rb_define_class_under(mOSSL, "HMACError", rb_eStandardError); + + cHMAC = rb_define_class_under(mOSSL, "HMAC", rb_cObject); + rb_define_singleton_method(cHMAC, "new", ossl_hmac_s_new, -1); + rb_define_method(cHMAC, "initialize", ossl_hmac_initialize, -1); + rb_define_method(cHMAC, "update", ossl_hmac_update, 1); + rb_define_alias(cHMAC, "<<", "update"); + rb_define_method(cHMAC, "hmac", ossl_hmac_hmac, 0); + rb_define_method(cHMAC, "hexhmac", ossl_hmac_hexhmac, 0); + rb_define_alias(cHMAC, "inspect", "hexhmac"); + rb_define_alias(cHMAC, "to_str", "hexhmac"); +} + +#else + +void +Init_hmac(VALUE dummy) +{ + rb_warning("HMAC will NOT be avaible: OpenSSL is compiled without HMAC"); +} + +#endif + diff --git a/ossl_ns_spki.c b/ossl_ns_spki.c index 3c43774..c11cf60 100644 --- a/ossl_ns_spki.c +++ b/ossl_ns_spki.c @@ -31,7 +31,8 @@ typedef struct ossl_spki_st { NETSCAPE_SPKI *spki; } ossl_spki; -static void ossl_spki_free(ossl_spki *spkip) +static void +ossl_spki_free(ossl_spki *spkip) { if(spkip) { if(spkip->spki) NETSCAPE_SPKI_free(spkip->spki); @@ -46,7 +47,8 @@ static void ossl_spki_free(ossl_spki *spkip) /* * Private functions */ -static VALUE ossl_spki_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_spki_s_new(int argc, VALUE *argv, VALUE klass) { ossl_spki *spkip = NULL; VALUE obj; @@ -57,7 +59,8 @@ static VALUE ossl_spki_s_new(int argc, VALUE *argv, VALUE klass) return obj; } -static VALUE ossl_spki_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_spki_initialize(int argc, VALUE *argv, VALUE self) { ossl_spki *spkip = NULL; NETSCAPE_SPKI *spki = NULL; @@ -86,7 +89,8 @@ static VALUE ossl_spki_initialize(int argc, VALUE *argv, VALUE self) return self; } -static VALUE ossl_spki_to_pem(VALUE self) +static VALUE +ossl_spki_to_pem(VALUE self) { ossl_spki *spkip = NULL; char *data = NULL; @@ -101,7 +105,8 @@ static VALUE ossl_spki_to_pem(VALUE self) return rb_str_new2(data); } -static VALUE ossl_spki_to_str(VALUE self) +static VALUE +ossl_spki_to_str(VALUE self) { ossl_spki *spkip = NULL; BIO *out = NULL; @@ -124,7 +129,8 @@ static VALUE ossl_spki_to_str(VALUE self) return str; } -static VALUE ossl_spki_get_public_key(VALUE self) +static VALUE +ossl_spki_get_public_key(VALUE self) { ossl_spki *spkip = NULL; EVP_PKEY *pkey = NULL; @@ -140,7 +146,8 @@ static VALUE ossl_spki_get_public_key(VALUE self) return pub_key; } -static VALUE ossl_spki_set_public_key(VALUE self, VALUE pubk) +static VALUE +ossl_spki_set_public_key(VALUE self, VALUE pubk) { ossl_spki *spkip = NULL; EVP_PKEY *pkey = NULL; @@ -158,7 +165,8 @@ static VALUE ossl_spki_set_public_key(VALUE self, VALUE pubk) return self; } -static VALUE ossl_spki_get_challenge(VALUE self) +static VALUE +ossl_spki_get_challenge(VALUE self) { ossl_spki *spkip = NULL; VALUE str; @@ -171,7 +179,8 @@ static VALUE ossl_spki_get_challenge(VALUE self) return rb_str_new2(""); } -static VALUE ossl_spki_set_challenge(VALUE self, VALUE str) +static VALUE +ossl_spki_set_challenge(VALUE self, VALUE str) { ossl_spki *spkip = NULL; @@ -185,7 +194,8 @@ static VALUE ossl_spki_set_challenge(VALUE self, VALUE str) return str; } -static VALUE ossl_spki_sign(VALUE self, VALUE key, VALUE digest) +static VALUE +ossl_spki_sign(VALUE self, VALUE key, VALUE digest) { ossl_spki *spkip = NULL; EVP_PKEY *pkey = NULL; @@ -213,7 +223,8 @@ static VALUE ossl_spki_sign(VALUE self, VALUE key, VALUE digest) /* * Checks that cert signature is made with PRIVversion of this PUBLIC 'key' */ -static VALUE ossl_spki_verify(VALUE self, VALUE key) +static VALUE +ossl_spki_verify(VALUE self, VALUE key) { ossl_spki *spkip = NULL; EVP_PKEY *pkey = NULL; @@ -237,7 +248,8 @@ static VALUE ossl_spki_verify(VALUE self, VALUE key) /* * NETSCAPE_SPKI init */ -void Init_ossl_spki(VALUE mNetscape) +void +Init_ossl_spki(VALUE mNetscape) { eSPKIError = rb_define_class_under(mNetscape, "SPKIError", rb_eStandardError); diff --git a/ossl_pkcs7.c b/ossl_pkcs7.c index 1a9f7c9..41a1a84 100644 --- a/ossl_pkcs7.c +++ b/ossl_pkcs7.c @@ -32,7 +32,6 @@ /* * Constants - * types */ #define SIGNED NID_pkcs7_signed #define ENVELOPED NID_pkcs7_enveloped @@ -60,7 +59,8 @@ typedef struct ossl_pkcs7si_st { PKCS7_SIGNER_INFO *signer; } ossl_pkcs7si; -static void ossl_pkcs7_free(ossl_pkcs7 *pkcs7p) +static void +ossl_pkcs7_free(ossl_pkcs7 *pkcs7p) { if (pkcs7p) { if (pkcs7p->pkcs7) { @@ -70,7 +70,8 @@ static void ossl_pkcs7_free(ossl_pkcs7 *pkcs7p) } } -static void ossl_pkcs7si_free(ossl_pkcs7si *p7sip) +static void +ossl_pkcs7si_free(ossl_pkcs7si *p7sip) { if (p7sip) { if (p7sip->signer) { @@ -83,7 +84,8 @@ static void ossl_pkcs7si_free(ossl_pkcs7si *p7sip) /* * Public */ -VALUE ossl_pkcs7si_new_null(void) +VALUE +ossl_pkcs7si_new_null(void) { ossl_pkcs7si *p7sip = NULL; VALUE obj; @@ -97,7 +99,8 @@ VALUE ossl_pkcs7si_new_null(void) return obj; } -VALUE ossl_pkcs7si_new(PKCS7_SIGNER_INFO *si) +VALUE +ossl_pkcs7si_new(PKCS7_SIGNER_INFO *si) { ossl_pkcs7si *p7sip = NULL; VALUE obj; @@ -114,7 +117,8 @@ VALUE ossl_pkcs7si_new(PKCS7_SIGNER_INFO *si) return obj; } -PKCS7_SIGNER_INFO *ossl_pkcs7si_get_PKCS7_SIGNER_INFO(VALUE obj) +PKCS7_SIGNER_INFO * +ossl_pkcs7si_get_PKCS7_SIGNER_INFO(VALUE obj) { ossl_pkcs7si *p7sip = NULL; PKCS7_SIGNER_INFO *si = NULL; @@ -169,7 +173,8 @@ static VALUE ossl_pkcs7_s_sign(VALUE klass, VALUE key, VALUE cert, VALUE data) } */ -static VALUE ossl_pkcs7_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_pkcs7_s_new(int argc, VALUE *argv, VALUE klass) { ossl_pkcs7 *pkcs7p = NULL; VALUE obj; @@ -180,7 +185,8 @@ static VALUE ossl_pkcs7_s_new(int argc, VALUE *argv, VALUE klass) return obj; } -static VALUE ossl_pkcs7_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_pkcs7_initialize(int argc, VALUE *argv, VALUE self) { ossl_pkcs7 *p7p = NULL; BIO *in = NULL; @@ -227,7 +233,8 @@ static VALUE ossl_pkcs7_set_type(VALUE self, VALUE type) } */ -static VALUE ossl_pkcs7_set_cipher(VALUE self, VALUE cipher) +static VALUE +ossl_pkcs7_set_cipher(VALUE self, VALUE cipher) { ossl_pkcs7 *p7p = NULL; @@ -242,7 +249,8 @@ static VALUE ossl_pkcs7_set_cipher(VALUE self, VALUE cipher) return cipher; } -static VALUE ossl_pkcs7_add_signer(VALUE self, VALUE pkey, VALUE signer) +static VALUE +ossl_pkcs7_add_signer(VALUE self, VALUE signer, VALUE pkey) { ossl_pkcs7 *p7p = NULL; PKCS7_SIGNER_INFO *si = NULL; @@ -270,7 +278,8 @@ static VALUE ossl_pkcs7_add_signer(VALUE self, VALUE pkey, VALUE signer) return self; } -static VALUE ossl_pkcs7_get_signer(VALUE self) +static VALUE +ossl_pkcs7_get_signer(VALUE self) { ossl_pkcs7 *p7p = NULL; STACK_OF(PKCS7_SIGNER_INFO) *sk = NULL; @@ -299,7 +308,8 @@ static VALUE ossl_pkcs7_get_signer(VALUE self) return ary; } -static VALUE ossl_pkcs7_add_recipient(VALUE self, VALUE cert) +static VALUE +ossl_pkcs7_add_recipient(VALUE self, VALUE cert) { ossl_pkcs7 *p7p = NULL; PKCS7_RECIP_INFO *ri = NULL; @@ -322,7 +332,8 @@ static VALUE ossl_pkcs7_add_recipient(VALUE self, VALUE cert) return self; } -static VALUE ossl_pkcs7_add_certificate(VALUE self, VALUE cert) +static VALUE +ossl_pkcs7_add_certificate(VALUE self, VALUE cert) { ossl_pkcs7 *p7p = NULL; @@ -337,7 +348,8 @@ static VALUE ossl_pkcs7_add_certificate(VALUE self, VALUE cert) return self; } -static VALUE ossl_pkcs7_add_crl(VALUE self, VALUE crl) +static VALUE +ossl_pkcs7_add_crl(VALUE self, VALUE crl) { ossl_pkcs7 *p7p = NULL; @@ -352,7 +364,8 @@ static VALUE ossl_pkcs7_add_crl(VALUE self, VALUE crl) return self; } -static VALUE ossl_pkcs7_add_data(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_pkcs7_add_data(int argc, VALUE *argv, VALUE self) { ossl_pkcs7 *p7p = NULL; BIO *bio = NULL; @@ -386,7 +399,8 @@ static VALUE ossl_pkcs7_add_data(int argc, VALUE *argv, VALUE self) return self; } -static VALUE ossl_pkcs7_data_verify(VALUE self, VALUE x509store, VALUE detached) +static VALUE +ossl_pkcs7_data_verify(VALUE self, VALUE x509store, VALUE detached) { ossl_pkcs7 *p7p = NULL; BIO *bio = NULL, *data = NULL; @@ -453,7 +467,8 @@ static VALUE ossl_pkcs7_data_verify(VALUE self, VALUE x509store, VALUE detached) return Qtrue; } -static VALUE ossl_pkcs7_data_decode(VALUE self, VALUE key, VALUE cert) +static VALUE +ossl_pkcs7_data_decode(VALUE self, VALUE key, VALUE cert) { ossl_pkcs7 *p7p = NULL; EVP_PKEY *pkey = NULL; @@ -488,7 +503,8 @@ static VALUE ossl_pkcs7_data_decode(VALUE self, VALUE key, VALUE cert) return str; } -static VALUE ossl_pkcs7_to_pem(VALUE self) +static VALUE +ossl_pkcs7_to_pem(VALUE self) { ossl_pkcs7 *p7p = NULL; BIO *out = NULL; @@ -514,7 +530,8 @@ static VALUE ossl_pkcs7_to_pem(VALUE self) /* * SIGNER INFO */ -static VALUE ossl_pkcs7si_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_pkcs7si_s_new(int argc, VALUE *argv, VALUE klass) { ossl_pkcs7si *p7sip = NULL; VALUE obj; @@ -526,7 +543,8 @@ static VALUE ossl_pkcs7si_s_new(int argc, VALUE *argv, VALUE klass) return obj; } -static VALUE ossl_pkcs7si_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_pkcs7si_initialize(int argc, VALUE *argv, VALUE self) { ossl_pkcs7si *p7sip = NULL; PKCS7_SIGNER_INFO *si = NULL; @@ -538,7 +556,7 @@ static VALUE ossl_pkcs7si_initialize(int argc, VALUE *argv, VALUE self) GetPKCS7si_unsafe(self, p7sip); rb_warn("HERE!"); - rb_scan_args(argc, argv, "30", &key, &cert, &digest); + rb_scan_args(argc, argv, "30", &cert, &key, &digest); OSSL_Check_Type(key, cPKey); OSSL_Check_Type(cert, cX509Certificate); @@ -562,7 +580,8 @@ static VALUE ossl_pkcs7si_initialize(int argc, VALUE *argv, VALUE self) return self; } -static VALUE ossl_pkcs7si_get_name(VALUE self) +static VALUE +ossl_pkcs7si_get_name(VALUE self) { ossl_pkcs7si *p7sip = NULL; @@ -571,7 +590,8 @@ static VALUE ossl_pkcs7si_get_name(VALUE self) return ossl_x509name_new2(p7sip->signer->issuer_and_serial->issuer); } -static VALUE ossl_pkcs7si_get_serial(VALUE self) +static VALUE +ossl_pkcs7si_get_serial(VALUE self) { ossl_pkcs7si *p7sip = NULL; @@ -580,7 +600,8 @@ static VALUE ossl_pkcs7si_get_serial(VALUE self) return INT2NUM(ASN1_INTEGER_get(p7sip->signer->issuer_and_serial->serial)); } -static VALUE ossl_pkcs7si_get_signed_time(VALUE self) +static VALUE +ossl_pkcs7si_get_signed_time(VALUE self) { ossl_pkcs7si *p7sip = NULL; ASN1_TYPE *asn1obj = NULL; @@ -596,7 +617,11 @@ static VALUE ossl_pkcs7si_get_signed_time(VALUE self) return Qnil; } -void Init_PKCS7(VALUE mPKCS7) +/* + * INIT + */ +void +Init_pkcs7(VALUE mPKCS7) { ePKCS7Error = rb_define_class_under(mPKCS7, "Error", rb_eStandardError); diff --git a/ossl_pkey.c b/ossl_pkey.c index 0bc374e..767095f 100644 --- a/ossl_pkey.c +++ b/ossl_pkey.c @@ -30,7 +30,8 @@ VALUE ePKeyError; /* * Public */ -VALUE ossl_pkey_new(EVP_PKEY *key) +VALUE +ossl_pkey_new(EVP_PKEY *key) { if (!key) rb_raise(ePKeyError, "Empty key!"); @@ -41,34 +42,38 @@ VALUE ossl_pkey_new(EVP_PKEY *key) case EVP_PKEY_DSA: return ossl_dsa_new(key->pkey.dsa); } - /* - * Make it or not? - * EVP_PKEY_free(new_key); - */ + rb_raise(ePKeyError, "unsupported key type"); return Qnil; } -VALUE ossl_pkey_new_from_file(VALUE v) +VALUE +ossl_pkey_new_from_file(VALUE path) { - char *path; - FILE *fp; - EVP_PKEY *pkey; + char *filename = NULL; + FILE *fp = NULL; + EVP_PKEY *pkey = NULL; VALUE obj; - path = RSTRING(v)->ptr; - if((fp = fopen(path, "r")) == NULL) + filename = RSTRING(path)->ptr; + if ((fp = fopen(filename, "r")) == NULL) rb_raise(ePKeyError, "%s", strerror(errno)); + /* + * Will we handle user passwords? + */ pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL); fclose(fp); - if(!pkey) rb_raise(ePKeyError, "%s", ossl_error()); - obj = rb_ensure(ossl_pkey_new, (VALUE)pkey, - (VALUE(*)(VALUE))EVP_PKEY_free, (VALUE)pkey); + if (!pkey) + rb_raise(ePKeyError, "%s", ossl_error()); + + obj = ossl_pkey_new(pkey); + EVP_PKEY_free(pkey); return obj; } -EVP_PKEY *ossl_pkey_get_EVP_PKEY(VALUE obj) +EVP_PKEY * +ossl_pkey_get_EVP_PKEY(VALUE obj) { ossl_pkey *pkeyp = NULL; @@ -80,18 +85,20 @@ EVP_PKEY *ossl_pkey_get_EVP_PKEY(VALUE obj) /* * Private */ -static VALUE ossl_pkey_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_pkey_s_new(int argc, VALUE *argv, VALUE klass) { ossl_pkey *pkeyp = NULL; VALUE obj; if (klass == cPKey) - rb_raise(rb_eNotImpError, "cannot do PKey.new - PKey is an abstract class"); + rb_raise(rb_eNotImpError, "cannot do PKey::ANY.new - it is an abstract class"); return Qnil; } -void Init_ossl_pkey(VALUE mPKey) +void +Init_ossl_pkey(VALUE mPKey) { ePKeyError = rb_define_class_under(mPKey, "Error", rb_eStandardError); diff --git a/ossl_pkey_dsa.c b/ossl_pkey_dsa.c index bfb35cc..1259d66 100644 --- a/ossl_pkey_dsa.c +++ b/ossl_pkey_dsa.c @@ -15,9 +15,7 @@ obj = Data_Make_Struct(cDSA, ossl_dsa, 0, ossl_dsa_free, dsap);\ dsap->pkey.get_EVP_PKEY = ossl_dsa_get_EVP_PKEY;\ } - #define GetDSA_unsafe(obj, dsap) Data_Get_Struct(obj, ossl_dsa, dsap) - #define GetDSA(obj, dsap) {\ GetDSA_unsafe(obj, dsap);\ if (!dsap->dsa) rb_raise(eDSAError, "not initialized!");\ @@ -39,7 +37,8 @@ typedef struct ossl_dsa_st { DSA *dsa; } ossl_dsa; -static void ossl_dsa_free(ossl_dsa *dsap) +static void +ossl_dsa_free(ossl_dsa *dsap) { if (dsap) { if (dsap->dsa) DSA_free(dsap->dsa); @@ -51,7 +50,8 @@ static void ossl_dsa_free(ossl_dsa *dsap) /* * Public */ -VALUE ossl_dsa_new_null() +VALUE +ossl_dsa_new_null() { ossl_dsa *dsap = NULL; VALUE obj; @@ -64,7 +64,8 @@ VALUE ossl_dsa_new_null() return obj; } -VALUE ossl_dsa_new(DSA *dsa) +VALUE +ossl_dsa_new(DSA *dsa) { ossl_dsa *dsap = NULL; VALUE obj; @@ -82,7 +83,8 @@ VALUE ossl_dsa_new(DSA *dsa) return obj; } -DSA *ossl_dsa_get_DSA(VALUE obj) +DSA * +ossl_dsa_get_DSA(VALUE obj) { ossl_dsa *dsap = NULL; DSA *dsa = NULL; @@ -97,7 +99,8 @@ DSA *ossl_dsa_get_DSA(VALUE obj) return dsa; } -EVP_PKEY *ossl_dsa_get_EVP_PKEY(VALUE obj) +EVP_PKEY * +ossl_dsa_get_EVP_PKEY(VALUE obj) { DSA *dsa = NULL; EVP_PKEY *pkey = NULL; @@ -120,7 +123,8 @@ EVP_PKEY *ossl_dsa_get_EVP_PKEY(VALUE obj) /* * Private */ -static VALUE ossl_dsa_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_dsa_s_new(int argc, VALUE *argv, VALUE klass) { ossl_dsa *dsap = NULL; VALUE obj; @@ -134,7 +138,8 @@ static VALUE ossl_dsa_s_new(int argc, VALUE *argv, VALUE klass) /* * CB for yielding when generating DSA params */ -static void ossl_dsa_generate_cb(int p, int n, void *arg) +static void +ossl_dsa_generate_cb(int p, int n, void *arg) { VALUE ary; @@ -145,7 +150,8 @@ static void ossl_dsa_generate_cb(int p, int n, void *arg) rb_yield(ary); } -static VALUE ossl_dsa_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_dsa_initialize(int argc, VALUE *argv, VALUE self) { ossl_dsa *dsap = NULL; DSA *dsa = NULL; @@ -210,7 +216,8 @@ static VALUE ossl_dsa_initialize(int argc, VALUE *argv, VALUE self) return self; } -static VALUE ossl_dsa_is_public(VALUE self) +static VALUE +ossl_dsa_is_public(VALUE self) { ossl_dsa *dsap = NULL; @@ -223,7 +230,8 @@ static VALUE ossl_dsa_is_public(VALUE self) return (dsap->dsa->pub_key) ? Qtrue : Qfalse; } -static VALUE ossl_dsa_is_private(VALUE self) +static VALUE +ossl_dsa_is_private(VALUE self) { ossl_dsa *dsap = NULL; @@ -232,7 +240,8 @@ static VALUE ossl_dsa_is_private(VALUE self) return (DSA_PRIVATE(dsap->dsa)) ? Qtrue : Qfalse; } -static VALUE ossl_dsa_export(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_dsa_export(int argc, VALUE *argv, VALUE self) { ossl_dsa *dsap = NULL; BIO *out = NULL; @@ -275,7 +284,8 @@ static VALUE ossl_dsa_export(int argc, VALUE *argv, VALUE self) return str; } -static VALUE ossl_dsa_to_der(VALUE self) +static VALUE +ossl_dsa_to_der(VALUE self) { ossl_dsa *dsap = NULL; DSA *dsa = NULL; @@ -320,7 +330,8 @@ static VALUE ossl_dsa_to_der(VALUE self) * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!! * Don't use :-)) (I's up to you) */ -static VALUE ossl_dsa_to_str(VALUE self) +static VALUE +ossl_dsa_to_str(VALUE self) { ossl_dsa *dsap = NULL; BIO *out = NULL; @@ -346,7 +357,8 @@ static VALUE ossl_dsa_to_str(VALUE self) /* * Makes new instance DSA PUBLIC_KEY from PRIVATE_KEY */ -static VALUE ossl_dsa_to_public_key(VALUE self) +static VALUE +ossl_dsa_to_public_key(VALUE self) { ossl_dsa *dsap1 = NULL, *dsap2 = NULL; VALUE obj; @@ -361,7 +373,8 @@ static VALUE ossl_dsa_to_public_key(VALUE self) return obj; } -static VALUE ossl_dsa_sign(VALUE self, VALUE data) +static VALUE +ossl_dsa_sign(VALUE self, VALUE data) { ossl_dsa *dsap = NULL; char *sig = NULL; @@ -389,7 +402,8 @@ static VALUE ossl_dsa_sign(VALUE self, VALUE data) return str; } -static VALUE ossl_dsa_verify(VALUE self, VALUE digest, VALUE sig) +static VALUE +ossl_dsa_verify(VALUE self, VALUE digest, VALUE sig) { ossl_dsa *dsap = NULL; int ret = -1; @@ -408,7 +422,11 @@ static VALUE ossl_dsa_verify(VALUE self, VALUE digest, VALUE sig) return Qnil; } -void Init_ossl_dsa(VALUE mPKey, VALUE cPKey, VALUE ePKeyError) +/* + * INIT + */ +void +Init_ossl_dsa(VALUE mPKey, VALUE cPKey, VALUE ePKeyError) { eDSAError = rb_define_class_under(mPKey, "DSAError", ePKeyError); diff --git a/ossl_pkey_rsa.c b/ossl_pkey_rsa.c index c8d9937..1254b4c 100644 --- a/ossl_pkey_rsa.c +++ b/ossl_pkey_rsa.c @@ -15,9 +15,7 @@ obj = Data_Make_Struct(cRSA, ossl_rsa, 0, ossl_rsa_free, rsap);\ rsap->pkey.get_EVP_PKEY = ossl_rsa_get_EVP_PKEY;\ } - #define GetRSA_unsafe(obj, rsap) Data_Get_Struct(obj, ossl_rsa, rsap) - #define GetRSA(obj, rsap) {\ GetRSA_unsafe(obj, rsap);\ if (!rsap->rsa) rb_raise(eRSAError, "not initialized!");\ @@ -39,7 +37,8 @@ typedef struct ossl_rsa_st { RSA *rsa; } ossl_rsa; -static void ossl_rsa_free(ossl_rsa *rsap) +static void +ossl_rsa_free(ossl_rsa *rsap) { if (rsap) { if (rsap->rsa) RSA_free(rsap->rsa); @@ -51,7 +50,8 @@ static void ossl_rsa_free(ossl_rsa *rsap) /* * Public */ -VALUE ossl_rsa_new_null() +VALUE +ossl_rsa_new_null() { ossl_rsa *rsap = NULL; VALUE obj; @@ -64,7 +64,8 @@ VALUE ossl_rsa_new_null() return obj; } -VALUE ossl_rsa_new(RSA *rsa) +VALUE +ossl_rsa_new(RSA *rsa) { ossl_rsa *rsap = NULL; VALUE obj; @@ -82,7 +83,8 @@ VALUE ossl_rsa_new(RSA *rsa) return obj; } -RSA *ossl_rsa_get_RSA(VALUE obj) +RSA * +ossl_rsa_get_RSA(VALUE obj) { ossl_rsa *rsap = NULL; RSA *rsa = NULL; @@ -97,7 +99,8 @@ RSA *ossl_rsa_get_RSA(VALUE obj) return rsa; } -EVP_PKEY *ossl_rsa_get_EVP_PKEY(VALUE obj) +EVP_PKEY * +ossl_rsa_get_EVP_PKEY(VALUE obj) { RSA *rsa = NULL; EVP_PKEY *pkey = NULL; @@ -120,7 +123,8 @@ EVP_PKEY *ossl_rsa_get_EVP_PKEY(VALUE obj) /* * Private */ -static VALUE ossl_rsa_s_new(int argc, VALUE *argv, VALUE klass) +static VALUE +ossl_rsa_s_new(int argc, VALUE *argv, VALUE klass) { ossl_rsa *rsap = NULL; VALUE obj; @@ -134,7 +138,8 @@ static VALUE ossl_rsa_s_new(int argc, VALUE *argv, VALUE klass) /* * CB for yielding when generating RSA data */ -static void ossl_rsa_generate_cb(int p, int n, void *arg) +static void +ossl_rsa_generate_cb(int p, int n, void *arg) { VALUE ary; @@ -145,7 +150,8 @@ static void ossl_rsa_generate_cb(int p, int n, void *arg) rb_yield(ary); } -static VALUE ossl_rsa_initialize(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_rsa_initialize(int argc, VALUE *argv, VALUE self) { ossl_rsa *rsap = NULL; RSA *rsa = NULL; @@ -203,7 +209,8 @@ static VALUE ossl_rsa_initialize(int argc, VALUE *argv, VALUE self) return self; } -static VALUE ossl_rsa_is_public(VALUE self) +static VALUE +ossl_rsa_is_public(VALUE self) { ossl_rsa *rsap = NULL; @@ -216,7 +223,8 @@ static VALUE ossl_rsa_is_public(VALUE self) return Qtrue; } -static VALUE ossl_rsa_is_private(VALUE self) +static VALUE +ossl_rsa_is_private(VALUE self) { ossl_rsa *rsap = NULL; @@ -225,7 +233,8 @@ static VALUE ossl_rsa_is_private(VALUE self) return (RSA_PRIVATE(rsap->rsa)) ? Qtrue : Qfalse; } -static VALUE ossl_rsa_export(int argc, VALUE *argv, VALUE self) +static VALUE +ossl_rsa_export(int argc, VALUE *argv, VALUE self) { ossl_rsa *rsap = NULL; BIO *out = NULL; @@ -268,7 +277,8 @@ static VALUE ossl_rsa_export(int argc, VALUE *argv, VALUE self) return str; } -static VALUE ossl_rsa_public_encrypt(VALUE self, VALUE buffer) +static VALUE +ossl_rsa_public_encrypt(VALUE self, VALUE buffer) { ossl_rsa *rsap = NULL; char *enc_text = NULL; @@ -293,7 +303,8 @@ static VALUE ossl_rsa_public_encrypt(VALUE self, VALUE buffer) return enc; } -static VALUE ossl_rsa_public_decrypt(VALUE self, VALUE buffer) +static VALUE +ossl_rsa_public_decrypt(VALUE self, VALUE buffer) { ossl_rsa *rsap = NULL; char *txt = NULL; @@ -318,7 +329,8 @@ static VALUE ossl_rsa_public_decrypt(VALUE self, VALUE buffer) return text; } -static VALUE ossl_rsa_private_encrypt(VALUE self, VALUE buffer) +static VALUE +ossl_rsa_private_encrypt(VALUE self, VALUE buffer) { ossl_rsa *rsap = NULL; char *enc_text = NULL; @@ -347,7 +359,8 @@ static VALUE ossl_rsa_private_encrypt(VALUE self, VALUE buffer) return enc; } -static VALUE ossl_rsa_private_decrypt(VALUE self, VALUE buffer) +static VALUE +ossl_rsa_private_decrypt(VALUE self, VALUE buffer) { ossl_rsa *rsap = NULL; char *txt = NULL; @@ -380,7 +393,8 @@ static VALUE ossl_rsa_private_decrypt(VALUE self, VALUE buffer) * Just sample * (it's not (maybe) wise to show private RSA values) */ -static VALUE ossl_rsa_get_n(VALUE self) +static VALUE +ossl_rsa_get_n(VALUE self) { ossl_rsa *rsap = NULL; BIO *out = NULL; @@ -403,7 +417,8 @@ static VALUE ossl_rsa_get_n(VALUE self) return num; } -static VALUE ossl_rsa_to_der(VALUE self) +static VALUE +ossl_rsa_to_der(VALUE self) { ossl_rsa *rsap = NULL; RSA *rsa = NULL; @@ -448,7 +463,8 @@ static VALUE ossl_rsa_to_der(VALUE self) * INSECURE: PRIVATE INFORMATIONS CAN LEAK OUT!!! * Don't use :-)) (I's up to you) */ -static VALUE ossl_rsa_to_str(VALUE self) +static VALUE +ossl_rsa_to_str(VALUE self) { ossl_rsa *rsap = NULL; BIO *out = NULL; @@ -474,7 +490,8 @@ static VALUE ossl_rsa_to_str(VALUE self) /* * Makes new instance RSA PUBLIC_KEY from PRIVATE_KEY */ -static VALUE ossl_rsa_to_public_key(VALUE self) +static VALUE +ossl_rsa_to_public_key(VALUE self) { ossl_rsa *rsap1 = NULL, *rsap2 = NULL; VALUE obj; @@ -492,7 +509,8 @@ static VALUE ossl_rsa_to_public_key(VALUE self) /* * Better to implement is in Ruby space? * -static VALUE ossl_rsa_sign(VALUE self, VALUE digest, VALUE text) +static VALUE +ossl_rsa_sign(VALUE self, VALUE digest, VALUE text) { ossl_rsa *rsap = NULL; EVP_MD_CTX ctx; @@ -523,11 +541,15 @@ static VALUE ossl_rsa_sign(VALUE self, VALUE digest, VALUE text) return str; } -static VALUE ossl_rsa_verify(VALUE self, VALUE digest, VALUE text) +static VALUE +ossl_rsa_verify(VALUE self, VALUE digest, VALUE text) { } */ +/* + * INIT + */ void Init_ossl_rsa(VALUE mPKey, VALUE cPKey, VALUE ePKeyError) { eRSAError = rb_define_class_under(mPKey, "RSAError", ePKeyError); @@ -548,7 +570,7 @@ void Init_ossl_rsa(VALUE mPKey, VALUE cPKey, VALUE ePKeyError) rb_define_method(cRSA, "n", ossl_rsa_get_n, 0); rb_define_method(cRSA, "to_der", ossl_rsa_to_der, 0); /* - * Rather in Ruby space? + * Implemented in Ruby space... * rb_define_method(cRSA, "sign", ossl_rsa_sign, 2); rb_define_method(cRSA, "verify", ossl_rsa_verify, 3); diff --git a/ossl_rand.c b/ossl_rand.c index ed78111..f13ce29 100644 --- a/ossl_rand.c +++ b/ossl_rand.c @@ -21,20 +21,22 @@ VALUE eRandomError; */ /* - * public + * Public */ /* - * private + * Private */ -static VALUE ossl_rand_seed(VALUE self, VALUE str) +static VALUE +ossl_rand_seed(VALUE self, VALUE str) { Check_SafeStr(str); RAND_seed(RSTRING(str)->ptr, RSTRING(str)->len); return str; } -static VALUE ossl_rand_load_file(VALUE self, VALUE filename) +static VALUE +ossl_rand_load_file(VALUE self, VALUE filename) { Check_SafeStr(filename); if(!RAND_load_file(RSTRING(filename)->ptr, -1)) { @@ -44,7 +46,8 @@ static VALUE ossl_rand_load_file(VALUE self, VALUE filename) return Qtrue; } -static VALUE ossl_rand_write_file(VALUE self, VALUE filename) +static VALUE +ossl_rand_write_file(VALUE self, VALUE filename) { Check_SafeStr(filename); if (RAND_write_file(RSTRING(filename)->ptr) == -1) { @@ -54,7 +57,8 @@ static VALUE ossl_rand_write_file(VALUE self, VALUE filename) return Qtrue; } -static VALUE ossl_rand_bytes(VALUE self, VALUE len) +static VALUE +ossl_rand_bytes(VALUE self, VALUE len) { unsigned char *buffer = NULL; VALUE str; @@ -76,7 +80,7 @@ static VALUE ossl_rand_bytes(VALUE self, VALUE len) } /* - * RAND init + * INIT */ void Init_ossl_rand(VALUE mOSSL) { diff --git a/ossl_version.h b/ossl_version.h index 1453a2a..b2c8e7b 100644 --- a/ossl_version.h +++ b/ossl_version.h @@ -1,6 +1,7 @@ #ifndef OSSL_VERSION_H #define OSSL_VERSION_H -#define OSSL_VERSION "0.0.9" +#define OSSL_VERSION "0.0.10-cvs" #endif + diff --git a/test/pkcs7.rb b/test/pkcs7.rb index 7852c05..b4f4b3b 100755 --- a/test/pkcs7.rb +++ b/test/pkcs7.rb @@ -13,8 +13,8 @@ cert = Certificate.new(str) key = RSA.new(str) p7 = PKCS7.new(SIGNED) -signer = Signer.new(key, cert, Digest::SHA1.new) -p7.add_signer(key, signer) +signer = Signer.new(cert, key, Digest::SHA1.new) +p7.add_signer(signer, key) p7.add_certificate(cert) p7.add_data(data, true) #...(data, (detached=false)) puts (str = p7.to_pem) |