6
6
import org .bouncycastle .asn1 .ASN1InputStream ;
7
7
import org .bouncycastle .asn1 .ASN1Sequence ;
8
8
import org .bouncycastle .asn1 .pkcs .PrivateKeyInfo ;
9
+ import org .bouncycastle .jce .provider .BouncyCastleProvider ;
9
10
import org .bouncycastle .openssl .PEMKeyPair ;
10
11
import org .bouncycastle .openssl .PEMParser ;
12
+ import org .bouncycastle .operator .InputDecryptorProvider ;
13
+ import org .bouncycastle .pkcs .PKCS8EncryptedPrivateKeyInfo ;
14
+ import org .bouncycastle .pkcs .jcajce .JcePKCSPBEInputDecryptorProviderBuilder ;
11
15
import org .bouncycastle .util .encoders .Base64 ;
12
16
17
+ import javax .crypto .EncryptedPrivateKeyInfo ;
13
18
import java .io .FileReader ;
14
19
import java .io .InputStream ;
15
20
import java .nio .file .Files ;
16
21
import java .nio .file .Paths ;
17
22
import java .security .KeyFactory ;
18
23
import java .security .KeyStore ;
24
+ import java .security .Security ;
19
25
import java .security .interfaces .RSAPrivateKey ;
20
26
import java .security .spec .PKCS8EncodedKeySpec ;
21
27
@@ -49,7 +55,7 @@ public static PrivateKeyUtil value(String ext) {
49
55
50
56
public static RSAPrivateKey getPrivateKey (String path , String alias , String password ) {
51
57
String extension = FilenameUtils .getExtension (path );
52
- PrivateKeyUtil ext = extension .isEmpty () ? PrivateKeyUtil .value ("b64" ): PrivateKeyUtil .value (extension );
58
+ PrivateKeyUtil ext = extension .isEmpty () ? PrivateKeyUtil .value ("b64" ) : PrivateKeyUtil .value (extension );
53
59
switch (ext ) {
54
60
case pfx :
55
61
case jks :
@@ -58,7 +64,7 @@ public static RSAPrivateKey getPrivateKey(String path, String alias, String pass
58
64
return loadFromPkcs12 (path , alias , password );
59
65
case pem :
60
66
case key :
61
- return loadFromPkcs8 (path );
67
+ return loadFromPkcs8 (path , password );
62
68
case b64 :
63
69
return loadFromBase64 (path );
64
70
default :
@@ -67,20 +73,18 @@ public static RSAPrivateKey getPrivateKey(String path, String alias, String pass
67
73
}
68
74
}
69
75
70
- private static RSAPrivateKey loadFromBase64 (String base64 )
71
- {
76
+ private static RSAPrivateKey loadFromBase64 (String base64 ) {
72
77
logger .debug ("loadFromBase64" );
73
- try (ASN1InputStream stream = new ASN1InputStream (Base64 .decode (base64 ))) {
78
+ try (ASN1InputStream stream = new ASN1InputStream (Base64 .decode (base64 ))) {
74
79
ASN1Sequence seq = (ASN1Sequence ) stream .readObject ();
75
80
return castPrivateKeyInfo (PrivateKeyInfo .getInstance (seq ));
76
- }catch (Exception e )
77
- {
81
+ } catch (Exception e ) {
78
82
logger .error ("loadFromBase64" , e );
79
83
return null ;
80
84
}
81
85
}
82
86
83
- private static RSAPrivateKey loadFromPkcs8 (String path ) {
87
+ private static RSAPrivateKey loadFromPkcs8 (String path , String password ) {
84
88
logger .debug ("loadFromPkcs8" );
85
89
try (FileReader privateKeyReader = new FileReader (path )) {
86
90
try (PEMParser parser = new PEMParser (privateKeyReader )) {
@@ -91,6 +95,13 @@ private static RSAPrivateKey loadFromPkcs8(String path) {
91
95
} else if (obj instanceof PEMKeyPair ) {
92
96
PEMKeyPair pemKeyPair = (PEMKeyPair ) obj ;
93
97
return castPrivateKeyInfo (pemKeyPair .getPrivateKeyInfo ());
98
+ } else if (obj instanceof EncryptedPrivateKeyInfo || obj instanceof PKCS8EncryptedPrivateKeyInfo ) {
99
+ logger .debug ("loadFromPkcs8 encrypted private key" );
100
+ Security .addProvider (new BouncyCastleProvider ());
101
+ PKCS8EncryptedPrivateKeyInfo encPrivKeyInfo = (PKCS8EncryptedPrivateKeyInfo ) obj ;
102
+ InputDecryptorProvider pkcs8Prov = new JcePKCSPBEInputDecryptorProviderBuilder ().setProvider ("BC" )
103
+ .build (password .toCharArray ());
104
+ return castPrivateKeyInfo (encPrivKeyInfo .decryptPrivateKeyInfo (pkcs8Prov ));
94
105
} else {
95
106
logger .error ("loadFromPkcs8: Could not load private key" );
96
107
return null ;
0 commit comments