3535import java .io .IOException ;
3636import java .net .URISyntaxException ;
3737import java .nio .charset .StandardCharsets ;
38+ import java .security .KeyPair ;
3839import java .security .PublicKey ;
3940import java .security .Security ;
4041import java .security .cert .Certificate ;
4142import java .util .Arrays ;
43+ import java .util .List ;
4244import jenkins .bouncycastle .api .PEMEncodable ;
4345import org .apache .commons .io .FileUtils ;
4446import org .bouncycastle .jce .provider .BouncyCastleProvider ;
@@ -73,6 +75,9 @@ public static void cleanupProvider() {
7375 private static File CERTIFICATE_PUBLIC_KEY_PEM ;
7476 private static File CERTIFICATE_PW_PEM ;
7577 private static File CERTIFICATE_PUBLIC_KEY_PW_PEM ;
78+ private static File CERTIFICATE_AND_PRIVATE_KEY_PEM ;
79+ private static File CERTIFICATE_AND_PRIVATE_KEY_PW_PEM ;
80+
7681 private static File PRIVATE_KEY_PW_PKCS8 ;
7782
7883 private static final String PRIVATE_KEY_PW = "test" ;
@@ -90,6 +95,8 @@ public static void setUpClass() throws URISyntaxException {
9095 CERTIFICATE_PUBLIC_KEY_PEM = getResourceFile ("test_cert_key.pem" );
9196 CERTIFICATE_PW_PEM = getResourceFile ("test_cert_cert_pass.pem" );
9297 CERTIFICATE_PUBLIC_KEY_PW_PEM = getResourceFile ("test_cert_key_pass.pem" );
98+ CERTIFICATE_AND_PRIVATE_KEY_PEM = getResourceFile ("test_cert_and_key.pem" );
99+ CERTIFICATE_AND_PRIVATE_KEY_PW_PEM = getResourceFile ("test_cert_and_key_pass.pem" );
93100 }
94101
95102 private static File getResourceFile (String resource ) throws URISyntaxException {
@@ -128,7 +135,7 @@ public void testReadPrivateKeyWithPasswordPEM() throws Exception {
128135 @ Test
129136 @ Issue (value = "JENKINS-66394" )
130137 public void testReadPrivateKeyWithPasswordPKCS8 () throws Exception {
131- PEMEncodable pemEnc = PEMEncodable .read (PRIVATE_KEY_PW_PKCS8 , "test" .toCharArray ());
138+ PEMEncodable pemEnc = PEMEncodable .read (PRIVATE_KEY_PW_PKCS8 , PRIVATE_KEY_PW .toCharArray ());
132139
133140 assertEquals (
134141 new String (Base64 .encode (pemEnc .toKeyPair ().getPrivate ().getEncoded ()), StandardCharsets .UTF_8 ),
@@ -186,11 +193,7 @@ public void testReadCertificatePEM() throws Exception {
186193
187194 Certificate certificate = pemEncCer .toCertificate ();
188195 PublicKey publicKey = pemEncKey .toPublicKey ();
189- assertNotNull (certificate );
190- assertNotNull (publicKey );
191- assertEquals (
192- new String (Base64 .encode (certificate .getPublicKey ().getEncoded ()), StandardCharsets .UTF_8 ),
193- new String (Base64 .encode (publicKey .getEncoded ()), StandardCharsets .UTF_8 ));
196+ assertCertificatePublicKeyMatches (certificate , publicKey );
194197 }
195198
196199 @ Test
@@ -200,11 +203,7 @@ public void testReadCertificateWithPasswordPEM() throws Exception {
200203
201204 Certificate certificate = pemEncCer .toCertificate ();
202205 PublicKey publicKey = pemEncKey .toPublicKey ();
203- assertNotNull (certificate );
204- assertNotNull (publicKey );
205- assertEquals (
206- new String (Base64 .encode (certificate .getPublicKey ().getEncoded ()), StandardCharsets .UTF_8 ),
207- new String (Base64 .encode (publicKey .getEncoded ()), StandardCharsets .UTF_8 ));
206+ assertCertificatePublicKeyMatches (certificate , publicKey );
208207 }
209208
210209 @ Test
@@ -271,4 +270,37 @@ public void testReadKeyPairFromPCKS8PEM() throws Exception {
271270 public void testInvalidPEM () throws Exception {
272271 PEMEncodable .decode (FileUtils .readFileToString (getResourceFile ("invalid.pem" ), StandardCharsets .UTF_8 ));
273272 }
273+
274+ @ Test
275+ public void testReadingCertAndKeyPEM () throws Exception {
276+ List <PEMEncodable > pems = PEMEncodable .readAll (CERTIFICATE_AND_PRIVATE_KEY_PEM );
277+ assertThat (pems ).hasSize (2 );
278+ assertCertPublicKeyMatches (pems .get (0 ).toCertificate (), pems .get (1 ).toKeyPair ());
279+ }
280+
281+ @ Test
282+ public void testReadingCertAndKeyPassPEM () throws Exception {
283+ List <PEMEncodable > pems =
284+ PEMEncodable .readAll (CERTIFICATE_AND_PRIVATE_KEY_PW_PEM , PRIVATE_KEY_PW .toCharArray ());
285+ assertThat (pems ).hasSize (2 );
286+ assertCertPublicKeyMatches (pems .get (0 ).toCertificate (), pems .get (1 ).toKeyPair ());
287+ }
288+
289+ /**
290+ * asserts that the given certificates public key corresponds to the provided KeyPair.
291+ */
292+ private static void assertCertPublicKeyMatches (Certificate cert , KeyPair kp ) {
293+ assertCertificatePublicKeyMatches (cert , kp != null ? kp .getPublic () : null );
294+ }
295+
296+ /**
297+ * asserts that the given certificates public key corresponds to the provided KeyPair.
298+ */
299+ private static void assertCertificatePublicKeyMatches (Certificate cert , PublicKey key ) {
300+ assertNotNull (cert );
301+ assertNotNull (key );
302+ assertEquals (
303+ new String (Base64 .encode (cert .getPublicKey ().getEncoded ()), StandardCharsets .UTF_8 ),
304+ new String (Base64 .encode (key .getEncoded ()), StandardCharsets .UTF_8 ));
305+ }
274306}
0 commit comments