Skip to content

Commit 2ca54fe

Browse files
authored
Merge pull request #363 from bdewater/marshal-pkey
Add Marshal support to PKey objects
2 parents 1f1641d + c4374ff commit 2ca54fe

File tree

8 files changed

+85
-23
lines changed

8 files changed

+85
-23
lines changed

History.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ Notable changes
2424
* Add `OpenSSL::SSL::SSLSocket.open` for opening a `TCPSocket` and
2525
returning an `OpenSSL::SSL::SSLSocket` for it.
2626
[[GitHub #225]](https://github.com/ruby/openssl/issues/225)
27-
* Support marshalling of `OpenSSL::X509` objects.
27+
* Support marshalling of `OpenSSL::X509` and `OpenSSL::PKey` objects.
2828
[[GitHub #281]](https://github.com/ruby/openssl/pull/281)
29+
[[GitHub #363]](https://github.com/ruby/openssl/pull/363)
2930
* Add `OpenSSL.secure_compare` for timing safe string comparison for
3031
strings of possibly unequal length.
3132
[[GitHub #280]](https://github.com/ruby/openssl/pull/280)

lib/openssl/marshal.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
#--
3+
# = Ruby-space definitions to add DER (de)serialization to classes
4+
#
5+
# = Info
6+
# 'OpenSSL for Ruby 2' project
7+
# Copyright (C) 2002 Michal Rokos <m.rokos@sh.cvut.cz>
8+
# All rights reserved.
9+
#
10+
# = Licence
11+
# This program is licensed under the same licence as Ruby.
12+
# (See the file 'LICENCE'.)
13+
#++
14+
module OpenSSL
15+
module Marshal
16+
def self.included(base)
17+
base.extend(ClassMethods)
18+
end
19+
20+
module ClassMethods
21+
def _load(string)
22+
new(string)
23+
end
24+
end
25+
26+
def _dump(_level)
27+
to_der
28+
end
29+
end
30+
end

lib/openssl/pkey.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44
# Copyright (C) 2017 Ruby/OpenSSL Project Authors
55
#++
66

7+
require_relative 'marshal'
8+
79
module OpenSSL::PKey
10+
class DH
11+
include OpenSSL::Marshal
12+
end
13+
14+
class DSA
15+
include OpenSSL::Marshal
16+
end
17+
818
if defined?(EC)
19+
class EC
20+
include OpenSSL::Marshal
21+
end
922
class EC::Point
1023
# :call-seq:
1124
# point.to_bn([conversion_form]) -> OpenSSL::BN
@@ -22,4 +35,8 @@ def to_bn(conversion_form = group.point_conversion_form)
2235
end
2336
end
2437
end
38+
39+
class RSA
40+
include OpenSSL::Marshal
41+
end
2542
end

lib/openssl/x509.rb

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,10 @@
1212
# (See the file 'LICENCE'.)
1313
#++
1414

15+
require_relative 'marshal'
16+
1517
module OpenSSL
1618
module X509
17-
module Marshal
18-
def self.included(base)
19-
base.extend(ClassMethods)
20-
end
21-
22-
module ClassMethods
23-
def _load(string)
24-
new(string)
25-
end
26-
end
27-
28-
def _dump(_level)
29-
to_der
30-
end
31-
end
32-
3319
class ExtensionFactory
3420
def create_extension(*arg)
3521
if arg.size > 1
@@ -57,7 +43,7 @@ def create_ext_from_hash(hash)
5743
end
5844

5945
class Extension
60-
include Marshal
46+
include OpenSSL::Marshal
6147

6248
def ==(other)
6349
return false unless Extension === other
@@ -216,7 +202,7 @@ def parse_aia_asn1
216202
end
217203

218204
class Name
219-
include Marshal
205+
include OpenSSL::Marshal
220206

221207
module RFC2253DN
222208
Special = ',=+<>#;'
@@ -321,7 +307,7 @@ def pretty_print(q)
321307
end
322308

323309
class Attribute
324-
include Marshal
310+
include OpenSSL::Marshal
325311

326312
def ==(other)
327313
return false unless Attribute === other
@@ -336,7 +322,7 @@ def cleanup
336322
end
337323

338324
class Certificate
339-
include Marshal
325+
include OpenSSL::Marshal
340326
include Extension::SubjectKeyIdentifier
341327
include Extension::AuthorityKeyIdentifier
342328
include Extension::CRLDistributionPoints
@@ -355,7 +341,7 @@ def pretty_print(q)
355341
end
356342

357343
class CRL
358-
include Marshal
344+
include OpenSSL::Marshal
359345
include Extension::AuthorityKeyIdentifier
360346

361347
def ==(other)
@@ -372,7 +358,7 @@ def ==(other)
372358
end
373359

374360
class Request
375-
include Marshal
361+
include OpenSSL::Marshal
376362

377363
def ==(other)
378364
return false unless Request === other

test/openssl/test_pkey_dh.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ def test_dup
7474
assert_equal dh2.g, dh.g
7575
end
7676

77+
def test_marshal
78+
dh = Fixtures.pkey("dh1024")
79+
deserialized = Marshal.load(Marshal.dump(dh))
80+
81+
assert_equal dh.to_der, deserialized.to_der
82+
end
83+
7784
private
7885

7986
def assert_equal_params(dh1, dh2)

test/openssl/test_pkey_dsa.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ def test_dup
191191
assert_not_equal key.params, key2.params
192192
end
193193

194+
def test_marshal
195+
key = Fixtures.pkey("dsa1024")
196+
deserialized = Marshal.load(Marshal.dump(key))
197+
198+
assert_equal key.to_der, deserialized.to_der
199+
end
200+
194201
private
195202
def assert_same_dsa(expected, key)
196203
check_component(expected, key, [:p, :q, :g, :pub_key, :priv_key])

test/openssl/test_pkey_ec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ def test_generate
5252
assert_equal(true, ec.private?)
5353
end
5454

55+
def test_marshal
56+
key = Fixtures.pkey("p256")
57+
deserialized = Marshal.load(Marshal.dump(key))
58+
59+
assert_equal key.to_der, deserialized.to_der
60+
end
61+
5562
def test_check_key
5663
key = OpenSSL::PKey::EC.new("prime256v1").generate_key!
5764
assert_equal(true, key.check_key)

test/openssl/test_pkey_rsa.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ def test_dup
443443
assert_not_equal key.params, key2.params
444444
end
445445

446+
def test_marshal
447+
key = Fixtures.pkey("rsa2048")
448+
deserialized = Marshal.load(Marshal.dump(key))
449+
450+
assert_equal key.to_der, deserialized.to_der
451+
end
452+
446453
private
447454
def assert_same_rsa(expected, key)
448455
check_component(expected, key, [:n, :e, :d, :p, :q, :dmp1, :dmq1, :iqmp])

0 commit comments

Comments
 (0)