Revision 39350a4c4d646ff97b250c4f59379674c4fb7d31 (click the page title to view the current version)

Behavioral/TLSHandshake

Changes from 39350a4c4d646ff97b250c4f59379674c4fb7d31 to 9b0d959aea55e085083652864435023cc39f0785
---
title: TLS Handshake Model
categories: behavioral
...

This example demonstrates modeling the TLS 1.2 handshake protocol (RFC 5246, section 7.3) between a client and a server, as a product line: anonymous mode and client authentication are features, fixed in each trace.

```
Client                                   Server
ClientHello                  -------->
                                         ServerHello
                                         Certificate*
                                         ServerKeyExchange*
                                         CertificateRequest*
                             <--------   ServerHelloDone
Certificate*
ClientKeyExchange
CertificateVerify*
[ChangeCipherSpec]
Finished                     -------->
                                         [ChangeCipherSpec]
                             <--------   Finished
Application Data             <------->   Application Data
```

The server answers with a protocol_version alert when the client and the server have no version in common, and a fatal alert ends the connection.

```{.clafer .links}
```

# Feature modeling

```clafer
abstract final Feature
abstract final FeatureModel

TLSFeatures : FeatureModel
    annonymousMode : Feature ?
    clientAuthentication : Feature ?
    // the server requests a client certificate only from a known server (no anonymous mode)
    [ clientAuthentication => no annonymousMode ]
```

```clafer
abstract final Domain

enum ProtocolVersion = SSL2_0 | SSL3_0 | TLS1_0 | TLS1_1 | TLS1_2

enum HandshakeStatus = initiated | completed | failed

abstract Port

abstract final Message
```

## Server side messages

```clafer
serverHelloRequestMessage : Message
serverHelloMessage : Message
serverCertificateMessage : Message
serverKeyExchangeMessage : Message
serverCertificateRequestMessage : Message
serverHelloDoneMessage : Message
serverChangeCipherSpecMessage : Message
serverFinishedMessage : Message
protocolMismatch : Message
```

## Client side messages

```clafer
clientHelloMessage : Message
clientCertificateMessage : Message
clientKeyExchangeMessage : Message
certificateVerifyMessage : Message
clientChangeCipherSpecMessage : Message
clientFinishedMessage : Message
```

## Application data

```clafer
applicationDataMessage : Message
```

## Nodes

```clafer
abstract final Node : Domain
    send : Port -> Message
    receive : Port -> Message
    protocolversion -> ProtocolVersion +
    certificate ?
        signing ?


abstract final Server : Node

abstract final Client : Node
```

## Connecting messages

```clafer
[Server.send = Client.receive]
[Server.receive = Client.send]
```

## Concrete client and server

```clafer
OurServer : Server
    [ protocolversion = SSL2_0, SSL3_0, TLS1_2 ]

aClient : Client
    [ certificate ]
    [ protocolversion = TLS1_2 ]
```

## The main protocol

A phase of the channel is left only from its last states. A transition that leaves a phase is a constraint of the channel: as a transition of a state nested in the phase, it would lapse when the phase is left, which is when it should take effect.

```clafer
Protocol : Domain
    final client -> aClient
    final server -> OurServer
    handshakeStatus -> HandshakeStatus

    // The phases of the connection. A phase is left only from its last states, and a
    // transition that leaves a phase is a constraint of the channel: as a transition of a state
    // nested in the phase, it would lapse when the phase is left, which is when it should take
    // effect. The transitions within a phase hold while the phase lasts.
    xor channel
        initial clientHello --> (serverResponse || fatalError)
            [ handshakeStatus = initiated ]
            [ client.send = clientHelloMessage ]

        xor serverResponse
            [ handshakeStatus = initiated ]
            [ initially (serverHello || mismatch) ]

            // no version in common: a protocol_version alert
            mismatch
                [ no (client.protocolversion ** server.protocolversion) ]
                [ server.send = protocolMismatch ]

            // the server picks a version that both support
            serverHello
                [ some (client.protocolversion ** server.protocolversion) ]
                [ server.send = serverHelloMessage ]
            // a known server sends its certificate; an anonymous one its key exchange
            [ serverHello -[ no annonymousMode ]-> serverCertificate ]
            [ serverHello -[ annonymousMode ]-> serverKeyExchange ]

            serverCertificate
                [ no annonymousMode ]
                [ server.send = serverCertificateMessage ]
            [ serverCertificate -[ clientAuthentication ]-> (serverKeyExchange || certificateRequest) ]
            [ serverCertificate -[ no clientAuthentication ]-> (serverKeyExchange || serverHelloDone) ]

            serverKeyExchange
                [ server.send = serverKeyExchangeMessage ]
            [ serverKeyExchange -[ clientAuthentication ]-> certificateRequest ]
            [ serverKeyExchange -[ no clientAuthentication ]-> serverHelloDone ]

            // features: client authentication
            certificateRequest --> serverHelloDone
                [ clientAuthentication ]
                [ server.send = serverCertificateRequestMessage ]

            serverHelloDone
                [ server.send = serverHelloDoneMessage ]

        [ serverResponse -[ !(serverResponse.mismatch || serverResponse.serverHelloDone) ]-> serverResponse ]
        [ serverResponse.mismatch --> fatalError ]
        [ serverResponse.serverHelloDone --> clientResponse ]

        xor clientResponse
            [ handshakeStatus = initiated ]
            // the client sends its certificate when the server requests it
            [ initially (clientAuthentication => clientCertificate) ]
            [ initially (no clientAuthentication => clientKeyExchange) ]

            clientCertificate --> clientKeyExchange
                [ clientAuthentication ]
                [ client.send = clientCertificateMessage ]

            // a client with a certificate that can sign proves that it has the private key
            clientKeyExchange
                [ client.send = clientKeyExchangeMessage ]
            [ clientKeyExchange -[ clientAuthentication && some client.certificate.signing ]-> certificateVerify ]
            [ clientKeyExchange -[ !(clientAuthentication && some client.certificate.signing) ]-> clientChangeCipherSpec ]

            certificateVerify --> clientChangeCipherSpec
                [ client.certificate.signing ]
                [ client.send = certificateVerifyMessage ]

            clientChangeCipherSpec --> clientFinished
                [ client.send = clientChangeCipherSpecMessage ]

            clientFinished
                [ client.send = clientFinishedMessage ]

        [ clientResponse -[ no clientResponse.clientFinished ]-> clientResponse ]
        [ clientResponse.clientFinished --> serverFinish ]

        xor serverFinish
            initial serverChangeCipherSpec --> serverFinished
                [ handshakeStatus = initiated ]
                [ server.send = serverChangeCipherSpecMessage ]

            // both Finished messages complete the handshake
            serverFinished
                [ server.send = serverFinishedMessage ]
                [ handshakeStatus = completed ]

        [ serverFinish -[ no serverFinish.serverFinished ]-> serverFinish ]
        [ serverFinish.serverFinished --> dataTransfer ]

        // the application data flows until a new handshake (renegotiation)
        dataTransfer -->> clientHello
            [ handshakeStatus = completed ]
            [ client.send = applicationDataMessage ]

        // a fatal alert ends the connection
        fatalError --> fatalError
            [ handshakeStatus = failed ]
```

```{.clafer .ide}
```