<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1999</year><year>2013</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>
    <file>ssl.xml</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>SSL</title>

    <list type="bulleted">
      <item>ssl requires the crypto and public_key applications.</item>
      <item>Supported SSL/TLS-versions are SSL-3.0, TLS-1.0,
      TLS-1.1 and TLS-1.2.</item>
      <item>For security reasons sslv2 is not supported.</item>
      <item>Ephemeral Diffie-Hellman cipher suites are supported
      but not Diffie Hellman Certificates cipher suites.</item>
      <item>Elliptic Curve cipher suites are supported if crypto
      supports it and named curves are used.
      </item>
      <item>Export cipher suites are not supported as the
      U.S. lifted its export restrictions in early 2000.</item>
      <item>IDEA cipher suites are not supported as they have
      become deprecated by the latest TLS spec so there is not any
      real motivation to implement them.</item>
      <item>CRL and policy certificate extensions are not supported
      yet. However CRL verification is supported by public_key, only not integrated
      in ssl yet. </item>
      <item>Support for 'Server Name Indication' extension client side
      (RFC 6066 section 3).</item>
    </list>
 
  </section>
  
  <section>
    <title>COMMON DATA TYPES</title>
    <p>The following data types are used in the functions below:
    </p>

    <p><c>boolean() = true | false</c></p>

    <p><c>option() = socketoption() | ssloption() | transportoption()</c></p>

     <p><c>socketoption() = proplists:property() - The default socket options are
      [{mode,list},{packet, 0},{header, 0},{active, true}].
    </c></p>

    <p>For valid options
      see <seealso marker="kernel:inet">inet(3)</seealso> and
      <seealso marker="kernel:gen_tcp">gen_tcp(3)</seealso>.
    </p>
    
    <p><marker id="type-ssloption"></marker><c>ssloption() = {verify, verify_type()} |
      {verify_fun, {fun(), term()}} |
      {fail_if_no_peer_cert, boolean()}
      {depth, integer()} |
      {cert, der_encoded()}| {certfile, path()} |
      {key, {'RSAPrivateKey'| 'DSAPrivateKey' | 'ECPrivateKey' |'PrivateKeyInfo', der_encoded()}} |
      {keyfile, path()} | {password, string()} |
      {cacerts, [der_encoded()]} | {cacertfile, path()} |
      |{dh, der_encoded()} | {dhfile, path()} | {ciphers, ciphers()} |
      {user_lookup_fun, {fun(), term()}}, {psk_identity, string()}, {srp_identity, {string(), string()}} |
      {ssl_imp, ssl_imp()} | {reuse_sessions, boolean()} | {reuse_session, fun()}
      {next_protocols_advertised, [binary()]} |
      {client_preferred_next_protocols, {client | server, [binary()]} | {client | server, [binary()], binary()}} |
      {log_alert, boolean()} | {server_name_indication, hostname() | disable}
    </c></p>

    <p><c>transportoption() = {cb_info, {CallbackModule::atom(), DataTag::atom(), ClosedTag::atom(), ErrTag:atom()}}
	- defaults to {gen_tcp, tcp, tcp_closed, tcp_error}. Can be used to customize
	the transport layer. The callback module must implement a reliable transport
	protocol and behave as gen_tcp and in addition have functions corresponding to
	inet:setopts/2, inet:getopts/2, inet:peername/1, inet:sockname/1 and inet:port/1.
	The callback gen_tcp is treated specially and will call inet directly.
    </c></p>
    
    <p><c>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CallbackModule =
	atom()</c>
    </p> <p><c>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTag =
	atom() - tag used in socket data message.</c></p>
    <p><c>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ClosedTag = atom() - tag used in
    socket close message.</c></p>

    <p><c>verify_type() = verify_none | verify_peer</c></p>
    
    <p><c>path() = string() - representing a file path.</c></p>

    <p><c>der_encoded() = binary() -Asn1 DER encoded entity as an erlang binary.</c></p>
    
    <p><c>host() = hostname() | ipaddress()</c></p>
        
    <p><c>hostname() = string()</c></p>
    
    <p><c>
      ip_address() = {N1,N2,N3,N4}  % IPv4
      | {K1,K2,K3,K4,K5,K6,K7,K8}  % IPv6    </c></p>

    <p><c>sslsocket() - opaque to the user. </c></p>
    
    <p><c>protocol() = sslv3 | tlsv1 | 'tlsv1.1' | 'tlsv1.2' </c></p>
    
    <p><c>ciphers() = [ciphersuite()] | string() (according to old API)</c></p>
    
    <p><c>ciphersuite() =
      {key_exchange(), cipher(), hash()}</c></p>
    
    <p><c>key_exchange() =  rsa | dhe_dss | dhe_rsa | dh_anon
       | psk | dhe_psk | rsa_psk | srp_anon | srp_dss | srp_rsa
       | ecdh_anon | ecdh_ecdsa | ecdhe_ecdsa | ecdh_rsa | ecdhe_rsa
    </c></p>

   <p><c>cipher() = rc4_128 | des_cbc | '3des_ede_cbc'
      | aes_128_cbc | aes_256_cbc </c></p>

   <p> <c>hash() = md5 | sha
    </c></p>

    <p><c>prf_random() =  client_random | server_random
    </c></p>

   <p><c>srp_param_type() = srp_1024 | srp_1536 | srp_2048 | srp_3072
      | srp_4096 | srp_6144 | srp_8192</c></p>

  </section>

  <section>
    <title>SSL OPTION DESCRIPTIONS - COMMON for SERVER and CLIENT</title>

    <p>Options described here are options that are have the same
    meaning in the client and the server.
    </p>
    
    <taglist>

      <tag>{cert, der_encoded()}</tag>
      <item> The DER encoded users certificate. If this option
      is supplied it will override the certfile option.</item>
      
      <tag>{certfile, path()}</tag>
      <item>Path to a file containing the user's certificate.</item>
      
      <tag>{key, {'RSAPrivateKey'| 'DSAPrivateKey' | 'ECPrivateKey' |'PrivateKeyInfo', der_encoded()}}</tag>
      <item> The DER encoded users private key. If this option
      is supplied it will override the keyfile option.</item>
      
      <tag>{keyfile, path()}</tag>
      <item>Path to file containing user's
      private PEM encoded key. As PEM-files may contain several
      entries this option defaults to the same file as given by
      certfile option.</item>

      <tag>{password, string()}</tag>
      <item>String containing the user's password.
	Only used if the private keyfile is password protected.
      </item>

      <tag>{cacerts, [der_encoded()]}</tag>
      <item> The DER encoded trusted certificates. If this option
      is supplied it will override the cacertfile option.</item>

      <tag>{cacertfile, path()}</tag>
      <item>Path to file containing PEM encoded
      CA certificates (trusted certificates used for verifying a peer
      certificate). May be omitted if you do not want to verify
      the peer.</item>

      <tag>{ciphers, ciphers()}</tag>
      <item>The cipher suites that should be supported. The function
      <c>cipher_suites/0</c> can be used to find all ciphers that are
      supported by default.  <c>cipher_suites(all)</c> may be called
      to find all available cipher suites.   
      Pre-Shared Key (<url href="http://www.ietf.org/rfc/rfc4279.txt">RFC 4279</url> and
      <url href="http://www.ietf.org/rfc/rfc5487.txt">RFC 5487</url>), 
      Secure Remote Password (<url href="http://www.ietf.org/rfc/rfc5054.txt">RFC 5054</url>) 
      and anonymous cipher suites only work if explicitly enabled by
      this option and they are supported/enabled by the peer also.
      Note that anonymous cipher suites are supported for testing purposes
      only and should not be used when security matters.
      </item>

      <tag>{ssl_imp, new | old}</tag>
      <item>No longer has any meaning as the old implementation has
      been removed, it will be ignored.
      </item>

      <tag>{secure_renegotiate, boolean()}</tag>
      <item>Specifies if to reject renegotiation attempt that does
      not live up to <url href="http://www.ietf.org/rfc/rfc5746.txt">RFC 5746</url>. By default secure_renegotiate is
      set to false i.e. secure renegotiation will be used if possible
      but it will fallback to unsecure renegotiation if the peer
      does not support <url href="http://www.ietf.org/rfc/rfc5746.txt">RFC 5746</url>.
      </item>

      <tag>{depth, integer()}</tag>
      <item>
	The depth is the maximum number of non-self-issued
	intermediate certificates that may follow the peer certificate
	in a valid certification path.  So if depth is 0 the PEER must
	be signed by the trusted ROOT-CA directly, if 1 the path can
	be PEER, CA, ROOT-CA, if it is 2 PEER, CA, CA, ROOT-CA and so
	on.  The default value is 1.
      </item>

      <tag>{verify_fun, {Verifyfun :: fun(), InitialUserState :: term()}}</tag>
      <item>
	<p>The verification fun should be defined as:</p>

	<code>
fun(OtpCert :: #'OTPCertificate'{}, Event :: {bad_cert, Reason :: atom()} |
	     {extension, #'Extension'{}}, InitialUserState :: term()) ->
	{valid, UserState :: term()} | {valid_peer, UserState :: term()} |
	{fail, Reason :: term()} | {unknown, UserState :: term()}.
	</code>

	<p>The verify fun will be called during the X509-path
	validation when an error or an extension unknown to the ssl
	application is encountered. Additionally it will be called
	when a certificate is considered valid by the path validation
	to allow access to each certificate in the path to the user
	application. Note that it will differentiate between the
	peer certificate and CA certificates by using valid_peer or
	valid as the second argument to the verify fun.  See <seealso
	marker="public_key:cert_records">the public_key User's
	Guide</seealso> for definition of #'OTPCertificate'{} and
	#'Extension'{}.</p>

	<p>If the verify callback fun returns {fail, Reason}, the
	verification process is immediately stopped and an alert is
	sent to the peer and the TLS/SSL handshake is terminated. If
	the verify callback fun returns {valid, UserState}, the
	verification process is continued.  If the verify callback fun
	always returns {valid, UserState}, the TLS/SSL handshake will
	not be terminated with respect to verification failures and
	the connection will be established. If called with an
	extension unknown to the user application the return value
	{unknown, UserState} should be used.</p>

	<p>The default verify_fun option in verify_peer mode:</p>

      <code>
{fun(_,{bad_cert, _} = Reason, _) ->
	 {fail, Reason};
    (_,{extension, _}, UserState) ->
	 {unknown, UserState};
    (_, valid, UserState) ->
	 {valid, UserState};
    (_, valid_peer, UserState) ->
         {valid, UserState}
 end, []}
      </code>

      <p>The default verify_fun option in verify_none mode:</p>

       <code>
{fun(_,{bad_cert, _}, UserState) ->
	 {valid, UserState};
    (_,{extension, _}, UserState) ->
	 {unknown, UserState};
    (_, valid, UserState) ->
	 {valid, UserState};
    (_, valid_peer, UserState) ->
         {valid, UserState}
 end, []}
      </code>

<p>Possible path validation errors: </p>

<p> {bad_cert, cert_expired}, {bad_cert, invalid_issuer}, {bad_cert, invalid_signature}, {bad_cert, unknown_ca},{bad_cert, selfsigned_peer}, {bad_cert, name_not_permitted}, {bad_cert, missing_basic_constraint}, {bad_cert, invalid_key_usage}</p>
      </item>

      <tag>{versions, [protocol()]}</tag>
      <item>TLS protocol versions that will be supported by started clients and servers.
      This option overrides the application environment option <c>protocol_version</c>. If the
      environment option is not set it defaults to all versions supported by the SSL application. See also
      <seealso marker="ssl:ssl_app">ssl(6)</seealso>
      </item>

      <tag>{hibernate_after, integer()|undefined}</tag>
      <item>When an integer-value is specified, the <c>ssl_connection</c>
            will go into hibernation after the specified number of milliseconds
            of inactivity, thus reducing its memory footprint. When
            <c>undefined</c> is specified (this is the default), the process
            will never go into hibernation.
      </item>

      <tag>{user_lookup_fun, {Lookupfun :: fun(), UserState :: term()}}</tag>
      <item>
	<p>The lookup fun should be defined as:</p>
	<code>
fun(psk, PSKIdentity ::string(), UserState :: term()) ->
	{ok, SharedSecret :: binary()} | error;
fun(srp, Username :: string(), UserState :: term()) ->
	{ok, {SRPParams :: srp_param_type(), Salt :: binary(), DerivedKey :: binary()}} | error.
	</code>

	<p>For Pre-Shared Key (PSK) cipher suites, the lookup fun will
	be called by the client and server to determine the shared
	secret. When called by the client, PSKIdentity will be set to the
	hint presented by the server or undefined. When called by the
	server, PSKIdentity is the identity presented by the client.
	</p>

	<p>For Secure Remote Password (SRP), the fun will only be used by the server to obtain
	parameters that it will use to generate its session keys. <c>DerivedKey</c> should be 
	derived according to <url href="http://tools.ietf.org/html/rfc2945#section-3"> RFC 2945</url> and
	 <url href="http://tools.ietf.org/html/rfc5054#section-2.4"> RFC 5054</url>:
	<c>crypto:sha([Salt, crypto:sha([Username, &lt;&lt;$:&gt;&gt;, Password])]) </c>
	</p>
      </item>

    </taglist>

  </section>

   <section>
    <title>SSL OPTION DESCRIPTIONS - CLIENT SIDE</title>

    <p>Options described here are client specific or has a slightly different
    meaning in the client than in the server.</p>

    <taglist>
      <tag>{verify, verify_type()}</tag>
      <item> In verify_none mode the default behavior will be to
      allow all x509-path validation errors. See also the verify_fun
      option.
      </item>
      <tag>{reuse_sessions, boolean()}</tag>
      <item>Specifies if client should try to reuse sessions
      when possible.
      </item>

      <tag>{client_preferred_next_protocols, {Precedence :: server | client, ClientPrefs :: [binary()]}}</tag>
      <tag>{client_preferred_next_protocols, {Precedence :: server | client, ClientPrefs :: [binary()], Default :: binary()}}</tag>
	   <item>
	   <p>Indicates the client will try to perform Next Protocol
	   Negotiation.</p>

	   <p>If precedence is server the negotiated protocol will be the
	   first protocol that appears on the server advertised list that is
	   also on the client preference list.</p>

	   <p>If precedence is client the negotiated protocol will be the
	   first protocol that appears on the client preference list that is
	   also on the server advertised list.</p>

	   <p>If the client does not support any of the server advertised
	   protocols or the server does not advertise any protocols the
	   client will fallback to the first protocol in its list or if a
	   default is supplied it will fallback to that instead. If the
	   server does not support Next Protocol Negotiation the
	   connection will be aborted if no default protocol is supplied.</p>
	   </item>

      <tag>{psk_identity, string()}</tag>
      <item>Specifies the identity the client presents to the server. The matching secret is
      found by calling the user_look_fun.
      </item>
      <tag>{srp_identity, {Username :: string(), Password :: string()}</tag>
      <item>Specifies the Username and Password to use to authenticate to the server.
      </item>
      <tag>{server_name_indication, hostname()}</tag>
      <tag>{server_name_indication, disable}</tag>
      <item>
        <p>This option can be specified when upgrading a TCP socket to a TLS
        socket to use the TLS Server Name Indication extension.</p>
        <p>When starting a TLS connection without upgrade the Server Name
        Indication extension will be sent if possible, this option may also be
        used to disable that behavior.</p>
      </item>
    </taglist>
   </section>

  <section>
    <title>SSL OPTION DESCRIPTIONS - SERVER SIDE</title>

    <p>Options described here are server specific or has a slightly different
    meaning in the server than in the client.</p>

    <taglist>

      <tag>{dh, der_encoded()}</tag>
      <item>The DER encoded Diffie Hellman parameters. If this option
      is supplied it will override the dhfile option.
      </item>

      <tag>{dhfile, path()}</tag>
      <item>Path to file containing PEM encoded Diffie Hellman parameters,
      for the server to use if a cipher suite using Diffie Hellman key exchange
      is negotiated. If not specified default parameters will be used.
      </item>

      <tag>{verify, verify_type()}</tag>
      <item>Servers only do the x509-path validation in verify_peer
      mode, as it then will send a certificate request to the client
      (this message is not sent if the verify option is verify_none)
      and you may then also want to specify the option
      fail_if_no_peer_cert.
      </item>

      <tag>{fail_if_no_peer_cert, boolean()}</tag>
      <item>Used together with {verify, verify_peer} by an ssl server.
      If set to true, the server will fail if the client does not have
      a certificate to send, i.e. sends a empty certificate, if set to
      false it will only fail if the client sends an invalid
      certificate (an empty certificate is considered valid).
      </item>

      <tag>{reuse_sessions, boolean()}</tag>
      <item>Specifies if the server should agree to reuse sessions
      when the clients request to do so. See also the reuse_session
      option.
      </item>

      <tag>{reuse_session, fun(SuggestedSessionId,
      PeerCert, Compression, CipherSuite) -> boolean()}</tag>
      <item>Enables the ssl server to have a local policy
      for deciding if a session should be reused or not,
      only meaningful if <c>reuse_sessions</c> is set to true.
      SuggestedSessionId is a binary(),  PeerCert is a DER encoded
      certificate, Compression is an enumeration integer
      and CipherSuite is of type ciphersuite().
    </item>

      <tag>{next_protocols_advertised, Protocols :: [binary()]}</tag>
      <item>The list of protocols to send to the client if the client indicates
      it supports the Next Protocol extension. The client may select a protocol
      that is not on this list. The list of protocols must not contain an empty
      binary. If the server negotiates a Next Protocol it can be accessed
      using <c>negotiated_next_protocol/1</c> method.
      </item>

      <tag>{psk_identity, string()}</tag>
      <item>Specifies the server identity hint the server presents to the client.
      </item>
      <tag>{log_alert, boolean()}</tag>
      <item>If false, error reports will not be displayed.</item>
    </taglist>
  </section>
  
  <section>
    <title>General</title>
      
    <p>When an ssl 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>{ssl, Socket, Data}
      </item>
      <item>{ssl_closed, Socket}
      </item>
      <item>
        {ssl_error, Socket, Reason}
      </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>
  </section>
  
  <funcs>
    <func>
      <name>cipher_suites() -></name>
      <name>cipher_suites(Type) -> ciphers()</name>
      <fsummary> Returns a list of supported cipher suites</fsummary>
      <type>
        <v>Type = erlang | openssl | all</v>

      </type>
      <desc><p>Returns a list of supported cipher suites.
	cipher_suites() is equivalent to cipher_suites(erlang).
	Type openssl is provided for backwards compatibility with
	old ssl that used openssl. cipher_suites(all) returns
	all available cipher suites. The cipher suites not present
	in cipher_suites(erlang) but in included in cipher_suites(all)
	will not be used unless explicitly configured by the user.
	</p>
    </desc>
    </func>
    
    <func>
      <name>connect(Socket, SslOptions) -> </name>
      <name>connect(Socket, SslOptions, Timeout) -> {ok, SslSocket}
	| {error, Reason}</name>
      <fsummary> Upgrades a gen_tcp, or
	equivalent, connected socket to an ssl socket. </fsummary>
      <type>
        <v>Socket = socket()</v>
        <v>SslOptions = [ssloption()]</v>
	<v>Timeout = integer() | infinity</v>
	<v>SslSocket = sslsocket()</v>
	<v>Reason = term()</v>
      </type>
      <desc> <p>Upgrades a gen_tcp, or equivalent,
	  connected socket to an ssl socket i.e. performs the
	  client-side ssl handshake.</p>
    </desc>
    </func>

    <func>
      <name>connect(Host, Port, Options) -></name>
      <name>connect(Host, Port, Options, Timeout) ->
	  {ok, SslSocket} | {error, Reason}</name>
      <fsummary>Opens an ssl connection to Host, Port. </fsummary>
      <type>
	  <v>Host = host()</v>
	  <v>Port = integer()</v>
	  <v>Options = [option()]</v>
	  <v>Timeout = integer() | infinity</v>
	  <v>SslSocket = sslsocket()</v>
	  <v>Reason = term()</v>
      </type>
      <desc> <p>Opens an ssl connection to Host, Port.</p> </desc>
    </func>

    <func>
      <name>close(SslSocket) -> ok | {error, Reason}</name>
      <fsummary>Close an ssl connection</fsummary>
      <type>
	  <v>SslSocket = sslsocket()</v>
	  <v>Reason = term()</v>
      </type>
      <desc><p>Close an ssl connection.</p>
      </desc>
    </func>

    <func>
      <name>controlling_process(SslSocket, NewOwner) ->
	ok | {error, Reason}</name>
      
	<fsummary>Assigns a new controlling process to the
	ssl-socket.</fsummary>
      
	<type>
	  <v>SslSocket = sslsocket()</v>
	  <v>NewOwner = pid()</v>
	  <v>Reason = term()</v>
	</type>
	<desc><p>Assigns a new controlling process to the ssl-socket. A
      controlling process is the owner of an ssl-socket, and receives
      all messages from the socket.</p>
      </desc>
    </func>

    <func>
	<name>connection_info(SslSocket) ->
	  {ok, {ProtocolVersion, CipherSuite}} |  {error, Reason} </name>
      <fsummary>Returns the negotiated protocol version and cipher suite.
      </fsummary>
      <type>
        <v>CipherSuite = ciphersuite()</v>
        <v>ProtocolVersion = protocol()</v>
      </type>
      <desc><p>Returns the negotiated protocol version and cipher suite.</p> 
      </desc>
    </func>

     <func>
      <name>format_error(Reason) -> string()</name>
      <fsummary>Return an error string.</fsummary>
      <type>
        <v>Reason = term()</v>
      </type>
      <desc>
        <p>Presents the error returned by an ssl function as a printable string.</p>
      </desc>
    </func>
   
    <func>
      <name>getopts(Socket, OptionNames) ->
	{ok, [socketoption()]} | {error, Reason}</name>
      <fsummary>Get the value of the specified options.</fsummary>
      <type>
	<v>Socket = sslsocket()</v>
	<v>OptionNames = [atom()]</v>
      </type>
      <desc>
	<p>Get the value of the specified socket options.
	</p>
      </desc>
    </func>

    <func>
      <name>listen(Port, Options) ->
	{ok, ListenSocket} | {error, Reason}</name>
      <fsummary>Creates an ssl listen socket.</fsummary>
      <type>
	<v>Port = integer()</v>
	<v>Options = options()</v>
	<v>ListenSocket = sslsocket()</v>
      </type>
      <desc>
	<p>Creates an ssl listen socket.</p>
      </desc>
    </func>

    <func>
      <name>peercert(Socket) -> {ok, Cert} | {error, Reason}</name>
      <fsummary>Return the peer certificate.</fsummary>
     <type>
        <v>Socket = sslsocket()</v>
        <v>Cert = binary()</v>
      </type>
      <desc>
        <p>The peer certificate is returned as a DER encoded binary.
	  The certificate can be decoded with <c>public_key:pkix_decode_cert/2</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>recv(Socket, Length) -> </name>
      <name>recv(Socket, Length, Timeout) -> {ok, Data} | {error,
	Reason}</name>
      <fsummary>Receive data on a socket.</fsummary>
      <type>
        <v>Socket = sslsocket()</v>
        <v>Length = integer()</v>
        <v>Timeout = integer()</v>
        <v>Data = [char()] | binary()</v>
      </type>
      <desc>
        <p>This function receives a packet from a socket in passive
          mode. A closed socket is indicated by a return value
          <c>{error, closed}</c>.</p>
        <p>The <c>Length</c> argument is only meaningful when
          the socket is in <c>raw</c> mode and denotes the number of
          bytes to read. If <c>Length</c> = 0, all available bytes are
          returned. If <c>Length</c> &gt; 0, exactly <c>Length</c>
          bytes are returned, or an error; possibly discarding less
          than <c>Length</c> bytes of data when the socket gets closed
          from the other side.</p>
        <p>The optional <c>Timeout</c> parameter specifies a timeout in
          milliseconds. The default value is <c>infinity</c>.</p>
      </desc>
    </func>
    
    <func>
      <name>prf(Socket, Secret, Label, Seed, WantedLength) -> {ok, binary()} | {error, reason()}</name>
      <fsummary>Use a sessions pseudo random function to generate key material.</fsummary>
      <type>
	<v>Socket = sslsocket()</v>
	<v>Secret = binary() | master_secret</v>
	<v>Label = binary()</v>
	<v>Seed = [binary() | prf_random()]</v>
	<v>WantedLength = non_neg_integer()</v>
      </type>
      <desc>
        <p>Use the pseudo random function (PRF) of a TLS session to generate
	  additional key material. It either takes user generated values for
	  <c>Secret</c> and <c>Seed</c> or atoms directing it use a specific
	  value from the session security parameters.</p>
        <p>This function can only be used with TLS connections, <c>{error, undefined}</c>
	  is returned for SSLv3 connections.</p>
      </desc>
    </func>

    <func>
      <name>renegotiate(Socket) -> ok | {error, Reason}</name>
      <fsummary> Initiates a new handshake.</fsummary>
      <type>
	<v>Socket = sslsocket()</v>
      </type>
      <desc><p>Initiates a new handshake. A notable return value is
      <c>{error, renegotiation_rejected}</c> indicating that the peer
      refused to go through with the renegotiation but the connection
      is still active using the previously negotiated session.</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 = iodata()</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>shutdown(Socket, How) -> ok | {error, Reason}</name>
      <fsummary>Immediately close a socket</fsummary>
      <type>
        <v>Socket = sslsocket()</v>
        <v>How = read | write | read_write</v>
        <v>Reason = reason()</v>
      </type>
      <desc>
        <p>Immediately close a socket in one or two directions.</p>
        <p><c>How == write</c> means closing the socket for writing,
          reading from it is still possible.</p>
        <p>To be able to handle that the peer has done a shutdown on
          the write side, the <c>{exit_on_close, false}</c> option
          is useful.</p>
      </desc>
    </func>
    
    <func>
      <name>ssl_accept(ListenSocket) -> </name>
      <name>ssl_accept(ListenSocket, Timeout) -> ok | {error, Reason}</name>
      <fsummary>Perform server-side SSL handshake</fsummary>
      <type>
        <v>ListenSocket = sslsocket()</v>
        <v>Timeout = integer()</v>
        <v>Reason = term()</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>
      </desc>
    </func>

    <func>
      <name>ssl_accept(ListenSocket, SslOptions) -> </name>
      <name>ssl_accept(ListenSocket, SslOptions, Timeout) -> {ok, Socket} | {error, Reason}</name>
      <fsummary>Perform server-side SSL handshake</fsummary>
      <type>
        <v>ListenSocket = socket()</v>
	<v>SslOptions = ssloptions()</v>
        <v>Timeout = integer()</v>
        <v>Reason = term()</v>
      </type>
      <desc>
        <p> Upgrades a gen_tcp, or
	  equivalent, socket to an ssl socket i.e. performs the
	ssl server-side handshake.</p>
	<warning><p>Note that the listen socket should be in {active, false} mode
	before telling the client that the server is ready to upgrade
	and calling this function, otherwise the upgrade may
	or may not succeed depending on timing.</p></warning>
      </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>start() -> </name>
      <name>start(Type) -> ok | {error, Reason}</name>
      <fsummary>Starts the Ssl application. </fsummary>
      <type>
        <v>Type =  permanent | transient | temporary</v>
      </type>
      <desc>
        <p>Starts the Ssl application. Default type
          is temporary.
          <seealso marker="kernel:application">application(3)</seealso></p>
      </desc>
    </func>
    <func>
      <name>stop() -> ok </name>
      <fsummary>Stops the Ssl application.</fsummary>
      <desc>
        <p>Stops the Ssl application.
          <seealso marker="kernel:application">application(3)</seealso></p>
      </desc>
    </func>

    <func>
      <name>transport_accept(Socket) -></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 = reason()</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>versions() ->
	[{SslAppVer, SupportedSslVer, AvailableSslVsn}]</name>
      <fsummary>Returns version information relevant for the
	ssl application.</fsummary>
      <type>
	<v>SslAppVer = string()</v>
      	<v>SupportedSslVer = [protocol()]</v>
      	<v>AvailableSslVsn = [protocol()]</v>
      </type>
      <desc>
	<p>
	  Returns version information relevant for the
	  ssl application.</p>
      </desc>
    </func>
    <func>
      <name>negotiated_next_protocol(Socket) -> {ok, Protocol} | {error, next_protocol_not_negotiated}</name>
      <fsummary>Returns the Next Protocol negotiated.</fsummary>
      <type>
        <v>Socket = sslsocket()</v>
        <v>Protocol = binary()</v>
      </type>
      <desc>
        <p>
          Returns the Next Protocol negotiated.
        </p>
      </desc>
    </func>

  </funcs> 

  <section>
    <title>SEE ALSO</title>
    <p><seealso marker="kernel:inet">inet(3) </seealso> and 
      <seealso marker="kernel:gen_tcp">gen_tcp(3) </seealso>
    </p>
  </section>

</erlref>

