mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
609103dbb5
Import Ruby/OpenSSL 2.1.0.beta1. The full commit log since v2.0.5 (imported by r59567) can be found at: https://github.com/ruby/openssl/compare/v2.0.5...v2.1.0.beta1 ---------------------------------------------------------------- Antonio Terceiro (1): test/test_ssl: explicitly accept TLS 1.1 in corresponding test Colby Swandale (1): document using secure protocol to fetch git master in Bundler Colton Jenkins (1): Add fips_mode_get to return fips_mode Kazuki Yamaguchi (85): Start preparing for 2.1.0 Remove support for OpenSSL 0.9.8 and 1.0.0 bn: refine tests bn: implement unary {plus,minus} operators for OpenSSL::BN bn: implement OpenSSL::BN#negative? Don't define main() when built with --enable-debug test: let OpenSSL::TestCase include OpenSSL::TestUtils test: prepare test PKey instances on demand Add OpenSSL.print_mem_leaks Enable OSSL_MDEBUG on CI builds ssl: move default DH parameters from OpenSSL::PKey::DH Make exceptions with the same format regardless of OpenSSL.debug ssl: show reason of 'certificate verify error' in exception message ssl: remove OpenSSL::ExtConfig::TLS_DH_anon_WITH_AES_256_GCM_SHA384 ssl: do not confuse different ex_data index registries ssl: assume SSL/SSL_CTX always have a valid reference to the Ruby object Fix RDoc markup ssl: suppress compiler warning ext/openssl/deprecation.rb: remove broken-apple-openssl extconf.rb: print informative message if OpenSSL can't be found Rakefile: compile the extension before test kdf: introduce OpenSSL::KDF module ossl.h: add NUM2UINT64T() macro kdf: add scrypt Expand rb_define_copy_func() macro Expand FPTR_TO_FD() macro Remove SafeGet*() macros cipher: rename GetCipherPtr() to ossl_evp_get_cipherbyname() digest: rename GetDigestPtr() to ossl_evp_get_digestbyname() Add ossl_str_new(), an exception-safe rb_str_new() bio: simplify ossl_membio2str() using ossl_str_new() Remove unused functions and macros Drop support for LibreSSL 2.3 ocsp: add OpenSSL::OCSP::Request#signed? asn1: infinite length -> indefinite length asn1: rearrange tests ssl: remove a needless NULL check in SSL::SSLContext#ciphers ssl: return nil in SSL::SSLSocket#cipher if session is not started asn1: remove an unnecessary function prototype asn1: require tag information when instantiating generic type asn1: initialize 'unused_bits' attribute of BitString with 0 asn1: check for illegal 'unused_bits' value of BitString asn1: disallow NULL to be passed to asn1time_to_time() asn1: avoid truncating OID in OpenSSL::ASN1::ObjectId#oid asn1: allow constructed encoding with definite length form asn1: prohibit indefinite length form for primitive encoding asn1: allow tag number to be >= 32 for universal tag class asn1: use ossl_asn1_tag() asn1: clean up OpenSSL::ASN1::Constructive#to_der asn1: harmonize OpenSSL::ASN1::*#to_der asn1: prevent EOC octets from being in the middle of the content asn1: do not treat EOC octets as part of content octets x509name: add 'loc' and 'set' kwargs to OpenSSL::X509::Name#add_entry ssl: do not call session_remove_cb during GC Backport "Merge branch 'topic/test-memory-leak'" to maint cipher: update the documentation for Cipher#auth_tag= Rakefile: let sync:to_ruby know about test/openssl/fixtures test: fix formatting test/utils: remove OpenSSL::TestUtils.silent test/utils: add SSLTestCase#tls12_supported? test/utils: have start_server yield only the port number test/utils: do not set ecdh_curves in start_server test/utils: let server_loop close socket test/utils: improve error handling in start_server test/utils: add OpenSSL::TestUtils.openssl? and .libressl? test/utils: do not use DSA certificates in SSL tests test/test_ssl: remove test_invalid_shutdown_by_gc test/test_ssl: move test_multibyte_read_write to test_pair test/test_ssl_session: rearrange tests test/test_pair, test/test_ssl: fix for TLS 1.3 ssl: remove useless call to rb_thread_wait_fd() ssl: fix NPN support ssl: mark OpenSSL::SSL::SSLContext::DEFAULT_{1024,2048} as private ssl: use 2048-bit group in the default tmp_dh_cb ssl: ensure that SSL option flags are non-negative ssl: update OpenSSL::SSL::OP_* flags ssl: prefer TLS_method() over SSLv23_method() ssl: add SSLContext#min_version= and #max_version= ssl: rework SSLContext#ssl_version= test/test_x509name: change script encoding to ASCII-8BIT x509name: refactor OpenSSL::X509::Name#to_s x509name: add OpenSSL::X509::Name#to_utf8 x509name: add OpenSSL::X509::Name#inspect x509name: update regexp in OpenSSL::X509::Name.parse Ruby/OpenSSL 2.1.0.beta1 Marcus Stollsteimer (1): Fix rdoc for core Integer class nobu (4): [DOC] {read,write}_nonblock with exception: false [DOC] keyword argument _exception_ [DOC] mark up literals Revert r57690 except for read_nonblock git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59734 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
276 lines
8.1 KiB
Ruby
276 lines
8.1 KiB
Ruby
# coding: us-ascii
|
|
# frozen_string_literal: false
|
|
require_relative 'utils'
|
|
require "prime"
|
|
|
|
if defined?(OpenSSL)
|
|
|
|
class OpenSSL::TestBN < OpenSSL::TestCase
|
|
def setup
|
|
super
|
|
@e1 = OpenSSL::BN.new(999.to_s(16), 16) # OpenSSL::BN.new(str, 16) must be most stable
|
|
@e2 = OpenSSL::BN.new("-" + 999.to_s(16), 16)
|
|
@e3 = OpenSSL::BN.new((2**107-1).to_s(16), 16)
|
|
@e4 = OpenSSL::BN.new("-" + (2**107-1).to_s(16), 16)
|
|
end
|
|
|
|
def test_new
|
|
assert_equal(@e1, OpenSSL::BN.new("999"))
|
|
assert_equal(@e1, OpenSSL::BN.new("999", 10))
|
|
assert_equal(@e1, OpenSSL::BN.new("\x03\xE7", 2))
|
|
assert_equal(@e1, OpenSSL::BN.new("\x00\x00\x00\x02\x03\xE7", 0))
|
|
assert_equal(@e2, OpenSSL::BN.new("-999"))
|
|
assert_equal(@e2, OpenSSL::BN.new("-999", 10))
|
|
assert_equal(@e2, OpenSSL::BN.new("\x00\x00\x00\x02\x83\xE7", 0))
|
|
assert_equal(@e3, OpenSSL::BN.new((2**107-1).to_s))
|
|
assert_equal(@e3, OpenSSL::BN.new((2**107-1).to_s, 10))
|
|
assert_equal(@e3, OpenSSL::BN.new("\a\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 2))
|
|
assert_equal(@e3, OpenSSL::BN.new("\x00\x00\x00\x0E\a\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0))
|
|
assert_equal(@e4, OpenSSL::BN.new("-" + (2**107-1).to_s))
|
|
assert_equal(@e4, OpenSSL::BN.new("-" + (2**107-1).to_s, 10))
|
|
assert_equal(@e4, OpenSSL::BN.new("\x00\x00\x00\x0E\x87\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0))
|
|
|
|
e1copy = OpenSSL::BN.new(@e1)
|
|
assert_equal(@e1, e1copy)
|
|
e1copy.clear_bit!(0) #=> 998
|
|
assert_not_equal(@e1, e1copy)
|
|
|
|
assert_equal(@e1, OpenSSL::BN.new(999))
|
|
assert_equal(@e2, OpenSSL::BN.new(-999))
|
|
assert_equal(@e3, OpenSSL::BN.new(2**107-1))
|
|
assert_equal(@e4, OpenSSL::BN.new(-(2**107-1)))
|
|
|
|
assert_equal(@e1, 999.to_bn)
|
|
assert_equal(@e2, -999.to_bn)
|
|
assert_equal(@e3, (2**107-1).to_bn)
|
|
assert_equal(@e4, (-(2**107-1)).to_bn)
|
|
end
|
|
|
|
def test_to_str
|
|
assert_equal("999", @e1.to_s(10))
|
|
assert_equal("-999", @e2.to_s(10))
|
|
assert_equal((2**107-1).to_s, @e3.to_s(10))
|
|
assert_equal((-(2**107-1)).to_s, @e4.to_s(10))
|
|
assert_equal("999", @e1.to_s)
|
|
|
|
assert_equal("03E7", @e1.to_s(16))
|
|
assert_equal("-03E7", @e2.to_s(16))
|
|
assert_equal("07FFFFFFFFFFFFFFFFFFFFFFFFFF", @e3.to_s(16))
|
|
assert_equal("-07FFFFFFFFFFFFFFFFFFFFFFFFFF", @e4.to_s(16))
|
|
|
|
assert_equal("\x03\xe7", @e1.to_s(2))
|
|
assert_equal("\x03\xe7", @e2.to_s(2))
|
|
assert_equal("\x07\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e3.to_s(2))
|
|
assert_equal("\x07\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e4.to_s(2))
|
|
|
|
assert_equal("\x00\x00\x00\x02\x03\xe7", @e1.to_s(0))
|
|
assert_equal("\x00\x00\x00\x02\x83\xe7", @e2.to_s(0))
|
|
assert_equal("\x00\x00\x00\x0e\x07\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e3.to_s(0))
|
|
assert_equal("\x00\x00\x00\x0e\x87\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", @e4.to_s(0))
|
|
end
|
|
|
|
def test_to_int
|
|
assert_equal(999, @e1.to_i)
|
|
assert_equal(-999, @e2.to_i)
|
|
assert_equal(2**107-1, @e3.to_i)
|
|
assert_equal(-(2**107-1), @e4.to_i)
|
|
|
|
assert_equal(999, @e1.to_int)
|
|
end
|
|
|
|
def test_coerce
|
|
assert_equal(["", "-999"], @e2.coerce(""))
|
|
assert_equal([1000, -999], @e2.coerce(1000))
|
|
assert_raise(TypeError) { @e2.coerce(Class.new.new) }
|
|
end
|
|
|
|
def test_zero_p
|
|
assert_equal(true, 0.to_bn.zero?)
|
|
assert_equal(false, 1.to_bn.zero?)
|
|
end
|
|
|
|
def test_one_p
|
|
assert_equal(true, 1.to_bn.one?)
|
|
assert_equal(false, 2.to_bn.one?)
|
|
end
|
|
|
|
def test_odd_p
|
|
assert_equal(true, 1.to_bn.odd?)
|
|
assert_equal(false, 2.to_bn.odd?)
|
|
end
|
|
|
|
def test_negative_p
|
|
assert_equal(false, 0.to_bn.negative?)
|
|
assert_equal(false, @e1.negative?)
|
|
assert_equal(true, @e2.negative?)
|
|
end
|
|
|
|
def test_sqr
|
|
assert_equal(1, 1.to_bn.sqr)
|
|
assert_equal(100, 10.to_bn.sqr)
|
|
end
|
|
|
|
def test_four_ops
|
|
assert_equal(3, 1.to_bn + 2)
|
|
assert_equal(-1, 1.to_bn + -2)
|
|
assert_equal(-1, 1.to_bn - 2)
|
|
assert_equal(3, 1.to_bn - -2)
|
|
assert_equal(2, 1.to_bn * 2)
|
|
assert_equal(-2, 1.to_bn * -2)
|
|
assert_equal([0, 1], 1.to_bn / 2)
|
|
assert_equal([2, 0], 2.to_bn / 1)
|
|
assert_raise(OpenSSL::BNError) { 1.to_bn / 0 }
|
|
end
|
|
|
|
def test_unary_plus_minus
|
|
assert_equal(999, +@e1)
|
|
assert_equal(-999, +@e2)
|
|
assert_equal(-999, -@e1)
|
|
assert_equal(+999, -@e2)
|
|
end
|
|
|
|
def test_mod
|
|
assert_equal(1, 1.to_bn % 2)
|
|
assert_equal(0, 2.to_bn % 1)
|
|
assert_equal(-2, -2.to_bn % 7)
|
|
end
|
|
|
|
def test_exp
|
|
assert_equal(1, 1.to_bn ** 5)
|
|
assert_equal(32, 2.to_bn ** 5)
|
|
end
|
|
|
|
def test_gcd
|
|
assert_equal(1, 7.to_bn.gcd(5))
|
|
assert_equal(8, 24.to_bn.gcd(16))
|
|
end
|
|
|
|
def test_mod_sqr
|
|
assert_equal(4, 3.to_bn.mod_sqr(5))
|
|
assert_equal(0, 59.to_bn.mod_sqr(59))
|
|
end
|
|
|
|
def test_mod_inverse
|
|
assert_equal(2, 3.to_bn.mod_inverse(5))
|
|
assert_raise(OpenSSL::BNError) { 3.to_bn.mod_inverse(6) }
|
|
end
|
|
|
|
def test_mod_add
|
|
assert_equal(1, 3.to_bn.mod_add(5, 7))
|
|
assert_equal(2, 3.to_bn.mod_add(5, 3))
|
|
assert_equal(5, 3.to_bn.mod_add(-5, 7))
|
|
end
|
|
|
|
def test_mod_sub
|
|
assert_equal(1, 11.to_bn.mod_sub(3, 7))
|
|
assert_equal(2, 11.to_bn.mod_sub(3, 3))
|
|
assert_equal(5, 3.to_bn.mod_sub(5, 7))
|
|
end
|
|
|
|
def test_mod_mul
|
|
assert_equal(1, 2.to_bn.mod_mul(4, 7))
|
|
assert_equal(5, 2.to_bn.mod_mul(-1, 7))
|
|
end
|
|
|
|
def test_mod_exp
|
|
assert_equal(1, 3.to_bn.mod_exp(2, 8))
|
|
assert_equal(4, 2.to_bn.mod_exp(5, 7))
|
|
end
|
|
|
|
def test_bit_operations
|
|
e = 0b10010010.to_bn
|
|
assert_equal(0b10010011, e.set_bit!(0))
|
|
assert_equal(0b10010011, e.set_bit!(1))
|
|
assert_equal(0b1010010011, e.set_bit!(9))
|
|
|
|
e = 0b10010010.to_bn
|
|
assert_equal(0b10010010, e.clear_bit!(0))
|
|
assert_equal(0b10010000, e.clear_bit!(1))
|
|
|
|
e = 0b10010010.to_bn
|
|
assert_equal(0b10010010, e.mask_bits!(8))
|
|
assert_equal(0b10, e.mask_bits!(3))
|
|
|
|
e = 0b10010010.to_bn
|
|
assert_equal(false, e.bit_set?(0))
|
|
assert_equal(true, e.bit_set?(1))
|
|
assert_equal(false, e.bit_set?(1000))
|
|
|
|
e = 0b10010010.to_bn
|
|
assert_equal(0b1001001000, e << 2)
|
|
assert_equal(0b10010010, e)
|
|
assert_equal(0b1001001000, e.lshift!(2))
|
|
assert_equal(0b1001001000, e)
|
|
|
|
e = 0b10010010.to_bn
|
|
assert_equal(0b100100, e >> 2)
|
|
assert_equal(0b10010010, e)
|
|
assert_equal(0b100100, e.rshift!(2))
|
|
assert_equal(0b100100, e)
|
|
end
|
|
|
|
def test_random
|
|
10.times {
|
|
r1 = OpenSSL::BN.rand(8)
|
|
assert_include(128..255, r1)
|
|
r2 = OpenSSL::BN.rand(8, -1)
|
|
assert_include(0..255, r2)
|
|
r3 = OpenSSL::BN.rand(8, 1)
|
|
assert_include(192..255, r3)
|
|
r4 = OpenSSL::BN.rand(8, 1, true)
|
|
assert_include(192..255, r4)
|
|
assert_equal(true, r4.odd?)
|
|
|
|
r5 = OpenSSL::BN.rand_range(256)
|
|
assert_include(0..255, r5)
|
|
}
|
|
end
|
|
|
|
def test_prime
|
|
p1 = OpenSSL::BN.generate_prime(32)
|
|
assert_include(0...2**32, p1)
|
|
assert_equal(true, Prime.prime?(p1.to_i))
|
|
p2 = OpenSSL::BN.generate_prime(32, true)
|
|
assert_equal(true, Prime.prime?((p2.to_i - 1) / 2))
|
|
p3 = OpenSSL::BN.generate_prime(32, false, 4)
|
|
assert_equal(1, p3 % 4)
|
|
p4 = OpenSSL::BN.generate_prime(32, false, 4, 3)
|
|
assert_equal(3, p4 % 4)
|
|
|
|
assert_equal(true, p1.prime?)
|
|
assert_equal(true, p2.prime?)
|
|
assert_equal(true, p3.prime?)
|
|
assert_equal(true, p4.prime?)
|
|
assert_equal(true, @e3.prime?)
|
|
assert_equal(true, @e3.prime_fasttest?)
|
|
end
|
|
|
|
def test_num_bits_bytes
|
|
assert_equal(10, @e1.num_bits)
|
|
assert_equal(2, @e1.num_bytes)
|
|
assert_equal(107, @e3.num_bits)
|
|
assert_equal(14, @e3.num_bytes)
|
|
assert_equal(0, 0.to_bn.num_bits)
|
|
assert_equal(0, 0.to_bn.num_bytes)
|
|
assert_equal(9, -256.to_bn.num_bits)
|
|
assert_equal(2, -256.to_bn.num_bytes)
|
|
end
|
|
|
|
def test_comparison
|
|
assert_equal(false, @e1 == nil)
|
|
assert_equal(false, @e1 == -999)
|
|
assert_equal(true, @e1 == 999)
|
|
assert_equal(true, @e1 == 999.to_bn)
|
|
assert_equal(false, @e1.eql?(nil))
|
|
assert_equal(false, @e1.eql?(999))
|
|
assert_equal(true, @e1.eql?(999.to_bn))
|
|
assert_equal(@e1.hash, 999.to_bn.hash)
|
|
assert_not_equal(@e1.hash, @e3.hash)
|
|
assert_equal(0, @e1.cmp(999))
|
|
assert_equal(1, @e1.cmp(-999))
|
|
assert_equal(0, @e1.ucmp(999))
|
|
assert_equal(0, @e1.ucmp(-999))
|
|
end
|
|
end
|
|
|
|
end
|