package tls

  1. Overview
  2. Docs

Transport layer security

TLS is an implementation of transport layer security in OCaml. TLS is a widely used security protocol which establishes an end-to-end secure channel (with optional (mutual) authentication) between two endpoints. It uses TCP/IP as transport. This library supports all three versions of TLS: 1.2, RFC5246, 1.1, RFC4346, and 1.0, RFC2246. SSL, the previous protocol definition, is not supported.

TLS is algorithmically agile: protocol version, key exchange algorithm, symmetric cipher, and message authentication code are negotiated upon connection.

This library implements several extensions of TLS, AES ciphers, TLS extensions (such as server name indication, SNI), Renegotiation extension, Session Hash and Extended Master Secret Extension.

This library does not contain insecure cipher suites (such as single DES, export ciphers, ...). It does not expose the server time in the server random, requires secure renegotiation.

This library consists of a core, implemented in a purely functional matter (Engine, this module), and effectful parts: Tls_lwt and Tls_mirage.

v0.11.0 - homepage

Abstract state type

type state

The abstract type of a TLS state.

Constructors

val client : Config.client -> state * Cstruct.t

client client is tls * out where tls is the initial state, and out the initial client hello

val server : Config.server -> state

server server is tls where tls is the initial server state

Protocol failures

type error = [
  1. | `AuthenticationFailure of X509.Validation.validation_error
  2. | `NoConfiguredCiphersuite of Ciphersuite.ciphersuite list
  3. | `NoConfiguredVersion of Core.tls_version
  4. | `NoConfiguredHash of Mirage_crypto.Hash.hash list
  5. | `NoMatchingCertificateFound of string
  6. | `NoCertificateConfigured
  7. | `CouldntSelectCertificate
]

failures which can be mitigated by reconfiguration

type fatal = [
  1. | `NoSecureRenegotiation
  2. | `NoCiphersuite of Packet.any_ciphersuite list
  3. | `NoVersion of Core.tls_any_version
  4. | `ReaderError of Reader.error
  5. | `NoCertificateReceived
  6. | `NotRSACertificate
  7. | `NotRSASignature
  8. | `KeyTooSmall
  9. | `RSASignatureMismatch
  10. | `RSASignatureVerificationFailed
  11. | `HashAlgorithmMismatch
  12. | `BadCertificateChain
  13. | `MACMismatch
  14. | `MACUnderflow
  15. | `RecordOverflow of int
  16. | `UnknownRecordVersion of int * int
  17. | `UnknownContentType of int
  18. | `CannotHandleApplicationDataYet
  19. | `NoHeartbeat
  20. | `BadRecordVersion of Core.tls_any_version
  21. | `BadFinished
  22. | `HandshakeFragmentsNotEmpty
  23. | `InvalidDH
  24. | `InvalidRenegotiation
  25. | `InvalidClientHello
  26. | `InvalidServerHello
  27. | `InvalidRenegotiationVersion of Core.tls_version
  28. | `InappropriateFallback
  29. | `UnexpectedCCS
  30. | `UnexpectedHandshake of Core.tls_handshake
  31. | `InvalidCertificateUsage
  32. | `InvalidCertificateExtendedUsage
  33. | `InvalidSession
  34. | `NoApplicationProtocol
]

failures from received garbage or lack of features

type failure = [
  1. | `Error of error
  2. | `Fatal of fatal
]

type of failures

val alert_of_failure : failure -> Packet.alert_type

alert_of_failure failure is alert, the TLS alert type for this failure.

val string_of_failure : failure -> string

string_of_failure failure is string, the string representation of the failure.

val failure_of_sexp : Sexplib.Sexp.t -> failure

failure_of_sexp sexp is failure, the unmarshalled sexp.

val sexp_of_failure : failure -> Sexplib.Sexp.t

sexp_of_failure failure is sexp, the marshalled failure.

Protocol handling

type ret = [
  1. | `Ok of [ `Ok of state | `Eof | `Alert of Packet.alert_type ] * [ `Response of Cstruct.t option ] * [ `Data of Cstruct.t option ]
  2. | `Fail of failure * [ `Response of Cstruct.t ]
]

result type of handle_tls: either failed to handle the incoming buffer (`Fail) with failure and potentially a message to send to the other endpoint, or sucessful operation (`Ok) with a new state, an end of file (`Eof), or an incoming (`Alert). Possibly some `Response to the other endpoint is needed, and potentially some `Data for the application was received.

val handle_tls : state -> Cstruct.t -> ret

handle_tls state buffer is ret, depending on incoming state and buffer, the result is the appropriate ret

val can_handle_appdata : state -> bool

can_handle_appdata state is a predicate which indicates when the connection has already completed a handshake.

val handshake_in_progress : state -> bool

handshake_in_progrss state is a predicate which indicates whether there is a handshake in progress or scheduled.

val send_application_data : state -> Cstruct.t list -> (state * Cstruct.t) option

send_application_data tls outs is (tls' * out) option where tls' is the new tls state, and out the cstruct to send over the wire (encrypted outs).

val send_close_notify : state -> state * Cstruct.t

send_close_notify tls is tls' * out where tls' is the new tls state, and out the (possible encrypted) close notify alert.

val reneg : ?authenticator:X509.Authenticator.t -> ?acceptable_cas:X509.Distinguished_name.t list -> ?cert:Config.own_cert -> state -> (state * Cstruct.t) option

reneg ~authenticator ~acceptable_cas ~cert tls initiates a renegotation on tls, using the provided authenticator. It is tls' * out where tls' is the new tls state, and out either a client hello or hello request (depending on which communication endpoint tls is).

Session information

type epoch = [
  1. | `InitialEpoch
  2. | `Epoch of Core.epoch_data
]

polymorphic variant of session information. The first variant `InitialEpoch will only be used for TLS states without completed handshake. The second variant, `Epoch, contains actual session data.

val epoch_of_sexp : Sexplib.Sexp.t -> epoch

epoch_of_sexp sexp is epoch, the unmarshalled sexp.

val sexp_of_epoch : epoch -> Sexplib.Sexp.t

sexp_of_epoch epoch is sexp, the marshalled epoch.

val epoch : state -> epoch

epoch state is epoch, which contains the session information.

OCaml

Innovation. Community. Security.