2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2011-06-13 05:58:09 +04:00
|
|
|
require_relative 'utils'
|
|
|
|
|
2017-09-03 15:35:27 +03:00
|
|
|
if defined?(OpenSSL) && defined?(OpenSSL::PKey::DH)
|
2011-06-13 05:58:09 +04:00
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
class OpenSSL::TestPKeyDH < OpenSSL::PKeyTestCase
|
2012-12-20 04:32:09 +04:00
|
|
|
NEW_KEYLEN = 256
|
2012-09-03 05:14:26 +04:00
|
|
|
|
2011-06-13 05:58:09 +04:00
|
|
|
def test_new
|
2012-09-03 05:14:26 +04:00
|
|
|
dh = OpenSSL::PKey::DH.new(NEW_KEYLEN)
|
2011-06-13 05:58:09 +04:00
|
|
|
assert_key(dh)
|
|
|
|
end
|
|
|
|
|
2011-09-01 11:42:29 +04:00
|
|
|
def test_new_break
|
2012-09-03 05:14:26 +04:00
|
|
|
assert_nil(OpenSSL::PKey::DH.new(NEW_KEYLEN) { break })
|
2015-06-02 05:18:44 +03:00
|
|
|
assert_raise(RuntimeError) do
|
2012-09-03 05:14:26 +04:00
|
|
|
OpenSSL::PKey::DH.new(NEW_KEYLEN) { raise }
|
2011-09-01 11:42:29 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-29 08:47:09 +03:00
|
|
|
def test_DHparams
|
2019-06-05 15:07:27 +03:00
|
|
|
dh1024 = Fixtures.pkey("dh1024")
|
2016-08-29 08:47:09 +03:00
|
|
|
asn1 = OpenSSL::ASN1::Sequence([
|
2017-09-03 15:35:27 +03:00
|
|
|
OpenSSL::ASN1::Integer(dh1024.p),
|
|
|
|
OpenSSL::ASN1::Integer(dh1024.g)
|
2016-08-29 08:47:09 +03:00
|
|
|
])
|
|
|
|
key = OpenSSL::PKey::DH.new(asn1.to_der)
|
2017-09-03 15:35:27 +03:00
|
|
|
assert_same_dh dup_public(dh1024), key
|
2016-08-29 08:47:09 +03:00
|
|
|
|
|
|
|
pem = <<~EOF
|
|
|
|
-----BEGIN DH PARAMETERS-----
|
|
|
|
MIGHAoGBAKnKQ8MNK6nYZzLrrcuTsLxuiJGXoOO5gT+tljOTbHBuiktdMTITzIY0
|
|
|
|
pFxIvjG05D7HoBZQfrR0c92NGWPkAiCkhQKB8JCbPVzwNLDy6DZ0pmofDKrEsYHG
|
|
|
|
AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC
|
|
|
|
-----END DH PARAMETERS-----
|
|
|
|
EOF
|
|
|
|
key = OpenSSL::PKey::DH.new(pem)
|
2017-09-03 15:35:27 +03:00
|
|
|
assert_same_dh dup_public(dh1024), key
|
2016-08-29 08:47:09 +03:00
|
|
|
|
2017-09-03 15:35:27 +03:00
|
|
|
assert_equal asn1.to_der, dh1024.to_der
|
|
|
|
assert_equal pem, dh1024.export
|
2011-06-13 05:58:09 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_public_key
|
2019-06-05 15:07:27 +03:00
|
|
|
dh = Fixtures.pkey("dh1024")
|
2011-06-13 05:58:09 +04:00
|
|
|
public_key = dh.public_key
|
|
|
|
assert_no_key(public_key) #implies public_key.public? is false!
|
|
|
|
assert_equal(dh.to_der, public_key.to_der)
|
|
|
|
assert_equal(dh.to_pem, public_key.to_pem)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_generate_key
|
2019-06-05 15:07:27 +03:00
|
|
|
dh = Fixtures.pkey("dh1024").public_key # creates a copy
|
2011-06-13 05:58:09 +04:00
|
|
|
assert_no_key(dh)
|
|
|
|
dh.generate_key!
|
|
|
|
assert_key(dh)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_key_exchange
|
2019-06-05 15:07:27 +03:00
|
|
|
dh = Fixtures.pkey("dh1024")
|
2011-06-13 05:58:09 +04:00
|
|
|
dh2 = dh.public_key
|
|
|
|
dh.generate_key!
|
|
|
|
dh2.generate_key!
|
|
|
|
assert_equal(dh.compute_key(dh2.pub_key), dh2.compute_key(dh.pub_key))
|
|
|
|
end
|
|
|
|
|
2016-06-19 12:29:59 +03:00
|
|
|
def test_dup
|
|
|
|
dh = OpenSSL::PKey::DH.new(NEW_KEYLEN)
|
|
|
|
dh2 = dh.dup
|
|
|
|
assert_equal dh.to_der, dh2.to_der # params
|
|
|
|
assert_equal_params dh, dh2 # keys
|
|
|
|
dh2.set_pqg(dh2.p + 1, nil, dh2.g)
|
|
|
|
assert_not_equal dh2.p, dh.p
|
|
|
|
assert_equal dh2.g, dh.g
|
|
|
|
end
|
|
|
|
|
2011-06-13 05:58:09 +04:00
|
|
|
private
|
|
|
|
|
|
|
|
def assert_equal_params(dh1, dh2)
|
|
|
|
assert_equal(dh1.g, dh2.g)
|
|
|
|
assert_equal(dh1.p, dh2.p)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_key(dh)
|
|
|
|
assert_equal(false, dh.public?)
|
|
|
|
assert_equal(false, dh.private?)
|
|
|
|
assert_equal(nil, dh.pub_key)
|
|
|
|
assert_equal(nil, dh.priv_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_key(dh)
|
|
|
|
assert(dh.public?)
|
|
|
|
assert(dh.private?)
|
|
|
|
assert(dh.pub_key)
|
|
|
|
assert(dh.priv_key)
|
|
|
|
end
|
2016-08-29 08:47:09 +03:00
|
|
|
|
|
|
|
def assert_same_dh(expected, key)
|
|
|
|
check_component(expected, key, [:p, :q, :g, :pub_key, :priv_key])
|
|
|
|
end
|
2011-06-13 05:58:09 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|