21
21
import java .nio .ByteBuffer ;
22
22
import java .nio .file .Files ;
23
23
import java .nio .file .Path ;
24
- import java .security .GeneralSecurityException ;
25
24
import java .security .KeyFactory ;
25
+ import java .security .NoSuchAlgorithmException ;
26
26
import java .security .PrivateKey ;
27
27
import java .security .spec .InvalidKeySpecException ;
28
28
import java .security .spec .PKCS8EncodedKeySpec ;
47
47
*/
48
48
final class PrivateKeyParser {
49
49
50
- private static final String PKCS1_HEADER = "-+BEGIN\\ s+RSA\\ s+PRIVATE\\ s+KEY[^-]*-+(?:\\ s|\\ r|\\ n)+" ;
50
+ private static final String PKCS1_RSA_HEADER = "-+BEGIN\\ s+RSA\\ s+PRIVATE\\ s+KEY[^-]*-+(?:\\ s|\\ r|\\ n)+" ;
51
51
52
- private static final String PKCS1_FOOTER = "-+END\\ s+RSA\\ s+PRIVATE\\ s+KEY[^-]*-+" ;
52
+ private static final String PKCS1_RSA_FOOTER = "-+END\\ s+RSA\\ s+PRIVATE\\ s+KEY[^-]*-+" ;
53
53
54
54
private static final String PKCS8_HEADER = "-+BEGIN\\ s+PRIVATE\\ s+KEY[^-]*-+(?:\\ s|\\ r|\\ n)+" ;
55
55
56
56
private static final String PKCS8_FOOTER = "-+END\\ s+PRIVATE\\ s+KEY[^-]*-+" ;
57
57
58
- private static final String EC_HEADER = "-+BEGIN\\ s+EC\\ s+PRIVATE\\ s+KEY[^-]*-+(?:\\ s|\\ r|\\ n)+" ;
58
+ private static final String SEC1_EC_HEADER = "-+BEGIN\\ s+EC\\ s+PRIVATE\\ s+KEY[^-]*-+(?:\\ s|\\ r|\\ n)+" ;
59
59
60
- private static final String EC_FOOTER = "-+END\\ s+EC\\ s+PRIVATE\\ s+KEY[^-]*-+" ;
60
+ private static final String SEC1_EC_FOOTER = "-+END\\ s+EC\\ s+PRIVATE\\ s+KEY[^-]*-+" ;
61
61
62
62
private static final String BASE64_TEXT = "([a-z0-9+/=\\ r\\ n]+)" ;
63
63
64
64
private static final List <PemParser > PEM_PARSERS ;
65
65
static {
66
66
List <PemParser > parsers = new ArrayList <>();
67
- parsers .add (new PemParser (PKCS1_HEADER , PKCS1_FOOTER , PrivateKeyParser ::createKeySpecForPkcs1 , "RSA" ));
68
- parsers .add (new PemParser (EC_HEADER , EC_FOOTER , PrivateKeyParser ::createKeySpecForEc , "EC" ));
69
- parsers .add (new PemParser (PKCS8_HEADER , PKCS8_FOOTER , PKCS8EncodedKeySpec ::new , "RSA" , "EC" , "DSA" , "Ed25519" ));
67
+ parsers
68
+ .add (new PemParser (PKCS1_RSA_HEADER , PKCS1_RSA_FOOTER , PrivateKeyParser ::createKeySpecForPkcs1Rsa , "RSA" ));
69
+ parsers .add (new PemParser (SEC1_EC_HEADER , SEC1_EC_FOOTER , PrivateKeyParser ::createKeySpecForSec1Ec , "EC" ));
70
+ parsers .add (new PemParser (PKCS8_HEADER , PKCS8_FOOTER , PKCS8EncodedKeySpec ::new , "RSA" , "RSASSA-PSS" , "EC" ,
71
+ "DSA" , "EdDSA" , "XDH" ));
70
72
PEM_PARSERS = Collections .unmodifiableList (parsers );
71
73
}
72
74
@@ -88,11 +90,11 @@ final class PrivateKeyParser {
88
90
private PrivateKeyParser () {
89
91
}
90
92
91
- private static PKCS8EncodedKeySpec createKeySpecForPkcs1 (byte [] bytes ) {
93
+ private static PKCS8EncodedKeySpec createKeySpecForPkcs1Rsa (byte [] bytes ) {
92
94
return createKeySpecForAlgorithm (bytes , RSA_ALGORITHM , null );
93
95
}
94
96
95
- private static PKCS8EncodedKeySpec createKeySpecForEc (byte [] bytes ) {
97
+ private static PKCS8EncodedKeySpec createKeySpecForSec1Ec (byte [] bytes ) {
96
98
DerElement ecPrivateKey = DerElement .of (bytes );
97
99
Assert .state (ecPrivateKey .isType (ValueType .ENCODED , TagType .SEQUENCE ),
98
100
"Key spec should be an ASN.1 encoded sequence" );
@@ -194,21 +196,16 @@ private static byte[] decodeBase64(String content) {
194
196
}
195
197
196
198
private PrivateKey parse (byte [] bytes ) {
197
- try {
198
- PKCS8EncodedKeySpec keySpec = this .keySpecFactory . apply ( bytes );
199
- for ( String algorithm : this . algorithms ) {
199
+ PKCS8EncodedKeySpec keySpec = this . keySpecFactory . apply ( bytes );
200
+ for ( String algorithm : this .algorithms ) {
201
+ try {
200
202
KeyFactory keyFactory = KeyFactory .getInstance (algorithm );
201
- try {
202
- return keyFactory .generatePrivate (keySpec );
203
- }
204
- catch (InvalidKeySpecException ex ) {
205
- }
203
+ return keyFactory .generatePrivate (keySpec );
204
+ }
205
+ catch (InvalidKeySpecException | NoSuchAlgorithmException ex ) {
206
206
}
207
- return null ;
208
- }
209
- catch (GeneralSecurityException ex ) {
210
- throw new IllegalArgumentException ("Unexpected key format" , ex );
211
207
}
208
+ return null ;
212
209
}
213
210
214
211
}
@@ -296,7 +293,7 @@ static final class DerElement {
296
293
297
294
private final long tagType ;
298
295
299
- private ByteBuffer contents ;
296
+ private final ByteBuffer contents ;
300
297
301
298
private DerElement (ByteBuffer bytes ) {
302
299
byte b = bytes .get ();
0 commit comments