diff options
author | Michal Rokos <m.rokos@sh.cvut.cz> | 2003-07-21 07:00:16 +0000 |
---|---|---|
committer | Michal Rokos <m.rokos@sh.cvut.cz> | 2003-07-21 07:00:16 +0000 |
commit | b4646f832b9f92e51ccf36823a9e3254c30e2c7c (patch) | |
tree | fae3456053b6793a12b261da6248385c343df5d1 | |
parent | 6b01bbcc031af7536a79426aa0893cab400a86cc (diff) | |
download | ruby-openssl-history-b4646f832b9f92e51ccf36823a9e3254c30e2c7c.tar.gz |
Digest is Ruby compatible again, more examples are working
-rw-r--r-- | ChangeLog | 5 | ||||
-rwxr-xr-x | examples/ca/gen_cert.rb | 4 | ||||
-rwxr-xr-x | examples/cert_store_view.rb | 2 | ||||
-rwxr-xr-x | examples/gen_crl.rb | 2 | ||||
-rwxr-xr-x | examples/gen_csr.rb | 2 | ||||
-rwxr-xr-x | examples/ossl_cipher.rb | 32 | ||||
-rwxr-xr-x | examples/ossl_config.rb | 17 | ||||
-rwxr-xr-x | examples/ossl_digest.rb | 2 | ||||
-rwxr-xr-x | examples/ossl_x509.rb | 4 | ||||
-rwxr-xr-x | examples/ossl_x509crl.rb | 10 | ||||
-rwxr-xr-x | examples/ossl_x509req.rb | 18 | ||||
-rw-r--r-- | lib/openssl/digest.rb | 4 | ||||
-rw-r--r-- | ossl_cipher.c | 6 | ||||
-rw-r--r-- | ossl_digest.c | 17 | ||||
-rw-r--r-- | ossl_pkey.c | 24 | ||||
-rw-r--r-- | ossl_x509cert.c | 6 |
16 files changed, 88 insertions, 67 deletions
@@ -1,3 +1,8 @@ +Mon, 21 Jul 2003 08:53:55 +0200 -- Michal Rokos <m.rokos@sh.cvut.cz> + * digest.c: Redo compatibility with original Ruby's digests + * pkey.c: Redo #to_der to be more straight going + * examples/*.rb: Fix parenthenses, File.open().read() -> File.read(), make some samples to work again + Tue, 15 Jul 2003 05:27:57 +0900 -- GOTOU Yuuzou <gotoyuzo@notwork.org> * extconf.rb: should make depend under $srcdir. * ossl.h: ossl_raise is a NORETURN. diff --git a/examples/ca/gen_cert.rb b/examples/ca/gen_cert.rb index 3d32c41..e3ce259 100755 --- a/examples/ca/gen_cert.rb +++ b/examples/ca/gen_cert.rb @@ -20,7 +20,7 @@ out_file = $OPT_out || 'cert.pem' csr_file = ARGV.shift or usage ARGV.empty? or usage -csr = X509::Request.new(File.open(csr_file).read) +csr = X509::Request.new(File.read(csr_file)) unless csr.verify(csr.public_key) raise "CSR sign verification failed." end @@ -50,7 +50,7 @@ ca_keypair_file = CAConfig::KEYPAIR_FILE puts "Reading CA keypair (from #{ca_keypair_file})" ca_keypair = PKey::RSA.new(File.read(ca_keypair_file), &CAConfig::PASSWD_CB) -serial = File.open(CAConfig::SERIAL_FILE, "r").read.chomp.hex +serial = File.read(CAConfig::SERIAL_FILE).chomp.hex File.open(CAConfig::SERIAL_FILE, "w") do |f| f << sprintf("%04X", serial + 1) end diff --git a/examples/cert_store_view.rb b/examples/cert_store_view.rb index 0728d61..472df31 100755 --- a/examples/cert_store_view.rb +++ b/examples/cert_store_view.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby18 +#!/usr/bin/env ruby require 'fox' require 'openssl' diff --git a/examples/gen_crl.rb b/examples/gen_crl.rb index a5ee388..30a97d5 100755 --- a/examples/gen_crl.rb +++ b/examples/gen_crl.rb @@ -25,7 +25,7 @@ def usage $stderr.puts $stderr.puts "Warning: You're publishing empty CRL." $stderr.puts "For revoking certificates use it like this:" - $stderr.puts "\t$ #{myname} Cert_to_revoke1.pem*" + $stderr.puts "\t$ #{myname} Cert_to_revoke1.pem [... Cert_to_revokeN.pem]" $stderr.puts end diff --git a/examples/gen_csr.rb b/examples/gen_csr.rb index 2a730ca..c22073b 100755 --- a/examples/gen_csr.rb +++ b/examples/gen_csr.rb @@ -29,7 +29,7 @@ name = X509::Name.new(name_ary) keypair = nil if keypair_file - keypair = PKey::RSA.new(File.open(keypair_file).read) + keypair = PKey::RSA.new(File.read(keypair_file)) else keypair = PKey::RSA.new(1024) { putc "." } puts diff --git a/examples/ossl_cipher.rb b/examples/ossl_cipher.rb index 806daa8..e198c46 100755 --- a/examples/ossl_cipher.rb +++ b/examples/ossl_cipher.rb @@ -2,14 +2,26 @@ require 'openssl' include OpenSSL -include Cipher - -p des = DES.new('EDE3', 'CBC') #Des3 CBC mode -p "ENCRYPT" -p des.encrypt("key")#, "iv12345678") -p cipher = des.update("abcdefghijklmnopqrstuvwxyz") -p cipher += des.cipher -p "DECRYPT" -p des.decrypt("key") #, "iv12345678") -p des.update(cipher) + des.cipher + +text = "abcdefghijklmnopqrstuvwxyz" +key = "key" +alg = "DES-EDE3-CBC" +#alg = "AES-128-CBC" + +puts "ClearText = \"#{text}\"" +puts "SymmetricKey = \"#{key}\"" +puts "CipherAlg = \"#{alg}\"" + +des = Cipher::Cipher.new(alg) +puts "--Encrypting with key--" +des.encrypt("key")#, "iv12345678") +cipher = des.update(text) +cipher += des.final +puts "EncryptedText = #{cipher.inspect}" +puts "--Decrypting with key--" +des.decrypt(key) #, "iv12345678") +out = des.update(cipher) + des.final +puts "DecryptedText = \"#{out}\"" + +puts "DONE." diff --git a/examples/ossl_config.rb b/examples/ossl_config.rb index e7f28b2..1609593 100755 --- a/examples/ossl_config.rb +++ b/examples/ossl_config.rb @@ -3,16 +3,11 @@ require 'openssl' include OpenSSL -p config = Config.load("./config.cnf") +config = Config.load("./config.cnf") -p string = config.get_value("req", "x509_extensions") -p string = config.get_value("req", "default_bits") -p number = config.get_value("req", "default_bits").to_i -p string = config.get_value("req", "distinguished_name") -p config.get_section("req") - -## -#DISABLED! -#p sect = config.get_section(string) -#p ConfigSection.new +p string = config.value("req", "x509_extensions") +p string = config.value("req", "default_bits") +p number = config.value("req", "default_bits").to_i +p string = config.value("req", "distinguished_name") +p config["req"] # or config.section("req") diff --git a/examples/ossl_digest.rb b/examples/ossl_digest.rb index 0ea7e0e..3edf889 100755 --- a/examples/ossl_digest.rb +++ b/examples/ossl_digest.rb @@ -4,7 +4,7 @@ require 'digest/sha1' require 'digest/md5' require 'openssl' -str = "This is only bullshit! :-))" +str = "This is only a dummy test :-))" md5 = Digest::MD5.new(str) md5a = OpenSSL::Digest::MD5.new(str) p md5.digest == md5a.digest diff --git a/examples/ossl_x509.rb b/examples/ossl_x509.rb index 6d6261e..0cbb943 100755 --- a/examples/ossl_x509.rb +++ b/examples/ossl_x509.rb @@ -5,7 +5,7 @@ include OpenSSL include X509 include PKey -p x509 = Certificate.new(File.open("./01cert.pem").read) +p x509 = Certificate.new(File.read("./1cert.pem")) #puts x509.to_pem #p x509.serial #puts "Version = #{x509.version}" @@ -24,7 +24,7 @@ p x509 = Certificate.new(File.open("./01cert.pem").read) #p k = x509.public_key #p k.private? #puts k.to_text -#p priv = RSA.new(File.open("./01key.pem").read, "pejs8nek") +#p priv = RSA.new(File.read("./1key.pem")) #p priv.private? #p x509.public_key = priv #puts x509.public_key.to_text diff --git a/examples/ossl_x509crl.rb b/examples/ossl_x509crl.rb index 7e19ac0..e9fb813 100755 --- a/examples/ossl_x509crl.rb +++ b/examples/ossl_x509crl.rb @@ -5,12 +5,14 @@ include OpenSSL include X509 include PKey -p ca = Certificate.new(File.open("./cacert.pem").read) +p ca = Certificate.new(File.read("./0cert.pem")) p key = ca.public_key -p crl = CRL.new(File.open("./01crl.pem").read) +p crl = CRL.new(File.read("./0crl.pem")) puts crl.to_text p crl.issuer.to_s -p crl.verify key -p crl.verify RSA.new(1024) +p crl.verify(key) +p crl.verify(RSA.new(1024)) crl.revoked.each {|rev| p rev.time} +puts "DOME." + diff --git a/examples/ossl_x509req.rb b/examples/ossl_x509req.rb index 215888e..939b961 100755 --- a/examples/ossl_x509req.rb +++ b/examples/ossl_x509req.rb @@ -6,18 +6,18 @@ include X509 include PKey p req = Request.new -p req = Request.new(File.open("./01req.pem").read) -p pkey = RSA.new(File.open("./02key.pem").read, "alfa") -p k2 = Certificate.new(File.open("./02cert.pem").read).public_key +p req = Request.new(File.read("./1req.pem")) +p pkey = RSA.new(File.read("./2key.pem")) +p k2 = Certificate.new(File.read("./2cert.pem")).public_key #puts req.to_pem #p req.methods.sort p key = req.public_key -p req.verify key -p req.verify pkey -p req.verify k2 +p req.verify(key) +p req.verify(pkey) +p req.verify(k2) p req.public_key = k2 p req.sign(pkey, Digest::MD5.new) -p req.verify key -p req.verify pkey -p req.verify k2 +p req.verify(key) +p req.verify(pkey) +p req.verify(k2) puts req.to_text diff --git a/lib/openssl/digest.rb b/lib/openssl/digest.rb index 66d41b3..fe638b8 100644 --- a/lib/openssl/digest.rb +++ b/lib/openssl/digest.rb @@ -34,8 +34,8 @@ module Digest ].each do |digest| eval(<<-EOD) class #{digest} < Digest - def initialize() - super(\"#{digest}\") + def initialize(str=nil) + super(\"#{digest}\", str) end def #{digest}::digest(data) Digest::digest(\"#{digest}\", data) diff --git a/ossl_cipher.c b/ossl_cipher.c index c50c6f0..eef7c90 100644 --- a/ossl_cipher.c +++ b/ossl_cipher.c @@ -287,7 +287,7 @@ ossl_cipher_set_key(VALUE self, VALUE key) if (EVP_CipherInit(ctx, NULL, RSTRING(key)->ptr, NULL, -1) != 1) ossl_raise(eCipherError, NULL); - return Qnil; + return key; } static VALUE @@ -304,7 +304,7 @@ ossl_cipher_set_iv(VALUE self, VALUE iv) if (EVP_CipherInit(ctx, NULL, NULL, RSTRING(iv)->ptr, -1) != 1) ossl_raise(eCipherError, NULL); - return Qnil; + return iv; } static VALUE @@ -317,7 +317,7 @@ ossl_cipher_set_padding(VALUE self, VALUE padding) if (EVP_CIPHER_CTX_set_padding(ctx, NUM2INT(padding)) != 1) ossl_raise(eCipherError, NULL); - return Qnil; + return padding; } #define CIPHER_0ARG_INT(func) \ diff --git a/ossl_digest.c b/ossl_digest.c index caca95b..e04b145 100644 --- a/ossl_digest.c +++ b/ossl_digest.c @@ -59,15 +59,21 @@ ossl_digest_alloc(VALUE klass) } DEFINE_ALLOC_WRAPPER(ossl_digest_alloc) +VALUE ossl_digest_update(VALUE, VALUE); + static VALUE -ossl_digest_initialize(VALUE self, VALUE str) +ossl_digest_initialize(int argc, VALUE *argv, VALUE self) { EVP_MD_CTX *ctx; const EVP_MD *md; char *name; - + VALUE type, data; + GetDigest(self, ctx); - name = StringValuePtr(str); + + rb_scan_args(argc, argv, "11", &type, &data); + name = StringValuePtr(type); + if (!NIL_P(data)) StringValue(data); md = EVP_get_digestbyname(name); if (!md) { @@ -75,6 +81,7 @@ ossl_digest_initialize(VALUE self, VALUE str) } EVP_DigestInit(ctx, md); + if (!NIL_P(data)) return ossl_digest_update(self, data); return self; } @@ -106,7 +113,7 @@ ossl_digest_reset(VALUE self) return self; } -static VALUE +VALUE ossl_digest_update(VALUE self, VALUE data) { EVP_MD_CTX *ctx; @@ -262,7 +269,7 @@ Init_ossl_digest() rb_define_singleton_method(cDigest, "digest", ossl_digest_s_digest, 2); rb_define_singleton_method(cDigest, "hexdigest", ossl_digest_s_hexdigest, 2); - rb_define_method(cDigest, "initialize", ossl_digest_initialize, 1); + rb_define_method(cDigest, "initialize", ossl_digest_initialize, -1); rb_define_method(cDigest, "reset", ossl_digest_reset, 0); rb_define_copy_func(cDigest, ossl_digest_copy); diff --git a/ossl_pkey.c b/ossl_pkey.c index b8c5f87..cd73355 100644 --- a/ossl_pkey.c +++ b/ossl_pkey.c @@ -134,24 +134,24 @@ static VALUE ossl_pkey_to_der(VALUE self) { EVP_PKEY *pkey; - X509_PUBKEY *key; VALUE str; - ASN1_OCTET_STRING *oc; + BIO *out; + BUF_MEM *buf; GetPKey(self, pkey); - if (!(key = X509_PUBKEY_new())) { - ossl_raise(ePKeyError, NULL); - } - if (!X509_PUBKEY_set(&key, pkey)) { - X509_PUBKEY_free(key); + + out = BIO_new(BIO_s_mem()); + if (!out) ossl_raise(ePKeyError, NULL); + + if (!i2d_PUBKEY_bio(out, pkey)) { + BIO_free(out); ossl_raise(ePKeyError, NULL); } + + BIO_get_mem_ptr(out, &buf); + str = rb_str_new(buf->data, buf->length); - oc = ASN1_item_pack(key, ASN1_ITEM_rptr(X509_PUBKEY), NULL); - str = rb_str_new(oc->data, oc->length); - - X509_PUBKEY_free(key); - ASN1_OCTET_STRING_free(oc); + BIO_free(out); return str; } diff --git a/ossl_x509cert.c b/ossl_x509cert.c index 6322740..b6ed438 100644 --- a/ossl_x509cert.c +++ b/ossl_x509cert.c @@ -117,9 +117,9 @@ ossl_x509_alloc(VALUE klass) X509 *x509; VALUE obj; - if (!(x509 = X509_new())) { - ossl_raise(eX509CertError, NULL); - } + x509 = X509_new(); + if (!x509) ossl_raise(eX509CertError, NULL); + WrapX509(klass, obj, x509); return obj; |