2020-02-16 01:21:29 -05:00
|
|
|
# frozen_string_literal: true
|
2010-12-15 14:50:00 -05:00
|
|
|
require_relative "utils"
|
2004-05-19 14:30:52 -04:00
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
if defined?(OpenSSL)
|
2004-05-17 16:23:28 -04:00
|
|
|
|
2016-05-18 00:07:47 -04:00
|
|
|
class OpenSSL::TestX509Request < OpenSSL::TestCase
|
2004-05-17 16:23:28 -04:00
|
|
|
def setup
|
2016-12-10 03:12:02 -05:00
|
|
|
super
|
2017-09-03 08:35:27 -04:00
|
|
|
@rsa1024 = Fixtures.pkey("rsa1024")
|
|
|
|
@rsa2048 = Fixtures.pkey("rsa2048")
|
|
|
|
@dsa256 = Fixtures.pkey("dsa256")
|
|
|
|
@dsa512 = Fixtures.pkey("dsa512")
|
2004-05-17 16:23:28 -04:00
|
|
|
@dn = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=GOTOU Yuuzou")
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_csr(ver, dn, key, digest)
|
|
|
|
req = OpenSSL::X509::Request.new
|
|
|
|
req.version = ver
|
|
|
|
req.subject = dn
|
|
|
|
req.public_key = key.public_key
|
|
|
|
req.sign(key, digest)
|
|
|
|
req
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_public_key
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(@rsa1024.public_key.to_der, req.public_key.to_der)
|
|
|
|
req = OpenSSL::X509::Request.new(req.to_der)
|
|
|
|
assert_equal(@rsa1024.public_key.to_der, req.public_key.to_der)
|
|
|
|
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @dsa512, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(@dsa512.public_key.to_der, req.public_key.to_der)
|
|
|
|
req = OpenSSL::X509::Request.new(req.to_der)
|
|
|
|
assert_equal(@dsa512.public_key.to_der, req.public_key.to_der)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_version
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(0, req.version)
|
|
|
|
req = OpenSSL::X509::Request.new(req.to_der)
|
|
|
|
assert_equal(0, req.version)
|
|
|
|
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(1, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(1, req.version)
|
|
|
|
req = OpenSSL::X509::Request.new(req.to_der)
|
|
|
|
assert_equal(1, req.version)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_subject
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(@dn.to_der, req.subject.to_der)
|
|
|
|
req = OpenSSL::X509::Request.new(req.to_der)
|
|
|
|
assert_equal(@dn.to_der, req.subject.to_der)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_ext_req(exts)
|
|
|
|
ef = OpenSSL::X509::ExtensionFactory.new
|
|
|
|
exts = exts.collect{|e| ef.create_extension(*e) }
|
|
|
|
return OpenSSL::ASN1::Set([OpenSSL::ASN1::Sequence(exts)])
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_ext_req(ext_req_value)
|
|
|
|
set = OpenSSL::ASN1.decode(ext_req_value)
|
|
|
|
seq = set.value[0]
|
|
|
|
seq.value.collect{|asn1ext|
|
|
|
|
OpenSSL::X509::Extension.new(asn1ext).to_a
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_attr
|
|
|
|
exts = [
|
|
|
|
["keyUsage", "Digital Signature, Key Encipherment", true],
|
|
|
|
["subjectAltName", "email:gotoyuzo@ruby-lang.org", false],
|
|
|
|
]
|
|
|
|
attrval = create_ext_req(exts)
|
|
|
|
attrs = [
|
|
|
|
OpenSSL::X509::Attribute.new("extReq", attrval),
|
|
|
|
OpenSSL::X509::Attribute.new("msExtReq", attrval),
|
|
|
|
]
|
|
|
|
|
2020-04-19 11:14:36 -04:00
|
|
|
req0 = issue_csr(0, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
attrs.each{|attr| req0.add_attribute(attr) }
|
2020-04-19 11:14:36 -04:00
|
|
|
req1 = issue_csr(0, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
req1.attributes = attrs
|
|
|
|
assert_equal(req0.to_der, req1.to_der)
|
|
|
|
|
|
|
|
attrs = req0.attributes
|
|
|
|
assert_equal(2, attrs.size)
|
|
|
|
assert_equal("extReq", attrs[0].oid)
|
|
|
|
assert_equal("msExtReq", attrs[1].oid)
|
|
|
|
assert_equal(exts, get_ext_req(attrs[0].value))
|
|
|
|
assert_equal(exts, get_ext_req(attrs[1].value))
|
|
|
|
|
|
|
|
req = OpenSSL::X509::Request.new(req0.to_der)
|
|
|
|
attrs = req.attributes
|
|
|
|
assert_equal(2, attrs.size)
|
|
|
|
assert_equal("extReq", attrs[0].oid)
|
|
|
|
assert_equal("msExtReq", attrs[1].oid)
|
|
|
|
assert_equal(exts, get_ext_req(attrs[0].value))
|
|
|
|
assert_equal(exts, get_ext_req(attrs[1].value))
|
|
|
|
end
|
|
|
|
|
2014-07-22 12:02:05 -04:00
|
|
|
def test_sign_and_verify_rsa_sha1
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(true, req.verify(@rsa1024))
|
|
|
|
assert_equal(false, req.verify(@rsa2048))
|
2010-02-27 21:47:19 -05:00
|
|
|
assert_equal(false, request_error_returns_false { req.verify(@dsa256) })
|
|
|
|
assert_equal(false, request_error_returns_false { req.verify(@dsa512) })
|
2004-05-17 16:23:28 -04:00
|
|
|
req.version = 1
|
|
|
|
assert_equal(false, req.verify(@rsa1024))
|
2014-07-22 12:02:05 -04:00
|
|
|
end
|
2004-05-17 16:23:28 -04:00
|
|
|
|
2014-07-22 12:02:05 -04:00
|
|
|
def test_sign_and_verify_rsa_md5
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @rsa2048, OpenSSL::Digest.new('MD5'))
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(false, req.verify(@rsa1024))
|
|
|
|
assert_equal(true, req.verify(@rsa2048))
|
2010-02-27 21:47:19 -05:00
|
|
|
assert_equal(false, request_error_returns_false { req.verify(@dsa256) })
|
|
|
|
assert_equal(false, request_error_returns_false { req.verify(@dsa512) })
|
2004-05-17 16:23:28 -04:00
|
|
|
req.subject = OpenSSL::X509::Name.parse("/C=JP/CN=FooBar")
|
|
|
|
assert_equal(false, req.verify(@rsa2048))
|
2014-07-23 01:37:09 -04:00
|
|
|
rescue OpenSSL::X509::RequestError # RHEL7 disables MD5
|
2014-07-22 12:02:05 -04:00
|
|
|
end
|
2004-05-17 16:23:28 -04:00
|
|
|
|
2014-07-22 12:02:05 -04:00
|
|
|
def test_sign_and_verify_dsa
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @dsa512, OpenSSL::Digest.new('SHA1'))
|
2010-02-27 21:47:19 -05:00
|
|
|
assert_equal(false, request_error_returns_false { req.verify(@rsa1024) })
|
|
|
|
assert_equal(false, request_error_returns_false { req.verify(@rsa2048) })
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(false, req.verify(@dsa256))
|
|
|
|
assert_equal(true, req.verify(@dsa512))
|
|
|
|
req.public_key = @rsa1024.public_key
|
|
|
|
assert_equal(false, req.verify(@dsa512))
|
2014-07-22 12:02:05 -04:00
|
|
|
end
|
2004-05-17 16:23:28 -04:00
|
|
|
|
2014-07-22 12:02:05 -04:00
|
|
|
def test_sign_and_verify_dsa_md5
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_raise(OpenSSL::X509::RequestError){
|
2020-04-19 11:14:36 -04:00
|
|
|
issue_csr(0, @dn, @dsa512, OpenSSL::Digest.new('MD5')) }
|
2004-05-17 16:23:28 -04:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
def test_dup
|
2020-04-19 11:14:36 -04:00
|
|
|
req = issue_csr(0, @dn, @rsa1024, OpenSSL::Digest.new('SHA1'))
|
2016-08-29 01:47:09 -04:00
|
|
|
assert_equal(req.to_der, req.dup.to_der)
|
|
|
|
end
|
|
|
|
|
2017-11-25 09:12:08 -05:00
|
|
|
def test_eq
|
|
|
|
req1 = issue_csr(0, @dn, @rsa1024, "sha1")
|
|
|
|
req2 = issue_csr(0, @dn, @rsa1024, "sha1")
|
|
|
|
req3 = issue_csr(0, @dn, @rsa1024, "sha256")
|
|
|
|
|
|
|
|
assert_equal false, req1 == 12345
|
|
|
|
assert_equal true, req1 == req2
|
|
|
|
assert_equal false, req1 == req3
|
|
|
|
end
|
|
|
|
|
2020-02-16 01:21:29 -05:00
|
|
|
def test_marshal
|
|
|
|
req = issue_csr(0, @dn, @rsa1024, "sha256")
|
|
|
|
deserialized = Marshal.load(Marshal.dump(req))
|
|
|
|
|
|
|
|
assert_equal req.to_der, deserialized.to_der
|
|
|
|
end
|
|
|
|
|
2010-02-27 21:47:19 -05:00
|
|
|
private
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2010-02-27 21:47:19 -05:00
|
|
|
def request_error_returns_false
|
|
|
|
yield
|
|
|
|
rescue OpenSSL::X509::RequestError
|
|
|
|
false
|
|
|
|
end
|
2004-05-17 16:23:28 -04:00
|
|
|
end
|
2004-05-19 14:30:52 -04:00
|
|
|
|
|
|
|
end
|