Showing posts with label SSL. Show all posts
Showing posts with label SSL. Show all posts

Friday, April 11, 2014

OpenSSL: How do I verify that a private key matches a certificate

To verify that a private key matches its certificate you need to compare the modulus of the certificate against the modulus of the private key.

Please follow the below command to view the modulus of the certificate.
openssl x509 -noout -modulus -in server.crt | openssl md5

Now you will receive the modulus something like a77c7953ea5283056a0c9ad75b274b96

Please follow the below command to view the modulus of the private key.
openssl rsa -noout -modulus -in myserver.key | openssl md5

Now you should get the modulus as same as certificate modulus above. i.ea77c7953ea5283056a0c9ad75b274b96

If the modulus of the certificate and the modulus of the private key do not match, then you're not using the right private key. You can either create a brand new key and CSR and send contact support or do a search for all private keys on the system and compare their modulus.

Please follow the below command to search for all private keys on your server .
find / -name *.key

Wednesday, November 13, 2013

SSL: Creating Self Signed Certificate and Java Keystore

The steps that need to be done are:
Create a self signed certificate authority (CA)
Sign a test key via the CA
Add both these keys to a keystore
Setup the application (client) and tomcat (the server) to use this keystore.

1) Create a self signed certificate authority (CA) and keystore

This is described in How to create a self signed certificate, but I will show the steps here

What is happening here:
you will create a CA that later will be added to your keystore file. By adding this CA to your keystore you are saying it is trusted like verisign and any certificates signed by it are then also trusted.


1.1) make a directory to hold the certs and keystore. This might be something like:
C:\ssl
1.2) generate a private key for the server
openssl genrsa -des3 -out server.key 1024
1.3) generate a CSR (Certificate Signing Request)
openssl req -new -key server.key -out server.csr
1.4) Remove the passphrasse from the key
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key

1.5) Generate the self signed certificate
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

2) Create a certificate for tomcat and add both to the keystore

2.1) cd to where the keystore is held. This might be something like:
C:\ssl

2.2) Create a keypair for 'tomcat'
keytool -genkey -alias tomcat -keyalg RSA -keystore tomcat.ks

2.3) Generate a CSR (Certificate Signing Request) for tomcat
keytool -keystore tomcat.ks -alias tomcat -certreq -file tomcat.csr

2.4) create unique serial number
echo 02 > serial.txt

2.5) Sign the tomcat CSR
openssl x509 -CA server.crt -CAkey server.key -CAserial serial.txt -req -in tomcat.csr -out tomcat.cer -days 365

2.6) Import the server CA certificate into the keystore
keytool -import -alias serverCA -file server.crt -keystore tomcat.ks

2.7) add the tomcat certificate to the keystore
keytool -import -alias tomcat -file tomcat.cer -keystore tomcat.ks

3) Tomcat configuration
3.1) Tomcat needs to be configured to use SSL
This is described in more detail at Tomcat SSL Configuration How-To
However all that is needed here is to edit the server.xml to enable SSL
This section is already in the server.xml but commented out.
NB that the location of the keystore has been added.

<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="C:\ssl\tomcat.ks"
keystorePass="changeit"
clientAuth="false" sslProtocol="TLS" />

3.2) test tomcat
start tomcat and go to https://localhost:8443/

your browser will return an error such as "sites certificate is not trusted"

3.3) import the CA certificate server.crt into your browser's tructed root certificates

3.4) test again at https://localhost:8443/

this time you should see the tomcat home page

4) Test your application

4.1) I have a unit test run from eclipse that I have been using to post off to my test server. This produces the error;
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:289)

4.2) The reason for this is that eclipse is not referring to the keystore that we have created. At the time of writing I have not sussed out how to make eclipse use this - Do you know ?
So instead I have added the same two certs to javas keystore

4.3) cd to the directory where java's keystore is held. This might be something like:
C:\java\jdk1.6.0_18\jre\lib\security

4.4) the keystore is a file called cacerts

4.5) copy the files c:\ssl\server.crt and c:\ssl\tomcat.cer to this directory

4.6) import the server CA into the java keystore
keytool -import -alias serverCA -file server.crt -keystore cacerts
4.7 import tomcats cert into the java keystore
keytool -import -alias tomcat -file tomcat.cer -keystore cacerts

5) Test your app again.
Hopefully all will be hunkdory. Enjoy.

6) Caveats:

6.1) the passwords for all keystores and certs are 'changeit'. this is the default keystore password
and I suggest you change this for a production system

6.2) Using a self signed cert is great for a test environment of for a private system but not for a commercial released application. For this you will need to get & pay for a signed certificate from an approved authority such as Verisign.

Sunday, October 20, 2013

SSL Offloading Configuration


Server.xml

<Connector
protocol="HTTP/1.1"
connectionTimeout="20000"
compression="on"
compressionMinSize="32"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/javascript,application/x-javascript,text/css"
redirectPort="8443"
URIEncoding="UTF-8"
proxyPort="443"
proxyName="www.invantive.com"
scheme="https"
secure="true"
/>

Apache Server

ServerAdmin sysadmin-acme@acme.com
#
# External name of the Apache web server.
#
ServerName apps.acme.com

#
# HTTPS configuration.
#
SSLEngine on
SSLProtocol -ALL +SSLv3 +TLSv1
SSLCipherSuite ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM
SSLCertificateFile /etc/apache243/conf/ssl/apps_acme_com.crt
SSLCertificateKeyFile /etc/apache243/conf/ssl/apps_acme_com.key
SSLCertificateChainFile /etc/apache243/conf/ssl/AddTrustExternalCARoot.crt
SSLCertificateChainFile /etc/apache243/conf/ssl/COMODOHigh-AssuranceSecureServerCA.crt

#
# Some useful settings.
#
ProxyRequests Off
ProxyPreserveHost On
AddDefaultCharset utf-8
HostnameLookups off
UseCanonicalName off
ProxyBadHeader Ignore
KeepAlive off

#
# Tracking who is requesting what for visitor analysis.
#
ErrorLog /var/log/ws453/apache243/apps.acme.com/error_log
CustomLog /var/log/ws453/apache243/apps.acme.com/access_log combined

#
# HTTPS requests to .../invantive-estate/production are forwarded to the
# Apache Tomcat server that runs on the server with IP address 10.1.2.3 on port 14593.
#
# And vice versa.
#
ProxyPass /invantive-estate/production http://10.1.2.3:14593/invantive-estate/production retry=3
ProxyPassReverse /invantive-estate/production qbubs http://10.1.2.3:14593/invantive-estate/production

#
# ... Space for other settings.
#

Wednesday, September 4, 2013

SSL: Create java keystore file from existing private key and certificate


Method 1:

Step one: 

Convert x509 Cert and Key to a pkcs12openssl pkcs12 -export -in server.crt -inkey server.key \ -out server.p12 -name some-alias \ -CAfile ca.crt -caname root



Step two: 

Convert the pkcs12 file to an java keystorekeytool -importkeystore \ -deststorepass changeit -destkeypass changeit -destkeystore server.keystore \ -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass some-password \ -alias some-alias


Method 2:

Suppose you have private.key and cert.crt in PEM format, that was signed by authority or you got it externally. Firstly export them into pkcs12 format:

openssl pkcs12 -export -in cert.crt -inkey private.key -certfile cert.crt -name "My certificate" -out keystore.p12

Next, use java keytool command to create keystore in JKS format (or any other that keytool supports):

keytool -importkeystore -srckeystore keystore.p12 -srcstoretype pkcs12 -destkeystore keystore.jks -deststoretype JKS

Wednesday, July 17, 2013

SSL: Useful Openssl command

Generate a Java keystore and key pair
  • keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize 2048
Generate a certificate signing request (CSR) for an existing Java keystore
  • keytool -certreq -alias mydomain -keystore keystore.jks -file mydomain.csr
Import a root or intermediate CA certificate to an existing Java keystore
  • keytool -import -trustcacerts -alias root -file Thawte.crt -keystore keystore.jks
Import a signed primary certificate to an existing Java keystore
  • keytool -import -trustcacerts -alias mydomain -file mydomain.crt -keystore keystore.jks
Generate a keystore and self-signed certificate (see How to Create a Self Signed Certificate using Java Keytool for more info)
  • keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048
Check a stand-alone certificate
  • keytool -printcert -v -file mydomain.crt
Check which certificates are in a Java keystore
  • keytool -list -v -keystore keystore.jks
Check a particular keystore entry using an alias
  • keytool -list -v -keystore keystore.jks -alias mydomain

Delete a certificate from a Java Keytool keystore
  • keytool -delete -alias mydomain -keystore keystore.jks
Change a Java keystore password
  • keytool -storepasswd -new new_storepass -keystore keystore.jks
Export a certificate from a keystore
  • keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks
List Trusted CA Certs
  • keytool -list -v -keystore $JAVA_HOME/jre/lib/security/cacerts
Import New CA into Trusted Certs
  • keytool -import -trustcacerts -file /path/to/ca/ca.pem -alias CA_ALIAS -keystore $JAVA_HOME/jre/lib/security/cacerts

SSL: SSL Configuration for multiple vhost

Apache SSL Configuration
httpd-vhost.conf

NameVirtualHost *:80
NameVirtualHost *:443


    RewriteEngine on
    RewriteRule   ^/$  /ivt/  [R]
    DocumentRoot "D:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps"
    ServerName domain_url
    DirectoryIndex index.jsp  
    JkMount  /ivt/* balancer



    RewriteEngine on
    RewriteRule   ^/$  /ivt/  [R]
    DocumentRoot "C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps"
    ServerName domain_url
    DirectoryIndex index.jsp
    SSLEngine on
    SSLCertificateFile          "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\cert\domain.crt"
    SSLCertificateKeyFile       "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\cert\domain.key"
    JkMount  /ivt/* balancer


httpd.conf
Ensure below modules and configuration files are loaded:
1. LoadModule rewrite_module modules/mod_rewrite.so
2. LoadModule    jk_module  modules/mod_jk.so
3. Include conf/extra/httpd-vhosts.conf

Monday, July 15, 2013

SSL: Create CSR

1. Download OpenSSL from http://slproweb.com/download/Win32OpenSSL_Light-1_0_1e.exe
2. Install OpenSSL execution file
3. Open command prompt
4. Navigate to Openssl bin folder
5. Set openssl config file location with command:set openssl_conf=c:\openssl\bin\openssl.cfg
6. Create CSR with command: openssl req -new -newkey rsa:2048 -nodes -out file_name.csr -keyout file_name.key -subj "/C=Country_Code/ST=Country/L=Country/O=Organisation Name/OU=Organisation_Unit/CN=URL Of Website"

You can easily create the above string from the website below:
https://www.digicert.com/easy-csr/openssl.htm

 Copy the string created and paste the string in command prompt. Press enter to generate CSR.