From ba675cd156747fbb90e7b961d7bed86d8a834f74 Mon Sep 17 00:00:00 2001 From: Lars Westergren Date: Fri, 25 Oct 2013 10:29:59 +0200 Subject: [PATCH 1/3] Tests for PKCS12 - allow creating from UTF8 strings but coerce to ASCII. Bug 1092. --- test/mri/openssl/test_pkcs12.rb | 48 +++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/test/mri/openssl/test_pkcs12.rb b/test/mri/openssl/test_pkcs12.rb index cdc00b2d3a3..da1de167a7c 100644 --- a/test/mri/openssl/test_pkcs12.rb +++ b/test/mri/openssl/test_pkcs12.rb @@ -52,31 +52,28 @@ def setup end def test_create - pkcs12 = OpenSSL::PKCS12.create( - "omg", - "hello", - TEST_KEY_RSA1024, - @mycert - ) + pkcs12 = create_store assert_equal @mycert, pkcs12.certificate assert_equal TEST_KEY_RSA1024, pkcs12.key assert_nil pkcs12.ca_certs end - def test_load - temp = OpenSSL::PKCS12.create( - "omg", - "hello", - TEST_KEY_RSA1024, - @mycert - ) - - Tempfile.open('test_cert.pk12') do |f| - f << temp.to_der - f.rewind + def test_from_file + tempfile do |f| OpenSSL::PKCS12.new(f, 'omg') end + end + def test_from_ascii_string + tempfile do |f| + OpenSSL::PKCS12.new(f.read.force_encoding(Encoding::ASCII_8BIT), 'omg') + end + end + + def test_from_utf8_string + tempfile do |f| + OpenSSL::PKCS12.new(f.read.force_encoding(Encoding::UTF_8), 'omg') + end end def test_create_no_pass @@ -219,6 +216,23 @@ def assert_include_cert cert, ary false end + def create_store + OpenSSL::PKCS12.create( + "omg", + "hello", + TEST_KEY_RSA1024, + @mycert + ) + end + + def tempfile store=create_store + Tempfile.open('test_cert.pk12') do |f| + f << store.to_der + f.rewind + yield f + end + end + end end From c65cfa57477cf8542ddd803f265b0850e4958e50 Mon Sep 17 00:00:00 2001 From: Lars Westergren Date: Fri, 25 Oct 2013 10:34:18 +0200 Subject: [PATCH 2/3] Coerce PKCS12 init string to ASCII_8BIT if not. --- lib/ruby/shared/openssl/pkcs12.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ruby/shared/openssl/pkcs12.rb b/lib/ruby/shared/openssl/pkcs12.rb index e11fb49065f..559253b8a1c 100644 --- a/lib/ruby/shared/openssl/pkcs12.rb +++ b/lib/ruby/shared/openssl/pkcs12.rb @@ -31,6 +31,7 @@ def initialize(str = nil, password = '') @der = file.read file.close else + str.force_encoding(Encoding::ASCII_8BIT) unless str.encoding == Encoding::ASCII_8BIT @der = str end From a5861b1c9fa5ea589fccf069fa10feee5caae0ae Mon Sep 17 00:00:00 2001 From: Lars Westergren Date: Tue, 29 Oct 2013 12:57:08 +0100 Subject: [PATCH 3/3] Remove unnecessary conditional. --- lib/ruby/shared/openssl/pkcs12.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby/shared/openssl/pkcs12.rb b/lib/ruby/shared/openssl/pkcs12.rb index 559253b8a1c..adb7c33be85 100644 --- a/lib/ruby/shared/openssl/pkcs12.rb +++ b/lib/ruby/shared/openssl/pkcs12.rb @@ -31,7 +31,7 @@ def initialize(str = nil, password = '') @der = file.read file.close else - str.force_encoding(Encoding::ASCII_8BIT) unless str.encoding == Encoding::ASCII_8BIT + str.force_encoding(Encoding::ASCII_8BIT) @der = str end