How to use the certificate for Tomcat


#19

Hi everybody!
Thanks a lot for all yours posts.
I’m new here and i’m tryin’ to figure out how to create keystore from existing certificate chain.
I already have private key and certificate for root, intermediate and server.
-ca.key.pem
-ca.cert.pem
-intermediate.key.pem
-intermediate.cert.pem
-www.mydomain.cert.pem
-www.mydomain.key.pem
I have also a file named ca-chain.cert.pem which contain ca and intermediate certificates and maintain their chain.
So i am wondering what your “full chain” contain and how i can create my keystore with the certificate chain, and then activate rtmps and https on my red5 server
thanks


#20

You’ve got the private key for the root and intermediate certificates? Nice! :stuck_out_tongue:

fullchain.pem just contains cert.pem and chain.pem, your servers and intermediate certificate respectively.


#21

ok
So if im good, to get fullchain.pem all i have to do is :
cat ca.cert.pem intermediate.cert.pem www.mydomain.cert.pem > fullchain.cert
Right?


#22

…And for chain.pem:
cat www.mydomain.cert.pem intermediate.cert.pem > chain.pem
?


#23

We’re talking about Let’s Encrypt here, right? Because the official client already provides you with fullchain.pem.

If not, then you’re a) not giving us enough information to work with and b) are you at the right place to ask these questions? :stuck_out_tongue: This is the Let’s Encrypt Community :wink: Questions about random TLS/certificate questions which do not have anything to do with Let’s Encrypt are probably best asked somewhere else as far as I’m concerned. You’d probably have more luck, because I’m not really understanding your problem to be honest… :slight_smile:


#24

Hi thanks much @melo for this solution. LetsEncrypt works great now in my environment for Chrome browsers. The problem is that it doesn’t work for Firefox. I’m looking at @eswn’s solution for the FF problem in Apache here: Cert not work in firefox. But importing the chain file he refers to to my java keystore does not resolve the firefox problem. How do I get LetsEncrypt working correctly for tomcat and Firefox?


Which browsers and operating systems support Let's Encrypt
#25

I’m not really familiar with tomcat, but generally when you run into issues like this where things work in one browser (or on one device) but not in another, it’s often because the correct intermediate certificate is missing. Browsers cache those intermediate certificates, so if (for example) you visited some other site using a recent Let’s Encrypt certificate in Chrome but not in Firefox, that would make Chrome work but Firefox fail. Basically, don’t think about it as a browser issue, but rather something that’s missing from your server configuration. I would recommend using SSL Labs to verify you’re serving the correct intermediate certificate.

If you find out you’re serving the wrong intermediate certificate and need to get the right one, you can find both the old one and the current one (X3) here. You’ll want to pass that to openssl as -CAfile chain.pem when creating your PKCS12 file.


#26

Hi @vlott I am glad my post is helping. However I am afraid you might have got it a little wrong. @pfg summarized it perfectly.

There is no issue with the browser itself. We must ensure you are serving the entire chain required to accept your certificate as trusted. SSL Labs is a good tool to check whether you serve the entire certificate chain or if something is missing.
In case you serve on a port other than 443, you can use SSL Checker and provide your domain and custom port.
Run the test on your site. It will list the server certificate and any additional certificates, if provided. You must also provide the intermediate certificate for the certification path to be complete

To achieve this, as stated in my post, it is important that you use the fullchain.pem file that was generated together with your certificate like so:
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out fullchain_and_key.p12 -name tomcat

I later use keytool to convert this to JKS and setup Tomcat’s connector accordingly, but I was told that Tomcat supports PKCS12 keystores, so the keytool step might be unnecessary.

Hope this helps.


#27

I had a problem where when I convert the cert generated into JKS the DST Root X3 cert in the chain is missing.

For some weird reason, ended up having to create the cert chain again this way…

openssl pkcs12 -in certificate-all.pfx -out clientcert.pem -nodes -clcerts
cat clientcert.pem lets-encrypt-x3-cross-signed.pem dst-root-ca-x3.pem >> clientcertchain.pem
openssl pkcs12 -export -in clientcertchain.pem -out clientcertchain.pfx
keytool -importkeystore -srckeystore clientcertchain.pfx -srcstoretype pkcs12 -destkeystore clientcertchain.jks -deststoretype JKS

#28

Hi @sintroo I believe that it is perfectly fine to NOT provide the DST Root X3 certificate. This is a certificate from IdenTrust, a widely accepted and trusted certificate authority. This means that their certificate is included in several certificate authority trust stores, e.g. the ones of operating systems (used by browsers) or the Java CA trust store.

For a valid certification path it is fine to NOT provide the root CA certificate.
Your generated cert says it is signed by LE, so you provide the LE cert for it to be available for validation. The LE cert is signed by DST, so for validation DST will be looked up in the available root CA trust store, were it will be found. This will give you a valid path of your-cert -> LE -> DST and you only need to provide your cert and every other cert required to build a path to a root cert, which in our case this is just the one LE cert.

Hope this helps.


#29

Thanks for the info @melo. Then could it be because the DST Root X3 certificate is not included in the default java cacerts?

Others having the same problem discussed here: https://github.com/ebekker/ACMESharp/issues/119


#30

@sintroo I guess it depends on the used java version. Java comes with keytool. You can use it to list the entries of a keystore. To list the trusted CA certs you could try running this on your server:
keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts

But I am not sure what your problem is exactly.
The DST Root X3 cert missing from the java trusted CA store would only be relevant when accessing your server with a java client and trying to validate the provided certificate.
There is nothing you need to to on your server for the DST Root X3 cert.


#31

Hello, melo.

Thank you for your solution.
Also I’d like to tell that it can be used pkcs12 keystore without jks converting.

Server.xml should look like this.

`< Connector
port="443"
protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="200"
SSLEnabled="true"
scheme="https"
secure="true"
clientAuth="false"
sslProtocol=“TLS"
keystoreFile=”[path to p12-file]“
keystoreType=“PKCS12"
keystorePass=”[selected pkcs12 keystore password]” />

`


#32

Hi @vaysman, thank you for your feedback!

This confirms my suspicion that the convert to JKS step is unnecessary.
One step less means slightly reduced chance to make mistakes =)
Thank you for sharing the connector configuration.

For me this means that in future I can just use the PKCS12 file and only need to change the keystoreFile value accordingly and add the keystoreType attribute with value PKCS12 to my connector.


#33

Thanks @melo, I saw your updated command further down in the other thread. This worked great for me and I am up and running in SpringBoot. If anyone is interested in what I did to get working with the embedded tomcat in SpringBoot, just reply and I’ll post.


#34

Thanks @melo, I struggled for so many hours and thanks to your guide I now have a working tomcat server on a raspberry pi with free ssl certificates from let’s encrypt and a free subdomain from selfhost.eu :slight_smile:


#35

I have a new preferred solution for HTTPS to Tomcat with Let’s Encrypt certificate. I do not configure Tomcat for HTTPS in the first place!
In case your setup consists of both a web server and Tomcat, consider setting up a reverse proxy on your web server and let it handle HTTPS for Tomcat. This way you can benefit from Let’s Encrypts automatic certifcate renewal capabilities that update the certificates your web server uses and you are still able to securely access Tomcat through HTTPS without manual keystore creation or anything like that.
I use a Synology DiskStation, which added LE support and a reverse proxy feature and I now have a fully automatic, one time setup solution I really like =)

In case anyone is interested in this specific setup, I described it in a blog post:


#36

Hi @vlott, I am running a SpringBoot application with embedded Tomcat and am trying to get it to work with Let’s Encrypt, and searching through the forum came across your comment here. Did you post the steps you used? It would be super super helpful to us. I’ve installed certbot, and generated fullchain.pem & the PKCS12 file, as in some comments above, but am getting errors on the conversion to JKS (keytool gives me a BadPaddingException).

Did you take the route of just using the PKCS12 file with Spring-Boot/Tomcat and if so how did it go? Or did you use the reverse proxy route?


#37

Here are the steps I used to generate certificates and get them working for all browsers in Spring Boot:

  • Make sure firewall will allow the letsencrypt client to receive a callback on port 80 of the server at the domain(s) being configured in the certificate.
  • Run the letsencrypt client:
    sudo /home/ec2-user/letsencrypt/letsencrypt-auto certonly --standalone --standalone-supported-challenges http-01
  • When prompted enter the domains that the letsencrypt server will call back to verify as per above
  • You should have a successful run and you should see the certificates in the /etc/letsencrypt/live directory
  • Create PKCS12 keystore (to be imported to jks later):
    sudo openssl pkcs12 -export -in /etc/letsencrypt/archive/yourDomain.com/fullchainx.pem -inkey /etc/letsencrypt/archive/yourDomain.com/privkeyx.pem -out yourDomain-cert_and_key.p12 -name tomcat (where fullchainx is fullchain1.pem, fullchain2.pem, etc., from the new certificate. This will be incremented every time new certs are generated. IMPORTANT! Use the fullchainx.pem instead of the certx or chainx or it won’t work in Firefox)
  • Convert PKCS12 Keystore to Java Keystore:
    /usr/java/jre1.7.0_65/bin/keytool -importkeystore -deststorepass yourKeyPassword -destkeypass yourPassword -destkeystore yourDomain-keystore.jks -srckeystore yourDomain-cert_and_key.p12 -srcstoretype PKCS12 -srcstorepass yourPassword -alias tomcat
  • Check Java Keystore:
    keytool -list -keystore yourDomain-keystore.jks
  • Copy keystore file to springBootJarLocation/ssl/yourDomain-keystore.jks on prod server

Spring Boot configuration

  • Add entries to prod environment and configure SpringBoot to read keystore off disk (application-prod.properties) (and autowire into Configuration-annotated class)

custom.server.port=8443
custom.server.ssl.key-store=file:/opt//ssl/yourDomain-keystore.jks
custom.server.ssl.key-store-password=yourPassword
custom.server.ssl.key-password=yourPassword

  • Add entries as needed to local environment for testing also

  • Add a bean for a custom EmbeddedServletContainerFactory that configures all requests to be encrypted and redirected to SSL. Also, this bean will add a second connector for port 443/SSL:

    @Bean

  public EmbeddedServletContainerFactory servletContainer() {
      TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
          \@Override
          protected void postProcessContext(Context context) {
              SecurityConstraint securityConstraint = new SecurityConstraint();
              securityConstraint.setUserConstraint("CONFIDENTIAL");
              SecurityCollection collection = new SecurityCollection();
              collection.addPattern("/*");
              securityConstraint.addCollection(collection);
              context.addConstraint(securityConstraint);
          }

      };
      tomcat.addAdditionalTomcatConnectors(createSslConnector());
      return tomcat;
  }

  private Connector createSslConnector() {
      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
      Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
      try {
          File keystore = new UrlResource(serverSslKeystore).getFile();
          File truststore = new UrlResource(serverSslKeystore).getFile();
          connector.setScheme("https");
          connector.setSecure(true);
          connector.setPort(serverPort);
          protocol.setSSLEnabled(true);
          protocol.setKeystoreFile(keystore.getAbsolutePath());
          protocol.setKeystorePass(serverSslKeystorePassword);
          protocol.setKeyPass(serverSslKeyPassword);
          protocol.setTruststoreFile(truststore.getAbsolutePath());
          protocol.setTruststorePass(serverSslKeystorePassword);
          protocol.setKeyAlias("tomcat");
          return connector;
      } catch (IOException ex) {
          throw new IllegalStateException("can't access keystore: [" + "keystore"
                  + "] or truststore: [" + "keystore" + "]", ex);
      }
  }

#38

@vlott: Thank you! Just followed those and have the cert installed and all working very nicely. Thanks a lot for the quick reply & clear instructions.