Skip to content

Commit ca5c69a

Browse files
committed
supporting private keys other than RSA
see njh#148
1 parent fdf0882 commit ca5c69a

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

lib/mqtt/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ def cert=(cert)
203203
# Set a path to a file containing a PEM-format client private key
204204
def key_file=(*args)
205205
path, passphrase = args.flatten
206-
ssl_context.key = OpenSSL::PKey::RSA.new(File.open(path), passphrase)
206+
ssl_context.key = OpenSSL::PKey.read(File.binread(path), passphrase)
207207
end
208208

209209
# Set to a PEM-format client private key
210210
def key=(*args)
211211
cert, passphrase = args.flatten
212-
ssl_context.key = OpenSSL::PKey::RSA.new(cert, passphrase)
212+
ssl_context.key = OpenSSL::PKey.read(cert, passphrase)
213213
end
214214

215215
# Set a path to a file containing a PEM-format CA certificate and enable peer verification

spec/fixtures/ec.key

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHcCAQEEIE2eyE3r4eoZCbHMYIwLCW42IKqaCkTSpw4dE4+j2TTqoAoGCCqGSM49
3+
AwEHoUQDQgAEoAyjMxTzzh9dEkzmXk26Vomq7HQFon/m4hDcKNAbqcrLVJI8bcQt
4+
yewCuHTAu3A6ymRxZnYvcNgMPyK+Oc+umA==
5+
-----END EC PRIVATE KEY-----

spec/fixtures/ec.pass.key

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
Proc-Type: 4,ENCRYPTED
3+
DEK-Info: AES-256-CBC,0727A85143BDD14830310915273C3879
4+
5+
vbjVLdV7YvVWnRHUzKVZjO5YR+q4GL3LU/BAlAj/E0klH+6ytEU34tpEtBfyC5QR
6+
bkDd/40qO6NGh81VvvEzafGQbnBHlBRxWZ52FspFob9ry+bW8F6sGbp46Ny6vTc/
7+
BSOtHDN+tDG5PQx9YXSVgBwRkekX86/63Zgh3jiy6rg=
8+
-----END EC PRIVATE KEY-----

spec/mqtt/client_spec.rb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,47 @@
211211
describe "setting an encrypted client private key, w/an incorrect passphrase" do
212212
let(:key_pass) { "ttqm" }
213213

214-
it "raises an OpenSSL::PKey::RSAError exception" do
214+
it "raises an exception" do
215215
expect(client.ssl_context.key).to be_nil
216216
expect { client.key_file = [fixture_path("client.pass.key"), key_pass] }.to(
217-
raise_error(OpenSSL::PKey::RSAError, /Neither PUB key nor PRIV key/)
217+
raise_error(/Could not parse PKey/)
218+
)
219+
end
220+
end
221+
222+
describe "setting a client private EC key file path" do
223+
it "adds a certificate to the SSL context" do
224+
expect(client.ssl_context.key).to be_nil
225+
client.key_file = fixture_path("ec.key")
226+
expect(client.ssl_context.key).to be_a(OpenSSL::PKey::EC)
227+
end
228+
end
229+
230+
describe "setting a client private EC key directly" do
231+
it "adds a certificate to the SSL context" do
232+
expect(client.ssl_context.key).to be_nil
233+
client.key = File.read(fixture_path("ec.key"))
234+
expect(client.ssl_context.key).to be_a(OpenSSL::PKey::EC)
235+
end
236+
end
237+
238+
describe "setting an encrypted client private EC key, w/the correct passphrase" do
239+
let(:key_pass) { "mqtt" }
240+
241+
it "adds the decrypted certificate to the SSL context" do
242+
expect(client.ssl_context.key).to be_nil
243+
client.key_file = [fixture_path("ec.pass.key"), key_pass]
244+
expect(client.ssl_context.key).to be_a(OpenSSL::PKey::EC)
245+
end
246+
end
247+
248+
describe "setting an encrypted client private EC key, w/an incorrect passphrase" do
249+
let(:key_pass) { "ttqm" }
250+
251+
it "raises an exception" do
252+
expect(client.ssl_context.key).to be_nil
253+
expect { client.key_file = [fixture_path("ec.pass.key"), key_pass] }.to(
254+
raise_error(/Could not parse PKey/)
218255
)
219256
end
220257
end

0 commit comments

Comments
 (0)