code: public static ImmutablePair<PrivateKey, X509Certificate> extractCertificateWithKey(KeyStore keyStore, char [] keyStorePassword) {
          Assert.notNull(keyStore, "KeyStore is mandatory");
          Assert.notNull(keyStorePassword, "Password for key store is mandatory");
          try {
              Enumeration<String> aliases = keyStore.aliases();
              while (aliases.hasMoreElements()) {
                  String aliasName = aliases.nextElement();
  
                  Key key = keyStore.getKey(aliasName, keyStorePassword);
                  if (key instanceof PrivateKey) {
                      PrivateKey privateKey = (PrivateKey) key;
                      Object cert = keyStore.getCertificate(aliasName);
                      if (cert instanceof X509Certificate) {
                          X509Certificate certificate = (X509Certificate) cert;
                          return ImmutablePair.of(privateKey, certificate);
                      }
                  }
              }
              throw new IllegalStateException("No valid key-certificate pair in the key store");
          } catch (KeyStoreException | UnrecoverableKeyException | NoSuchAlgorithmException ex) {
              throw new IllegalStateException("Failed to extract a valid key-certificate pair from key store", ex);
          }
      }
docs: Extract a pair of key and certificate from an already loaded {@link KeyStore}.
  
  @param keyStore {@link KeyStore} instance.
  @param keyStorePassword password to access the key store.
  @return pair of valid {@link PrivateKey} and {@link X509Certificate} loaded from {@code keyStore}
  @throws IllegalStateException
  If {@link X509Certificate} loading failed.