2727import org .junit .jupiter .params .provider .ValueSource ;
2828
2929import org .springframework .core .io .ClassPathResource ;
30+ import org .springframework .util .ObjectUtils ;
3031
3132import static org .assertj .core .api .Assertions .assertThat ;
3233import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
@@ -49,7 +50,7 @@ class PemPrivateKeyParserTests {
4950 })
5051 // @formatter:on
5152 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 ));
5354 assertThat (privateKey ).isNotNull ();
5455 assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
5556 assertThat (privateKey .getAlgorithm ()).isEqualTo (algorithm );
@@ -62,7 +63,7 @@ void shouldParseTraditionalPkcs8(String file, String algorithm) throws IOExcepti
6263 })
6364 // @formatter:on
6465 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 ));
6667 assertThat (privateKey ).isNotNull ();
6768 assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
6869 assertThat (privateKey .getAlgorithm ()).isEqualTo (algorithm );
@@ -76,11 +77,11 @@ void shouldParseTraditionalPkcs1(String file, String algorithm) throws IOExcepti
7677 // @formatter:on
7778 void shouldNotParseUnsupportedTraditionalPkcs1 (String file ) {
7879 assertThatIllegalStateException ()
79- .isThrownBy (() -> PemPrivateKeyParser . parse (read ("org/springframework/boot/web/server/pkcs1/" + file )))
80+ .isThrownBy (() -> parse (read ("org/springframework/boot/web/server/pkcs1/" + file )))
8081 .withMessageContaining ("Error loading private key file" )
8182 .withCauseInstanceOf (IllegalStateException .class )
8283 .havingCause ()
83- .withMessageContaining ("Unrecognized private key format" );
84+ .withMessageContaining ("Unsupported private key format" );
8485 }
8586
8687 @ ParameterizedTest
@@ -99,7 +100,7 @@ void shouldNotParseUnsupportedTraditionalPkcs1(String file) {
99100 })
100101 // @formatter:on
101102 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 ));
103104 assertThat (privateKey ).isNotNull ();
104105 assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
105106 assertThat (privateKey .getAlgorithm ()).isEqualTo ("EC" );
@@ -134,7 +135,7 @@ void shouldNotParseUnsupportedEcPkcs8(String file) {
134135 })
135136 // @formatter:on
136137 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 ));
138139 assertThat (privateKey ).isNotNull ();
139140 assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
140141 assertThat (privateKey .getAlgorithm ()).isEqualTo ("EdDSA" );
@@ -148,7 +149,7 @@ void shouldParseEdDsaPkcs8(String file) throws IOException {
148149 })
149150 // @formatter:on
150151 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 ));
152153 assertThat (privateKey ).isNotNull ();
153154 assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
154155 assertThat (privateKey .getAlgorithm ()).isEqualTo ("XDH" );
@@ -170,7 +171,7 @@ void shouldParseXdhPkcs8(String file) throws IOException {
170171 })
171172 // @formatter:on
172173 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 ));
174175 assertThat (privateKey ).isNotNull ();
175176 assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
176177 assertThat (privateKey .getAlgorithm ()).isEqualTo ("EC" );
@@ -198,8 +199,8 @@ void shouldNotParseUnsupportedEcSec1(String file) {
198199 }
199200
200201 @ 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 ( );
203204 }
204205
205206 @ ParameterizedTest
@@ -217,9 +218,10 @@ void shouldParseEncryptedPkcs8(String file, String algorithm) throws IOException
217218 // openssl pkcs8 -topk8 -in <input file> -out <output file> -v2 <algorithm>
218219 // -passout pass:test
219220 // 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 ];
223225 assertThat (privateKey .getFormat ()).isEqualTo ("PKCS#8" );
224226 assertThat (privateKey .getAlgorithm ()).isEqualTo (algorithm );
225227 }
@@ -248,24 +250,26 @@ void shouldNotParseEncryptedPkcs8NotUsingPbkdf2() {
248250 }
249251
250252 @ Test
251- void shouldNotParseEncryptedSec1 () {
253+ void shouldNotParseEncryptedSec1 () throws Exception {
252254 // created with:
253255 // openssl ecparam -genkey -name prime256v1 | openssl ec -aes-128-cbc -out
254256 // 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 ();
259259 }
260260
261261 @ Test
262262 void shouldNotParseEncryptedPkcs1 () throws Exception {
263263 // created with:
264264 // 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 ;
269273 }
270274
271275 private String read (String path ) throws IOException {
0 commit comments