Skip to content

Commit 7324272

Browse files
Matt Gauntmarco-c
authored andcommitted
Fixes #295 (#296)
* Fixes #295 * Fixing lint * Adding test to pad public and private key
1 parent b56c83e commit 7324272

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"selenium-assistant": "1.0.0",
5151
"selenium-webdriver": "3.0.0-beta-2",
5252
"semver": "^5.1.0",
53+
"sinon": "^4.0.1",
5354
"which": "^1.2.11"
5455
},
5556
"engines": {

src/vapid-helper.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,27 @@ function generateVAPIDKeys() {
3939
const curve = crypto.createECDH('prime256v1');
4040
curve.generateKeys();
4141

42+
let publicKeyBuffer = curve.getPublicKey();
43+
let privateKeyBuffer = curve.getPrivateKey();
44+
45+
// Occassionally the keys will not be padded to the correct lengh resulting
46+
// in errors, hence this padding.
47+
// See https://github.com/web-push-libs/web-push/issues/295 for history.
48+
if (privateKeyBuffer.length < 32) {
49+
const padding = new Buffer(32 - privateKeyBuffer.length);
50+
padding.fill(0);
51+
privateKeyBuffer = Buffer.concat([privateKeyBuffer, padding]);
52+
}
53+
54+
if (publicKeyBuffer.length < 65) {
55+
const padding = new Buffer(65 - publicKeyBuffer.length);
56+
padding.fill(0);
57+
publicKeyBuffer = Buffer.concat([publicKeyBuffer, padding]);
58+
}
59+
4260
return {
43-
publicKey: urlBase64.encode(curve.getPublicKey()),
44-
privateKey: urlBase64.encode(curve.getPrivateKey())
61+
publicKey: urlBase64.encode(publicKeyBuffer),
62+
privateKey: urlBase64.encode(privateKeyBuffer)
4563
};
4664
}
4765

test/test-vapid-helper.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const assert = require('assert');
44
const urlBase64 = require('urlsafe-base64');
5+
const sinon = require('sinon');
6+
const crypto = require('crypto');
57
const webPush = require('../src/index');
68
const vapidHelper = require('../src/vapid-helper');
79

@@ -13,6 +15,16 @@ const VALID_PRIVATE_KEY = urlBase64.encode(new Buffer(32));
1315
const VALID_EXPIRATION = Math.floor(Date.now() / 1000) + (60 * 60 * 12);
1416

1517
suite('Test Vapid Helpers', function() {
18+
const sandbox = sinon.sandbox.create();
19+
20+
beforeEach(function() {
21+
sandbox.restore();
22+
});
23+
24+
after(function() {
25+
sandbox.restore();
26+
});
27+
1628
test('is defined', function() {
1729
assert(webPush.generateVAPIDKeys);
1830
assert(webPush.getVapidHeaders);
@@ -30,6 +42,26 @@ suite('Test Vapid Helpers', function() {
3042
assert.equal(urlBase64.decode(keys.publicKey).length, 65);
3143
});
3244

45+
test('generate vapid keys with padding', function() {
46+
sandbox.stub(crypto, 'createECDH').callsFake(() => {
47+
return {
48+
generateKeys: () => {},
49+
getPublicKey: () => new Buffer(60),
50+
getPrivateKey: () => new Buffer(27)
51+
};
52+
});
53+
54+
const keys = webPush.generateVAPIDKeys();
55+
assert(keys.privateKey);
56+
assert(keys.publicKey);
57+
58+
assert.equal(typeof keys.privateKey, 'string');
59+
assert.equal(typeof keys.publicKey, 'string');
60+
61+
assert.equal(urlBase64.decode(keys.privateKey).length, 32);
62+
assert.equal(urlBase64.decode(keys.publicKey).length, 65);
63+
});
64+
3365
test('generate new vapid keys between calls', function() {
3466
const keys = webPush.generateVAPIDKeys();
3567
assert(keys.privateKey);

0 commit comments

Comments
 (0)