Import signed certificate for VDI-in-a-Box - Java keystore

You’d think it would be easy to perform what should be a common task. Purchase a SSL certificate from an Issuer and install it your Citrix VDI-in-a-Box server. As anyone who has performed the same with Apache it is a fairly trivial task.
But because ViaB is some sort of black magic it is rather difficult, well that and the fact the java ‘keytool’ is fu%#*ing pain the a$$!

A lot of documentation reports that ‘The cerificate must be installed to the same keystore that was used to generate the CSR. If you try to install it to a different keystore it will not work.’
Even the stupid Citrix documentation doesn’t even tell you how to do it. What they do tell you to do is to generate the CSR on ViaB server and then get that signed! This doesn’t help the 99.99% of the world who have corporate certificates created another way!

But fear not - IT CAN BE DONE! It took me fair too long to work it out but no you can do it with ease! Please follow the directions to the letter. Please type the passwords on the command line, omitting them seems to make it not work. In my example I used DigiCert who supply four certificates in the chain of trust. If you received less that OK but make sure the order you do things in (PEM generation) is the same!

# Import all the damn certs in the chain into a PEM file:

cat www.yourwebsite.com.crt >> all-certs.pem
cat DigiCertCA.crt >> all-certs.pem 
cat DigiCertCA2.crt >> all-certs.pem
cat TrustedRoot.crt >> all-certs.pem

# Import the private key and create a PKCS12 file (that contains the full chain + private key)
openssl pkcs12 -export -in all-certs.pem -inkey www.yourwebsite.com -out all-certs.p12

# Now create a java keystore based on the PKCS12 file

keytool -importkeystore -deststorepass PASSWORD-GOES-HERE -destkeystore all-certs.jks 
-srckeystore all-certs.p12 -srcstoretype PKCS12 -srcstorepass PASSWORD-GOES-HERE

# Check the output (although you can't really tell if it's going to work until you try it...)
keytool -list -keystore all-certs.jks 

# Output should be similar to: 
Enter keystore password:                                                                                                                                     
                                                                                                                                                             
Keystore type: JKS                                                                                                                                           
Keystore provider: SUN                                                                                                                                       
                                                                                                                                                             
Your keystore contains 1 entry                                                                                                                               
                                                                                                                                                             
1, 15-Jun-2013, PrivateKeyEntry,                                                                                                                             
Certificate fingerprint (SHA1): xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx 

# Check the full certificate chain
keytool -list -v -keystore all-certs.jks 

# (DigiCert supplies four certifcates in this example)
# Check the order, it should be your www.yourwebsite.com, the Issuing CA, Issuing CA's Root, TrustedRoot Cert.

# Update the cert on the VDI server: 
ssh to vdi server as kvm
cd /home/kvm/kvm/install/servlet_container/conf
Backup the default keystore file:
mv .keystore old.keystore
Copy (SCP?) the new keystore file to the conf directory:
cp /home/kvm/kvm/install/servlet_container/conf/all-certs.p12 .keystore
Verify that the .keystore and old.keystore files exist:
ls –al

# Update the SSL Password in the configuration file:

Edit the server.xml file using the vi editor:
Find the clientAuth line by searching: /clientAuth=
Verify the keystorePass=”password” entry does not already exist in entire Define a SSL HTTP/1.1 Connector
 on port 8443 section. Add the following line, replacing “password” with your keystore password:
If keystorePass=”changeit” already exists in the section, simply replace the “changeit” with your
 keystore password.

# Note: Having two keystorePassword lines in the server.xml file may cause tomcat to fail when starting. 
Ensure there is only one instance of the keystorePassword.
Save and exit 

# Restart Tomcat Services
(as the kvm user)
tc_stop && tc_start