Skip to content

Commit

Permalink
Fix Firefox padding
Browse files Browse the repository at this point in the history
  • Loading branch information
martijndwars committed May 29, 2016
1 parent da2bf6e commit 363af79
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/main/java/nl/martijndwars/webpush/GcmNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public String getEndpoint() {
public String getRegistrationId() {
return super.getEndpoint().substring(super.getEndpoint().lastIndexOf("/") + 1);
}

@Override
public int getPadSize() {
return 2;
}
}
2 changes: 1 addition & 1 deletion src/main/java/nl/martijndwars/webpush/HttpEce.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public byte[] encrypt(byte[] buffer, byte[] salt, byte[] key, String keyid, Publ

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key_, "AES"), new GCMParameterSpec(16 * 8, nonce_));
cipher.update(new byte[2]);
cipher.update(new byte[padSize]);

return cipher.doFinal(buffer);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/nl/martijndwars/webpush/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public byte[] getPayload() {
public int getTTL() {
return ttl;
}

public int getPadSize() {
return 1;
}
}
11 changes: 6 additions & 5 deletions src/main/java/nl/martijndwars/webpush/PushService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public PushService(String gcmApiKey) {
* @return An Encrypted object containing the public key, salt, and
* ciphertext, which can be sent to the other party.
*/
public static Encrypted encrypt(byte[] buffer, PublicKey userPublicKey, byte[] userAuth) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException, IOException {
public static Encrypted encrypt(byte[] buffer, PublicKey userPublicKey, byte[] userAuth, int padSize) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException, IOException {
ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec("prime256v1");

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDH", "BC");
Expand All @@ -59,7 +59,7 @@ public static Encrypted encrypt(byte[] buffer, PublicKey userPublicKey, byte[] u
byte[] salt = SecureRandom.getSeed(16);

HttpEce httpEce = new HttpEce(keys, labels);
byte[] ciphertext = httpEce.encrypt(buffer, salt, null, "server-key-id", userPublicKey, userAuth, 2);
byte[] ciphertext = httpEce.encrypt(buffer, salt, null, "server-key-id", userPublicKey, userAuth, padSize);

return new Encrypted.Builder()
.withSalt(salt)
Expand All @@ -78,7 +78,8 @@ public Future<Content> send(Notification notification) throws NoSuchPaddingExcep
Encrypted encrypted = encrypt(
notification.getPayload(),
notification.getUserPublicKey(),
notification.getUserAuth()
notification.getUserAuth(),
notification.getPadSize()
);

byte[] dh = Utils.savePublicKey((ECPublicKey) encrypted.getPublicKey());
Expand Down Expand Up @@ -108,8 +109,8 @@ public Future<Content> send(Notification notification) throws NoSuchPaddingExcep
request
.addHeader("Content-Type", "application/octet-stream")
.addHeader("Content-Encoding", "aesgcm128")
.addHeader("Encryption-Key", "keyid=p256dh;dh=" + base64url.encode(dh))
.addHeader("Encryption", "keyid=p256dh;salt=" + base64url.encode(salt))
.addHeader("Encryption-Key", "keyid=p256dh;dh=" + base64url.omitPadding().encode(dh))
.addHeader("Encryption", "keyid=p256dh;salt=" + base64url.omitPadding().encode(salt))
.bodyByteArray(encrypted.getCiphertext());
}

Expand Down
24 changes: 23 additions & 1 deletion src/test/java/nl/martijndwars/webpush/PushServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void addSecurityProvider() {
}

@Test
public void testPush() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IOException, ExecutionException, InterruptedException {
public void testPushChrome() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IOException, ExecutionException, InterruptedException {
Security.addProvider(new BouncyCastleProvider());

String gcmApiKey = "AIzaSyDSa2bw0b0UGOmkZRw-dqHGQRI_JqpiHug";
Expand All @@ -45,4 +45,26 @@ public void testPush() throws NoSuchProviderException, NoSuchAlgorithmException,

System.out.println(httpResponse.get().asString());
}

@Test
public void testPushFirefox() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IOException, ExecutionException, InterruptedException {
Security.addProvider(new BouncyCastleProvider());

String endpoint = "https://updates.push.services.mozilla.com/push/v1/gAAAAABXS0Nhothwqf0Je2mmjuRgyXjgVylY0yZ4qmP3cglrFoneY-XOLdJuGZOsv5Eh7ndhe8mMvge3VcLhpgbQ3w6_vWK7FZkSXhzjlaIxikL6cbW6Gok5BVw1tL1jqruy5Y-deSoz";
String encodedUserPublicKey = "BNP6uzB5yqQDltCnO1snr-Qx3wLUPgeznuUQjfFbmehRHJK3s4eaqy04nOnm9796mceidVJPlFaobd94yjwtmpU=";

PublicKey userPublicKey = Utils.loadPublicKey(encodedUserPublicKey);

Notification notification = new Notification(
endpoint,
userPublicKey,
null,
"{\"title\": \"Hello\", \"message\": \"World\"}".getBytes()
);

PushService pushService = new PushService();
Future<Content> httpResponse = pushService.send(notification);

System.out.println(httpResponse.get().asString());
}
}

0 comments on commit 363af79

Please sign in to comment.