You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2021/02/18 15:57:20 UTC

[pulsar] 26/27: Add BouncyCastleProvider as security provider to prevent NPE (#9601)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch branch-2.7
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 276642ec95d601a188574cc72e5fa93a1e9181e2
Author: Masahiro Sakamoto <ma...@yahoo-corp.jp>
AuthorDate: Thu Feb 18 14:39:35 2021 +0900

    Add BouncyCastleProvider as security provider to prevent NPE (#9601)
    
    
    (cherry picked from commit 28585e2a65a7a24141be4643da0391266d7c1368)
---
 .../java/org/apache/pulsar/client/impl/crypto/MessageCryptoBc.java  | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/pulsar-client-messagecrypto-bc/src/main/java/org/apache/pulsar/client/impl/crypto/MessageCryptoBc.java b/pulsar-client-messagecrypto-bc/src/main/java/org/apache/pulsar/client/impl/crypto/MessageCryptoBc.java
index c8b4aa0..d7563ba 100644
--- a/pulsar-client-messagecrypto-bc/src/main/java/org/apache/pulsar/client/impl/crypto/MessageCryptoBc.java
+++ b/pulsar-client-messagecrypto-bc/src/main/java/org/apache/pulsar/client/impl/crypto/MessageCryptoBc.java
@@ -37,6 +37,7 @@ import java.security.NoSuchProviderException;
 import java.security.PrivateKey;
 import java.security.PublicKey;
 import java.security.SecureRandom;
+import java.security.Security;
 import java.security.spec.InvalidKeySpecException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -126,6 +127,11 @@ public class MessageCryptoBc implements MessageCrypto<MessageMetadata, MessageMe
 
         // Initial seed
         secureRandom.nextBytes(new byte[IV_LEN]);
+
+        // Add provider only if it's not in the JVM
+        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
+            Security.addProvider(new BouncyCastleProvider());
+        }
     }
 
     public MessageCryptoBc(String logCtx, boolean keyGenNeeded) {