<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>1999</year><year>2009</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
The contents of this file are subject to the Erlang Public License,
Version 1.1, (the "License"); you may not use this file except in
compliance with the License. You should have received a copy of the
Erlang Public License along with this software. If not, it can be
retrieved online at http://www.erlang.org/.
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
</legalnotice>
<title>ssl</title>
<prepared>Peter Högfeldt</prepared>
<responsible>Peter Högfeldt</responsible>
<docno></docno>
<approved>Peter Högfeldt</approved>
<checked></checked>
<date>2003-03-25</date>
<rev>D</rev>
<file>ssl.sgml</file>
</header>
<module>ssl</module>
<modulesummary>Interface Functions for Secure Socket Layer</modulesummary>
<description>
<p>This module contains interface functions to the Secure Socket Layer.</p>
</description>
<section>
<title>General</title>
<p>There is a new implementation of ssl available in
this module but until it is 100 % complete, so that it can replace
the old implementation in all aspects it will be
described here <seealso marker="new_ssl"> new ssl API </seealso></p>
<p>The reader is advised to also read the <c>ssl(6)</c> manual page
describing the SSL application.
</p>
<warning>
<p>It is strongly advised to seed the random generator after
the ssl application has been started (see <c>seed/1</c>
below), and before any connections are established. Although
the port program interfacing to the ssl libraries does a
"random" seeding of its own in order to make everything work
properly, that seeding is by no means random for the world
since it has a constant value which is known to everyone
reading the source code of the port program.</p>
</warning>
</section>
<section>
<title>Common data types</title>
<p>The following datatypes are used in the functions below:
</p>
<list type="bulleted">
<item>
<p><c>options() = [option()]</c></p>
</item>
<item>
<p><c>option() = socketoption() | ssloption()</c></p>
</item>
<item>
<p><c>socketoption() = {mode, list} | {mode, binary} | binary | {packet, packettype()} | {header, integer()} | {nodelay, boolean()} | {active, activetype()} | {backlog, integer()} | {ip, ipaddress()} | {port, integer()}</c></p>
</item>
<item>
<p><c>ssloption() = {verify, code()} | {depth, depth()} | {certfile, path()} | {keyfile, path()} | {password, string()} | {cacertfile, path()} | {ciphers, string()}</c></p>
</item>
<item>
<p><c>packettype()</c> (see inet(3))</p>
</item>
<item>
<p><c>activetype()</c> (see inet(3))</p>
</item>
<item>
<p><c>reason() = atom() | {atom(), string()}</c></p>
</item>
<item>
<p><c>bytes() = [byte()]</c></p>
</item>
<item>
<p><c>string() = [byte()]</c></p>
</item>
<item>
<p><c>byte() = 0 | 1 | 2 | ... | 255</c></p>
</item>
<item>
<p><c>code() = 0 | 1 | 2</c></p>
</item>
<item>
<p><c>depth() = byte()</c></p>
</item>
<item>
<p><c>address() = hostname() | ipstring() | ipaddress()</c></p>
</item>
<item>
<p><c>ipaddress() = ipstring() | iptuple()</c></p>
</item>
<item>
<p><c>hostname() = string()</c></p>
</item>
<item>
<p><c>ipstring() = string()</c></p>
</item>
<item>
<p><c>iptuple() = {byte(), byte(), byte(), byte()}</c></p>
</item>
<item>
<p><c>sslsocket()</c></p>
</item>
<item>
<p><c>protocol() = sslv2 | sslv3 | tlsv1</c></p>
</item>
<item>
<p><c></c></p>
</item>
</list>
<p>The socket option <c>{backlog, integer()}</c> is for
<c>listen/2</c> only, and the option <c>{port, integer()}</c>
is for <c>connect/3/4</c> only.
</p>
<p>The following socket options are set by default: <c>{mode, list}</c>, <c>{packet, 0}</c>, <c>{header, 0}</c>, <c>{nodelay, false}</c>, <c>{active, true}</c>, <c>{backlog, 5}</c>,
<c>{ip, {0,0,0,0}}</c>, and <c>{port, 0}</c>.
</p>
<p>Note that the options <c>{mode, binary}</c> and <c>binary</c>
are equivalent. Similarly <c>{mode, list}</c> and the absence of
option <c>binary</c> are equivalent.
</p>
<p>The ssl options are for setting specific SSL parameters as follows:
</p>
<list type="bulleted">
<item>
<p><c>{verify, code()}</c> Specifies type of verification:
0 = do not verify peer; 1 = verify peer, 2 = verify peer,
fail if no peer certificate. The default value is 0.
</p>
</item>
<item>
<p><c>{depth, depth()}</c> Specifies the maximum
verification depth, i.e. how far in a chain of certificates
the verification process can proceed before the verification
is considered to fail.
</p>
<p>Peer certificate = 0, CA certificate = 1, higher level CA
certificate = 2, etc. The value 2 thus means that a chain
can at most contain peer cert, CA cert, next CA cert, and an
additional CA cert.
</p>
<p>The default value is 1.
</p>
</item>
<item>
<p><c>{certfile, path()}</c> Path to a file containing the
user's certificate.
chain of PEM encoded certificates.</p>
</item>
<item>
<p><c>{keyfile, path()}</c> Path to file containing user's
private PEM encoded key.</p>
</item>
<item>
<p><c>{password, string()}</c> String containing the user's
password. Only used if the private keyfile is password protected.</p>
</item>
<item>
<p><c>{cacertfile, path()}</c> Path to file containing PEM encoded
CA certificates (trusted certificates used for verifying a peer
certificate).</p>
</item>
<item>
<p><c>{ciphers, string()}</c> String of ciphers as a colon
separated list of ciphers. The function <c>ciphers/0</c> can
be used to find all available ciphers.</p>
</item>
</list>
<p>The type <c>sslsocket()</c> is opaque to the user.
</p>
<p>The owner of a socket is the one that created it by a call to
<c>transport_accept/[1,2]</c>, <c>connect/[3,4]</c>,
or <c>listen/2</c>.
</p>
<p>When a socket is in active mode (the default), data from the
socket is delivered to the owner of the socket in the form of
messages:
</p>
<list type="bulleted">
<item>
<p><c>{ssl, Socket, Data}</c></p>
</item>
<item>
<p><c>{ssl_closed, Socket}</c></p>
</item>
<item>
<p><c>{ssl_error, Socket, Reason}</c></p>
</item>
</list>
<p>A <c>Timeout</c> argument specifies a timeout in milliseconds. The
default value for a <c>Timeout</c> argument is <c>infinity</c>.
</p>
<p>Functions listed below may return the value <c>{error, closed}</c>, which only indicates that the SSL socket is
considered closed for the operation in question. It is for
instance possible to have <c>{error, closed}</c> returned from
an call to <c>send/2</c>, and a subsequent call to <c>recv/3</c>
returning <c>{ok, Data}</c>.
</p>
<p>Hence a return value of <c>{error, closed}</c> must not be
interpreted as if the socket was completely closed. On the
contrary, in order to free all resources occupied by an SSL
socket, <c>close/1</c> must be called, or else the process owning
the socket has to terminate.
</p>
<p>For each SSL socket there is an Erlang process representing the
socket. When a socket is opened, that process links to the
calling client process. Implementations that want to detect
abnormal exits from the socket process by receiving <c>{'EXIT', Pid, Reason}</c> messages, should use the function <c>pid/1</c>
to retrieve the process identifier from the socket, in order to
be able to match exit messages properly.</p>
</section>
<funcs>
<func>
<name>ciphers() -> {ok, string()} | {error, enotstarted}</name>
<fsummary>Get supported ciphers.</fsummary>
<desc>
<p>Returns a string consisting of colon separated cipher
designations that are supported by the current SSL library
implementation.
</p>
<p>The SSL application has to be started to return the string
of ciphers.</p>
</desc>
</func>
<func>
<name>close(Socket) -> ok | {error, Reason}</name>
<fsummary>Close a socket returned by <c>transport_accept/[1,2]</c>, <c>connect/3/4</c>, or <c>listen/2</c>.</fsummary>
<type>
<v>Socket = sslsocket()</v>
</type>
<desc>
<p>Closes a socket returned by <c>transport_accept/[1,2]</c>,
<c>connect/[3,4]</c>, or <c>listen/2</c></p>
</desc>
</func>
<func>
<name>connect(Address, Port, Options) -> {ok, Socket} | {error, Reason}</name>
<name>connect(Address, Port, Options, Timeout) -> {ok, Socket} | {error, Reason}</name>
<fsummary>Connect to <c>Port</c>at <c>Address</c>.</fsummary>
<type>
<v>Address = address()</v>
<v>Port = integer()</v>
<v>Options = [connect_option()]</v>
<v>connect_option() = {mode, list} | {mode, binary} | binary | {packet, packettype()} | {header, integer()} | {nodelay, boolean()} | {active, activetype()} | {ip, ipaddress()} | {port, integer()} | {verify, code()} | {depth, depth()} | {certfile, path()} | {keyfile, path()} | {password, string()} | {cacertfile, path()} | {ciphers, string()}</v>
<v>Timeout = integer()</v>
<v>Socket = sslsocket()</v>
</type>
<desc>
<p>Connects to <c>Port</c> at <c>Address</c>. If the optional
<c>Timeout</c> argument is specified, and a connection could not
be established within the given time, <c>{error, timeout}</c> is
returned. The default value for <c>Timeout</c> is <c>infinity</c>.
</p>
<p>The <c>ip</c> and <c>port</c> options are for binding to a
particular <em>local</em> address and port, respectively.</p>
</desc>
</func>
<func>
<name>connection_info(Socket) -> {ok, {Protocol, Cipher}} | {error, Reason}</name>
<fsummary>Get current protocol version and cipher.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Protocol = protocol()</v>
<v>Cipher = string()</v>
</type>
<desc>
<p>Gets the chosen protocol version and cipher for an established
connection (accepted och connected). </p>
</desc>
</func>
<func>
<name>controlling_process(Socket, NewOwner) -> ok | {error, Reason}</name>
<fsummary>Assign a new controlling process to the socket.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>NewOwner = pid()</v>
</type>
<desc>
<p>Assigns a new controlling process to <c>Socket</c>. A controlling
process is the owner of a socket, and receives all messages from
the socket.</p>
</desc>
</func>
<func>
<name>format_error(ErrorCode) -> string()</name>
<fsummary>Return an error string.</fsummary>
<type>
<v>ErrorCode = term()</v>
</type>
<desc>
<p>Returns a diagnostic string describing an error.</p>
</desc>
</func>
<func>
<name>getopts(Socket, OptionsTags) -> {ok, Options} | {error, Reason}</name>
<fsummary>Get options set for socket</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>OptionTags = [optiontag()]()</v>
</type>
<desc>
<p>Returns the options the tags of which are <c>OptionTags</c> for
for the socket <c>Socket</c>. </p>
</desc>
</func>
<func>
<name>listen(Port, Options) -> {ok, ListenSocket} | {error, Reason}</name>
<fsummary>Set up a socket to listen on a port on the local host.</fsummary>
<type>
<v>Port = integer()</v>
<v>Options = [listen_option()]</v>
<v>listen_option() = {mode, list} | {mode, binary} | binary | {packet, packettype()} | {header, integer()} | {active, activetype()} | {backlog, integer()} | {ip, ipaddress()} | {verify, code()} | {depth, depth()} | {certfile, path()} | {keyfile, path()} | {password, string()} | {cacertfile, path()} | {ciphers, string()}</v>
<v>ListenSocket = sslsocket()</v>
</type>
<desc>
<p>Sets up a socket to listen on port <c>Port</c> at the local host.
If <c>Port</c> is zero, <c>listen/2</c> picks an available port
number (use <c>port/1</c> to retrieve it).
</p>
<p>The listen queue size defaults to 5. If a different value is
wanted, the option <c>{backlog, Size}</c> should be added to the
list of options.
</p>
<p>An empty <c>Options</c> list is considered an error, and
<c>{error, enooptions}</c> is returned.
</p>
<p>The returned <c>ListenSocket</c> can only be used in calls to
<c>transport_accept/[1,2]</c>.</p>
</desc>
</func>
<func>
<name>peercert(Socket) -> </name>
<name>peercert(Socket, Opts) -> {ok, Cert} | {ok, Subject} | {error, Reason}</name>
<fsummary>Return the peer certificate.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Opts = [pkix | ssl | subject]()</v>
<v>Cert = term()()</v>
<v>Subject = term()()</v>
</type>
<desc>
<p><c>peercert(Cert)</c> is equivalent to <c>peercert(Cert, [])</c>.
</p>
<p>The form of the returned certificate depends on the
options.
</p>
<p>If the options list is empty the certificate is returned as
a DER encoded binary.
</p>
<p>The options <c>pkix</c> and <c>ssl</c> implies that the
certificate is returned as a parsed ASN.1 structure in the
form of an Erlang term.
</p>
<p>The <c>ssl</c> option gives a more elaborate return
structure, with more explicit information. In particular
object identifiers are replaced by atoms.
</p>
<p>The options <c>pkix</c>, and <c>ssl</c> are mutually
exclusive.
</p>
<p>The option <c>subject</c> implies that only the subject's
distinguished name part of the peer certificate is returned.
It can only be used together with the option <c>pkix</c> or
the option <c>ssl</c>.</p>
</desc>
</func>
<func>
<name>peername(Socket) -> {ok, {Address, Port}} | {error, Reason}</name>
<fsummary>Return peer address and port.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Address = ipaddress()</v>
<v>Port = integer()</v>
</type>
<desc>
<p>Returns the address and port number of the peer.</p>
</desc>
</func>
<func>
<name>pid(Socket) -> pid()</name>
<fsummary>Return the pid of the socket process.</fsummary>
<type>
<v>Socket = sslsocket()</v>
</type>
<desc>
<p>Returns the pid of the socket process. The returned pid should
only be used for receiving exit messages.</p>
</desc>
</func>
<func>
<name>recv(Socket, Length) -> {ok, Data} | {error, Reason}</name>
<name>recv(Socket, Length, Timeout) -> {ok, Data} | {error, Reason}</name>
<fsummary>Receive data on socket.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Length = integer() >= 0</v>
<v>Timeout = integer()</v>
<v>Data = bytes() | binary()</v>
</type>
<desc>
<p>Receives data on socket <c>Socket</c> when the socket is in
passive mode, i.e. when the option <c>{active, false}</c>
has been specified.
</p>
<p>A notable return value is <c>{error, closed}</c> which
indicates that the socket is closed.
</p>
<p>A positive value of the <c>Length</c> argument is only
valid when the socket is in raw mode (option <c>{packet, 0}</c> is set, and the option <c>binary</c> is <em>not</em>
set); otherwise it should be set to 0, whence all available
bytes are returned.
</p>
<p>If the optional <c>Timeout</c> parameter is specified, and
no data was available within the given time, <c>{error, timeout}</c> is returned. The default value for
<c>Timeout</c> is <c>infinity</c>.</p>
</desc>
</func>
<func>
<name>seed(Data) -> ok | {error, Reason}</name>
<fsummary>Seed the ssl random generator.</fsummary>
<type>
<v>Data = iolist() | binary()</v>
</type>
<desc>
<p>Seeds the ssl random generator.
</p>
<p>It is strongly advised to seed the random generator after
the ssl application has been started, and before any
connections are established. Although the port program
interfacing to the OpenSSL libraries does a "random" seeding
of its own in order to make everything work properly, that
seeding is by no means random for the world since it has a
constant value which is known to everyone reading the source
code of the seeding.
</p>
<p>A notable return value is <c>{error, edata}}</c> indicating that
<c>Data</c> was not a binary nor an iolist.</p>
</desc>
</func>
<func>
<name>send(Socket, Data) -> ok | {error, Reason}</name>
<fsummary>Write data to a socket.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Data = iolist() | binary()</v>
</type>
<desc>
<p>Writes <c>Data</c> to <c>Socket</c>. </p>
<p>A notable return value is <c>{error, closed}</c> indicating that
the socket is closed.</p>
</desc>
</func>
<func>
<name>setopts(Socket, Options) -> ok | {error, Reason}</name>
<fsummary>Set socket options.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Options = [socketoption]()</v>
</type>
<desc>
<p>Sets options according to <c>Options</c> for the socket
<c>Socket</c>. </p>
</desc>
</func>
<func>
<name>ssl_accept(Socket) -> ok | {error, Reason}</name>
<name>ssl_accept(Socket, Timeout) -> ok | {error, Reason}</name>
<fsummary>Perform server-side SSL handshake and key exchange</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Timeout = integer()</v>
<v>Reason = atom()</v>
</type>
<desc>
<p>The <c>ssl_accept</c> function establish the SSL connection
on the server side. It should be called directly after
<c>transport_accept</c>, in the spawned server-loop.</p>
<p>Note that the ssl connection is not complete until <c>ssl_accept</c>
has returned <c>true</c>, and if an error is returned, the socket
is unavailable and for instance <c>close/1</c> will crash.</p>
</desc>
</func>
<func>
<name>sockname(Socket) -> {ok, {Address, Port}} | {error, Reason}</name>
<fsummary>Return the local address and port.</fsummary>
<type>
<v>Socket = sslsocket()</v>
<v>Address = ipaddress()</v>
<v>Port = integer()</v>
</type>
<desc>
<p>Returns the local address and port number of the socket
<c>Socket</c>.</p>
</desc>
</func>
<func>
<name>transport_accept(Socket) -> {ok, NewSocket} | {error, Reason}</name>
<name>transport_accept(Socket, Timeout) -> {ok, NewSocket} | {error, Reason}</name>
<fsummary>Accept an incoming connection and prepare for <c>ssl_accept</c></fsummary>
<type>
<v>Socket = NewSocket = sslsocket()</v>
<v>Timeout = integer()</v>
<v>Reason = atom()</v>
</type>
<desc>
<p>Accepts an incoming connection request on a listen socket.
<c>ListenSocket</c> must be a socket returned from <c>listen/2</c>.
The socket returned should be passed to <c>ssl_accept</c> to
complete ssl handshaking and establishing the connection.</p>
<warning>
<p>The socket returned can only be used with <c>ssl_accept</c>,
no traffic can be sent or received before that call.</p>
</warning>
<p>The accepted socket inherits the options set for <c>ListenSocket</c>
in <c>listen/2</c>.</p>
<p>The default value for <c>Timeout</c> is <c>infinity</c>. If
<c>Timeout</c> is specified, and no connection is accepted within
the given time, <c>{error, timeout}</c> is returned.</p>
</desc>
</func>
<func>
<name>version() -> {ok, {SSLVsn, CompVsn, LibVsn}}</name>
<fsummary>Return the version of SSL.</fsummary>
<type>
<v>SSLVsn = CompVsn = LibVsn = string()()</v>
</type>
<desc>
<p>Returns the SSL application version (<c>SSLVsn</c>), the library
version used when compiling the SSL application port program
(<c>CompVsn</c>), and the actual library version used when
dynamically linking in runtime (<c>LibVsn</c>).
</p>
<p>If the SSL application has not been started, <c>CompVsn</c> and
<c>LibVsn</c> are empty strings.
</p>
</desc>
</func>
</funcs>
<section>
<title>ERRORS</title>
<p>The possible error reasons and the corresponding diagnostic strings
returned by <c>format_error/1</c> are either the same as those defined
in the <c>inet(3)</c> reference manual, or as follows:
</p>
<taglist>
<tag><c>closed</c></tag>
<item>
<p>Connection closed for the operation in question.
</p>
</item>
<tag><c>ebadsocket</c></tag>
<item>
<p>Connection not found (internal error).
</p>
</item>
<tag><c>ebadstate</c></tag>
<item>
<p>Connection not in connect state (internal error).
</p>
</item>
<tag><c>ebrokertype</c></tag>
<item>
<p>Wrong broker type (internal error).
</p>
</item>
<tag><c>ecacertfile</c></tag>
<item>
<p>Own CA certificate file is invalid.
</p>
</item>
<tag><c>ecertfile</c></tag>
<item>
<p>Own certificate file is invalid.
</p>
</item>
<tag><c>echaintoolong</c></tag>
<item>
<p>The chain of certificates provided by peer is too long.
</p>
</item>
<tag><c>ecipher</c></tag>
<item>
<p>Own list of specified ciphers is invalid.
</p>
</item>
<tag><c>ekeyfile</c></tag>
<item>
<p>Own private key file is invalid.
</p>
</item>
<tag><c>ekeymismatch</c></tag>
<item>
<p>Own private key does not match own certificate.
</p>
</item>
<tag><c>enoissuercert</c></tag>
<item>
<p>Cannot find certificate of issuer of certificate provided
by peer.
</p>
</item>
<tag><c>enoservercert</c></tag>
<item>
<p>Attempt to do accept without having set own certificate.
</p>
</item>
<tag><c>enotlistener</c></tag>
<item>
<p>Attempt to accept on a non-listening socket.
</p>
</item>
<tag><c>enoproxysocket</c></tag>
<item>
<p>No proxy socket found (internal error).
</p>
</item>
<tag><c>enooptions</c></tag>
<item>
<p>The list of options is empty.
</p>
</item>
<tag><c>enotstarted</c></tag>
<item>
<p>The SSL application has not been started.
</p>
</item>
<tag><c>eoptions</c></tag>
<item>
<p>Invalid list of options.
</p>
</item>
<tag><c>epeercert</c></tag>
<item>
<p>Certificate provided by peer is in error.
</p>
</item>
<tag><c>epeercertexpired</c></tag>
<item>
<p>Certificate provided by peer has expired.
</p>
</item>
<tag><c>epeercertinvalid</c></tag>
<item>
<p>Certificate provided by peer is invalid.
</p>
</item>
<tag><c>eselfsignedcert</c></tag>
<item>
<p>Certificate provided by peer is self signed.
</p>
</item>
<tag><c>esslaccept</c></tag>
<item>
<p>Server SSL handshake procedure between client and server failed.
</p>
</item>
<tag><c>esslconnect</c></tag>
<item>
<p>Client SSL handshake procedure between client and server failed.
</p>
</item>
<tag><c>esslerrssl</c></tag>
<item>
<p>SSL protocol failure. Typically because of a fatal alert
from peer.
</p>
</item>
<tag><c>ewantconnect</c></tag>
<item>
<p>Protocol wants to connect, which is not supported in
this version of the SSL application.
</p>
</item>
<tag><c>ex509lookup</c></tag>
<item>
<p>Protocol wants X.509 lookup, which is not supported in
this version of the SSL application.
</p>
</item>
<tag><c>{badcall, Call}</c></tag>
<item>
<p>Call not recognized for current mode (active or passive) and
state of socket.
</p>
</item>
<tag><c>{badcast, Cast}</c></tag>
<item>
<p>Call not recognized for current mode (active or passive) and
state of socket.
</p>
</item>
<tag><c>{badinfo, Info}</c></tag>
<item>
<p>Call not recognized for current mode (active or passive) and
state of socket.
</p>
</item>
</taglist>
</section>
<section>
<title>SEE ALSO</title>
<p>gen_tcp(3), inet(3)
</p>
</section>
</erlref>