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::TestX509Certificate < 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
|
|
|
@ca = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=CA")
|
|
|
|
@ee1 = OpenSSL::X509::Name.parse("/DC=org/DC=ruby-lang/CN=EE1")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_serial
|
|
|
|
[1, 2**32, 2**100].each{|s|
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @rsa2048, s, [], nil, nil)
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(s, cert.serial)
|
|
|
|
cert = OpenSSL::X509::Certificate.new(cert.to_der)
|
|
|
|
assert_equal(s, cert.serial)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_public_key
|
|
|
|
exts = [
|
|
|
|
["basicConstraints","CA:TRUE",true],
|
|
|
|
["subjectKeyIdentifier","hash",false],
|
|
|
|
["authorityKeyIdentifier","keyid:always",false],
|
|
|
|
]
|
|
|
|
|
|
|
|
[
|
2017-09-03 08:35:27 -04:00
|
|
|
@rsa1024, @rsa2048, @dsa256, @dsa512,
|
|
|
|
].each{|pk|
|
|
|
|
cert = issue_cert(@ca, pk, 1, exts, nil, nil)
|
2014-06-23 14:59:07 -04:00
|
|
|
assert_equal(cert.extensions.sort_by(&:to_s)[2].value,
|
2004-05-17 16:23:28 -04:00
|
|
|
OpenSSL::TestUtils.get_subject_key_id(cert))
|
|
|
|
cert = OpenSSL::X509::Certificate.new(cert.to_der)
|
2014-06-23 14:59:07 -04:00
|
|
|
assert_equal(cert.extensions.sort_by(&:to_s)[2].value,
|
2004-05-17 16:23:28 -04:00
|
|
|
OpenSSL::TestUtils.get_subject_key_id(cert))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_validity
|
2016-11-30 09:41:46 -05:00
|
|
|
now = Time.at(Time.now.to_i + 0.9)
|
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil,
|
|
|
|
not_before: now, not_after: now+3600)
|
|
|
|
assert_equal(Time.at(now.to_i), cert.not_before)
|
|
|
|
assert_equal(Time.at(now.to_i+3600), cert.not_after)
|
2004-05-17 16:23:28 -04:00
|
|
|
|
|
|
|
now = Time.at(now.to_i)
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil,
|
|
|
|
not_before: now, not_after: now+3600)
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(now.getutc, cert.not_before)
|
|
|
|
assert_equal((now+3600).getutc, cert.not_after)
|
|
|
|
|
|
|
|
now = Time.at(0)
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil,
|
|
|
|
not_before: now, not_after: now)
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(now.getutc, cert.not_before)
|
|
|
|
assert_equal(now.getutc, cert.not_after)
|
|
|
|
|
|
|
|
now = Time.at(0x7fffffff)
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil,
|
|
|
|
not_before: now, not_after: now)
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(now.getutc, cert.not_before)
|
|
|
|
assert_equal(now.getutc, cert.not_after)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extension
|
|
|
|
ca_exts = [
|
|
|
|
["basicConstraints","CA:TRUE",true],
|
|
|
|
["keyUsage","keyCertSign, cRLSign",true],
|
|
|
|
["subjectKeyIdentifier","hash",false],
|
2020-02-16 01:21:29 -05:00
|
|
|
["authorityKeyIdentifier","issuer:always,keyid:always",false],
|
2004-05-17 16:23:28 -04:00
|
|
|
]
|
2016-11-30 09:41:46 -05:00
|
|
|
ca_cert = issue_cert(@ca, @rsa2048, 1, ca_exts, nil, nil)
|
2020-02-16 01:21:29 -05:00
|
|
|
keyid = get_subject_key_id(ca_cert.to_der, hex: false)
|
|
|
|
assert_equal keyid, ca_cert.authority_key_identifier
|
|
|
|
assert_equal keyid, ca_cert.subject_key_identifier
|
2004-05-17 16:23:28 -04:00
|
|
|
ca_cert.extensions.each_with_index{|ext, i|
|
|
|
|
assert_equal(ca_exts[i].first, ext.oid)
|
|
|
|
assert_equal(ca_exts[i].last, ext.critical?)
|
|
|
|
}
|
|
|
|
|
|
|
|
ee1_exts = [
|
|
|
|
["keyUsage","Non Repudiation, Digital Signature, Key Encipherment",true],
|
|
|
|
["subjectKeyIdentifier","hash",false],
|
2020-02-16 01:21:29 -05:00
|
|
|
["authorityKeyIdentifier","issuer:always,keyid:always",false],
|
2004-05-17 16:23:28 -04:00
|
|
|
["extendedKeyUsage","clientAuth, emailProtection, codeSigning",false],
|
|
|
|
["subjectAltName","email:ee1@ruby-lang.org",false],
|
2020-02-16 01:21:29 -05:00
|
|
|
["authorityInfoAccess","caIssuers;URI:http://www.example.com/caIssuers,OCSP;URI:http://www.example.com/ocsp",false],
|
2004-05-17 16:23:28 -04:00
|
|
|
]
|
2016-11-30 09:41:46 -05:00
|
|
|
ee1_cert = issue_cert(@ee1, @rsa1024, 2, ee1_exts, ca_cert, @rsa2048)
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(ca_cert.subject.to_der, ee1_cert.issuer.to_der)
|
|
|
|
ee1_cert.extensions.each_with_index{|ext, i|
|
|
|
|
assert_equal(ee1_exts[i].first, ext.oid)
|
|
|
|
assert_equal(ee1_exts[i].last, ext.critical?)
|
|
|
|
}
|
2020-02-16 01:21:29 -05:00
|
|
|
assert_nil(ee1_cert.crl_uris)
|
|
|
|
|
|
|
|
ef = OpenSSL::X509::ExtensionFactory.new
|
|
|
|
ef.config = OpenSSL::Config.parse(<<~_cnf_)
|
|
|
|
[crlDistPts]
|
|
|
|
URI.1 = http://www.example.com/crl
|
|
|
|
URI.2 = ldap://ldap.example.com/cn=ca?certificateRevocationList;binary
|
|
|
|
_cnf_
|
|
|
|
cdp_cert = generate_cert(@ee1, @rsa1024, 3, ca_cert)
|
|
|
|
ef.subject_certificate = cdp_cert
|
|
|
|
cdp_cert.add_extension(ef.create_extension("crlDistributionPoints", "@crlDistPts"))
|
|
|
|
cdp_cert.sign(@rsa2048, "sha256")
|
|
|
|
assert_equal(
|
|
|
|
["http://www.example.com/crl", "ldap://ldap.example.com/cn=ca?certificateRevocationList;binary"],
|
|
|
|
cdp_cert.crl_uris
|
|
|
|
)
|
|
|
|
|
|
|
|
ef = OpenSSL::X509::ExtensionFactory.new
|
|
|
|
aia_cert = generate_cert(@ee1, @rsa1024, 4, ca_cert)
|
|
|
|
ef.subject_certificate = aia_cert
|
|
|
|
aia_cert.add_extension(
|
|
|
|
ef.create_extension(
|
|
|
|
"authorityInfoAccess",
|
|
|
|
"caIssuers;URI:http://www.example.com/caIssuers," \
|
|
|
|
"caIssuers;URI:ldap://ldap.example.com/cn=ca?authorityInfoAccessCaIssuers;binary," \
|
|
|
|
"OCSP;URI:http://www.example.com/ocsp," \
|
|
|
|
"OCSP;URI:ldap://ldap.example.com/cn=ca?authorityInfoAccessOcsp;binary",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
aia_cert.sign(@rsa2048, "sha256")
|
|
|
|
assert_equal(
|
|
|
|
["http://www.example.com/caIssuers", "ldap://ldap.example.com/cn=ca?authorityInfoAccessCaIssuers;binary"],
|
|
|
|
aia_cert.ca_issuer_uris
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
["http://www.example.com/ocsp", "ldap://ldap.example.com/cn=ca?authorityInfoAccessOcsp;binary"],
|
|
|
|
aia_cert.ocsp_uris
|
|
|
|
)
|
|
|
|
|
|
|
|
no_exts_cert = issue_cert(@ca, @rsa2048, 5, [], nil, nil)
|
|
|
|
assert_equal nil, no_exts_cert.authority_key_identifier
|
|
|
|
assert_equal nil, no_exts_cert.subject_key_identifier
|
|
|
|
assert_equal nil, no_exts_cert.crl_uris
|
|
|
|
assert_equal nil, no_exts_cert.ca_issuer_uris
|
|
|
|
assert_equal nil, no_exts_cert.ocsp_uris
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_extension
|
|
|
|
integer = OpenSSL::ASN1::Integer.new(0)
|
|
|
|
invalid_exts_cert = generate_cert(@ee1, @rsa1024, 1, nil)
|
|
|
|
["subjectKeyIdentifier", "authorityKeyIdentifier", "crlDistributionPoints", "authorityInfoAccess"].each do |ext|
|
|
|
|
invalid_exts_cert.add_extension(
|
|
|
|
OpenSSL::X509::Extension.new(ext, integer.to_der)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(OpenSSL::ASN1::ASN1Error, "invalid extension") {
|
|
|
|
invalid_exts_cert.authority_key_identifier
|
|
|
|
}
|
|
|
|
assert_raise(OpenSSL::ASN1::ASN1Error, "invalid extension") {
|
|
|
|
invalid_exts_cert.subject_key_identifier
|
|
|
|
}
|
|
|
|
assert_raise(OpenSSL::ASN1::ASN1Error, "invalid extension") {
|
|
|
|
invalid_exts_cert.crl_uris
|
|
|
|
}
|
|
|
|
assert_raise(OpenSSL::ASN1::ASN1Error, "invalid extension") {
|
|
|
|
invalid_exts_cert.ca_issuer_uris
|
|
|
|
}
|
|
|
|
assert_raise(OpenSSL::ASN1::ASN1Error, "invalid extension") {
|
|
|
|
invalid_exts_cert.ocsp_uris
|
|
|
|
}
|
2004-05-17 16:23:28 -04:00
|
|
|
end
|
|
|
|
|
2014-07-22 12:02:05 -04:00
|
|
|
def test_sign_and_verify_rsa_sha1
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil, digest: "sha1")
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(false, cert.verify(@rsa1024))
|
|
|
|
assert_equal(true, cert.verify(@rsa2048))
|
2010-02-27 21:47:19 -05:00
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@dsa256) })
|
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@dsa512) })
|
2004-05-17 16:23:28 -04:00
|
|
|
cert.serial = 2
|
|
|
|
assert_equal(false, cert.verify(@rsa2048))
|
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
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil, digest: "md5")
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(false, cert.verify(@rsa1024))
|
2010-02-27 21:47:19 -05:00
|
|
|
assert_equal(true, cert.verify(@rsa2048))
|
|
|
|
|
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@dsa256) })
|
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@dsa512) })
|
2004-05-17 16:23:28 -04:00
|
|
|
cert.subject = @ee1
|
|
|
|
assert_equal(false, cert.verify(@rsa2048))
|
2014-07-22 22:35:06 -04:00
|
|
|
rescue OpenSSL::X509::CertificateError # 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
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @dsa512, 1, [], nil, nil)
|
2010-02-27 21:47:19 -05:00
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@rsa1024) })
|
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@rsa2048) })
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(false, cert.verify(@dsa256))
|
|
|
|
assert_equal(true, cert.verify(@dsa512))
|
2009-03-05 22:56:38 -05:00
|
|
|
cert.not_after = Time.now
|
2004-05-17 16:23:28 -04:00
|
|
|
assert_equal(false, cert.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_rsa_dss1
|
2020-04-19 11:14:36 -04:00
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil, digest: OpenSSL::Digest.new('DSS1'))
|
2014-07-22 12:02:05 -04:00
|
|
|
assert_equal(false, cert.verify(@rsa1024))
|
|
|
|
assert_equal(true, cert.verify(@rsa2048))
|
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@dsa256) })
|
|
|
|
assert_equal(false, certificate_error_returns_false { cert.verify(@dsa512) })
|
|
|
|
cert.subject = @ee1
|
|
|
|
assert_equal(false, cert.verify(@rsa2048))
|
|
|
|
rescue OpenSSL::X509::CertificateError
|
2016-06-07 08:20:46 -04:00
|
|
|
end if defined?(OpenSSL::Digest::DSS1)
|
2010-12-25 04:02:03 -05:00
|
|
|
|
2014-07-22 12:02:05 -04:00
|
|
|
def test_sign_and_verify_dsa_md5
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(OpenSSL::X509::CertificateError){
|
2016-11-30 09:41:46 -05:00
|
|
|
issue_cert(@ca, @dsa512, 1, [], nil, nil, digest: "md5")
|
2004-05-17 16:23:28 -04:00
|
|
|
}
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2011-05-21 17:09:51 -04:00
|
|
|
def test_dsa_with_sha2
|
2017-09-03 08:35:27 -04:00
|
|
|
cert = issue_cert(@ca, @dsa256, 1, [], nil, nil, digest: "sha256")
|
|
|
|
assert_equal("dsa_with_SHA256", cert.signature_algorithm)
|
2011-05-21 17:09:51 -04:00
|
|
|
# TODO: need more tests for dsa + sha2
|
|
|
|
|
|
|
|
# SHA1 is allowed from OpenSSL 1.0.0 (0.9.8 requires DSS1)
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @dsa256, 1, [], nil, nil, digest: "sha1")
|
2011-05-21 17:09:51 -04:00
|
|
|
assert_equal("dsaWithSHA1", cert.signature_algorithm)
|
2017-09-03 08:35:27 -04:00
|
|
|
end
|
2011-05-21 17:09:51 -04:00
|
|
|
|
|
|
|
def test_check_private_key
|
2016-11-30 09:41:46 -05:00
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil)
|
2011-05-21 17:09:51 -04:00
|
|
|
assert_equal(true, cert.check_private_key(@rsa2048))
|
|
|
|
end
|
|
|
|
|
2017-08-10 05:23:45 -04:00
|
|
|
def test_read_from_file
|
|
|
|
cert = issue_cert(@ca, @rsa2048, 1, [], nil, nil)
|
|
|
|
Tempfile.create("cert") { |f|
|
|
|
|
f << cert.to_pem
|
|
|
|
f.rewind
|
|
|
|
assert_equal cert.to_der, OpenSSL::X509::Certificate.new(f).to_der
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-05-19 04:58:18 -04:00
|
|
|
def test_read_der_then_pem
|
|
|
|
cert1 = issue_cert(@ca, @rsa2048, 1, [], nil, nil)
|
|
|
|
exts = [
|
|
|
|
# A new line before PEM block
|
|
|
|
["nsComment", "Another certificate:\n" + cert1.to_pem],
|
|
|
|
]
|
|
|
|
cert2 = issue_cert(@ca, @rsa2048, 2, exts, nil, nil)
|
|
|
|
|
|
|
|
assert_equal cert2, OpenSSL::X509::Certificate.new(cert2.to_der)
|
|
|
|
assert_equal cert2, OpenSSL::X509::Certificate.new(cert2.to_pem)
|
|
|
|
end
|
|
|
|
|
2017-11-25 09:12:08 -05:00
|
|
|
def test_eq
|
|
|
|
now = Time.now
|
|
|
|
cacert = issue_cert(@ca, @rsa1024, 1, [], nil, nil,
|
|
|
|
not_before: now, not_after: now + 3600)
|
|
|
|
cert1 = issue_cert(@ee1, @rsa2048, 2, [], cacert, @rsa1024,
|
|
|
|
not_before: now, not_after: now + 3600)
|
|
|
|
cert2 = issue_cert(@ee1, @rsa2048, 2, [], cacert, @rsa1024,
|
|
|
|
not_before: now, not_after: now + 3600)
|
|
|
|
cert3 = issue_cert(@ee1, @rsa2048, 3, [], cacert, @rsa1024,
|
|
|
|
not_before: now, not_after: now + 3600)
|
|
|
|
cert4 = issue_cert(@ee1, @rsa2048, 2, [], cacert, @rsa1024,
|
|
|
|
digest: "sha512", not_before: now, not_after: now + 3600)
|
|
|
|
|
|
|
|
assert_equal false, cert1 == 12345
|
|
|
|
assert_equal true, cert1 == cert2
|
|
|
|
assert_equal false, cert1 == cert3
|
|
|
|
assert_equal false, cert1 == cert4
|
|
|
|
assert_equal false, cert3 == cert4
|
|
|
|
end
|
|
|
|
|
2020-02-16 01:21:29 -05:00
|
|
|
def test_marshal
|
|
|
|
now = Time.now
|
|
|
|
cacert = issue_cert(@ca, @rsa1024, 1, [], nil, nil,
|
|
|
|
not_before: now, not_after: now + 3600)
|
|
|
|
cert = issue_cert(@ee1, @rsa2048, 2, [], cacert, @rsa1024,
|
|
|
|
not_before: now, not_after: now + 3600)
|
|
|
|
deserialized = Marshal.load(Marshal.dump(cert))
|
|
|
|
|
|
|
|
assert_equal cert.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 certificate_error_returns_false
|
|
|
|
yield
|
|
|
|
rescue OpenSSL::X509::CertificateError
|
|
|
|
false
|
|
|
|
end
|
2004-05-17 16:23:28 -04:00
|
|
|
end
|
2004-05-19 14:30:52 -04:00
|
|
|
|
|
|
|
end
|