27
27
import org .junit .jupiter .params .provider .ValueSource ;
28
28
29
29
import org .springframework .core .io .ClassPathResource ;
30
+ import org .springframework .util .ObjectUtils ;
30
31
31
32
import static org .assertj .core .api .Assertions .assertThat ;
32
33
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
@@ -49,7 +50,7 @@ class PemPrivateKeyParserTests {
49
50
})
50
51
// @formatter:on
51
52
void shouldParseTraditionalPkcs8 (String file , String algorithm ) throws IOException {
52
- PrivateKey privateKey = PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
53
+ PrivateKey privateKey = parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
53
54
assertThat (privateKey ).isNotNull ();
54
55
assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
55
56
assertThat (privateKey .getAlgorithm ()).isEqualTo (algorithm );
@@ -62,7 +63,7 @@ void shouldParseTraditionalPkcs8(String file, String algorithm) throws IOExcepti
62
63
})
63
64
// @formatter:on
64
65
void shouldParseTraditionalPkcs1 (String file , String algorithm ) throws IOException {
65
- PrivateKey privateKey = PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/pkcs1/" + file ));
66
+ PrivateKey privateKey = parse (read ("org/springframework/boot/web/server/pkcs1/" + file ));
66
67
assertThat (privateKey ).isNotNull ();
67
68
assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
68
69
assertThat (privateKey .getAlgorithm ()).isEqualTo (algorithm );
@@ -76,11 +77,11 @@ void shouldParseTraditionalPkcs1(String file, String algorithm) throws IOExcepti
76
77
// @formatter:on
77
78
void shouldNotParseUnsupportedTraditionalPkcs1 (String file ) {
78
79
assertThatIllegalStateException ()
79
- .isThrownBy (() -> PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/pkcs1/" + file )))
80
+ .isThrownBy (() -> parse (read ("org/springframework/boot/web/server/pkcs1/" + file )))
80
81
.withMessageContaining ("Error loading private key file" )
81
82
.withCauseInstanceOf (IllegalStateException .class )
82
83
.havingCause ()
83
- .withMessageContaining ("Unrecognized private key format" );
84
+ .withMessageContaining ("Unsupported private key format" );
84
85
}
85
86
86
87
@ ParameterizedTest
@@ -99,7 +100,7 @@ void shouldNotParseUnsupportedTraditionalPkcs1(String file) {
99
100
})
100
101
// @formatter:on
101
102
void shouldParseEcPkcs8 (String file , String curveName , String oid ) throws IOException {
102
- PrivateKey privateKey = PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
103
+ PrivateKey privateKey = parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
103
104
assertThat (privateKey ).isNotNull ();
104
105
assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
105
106
assertThat (privateKey .getAlgorithm ()).isEqualTo ("EC" );
@@ -134,7 +135,7 @@ void shouldNotParseUnsupportedEcPkcs8(String file) {
134
135
})
135
136
// @formatter:on
136
137
void shouldParseEdDsaPkcs8 (String file ) throws IOException {
137
- PrivateKey privateKey = PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
138
+ PrivateKey privateKey = parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
138
139
assertThat (privateKey ).isNotNull ();
139
140
assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
140
141
assertThat (privateKey .getAlgorithm ()).isEqualTo ("EdDSA" );
@@ -148,7 +149,7 @@ void shouldParseEdDsaPkcs8(String file) throws IOException {
148
149
})
149
150
// @formatter:on
150
151
void shouldParseXdhPkcs8 (String file ) throws IOException {
151
- PrivateKey privateKey = PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
152
+ PrivateKey privateKey = parse (read ("org/springframework/boot/web/server/pkcs8/" + file ));
152
153
assertThat (privateKey ).isNotNull ();
153
154
assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
154
155
assertThat (privateKey .getAlgorithm ()).isEqualTo ("XDH" );
@@ -170,7 +171,7 @@ void shouldParseXdhPkcs8(String file) throws IOException {
170
171
})
171
172
// @formatter:on
172
173
void shouldParseEcSec1 (String file , String curveName , String oid ) throws IOException {
173
- PrivateKey privateKey = PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/sec1/" + file ));
174
+ PrivateKey privateKey = parse (read ("org/springframework/boot/web/server/sec1/" + file ));
174
175
assertThat (privateKey ).isNotNull ();
175
176
assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
176
177
assertThat (privateKey .getAlgorithm ()).isEqualTo ("EC" );
@@ -198,8 +199,8 @@ void shouldNotParseUnsupportedEcSec1(String file) {
198
199
}
199
200
200
201
@ Test
201
- void parseWithNonKeyTextWillThrowException () {
202
- assertThatIllegalStateException (). isThrownBy (() -> PemPrivateKeyParser .parse (read ("test-banner.txt" )));
202
+ void parseWithNonKeyTextWillReturnEmptyArray () throws Exception {
203
+ assertThat ( PemPrivateKeyParser .parse (read ("test-banner.txt" ))). isEmpty ( );
203
204
}
204
205
205
206
@ ParameterizedTest
@@ -217,9 +218,10 @@ void shouldParseEncryptedPkcs8(String file, String algorithm) throws IOException
217
218
// openssl pkcs8 -topk8 -in <input file> -out <output file> -v2 <algorithm>
218
219
// -passout pass:test
219
220
// where <algorithm> is aes128 or aes256
220
- PrivateKey privateKey = PemPrivateKeyParser .parse (read ("org/springframework/boot/web/server/pkcs8/" + file ),
221
- "test" );
222
- assertThat (privateKey ).isNotNull ();
221
+ String content = read ("org/springframework/boot/web/server/pkcs8/" + file );
222
+ PrivateKey [] privateKeys = PemPrivateKeyParser .parse (content , "test" );
223
+ assertThat (privateKeys ).isNotEmpty ();
224
+ PrivateKey privateKey = privateKeys [0 ];
223
225
assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
224
226
assertThat (privateKey .getAlgorithm ()).isEqualTo (algorithm );
225
227
}
@@ -248,24 +250,26 @@ void shouldNotParseEncryptedPkcs8NotUsingPbkdf2() {
248
250
}
249
251
250
252
@ Test
251
- void shouldNotParseEncryptedSec1 () {
253
+ void shouldNotParseEncryptedSec1 () throws Exception {
252
254
// created with:
253
255
// openssl ecparam -genkey -name prime256v1 | openssl ec -aes-128-cbc -out
254
256
// prime256v1-aes-128-cbc.key
255
- assertThatIllegalStateException ()
256
- .isThrownBy (() -> PemPrivateKeyParser
257
- .parse (read ("org/springframework/boot/web/server/sec1/prime256v1-aes-128-cbc.key" ), "test" ))
258
- .withMessageContaining ("Unrecognized private key format" );
257
+ assertThat (PemPrivateKeyParser
258
+ .parse (read ("org/springframework/boot/web/server/sec1/prime256v1-aes-128-cbc.key" ), "test" )).isEmpty ();
259
259
}
260
260
261
261
@ Test
262
262
void shouldNotParseEncryptedPkcs1 () throws Exception {
263
263
// created with:
264
264
// openssl genrsa -aes-256-cbc -out rsa-aes-256-cbc.key
265
- assertThatIllegalStateException ()
266
- .isThrownBy (() -> PemPrivateKeyParser
267
- .parse (read ("org/springframework/boot/web/server/pkcs1/rsa-aes-256-cbc.key" ), "test" ))
268
- .withMessageContaining ("Unrecognized private key format" );
265
+ assertThat (PemPrivateKeyParser .parse (read ("org/springframework/boot/web/server/pkcs1/rsa-aes-256-cbc.key" ),
266
+ "test" ))
267
+ .isEmpty ();
268
+ }
269
+
270
+ private PrivateKey parse (String key ) {
271
+ PrivateKey [] keys = PemPrivateKeyParser .parse (key );
272
+ return (!ObjectUtils .isEmpty (keys )) ? keys [0 ] : null ;
269
273
}
270
274
271
275
private String read (String path ) throws IOException {
0 commit comments