| 1 |
|
| 2 |
<html><head> |
| 3 |
<title>Securing Traffic Between two Socat Instances Using SSL</title> |
| 4 |
<link rel="stylesheet" type="text/css" href="dest-unreach.css"> |
| 5 |
</head> |
| 6 |
|
| 7 |
<body> |
| 8 |
|
| 9 |
<h1>Securing Traffic Between two Socat Instances Using SSL</h1> |
| 10 |
|
| 11 |
<h2>Introduction</h2> |
| 12 |
<p> |
| 13 |
When you want to connect two socat processes running on different machines and |
| 14 |
feel that you need to protect the connection against unauthorized access, |
| 15 |
sniffing, data manipulation etc., you might want to encrypt the communications. |
| 16 |
</p> |
| 17 |
<p> |
| 18 |
For this purpose socat integrates the OpenSSL library and provides SSL client |
| 19 |
and server features. |
| 20 |
</p> |
| 21 |
<p> |
| 22 |
SSL is a complex protocol that provides much more features than required for |
| 23 |
protecting a single connection; in this document we present only a simple |
| 24 |
scenario that provides just the basic security requirements. |
| 25 |
</p> |
| 26 |
|
| 27 |
|
| 28 |
<h2>Configuring OpenSSL in socat</h2> |
| 29 |
<p> |
| 30 |
This section shows how the SSL addresses can be configured in socat. |
| 31 |
In this docu we only use self signed certificates for the sake of simplicity. |
| 32 |
</p> |
| 33 |
<p>We assume that the server host is called <tt>server.domain.org</tt> and the |
| 34 |
server process uses port 4433. To keep it simple, we use a very simple server |
| 35 |
functionality that just echos data (<tt>echo</tt>), and <tt>stdio</tt> on the |
| 36 |
client.</p> |
| 37 |
<h3>Generate a server certificate</h3> |
| 38 |
|
| 39 |
<p>Perform the following steps on a trusted host where OpenSSL is |
| 40 |
installed. It might as well be the client or server host themselves.</p> |
| 41 |
<p>Prepare a basename for the files related to the server certificate:</p> |
| 42 |
<span class="shell">FILENAME=server</span> |
| 43 |
|
| 44 |
<p>Generate a public/private key pair:</p> |
| 45 |
<span class="shell">openssl genrsa -out $FILENAME.key 2048</span> |
| 46 |
|
| 47 |
<p>Generate a self signed certificate:</p> |
| 48 |
<span class="shell">openssl req -new -key $FILENAME.key -x509 -days 3653 -out $FILENAME.crt</span> |
| 49 |
<p>You will be prompted for your country code, name etc.; you may quit all prompts |
| 50 |
with the ENTER key, except for the Common Name which must be exactly the name or IP address of the server that the client will use.</p> |
| 51 |
<p>Generate the PEM file by just appending the key and certificate files:<p> |
| 52 |
<span class="shell">cat $FILENAME.key $FILENAME.crt >$FILENAME.pem</span> |
| 53 |
|
| 54 |
<p>The files that contain the private key should be kept secret, thus adapt |
| 55 |
their permissions:<p> |
| 56 |
<span class="shell">chmod 600 $FILENAME.key $FILENAME.pem</span> |
| 57 |
|
| 58 |
<p>Now bring the file <tt>server.pem</tt> to the SSL server, e.g. to directory |
| 59 |
<tt>$HOME/etc/</tt>, using a secure channel like USB memory stick or SSH. Keep |
| 60 |
tight permissions on the file even on the target host, and remove all other |
| 61 |
instances of <tt>server.key</tt> and <tt>server.pem</tt>. |
| 62 |
</p> |
| 63 |
<p>Copy the trust certificate server.crt to the SSL client host, e.g. to directory |
| 64 |
<tt>$HOME/etc/</tt>; a secure channel is not required here, and the permissions |
| 65 |
are not critical. |
| 66 |
</p> |
| 67 |
|
| 68 |
<h3>Generate a client certificate</h3> |
| 69 |
<p>First prepare a different basename for the files related to the client certificate:</p> |
| 70 |
<span class="shell">FILENAME=client</span> |
| 71 |
|
| 72 |
<p>Repeat the procedure for certificate generation described above. A special common name is not required. |
| 73 |
Copy <tt>client.pem</tt> to the SSL client, and <tt>client.crt</tt> to the |
| 74 |
server.</p> |
| 75 |
|
| 76 |
<h3>OpenSSL Server</h3> |
| 77 |
|
| 78 |
<p>Instead of using a tcp-listen (tcp-l) address, we use openssl-listen (ssl-l) |
| 79 |
for the server, <tt>cert=...</tt> tells the program to the file containing its |
| 80 |
certificate and private key, and <tt>cafile=...</tt> points to the file |
| 81 |
containing the certificate of the peer; we trust clients only if they can proof |
| 82 |
that they have the related private key (OpenSSL handles this for us):<p> |
| 83 |
<span class="shell">socat \ |
| 84 |
OPENSSL-LISTEN:4433,reuseaddr,cert=$HOME/etc/server.pem,cafile=$HOME/etc/client.crt \ |
| 85 |
PIPE</span> |
| 86 |
<p>After starting this command, socat should be listening on port 4433, but |
| 87 |
will require client authentication.</p> |
| 88 |
|
| 89 |
<h3>OpenSSL Client</h3> |
| 90 |
<p>Substitute your <tt>tcp-connect</tt> or <tt>tcp</tt> address keyword with |
| 91 |
<tt>openssl-connect</tt> or just <tt>ssl</tt> and here too add the |
| 92 |
<tt>cert</tt> and <tt>cafile</tt> options:<p> |
| 93 |
<span class="shell">socat STDIO \ |
| 94 |
OPENSSL-CONNECT:server.domain.org:4433,cert=$HOME/etc/client.pem,cafile=$HOME/etc/server.crt</span> |
| 95 |
<p>This command should establish a secured connection to the server |
| 96 |
process.</p> |
| 97 |
|
| 98 |
<h3>TCP/IP version 6</h3> |
| 99 |
|
| 100 |
<p>If the communication is to go over IPv6, the above described commands have |
| 101 |
to be adapted; <tt>ip6name.domain.org</tt> is assumed to resolve to the IPv6 |
| 102 |
address of the server:</p> |
| 103 |
<p>Server:</p> |
| 104 |
<span class="shell">socat \ |
| 105 |
OPENSSL-LISTEN:4433,<b style="color:yellow">pf=ip6</b>,reuseaddr,cert=$HOME/etc/server.pem,cafile=$HOME/etc/client.crt \ |
| 106 |
PIPE</span> |
| 107 |
|
| 108 |
<p>Client:</p> |
| 109 |
<span class="shell">socat STDIO \ |
| 110 |
OPENSSL-CONNECT:<b style="color:yellow">ip6name</b>.domain.org:4433,cert=$HOME/etc/client.pem,cafile=$HOME/etc/server.crt</span> |
| 111 |
|
| 112 |
<h2>Troubleshooting</h2> |
| 113 |
|
| 114 |
<h3>Test OpenSSL Integration</h3> |
| 115 |
<p> |
| 116 |
If you get error messages like this:</p> |
| 117 |
<span class="error">... E unknown device/address "OPENSSL-LISTEN"</span> |
| 118 |
<p>your socat executable probably does not have the OpenSSL library linked in. |
| 119 |
Check socat's compile time configuration with the following command:</p> |
| 120 |
<span class="shell">socat -V |grep SSL</span> |
| 121 |
<p>Positive output: |
| 122 |
<tt>#define WITH_OPENSSL 1</tt><br> |
| 123 |
Negative output: |
| 124 |
<tt>#undef WITH_OPENSSL</tt><br> |
| 125 |
</p> |
| 126 |
<p> |
| 127 |
In the latter case, make sure you have OpenSSL and its development package |
| 128 |
(include files) installed, and check the run of the configure script. |
| 129 |
</p> |
| 130 |
|
| 131 |
|
| 132 |
<h2>History</h2> |
| 133 |
<p> |
| 134 |
A first OpenSSL client was implemented in socat 1.2.0; it did not support |
| 135 |
client certificates and could not verify server certificates. It was rather |
| 136 |
considered as a tool for probing typical SSL secured Internet services. |
| 137 |
</p> |
| 138 |
<p> |
| 139 |
From version 1.4.0 on, socat provided experimental support for SSL client and |
| 140 |
SSL server, implemented using the OpenSSL libraries. Only TCP/IPv4 transport |
| 141 |
was supported. With both SSL client and server, trust certificates for checking |
| 142 |
the peers authentication, and certificates for authentication could be |
| 143 |
specified. This allowed for non interactive secure connection establishing. |
| 144 |
The features were considered experimental; like most Internet sites, socat |
| 145 |
server did not require the client to present a certificate per default, but the |
| 146 |
client required a server certificate. |
| 147 |
|
| 148 |
</p> |
| 149 |
<p> |
| 150 |
DSA certificate support is implemented since version 1.4.2. |
| 151 |
</p> |
| 152 |
<p> |
| 153 |
Socat version 1.5.0 extended SSL to TCP/IPv6 transports. |
| 154 |
</p> |
| 155 |
<p> |
| 156 |
With socat version 1.6.0, the SSL server per default requires the client to |
| 157 |
present a trusted certificate. socat's OpenSSL implementation still does not |
| 158 |
check the contents of a certificate like host name or host address. |
| 159 |
</p> |
| 160 |
<p> |
| 161 |
Socat 1.7.3.0 introduces check of servers commonname by the client, and optionally check of clients commonname by the server. |
| 162 |
</p> |
| 163 |
|
| 164 |
<p>This document was last modified in Oct. 2023.</p> |
| 165 |
|
| 166 |
<h2>More info about socat OpenSSL</h2> |
| 167 |
|
| 168 |
<h3>Links regarding this tutorial</h3> |
| 169 |
<a href="socat.html#ADDRESS_OPENSSL_CONNECT">address openssl-connect</a><br> |
| 170 |
<a href="socat.html#ADDRESS_OPENSSL_LISTEN">address openssl-listen</a><br> |
| 171 |
<a href="socat.html#OPTION_OPENSSL_CERTIFICATE">option cert</a><br> |
| 172 |
<a href="socat.html#OPTION_OPENSSL_CAFILE">option cafile</a><br> |
| 173 |
|
| 174 |
<h3>More socat options for OpenSSL addresses</h3> |
| 175 |
<a href="socat.html#GROUP_OPENSSL">OpenSSL options</a><br> |
| 176 |
<a href="socat.html#GROUP_TCP">TCP options</a><br> |
| 177 |
<a href="socat.html#GROUP_IP">IP options</a><br> |
| 178 |
<a href="socat.html#GROUP_SOCKET">socket options</a><br> |
| 179 |
<a href="socat.html#GROUP_FD">file descriptor options</a><br> |
| 180 |
<a href="socat.html#GROUP_RETRY">retry options</a><br> |
| 181 |
<p>For openssl-listen only:</p> |
| 182 |
<a href="socat.html#GROUP_LISTEN">listen options</a><br> |
| 183 |
<a href="socat.html#GROUP_CHILD">child options</a><br> |
| 184 |
<a href="socat.html#GROUP_RANGE">range options</a><br> |
| 185 |
|
| 186 |
<h2>References</h2> |
| 187 |
<a href="http://www.dest-unreach.org/socat">socat home page</a><br> |
| 188 |
<a href="socat.html">socat man page</a><br> |
| 189 |
<a href="http://www.openssl.org/">OpenSSL home page</a><br> |
| 190 |
<a href="http://www.stunnel.org/">stunnel home page</a><br> |
| 191 |
<a href="http://en.wikipedia.org/wiki/Secure_Sockets_Layer">secure sockets layer on Wikipedia</a><br> |
| 192 |
|
| 193 |
<p> |
| 194 |
<small>Copyright: Gerhard Rieger 2007</small><br> |
| 195 |
<small>License: <a href="http://www.fsf.org/licensing/licenses/fdl.html">GNU Free Documentation License (FDL)</a></small> |
| 196 |
</p> |
| 197 |
|
| 198 |
</body> |
| 199 |
</html> |