
# Chapter IV: TLS handshaking

This chapter describes measuring TLS handshakes.

Without further ado, let's describe our example `main.go` program
and let's use it to better understand how to measure that.

(This file is auto-generated. Do not edit it directly! To apply
changes you need to modify `./internal/tutorial/measurex/chapter04/main.go`.)

## main.go

The initial part of the program is pretty much the same as the one
used in previous chapters, so I will not add further comments.

```Go
package main

import (
	"context"
	"crypto/tls"
	"encoding/json"
	"flag"
	"fmt"
	"time"

	"github.com/ooni/probe-cli/v3/internal/measurex"
	"github.com/ooni/probe-cli/v3/internal/netxlite"
	"github.com/ooni/probe-cli/v3/internal/runtimex"
)

func main() {
	sni := flag.String("sni", "dns.google", "domain to resolver")
	address := flag.String("address", "8.8.4.4:443", "remote endpoint address")
	timeout := flag.Duration("timeout", 60*time.Second, "timeout to use")
	flag.Parse()
	ctx, cancel := context.WithTimeout(context.Background(), *timeout)
	defer cancel()
	mx := measurex.NewMeasurerWithDefaultSettings()
```

### Connecting and handshaking.

We call the `ConnectAndHandshake` method. The arguments
are the context, the address, and a TLS config.

Under the hood, the code will call the TCP connect functionality
we have seen in chapter02, using the address argument. Then, if
successful, it will TLS handshake using the given TLS config.

```Go
	m := mx.TLSConnectAndHandshake(ctx, *address, &tls.Config{
		ServerName: *sni,
		NextProtos: []string{"h2", "http/1.1"},
		RootCAs:    netxlite.NewDefaultCertPool(),
	})
```

Regarding the TLS config, in particular,
the three fields above are the files you should always set
in a TLS config when doing handshakes manually. The `ServerName`
field forces the SNI, the NextProtos field forces the ALPN,
and the `RootCAs` field is overridden so that we use the
CA bundle that we bundle with OONI. (This CA bundle is the
same you can find at https://curl.haxx.se/ca/.)

As usual, the method to perform a measurement returns
the measurement itself, which we print below.

```
	data, err := json.Marshal(measurex.NewArchivalEndpointMeasurement(m))
	runtimex.PanicOnError(err, "json.Marshal failed")
	fmt.Printf("%s\n", string(data))
}

```
## Running the example program

As before, let us start off with a vanilla run:

```bash
go run -race ./internal/tutorial/measurex/chapter04 | jq
```

Let us comment the JSON in detail:

```JavaScript
{
  "network": "tcp",
  "address": "8.8.4.4:443",

  // These are the I/O events during the handshake
  "network_events": [
    {
      "address": "8.8.4.4:443",
      "failure": null,
      "num_bytes": 280,
      "operation": "write",
      "proto": "tcp",
      "t": 0.048268333,
      "started": 0.048246666,
      "oddity": ""
    },
    {
      "address": "8.8.4.4:443",
      "failure": null,
      "num_bytes": 517,
      "operation": "read",
      "proto": "tcp",
      "t": 0.086214708,
      "started": 0.048287791,
      "oddity": ""
    },
    {
      "address": "8.8.4.4:443",
      "failure": null,
      "num_bytes": 4303,
      "operation": "read",
      "proto": "tcp",
      "t": 0.087951708,
      "started": 0.08792725,
      "oddity": ""
    },
    {
      "address": "8.8.4.4:443",
      "failure": null,
      "num_bytes": 64,
      "operation": "write",
      "proto": "tcp",
      "t": 0.090097833,
      "started": 0.090083875,
      "oddity": ""
    }
  ],

  // This block is generated when connecting to a TCP
  // socket, as we've already seen in chapter02
  "tcp_connect": [
    {
      "ip": "8.8.4.4",
      "port": 443,
      "t": 0.046022583,
      "status": {
        "blocked": false,
        "failure": null,
        "success": true
      },
      "started": 0.024424916,
      "oddity": ""
    }
  ],

  // This block contains information about the handshake.
  //
  // See https://github.com/ooni/spec/blob/master/data-formats/df-006-tlshandshake.md
  "tls_handshakes": [
    {
      "cipher_suite": "TLS_AES_128_GCM_SHA256",
      "failure": null,
      "negotiated_proto": "h2",
      "tls_version": "TLSv1.3",
      "peer_certificates": [
        {
          "data": " ",
          "format": "base64"
        },
        {
          "data": " ",
          "format": "base64"
        },
        {
          "data": " ",
          "format": "base64"
        }
      ],
      "t": 0.090150666,
      "address": "8.8.4.4:443",
      "server_name": "dns.google",
      "alpn": [
        "h2",
        "http/1.1"
      ],
      "no_tls_verify": false,
      "oddity": "",
      "proto": "tcp",
      "started": 0.04635975
    }
  ]
}
```

### Suggested follow-up experiments

Try to run experiments in the following scenarios, and
check the output JSON to familiarize yourself with what
changes in different error conditions.

1. measurement that causes timeout

2. measurement with wrong SNI

3. measurement with self-signed certificate

4. measurement with expired certificate

5. measurement with connection reset during handshake

6. measurement with timeout during handshake

Here are the commands I used for each proposed exercise:

1. go run -race ./internal/tutorial/measurex/chapter04 -address 8.8.4.4:1 | jq

2. go run -race ./internal/tutorial/measurex/chapter04 -sni example.org | jq

3. go run -race ./internal/tutorial/measurex/chapter04 -address 104.154.89.105:443 -sni self-signed.badssl.com | jq

4. go run -race ./internal/tutorial/measurex/chapter04 -address 104.154.89.105:443 -sni expire.badssl.com | jq

To emulate the last two scenarios, if you're on Linux, a
possibility is building Jafar with this command:

```
go build -v ./internal/cmd/jafar
```

Then, for example, to provoke a connection reset you
can run in a terminal:

```
sudo ./jafar -iptables-reset-keyword dns.google
```

and you can run this tutorial with `dns.google` as
the SNI in another terminal.

Likewise, you can obtain a timeout using the
`-iptables-drop-keyword` flag instead.

(Jafar runs forever and censors. You need to use
`^C` to terminate it from running.)

## Conclusion

We have seen how to measure TLS handshakes. We have seen how
this flow produces a different output on different error conditions.

