mTLS HTTP Connection

This demostrates how to make a mutual TLS (2 Way SSL) http connection using the popular library Bouncy Castle in Java Maven Project.

Prerequisite

Install the following maven dependencies

//pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...

  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcprov-jdk15on</artifactId>
      <version>1.70</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on -->
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcpkix-jdk15on</artifactId>
      <version>1.70</version>
    </dependency>
  </dependencies>
  ...

Example With EC Private Key

In this example, we are using ca cert, public cert, and elliptic curve key-pair (private key)

The ca cert and public cert should in below format

The elliptic curve key-pair (private key) should in below format

Example

Example With PKCS#8 Private Key

The ca cert and public cert should in below format

The PKCS#8 unencrypted private key should in below format

Example

Pem Utilities

OpenSSL Commands

Check a certificate

Convert elliptic curve key-pair (private key) to PKCS#8 pem format

Others

If you having the ".jks" file, it would be more straigh forward to generate the KeyManagers and TrustManager compare to using PEM files in this example.

Reference

Last updated

Was this helpful?