title: Connecting to SOAP with WSS using two pem files
body: <p>A 3rd-party-app, which uses <a href="https://www.oasis-open.org/committees/wss/" rel="nofollow noreferrer">OASIS Web Services Security (WSS)</a> for their SOAP authorization, provided me with following two pem files:</p>
  
  <p><strong>ws-client.pem</strong></p>
  
  <pre><code>-----BEGIN CERTIFICATE-----
  VQQKDAxCZWFyaW5nUG9pbnQxFDASBgNVBAcMC0TDvHNzZWxkb3JmMQkwBwYDVQQI
  [..]
  CCsGAQUFBwMCBggrBgEFBQcDCDANBgkqhkiG9w0BAQsFAAOCAQEAY8MiqxLIE+dQ
  -----END CERTIFICATE-----
  -----BEGIN RSA PRIVATE KEY-----
  XKJyj28HAgMBAAECggEATF3hM/l2mIDFkJ69kskJUep/+ejl33AXVc9akY7gocLV
  [..]
  P+htLANmvC29Z5eFgkotZsAQoRi+L4gNsH1kbWHxcYA6YWRCHtpTYsa6/agUnywq
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p><strong>ws-server.pem</strong></p>
  
  <pre><code>-----BEGIN CERTIFICATE-----
  cmcxCzAJBgNVBAYTAkRFMB4XDTE3MTExNDExMjA1NloXDTI3MTExNDExMjA1Nlow
  [..]
  z6RWmsrSfLXGAM5QL4CCDzMth2AP8WzksMzW6dD6qK5/X9yD3g2GRXbHbtnvl6fk
  -----END CERTIFICATE-----
  </code></pre>
  
  <p>The last two days I tried a lot of different approaches, followed several examples.. using curl, nodejs, java etc.. with no success.</p>
  
  <p><strong>This is my latest code:</strong></p>
  
  <pre><code>import org.bouncycastle.jce.provider.BouncyCastleProvider
  import org.bouncycastle.openssl.PEMReader
  
  import java.security.*
  import java.security.cert.Certificate
  import java.security.cert.CertificateException
  import java.security.cert.X509Certificate
  
  class WsSecurityHandler {
  
      private static final char[] KSPASSWORD = null
  
      WsSecurityHandler() {
          Security.addProvider(new BouncyCastleProvider())
      }
  
      private PrivateKey getPrivateKey() throws IOException {
          InputStream pemClientInput = getClass().getClassLoader().getResourceAsStream("pem/ws-client.pem")
          Reader reader = new InputStreamReader(pemClientInput, "UTF8")
          PEMReader pemReader = new PEMReader(reader)
          PrivateKey privateKey = (PrivateKey) pemReader.readObject()
          pemReader.close()
          return privateKey
      }
  
      private X509Certificate getPublicCertificate() throws IOException {
          InputStream pemServerInput = getClass().getClassLoader().getResourceAsStream("pem/ws-server.pem")
          Reader reader = new InputStreamReader(pemServerInput, "UTF8")
          PEMReader pemReader = new PEMReader(reader)
          X509Certificate certificate = (X509Certificate) pemReader.readObject()
          pemReader.close()
          return certificate
      }
  
  
      void initKeyStore() throws IOException {
          PrivateKey privateKey = getPrivateKey()
          X509Certificate certificate = getPublicCertificate() 
          Certificate[] chain = new Certificate[1]
          chain[0] = certificate
  
          try {
              KeyStore ks = KeyStore.getInstance("JKS")
              ks.load(null, KSPASSWORD)
              ks.setKeyEntry("FooBar", privateKey, KSPASSWORD, chain)
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace()
          } catch (KeyStoreException e) {
              e.printStackTrace()
          } catch (CertificateException e) {
              e.printStackTrace()
          }
      }
  }
  </code></pre>
  
  <p>which fails at line</p>
  
  <pre><code>PrivateKey privateKey = (PrivateKey) pemReader.readObject()
  </code></pre>
  
  <p>with:</p>
  
  <blockquote>
    <p>Exception in thread "main" 
    org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot
    cast object with class
    'org.bouncycastle.jce.provider.X509CertificateObject' to class
    'java.security.PrivateKey'</p>
  </blockquote>
  
  <p>I am somehow getting the impression that I misunderstood a big part in this topic.. So it comes to this kind of embarrassing question:
  How exactly do I prepare my application to be able to connect to this WSS-SOAP-API?</p>