[ruby/openssl] Add Marshal support to PKey objects
https://github.com/ruby/openssl/commit/c4374ff041
This commit is contained in:
parent
fcd2576290
commit
3f8665fe0e
@ -24,8 +24,9 @@ Notable changes
|
|||||||
* Add `OpenSSL::SSL::SSLSocket.open` for opening a `TCPSocket` and
|
* Add `OpenSSL::SSL::SSLSocket.open` for opening a `TCPSocket` and
|
||||||
returning an `OpenSSL::SSL::SSLSocket` for it.
|
returning an `OpenSSL::SSL::SSLSocket` for it.
|
||||||
[[GitHub #225]](https://github.com/ruby/openssl/issues/225)
|
[[GitHub #225]](https://github.com/ruby/openssl/issues/225)
|
||||||
* Support marshalling of `OpenSSL::X509` objects.
|
* Support marshalling of `OpenSSL::X509` and `OpenSSL::PKey` objects.
|
||||||
[[GitHub #281]](https://github.com/ruby/openssl/pull/281)
|
[[GitHub #281]](https://github.com/ruby/openssl/pull/281)
|
||||||
|
[[GitHub #363]](https://github.com/ruby/openssl/pull/363)
|
||||||
* Add `OpenSSL.secure_compare` for timing safe string comparison for
|
* Add `OpenSSL.secure_compare` for timing safe string comparison for
|
||||||
strings of possibly unequal length.
|
strings of possibly unequal length.
|
||||||
[[GitHub #280]](https://github.com/ruby/openssl/pull/280)
|
[[GitHub #280]](https://github.com/ruby/openssl/pull/280)
|
||||||
|
30
ext/openssl/lib/openssl/marshal.rb
Normal file
30
ext/openssl/lib/openssl/marshal.rb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
#--
|
||||||
|
# = Ruby-space definitions to add DER (de)serialization to classes
|
||||||
|
#
|
||||||
|
# = Info
|
||||||
|
# 'OpenSSL for Ruby 2' project
|
||||||
|
# Copyright (C) 2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# = Licence
|
||||||
|
# This program is licensed under the same licence as Ruby.
|
||||||
|
# (See the file 'LICENCE'.)
|
||||||
|
#++
|
||||||
|
module OpenSSL
|
||||||
|
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
|
||||||
|
end
|
@ -4,8 +4,21 @@
|
|||||||
# Copyright (C) 2017 Ruby/OpenSSL Project Authors
|
# Copyright (C) 2017 Ruby/OpenSSL Project Authors
|
||||||
#++
|
#++
|
||||||
|
|
||||||
|
require_relative 'marshal'
|
||||||
|
|
||||||
module OpenSSL::PKey
|
module OpenSSL::PKey
|
||||||
|
class DH
|
||||||
|
include OpenSSL::Marshal
|
||||||
|
end
|
||||||
|
|
||||||
|
class DSA
|
||||||
|
include OpenSSL::Marshal
|
||||||
|
end
|
||||||
|
|
||||||
if defined?(EC)
|
if defined?(EC)
|
||||||
|
class EC
|
||||||
|
include OpenSSL::Marshal
|
||||||
|
end
|
||||||
class EC::Point
|
class EC::Point
|
||||||
# :call-seq:
|
# :call-seq:
|
||||||
# point.to_bn([conversion_form]) -> OpenSSL::BN
|
# point.to_bn([conversion_form]) -> OpenSSL::BN
|
||||||
@ -22,4 +35,8 @@ module OpenSSL::PKey
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class RSA
|
||||||
|
include OpenSSL::Marshal
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -12,24 +12,10 @@
|
|||||||
# (See the file 'LICENCE'.)
|
# (See the file 'LICENCE'.)
|
||||||
#++
|
#++
|
||||||
|
|
||||||
|
require_relative 'marshal'
|
||||||
|
|
||||||
module OpenSSL
|
module OpenSSL
|
||||||
module X509
|
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
|
class ExtensionFactory
|
||||||
def create_extension(*arg)
|
def create_extension(*arg)
|
||||||
if arg.size > 1
|
if arg.size > 1
|
||||||
@ -57,7 +43,7 @@ module OpenSSL
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Extension
|
class Extension
|
||||||
include Marshal
|
include OpenSSL::Marshal
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
return false unless Extension === other
|
return false unless Extension === other
|
||||||
@ -216,7 +202,7 @@ module OpenSSL
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Name
|
class Name
|
||||||
include Marshal
|
include OpenSSL::Marshal
|
||||||
|
|
||||||
module RFC2253DN
|
module RFC2253DN
|
||||||
Special = ',=+<>#;'
|
Special = ',=+<>#;'
|
||||||
@ -321,7 +307,7 @@ module OpenSSL
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Attribute
|
class Attribute
|
||||||
include Marshal
|
include OpenSSL::Marshal
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
return false unless Attribute === other
|
return false unless Attribute === other
|
||||||
@ -336,7 +322,7 @@ module OpenSSL
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Certificate
|
class Certificate
|
||||||
include Marshal
|
include OpenSSL::Marshal
|
||||||
include Extension::SubjectKeyIdentifier
|
include Extension::SubjectKeyIdentifier
|
||||||
include Extension::AuthorityKeyIdentifier
|
include Extension::AuthorityKeyIdentifier
|
||||||
include Extension::CRLDistributionPoints
|
include Extension::CRLDistributionPoints
|
||||||
@ -355,7 +341,7 @@ module OpenSSL
|
|||||||
end
|
end
|
||||||
|
|
||||||
class CRL
|
class CRL
|
||||||
include Marshal
|
include OpenSSL::Marshal
|
||||||
include Extension::AuthorityKeyIdentifier
|
include Extension::AuthorityKeyIdentifier
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
@ -372,7 +358,7 @@ module OpenSSL
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Request
|
class Request
|
||||||
include Marshal
|
include OpenSSL::Marshal
|
||||||
|
|
||||||
def ==(other)
|
def ==(other)
|
||||||
return false unless Request === other
|
return false unless Request === other
|
||||||
|
@ -74,6 +74,13 @@ class OpenSSL::TestPKeyDH < OpenSSL::PKeyTestCase
|
|||||||
assert_equal dh2.g, dh.g
|
assert_equal dh2.g, dh.g
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_marshal
|
||||||
|
dh = Fixtures.pkey("dh1024")
|
||||||
|
deserialized = Marshal.load(Marshal.dump(dh))
|
||||||
|
|
||||||
|
assert_equal dh.to_der, deserialized.to_der
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def assert_equal_params(dh1, dh2)
|
def assert_equal_params(dh1, dh2)
|
||||||
|
@ -191,6 +191,13 @@ fWLOqqkzFeRrYMDzUpl36XktY6Yq8EJYlW9pCMmBVNy/dQ==
|
|||||||
assert_not_equal key.params, key2.params
|
assert_not_equal key.params, key2.params
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_marshal
|
||||||
|
key = Fixtures.pkey("dsa1024")
|
||||||
|
deserialized = Marshal.load(Marshal.dump(key))
|
||||||
|
|
||||||
|
assert_equal key.to_der, deserialized.to_der
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def assert_same_dsa(expected, key)
|
def assert_same_dsa(expected, key)
|
||||||
check_component(expected, key, [:p, :q, :g, :pub_key, :priv_key])
|
check_component(expected, key, [:p, :q, :g, :pub_key, :priv_key])
|
||||||
|
@ -52,6 +52,13 @@ class OpenSSL::TestEC < OpenSSL::PKeyTestCase
|
|||||||
assert_equal(true, ec.private?)
|
assert_equal(true, ec.private?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_marshal
|
||||||
|
key = Fixtures.pkey("p256")
|
||||||
|
deserialized = Marshal.load(Marshal.dump(key))
|
||||||
|
|
||||||
|
assert_equal key.to_der, deserialized.to_der
|
||||||
|
end
|
||||||
|
|
||||||
def test_check_key
|
def test_check_key
|
||||||
key = OpenSSL::PKey::EC.new("prime256v1").generate_key!
|
key = OpenSSL::PKey::EC.new("prime256v1").generate_key!
|
||||||
assert_equal(true, key.check_key)
|
assert_equal(true, key.check_key)
|
||||||
|
@ -443,6 +443,13 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase
|
|||||||
assert_not_equal key.params, key2.params
|
assert_not_equal key.params, key2.params
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_marshal
|
||||||
|
key = Fixtures.pkey("rsa2048")
|
||||||
|
deserialized = Marshal.load(Marshal.dump(key))
|
||||||
|
|
||||||
|
assert_equal key.to_der, deserialized.to_der
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def assert_same_rsa(expected, key)
|
def assert_same_rsa(expected, key)
|
||||||
check_component(expected, key, [:n, :e, :d, :p, :q, :dmp1, :dmq1, :iqmp])
|
check_component(expected, key, [:n, :e, :d, :p, :q, :dmp1, :dmq1, :iqmp])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user