I have a requirement where I need to run a Java-based HTTP server on SSL and connect to that from browsers. I also need to make sure that browsers don't show the security exception for self-signed certificate.
I did the following -
- Generated a JKS keystore using Java keytool -keygen.
- Imported that keystore as a PKCS12 p12 file, using keytool -importkeystore.
Loaded the p12 file in to a X509Certificate2 object and added that to Root and CertificateAuthority
X509Store store5 = new X509Store(StoreName.Root, StoreLocation.LocalMachine); store5.Open(OpenFlags.ReadWrite); store5.Add(cert); store5.Close(); X509Store store2 = new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine); store2.Open(OpenFlags.ReadWrite); store2.Add(cert); store2.Close();
Exported a certificate file from JKS keystore.
- Added that .cer file to cacerts of Java.
Now when I run the HTTP server, it picks the certificate and serves HTTPS requests, but the browser still shows the site as untrusted.