Skip to content

Commit 558cf20

Browse files
committed
Remove explicit BouncyCastle provider from JCE getInstance calls
1 parent 52eb94b commit 558cf20

5 files changed

Lines changed: 20 additions & 26 deletions

File tree

src/main/java/nl/martijndwars/webpush/HttpEce.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public byte[] encrypt(byte[] plaintext, byte[] salt, byte[] privateKey, String k
7272
byte[] nonce = keyAndNonce[1];
7373

7474
// Note: Cipher adds the tag to the end of the ciphertext
75-
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
75+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
7676
GCMParameterSpec params = new GCMParameterSpec(TAG_SIZE * 8, nonce);
7777
cipher.init(ENCRYPT_MODE, new SecretKeySpec(key, "AES"), params);
7878

@@ -101,7 +101,7 @@ public byte[] encrypt(byte[] plaintext, byte[] salt, byte[] privateKey, String k
101101
* @param version AES128GCM or AESGCM.
102102
* @return
103103
*/
104-
public byte[] decrypt(byte[] payload, byte[] salt, byte[] key, String keyid, Encoding version) throws InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidAlgorithmParameterException, BadPaddingException, NoSuchProviderException, NoSuchPaddingException {
104+
public byte[] decrypt(byte[] payload, byte[] salt, byte[] key, String keyid, Encoding version) throws InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidAlgorithmParameterException, BadPaddingException, NoSuchPaddingException {
105105
byte[] body;
106106

107107
// Parse and strip the header
@@ -136,8 +136,8 @@ public byte[][] parseHeader(byte[] payload) {
136136
};
137137
}
138138

139-
public byte[] decryptRecord(byte[] ciphertext, byte[] key, byte[] nonce, Encoding version) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
140-
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
139+
public byte[] decryptRecord(byte[] ciphertext, byte[] key, byte[] nonce, Encoding version) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
140+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
141141
GCMParameterSpec params = new GCMParameterSpec(TAG_SIZE * 8, nonce);
142142
cipher.init(DECRYPT_MODE, new SecretKeySpec(key, "AES"), params);
143143

src/main/java/nl/martijndwars/webpush/Notification.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.net.MalformedURLException;
66
import java.net.URL;
77
import java.security.NoSuchAlgorithmException;
8-
import java.security.NoSuchProviderException;
98
import java.security.PublicKey;
109
import java.security.spec.InvalidKeySpecException;
1110

@@ -54,15 +53,15 @@ public Notification(String endpoint, PublicKey userPublicKey, byte[] userAuth, b
5453
this(endpoint, userPublicKey, userAuth, payload, 2419200);
5554
}
5655

57-
public Notification(String endpoint, String userPublicKey, String userAuth, byte[] payload) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
56+
public Notification(String endpoint, String userPublicKey, String userAuth, byte[] payload) throws NoSuchAlgorithmException, InvalidKeySpecException {
5857
this(endpoint, Utils.loadPublicKey(userPublicKey), Base64Encoder.decode(userAuth), payload);
5958
}
6059

61-
public Notification(String endpoint, String userPublicKey, String userAuth, String payload) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
60+
public Notification(String endpoint, String userPublicKey, String userAuth, String payload) throws NoSuchAlgorithmException, InvalidKeySpecException {
6261
this(endpoint, Utils.loadPublicKey(userPublicKey), Base64Encoder.decode(userAuth), payload.getBytes(UTF_8));
6362
}
6463

65-
public Notification(Subscription subscription, String payload) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
64+
public Notification(Subscription subscription, String payload) throws NoSuchAlgorithmException, InvalidKeySpecException {
6665
this(subscription.endpoint, subscription.keys.p256dh, subscription.keys.auth, payload);
6766
}
6867

src/main/java/nl/martijndwars/webpush/PushService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,11 @@ public static Encrypted encrypt(byte[] payload, ECPublicKey userPublicKey, byte[
106106
*
107107
* @return
108108
* @throws NoSuchAlgorithmException
109-
* @throws NoSuchProviderException
110109
* @throws InvalidAlgorithmParameterException
111110
*/
112-
private static KeyPair generateLocalKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
111+
private static KeyPair generateLocalKeyPair() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
113112
ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec("prime256v1");
114-
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDH", "BC");
113+
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDH");
115114
keyPairGenerator.initialize(parameterSpec);
116115

117116
return keyPairGenerator.generateKeyPair();
@@ -302,7 +301,7 @@ public PublicKey getPublicKey() {
302301
* @param publicKey
303302
* @return
304303
*/
305-
public PushService setPublicKey(String publicKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
304+
public PushService setPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
306305
setPublicKey(Utils.loadPublicKey(publicKey));
307306

308307
return this;
@@ -334,7 +333,7 @@ public PushService setPublicKey(PublicKey publicKey) {
334333
* @param privateKey
335334
* @return
336335
*/
337-
public PushService setPrivateKey(String privateKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
336+
public PushService setPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
338337
setPrivateKey(Utils.loadPrivateKey(privateKey));
339338

340339
return this;

src/main/java/nl/martijndwars/webpush/Utils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.security.*;
1717
import java.security.spec.InvalidKeySpecException;
1818

19-
import static org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME;
2019

2120
public class Utils {
2221
public static final String CURVE = "prime256v1";
@@ -44,9 +43,9 @@ public static byte[] encode(ECPrivateKey privateKey) {
4443
*
4544
* @param encodedPublicKey
4645
*/
47-
public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
46+
public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
4847
byte[] decodedPublicKey = Base64Encoder.decode(encodedPublicKey);
49-
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
48+
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
5049
ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
5150
ECCurve curve = parameterSpec.getCurve();
5251
ECPoint point = curve.decodePoint(decodedPublicKey);
@@ -60,16 +59,15 @@ public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProv
6059
*
6160
* @param encodedPrivateKey
6261
* @return
63-
* @throws NoSuchProviderException
6462
* @throws NoSuchAlgorithmException
6563
* @throws InvalidKeySpecException
6664
*/
67-
public static PrivateKey loadPrivateKey(String encodedPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
65+
public static PrivateKey loadPrivateKey(String encodedPrivateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
6866
byte[] decodedPrivateKey = Base64Encoder.decode(encodedPrivateKey);
6967
BigInteger s = BigIntegers.fromUnsignedByteArray(decodedPrivateKey);
7068
ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
7169
ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, parameterSpec);
72-
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
70+
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
7371

7472
return keyFactory.generatePrivate(privateKeySpec);
7573
}
@@ -80,8 +78,8 @@ public static PrivateKey loadPrivateKey(String encodedPrivateKey) throws NoSuchP
8078
* @param privateKey
8179
* @return
8280
*/
83-
public static ECPublicKey loadPublicKey(ECPrivateKey privateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
84-
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
81+
public static ECPublicKey loadPublicKey(ECPrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
82+
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
8583
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CURVE);
8684
ECPoint Q = ecSpec.getG().multiply(privateKey.getD());
8785
byte[] publicDerBytes = Q.getEncoded(false);

src/main/java/nl/martijndwars/webpush/cli/handlers/GenerateKeyHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static nl.martijndwars.webpush.Utils.ALGORITHM;
2020
import static nl.martijndwars.webpush.Utils.CURVE;
21-
import static org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME;
2221

2322
public class GenerateKeyHandler implements HandlerInterface {
2423
private GenerateKeyCommand generateKeyCommand;
@@ -28,7 +27,7 @@ public GenerateKeyHandler(GenerateKeyCommand generateKeyCommand) {
2827
}
2928

3029
@Override
31-
public void run() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
30+
public void run() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException {
3231
KeyPair keyPair = generateKeyPair();
3332

3433
ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
@@ -53,13 +52,12 @@ public void run() throws InvalidAlgorithmParameterException, NoSuchAlgorithmExce
5352
*
5453
* @return
5554
* @throws InvalidAlgorithmParameterException
56-
* @throws NoSuchProviderException
5755
* @throws NoSuchAlgorithmException
5856
*/
59-
public KeyPair generateKeyPair() throws InvalidAlgorithmParameterException, NoSuchProviderException, NoSuchAlgorithmException {
57+
public KeyPair generateKeyPair() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
6058
ECNamedCurveParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
6159

62-
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM, PROVIDER_NAME);
60+
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
6361
keyPairGenerator.initialize(parameterSpec);
6462

6563
return keyPairGenerator.generateKeyPair();

0 commit comments

Comments
 (0)