ssl
Interface Functions for Secure Socket Layer
- Provided by: erlang-manpages (Version: 1:18.3-dfsg-1ubuntu3.1)
- Source: erlang
- Report a bug
Interface Functions for Secure Socket Layer
This module contains interface functions for the SSL/TLS protocol. For detailed information about the supported standards see ssl(7).
The following data types are used in the functions for SSL:
The default socket options are [{mode,list},{packet, 0},{header, 0},{active, true}].
For valid options, see the inet(3erl) and gen_tcp(3erl) manual pages in Kernel.
| {verify_fun, {fun(), term()}}
| {fail_if_no_peer_cert, boolean()}
| {depth, integer()}
| {cert, public_key:der_encoded()}
| {certfile, path()}
| {key, {'RSAPrivateKey'| 'DSAPrivateKey' | 'ECPrivateKey' | 'PrivateKeyInfo', public_key:der_encoded()}}
| {keyfile, path()}
| {password, string()}
| {cacerts, [public_key:der_encoded()]}
| {cacertfile, path()}
| {dh, public_key:der_encoded()}
| {dhfile, path()}
| {ciphers, ciphers()}
| {user_lookup_fun, {fun(), term()}}, {psk_identity, string()}, {srp_identity, {string(), string()}}
| {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}
| {sni_hosts, [{hostname(), ssloptions()}]}
| {sni_fun, SNIfun::fun()}
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, behave as gen_tcp, and 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 calls inet directly.
Used in socket data message.
Used in socket close message.
Represents a file path.
ASN.1 DER-encoded entity as an Erlang binary.
According to old API.
The following options have the same meaning in the client and the server:
fun(OtpCert :: #'OTPCertificate'{}, Event :: {bad_cert, Reason :: atom() | {revoked,
atom()}} |
{extension, #'Extension'{}}, InitialUserState :: term()) ->
{valid, UserState :: term()} | {valid_peer, UserState :: term()} |
{fail, Reason :: term()} | {unknown, UserState :: term()}.
The verification fun is called during the X509-path validation when an error or an extension unknown to the SSL application is encountered. It is also called when a certificate is considered valid by the path validation to allow access to each certificate in the path to the user application. It differentiates between the peer certificate and the CA certificates by using valid_peer or valid as second argument to the verification fun. See the public_key User's Guide for definition of #'OTPCertificate'{} and #'Extension'{}.
Note that if the fun returns unknown for an extension marked as critical, validation will fail.
Default option verify_fun in verify_peer mode:
{fun(_,{bad_cert, _} = Reason, _) ->
{fail, Reason};
(_,{extension, _}, UserState) ->
{unknown, UserState};
(_, valid, UserState) ->
{valid, UserState};
(_, valid_peer, UserState) ->
{valid, UserState}
end, []}
Default option verify_fun in mode verify_none:
{fun(_,{bad_cert, _}, UserState) ->
{valid, UserState};
(_,{extension, #'Extension'{critical = true}}, UserState) ->
{valid, UserState};
(_,{extension, _}, UserState) ->
{unknown, UserState};
(_, valid, UserState) ->
{valid, UserState};
(_, valid_peer, UserState) ->
{valid, UserState}
end, []}
The possible path validation errors are given on form {bad_cert, Reason} where Reason is:
peer - check is only performed on the peer certificate.
best_effort - if certificate revocation status can not be determined it will be accepted as valid.
The CA certificates specified for the connection will be used to construct the certificate chain validating the CRLs.
The CRLs will be fetched from a local or external cache see ssl_crl_cache_api(3erl).
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.
For Pre-Shared Key (PSK) cipher suites, the lookup fun is called by the client and server to determine the shared secret. When called by the client, PSKIdentity is set to the hint presented by the server or to undefined. When called by the server, PSKIdentity is the identity presented by the client.
For Secure Remote Password (SRP), the fun is only used by the server to obtain parameters that it uses to generate its session keys. DerivedKey is to be derived according to RFC 2945 and RFC 5054: crypto:sha([Salt, crypto:sha([Username, <<$:>>, Password])])
The following options are client-specific or have a slightly different meaning in the client than in the server:
The list of protocols must not contain an empty binary.
The negotiated protocol can be retrieved using the negotiated_protocol/1 function.
If precedence is server, the negotiated protocol is the first protocol to be shown on the server advertised list, which is also on the client preference list.
If precedence is client, the negotiated protocol is the first protocol to be shown on the client preference list, which is also on the server advertised list.
If the client does not support any of the server advertised protocols or the server does not advertise any protocols, the client falls back to the first protocol in its list or to the default protocol (if a default is supplied). If the server does not support Next Protocol Negotiation, the connection terminates if no default protocol is supplied.
ssl:connect(Host, Port, [...{versions, ['tlsv2', 'tlsv1.1', 'tlsv1', 'sslv3']}])
ssl:connect(Host, Port, [...{versions, [tlsv1.1', 'tlsv1', 'sslv3']}, {fallback, true}])
ssl:connect(Host, Port, [...{versions, ['tlsv1', 'sslv3']}, {fallback, true}])
ssl:connect(Host, Port, [...{versions, ['sslv3']}, {fallback, true}])
may use it to avoid undesired TLS version downgrade. Note that TLS_FALLBACK_SCSV must also be supported by the server for the prevention to work.
The following options are server-specific or have a slightly different meaning in the server than in the client:
The list of protocols is in order of preference. The protocol negotiated will be the first in the list that matches one of the protocols advertised by the client. If no protocol matches, the server will fail the connection with a "no_application_protocol" alert.
The negotiated protocol can be retrieved using the negotiated_protocol/1 function.
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:
A Timeout argument specifies a time-out in milliseconds. The default value for argument Timeout is infinity.
cipher_suites() ->
cipher_suites(Type) -> ciphers()
Types:
Returns a list of supported cipher suites. cipher_suites() is equivalent to cipher_suites(erlang). Type openssl is provided for backwards compatibility with the old SSL, which used OpenSSL. cipher_suites(all) returns all available cipher suites. The cipher suites not present in cipher_suites(erlang) but included in cipher_suites(all) are not used unless explicitly configured by the user.
clear_pem_cache() -> ok
PEM files, used by ssl API-functions, are cached. The cache is regularly checked to see if any cache entries should be invalidated, however this function provides a way to unconditionally clear the whole cache.
connect(Socket, SslOptions) ->
connect(Socket, SslOptions, Timeout) -> {ok, SslSocket} | {error,
Reason}
Types:
Upgrades a gen_tcp, or equivalent, connected socket to an SSL socket, that is, performs the client-side ssl handshake.
connect(Host, Port, Options) ->
connect(Host, Port, Options, Timeout) -> {ok, SslSocket} | {error,
Reason}
Types:
Opens an SSL connection to Host, Port.
close(SslSocket) -> ok | {error, Reason}
Types:
Closes an SSL connection.
close(SslSocket, How) -> ok | {ok, port()} | {error,
Reason}
Types:
Closes or downgrades an SSL connection. In the latter case the transport connection will be handed over to the NewController process after receiving the TLS close alert from the peer. The returned transport socket will have the following options set: [{active, false}, {packet, 0}, {mode, binary}]
controlling_process(SslSocket, NewOwner) -> ok | {error,
Reason}
Types:
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.
connection_information(SslSocket) -> {ok, Result} | {error,
Reason}
Types:
Returns all relevant information about the connection, ssl options that are undefined will be filtered out.
connection_information(SslSocket, Items) -> {ok, Result} |
{error, Reason}
Types:
Returns the requested information items about the connection, if they are defined.
format_error(Reason) -> string()
Types:
Presents the error returned by an SSL function as a printable string.
getopts(Socket, OptionNames) -> {ok, [socketoption()]} |
{error, Reason}
Types:
Gets the values of the specified socket options.
listen(Port, Options) -> {ok, ListenSocket} | {error,
Reason}
Types:
Creates an SSL listen socket.
negotiated_protocol(Socket) -> {ok, Protocol} | {error,
protocol_not_negotiated}
Types:
Returns the protocol negotiated through ALPN or NPN extensions.
peercert(Socket) -> {ok, Cert} | {error, Reason}
Types:
The peer certificate is returned as a DER-encoded binary. The certificate can be decoded with public_key:pkix_decode_cert/2.
peername(Socket) -> {ok, {Address, Port}} | {error,
Reason}
Types:
Returns the address and port number of the peer.
prf(Socket, Secret, Label, Seed, WantedLength) -> {ok,
binary()} | {error, reason()}
Types:
Uses the Pseudo-Random Function (PRF) of a TLS session to generate extra key material. It either takes user-generated values for Secret and Seed or atoms directing it to use a specific value from the session security parameters.
Can only be used with TLS connections; {error, undefined} is returned for SSLv3 connections.
recv(Socket, Length) ->
recv(Socket, Length, Timeout) -> {ok, Data} | {error, Reason}
Types:
Receives a packet from a socket in passive mode. A closed socket is indicated by return value {error, closed}.
Argument Length is meaningful only when the socket is in mode raw and denotes the number of bytes to read. If Length = 0, all available bytes are returned. If Length > 0, exactly Length bytes are returned, or an error; possibly discarding less than Length bytes of data when the socket gets closed from the other side.
Optional argument Timeout specifies a time-out in milliseconds. The default value is infinity.
renegotiate(Socket) -> ok | {error, Reason}
Types:
Initiates a new handshake. A notable return value is {error, renegotiation_rejected} indicating that the peer refused to go through with the renegotiation, but the connection is still active using the previously negotiated session.
send(Socket, Data) -> ok | {error, Reason}
Types:
Writes Data to Socket.
A notable return value is {error, closed} indicating that the socket is closed.
setopts(Socket, Options) -> ok | {error, Reason}
Types:
Sets options according to Options for socket Socket.
shutdown(Socket, How) -> ok | {error, Reason}
Types:
Immediately closes a socket in one or two directions.
How == write means closing the socket for writing, reading from it is still possible.
To be able to handle that the peer has done a shutdown on the write side, option {exit_on_close, false} is useful.
ssl_accept(Socket) ->
ssl_accept(Socket, Timeout) -> ok | {error, Reason}
Types:
Performs the SSL/TLS server-side handshake.
Socket is a socket as returned by ssl:transport_accept/[1,2]
ssl_accept(Socket, SslOptions) ->
ssl_accept(Socket, SslOptions, Timeout) -> {ok, Socket} | ok | {error,
Reason}
Types:
If Socket is a socket(): upgrades a gen_tcp, or equivalent, socket to an SSL socket, that is, performs the SSL/TLS server-side handshake and returns the SSL socket.
If Socket is an sslsocket(): provides extra SSL/TLS options to those specified in ssl:listen/2 and then performs the SSL/TLS handshake.
sockname(Socket) -> {ok, {Address, Port}} | {error,
Reason}
Types:
Returns the local address and port number of socket Socket.
start() ->
start(Type) -> ok | {error, Reason}
Types:
Starts the SSL application. Default type is temporary.
stop() -> ok
Stops the SSL application.
transport_accept(ListenSocket) ->
transport_accept(ListenSocket, Timeout) -> {ok, NewSocket} | {error,
Reason}
Types:
Accepts an incoming connection request on a listen socket. ListenSocket must be a socket returned from ssl:listen/2. The socket returned is to be passed to ssl:ssl_accept[2,3] to complete handshaking, that is, establishing the SSL/TLS connection.
The accepted socket inherits the options set for ListenSocket in ssl:listen/2.
The default value for Timeout is infinity. If Timeout is specified and no connection is accepted within the given time, {error, timeout} is returned.
versions() -> [versions_info()]
Types:
Returns version information relevant for the SSL application.