blob: 3a77e8adf7d0253437ce9936783e3e081f01a2d7 [file] [log] [blame]
刘洪青6266f992017-05-15 21:21:03 +08001<html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Apache Tomcat 7 (7.0.77) - SSL/TLS Configuration HOW-TO</title><meta name="author" content="Christopher Cain"><meta name="author" content="Yoav Shapira"><style type="text/css" media="print">
2 .noPrint {display: none;}
3 td#mainBody {width: 100%;}
4</style><style type="text/css">
5code {background-color:rgb(224,255,255);padding:0 0.1em;}
6code.attributeName, code.propertyName {background-color:transparent;}
7
8
9table {
10 border-collapse: collapse;
11 text-align: left;
12}
13table *:not(table) {
14 /* Prevent border-collapsing for table child elements like <div> */
15 border-collapse: separate;
16}
17
18th {
19 text-align: left;
20}
21
22
23div.codeBox pre code, code.attributeName, code.propertyName, code.noHighlight, .noHighlight code {
24 background-color: transparent;
25}
26div.codeBox {
27 overflow: auto;
28 margin: 1em 0;
29}
30div.codeBox pre {
31 margin: 0;
32 padding: 4px;
33 border: 1px solid #999;
34 border-radius: 5px;
35 background-color: #eff8ff;
36 display: table; /* To prevent <pre>s from taking the complete available width. */
37 /*
38 When it is officially supported, use the following CSS instead of display: table
39 to prevent big <pre>s from exceeding the browser window:
40 max-width: available;
41 width: min-content;
42 */
43}
44
45div.codeBox pre.wrap {
46 white-space: pre-wrap;
47}
48
49
50table.defaultTable tr, table.detail-table tr {
51 border: 1px solid #CCC;
52}
53
54table.defaultTable tr:nth-child(even), table.detail-table tr:nth-child(even) {
55 background-color: #FAFBFF;
56}
57
58table.defaultTable tr:nth-child(odd), table.detail-table tr:nth-child(odd) {
59 background-color: #EEEFFF;
60}
61
62table.defaultTable th, table.detail-table th {
63 background-color: #88b;
64 color: #fff;
65}
66
67table.defaultTable th, table.defaultTable td, table.detail-table th, table.detail-table td {
68 padding: 5px 8px;
69}
70
71
72p.notice {
73 border: 1px solid rgb(255, 0, 0);
74 background-color: rgb(238, 238, 238);
75 color: rgb(0, 51, 102);
76 padding: 0.5em;
77 margin: 1em 2em 1em 1em;
78}
79</style></head><body bgcolor="#ffffff" text="#000000" link="#525D76" alink="#525D76" vlink="#525D76"><table border="0" width="100%" cellspacing="0"><!--PAGE HEADER--><tr><td><!--PROJECT LOGO--><a href="http://tomcat.apache.org/"><img src="./images/tomcat.gif" align="right" alt="
Hongqing Liufd5ee812014-05-10 16:32:51 +080080 The Apache Tomcat Servlet/JSP Container
刘洪青6266f992017-05-15 21:21:03 +080081 " border="0"></a></td><td><h1><font face="arial,helvetica,sanserif">Apache Tomcat 7</font></h1><font face="arial,helvetica,sanserif">Version 7.0.77, Mar 28 2017</font></td><td><!--APACHE LOGO--><a href="http://www.apache.org/"><img src="./images/asf-logo.svg" align="right" alt="Apache Logo" border="0" style="width: 266px;height: 83px;"></a></td></tr></table><table border="0" width="100%" cellspacing="4"><!--HEADER SEPARATOR--><tr><td colspan="2"><hr noshade size="1"></td></tr><tr><!--LEFT SIDE NAVIGATION--><td width="20%" valign="top" nowrap class="noPrint"><p><strong>Links</strong></p><ul><li><a href="index.html">Docs Home</a></li><li><a href="http://wiki.apache.org/tomcat/FAQ">FAQ</a></li><li><a href="#comments_section">User Comments</a></li></ul><p><strong>User Guide</strong></p><ul><li><a href="introduction.html">1) Introduction</a></li><li><a href="setup.html">2) Setup</a></li><li><a href="appdev/index.html">3) First webapp</a></li><li><a href="deployer-howto.html">4) Deployer</a></li><li><a href="manager-howto.html">5) Manager</a></li><li><a href="realm-howto.html">6) Realms and AAA</a></li><li><a href="security-manager-howto.html">7) Security Manager</a></li><li><a href="jndi-resources-howto.html">8) JNDI Resources</a></li><li><a href="jndi-datasource-examples-howto.html">9) JDBC DataSources</a></li><li><a href="class-loader-howto.html">10) Classloading</a></li><li><a href="jasper-howto.html">11) JSPs</a></li><li><a href="ssl-howto.html">12) SSL/TLS</a></li><li><a href="ssi-howto.html">13) SSI</a></li><li><a href="cgi-howto.html">14) CGI</a></li><li><a href="proxy-howto.html">15) Proxy Support</a></li><li><a href="mbeans-descriptors-howto.html">16) MBeans Descriptors</a></li><li><a href="default-servlet.html">17) Default Servlet</a></li><li><a href="cluster-howto.html">18) Clustering</a></li><li><a href="balancer-howto.html">19) Load Balancer</a></li><li><a href="connectors.html">20) Connectors</a></li><li><a href="monitoring.html">21) Monitoring and Management</a></li><li><a href="logging.html">22) Logging</a></li><li><a href="apr.html">23) APR/Native</a></li><li><a href="virtual-hosting-howto.html">24) Virtual Hosting</a></li><li><a href="aio.html">25) Advanced IO</a></li><li><a href="extras.html">26) Additional Components</a></li><li><a href="maven-jars.html">27) Mavenized</a></li><li><a href="security-howto.html">28) Security Considerations</a></li><li><a href="windows-service-howto.html">29) Windows Service</a></li><li><a href="windows-auth-howto.html">30) Windows Authentication</a></li><li><a href="jdbc-pool.html">31) Tomcat's JDBC Pool</a></li><li><a href="web-socket-howto.html">32) WebSocket</a></li></ul><p><strong>Reference</strong></p><ul><li><a href="RELEASE-NOTES.txt">Release Notes</a></li><li><a href="config/index.html">Configuration</a></li><li><a href="api/index.html">Tomcat Javadocs</a></li><li><a href="servletapi/index.html">Servlet Javadocs</a></li><li><a href="jspapi/index.html">JSP 2.2 Javadocs</a></li><li><a href="elapi/index.html">EL 2.2 Javadocs</a></li><li><a href="websocketapi/index.html">WebSocket 1.1 Javadocs</a></li><li><a href="http://tomcat.apache.org/connectors-doc/">JK 1.2 Documentation</a></li></ul><p><strong>Apache Tomcat Development</strong></p><ul><li><a href="building.html">Building</a></li><li><a href="changelog.html">Changelog</a></li><li><a href="http://wiki.apache.org/tomcat/TomcatVersions">Status</a></li><li><a href="developers.html">Developers</a></li><li><a href="architecture/index.html">Architecture</a></li><li><a href="funcspecs/index.html">Functional Specs.</a></li><li><a href="tribes/introduction.html">Tribes</a></li></ul></td><!--RIGHT SIDE MAIN BODY--><td width="80%" valign="top" align="left" id="mainBody"><h1>SSL/TLS Configuration HOW-TO</h1><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Table of Contents"><!--()--></a><a name="Table_of_Contents"><strong>Table of Contents</strong></a></font></td></tr><tr><td><blockquote>
82<ul><li><a href="#Quick_Start">Quick Start</a></li><li><a href="#Introduction_to_SSL">Introduction to SSL/TLS</a></li><li><a href="#SSL_and_Tomcat">SSL/TLS and Tomcat</a></li><li><a href="#Certificates">Certificates</a></li><li><a href="#General_Tips_on_Running_SSL">General Tips on Running SSL</a></li><li><a href="#Configuration">Configuration</a><ol><li><a href="#Prepare_the_Certificate_Keystore">Prepare the Certificate Keystore</a></li><li><a href="#Edit_the_Tomcat_Configuration_File">Edit the Tomcat Configuration File</a></li></ol></li><li><a href="#Installing_a_Certificate_from_a_Certificate_Authority">Installing a Certificate from a Certificate Authority</a><ol><li><a href="#Create_a_local_Certificate_Signing_Request_(CSR)">Create a local Certificate Signing Request (CSR)</a></li><li><a href="#Importing_the_Certificate">Importing the Certificate</a></li></ol></li><li><a href="#Troubleshooting">Troubleshooting</a></li><li><a href="#Using_the_SSL_for_session_tracking_in_your_application">Using the SSL for session tracking in your application</a></li><li><a href="#Miscellaneous_Tips_and_Bits">Miscellaneous Tips and Bits</a></li></ul>
Hongqing Liufd5ee812014-05-10 16:32:51 +080083</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Quick Start"><!--()--></a><a name="Quick_Start"><strong>Quick Start</strong></a></font></td></tr><tr><td><blockquote>
84
85 <blockquote><em>
86 <p>The description below uses the variable name $CATALINA_BASE to refer the
87 base directory against which most relative paths are resolved. If you have
刘洪青6266f992017-05-15 21:21:03 +080088 not configured Tomcat for multiple instances by setting a CATALINA_BASE
Hongqing Liufd5ee812014-05-10 16:32:51 +080089 directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME,
刘洪青6266f992017-05-15 21:21:03 +080090 the directory into which you have installed Tomcat.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +080091 </em></blockquote>
92
刘洪青6266f992017-05-15 21:21:03 +080093<p>To install and configure SSL/TLS support on Tomcat, you need to follow
Hongqing Liufd5ee812014-05-10 16:32:51 +080094these simple steps. For more information, read the rest of this HOW-TO.</p>
95<ol>
96<li>Create a keystore file to store the server's private key and
97self-signed certificate by executing the following command:
98<p>Windows:</p>
刘洪青6266f992017-05-15 21:21:03 +080099<div class="codeBox"><pre><code>"%JAVA_HOME%\bin\keytool" -genkey -alias tomcat -keyalg RSA</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800100<p>Unix:</p>
刘洪青6266f992017-05-15 21:21:03 +0800101<div class="codeBox"><pre><code>$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA</code></pre></div>
102
103<p>and specify a password value of "changeit".</p></li>
104<li><p>Uncomment the "SSL HTTP/1.1 Connector" entry in
105 <code>$CATALINA_BASE/conf/server.xml</code> and modify as described in
106 the <a href="#Configuration">Configuration section</a> below.</p></li>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800107</ol>
108
109
刘洪青6266f992017-05-15 21:21:03 +0800110</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Introduction_to_SSL"><strong>Introduction to SSL/TLS</strong></a></font></td></tr><tr><td><blockquote>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800111
刘洪青6266f992017-05-15 21:21:03 +0800112<p>Transport Layer Security (TLS) and its predecessor, Secure Sockets Layer
113(SSL), are technologies which allow web browsers and web servers to communicate
114over a secured connection. This means that the data being sent is encrypted by
115one side, transmitted, then decrypted by the other side before processing.
116This is a two-way process, meaning that both the server AND the browser encrypt
117all traffic before sending out data.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800118
刘洪青6266f992017-05-15 21:21:03 +0800119<p>Another important aspect of the SSL/TLS protocol is Authentication. This means
Hongqing Liufd5ee812014-05-10 16:32:51 +0800120that during your initial attempt to communicate with a web server over a secure
121connection, that server will present your web browser with a set of
122credentials, in the form of a "Certificate", as proof the site is who and what
123it claims to be. In certain cases, the server may also request a Certificate
124from your web browser, asking for proof that <em>you</em> are who you claim
125to be. This is known as "Client Authentication," although in practice this is
126used more for business-to-business (B2B) transactions than with individual
127users. Most SSL-enabled web servers do not request Client Authentication.</p>
128
刘洪青6266f992017-05-15 21:21:03 +0800129</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="SSL_and_Tomcat"><strong>SSL/TLS and Tomcat</strong></a></font></td></tr><tr><td><blockquote>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800130
131<p>It is important to note that configuring Tomcat to take advantage of
132secure sockets is usually only necessary when running it as a stand-alone
刘洪青6266f992017-05-15 21:21:03 +0800133web server. Details can be found in the
134<a href="security-howto.html">Security Considerations Document</a>.
135When running Tomcat primarily as a Servlet/JSP container behind
Hongqing Liufd5ee812014-05-10 16:32:51 +0800136another web server, such as Apache or Microsoft IIS, it is usually necessary
137to configure the primary web server to handle the SSL connections from users.
138Typically, this server will negotiate all SSL-related functionality, then
139pass on any requests destined for the Tomcat container only after decrypting
140those requests. Likewise, Tomcat will return cleartext responses, that will
141be encrypted before being returned to the user's browser. In this environment,
142Tomcat knows that communications between the primary web server and the
143client are taking place over a secure connection (because your application
144needs to be able to ask about this), but it does not participate in the
145encryption or decryption itself.</p>
146
147</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Certificates"><strong>Certificates</strong></a></font></td></tr><tr><td><blockquote>
148
149<p>In order to implement SSL, a web server must have an associated Certificate
150for each external interface (IP address) that accepts secure connections.
151The theory behind this design is that a server should provide some kind of
152reasonable assurance that its owner is who you think it is, particularly
153before receiving any sensitive information. While a broader explanation of
刘洪青6266f992017-05-15 21:21:03 +0800154Certificates is beyond the scope of this document, think of a Certificate as a
155"digital passport" for an Internet address. It states which organisation the
156site is associated with, along with some basic contact information about the
157site owner or administrator.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800158
刘洪青6266f992017-05-15 21:21:03 +0800159<p>This certificate is cryptographically signed by its owner, and is
160therefore extremely difficult for anyone else to forge. For the certificate to
161work in the visitors browsers without warnings, it needs to be signed by a
162trusted third party. These are called <em>Certificate Authorities</em> (CAs). To
163obtain a signed certificate, you need to choose a CA and follow the instructions
164your chosen CA provides to obtain your certificate. A range of CAs is available
165including some that offer certificates at no cost.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800166
刘洪青6266f992017-05-15 21:21:03 +0800167<p>Java provides a relatively simple command-line tool, called
168<code>keytool</code>, which can easily create a "self-signed" Certificate.
169Self-signed Certificates are simply user generated Certificates which have not
170been signed by a well-known CA and are, therefore, not really guaranteed to be
171authentic at all. While self-signed certificates can be useful for some testing
172scenarios, they are not suitable for any form of production use.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800173
174</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="General Tips on Running SSL"><!--()--></a><a name="General_Tips_on_Running_SSL"><strong>General Tips on Running SSL</strong></a></font></td></tr><tr><td><blockquote>
175
刘洪青6266f992017-05-15 21:21:03 +0800176<p>When securing a website with SSL it's important to make sure that all assets
177that the site uses are served over SSL, so that an attacker can't bypass
178the security by injecting malicious content in a javascript file or similar. To
179further enhance the security of your website, you should evaluate to use the
180HSTS header. It allows you to communicate to the browser that your site should
181always be accessed over https.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800182
刘洪青6266f992017-05-15 21:21:03 +0800183<p>Using name-based virtual hosts on a secured connection requires careful
184configuration of the names specified in a single certificate or Tomcat 8.5
185onwards where Server Name Indication (SNI) support is available. SNI allows
186multiple certificates with different names to be associated with a single TLS
187connector.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800188
189</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Configuration"><strong>Configuration</strong></a></font></td></tr><tr><td><blockquote>
190
191<table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#828DA6"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Prepare the Certificate Keystore"><!--()--></a><a name="Prepare_the_Certificate_Keystore"><strong>Prepare the Certificate Keystore</strong></a></font></td></tr><tr><td><blockquote>
192
193<p>Tomcat currently operates only on <code>JKS</code>, <code>PKCS11</code> or
194<code>PKCS12</code> format keystores. The <code>JKS</code> format
195is Java's standard "Java KeyStore" format, and is the format created by the
196<code>keytool</code> command-line utility. This tool is included in the JDK.
197The <code>PKCS12</code> format is an internet standard, and can be manipulated
198via (among other things) OpenSSL and Microsoft's Key-Manager.
199</p>
200
201<p>Each entry in a keystore is identified by an alias string. Whilst many
202keystore implementations treat aliases in a case insensitive manner, case
203sensitive implementations are available. The <code>PKCS11</code> specification,
204for example, requires that aliases are case sensitive. To avoid issues related
205to the case sensitivity of aliases, it is not recommended to use aliases that
206differ only in case.
207</p>
208
刘洪青6266f992017-05-15 21:21:03 +0800209<p>To import an existing certificate into a <code>JKS</code> keystore, please read the
Hongqing Liufd5ee812014-05-10 16:32:51 +0800210documentation (in your JDK documentation package) about <code>keytool</code>.
刘洪青6266f992017-05-15 21:21:03 +0800211Note that OpenSSL often adds readable comments before the key, but
212<code>keytool</code> does not support that. So if your certificate has
213comments before the key data, remove them before importing the certificate with
214<code>keytool</code>.
Hongqing Liufd5ee812014-05-10 16:32:51 +0800215</p>
刘洪青6266f992017-05-15 21:21:03 +0800216<p>To import an existing certificate signed by your own CA into a <code>PKCS12</code>
217keystore using OpenSSL you would execute a command like:</p>
218<div class="codeBox"><pre><code>openssl pkcs12 -export -in mycert.crt -inkey mykey.key
219 -out mycert.p12 -name tomcat -CAfile myCA.crt
220 -caname root -chain</code></pre></div>
221<p>For more advanced cases, consult the
222<a href="http://www.openssl.org/" rel="nofollow">OpenSSL documentation</a>.</p>
223
224<p>To create a new <code>JKS</code> keystore from scratch, containing a single
225self-signed Certificate, execute the following from a terminal command line:</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800226<p>Windows:</p>
刘洪青6266f992017-05-15 21:21:03 +0800227<div class="codeBox"><pre><code>"%JAVA_HOME%\bin\keytool" -genkey -alias tomcat -keyalg RSA</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800228<p>Unix:</p>
刘洪青6266f992017-05-15 21:21:03 +0800229<div class="codeBox"><pre><code>$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800230
231<p>(The RSA algorithm should be preferred as a secure algorithm, and this
232also ensures general compatibility with other servers and components.)</p>
233
234<p>This command will create a new file, in the home directory of the user
235under which you run it, named "<code>.keystore</code>". To specify a
236different location or filename, add the <code>-keystore</code> parameter,
237followed by the complete pathname to your keystore file,
238to the <code>keytool</code> command shown above. You will also need to
239reflect this new location in the <code>server.xml</code> configuration file,
240as described later. For example:</p>
241<p>Windows:</p>
刘洪青6266f992017-05-15 21:21:03 +0800242<div class="codeBox"><pre><code>"%JAVA_HOME%\bin\keytool" -genkey -alias tomcat -keyalg RSA
243 -keystore \path\to\my\keystore</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800244<p>Unix:</p>
刘洪青6266f992017-05-15 21:21:03 +0800245<div class="codeBox"><pre><code>$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA
246 -keystore /path/to/my/keystore</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800247
248<p>After executing this command, you will first be prompted for the keystore
249password. The default password used by Tomcat is "<code>changeit</code>"
250(all lower case), although you can specify a custom password if you like.
251You will also need to specify the custom password in the
252<code>server.xml</code> configuration file, as described later.</p>
253
254<p>Next, you will be prompted for general information about this Certificate,
255such as company, contact name, and so on. This information will be displayed
256to users who attempt to access a secure page in your application, so make
257sure that the information provided here matches what they will expect.</p>
258
259<p>Finally, you will be prompted for the <em>key password</em>, which is the
260password specifically for this Certificate (as opposed to any other
刘洪青6266f992017-05-15 21:21:03 +0800261Certificates stored in the same keystore file). The <code>keytool</code> prompt
262will tell you that pressing the ENTER key automatically uses the same password
263for the key as the keystore. You are free to use the same password or to select
264a custom one. If you select a different password to the keystore password, you
265will also need to specify the custom password in the <code>server.xml</code>
266configuration file.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800267
268<p>If everything was successful, you now have a keystore file with a
269Certificate that can be used by your server.</p>
270
Hongqing Liufd5ee812014-05-10 16:32:51 +0800271</blockquote></td></tr></table>
272
273<table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#828DA6"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Edit the Tomcat Configuration File"><!--()--></a><a name="Edit_the_Tomcat_Configuration_File"><strong>Edit the Tomcat Configuration File</strong></a></font></td></tr><tr><td><blockquote>
274<p>
275Tomcat can use two different implementations of SSL:
276<ul>
277<li>the JSSE implementation provided as part of the Java runtime (since 1.4)</li>
278<li>the APR implementation, which uses the OpenSSL engine by default.</li>
279</ul>
280The exact configuration details depend on which implementation is being used.
刘洪青6266f992017-05-15 21:21:03 +0800281If you configured Connector by specifying generic
282<code>protocol="HTTP/1.1"</code> then the implementation used by Tomcat is
283chosen automatically. If the installation uses <a href="apr.html">APR</a>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800284- i.e. you have installed the Tomcat native library -
刘洪青6266f992017-05-15 21:21:03 +0800285then it will use the APR SSL implementation, otherwise it will use the Java
286JSSE implementation.
Hongqing Liufd5ee812014-05-10 16:32:51 +0800287</p>
288
289<p>
刘洪青6266f992017-05-15 21:21:03 +0800290As configuration attributes for SSL support significantly differ between
291APR vs. JSSE implementations, it is <strong>recommended</strong> to
292avoid auto-selection of implementation. It is done by specifying a classname
293in the <b>protocol</b> attribute of the <a href="config/http.html">Connector</a>.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800294
刘洪青6266f992017-05-15 21:21:03 +0800295<p>To define a Java (JSSE) connector, regardless of whether the APR library is
296loaded or not, use one of the following:</p>
297<div class="codeBox"><pre><code>&lt;!-- Define a HTTP/1.1 Connector on port 8443, JSSE NIO implementation --&gt;
Hongqing Liufd5ee812014-05-10 16:32:51 +0800298&lt;Connector protocol="org.apache.coyote.http11.Http11NioProtocol"
299 port="8443" .../&gt;
Hongqing Liufd5ee812014-05-10 16:32:51 +0800300
刘洪青6266f992017-05-15 21:21:03 +0800301&lt;!-- Define a HTTP/1.1 Connector on port 8443, JSSE BIO implementation --&gt;
302&lt;Connector protocol="org.apache.coyote.http11.Http11Protocol"
303 port="8443" .../&gt;</code></pre></div>
304<p>Alternatively, to specify an APR connector (the APR library must be available) use:</p>
305<div class="codeBox"><pre><code>&lt;!-- Define a HTTP/1.1 Connector on port 8443, APR implementation --&gt;
306&lt;Connector protocol="org.apache.coyote.http11.Http11AprProtocol"
307 port="8443" .../&gt;</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800308
309<p>If you are using APR, you have the option of configuring an alternative engine to OpenSSL.
刘洪青6266f992017-05-15 21:21:03 +0800310<div class="codeBox"><pre><code>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800311&lt;Listener className="org.apache.catalina.core.AprLifecycleListener"
312 SSLEngine="someengine" SSLRandomSeed="somedevice" /&gt;
刘洪青6266f992017-05-15 21:21:03 +0800313</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800314The default value is
刘洪青6266f992017-05-15 21:21:03 +0800315<div class="codeBox"><pre><code>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800316&lt;Listener className="org.apache.catalina.core.AprLifecycleListener"
317 SSLEngine="on" SSLRandomSeed="builtin" /&gt;
刘洪青6266f992017-05-15 21:21:03 +0800318</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800319So to use SSL under APR, make sure the SSLEngine attribute is set to something other than <code>off</code>.
320The default value is <code>on</code> and if you specify another value, it has to be a valid engine name.
刘洪青6266f992017-05-15 21:21:03 +0800321</p>
322
323<p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800324SSLRandomSeed allows to specify a source of entropy. Productive system needs a reliable source of entropy
325but entropy may need a lot of time to be collected therefore test systems could use no blocking entropy
326sources like "/dev/urandom" that will allow quicker starts of Tomcat.
327
328</p>
329
330<p>The final step is to configure the Connector in the
331<code>$CATALINA_BASE/conf/server.xml</code> file, where
332<code>$CATALINA_BASE</code> represents the base directory for the
刘洪青6266f992017-05-15 21:21:03 +0800333Tomcat instance. An example <code>&lt;Connector&gt;</code> element
Hongqing Liufd5ee812014-05-10 16:32:51 +0800334for an SSL connector is included in the default <code>server.xml</code>
刘洪青6266f992017-05-15 21:21:03 +0800335file installed with Tomcat. To configure an SSL connector that uses JSSE, you
336will need to remove the comments and edit it so it looks something like
337this:</p>
338<div class="codeBox"><pre><code>
339&lt;!-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 --&gt;
340&lt;Connector
341 protocol="org.apache.coyote.http11.Http11NioProtocol"
Hongqing Liufd5ee812014-05-10 16:32:51 +0800342 port="8443" maxThreads="200"
343 scheme="https" secure="true" SSLEnabled="true"
344 keystoreFile="${user.home}/.keystore" keystorePass="changeit"
345 clientAuth="false" sslProtocol="TLS"/&gt;
刘洪青6266f992017-05-15 21:21:03 +0800346</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800347<p>
刘洪青6266f992017-05-15 21:21:03 +0800348 The APR connector uses different attributes for many SSL settings,
349 particularly keys and certificates. An example of an APR configuration is:
350<div class="codeBox"><pre><code>
351&lt;!-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 --&gt;
352&lt;Connector
353 protocol="org.apache.coyote.http11.Http11AprProtocol"
Hongqing Liufd5ee812014-05-10 16:32:51 +0800354 port="8443" maxThreads="200"
355 scheme="https" secure="true" SSLEnabled="true"
刘洪青6266f992017-05-15 21:21:03 +0800356 SSLCertificateFile="/usr/local/ssl/server.crt"
Hongqing Liufd5ee812014-05-10 16:32:51 +0800357 SSLCertificateKeyFile="/usr/local/ssl/server.pem"
刘洪青6266f992017-05-15 21:21:03 +0800358 SSLVerifyClient="optional" SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"/&gt;
359</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800360</p>
361
刘洪青6266f992017-05-15 21:21:03 +0800362<p>The configuration options and information on which attributes
363are mandatory, are documented in the SSL Support section of the
364<a href="config/http.html#SSL Support">HTTP connector</a> configuration
365reference. Make sure that you use the correct attributes for the connector you
366are using. The BIO and NIO connectors use JSSE whereas the APR/native connector
367uses APR.</p>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800368
刘洪青6266f992017-05-15 21:21:03 +0800369<p>The <code>port</code> attribute is the TCP/IP
Hongqing Liufd5ee812014-05-10 16:32:51 +0800370port number on which Tomcat will listen for secure connections. You can
371change this to any port number you wish (such as to the default port for
372<code>https</code> communications, which is 443). However, special setup
373(outside the scope of this document) is necessary to run Tomcat on port
374numbers lower than 1024 on many operating systems.</p>
375
376 <blockquote><em>
377 <p>If you change the port number here, you should also change the
378 value specified for the <code>redirectPort</code> attribute on the
379 non-SSL connector. This allows Tomcat to automatically redirect
380 users who attempt to access a page with a security constraint specifying
381 that SSL is required, as required by the Servlet Specification.</p>
382 </em></blockquote>
383
384<p>After completing these configuration changes, you must restart Tomcat as
385you normally do, and you should be in business. You should be able to access
386any web application supported by Tomcat via SSL. For example, try:</p>
刘洪青6266f992017-05-15 21:21:03 +0800387<div class="codeBox"><pre><code>https://localhost:8443/</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800388<p>and you should see the usual Tomcat splash page (unless you have modified
389the ROOT web application). If this does not work, the following section
390contains some troubleshooting tips.</p>
391
392</blockquote></td></tr></table>
393
394</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Installing a Certificate from a Certificate Authority"><!--()--></a><a name="Installing_a_Certificate_from_a_Certificate_Authority"><strong>Installing a Certificate from a Certificate Authority</strong></a></font></td></tr><tr><td><blockquote>
刘洪青6266f992017-05-15 21:21:03 +0800395<p>To obtain and install a Certificate from a Certificate Authority (like verisign.com, thawte.com
Hongqing Liufd5ee812014-05-10 16:32:51 +0800396or trustcenter.de), read the previous section and then follow these instructions:</p>
397
398<table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#828DA6"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Create a local Certificate Signing Request (CSR)"><!--()--></a><a name="Create_a_local_Certificate_Signing_Request_(CSR)"><strong>Create a local Certificate Signing Request (CSR)</strong></a></font></td></tr><tr><td><blockquote>
刘洪青6266f992017-05-15 21:21:03 +0800399<p>In order to obtain a Certificate from the Certificate Authority of your choice
400you have to create a so called Certificate Signing Request (CSR). That CSR will be used
401by the Certificate Authority to create a Certificate that will identify your website
Hongqing Liufd5ee812014-05-10 16:32:51 +0800402as "secure". To create a CSR follow these steps:</p>
403<ul>
刘洪青6266f992017-05-15 21:21:03 +0800404<li>Create a local self-signed Certificate (as described in the previous section):
405 <div class="codeBox"><pre><code>keytool -genkey -alias tomcat -keyalg RSA
406 -keystore &lt;your_keystore_filename&gt;</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800407 Note: In some cases you will have to enter the domain of your website (i.e. <code>www.myside.org</code>)
刘洪青6266f992017-05-15 21:21:03 +0800408 in the field "first- and lastname" in order to create a working Certificate.
Hongqing Liufd5ee812014-05-10 16:32:51 +0800409</li>
410<li>The CSR is then created with:
刘洪青6266f992017-05-15 21:21:03 +0800411 <div class="codeBox"><pre><code>keytool -certreq -keyalg RSA -alias tomcat -file certreq.csr
412 -keystore &lt;your_keystore_filename&gt;</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800413</li>
414</ul>
415<p>Now you have a file called <code>certreq.csr</code> that you can submit to the Certificate Authority (look at the
416documentation of the Certificate Authority website on how to do this). In return you get a Certificate.</p>
417</blockquote></td></tr></table>
418
419<table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#828DA6"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Importing the Certificate"><!--()--></a><a name="Importing_the_Certificate"><strong>Importing the Certificate</strong></a></font></td></tr><tr><td><blockquote>
刘洪青6266f992017-05-15 21:21:03 +0800420<p>Now that you have your Certificate you can import it into you local keystore.
421First of all you have to import a so called Chain Certificate or Root Certificate into your keystore.
Hongqing Liufd5ee812014-05-10 16:32:51 +0800422After that you can proceed with importing your Certificate.</p>
423
424<ul>
425<li>Download a Chain Certificate from the Certificate Authority you obtained the Certificate from.<br>
426 For Verisign.com commercial certificates go to:
427 http://www.verisign.com/support/install/intermediate.html<br>
428 For Verisign.com trial certificates go to:
429 http://www.verisign.com/support/verisign-intermediate-ca/Trial_Secure_Server_Root/index.html<br>
430 For Trustcenter.de go to:
431 http://www.trustcenter.de/certservices/cacerts/en/en.htm#server<br>
432 For Thawte.com go to:
433 http://www.thawte.com/certs/trustmap.html<br>
434</li>
435<li>Import the Chain Certificate into your keystore
刘洪青6266f992017-05-15 21:21:03 +0800436 <div class="codeBox"><pre><code>keytool -import -alias root -keystore &lt;your_keystore_filename&gt;
437 -trustcacerts -file &lt;filename_of_the_chain_certificate&gt;</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800438</li>
439<li>And finally import your new Certificate
刘洪青6266f992017-05-15 21:21:03 +0800440 <div class="codeBox"><pre><code>keytool -import -alias tomcat -keystore &lt;your_keystore_filename&gt;
441 -file &lt;your_certificate_filename&gt;</code></pre></div>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800442</li>
443</ul>
444</blockquote></td></tr></table>
445</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Troubleshooting"><strong>Troubleshooting</strong></a></font></td></tr><tr><td><blockquote>
446
447<p>Here is a list of common problems that you may encounter when setting up
448SSL communications, and what to do about them.</p>
449
450<ul>
451
452<li>When Tomcat starts up, I get an exception like
453 "java.io.FileNotFoundException: {some-directory}/{some-file} not found".
454 <blockquote>
455 <p>A likely explanation is that Tomcat cannot find the keystore file
456 where it is looking. By default, Tomcat expects the keystore file to
457 be named <code>.keystore</code> in the user home directory under which
458 Tomcat is running (which may or may not be the same as yours :-). If
459 the keystore file is anywhere else, you will need to add a
刘洪青6266f992017-05-15 21:21:03 +0800460 <code>keystoreFile</code> attribute to the <code>&lt;Connector&gt;</code>
Hongqing Liufd5ee812014-05-10 16:32:51 +0800461 element in the <a href="#Edit the Tomcat Configuration File">Tomcat
462 configuration file</a>.</p>
463 </blockquote></li>
464
465<li>When Tomcat starts up, I get an exception like
466 "java.io.FileNotFoundException: Keystore was tampered with, or
467 password was incorrect".
468 <blockquote>
469 <p>Assuming that someone has not <em>actually</em> tampered with
470 your keystore file, the most likely cause is that Tomcat is using
471 a different password than the one you used when you created the
472 keystore file. To fix this, you can either go back and
473 <a href="#Prepare the Certificate Keystore">recreate the keystore
474 file</a>, or you can add or update the <code>keystorePass</code>
475 attribute on the <code>&lt;Connector&gt;</code> element in the
476 <a href="#Edit the Tomcat Configuration File">Tomcat configuration
477 file</a>. <strong>REMINDER</strong> - Passwords are case sensitive!</p>
478 </blockquote></li>
479
480<li>When Tomcat starts up, I get an exception like
刘洪青6266f992017-05-15 21:21:03 +0800481 "java.net.SocketException: SSL handshake error javax.net.ssl.SSLException: No
Hongqing Liufd5ee812014-05-10 16:32:51 +0800482 available certificate or key corresponds to the SSL cipher suites which are
483 enabled."
484 <blockquote>
485 <p>A likely explanation is that Tomcat cannot find the alias for the server
486 key within the specified keystore. Check that the correct
487 <code>keystoreFile</code> and <code>keyAlias</code> are specified in the
488 <code>&lt;Connector&gt;</code> element in the
489 <a href="#Edit the Tomcat Configuration File">Tomcat configuration file</a>.
490 <strong>REMINDER</strong> - <code>keyAlias</code> values may be case
491 sensitive!</p>
492 </blockquote></li>
493
刘洪青6266f992017-05-15 21:21:03 +0800494<li>My Java-based client aborts handshakes with exceptions such as
495 "java.lang.RuntimeException: Could not generate DH keypair" and
496 "java.security.InvalidAlgorithmParameterException: Prime size must be multiple
497 of 64, and can only range from 512 to 1024 (inclusive)"
498
499 <p>If you are using the APR/native connector, starting with version 1.1.34
500 it will determine the strength of ephemeral DH keys from the key size of
501 your RSA certificate. For example a 2048 bit RSA key will result in
502 using a 2048 bit primefor the DH keys. Unfortunately Java 6 only supports
503 768 bit and Java 7 only supports 1024 bit. So if your certificate has a
504 stronger key, old Java clients might produce such handshake failures.
505 As a mitigation you can either try to force them to use another cipher by
506 configuring an appropriate <code>SSLCipherSuite</code> and activate
507 <code>SSLHonorCipherOrder</code>, or embed weak DH params in your
508 certificate file. The latter approach is not recommended because it weakens
509 the SSL security (logjam attack).</p>
510 </li>
511
Hongqing Liufd5ee812014-05-10 16:32:51 +0800512</ul>
513
514<p>If you are still having problems, a good source of information is the
515<strong>TOMCAT-USER</strong> mailing list. You can find pointers to archives
516of previous messages on this list, as well as subscription and unsubscription
517information, at
518<a href="http://tomcat.apache.org/lists.html">http://tomcat.apache.org/lists.html</a>.</p>
519
刘洪青6266f992017-05-15 21:21:03 +0800520</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Using the SSL for session tracking in your application"><!--()--></a><a name="Using_the_SSL_for_session_tracking_in_your_application"><strong>Using the SSL for session tracking in your application</strong></a></font></td></tr><tr><td><blockquote>
521 <p>This is a new feature in the Servlet 3.0 specification. Because it uses the
522 SSL session ID associated with the physical client-server connection there
523 are some limitations. They are:
524 <ul>
525 <li>Tomcat must have a connector with the attribute
526 <strong>isSecure</strong> set to <code>true</code>.</li>
527 <li>If SSL connections are managed by a proxy or a hardware accelerator
528 they must populate the SSL request headers (see the
529 <a href="config/valve.html">SSLValve</a>) so that
530 the SSL session ID is visible to Tomcat.</li>
531 <li>If Tomcat terminates the SSL connection, it will not be possible to use
532 session replication as the SSL session IDs will be different on each
533 node.</li>
534 </ul>
535 </p>
536
537 <p>
538 To enable SSL session tracking you need to use a context listener to set the
539 tracking mode for the context to be just SSL (if any other tracking mode is
540 enabled, it will be used in preference). It might look something like:
541 <div class="codeBox"><pre><code>
542package org.apache.tomcat.example;
543
544import java.util.EnumSet;
545
546import javax.servlet.ServletContext;
547import javax.servlet.ServletContextEvent;
548import javax.servlet.ServletContextListener;
549import javax.servlet.SessionTrackingMode;
550
551public class SessionTrackingModeListener implements ServletContextListener {
552
553 @Override
554 public void contextDestroyed(ServletContextEvent event) {
555 // Do nothing
556 }
557
558 @Override
559 public void contextInitialized(ServletContextEvent event) {
560 ServletContext context = event.getServletContext();
561 EnumSet&lt;SessionTrackingMode&gt; modes =
562 EnumSet.of(SessionTrackingMode.SSL);
563
564 context.setSessionTrackingModes(modes);
565 }
566
567}
568 </code></pre></div>
569 </p>
570 <p>Note: SSL session tracking is implemented for the BIO and NIO connectors.
571 It is not yet implemented for the APR connector.</p>
572
Hongqing Liufd5ee812014-05-10 16:32:51 +0800573</blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Miscellaneous Tips and Bits"><!--()--></a><a name="Miscellaneous_Tips_and_Bits"><strong>Miscellaneous Tips and Bits</strong></a></font></td></tr><tr><td><blockquote>
574
575<p>To access the SSL session ID from the request, use:<br>
576
577 <code>
刘洪青6266f992017-05-15 21:21:03 +0800578 String sslID = (String)request.getAttribute("javax.servlet.request.ssl_session_id");
Hongqing Liufd5ee812014-05-10 16:32:51 +0800579 </code>
580<br>
581For additional discussion on this area, please see
刘洪青6266f992017-05-15 21:21:03 +0800582<a href="http://bz.apache.org/bugzilla/show_bug.cgi?id=22679">Bugzilla</a>.
Hongqing Liufd5ee812014-05-10 16:32:51 +0800583</p>
584
刘洪青6266f992017-05-15 21:21:03 +0800585 <p>To terminate an SSL session, use:
586 <div class="codeBox"><pre><code>
587// Standard HTTP session invalidation
588session.invalidate();
589
590// Invalidate the SSL Session
591org.apache.tomcat.util.net.SSLSessionManager mgr =
592 (org.apache.tomcat.util.net.SSLSessionManager)
593 request.getAttribute("javax.servlet.request.ssl_session_mgr");
594mgr.invalidateSession();
595
596// Close the connection since the SSL session will be active until the connection
597// is closed
598response.setHeader("Connection", "close");
599 </code></pre></div>
600 Note that this code is Tomcat specific due to the use of the
601 SSLSessionManager class. This is currently only available for the BIO and
602 NIO connectors, not the APR/native connector.
603 </p>
604</blockquote></td></tr></table></td></tr><tr class="noPrint"><td width="20%" valign="top" nowrap class="noPrint"></td><td width="80%" valign="top" align="left"><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="comments_section" id="comments_section"><strong>Comments</strong></a></font></td></tr><tr><td><blockquote><p class="notice"><strong>Notice: </strong>This comments section collects your suggestions
605 on improving documentation for Apache Tomcat.<br><br>
606 If you have trouble and need help, read
607 <a href="http://tomcat.apache.org/findhelp.html">Find Help</a> page
608 and ask your question on the tomcat-users
609 <a href="http://tomcat.apache.org/lists.html">mailing list</a>.
610 Do not ask such questions here. This is not a Q&amp;A section.<br><br>
611 The Apache Comments System is explained <a href="./comments.html">here</a>.
612 Comments may be removed by our moderators if they are either
613 implemented or considered invalid/off-topic.</p><script type="text/javascript"><!--//--><![CDATA[//><!--
614 var comments_shortname = 'tomcat';
615 var comments_identifier = 'http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html';
616 (function(w, d) {
617 if (w.location.hostname.toLowerCase() == "tomcat.apache.org") {
618 d.write('<div id="comments_thread"><\/div>');
619 var s = d.createElement('script');
620 s.type = 'text/javascript';
621 s.async = true;
622 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
623 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
624 }
625 else {
626 d.write('<div id="comments_thread"><strong>Comments are disabled for this page at the moment.<\/strong><\/div>');
627 }
628 })(window, document);
629 //--><!]]></script></blockquote></td></tr></table></td></tr><!--FOOTER SEPARATOR--><tr><td colspan="2"><hr noshade size="1"></td></tr><!--PAGE FOOTER--><tr><td colspan="2"><div align="center"><font color="#525D76" size="-1"><em>
630 Copyright &copy; 1999-2017, Apache Software Foundation
Hongqing Liufd5ee812014-05-10 16:32:51 +0800631 </em></font></div></td></tr></table></body></html>