[前][次][番号順一覧][スレッド一覧]

ruby-changes:39301

From: tenderlove <ko1@a...>
Date: Sun, 26 Jul 2015 08:35:58 +0900 (JST)
Subject: [ruby-changes:39301] tenderlove:r51382 (trunk): * ext/openssl/lib/openssl/pkey.rb: implement DEFAULT_512 and

tenderlove	2015-07-26 08:35:49 +0900 (Sun, 26 Jul 2015)

  New Revision: 51382

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51382

  Log:
    * ext/openssl/lib/openssl/pkey.rb: implement DEFAULT_512 and
      DEFAULT_1024 constants in Ruby.
    
    * ext/openssl/lib/openssl/ssl.rb (module OpenSSL): Ask PKey for the
      default DH callback since it aleady must check whether openssl has
      been compiled with DH support.
    
    * ext/openssl/ossl_pkey_dh.c (OSSL_PKEY_BN): Remove C definitions of
      DEFAULT_512 and DEFAULT_1024
    
    * ext/openssl/ossl_pkey_dh.c (Init_ossl_dh): ditto
    
    * test/openssl/test_pkey_dh.rb (class OpenSSL): add test to ensure the
      Ruby definitions are the same as the C definitions were.

  Added files:
    trunk/ext/openssl/lib/openssl/pkey.rb
  Modified files:
    trunk/ChangeLog
    trunk/ext/openssl/lib/openssl/ssl.rb
    trunk/ext/openssl/lib/openssl.rb
    trunk/ext/openssl/ossl_pkey_dh.c
    trunk/test/openssl/test_pkey_dh.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51381)
+++ ChangeLog	(revision 51382)
@@ -1,3 +1,20 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jul 26 08:33:03 2015  Aaron Patterson <tenderlove@r...>
+
+	* ext/openssl/lib/openssl/pkey.rb: implement DEFAULT_512 and
+	  DEFAULT_1024 constants in Ruby.
+
+	* ext/openssl/lib/openssl/ssl.rb (module OpenSSL): Ask PKey for the
+	  default DH callback since it aleady must check whether openssl has
+	  been compiled with DH support.
+
+	* ext/openssl/ossl_pkey_dh.c (OSSL_PKEY_BN): Remove C definitions of
+	  DEFAULT_512 and DEFAULT_1024
+
+	* ext/openssl/ossl_pkey_dh.c (Init_ossl_dh): ditto
+
+	* test/openssl/test_pkey_dh.rb (class OpenSSL): add test to ensure the
+	  Ruby definitions are the same as the C definitions were.
+
 Sun Jul 26 08:14:59 2015  Aaron Patterson <tenderlove@r...>
 
 	* ext/openssl/lib/openssl/ssl.rb (module OpenSSL): support
Index: ext/openssl/lib/openssl/ssl.rb
===================================================================
--- ext/openssl/lib/openssl/ssl.rb	(revision 51381)
+++ ext/openssl/lib/openssl/ssl.rb	(revision 51382)
@@ -74,20 +74,6 @@ module OpenSSL https://github.com/ruby/ruby/blob/trunk/ext/openssl/lib/openssl/ssl.rb#L74
         DEFAULT_CERT_STORE.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL
       end
 
-      if defined?(OpenSSL::PKey::DH)
-        DEFAULT_TMP_DH_CALLBACK = lambda { |ctx, is_export, keylen|
-          warn "using default DH parameters." if $VERBOSE
-          case keylen
-          when 512  then OpenSSL::PKey::DH::DEFAULT_512
-          when 1024 then OpenSSL::PKey::DH::DEFAULT_1024
-          else
-            nil
-          end
-        }
-      else
-        DEFAULT_TMP_DH_CALLBACK = nil
-      end
-
       INIT_VARS = ["cert", "key", "client_ca", "ca_file", "ca_path",
         "timeout", "verify_mode", "verify_depth", "renegotiation_cb",
         "verify_callback", "options", "cert_store", "extra_chain_cert",
@@ -105,7 +91,7 @@ module OpenSSL https://github.com/ruby/ruby/blob/trunk/ext/openssl/lib/openssl/ssl.rb#L91
       # You can get a list of valid methods with OpenSSL::SSL::SSLContext::METHODS
       def initialize(version = nil)
         INIT_VARS.each { |v| instance_variable_set v, nil }
-        @tmp_dh_callback = DEFAULT_TMP_DH_CALLBACK
+        @tmp_dh_callback = OpenSSL::PKey::DEFAULT_TMP_DH_CALLBACK
         return unless version
         self.ssl_version = version
       end
@@ -130,7 +116,7 @@ module OpenSSL https://github.com/ruby/ruby/blob/trunk/ext/openssl/lib/openssl/ssl.rb#L116
       end
 
       def tmp_dh_callback=(value)
-        @tmp_dh_callback = value || DEFAULT_TMP_DH_CALLBACK
+        @tmp_dh_callback = value || OpenSSL::PKey::DEFAULT_TMP_DH_CALLBACK
       end
     end
 
Index: ext/openssl/lib/openssl/pkey.rb
===================================================================
--- ext/openssl/lib/openssl/pkey.rb	(revision 0)
+++ ext/openssl/lib/openssl/pkey.rb	(revision 51382)
@@ -0,0 +1,36 @@ https://github.com/ruby/ruby/blob/trunk/ext/openssl/lib/openssl/pkey.rb#L1
+module OpenSSL
+  module PKey
+    if defined?(OpenSSL::PKey::DH)
+
+    class DH
+      DEFAULT_512 = new <<-_end_of_pem_
+-----BEGIN DH PARAMETERS-----
+MEYCQQD0zXHljRg/mJ9PYLACLv58Cd8VxBxxY7oEuCeURMiTqEhMym16rhhKgZG2
+zk2O9uUIBIxSj+NKMURHGaFKyIvLAgEC
+-----END DH PARAMETERS-----
+      _end_of_pem_
+
+      DEFAULT_1024 = new <<-_end_of_pem_
+-----BEGIN DH PARAMETERS-----
+MIGHAoGBAJ0lOVy0VIr/JebWn0zDwY2h+rqITFOpdNr6ugsgvkDXuucdcChhYExJ
+AV/ZD2AWPbrTqV76mGRgJg4EddgT1zG0jq3rnFdMj2XzkBYx3BVvfR0Arnby0RHR
+T4h7KZ/2zmjvV+eF8kBUHBJAojUlzxKj4QeO2x20FP9X5xmNUXeDAgEC
+-----END DH PARAMETERS-----
+      _end_of_pem_
+    end
+
+    DEFAULT_TMP_DH_CALLBACK = lambda { |ctx, is_export, keylen|
+      warn "using default DH parameters." if $VERBOSE
+      case keylen
+      when 512  then OpenSSL::PKey::DH::DEFAULT_512
+      when 1024 then OpenSSL::PKey::DH::DEFAULT_1024
+      else
+        nil
+      end
+    }
+
+    else
+      DEFAULT_TMP_DH_CALLBACK = nil
+    end
+  end
+end
Index: ext/openssl/lib/openssl.rb
===================================================================
--- ext/openssl/lib/openssl.rb	(revision 51381)
+++ ext/openssl/lib/openssl.rb	(revision 51382)
@@ -17,6 +17,7 @@ https://github.com/ruby/ruby/blob/trunk/ext/openssl/lib/openssl.rb#L17
 require 'openssl.so'
 
 require 'openssl/bn'
+require 'openssl/pkey'
 require 'openssl/cipher'
 require 'openssl/config'
 require 'openssl/digest'
Index: ext/openssl/ossl_pkey_dh.c
===================================================================
--- ext/openssl/ossl_pkey_dh.c	(revision 51381)
+++ ext/openssl/ossl_pkey_dh.c	(revision 51382)
@@ -524,67 +524,6 @@ OSSL_PKEY_BN(dh, pub_key) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey_dh.c#L524
 OSSL_PKEY_BN(dh, priv_key)
 
 /*
- * -----BEGIN DH PARAMETERS-----
- * MEYCQQD0zXHljRg/mJ9PYLACLv58Cd8VxBxxY7oEuCeURMiTqEhMym16rhhKgZG2
- * zk2O9uUIBIxSj+NKMURHGaFKyIvLAgEC
- * -----END DH PARAMETERS-----
- */
-static unsigned char DEFAULT_DH_512_PRIM[] = {
-    0xf4, 0xcd, 0x71, 0xe5, 0x8d, 0x18, 0x3f, 0x98,
-    0x9f, 0x4f, 0x60, 0xb0, 0x02, 0x2e, 0xfe, 0x7c,
-    0x09, 0xdf, 0x15, 0xc4, 0x1c, 0x71, 0x63, 0xba,
-    0x04, 0xb8, 0x27, 0x94, 0x44, 0xc8, 0x93, 0xa8,
-    0x48, 0x4c, 0xca, 0x6d, 0x7a, 0xae, 0x18, 0x4a,
-    0x81, 0x91, 0xb6, 0xce, 0x4d, 0x8e, 0xf6, 0xe5,
-    0x08, 0x04, 0x8c, 0x52, 0x8f, 0xe3, 0x4a, 0x31,
-    0x44, 0x47, 0x19, 0xa1, 0x4a, 0xc8, 0x8b, 0xcb,
-};
-static unsigned char DEFAULT_DH_512_GEN[] = { 0x02 };
-
-/*
- * -----BEGIN DH PARAMETERS-----
- * MIGHAoGBAJ0lOVy0VIr/JebWn0zDwY2h+rqITFOpdNr6ugsgvkDXuucdcChhYExJ
- * AV/ZD2AWPbrTqV76mGRgJg4EddgT1zG0jq3rnFdMj2XzkBYx3BVvfR0Arnby0RHR
- * T4h7KZ/2zmjvV+eF8kBUHBJAojUlzxKj4QeO2x20FP9X5xmNUXeDAgEC
- * -----END DH PARAMETERS-----
- */
-static unsigned char DEFAULT_DH_1024_PRIM[] = {
-    0x9d, 0x25, 0x39, 0x5c, 0xb4, 0x54, 0x8a, 0xff,
-    0x25, 0xe6, 0xd6, 0x9f, 0x4c, 0xc3, 0xc1, 0x8d,
-    0xa1, 0xfa, 0xba, 0x88, 0x4c, 0x53, 0xa9, 0x74,
-    0xda, 0xfa, 0xba, 0x0b, 0x20, 0xbe, 0x40, 0xd7,
-    0xba, 0xe7, 0x1d, 0x70, 0x28, 0x61, 0x60, 0x4c,
-    0x49, 0x01, 0x5f, 0xd9, 0x0f, 0x60, 0x16, 0x3d,
-    0xba, 0xd3, 0xa9, 0x5e, 0xfa, 0x98, 0x64, 0x60,
-    0x26, 0x0e, 0x04, 0x75, 0xd8, 0x13, 0xd7, 0x31,
-    0xb4, 0x8e, 0xad, 0xeb, 0x9c, 0x57, 0x4c, 0x8f,
-    0x65, 0xf3, 0x90, 0x16, 0x31, 0xdc, 0x15, 0x6f,
-    0x7d, 0x1d, 0x00, 0xae, 0x76, 0xf2, 0xd1, 0x11,
-    0xd1, 0x4f, 0x88, 0x7b, 0x29, 0x9f, 0xf6, 0xce,
-    0x68, 0xef, 0x57, 0xe7, 0x85, 0xf2, 0x40, 0x54,
-    0x1c, 0x12, 0x40, 0xa2, 0x35, 0x25, 0xcf, 0x12,
-    0xa3, 0xe1, 0x07, 0x8e, 0xdb, 0x1d, 0xb4, 0x14,
-    0xff, 0x57, 0xe7, 0x19, 0x8d, 0x51, 0x77, 0x83
-};
-static unsigned char DEFAULT_DH_1024_GEN[] = { 0x02 };
-
-static DH*
-ossl_create_dh(unsigned char *p, size_t plen, unsigned char *g, size_t glen)
-{
-    DH *dh;
-
-    if ((dh = DH_new()) == NULL) ossl_raise(eDHError, NULL);
-    dh->p = BN_bin2bn(p, rb_long2int(plen), NULL);
-    dh->g = BN_bin2bn(g, rb_long2int(glen), NULL);
-    if (dh->p == NULL || dh->g == NULL){
-        DH_free(dh);
-	ossl_raise(eDHError, NULL);
-    }
-
-    return dh;
-}
-
-/*
  * INIT
  */
 void
@@ -594,8 +533,6 @@ Init_ossl_dh(void) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey_dh.c#L533
     mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL and mPKey */
     mPKey = rb_define_module_under(mOSSL, "PKey");
 #endif
-    DH *OSSL_DEFAULT_DH_512;
-    DH *OSSL_DEFAULT_DH_1024;
 
     /* Document-class: OpenSSL::PKey::DHError
      *
@@ -651,16 +588,6 @@ Init_ossl_dh(void) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey_dh.c#L588
     DEF_OSSL_PKEY_BN(cDH, dh, pub_key);
     DEF_OSSL_PKEY_BN(cDH, dh, priv_key);
     rb_define_method(cDH, "params", ossl_dh_get_params, 0);
-
-    OSSL_DEFAULT_DH_512 = ossl_create_dh(
-	DEFAULT_DH_512_PRIM, sizeof(DEFAULT_DH_512_PRIM),
-	DEFAULT_DH_512_GEN, sizeof(DEFAULT_DH_512_GEN));
-    OSSL_DEFAULT_DH_1024 = ossl_create_dh(
-	DEFAULT_DH_1024_PRIM, sizeof(DEFAULT_DH_1024_PRIM),
-	DEFAULT_DH_1024_GEN, sizeof(DEFAULT_DH_1024_GEN));
-
-    rb_define_const(cDH, "DEFAULT_512", dh_instance(cDH, OSSL_DEFAULT_DH_512));
-    rb_define_const(cDH, "DEFAULT_1024", dh_instance(cDH, OSSL_DEFAULT_DH_1024));
 }
 
 #else /* defined NO_DH */
Index: test/openssl/test_pkey_dh.rb
===================================================================
--- test/openssl/test_pkey_dh.rb	(revision 51381)
+++ test/openssl/test_pkey_dh.rb	(revision 51382)
@@ -6,6 +6,27 @@ class OpenSSL::TestPKeyDH < Test::Unit:: https://github.com/ruby/ruby/blob/trunk/test/openssl/test_pkey_dh.rb#L6
 
   NEW_KEYLEN = 256
 
+  def test_DEFAULT_512
+    params = <<-eop
+-----BEGIN DH PARAMETERS-----
+MEYCQQD0zXHljRg/mJ9PYLACLv58Cd8VxBxxY7oEuCeURMiTqEhMym16rhhKgZG2
+zk2O9uUIBIxSj+NKMURHGaFKyIvLAgEC
+-----END DH PARAMETERS-----
+    eop
+    assert_equal params, OpenSSL::PKey::DH::DEFAULT_512.to_s
+  end
+
+  def test_DEFAULT_1024
+    params = <<-eop
+-----BEGIN DH PARAMETERS-----
+MIGHAoGBAJ0lOVy0VIr/JebWn0zDwY2h+rqITFOpdNr6ugsgvkDXuucdcChhYExJ
+AV/ZD2AWPbrTqV76mGRgJg4EddgT1zG0jq3rnFdMj2XzkBYx3BVvfR0Arnby0RHR
+T4h7KZ/2zmjvV+eF8kBUHBJAojUlzxKj4QeO2x20FP9X5xmNUXeDAgEC
+-----END DH PARAMETERS-----
+    eop
+    assert_equal params, OpenSSL::PKey::DH::DEFAULT_1024.to_s
+  end
+
   def test_new
     dh = OpenSSL::PKey::DH.new(NEW_KEYLEN)
     assert_key(dh)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]