ssh_data 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a33f8e3096dba56186df0bf3288d36c0fda79f75d7a02e6a58c585b468d4cd76
4
- data.tar.gz: 3f5f652b61e4fbbb06bc24154c51683e97be194a70565d993ee27ff87ec8eb2f
3
+ metadata.gz: c902ee03ed5d06e54dc59e312c4e32ff1cbbe67fc3ab1ff1f57afb56677a6fb1
4
+ data.tar.gz: be717f0bda679f014a918ab857745996c3f258368c53f18f48fea9865b69568f
5
5
  SHA512:
6
- metadata.gz: b6b1324539e847dd7e7d858cd0f64a8c369de781bdb8f5fcde7667ad9a61748e347209f7d6f06f7e5400d9147e67a9289f6f99aa187ec368bfc4a38ddccc22b9
7
- data.tar.gz: 7c1a063701138b853aebff7adb7f7a9d8bccabdf04d58d81dffb16ab2a499706401355ac01f2f4e8e91789140fd051569ecf8056eae27a76d0ae1425128e62c7
6
+ metadata.gz: eea479d192ec43202a94a83036d5244da75ce1a25f380cc40cea03934cad62fa62451c3c8261a4ded58622c98dfe1e31dee8f85a4441efa037f178b2925d89ba
7
+ data.tar.gz: f573e211ef79907c729f886724cb1a043bf939d5144129d32caa2b8ba182007f8e2cb5e74b4492b324093cfdb4a0f26e8b8e8fbcc83b5fd4865fae0016be019a
@@ -18,12 +18,13 @@ module SSHData
18
18
  ALGO_ECDSA384 = "ecdsa-sha2-nistp384-cert-v01@openssh.com"
19
19
  ALGO_ECDSA521 = "ecdsa-sha2-nistp521-cert-v01@openssh.com"
20
20
  ALGO_ED25519 = "ssh-ed25519-cert-v01@openssh.com"
21
+ ALGO_MLDSA44_ED25519 = "ssh-mldsa44-ed25519-cert"
21
22
  ALGO_SKECDSA256 = "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com"
22
23
  ALGO_SKED25519 = "sk-ssh-ed25519-cert-v01@openssh.com"
23
24
 
24
25
  ALGOS = [
25
26
  ALGO_RSA, ALGO_DSA, ALGO_ECDSA256, ALGO_ECDSA384, ALGO_ECDSA521,
26
- ALGO_ED25519, ALGO_SKECDSA256, ALGO_SKED25519
27
+ ALGO_ED25519, ALGO_MLDSA44_ED25519, ALGO_SKECDSA256, ALGO_SKED25519
27
28
  ]
28
29
 
29
30
  CRITICAL_OPTION_FORCE_COMMAND = "force-command"
@@ -83,7 +84,7 @@ module SSHData
83
84
  #
84
85
  # algo: - The certificate's String algorithm id (one of ALGO_RSA,
85
86
  # ALGO_DSA, ALGO_ECDSA256, ALGO_ECDSA384, ALGO_ECDSA521,
86
- # or ALGO_ED25519)
87
+ # ALGO_ED25519, ALGO_MLDSA44_ED25519, or a security-key algorithm)
87
88
  # nonce: - The certificate's String nonce field.
88
89
  # public_key: - The certificate's public key as an PublicKey::Base
89
90
  # subclass instance.
@@ -55,6 +55,11 @@ module SSHData
55
55
  [:sk, :string]
56
56
  ]
57
57
 
58
+ MLDSA44_ED25519_PRIVATE_KEY_FIELDS = [
59
+ [:pk, :string],
60
+ [:sk, :string]
61
+ ]
62
+
58
63
  # Fields in an RSA public key
59
64
  RSA_KEY_FIELDS = [
60
65
  [:e, :mpint],
@@ -87,6 +92,10 @@ module SSHData
87
92
  [:pk, :string]
88
93
  ]
89
94
 
95
+ MLDSA44_ED25519_KEY_FIELDS = [
96
+ [:pk, :string]
97
+ ]
98
+
90
99
  # Fields in a SK-ED25519 public key
91
100
  SKED25519_KEY_FIELDS = [
92
101
  [:pk, :string],
@@ -100,6 +109,7 @@ module SSHData
100
109
  Certificate::ALGO_ECDSA384 => PublicKey::ALGO_ECDSA384,
101
110
  Certificate::ALGO_ECDSA521 => PublicKey::ALGO_ECDSA521,
102
111
  Certificate::ALGO_ED25519 => PublicKey::ALGO_ED25519,
112
+ Certificate::ALGO_MLDSA44_ED25519 => PublicKey::ALGO_MLDSA44_ED25519,
103
113
  Certificate::ALGO_SKECDSA256 => PublicKey::ALGO_SKECDSA256,
104
114
  Certificate::ALGO_SKED25519 => PublicKey::ALGO_SKED25519,
105
115
  }
@@ -111,6 +121,7 @@ module SSHData
111
121
  PublicKey::ALGO_ECDSA384 => Certificate::ALGO_ECDSA384,
112
122
  PublicKey::ALGO_ECDSA521 => Certificate::ALGO_ECDSA521,
113
123
  PublicKey::ALGO_ED25519 => Certificate::ALGO_ED25519,
124
+ PublicKey::ALGO_MLDSA44_ED25519 => Certificate::ALGO_MLDSA44_ED25519,
114
125
  PublicKey::ALGO_SKECDSA256 => Certificate::ALGO_SKECDSA256,
115
126
  PublicKey::ALGO_SKED25519 => Certificate::ALGO_SKED25519,
116
127
  }
@@ -122,6 +133,7 @@ module SSHData
122
133
  PublicKey::ALGO_ECDSA384 => ECDSA_KEY_FIELDS,
123
134
  PublicKey::ALGO_ECDSA521 => ECDSA_KEY_FIELDS,
124
135
  PublicKey::ALGO_ED25519 => ED25519_KEY_FIELDS,
136
+ PublicKey::ALGO_MLDSA44_ED25519 => MLDSA44_ED25519_KEY_FIELDS,
125
137
  PublicKey::ALGO_SKED25519 => SKED25519_KEY_FIELDS,
126
138
  PublicKey::ALGO_SKECDSA256 => SKECDSA_KEY_FIELDS,
127
139
  }
@@ -133,6 +145,7 @@ module SSHData
133
145
  PublicKey::ALGO_ECDSA384 => ECDSA_PRIVATE_KEY_FIELDS,
134
146
  PublicKey::ALGO_ECDSA521 => ECDSA_PRIVATE_KEY_FIELDS,
135
147
  PublicKey::ALGO_ED25519 => ED25519_PRIVATE_KEY_FIELDS,
148
+ PublicKey::ALGO_MLDSA44_ED25519 => MLDSA44_ED25519_PRIVATE_KEY_FIELDS,
136
149
  }
137
150
 
138
151
  # Get the type from a PEM encoded blob.
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module SSHData
6
+ module PrivateKey
7
+ class MLDSA44ED25519 < Base
8
+ SEED_SIZE = 32
9
+ COMPOSITE_PREFIX = "CompositeAlgorithmSignatures2025"
10
+ COMPOSITE_LABEL = "COMPSIG-MLDSA44-Ed25519-SHA512"
11
+
12
+ attr_reader :sk
13
+
14
+ def self.generate
15
+ new(algo: PublicKey::ALGO_MLDSA44_ED25519, sk: SecureRandom.random_bytes(SEED_SIZE * 2), comment: "")
16
+ end
17
+
18
+ def initialize(algo:, sk:, comment:, pk: nil)
19
+ unless algo == PublicKey::ALGO_MLDSA44_ED25519
20
+ raise DecodeError, "bad algorithm: #{algo.inspect}"
21
+ end
22
+
23
+ unless sk.bytesize == SEED_SIZE * 2
24
+ raise DecodeError, "bad private key length"
25
+ end
26
+
27
+ # The OpenSSH private blob stores the ML-DSA seed first, then the Ed25519 seed.
28
+ # https://datatracker.ietf.org/doc/html/draft-miller-sshm-composite-sigs-01#section-4.1.1.2
29
+ @sk = sk.b.freeze
30
+ @mldsa44_key = OpenSSL::PKey.generate_key("ML-DSA-44", "hexseed" => @sk.byteslice(0, SEED_SIZE).unpack1("H*"))
31
+ @ed25519_key = read_ed25519_seed(@sk.byteslice(SEED_SIZE, SEED_SIZE))
32
+ derived_pk = public_key_bytes(@mldsa44_key) + public_key_bytes(@ed25519_key)
33
+ raise DecodeError, "public key mismatch" if pk && pk != derived_pk
34
+
35
+ @public_key = PublicKey::MLDSA44ED25519.new(algo: algo, pk: derived_pk)
36
+ super(algo: algo, comment: comment)
37
+ rescue OpenSSL::PKey::PKeyError
38
+ # Errors applying hexseed can include private material.
39
+ raise AlgorithmError, "OpenSSL could not load an #{algo} private key", cause: nil
40
+ end
41
+
42
+ def pk
43
+ public_key.pk
44
+ end
45
+
46
+ def sign(signed_data, algo: nil)
47
+ algo ||= self.algo
48
+ raise AlgorithmError unless algo == self.algo
49
+
50
+ # SSH uses an empty composite context (the NUL byte is its length).
51
+ # https://datatracker.ietf.org/doc/html/draft-miller-sshm-composite-sigs-01#section-4.2.1
52
+ message = COMPOSITE_PREFIX + COMPOSITE_LABEL + "\0" + OpenSSL::Digest::SHA512.digest(signed_data)
53
+ mldsa44_sig = @mldsa44_key.sign(nil, message, "context-string" => COMPOSITE_LABEL)
54
+ ed25519_sig = @ed25519_key.sign(nil, message)
55
+ Encoding.encode_signature(algo, mldsa44_sig + ed25519_sig)
56
+ end
57
+
58
+ private
59
+
60
+ def read_ed25519_seed(seed)
61
+ # PKCS#8 carries the seed inside a nested octet string.
62
+ # https://www.rfc-editor.org/rfc/rfc8410.html#section-7
63
+ algorithm = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::ObjectId.new(PublicKey::MLDSA44ED25519::ED25519_OID)])
64
+ key = OpenSSL::ASN1::OctetString.new(OpenSSL::ASN1::OctetString.new(seed).to_der)
65
+ pkcs8 = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::Integer.new(0), algorithm, key])
66
+ OpenSSL::PKey.read(pkcs8.to_der)
67
+ end
68
+
69
+ def public_key_bytes(key)
70
+ OpenSSL::ASN1.decode(key.public_to_der).value.last.value
71
+ end
72
+ end
73
+ end
74
+ end
@@ -48,7 +48,7 @@ module SSHData
48
48
  end
49
49
 
50
50
  def self.from_data(data)
51
- data[:private_keys].map do |priv|
51
+ data[:private_keys].map.with_index do |priv, index|
52
52
  case priv[:algo]
53
53
  when PublicKey::ALGO_RSA
54
54
  RSA.new(**priv)
@@ -58,6 +58,12 @@ module SSHData
58
58
  ECDSA.new(**priv)
59
59
  when PublicKey::ALGO_ED25519
60
60
  ED25519.new(**priv)
61
+ when PublicKey::ALGO_MLDSA44_ED25519
62
+ MLDSA44ED25519.new(**priv).tap do |key|
63
+ unless data[:public_keys][index] == key.public_key.rfc4253
64
+ raise DecodeError, "public key mismatch"
65
+ end
66
+ end
61
67
  else
62
68
  raise DecodeError, "unkown algo: #{priv[:algo].inspect}"
63
69
  end
@@ -71,3 +77,4 @@ require "ssh_data/private_key/rsa"
71
77
  require "ssh_data/private_key/dsa"
72
78
  require "ssh_data/private_key/ecdsa"
73
79
  require "ssh_data/private_key/ed25519"
80
+ require "ssh_data/private_key/mldsa44_ed25519"
@@ -71,7 +71,9 @@ module SSHData
71
71
  #
72
72
  # Returns boolean.
73
73
  def verify(signed_data, signature)
74
- sig_algo, ssh_sig, _ = Encoding.decode_signature(signature)
74
+ sig_algo, ssh_sig, read = Encoding.decode_signature(signature)
75
+ raise DecodeError, "unexpected trailing data" if read != signature.bytesize
76
+
75
77
  if sig_algo != ALGO_DSA
76
78
  raise DecodeError, "bad signature algorithm: #{sig_algo.inspect}"
77
79
  end
@@ -97,7 +97,9 @@ module SSHData
97
97
  #
98
98
  # Returns boolean.
99
99
  def verify(signed_data, signature)
100
- sig_algo, ssh_sig, _ = Encoding.decode_signature(signature)
100
+ sig_algo, ssh_sig, read = Encoding.decode_signature(signature)
101
+ raise DecodeError, "unexpected trailing data" if read != signature.bytesize
102
+
101
103
  if sig_algo != "ecdsa-sha2-#{curve}"
102
104
  raise DecodeError, "bad signature algorithm: #{sig_algo.inspect}"
103
105
  end
@@ -43,7 +43,9 @@ module SSHData
43
43
  def verify(signed_data, signature)
44
44
  self.class.ed25519_gem_required!
45
45
 
46
- sig_algo, raw_sig, _ = Encoding.decode_signature(signature)
46
+ sig_algo, raw_sig, read = Encoding.decode_signature(signature)
47
+ raise DecodeError, "unexpected trailing data" if read != signature.bytesize
48
+
47
49
  if sig_algo != self.class.algorithm_identifier
48
50
  raise DecodeError, "bad signature algorithm: #{sig_algo.inspect}"
49
51
  end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SSHData
4
+ module PublicKey
5
+ class MLDSA44ED25519 < Base
6
+ MLDSA44_PUBLIC_KEY_SIZE = 1312
7
+ ED25519_PUBLIC_KEY_SIZE = 32
8
+ MLDSA44_SIGNATURE_SIZE = 2420
9
+ ED25519_SIGNATURE_SIZE = 64
10
+
11
+ MLDSA44_OID = "2.16.840.1.101.3.4.3.17"
12
+ ED25519_OID = "1.3.101.112"
13
+ COMPOSITE_PREFIX = "CompositeAlgorithmSignatures2025"
14
+ COMPOSITE_LABEL = "COMPSIG-MLDSA44-Ed25519-SHA512"
15
+
16
+ attr_reader :pk
17
+
18
+ def initialize(algo:, pk:)
19
+ unless algo == ALGO_MLDSA44_ED25519
20
+ raise DecodeError, "bad algorithm: #{algo.inspect}"
21
+ end
22
+
23
+ unless pk.bytesize == MLDSA44_PUBLIC_KEY_SIZE + ED25519_PUBLIC_KEY_SIZE
24
+ raise DecodeError, "bad public key length"
25
+ end
26
+
27
+ @pk = pk.b.freeze
28
+ super(algo: algo)
29
+ end
30
+
31
+ def verify(signed_data, signature)
32
+ sig_algo, raw_sig, read = Encoding.decode_signature(signature)
33
+ raise DecodeError, "unexpected trailing data" if read != signature.bytesize
34
+
35
+ unless sig_algo == ALGO_MLDSA44_ED25519
36
+ raise DecodeError, "bad signature algorithm: #{sig_algo.inspect}"
37
+ end
38
+
39
+ unless raw_sig.bytesize == MLDSA44_SIGNATURE_SIZE + ED25519_SIGNATURE_SIZE
40
+ raise DecodeError, "bad signature length"
41
+ end
42
+
43
+ mldsa44_key, ed25519_key = openssl_keys
44
+ mldsa44_sig = raw_sig.byteslice(0, MLDSA44_SIGNATURE_SIZE)
45
+ ed25519_sig = raw_sig.byteslice(MLDSA44_SIGNATURE_SIZE, ED25519_SIGNATURE_SIZE)
46
+
47
+ # SSH uses an empty composite context (the NUL byte is its length).
48
+ # https://datatracker.ietf.org/doc/html/draft-miller-sshm-composite-sigs-01#section-4.2.1
49
+ message = COMPOSITE_PREFIX + COMPOSITE_LABEL + "\0" + OpenSSL::Digest::SHA512.digest(signed_data)
50
+
51
+ # ML-DSA receives the composite label as its own context.
52
+ # https://datatracker.ietf.org/doc/html/draft-miller-sshm-composite-sigs-01#section-4.2.2.4.1
53
+ # https://www.rfc-editor.org/rfc/rfc10073.html#section-3.3
54
+ mldsa44_key.verify(nil, mldsa44_sig, message, "context-string" => COMPOSITE_LABEL) &&
55
+ ed25519_key.verify(nil, ed25519_sig, message)
56
+ end
57
+
58
+ def rfc4253
59
+ Encoding.encode_fields(
60
+ [:string, algo],
61
+ [:string, pk],
62
+ )
63
+ end
64
+
65
+ def ==(other)
66
+ super && other.pk == pk
67
+ end
68
+
69
+ private
70
+
71
+ def openssl_keys
72
+ @openssl_keys ||= [
73
+ read_public_key(MLDSA44_OID, pk.byteslice(0, MLDSA44_PUBLIC_KEY_SIZE)),
74
+ read_public_key(ED25519_OID, pk.byteslice(MLDSA44_PUBLIC_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE)),
75
+ ]
76
+ rescue OpenSSL::PKey::PKeyError => e
77
+ raise AlgorithmError, "OpenSSL could not load an #{ALGO_MLDSA44_ED25519} public key: #{e.message}"
78
+ end
79
+
80
+ def read_public_key(oid, bytes)
81
+ algorithm = OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::ObjectId.new(oid)])
82
+ key = OpenSSL::ASN1::Sequence.new([algorithm, OpenSSL::ASN1::BitString.new(bytes)])
83
+ OpenSSL::PKey.read(key.to_der)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -30,7 +30,9 @@ module SSHData
30
30
  #
31
31
  # Returns boolean.
32
32
  def verify(signed_data, signature)
33
- sig_algo, raw_sig, _ = Encoding.decode_signature(signature)
33
+ sig_algo, raw_sig, read = Encoding.decode_signature(signature)
34
+ raise DecodeError, "unexpected trailing data" if read != signature.bytesize
35
+
34
36
  digest = ALGO_DIGESTS[sig_algo]
35
37
 
36
38
  if digest.nil?
@@ -7,6 +7,7 @@ module SSHData
7
7
  ALGO_ECDSA384 = "ecdsa-sha2-nistp384"
8
8
  ALGO_ECDSA521 = "ecdsa-sha2-nistp521"
9
9
  ALGO_ED25519 = "ssh-ed25519"
10
+ ALGO_MLDSA44_ED25519 = "ssh-mldsa44-ed25519"
10
11
  ALGO_SKED25519 = "sk-ssh-ed25519@openssh.com"
11
12
  ALGO_SKECDSA256 = "sk-ecdsa-sha2-nistp256@openssh.com"
12
13
 
@@ -17,7 +18,7 @@ module SSHData
17
18
 
18
19
  ALGOS = [
19
20
  ALGO_RSA, ALGO_DSA, ALGO_ECDSA256, ALGO_ECDSA384, ALGO_ECDSA521,
20
- ALGO_ED25519, ALGO_SKECDSA256, ALGO_SKED25519
21
+ ALGO_ED25519, ALGO_MLDSA44_ED25519, ALGO_SKECDSA256, ALGO_SKED25519
21
22
  ]
22
23
 
23
24
  # Parse an OpenSSH public key in authorized_keys format (see sshd(8) manual
@@ -66,6 +67,8 @@ module SSHData
66
67
  ECDSA.new(**data)
67
68
  when ALGO_ED25519
68
69
  ED25519.new(**data)
70
+ when ALGO_MLDSA44_ED25519
71
+ MLDSA44ED25519.new(**data)
69
72
  when ALGO_SKED25519
70
73
  SKED25519.new(**data)
71
74
  when ALGO_SKECDSA256
@@ -83,5 +86,6 @@ require "ssh_data/public_key/rsa"
83
86
  require "ssh_data/public_key/dsa"
84
87
  require "ssh_data/public_key/ecdsa"
85
88
  require "ssh_data/public_key/ed25519"
89
+ require "ssh_data/public_key/mldsa44_ed25519"
86
90
  require "ssh_data/public_key/sked25519"
87
91
  require "ssh_data/public_key/skecdsa"
@@ -1,3 +1,3 @@
1
1
  module SSHData
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ssh_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mastahyeti
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 2025-01-06 00:00:00.000000000 Z
11
+ date: 2026-12-03 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: base64
@@ -93,6 +94,7 @@ dependencies:
93
94
  - - "~>"
94
95
  - !ruby/object:Gem::Version
95
96
  version: '3.10'
97
+ description:
96
98
  email: opensource+ssh_data@github.com
97
99
  executables: []
98
100
  extensions: []
@@ -108,12 +110,14 @@ files:
108
110
  - "./lib/ssh_data/private_key/dsa.rb"
109
111
  - "./lib/ssh_data/private_key/ecdsa.rb"
110
112
  - "./lib/ssh_data/private_key/ed25519.rb"
113
+ - "./lib/ssh_data/private_key/mldsa44_ed25519.rb"
111
114
  - "./lib/ssh_data/private_key/rsa.rb"
112
115
  - "./lib/ssh_data/public_key.rb"
113
116
  - "./lib/ssh_data/public_key/base.rb"
114
117
  - "./lib/ssh_data/public_key/dsa.rb"
115
118
  - "./lib/ssh_data/public_key/ecdsa.rb"
116
119
  - "./lib/ssh_data/public_key/ed25519.rb"
120
+ - "./lib/ssh_data/public_key/mldsa44_ed25519.rb"
117
121
  - "./lib/ssh_data/public_key/rsa.rb"
118
122
  - "./lib/ssh_data/public_key/security_key.rb"
119
123
  - "./lib/ssh_data/public_key/skecdsa.rb"
@@ -124,6 +128,7 @@ homepage: https://github.com/github/ssh_data
124
128
  licenses:
125
129
  - MIT
126
130
  metadata: {}
131
+ post_install_message:
127
132
  rdoc_options: []
128
133
  require_paths:
129
134
  - lib
@@ -138,7 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
143
  - !ruby/object:Gem::Version
139
144
  version: '0'
140
145
  requirements: []
141
- rubygems_version: 3.6.2
146
+ rubygems_version: 3.5.22
147
+ signing_key:
142
148
  specification_version: 4
143
149
  summary: Library for parsing SSH certificates
144
150
  test_files: []