---
name: "new_ssl"
section: "3erl"
platform: "linux"
package: "erlang-manpages"
version: "1:12.b.3-1"
release: "Debian 5.0 (Lenny)"
platforms: ["linux"]
url: "https://new.carta.tech/new_ssl"
---

# new\_ssl(3erl)

Interface Functions for Secure Socket Layer

## NAME

ssl - Interface Functions for Secure Socket Layer

## DESCRIPTION

This module contains interface functions to the Secure Socket Layer.

## NEW SSL

This manual page describes functions that are defined in the ssl module and represents the new ssl implementation that coexists with the old one, as the new implementation is not yet compleat enough to replace the old one.

The new implementation can be accessed by providing the option {ssl\_imp, new} to the ssl:connect and ssl:listen functions.

The new implementation is Erlang based and all logic is in Erlang and only payload encryption calculations are done in C via the crypto application. The main reason for making a new implementation is that the old solution was very crippled as the control of the ssl-socket was deep down in openssl making it hard if not impossible to support all inet options, ipv6 and upgrade of a tcp connection to a ssl connection. The alfa version has a few limitations that will be removed before the ssl-4.0 release. Main differences and limitations in the alfa are listed below.

- \*

  New ssl requires the crypto application.

- \*

  The option reuseaddr is supported and the default value is false as in gen\_tcp. Old ssl is patched to accept that the option is set to true to provide a smoother migration between the versions. In old ssl the option is hard coded to true.

- \*

  ssl:version/0 is replaced by ssl:versions/0

- \*

  ssl:ciphers/0 is replaced by ssl:cipher\_suites/0

- \*

  ssl:pid/1 is a meaningless function in new ssl and will be deprecated in ssl-4.0 until it is removed it will return a valid but meaningless pid.

- \*

  New API functions are ssl:shutdown/2, ssl:cipher\_suites/\[0,1\] and ssl:versions/0

- \*

  Diffie-Hellman keyexchange is not supported.

- \*

  Not all inet packet types are supported.

- \*

  CRL and policy certificate extensions are not supported.

- \*

  In this alfa only sslv3 is enabled, although tlsv1 and tlsv1.1 versions are implemented and will be supported in future versions.

- \*

  For security reasons sslv2 is not supported.

## COMMON DATA TYPES

The following data types are used in the functions below:

```
boolean() = true | false
```

```
option() = socketoption() | ssloption()
```

```
socketoption() - [{property(), value()}], property() = atom(),
      value() = term(), defaults to
      [{mode,list},{packet, 0},{header, 0},{active, true}].
```

For valid options see [inet(3erl)](/inet/3erl.md)  and [gen\_tcp(3erl)](/gen_tcp/3erl.md) .

```
ssloption() = {verify, verify_code()} | {depth, integer()} |
      {certfile, path()} | {keyfile, path()} | {password, string()} |
      {cacertfile, path()} | {ciphers, ciphers()} | {ssl_imp, ssl_imp()}
      | {reuse_sessions, boolean()}
```

```
path() = string() - representing a file path.
```

```
verify_code() = 0 | 1 | 2
```

```
host() = hostname() | ipaddress()
```

```
hostname() = string()
```

```
      ip_address() = {N1,N2,N3,N4}  % IPv4
      | {K1,K2,K3,K4,K5,K6,K7,K8}  % IPv6
```

```
sslsocket() - opaque to the user.
```

```
protocol() = sslv3 | tlsv1 | 'tlsv1.1'
```

```
ciphers() = [ciphersuite()] | sting() (according to old API)
```

```
chiphersuite() =
      {key_exchange(), chipher(), hash(), exportable()}
```

```
key_exchange() =  rsa | dh_dss | dh_rsa | dh_anon | dhe_dss
      | dhe_rsa | krb5 | KeyExchange_export
```

```
cipher() = rc4_128 | idea_cbc | des_cbc | '3des_ede_cbc'
      des40_cbc | dh_dss | aes_128_cbc | aes_256_cbc |
      rc2_cbc_40 | rc4_40
```

```
hash() = md5 | sha
```

```
exportable() = export | no_export | ignore
```

```
ssl_imp() = new | old - default is old.
```

## SSL OPTION DESCRIPTIONS

- **{verify, verify\_code()}:**

  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.

- **{depth, integer()}:**

  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. 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. The default value is 1.

- **{certfile, path()}:**

  Path to a file containing the user's certificate.

- **{keyfile, path()}:**

  Path to file containing user's private PEM encoded key.

- **{password, string()}:**

  String containing the user's password. Only used if the private keyfile is password protected.

- **{cacertfile, path()}:**

  Path to file containing PEM encoded CA certificates (trusted certificates used for verifying a peer certificate).

- **{ciphers, ciphers()}:**

  The function *ciphers\_suites/0* can be used to find all availabe ciphers.

- **{ssl\_imp, ssl\_imp()}:**

  Specify which ssl implementation you want to use.

- **{reuse\_sessions, boolean()}:**

  Specifies if ssl sessions should be reused when possible.

## GENERAL

When a 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:

- \*

  {ssl, Socket, Data}

- \*

  {ssl\_closed, Socket}

- \*

  {ssl\_error, Socket, Reason}

A *Timeout* argument specifies a timeout in milliseconds. The default value for a *Timeout* argument is *infinity*.

## EXPORTS

**cipher\_suites() -\>**\
 **cipher\_suites(Type) -\> ciphers()**\

- Types

  Type = erlang \| openssl\

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.

**connect(Socket, SslOptions) -\>** \
 **connect(Socket, SslOptions, Timeout) -\> {ok, SslSocket} \| {error, Reason}**\

- Types

  Socket = socket()\
   SslOptions = \[ssloption()\]\
   Timeout = integer() \| infinity\
   SslSocket = sslsocket()\
   Reason = term()\

Upgrades a gen\_tcp, or equivalent, connected socket to a ssl socket e.i performs the client-side ssl handshake.

**connect(Host, Port, Options) -\>**\
 **connect(Host, Port, Options, Timeout) -\> {ok, SslSocket} \| {error, Reason}**\

- Types

  Host = host()\
   Port = integer()\
   Options = \[option()\]\
   Timeout = integer() \| infinity\
   SslSocket = sslsocket()\
   Reason = term()\

Opens an ssl connection to Host, Port.

**close(SslSocket) -\> ok \| {error, Reason}**\

- Types

  SslSocket = sslsocket()\
   Reason = term()\

Close a ssl connection.

**controlling\_process(SslSocket, NewOwner) -\> ok \| {error, Reason}**\

- Types

  SslSocket = sslsocket()\
   NewOwner = pid()\
   Reason = term()\

Assigns a new controlling process to the ssl-socket. A controlling process is the owner of a ssl-socket, and receives all messages from the socket.

**connection\_info(SslSocket) -\> {ok, {ProtocolVersion, CipherSuite}} \| {error, Reason}** \

- Types

  CipherSuite = ciphersuite()\
   ProtocolVersion = protocol()\

Returns the negotiated protocol version and cipher suite.

**getopts(Socket) -\>** \
 **getopts(Socket, OptionNames) -\> {ok, \[socketoption()\]} \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   OptionNames = \[property()\]\

Get the value of the specified socket options, if no options are specified all options are returned.

**listen(Port, Options) -\> {ok, ListenSocket} \| {error, Reason}**\

- Types

  Port = integer()\
   Options = options()\
   ListenSocket = sslsocket()\

Creates a ssl listen socket.

**peercert(Socket) -\>** \
 **peercert(Socket, Opts) -\> {ok, Cert} \| {ok, Subject} \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   Opts = \[\] \| \[pkix\] \| \[ssl\] \| \[pkix, subject\] \| \[ssl, subject\]\
   Cert = term()\
   Subject = term()\

*peercert(Cert)* is equivalent to *peercert(Cert, \[\])*.

The form of the returned certificate depends on the options.

If the options list is empty the certificate is returned as a DER encoded binary.

The options *pkix* and *ssl* implies that the certificate is returned as a parsed ASN.1 structure in the form of an Erlang term.

The *ssl* option gives a more elaborate return structure, with more explicit information. In particular object identifiers are replaced by atoms.

The option *subject* implies that only the subject's distinguished name part of the peer certificate is returned.

**peername(Socket) -\> {ok, {Address, Port}} \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   Address = ipaddress()\
   Port = integer()\

Returns the address and port number of the peer.

**recv(Socket, Length) -\>** \
 **recv(Socket, Length, Timeout) -\> {ok, Data} \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   Length = integer()\
   Timeout = integer()\
   Data = \[char()\] \| binary()\

This function receives a packet from a socket in passive mode. A closed socket is indicated by a return value *{error, closed}*.

The *Length* argument is only meaningful when the socket is in *raw* mode 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.

The optional *Timeout* parameter specifies a timeout in milliseconds. The default value is *infinity*.

**send(Socket, Data) -\> ok \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   Data = iolist() \| binary()\

Writes *Data* to *Socket*.

A notable return value is *{error, closed}* indicating that the socket is closed.

**setopts(Socket, Options) -\> ok \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   Options = \[socketoption\]()\

Sets options according to *Options* for the socket *Socket*.

**shutdown(Socket, How) -\> ok \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   How = read \| write \| read\_write\
   Reason = reason()\

Immediately close 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, the *{exit\_on\_close, false}* option is useful.

**ssl\_accept(ListenSocket) -\>** \
 **ssl\_accept(ListenSocket, Timeout) -\> ok \| {error, Reason}**\

- Types

  ListenSocket = sslsocket()\
   Timeout = integer()\
   Reason = term()\

The *ssl\_accept* function establish the SSL connection on the server side. It should be called directly after *transport\_accept*, in the spawned server-loop.

**ssl\_accept(ListenSocket, SslOptions) -\>** \
 **ssl\_accept(ListenSocket, SslOptions, Timeout) -\> {ok, Socket} \| {error, Reason}**\

- Types

  ListenSocket = socket()\
   SslOptions = ssloptions()\
   Timeout = integer()\
   Reason = term()\

Upgrades a gen\_tcp, or equivalent, socket to a ssl socket e.i performs the ssl server-side handshake.

**sockname(Socket) -\> {ok, {Address, Port}} \| {error, Reason}**\

- Types

  Socket = sslsocket()\
   Address = ipaddress()\
   Port = integer()\

Returns the local address and port number of the socket *Socket*.

**start() -\>** \
 **start(Type) -\> ok \| {error, Reason}**\

- Types

  Type = permanent \| transient \| temporary\

Starts the Ssl application. Default type is temporary. [application(3erl)](/application/3erl.md)

**stop() -\> ok** \

Stops the Ssl application. [application(3erl)](/application/3erl.md)

**transport\_accept(Socket) -\>**\
 **transport\_accept(Socket, Timeout) -\> {ok, NewSocket} \| {error, Reason}**\

- Types

  Socket = NewSocket = sslsocket()\
   Timeout = integer()\
   Reason = reason()\

Accepts an incoming connection request on a listen socket. *ListenSocket* must be a socket returned from *listen/2*. The socket returned should be passed to *ssl\_accept* to complete ssl handshaking and establishing the connection.

### Warning:

The socket returned can only be used with *ssl\_accept*, no traffic can be sent or received before that call.

The accepted socket inherits the options set for *ListenSocket* in *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() -\> \[{SslAppVer, SupportedSslVer, AvailableSslVsn}\]**\

- Types

  SslAppVer = string()\
   SupportedSslVer = \[protocol()\]\
   AvailableSslVsn = \[protocol()\]\

Returns version information relevant for the ssl application.

## SEE ALSO

[inet(3erl)](/inet/3erl.md)  and [gen\_tcp(3erl)](/gen_tcp/3erl.md)
