Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Marshal support to X509 objects #281

Merged
merged 1 commit into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/openssl/x509.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@

module OpenSSL
module X509
module Marshal
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def _load(string)
new(string)
end
end

def _dump(_level)
to_der
end
end

class ExtensionFactory
def create_extension(*arg)
if arg.size > 1
Expand Down Expand Up @@ -41,6 +57,8 @@ def create_ext_from_hash(hash)
end

class Extension
include Marshal

def ==(other)
return false unless Extension === other
to_der == other.to_der
Expand Down Expand Up @@ -116,6 +134,8 @@ def authority_key_identifier
end

class Name
include Marshal

module RFC2253DN
Special = ',=+<>#;'
HexChar = /[0-9a-fA-F]/
Expand Down Expand Up @@ -219,6 +239,8 @@ def pretty_print(q)
end

class Attribute
include Marshal

def ==(other)
return false unless Attribute === other
to_der == other.to_der
Expand All @@ -232,6 +254,7 @@ def cleanup
end

class Certificate
include Marshal
include Extension::SubjectKeyIdentifier
include Extension::AuthorityKeyIdentifier

Expand All @@ -248,6 +271,7 @@ def pretty_print(q)
end

class CRL
include Marshal
include Extension::AuthorityKeyIdentifier

def ==(other)
Expand All @@ -264,6 +288,8 @@ def ==(other)
end

class Request
include Marshal

def ==(other)
return false unless Request === other
to_der == other.to_der
Expand Down
10 changes: 10 additions & 0 deletions test/test_x509attr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ def test_eq
assert_equal true, attr1 == attr2
assert_equal false, attr1 == attr3
end

def test_marshal
val = OpenSSL::ASN1::Set([
OpenSSL::ASN1::UTF8String("abc123")
])
attr = OpenSSL::X509::Attribute.new("challengePassword", val)
deserialized = Marshal.load(Marshal.dump(attr))

assert_equal attr.to_der, deserialized.to_der
end
end

end
11 changes: 11 additions & 0 deletions test/test_x509cert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ def test_eq
assert_equal false, cert3 == cert4
end

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

private

def certificate_error_returns_false
Expand Down
16 changes: 16 additions & 0 deletions test/test_x509crl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ def test_eq
assert_equal true, rev2 == crl2.revoked[1]
end

def test_marshal
now = Time.now

cacert = issue_cert(@ca, @rsa1024, 1, [], nil, nil)
crl = issue_crl([], 1, now, now + 3600, [], cacert, @rsa1024, "sha256")
rev = OpenSSL::X509::Revoked.new.tap { |rev|
rev.serial = 1
rev.time = now
}
crl.add_revoked(rev)
deserialized = Marshal.load(Marshal.dump(crl))

assert_equal crl.to_der, deserialized.to_der
assert_equal crl.revoked[0].to_der, deserialized.revoked[0].to_der
end

private

def crl_error_returns_false
Expand Down
8 changes: 8 additions & 0 deletions test/test_x509ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def test_eq
assert_equal false, ext1 == ext3
end

def test_marshal
ef = OpenSSL::X509::ExtensionFactory.new
ext = ef.create_extension("basicConstraints", "critical, CA:TRUE, pathlen:2")
deserialized = Marshal.load(Marshal.dump(ext))

assert_equal ext.to_der, deserialized.to_der
end

def test_value_der
ext = OpenSSL::X509::Extension.new(@basic_constraints.to_der)
assert_equal @basic_constraints_value.to_der, ext.value_der
Expand Down
7 changes: 7 additions & 0 deletions test/test_x509name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ def test_equality
assert_equal false, name0.eql?(name2)
end

def test_marshal
name = OpenSSL::X509::Name.new([["DC", "org"], ["DC", "ruby-lang"], ["CN", "bar.ruby-lang.org"]])
deserialized = Marshal.load(Marshal.dump(name))

assert_equal name.to_der, deserialized.to_der
end

def test_dup
name = OpenSSL::X509::Name.parse("/CN=ruby-lang.org")
assert_equal(name.to_der, name.dup.to_der)
Expand Down
7 changes: 7 additions & 0 deletions test/test_x509req.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ def test_eq
assert_equal false, req1 == req3
end

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

private

def request_error_returns_false
Expand Down