SSL Endpoint

Last Updated: 04 April 2014

https ssl

Table of Contents

SSL is a cryptographic protocol that provides end-to-end encryption and integrity for all web requests. Apps that transmit sensitive data should enable SSL to ensure all information is transmitted securely.

To enable SSL on a custom domain, e.g., www.example.com, use the SSL Endpoint add-on. (SSL Endpoint is a paid add-on service. Please keep this in mind when provisioning the service).

SSL Endpoint is only useful for custom domains. All default appname.herokuapp.com domains are SSL-enabled already and can be accessed simply by using https, e.g., https://appname.herokuapp.com.

Overview

Because of the unique nature of SSL validation, provisioning SSL for your application is a multi-step process that involves several third-parties. You will need to:

  1. Purchase an SSL certificate from your SSL provider
  2. Provision an SSL Endpoint from Heroku
  3. Upload the cert to Heroku
  4. Update your DNS settings to reference the new SSL Endpoint URL

Acquire SSL certificate

Staging, and other non-production, apps can use a free self-signed SSL certificate instead of purchasing one.

Purchasing an SSL cert varies in cost and process depending on the vendor. DNSimple offers the simplest way to purchase a certificate and is highly recommended. If you’re able to use DNSimple, see purchasing an SSL cert with DNSimple for instructions.

Otherwise, using other SSL providers will require some or all of the following steps.

Generate private key

Before requesting an SSL cert, you need to generate a private key in your local environment using the openssl tool. If you aren’t able to execute the openssl command from the terminal you may need to install it.

If you have… Install with…
Mac OS X Homebrew: brew install openssl
Windows Windows complete package .exe installer
Ubuntu Linux apt-get install openssl

Use openssl to generate a new private key.

When prompted, enter an easy password value as it will only be used when generating the CSR and not by your app at runtime.

$ openssl genrsa -des3 -out server.pass.key 2048
...
Enter pass phrase for server.pass.key:
Verifying - Enter pass phrase for server.pass.key:

The private key needs to be stripped of its password so it can be loaded without manually entering the password.

$ openssl rsa -in server.pass.key -out server.key

You now have a server.key private key file in your current working directory.

Generate CSR

A CSR is a certificate signing request and is also required when purchasing an SSL cert. Using the private key from the previous step, generate the CSR. This will require you to enter identifying information about your organization and domain.

Though most fields are self-explanatory, pay close attention to the following:

Field Description
Country Name The two letter code, in ISO 3166-1 format, of the country in which your organization is based.
Common Name This is the fully qualified domain name that you wish to secure.
  • For a single subdomain: www.example.com
  • For all subdomains, specify the wildcard URL: *.example.com
  • For the root domain: example.com

The Common Name field must match the secure domain. You cannot purchase a certificate for the root domain, e.g., example.com, and expect to secure www.example.com. The inverse is also true. Additionally, SSL Endpoint only supports one certificate per app. Please keep this in mind for multi-domain applications and specify a Common Domain that matches all required domains.

Generate the CSR:

$ openssl req -nodes -new -key server.key -out server.csr
...
Country Name (2 letter code) [AU]:US
Common Name (eg, YOUR name) []:www.example.com
...

The result of this operation will be a server.csr file in your local directory (alongside the server.key private key file from the previous step).

Submit CSR to SSL provider

Next, begin the process of creating a new SSL certificate with your chosen certificate provider. This will vary depending on your provider, but at some point you will need to upload the CSR generated in the previous step.

You may also be asked for what web server to create the certificate. If so, select Nginx as the web server for use on Heroku. If Nginx is not an option, Apache 2.x will also suffice.

If you’re given an option of what certificate format to use (PKCS, X.509 etc…) choose X.509.

If you want to secure more than one subdomain you will need to purchase a wildcard certificate from your provider. While these certificates are typically more expensive, they allow you to serve requests for all subdomains of *.example.com over SSL.

On completion of the SSL certificate purchase process you should have several files including:

  • The SSL certificate for the domain specified in your CSR, downloaded from your certificate provider. This file will have either a .pem or .crt extension.
  • The private key you generated in the first step, server.key.

Provision the add-on

Once you have the SSL certificate file and private key you are ready to configure SSL Endpoint for your app. First, provision an endpoint.

$ heroku addons:add ssl:endpoint
Adding ssl:endpoint on example... done, v1 ($20/mo)

Next add your certificate, any intermediate certificates, and private key to the endpoint with the certs:add command.

Heroku automatically strips out unnecessary parts of the certificate chain as part of the certs:add command. In some scenarios, this may not be desired. To avoid this automatic manipulation of the chain, include the --bypass flag.

$ heroku certs:add server.crt server.key
Adding SSL Endpoint to example... done
example now served by tokyo-2121.herokussl.com.
Certificate details:
Expires At: 2012-10-31 21:53:18 GMT
Issuer: C=US; ST=CA; L=SF; O=Heroku; CN=www.example.com
Starts At: 2011-11-01 21:53:18 GMT
...

The endpoint URL assigned to your app will be listed in the output, tokyo-2121.herokussl.com in this example. Visiting this URL will result in a “no such app” message – this is expected. Read further for proper verification steps.

Apps located in a non-default region, e.g., Europe, will not have a distinct herokussl.com SSL endpoint URL. Instead, the endpoint URL will just be your app’s herokuapp domain, e.g., example.herokuapp.com. The output of the certs:add command will accurately reflect this.

Endpoint details

You can verify the details of the SSL endpoint configuration with heroku certs.

$ heroku certs
Endpoint                    Common Name         Expires                    Trusted
------------------------    ----------------    -----------------------    -------
tokyo-2121.herokussl.com    www.example.com    2012-10-31 21:53:18 GMT    False

To get the detailed information about a certificate at any time use certs:info.

$ heroku certs:info
Fetching SSL Endpoint tokyo-2121.herokussl.com info for example... done
Certificate details:
Expires At: 2012-10-31 21:53:18 GMT
Issuer: C=US; ST=CA; L=SF; O=Heroku; CN=www.example.com
Starts At: 2011-11-01 21:53:18 GMT
Subject: C=US; ST=CA; L=SF; O=Heroku; CN=www.example.com
...

In rare circumstances, it can take an SSL endpoint up to 30 minutes before it’s provisioned. If you are unable to hit the endpoint URL, please wait that amount of time before proceeding.

If you have a herokussl.com endpoint URL, visit it via https, e.g., https://tokyo-2121.herokussl.com. This should throw a cert error saying that the certificate at www.example.com doesn’t match tokyo-2121.herokussl.com. This means that you are serving up the cert that you’d expect to serve (just not for the requested herokussl.com domain).

DNS and domain configuration

Once the SSL Endpoint is provisioned and your cert confirmed, you must route requests for you secure domain through the endpoint URL. Unless you’ve already done so, add the domain specified when generating the CSR to your app with:

$ heroku domains:add www.example.com
Added www.example.com to example... done

Assuming proper custom domain DNS configuration already, apps located in a non-default region, e.g., Europe, will not have to make any additional DNS modifications. Such apps can skip the remainder of this DNS section.

Subdomain

If you’re securing a subdomain, e.g., www.example.com, modify your DNS settings and create a CNAME record to the endpoint or modify the CNAME target if you already have a CNAME record.

Record Name Target
CNAME www tokyo-2121.herokussl.com.

If you’re using a wildcard certificate your DNS setup will look similar.

Record Name Target
CNAME * tokyo-2121.herokussl.com.

Root domain

If you’re securing a root domain, e.g., example.com, you must be using a DNS provider that provides CNAME-like functionality at the zone apex.

Modify your DNS settings and create an ALIAS or ANAME record to the endpoint.

Record Name Target
ALIAS or ANAME <empty> or @ tokyo-2121.herokussl.com.

Testing SSL

Use a command line utility like curl to test that everything is configured correctly for your secure domain.

The -k option tells curl to ignore untrusted certificates.

$ curl -kvI https://www.example.com
* About to connect() to www.example.com port 443 (#0)
*   Trying 50.16.234.21... connected
* Connected to www.example.com (50.16.234.21) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using AES256-SHA
* Server certificate:
*    subject: C=US; ST=CA; L=SF; O=SFDC; OU=Heroku; CN=www.example.com
*    start date: 2011-11-01 17:18:11 GMT
*    expire date: 2012-10-31 17:18:11 GMT
*    common name: www.example.com (matched)
*    issuer: C=US; ST=CA; L=SF; O=SFDC; OU=Heroku; CN=www.heroku.com
*    SSL certificate verify ok.
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
> Host: www.example.com
> Accept: */*
...

Pay attention to the output. It should print SSL certificate verify ok. If it prints something like common name: www.example.com (does not match 'www.somedomain.com') then something is not configured correctly.

Update certificate

Heroku automatically strips out unnecessary parts of the certificate chain as part of the certs:update command. In some scenarios, this may not be desired. To avoid this automatic manipulation of the chain, include the --bypass flag.

You can update a certificate using the certs:update command with the new cert and existing private key:

$ heroku certs:update server.crt server.key
Updating SSL Endpoint endpoint tokyo-2121.herokussl.com for example... done

Undo

If, for some reason, the new certificate is not working properly and traffic to your app is being disrupted, you can roll back to the previous certificate:

$ heroku certs:rollback
Rolling back SSL Endpoint endpoint tokoy-2121.herokussl.com on example... done

If there is no previous certificate, this command will fail.

Heroku will clean up and remove non-active expired certificates after a month. Attempting to roll back to an expired certificate will not work.

Remove certificate

You can remove a certificate using the certs:remove command:

$ heroku certs:remove
Removing SSL Endpoint endpoint tokyo-2121.herokussl.com on example... done

Removing a certificate does not stop billing. To stop billing, you must remove the SSL endpoint add-on. Remove the add-on with heroku addons:remove ssl:endpoint.

If you try to remove the SSL endpoint add-on before the certificate is removed, you will receive an error.

Client IP address

When an end-client (often the browser) initiates an SSL request, the request must be decrypted before being sent to your app. This extra SSL termination step obfuscates the originating IP address of the request. As a workaround, the IP address of the external client is added to the X-Forwarded-For HTTP request header.

Performance

SSL Endpoint infrastructure is elastic and scales automatically based on historical traffic levels. However, if you plan to switch a lot of traffic to a newly created SSL endpoint or if you expect large spikes, contact Heroku support so we can help with preemptive scaling.

An initial request rate of greater than 150 requests/sec or a doubling of the existing requests/second within a 5 minute period are the thresholds at which you should consider contacting support to pre-warm your endpoint. Please give us at least 1 day advanced notice for these types of requests.

Troubleshooting

Untrusted certificate

In some cases, when running heroku certs it may list your certificate as untrusted.

$ heroku certs
Endpoint                    Common Name         Expires                    Trusted
------------------------    ----------------    -----------------------    -------
tokyo-2121.herokussl.com    www.example.com    2012-10-31 21:53:18 GMT    False

If this occurs it may be because it is not trusted by Mozilla’s list of root CA’s. If this is the case your certificate should work as you expect for many browsers.

If you have uploaded a certificate that was signed by a root authority but you get the message that it is not trusted, then something is wrong with the certificate. For example, it may be missing intermediary certificates. If so, download the intermediary certificates from your SSL provider and re-run the certs:add command.

Internal server error

If you get an Internal server error when adding your cert it may be that you have an outdated version of the Heroku Toolbelt.

$ heroku certs:add server.crt server.key
Adding SSL endpoint to example... failed
! Internal server error.
! Run 'heroku status' to check for known platform issues.

Verify your toolbelt installation and update it to the latest version with heroku update.

SSL file types

Many different file types are produced and consumed when creating an SSL certificate.

  • A .csr file is a certificate signing request which initiates your certificate request with a certificate provider and contains administrative information about your organization.
  • A .key file is the private key used for your site’s SSL-enabled requests.
  • .pem and .crt extensions are often used interchangeably and are both base64 ASCII encoded files. The technical difference is that .pem files contain both the certificate and key whereas a .crt file only contains the certificate. In reality this distinction is often ignored.

Upgrade from legacy SSL add-ons

It is straightforward to upgrade to SSL Endpoint from a legacy ssl:hostname add-on. No downtime is required.

Start by adding ssl:endpoint to your app:

$ heroku addons:add --app myapp ssl:endpoint
Adding ssl:endpoint to myapp... done

Now upload your certificate and private key. You don’t need to get a new certificate; you can use the same one as in your existing SSL setup. (Now would also be a good time to make sure it’s still valid and not expired).

$ heroku certs:add --app myapp my_existing.crt my_existing.key
Adding certificate to myapp... done
myapp now served by tokyo-2121.herokussl.com.

Your new endpoint is now ready to receive traffic. To direct traffic to the endpoint, go to your DNS provider and update the records for your domain so that you have a single CNAME entry pointing to the SSL endpoint host (e.g. tokyo-2121.herokussl.com). If you already had a CNAME entry pointing to your app, change it to point to your SSL endpoint name instead.

Once the DNS change propagates, your users will be routed to the new endpoint. You can de-provision the old SSL add-on, for example:

$ heroku addons:remove --app myapp ssl:hostname
Removing ssl:hostname from myapp... done