<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Secure TCP/IP Connections with SSL</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.6.19 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Server Setup and Operation" HREF="runtime.html"><LINK REL="PREVIOUS" TITLE="Encryption Options" HREF="encryption-options.html"><LINK REL="NEXT" TITLE="Secure TCP/IP Connections with SSH Tunnels" HREF="ssh-tunnels.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2020-09-02T07:27:19"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="4" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.6.19 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="Encryption Options" HREF="encryption-options.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="runtime.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Chapter 18. Server Setup and Operation</TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="Secure TCP/IP Connections with SSH Tunnels" HREF="ssh-tunnels.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="SSL-TCP" >18.9. Secure TCP/IP Connections with SSL</A ></H1 ><P > <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > has native support for using <ACRONYM CLASS="ACRONYM" >SSL</ACRONYM > connections to encrypt client/server communications for increased security. This requires that <SPAN CLASS="PRODUCTNAME" >OpenSSL</SPAN > is installed on both client and server systems and that support in <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > is enabled at build time (see <A HREF="installation.html" >Chapter 16</A >). </P ><P > With <ACRONYM CLASS="ACRONYM" >SSL</ACRONYM > support compiled in, the <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > server can be started with <ACRONYM CLASS="ACRONYM" >SSL</ACRONYM > enabled by setting the parameter <A HREF="runtime-config-connection.html#GUC-SSL" >ssl</A > to <TT CLASS="LITERAL" >on</TT > in <TT CLASS="FILENAME" >postgresql.conf</TT >. The server will listen for both normal and <ACRONYM CLASS="ACRONYM" >SSL</ACRONYM > connections on the same TCP port, and will negotiate with any connecting client on whether to use <ACRONYM CLASS="ACRONYM" >SSL</ACRONYM >. By default, this is at the client's option; see <A HREF="auth-pg-hba-conf.html" >Section 20.1</A > about how to set up the server to require use of <ACRONYM CLASS="ACRONYM" >SSL</ACRONYM > for some or all connections. </P ><P > <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > reads the system-wide <SPAN CLASS="PRODUCTNAME" >OpenSSL</SPAN > configuration file. By default, this file is named <TT CLASS="FILENAME" >openssl.cnf</TT > and is located in the directory reported by <TT CLASS="LITERAL" >openssl version -d</TT >. This default can be overridden by setting environment variable <TT CLASS="ENVAR" >OPENSSL_CONF</TT > to the name of the desired configuration file. </P ><P > <SPAN CLASS="PRODUCTNAME" >OpenSSL</SPAN > supports a wide range of ciphers and authentication algorithms, of varying strength. While a list of ciphers can be specified in the <SPAN CLASS="PRODUCTNAME" >OpenSSL</SPAN > configuration file, you can specify ciphers specifically for use by the database server by modifying <A HREF="runtime-config-connection.html#GUC-SSL-CIPHERS" >ssl_ciphers</A > in <TT CLASS="FILENAME" >postgresql.conf</TT >. </P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > It is possible to have authentication without encryption overhead by using <TT CLASS="LITERAL" >NULL-SHA</TT > or <TT CLASS="LITERAL" >NULL-MD5</TT > ciphers. However, a man-in-the-middle could read and pass communications between client and server. Also, encryption overhead is minimal compared to the overhead of authentication. For these reasons NULL ciphers are not recommended. </P ></BLOCKQUOTE ></DIV ><P > To start in <ACRONYM CLASS="ACRONYM" >SSL</ACRONYM > mode, files containing the server certificate and private key must exist. By default, these files are expected to be named <TT CLASS="FILENAME" >server.crt</TT > and <TT CLASS="FILENAME" >server.key</TT >, respectively, in the server's data directory, but other names and locations can be specified using the configuration parameters <A HREF="runtime-config-connection.html#GUC-SSL-CERT-FILE" >ssl_cert_file</A > and <A HREF="runtime-config-connection.html#GUC-SSL-KEY-FILE" >ssl_key_file</A >. </P ><P > On Unix systems, the permissions on <TT CLASS="FILENAME" >server.key</TT > must disallow any access to world or group; achieve this by the command <TT CLASS="COMMAND" >chmod 0600 server.key</TT >. Alternatively, the file can be owned by root and have group read access (that is, <TT CLASS="LITERAL" >0640</TT > permissions). That setup is intended for installations where certificate and key files are managed by the operating system. The user under which the <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > server runs should then be made a member of the group that has access to those certificate and key files. </P ><P > If the private key is protected with a passphrase, the server will prompt for the passphrase and will not start until it has been entered. </P ><P > The first certificate in <TT CLASS="FILENAME" >server.crt</TT > must be the server's certificate because it must match the server's private key. The certificates of <SPAN CLASS="QUOTE" >"intermediate"</SPAN > certificate authorities can also be appended to the file. Doing this avoids the necessity of storing intermediate certificates on clients, assuming the root and intermediate certificates were created with <TT CLASS="LITERAL" >v3_ca</TT > extensions. This allows easier expiration of intermediate certificates. </P ><P > It is not necessary to add the root certificate to <TT CLASS="FILENAME" >server.crt</TT >. Instead, clients must have the root certificate of the server's certificate chain. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="SSL-CLIENT-CERTIFICATES" >18.9.1. Using Client Certificates</A ></H2 ><P > To require the client to supply a trusted certificate, place certificates of the root certificate authorities (<ACRONYM CLASS="ACRONYM" >CA</ACRONYM >s) you trust in a file in the data directory, set the parameter <A HREF="runtime-config-connection.html#GUC-SSL-CA-FILE" >ssl_ca_file</A > in <TT CLASS="FILENAME" >postgresql.conf</TT > to the new file name, and add the authentication option <TT CLASS="LITERAL" >clientcert=1</TT > to the appropriate <TT CLASS="LITERAL" >hostssl</TT > line(s) in <TT CLASS="FILENAME" >pg_hba.conf</TT >. A certificate will then be requested from the client during SSL connection startup. (See <A HREF="libpq-ssl.html" >Section 32.18</A > for a description of how to set up certificates on the client.) The server will verify that the client's certificate is signed by one of the trusted certificate authorities. </P ><P > Intermediate certificates that chain up to existing root certificates can also appear in the file <TT CLASS="FILENAME" >root.crt</TT > if you wish to avoid storing them on clients (assuming the root and intermediate certificates were created with <TT CLASS="LITERAL" >v3_ca</TT > extensions). Certificate Revocation List (CRL) entries are also checked if the parameter <A HREF="runtime-config-connection.html#GUC-SSL-CRL-FILE" >ssl_crl_file</A > is set. </P ><P > The <TT CLASS="LITERAL" >clientcert</TT > authentication option is available for all authentication methods, but only in <TT CLASS="FILENAME" >pg_hba.conf</TT > lines specified as <TT CLASS="LITERAL" >hostssl</TT >. When <TT CLASS="LITERAL" >clientcert</TT > is not specified or is set to 0, the server will still verify any presented client certificates against its CA file, if one is configured — but it will not insist that a client certificate be presented. </P ><P > If you are setting up client certificates, you may wish to use the <TT CLASS="LITERAL" >cert</TT > authentication method, so that the certificates control user authentication as well as providing connection security. See <A HREF="auth-methods.html#AUTH-CERT" >Section 20.3.9</A > for details. (It is not necessary to specify <TT CLASS="LITERAL" >clientcert=1</TT > explicitly when using the <TT CLASS="LITERAL" >cert</TT > authentication method.) </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="SSL-SERVER-FILES" >18.9.2. SSL Server File Usage</A ></H2 ><P > <A HREF="ssl-tcp.html#SSL-FILE-USAGE" >Table 18-2</A > summarizes the files that are relevant to the SSL setup on the server. (The shown file names are default or typical names. The locally configured names could be different.) </P ><DIV CLASS="TABLE" ><A NAME="SSL-FILE-USAGE" ></A ><P ><B >Table 18-2. SSL Server File Usage</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >File</TH ><TH >Contents</TH ><TH >Effect</TH ></TR ></THEAD ><TBODY ><TR ><TD ><A HREF="runtime-config-connection.html#GUC-SSL-CERT-FILE" >ssl_cert_file</A > (<TT CLASS="FILENAME" >$PGDATA/server.crt</TT >)</TD ><TD >server certificate</TD ><TD >sent to client to indicate server's identity</TD ></TR ><TR ><TD ><A HREF="runtime-config-connection.html#GUC-SSL-KEY-FILE" >ssl_key_file</A > (<TT CLASS="FILENAME" >$PGDATA/server.key</TT >)</TD ><TD >server private key</TD ><TD >proves server certificate was sent by the owner; does not indicate certificate owner is trustworthy</TD ></TR ><TR ><TD ><A HREF="runtime-config-connection.html#GUC-SSL-CA-FILE" >ssl_ca_file</A > (<TT CLASS="FILENAME" >$PGDATA/root.crt</TT >)</TD ><TD >trusted certificate authorities</TD ><TD >checks that client certificate is signed by a trusted certificate authority</TD ></TR ><TR ><TD ><A HREF="runtime-config-connection.html#GUC-SSL-CRL-FILE" >ssl_crl_file</A > (<TT CLASS="FILENAME" >$PGDATA/root.crl</TT >)</TD ><TD >certificates revoked by certificate authorities</TD ><TD >client certificate must not be on this list</TD ></TR ></TBODY ></TABLE ></DIV ><P > The files <TT CLASS="FILENAME" >server.key</TT >, <TT CLASS="FILENAME" >server.crt</TT >, <TT CLASS="FILENAME" >root.crt</TT >, and <TT CLASS="FILENAME" >root.crl</TT > (or their configured alternative names) are only examined during server start; so you must restart the server for changes in them to take effect. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="SSL-CERTIFICATE-CREATION" >18.9.3. Creating Certificates</A ></H2 ><P > To create a simple self-signed certificate for the server, valid for 365 days, use the following <SPAN CLASS="PRODUCTNAME" >OpenSSL</SPAN > command, replacing <TT CLASS="REPLACEABLE" ><I >dbhost.yourdomain.com</I ></TT > with the server's host name: </P><PRE CLASS="PROGRAMLISTING" >openssl req -new -x509 -days 365 -nodes -text -out server.crt \ -keyout server.key -subj "/CN=<TT CLASS="REPLACEABLE" ><I >dbhost.yourdomain.com</I ></TT >"</PRE ><P> Then do: </P><PRE CLASS="PROGRAMLISTING" >chmod og-rwx server.key</PRE ><P> because the server will reject the file if its permissions are more liberal than this. For more details on how to create your server private key and certificate, refer to the <SPAN CLASS="PRODUCTNAME" >OpenSSL</SPAN > documentation. </P ><P > While a self-signed certificate can be used for testing, a certificate signed by a certificate authority (<ACRONYM CLASS="ACRONYM" >CA</ACRONYM >) (usually an enterprise-wide root <ACRONYM CLASS="ACRONYM" >CA</ACRONYM >) should be used in production. </P ><P > To create a server certificate whose identity can be validated by clients, first create a certificate signing request (<ACRONYM CLASS="ACRONYM" >CSR</ACRONYM >) and a public/private key file: </P><PRE CLASS="PROGRAMLISTING" >openssl req -new -nodes -text -out root.csr \ -keyout root.key -subj "/CN=<TT CLASS="REPLACEABLE" ><I >root.yourdomain.com</I ></TT >" chmod og-rwx root.key</PRE ><P> Then, sign the request with the key to create a root certificate authority (using the default <SPAN CLASS="PRODUCTNAME" >OpenSSL</SPAN > configuration file location on <SPAN CLASS="PRODUCTNAME" >Linux</SPAN >): </P><PRE CLASS="PROGRAMLISTING" >openssl x509 -req -in root.csr -text -days 3650 \ -extfile /etc/ssl/openssl.cnf -extensions v3_ca \ -signkey root.key -out root.crt</PRE ><P> Finally, create a server certificate signed by the new root certificate authority: </P><PRE CLASS="PROGRAMLISTING" >openssl req -new -nodes -text -out server.csr \ -keyout server.key -subj "/CN=<TT CLASS="REPLACEABLE" ><I >dbhost.yourdomain.com</I ></TT >" chmod og-rwx server.key openssl x509 -req -in server.csr -text -days 365 \ -CA root.crt -CAkey root.key -CAcreateserial \ -out server.crt</PRE ><P> <TT CLASS="FILENAME" >server.crt</TT > and <TT CLASS="FILENAME" >server.key</TT > should be stored on the server, and <TT CLASS="FILENAME" >root.crt</TT > should be stored on the client so the client can verify that the server's leaf certificate was signed by its trusted root certificate. <TT CLASS="FILENAME" >root.key</TT > should be stored offline for use in creating future certificates. </P ><P > It is also possible to create a chain of trust that includes intermediate certificates: </P><PRE CLASS="PROGRAMLISTING" ># root openssl req -new -nodes -text -out root.csr \ -keyout root.key -subj "/CN=<TT CLASS="REPLACEABLE" ><I >root.yourdomain.com</I ></TT >" chmod og-rwx root.key openssl x509 -req -in root.csr -text -days 3650 \ -extfile /etc/ssl/openssl.cnf -extensions v3_ca \ -signkey root.key -out root.crt # intermediate openssl req -new -nodes -text -out intermediate.csr \ -keyout intermediate.key -subj "/CN=<TT CLASS="REPLACEABLE" ><I >intermediate.yourdomain.com</I ></TT >" chmod og-rwx intermediate.key openssl x509 -req -in intermediate.csr -text -days 1825 \ -extfile /etc/ssl/openssl.cnf -extensions v3_ca \ -CA root.crt -CAkey root.key -CAcreateserial \ -out intermediate.crt # leaf openssl req -new -nodes -text -out server.csr \ -keyout server.key -subj "/CN=<TT CLASS="REPLACEABLE" ><I >dbhost.yourdomain.com</I ></TT >" chmod og-rwx server.key openssl x509 -req -in server.csr -text -days 365 \ -CA intermediate.crt -CAkey intermediate.key -CAcreateserial \ -out server.crt</PRE ><P> <TT CLASS="FILENAME" >server.crt</TT > and <TT CLASS="FILENAME" >intermediate.crt</TT > should be concatenated into a certificate file bundle and stored on the server. <TT CLASS="FILENAME" >server.key</TT > should also be stored on the server. <TT CLASS="FILENAME" >root.crt</TT > should be stored on the client so the client can verify that the server's leaf certificate was signed by a chain of certificates linked to its trusted root certificate. <TT CLASS="FILENAME" >root.key</TT > and <TT CLASS="FILENAME" >intermediate.key</TT > should be stored offline for use in creating future certificates. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="encryption-options.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="ssh-tunnels.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Encryption Options</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="runtime.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Secure TCP/IP Connections with <SPAN CLASS="APPLICATION" >SSH</SPAN > Tunnels</TD ></TR ></TABLE ></DIV ></BODY ></HTML >