Skip to content

Commit 684dbe6

Browse files
author
Alex Wilson
committed
#62 handle pkcs8 ECDSA keys with missing public parts
Reviewed by: Cody Peter Mello <[email protected]>
1 parent 574ff21 commit 684dbe6

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

lib/formats/pkcs8.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,22 @@ function readPkcs8ECDSAPrivate(der) {
301301
assert.equal(version[0], 1, 'unknown version of ECDSA key');
302302

303303
var d = der.readString(asn1.Ber.OctetString, true);
304-
der.readSequence(0xa1);
304+
var Q;
305305

306-
var Q = der.readString(asn1.Ber.BitString, true);
307-
Q = utils.ecNormalize(Q);
306+
if (der.peek() == 0xa0) {
307+
der.readSequence(0xa0);
308+
der._offset += der.length;
309+
}
310+
if (der.peek() == 0xa1) {
311+
der.readSequence(0xa1);
312+
Q = der.readString(asn1.Ber.BitString, true);
313+
Q = utils.ecNormalize(Q);
314+
}
315+
316+
if (Q === undefined) {
317+
var pub = utils.publicFromPrivateECDSA(curveName, d);
318+
Q = pub.part.Q.data;
319+
}
308320

309321
var key = {
310322
type: 'ecdsa',

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sshpk",
3-
"version": "1.16.0",
3+
"version": "1.16.1",
44
"description": "A library for finding and using SSH public keys",
55
"main": "lib/index.js",
66
"scripts": {

test/assets/pkcs8-nopub.pem

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCD1/r3zho5W2MpyZEk+
3+
2d7gxUcQYUJzvWSOiwkUxCj8Bw==
4+
-----END PRIVATE KEY-----

test/private-key.js

+11
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,17 @@ test('PrivateKey.generate ecdsa p-384', function (t) {
373373
t.end();
374374
});
375375

376+
test('pkcs8 PrivateKey without public part', function (t) {
377+
var pem = fs.readFileSync(path.join(testDir, 'pkcs8-nopub.pem'));
378+
var key = sshpk.parsePrivateKey(pem, 'pem');
379+
t.strictEqual(key.type, 'ecdsa');
380+
t.strictEqual(key.curve, 'nistp256');
381+
var fp = sshpk.parseFingerprint(
382+
'SHA256:wU/JTqlHV21vv0tcaNOFUZD2FXciO2KwImEOW1+AH50');
383+
t.ok(fp.matches(key));
384+
t.end();
385+
});
386+
376387
if (process.version.match(/^v0\.[0-9]\./))
377388
return;
378389

0 commit comments

Comments
 (0)