> For the complete documentation index, see [llms.txt](https://phiph.gitbook.io/foundation-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://phiph.gitbook.io/foundation-1/network/ssl.md). Agents can also search and read this documentation through its [MCP server](https://phiph.gitbook.io/foundation-1/~gitbook/mcp).

# TLS/SSL

* Secure transport protocol
* SSL v3 - 1996 - Netscape
* Netscape handed it over to IETF
* IETF renamed it to TLS in 1999

## High level

* Key exchange + shared secret calculation: RSA, DH, DHE, ECDHE
* Key exchange should be authenticated - Server to client: mandatory - Client to server - optional
  * X.509 certificate signed by trusted CA (any signature algorithm can be used), not expired or revoked - contains RSA or ECDSA or EdDSA public key in it - to prove that this is indeed correct server
  * Digital signature of handshake transcript with server's private key - RSA, ECDSA... - signature algorithm should be the same as public key type in a certificate - to prove that server has private key
* Encrypt data with (symmetric algorithm + HMAC authentication) or AEAD ciphers

## Versions

* SSLv1(out of date, vulnerable)
* SSLv2(do not use)
* SSLv3(do not use)
* TLS 1.0(deprecated, many attacks, PCI DSS prohibited) - RFC 2246
* TLS 1.1 - RFC 4346
* TLS 1.2 - is the best option for now - RFC 5246 - secure
* TLS 1.3 - RFC 8446 - secure

## Concepts

* TLS communicates via records, can be several records in one packet TCP packet (one or many IP packets)
* In a nutshell TLS is all about different records. Different records serve different purposes. Records have Content-Type field and Message fields
* Message field will contain the actual message related to a particular Record Protocol type

## SSL Records

* Handshake - Content-Type Code 22
* Application Data - Content-Type Code 23
* Alerts - Content-Type Code 21
* Change Cipher Spec - Content-Type Code

## Handshake

### Tasks

* Negotiation of ciphers and protocols: authenticated encryption algorithms
* Key exchange - main goal actually: algorithm itself + groups + key derivation algorithm
* Authentication of key exchange - digital signature algorithm + hash algorithm - two, for different parts of the handshake - one signature algorithm is used for server certificate signature, the other one for signing key exchange parameters or messages exchanged between the client and server
* Session resumption - to avoid redoing key exchange every time user connects again

### Negotiated parametres

9

* TLS version
* Key Exchange Algorithm
  * In TLS 1.2: specified inside the cipher suite name (e.g. DHE-RSA, ECDHE-ECDSA).
  * In TLS 1.3: always (EC)DHE, but the key exchange group is negotiated separately
* Key exchange group
* 2 Signature algorithms for authenticated key exchange: one for certificate(Not really “negotiated” — the client just needs to support it in its crypto library), one for transcript
* Hash functions to use with signature alorithms: one for certificate(fixed by the cert (e.g. cert signed with sha256WithRSA). Not negotiated.), one for transcript
* Symmetric encryption algorithm
* Symmetric encryption key size
* Symmetric encryption mode
* Hash function - That hash was used inside an HMAC (Hash-based MAC) to generate the record’s authentication tag - In AEAD suites, the “hash” part in the name is gone — because integrity is built into the AEAD mode itself - Not used in TLS 1.3 - plus this hash function is used in key derivation process

### Handshake Stages

* Key Exchange
  * ClientHello
  * ServerHello
* Key Derivation - This derivation process is called the key schedule — it takes the shared key from ECDHE, mixes in random values (nonces), and runs them through a hash-based pseudorandom function (PRF) or HKDF to produce strong, independent session keys. Set of shared secrets:
  * handshake traffic keys
  * application traffic keys
  * MAC keys (if needed)
* Server Parametres
  * The server sends a CertificateRequest message with details about the accepted certificate types, supported signature algorithms, and acceptable certificate authorities (CAs)
  * A server's X.509 certificate chain is sent, including: The server's certificate, Intermediate certificates, if required, Root certificate (often omitted, as it's known to the client)
  * The server provides a digital signature over previously exchanged handshake messages to prove it controls the private key corresponding to its certificate. CertificateVerify message
  * In TLS 1.3, much of the functionality traditionally handled in the Server Parameters stage (like key exchange) has been shifted earlier, into the ServerHello message, simplifying the handshake
* Authetication
* Post-Handshake

### TLS 1.2 handshake

* Client sends Hello: cipher suite, protocol version
* Server sends hello with chosen cipher suite + server certificate(public key + certificate info)
* Server sends Hello Done
* Client checks certificate
* Client generates pre-master key, encrypt it with server public key and sends it to server if RSA is used, Diffie-Hellman may be also used
* Client sends client finished
* Client and server generate symmetric key based on pre master key
* Both send change cypher spec - changing to symmetric encryption
* If Diffie-Hellman is used, than private key on the server side is used for Auth only, not for encryption

Handshake process, records, 10 in total:

1. Client hello - Type 1 - TLS version, Cipher Suites, Server Name Indication
2. Server Hello - Type 2 - chosen cipher suite,TLS version
3. Certificate (Type 11) - both certs - server and intermediate
4. Server Key Exchange (Type 12) - Difie Hellman Data
5. Server Hello Done(Type 14)
6. Client key Exchange(Type 16) - Difie Hellman data from client
7. Client Change Cipher Spec (Type - 20, This message notifies the server that all the future messages will be encrypted using the algorithm and keys that were just negotiated.)
8. Client Encrypted Handshake Message
9. Change Cipher Spec from server
10. Encrypted handshake message from server

### TLS 1.3 Handshake

* Client Hello: Supported Cipher Suites, Guesses Key Agreement Protocol, Key Share
* Server Hello, Key Agreement Protocol, Key Share, Server Finished

### Client Hello

* Universal for all TLS versions
* Content Type - Handshake - 22
* Handshake Type - Client Hello - 1
* Session ID - large number
* Random - large number - randomly generated for every new session
* All possible cipher suites for all TLS versions
* Many Extensions, some of them: - TLS supported versions - Key share extension: Group: x25519, Key Exchange - big number - client public key for Diffie-Helman exchange algorithm - TLS 1.3 only - SNI - server name - Supported Diffie-Hellman groups - Signature alhorithms - ALPN protocol

A lot of extensions and fields:

```
Handshake Protocol: Client Hello
    Handshake Type: Client Hello (1)
    Length: 508
    Version: TLS 1.2 (0x0303)
    Random: c980be07b33ea29eb4f92d274d366a9bcc00fa70957336a16f050c136fc778e2
    Session ID Length: 32
    Session ID: b144d555d1ccdc1453a3950601fe1b165bb40c94a7522cebcbb8f480c02c5a10
    Cipher Suites Length: 42
    Cipher Suites (21 suites)
    Compression Methods Length: 1
    Compression Methods (1 method)
    Extensions Length: 393
    Extension: Reserved (GREASE) (len=0)
    Extension: server_name (len=13)
    Extension: extended_master_secret (len=0)
    Extension: renegotiation_info (len=1)
    Extension: supported_groups (len=12)
    Extension: ec_point_formats (len=2)
    Extension: application_layer_protocol_negotiation (len=14)
    Extension: status_request (len=5)
    Extension: signature_algorithms (len=24)
    Extension: signed_certificate_timestamp (len=0)
    Extension: key_share (len=43)
    Extension: psk_key_exchange_modes (len=2)
    Extension: supported_versions (len=11)
    Extension: compress_certificate (len=3)
    Extension: Reserved (GREASE) (len=1)
    Extension: padding (len=198)
    [JA3 Fullstring: 771,4865-4866-4867-49196-49195-52393-49200-49199-52392-49162-49161-49172-49171-157-156-53-47-49160-49170-10,0-23-65281-10-11-16-5-13-18-51-45-43-27-21,29-23-24-25,0]
    [JA3: 773906b0efdefa24a7f2b8eb6985bf37]
```

Cipher suites are particular important, every suite has its own code

```
Cipher Suites (21 suites)
    Cipher Suite: Reserved (GREASE) (0xfafa)
    Cipher Suite: TLS_AES_128_GCM_SHA256 (0x1301)
    Cipher Suite: TLS_AES_256_GCM_SHA384 (0x1302)
    Cipher Suite: TLS_CHACHA20_POLY1305_SHA256 (0x1303)
    Cipher Suite: TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 (0xc02c)
    Cipher Suite: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 (0xc02b)
    Cipher Suite: TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 (0xcca9)
    Cipher Suite: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (0xc030)
    Cipher Suite: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xc02f)
    Cipher Suite: TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 (0xcca8)
    Cipher Suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA (0xc00a)
    Cipher Suite: TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA (0xc009)
    Cipher Suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA (0xc014)
    Cipher Suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA (0xc013)
    Cipher Suite: TLS_RSA_WITH_AES_256_GCM_SHA384 (0x009d)
    Cipher Suite: TLS_RSA_WITH_AES_128_GCM_SHA256 (0x009c)
    Cipher Suite: TLS_RSA_WITH_AES_256_CBC_SHA (0x0035)
    Cipher Suite: TLS_RSA_WITH_AES_128_CBC_SHA (0x002f)
    Cipher Suite: TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA (0xc008)
    Cipher Suite: TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA (0xc012)
    Cipher Suite: TLS_RSA_WITH_3DES_EDE_CBC_SHA (0x000a)

```

Server Name Indication is very important as well

```
Extension: server_name (len=13)
    Type: server_name (0)
    Length: 13
    Server Name Indication extension
        Server Name list length: 11
        Server Name Type: host_name (0)
        Server Name length: 8
        Server Name: nasa.gov
```

### Server Hello

* Contains one cipher suite, which was chosen from Client Hello
* If Server cannot find an algorithm it supports, it may drop a connection or it may ask a client for more information via Hello Retry Request, then client resends its Hello

### Authentication

* Authentication of key exchange is done using assymetric cryptography and hash algorithms
* Signature and hash algorithms are negotiated in Client/Server Hello
* Supported signature and hash algorithms are sent in `signature_alhorithms` extension, then Server may use these algorithms for transript signing, it also checks that client supports certificate signature algorithm used by CA, if client does not support it, handshake will not be established. Certificate signature algorithm and transcript signature algorithm maybe different. The most important that both of them are supported by client
* TLS 1.3 introduces signature\_algorithms\_cert (a separate extension) to list signature algorithms specifically for certificate signatures. This separates the algorithms used for certificates from those used for the handshake - `This extension is optional` - You will not see it in a capture from browser
* In TLS 1.2 certificate type is also specified in Cipher Suite, this is not used in TLS 1.3
* The certificate type (RSA or ECDSA) is automatically determined by the certificate itself when the server sends it to the client
* Unlike in TLS 1.2, signature algorithms in TLS 1.3 are not tightly coupled to hash algorithms

Example of signature\_algorithms in TLS 1.3:

```
Signature Hash Algorithms (11 algorithms)
    Signature Algorithm: ecdsa_secp256r1_sha256 (0x0403)
    Signature Algorithm: rsa_pss_rsae_sha256 (0x0804)
    Signature Algorithm: rsa_pkcs1_sha256 (0x0401)
    Signature Algorithm: ecdsa_secp384r1_sha384 (0x0503)
    Signature Algorithm: ecdsa_sha1 (0x0203)
    Signature Algorithm: rsa_pss_rsae_sha384 (0x0805)
    Signature Algorithm: rsa_pss_rsae_sha384 (0x0805)
    Signature Algorithm: rsa_pkcs1_sha384 (0x0501)
    Signature Algorithm: rsa_pss_rsae_sha512 (0x0806)
    Signature Algorithm: rsa_pkcs1_sha512 (0x0601)
    Signature Algorithm: rsa_pkcs1_sha1 (0x0201)

```

* No matter what algorithm is used - first hash of transcript is done and then signature algorithm is applied together with private key
* The ServerHello does not explicitly confirm the signature algorithm chosen for the handshake
* The actual signature algorithm used by the server is implicitly confirmed later in the CertificateVerify message when the server signs the handshake transcript
* Server chooses Signature Algorithm from Client's list based on a compatible signature algorithm that it supports and that matches its certificate's capabilities
* For example, if the server's certificate uses an RSA key, it will choose an RSA-based signature algorithm (e.g., rsa\_pss\_rsae\_sha256),if the server's certificate uses an ECDSA key, it will choose an ECDSA-based signature algorithm (e.g., ecdsa\_secp256r1\_sha256)
* Different signature algorithms and hash algorithms may be used for the server's certificate signature and the CertificateVerify message. This distinction exists because these signatures serve different purposes and are governed by different rules
* `Server hashes a handshake transcript > Server signs this hash > Server creates a CertificateVerify message`
* Server sends CertificateVerify message
* This message includes a digital signature over a handshake transcript
* The CertificateVerify message has the following components: - Signature Algorithm: Indicates the algorithm used for signing: RSA PKCS#1 version 1.5, RSA-PSS, ECDSA, EdDSA - Hash Algorithm used to create a hash for handshake transcript: SHA-256... - Signature: The digital signature itself, calculated over the handshake transcript hash
* Handshake transcript includes: `ClientHello, ServerHello, EncryptedExtensions, Certificate (if applicable)` - so all the records sent before
* Handshake transcript is different for TLS 1.3 and TLS 1.2
* The client validates CertificateVerify message by Verifying the server's certificate chain (ensuring the certificate is valid and trusted and Using the public key in the server's certificate to verify the signature in the CertificateVerify message
* After this client sends Finished message in TLS 1.3, in TLS 1.3 it sends ClientKeyExchange
* RSA - good to use
* ECDSA (Ecliptic curve digital signature algorithm) - slow
* DSA
* Auth is checked by verifying server certificate signature by CA + that server indeed has the private key: server sends something encrypted with private key, and client decrypts it with public key
* Auth is always a public key cryptography. Most commonly RSA, but sometimes ECDSA
* Auth is dependent on which key exchange algorithm is used
* During the RSA key exchange, the client generates a random value as the premaster secret and sends it encrypted with the server’s public key. The server, which is in possession of the corresponding private key, decrypts the message to obtain the premaster secret. The authentication is implicit: it is assumed that only the server in possession of the corresponding private key can retrieve the premaster secret, construct the correct session keys, and producethe correct Finished message
* During the DHE and ECDHE exchanges, the server contributes to the key exchange with its parameters. The parameters are signed with its private key. The client, which is in possession of the corresponding public key (obtained from the validated certificate), can verify that the parameters genuinely arrived from the intended server
* The server uses its private RSA key to sign parts of the handshake process to prove its identity to the client. This ensures that the Diffie-Hellman key exchange parameters are authentic and have not been tampered with

### Hash function

* Used with HMAC and HKDF
* The hash function is used in conjunction with the HMAC (Hash-based Message Authentication Code) algorithm to ensure the integrity of encrypted messages in the record layer
* It ensures that the messages exchanged between the client and server have not been tampered with
* The plaintext data is first encrypted using the encryption algorithm (AES-128-CBC).
* An HMAC is calculated over the encrypted data using the specified hash function (SHA-1).
* The HMAC is appended to the encrypted data to allow the recipient to verify its integrity

Key derivation

* The hash function is also used during the key derivation process to generate session keys from shared secrets
* TLS 1.3 allows SHA-256 and SHA-384

## TLS 1.3

<https://tls13.xargs.org\\>
<https://blog.cloudflare.com/rfc-8446-aka-tls-1-3/>

High level:

* Increased speed
* More Secure

Main features:

* Only strong ciphers and algorithms
* Faster handshake - increased performance
* The server now signs the entire handshake, including the cipher negotiation, so it is impossible to forge handshake process
* No user-defined DH parameters, no RSA. This means that every connection will use a DH-based key agreement and the parameters supported by the server are likely easy to guess (ECDHE with X25519 or P-256). Because of this limited set of choices, the client can simply choose to send DH key shares in the first message instead of waiting until the server has confirmed which key shares it is willing to support. That way, the server can learn the shared secret and send encrypted data one round trip earlier. SO CLIENT sends KEY IMMEDIATELY.
* Everything after Server Hello is encrypted, Certificate is encrypted - in order to understand where client goes, we need full interception
* Passive decryption, when you have TLS server private key, will not work - perfect forward secrecy
* Short notation of cipher suite: authentication algotithm is sent as an extension, key exchange algorithm is always Ephemeral Diffie-Hellman, cipher suite became too long too complex, there were too many variants of suites, for every suite IANA created a code
* Version negotiation removed
* SNI is still opened, but there is new RFC where it is encrypted
* 0-RTT Resumption (early data): if the client has connected to the server before, TLS 1.3 permits a zero-round trip handshake. This is accomplished by storing secret information (typically, Session ID or Session Tickets) of previous sessions and using them when both parties connect with each other in future. But it leads to the absense of Forward Secrecy. The second security concern when it comes to TLS 1.3 0-RTT is that it doesn’t provide a guarantee of non-replay between connections. If an attacker somehow manages to get hold of your 0-RTT encrypted data, it can fool the server into believing that the request came from the client. It is not very recomendeded to use it.

**Strong ciphers and algorithms**

Symmetric encryption - only AEAD ciphers:

* AES-GCM(most common)
* AES-EAX
* ChaCha20 with Poly1305(as MAC)

Perfect forward secrecy is mandatory, no RSA, only Diffie Hellman, TLS v1.3 supports only three key exchange methods:

* Ephemeral Diffie-Hellman (combined with digital signatures for authentication);
* PSK with ephemeral Diffie-Hellman;
* PSK without ephemeral Diffie-Hellman.

## Record protocol - Application Data

`[TLS Record Header] + [Encrypted Data] + [Authentication Tag]`

* Traffic protection between peers
* Division of traffic into a series of records, each of which is independently protected using the traffic keys
* TLS ensures that all messages cannot be replayed or reordered
* Nonce is used, starts at fixed value, for incremented for each new message, if nonce is not correct, connection is dropped
* In TLS 1.3, the nonce is generated independently by both the client and server based on shared keys and a sequence number
* TLS record with application data includes:
  * Content Type - 23
  * Protoocol version TLS 1.2 or TLS 1.3
  * Length of encrypted data
  * Nonce
  * Encrypted HTTP data
  * Authentication Tag for integrity
* Authentication Tag - verifying that the data was not tampered with during transmission. The authentication tag is produced using a Message Authentication Code (MAC), combined with encryption

Example of application data - nothing interesting

```
Transport Layer Security
    TLSv1.3 Record Layer: Application Data Protocol: Hypertext Transfer Protocol
        Opaque Type: Application Data (23)
        Version: TLS 1.2 (0x0303)
        Length: 26
        Encrypted Application Data: 1b226c63c851eea9897e0e6e4d3d265d96cac4f55564e66e81d4
        [Application Data Protocol: Hypertext Transfer Protocol]
```

## Alerts

* The Alert protocol further has a field called Description. This field contains the actual error information
* Alert example

![image](https://github.com/phph9/Foundation/assets/116812447/84324f16-9698-41df-a068-f0c4f2597b33)

* Alert types

```
close_notify(0)
unexpected_message(10),
bad_record_mac(20),
decryption_failed_RESERVED(21),
record_overflow(22),
decompression_failure(30),
          handshake_failure(40),
          no_certificate_RESERVED(41),
          bad_certificate(42),
          unsupported_certificate(43),
          certificate_revoked(44),
          certificate_expired(45),
          certificate_unknown(46),
          illegal_parameter(47),
          unknown_ca(48),
          access_denied(49),
          decode_error(50),
          decrypt_error(51)
```

## Cipher suite

<https://ciphersuite.info>

TLS suites use the TLS\_ prefix, SSL 3 suites use the SSL\_ prefix, and SSL 2 suites use the SSL\_CK\_ prefix. In all cases, the approach to naming is roughly the same. However, not all vendors use the standard suite names. OpenSSL and GnuTLS use different names. Microsoft largely uses the standard names but sometimes extends them with suffixes that are used to indicate the strength of the ECDHE key exchange.

**TLS1.2 cipher suite - 4 parametres**

```
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
```

* ECDHE - key exchange algorithm
* RSA - Authentication method - specifies which digital signature algorithm the server uses to sign the hash of handshake data, proving that it owns the private key corresponding to the public key in its certificate
* AES\_128\_GCM - symmetric encryption algorithm + key length + mode
* SHA256 - hash function for the HMAC used to authenticate each record (data packet) + hash function used in the PRF / key derivation function
* All other parametres are negotiated via extensions

**TLS 1.3 cipher suite - 2 parametres**

```
TLS_AES_128_GCM_SHA256
```

* In TLS 1.3 there are fewer parametres to avoid many variants of suites.
* Signature algorithm is sent as extension in Client hello - for signing hash of handshake with server's private key
* Key exchange is ECDHE always
* AES\_128\_GCM - symmetric encryption algorithm + key length + mode - all algorithms are AEAD, so authentication per packet is included, no need a HMAC
* SHA256 - This derivation process is called the key schedule — it takes the shared key from ECDHE, mixes in random values (nonces), and runs them through a hash-based pseudorandom function (PRF) or HKDF to produce strong, independent session keys. If your cipher suite ends in SHA256, then SHA256 is the hash function used inside that PRF/HKDF
* All other parametres are negotiated via extensions

TLS 1.3 Cipher Suites:

* TLS\_AES\_128\_GCM\_SHA256
* TLS\_AES\_256\_GCM\_SHA384
* TLS\_CHACHA20\_POLY1305\_SHA256
* TLS\_AES\_128\_CCM\_SHA256
* TLS\_AES\_128\_CCM\_8\_SHA256

TLS 1.3 cipher suites are defined differently, only specifying the symmetric ciphers, and cannot be used for TLS 1.2. Similarly, cipher suites for TLS 1.2 and lower cannot be used with TLS 1.3.

## TLS 1.3 VS TLS 1.2

There are 2 main high level advantages and goals in TLS 1.3:

* Increased speed
* More Secure

Symmetric encryption - only strong AEAD ciphers

* AES-GCM - most common
* AES-EAX
* ChaCha20 with Poly1305(as MAC)

Key exchange - only Ecliptic Curve Diffie Hellman Ephemeral (ECDHE)

* No RSA algorithm for key exchange
* No user-defined DH parameters

Key share is sent right in Client Hello

* In TLS 1.3 Client does not wait for Server's opinion, it speculatively sends Client public key for Diffie-Hellman key exchange algorithm in the Client Hello message as well as preferred Group to avoid waiting for the server to confirm which key shares it supports, consequently to speed up handshake

Server certificate is sent encrypted

* Server certificate cannot be seen in traffic capture
* Everything after Server Hello is encrypted
* In Wireshark, you will see nothing except Client and Server Hellos

SNI can be encrypted

* RFC 8744
* Both client and server should support it
* In this case it will be impossible to use URLs and URL categories as a condition for traffic decryption, only IP addresses can be used
* In order to understand where client goes, we need full interception: both Server Certificate and SNI are encrypted

Short notation of cipher suite

* TLS 1.2 cipher suite contains 4 parameters and looks like this:

```
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
ECDHE - key exchange algorithm
RSA - type of certificate used by the server for authentication
AES_128_GCM - Encryption algorithm + Encryption key size + Cipher mode
SHA256 - Hash function for message integrity and key derivation in the TLS handshake and record layer
```

TLS 1.3 cipher suite contains 2 parameters and looks like this:

`TLS_AES_128_GCM_SHA256`

* In TLS 1.3 there are fewer parameters to avoid many variants of suites
* Certificate type is not specified any more, the certificate type (RSA or ECDSA) is automatically determined by the certificate itself when the server sends it to the client
* Key exchange algorithm is ECDHE always
* Authentication method - specifies which digital signature algorithm the server uses to sign the hash of handshake data, proving that it owns the private key corresponding to the public key in its certificate - in extension

Shorter and faster handshake

* TLS 1.2 - 2 round trips, because Diffie-Hellman public keys are sent in separate messages
* TLS 1.3 - 1 round trip, Diffie-Hellman public keys are sent in Client Hello and Server Hello

Possible decryption issues

* Passive decryption, when traffic in mirrored to decryption device is not possible any more, because RSA key exchange is not used
* Active decryption during traffic interception is still possible and can be done by most NGFWs

New extensions in Client Hello messages

* Supported Versions - Indicates the versions of TLS that the client supports. This allows a client to explicitly declare support for TLS 1.3 and fallback to older versions if necessary. TLS 1.2 does not use this extension; instead, version negotiation is done using the ClientHello.version field
* Key Share - The client includes its Diffie-Hellman (DH) or Elliptic Curve Diffie-Hellman (ECDH) key shares in this extension
* Pre-Shared Key - Supports resumption and 0-RTT (zero round-trip time) handshakes by allowing the client to use a previously established Pre-Shared Key (PSK). In TLS 1.2 session resumption is handled using session IDs or session tickets, without a dedicated extension
* Signature Algorithms Certificate - Specifies supported signature algorithms for certificate verification. This is separate from the existing signature\_algorithms extension used for handshake signatures. TLS 1.2 does not distinguish between handshake and certificate signatures. This extension is optional and is not always used. Most probably, you will not see it in requests from browser

## SNI

* Server Name Indication
* Can be encrypted in TLS 1.3
* Server Name (the domain part of the URL) is presented in the ClientHello packet, in plain text.
* The payload in the SNI extension can now be encrypted in TLS1.3
* **SNI can be changed by client to bypass security policies, so it is very important to check server certificate not only SNI**
* All policies on NGWF and SSL decryptors are based on SNI or Server certificate, Server certificate is more accurate source. In TLS 1.3 it is encrypted. So it is more difficult to understand what to decrypt and what not
* Also, reverse DNS lookup may help
* Correct application identification is only possible with decryption: gmail site is signed by Google cert and you will not be able to understand what it is exactly without decryption
* SNI Routing can be uased on load balancers to forward traffic without SSL termination to particular pool
* Browsers add SNI to every request
* Example from traffic capture

```
Extension: server_name (len=12)
Type: server_name (0)
Length: 12
Server Name Indication extension
	Server Name list length: 10
	Server Name Type: host_name (0)
	Server Name length: 7
	Server Name: mail.ru
```

## HSTS

* HTTP Strict Transport Security
* Forces browsers to use HTTPS only when connecting to a website
* The server sends a special HTTP header: `Strict-Transport-Security: max-age=31536000; includeSubDomains; preload`
* The browser remembers this instruction for the given time (max-age in seconds)
* The browser never attempts HTTP — it goes straight to HTTPS

## Wireshark

SNI filtering

```
tls.handshake.extensions_server_name contains "dlp"
```

Show only Handshake protocol, no application data

```
tls.record.content_type == 22
```

Show only client hellos

```
tls.handshake.type == 1
```

Show only server hellos

```
tls.handshake.type == 2
```

## Client authentication

* When a server indicates a requirement for the client to submit its certificate, the client must send both its certificate, and a digitally-signed hash value. This hash value is signed (i.e. encrypted) with the client's private key
* Server has to have the same CA, which was used to create client's cert
* Server checks the validity of CA + that client indeed has private key
* If someones decide to decrypt an re-encrypt - it will not possible, because interceptor does not have client's private key
* Such connections usually compltetely bypassed

## Decryption

Why decryption fails:

* Untrusted server certificate
* Expired certificate
* Pinned certificate to client
* Client authentication
* Unsupported ciphers
* Custom app

## PSK

* PSK can be used instead of certificates
* Client sends PSK identifiers in ClientHello
* Authentication and key exchange phases are skipped
* Used for session resumption

## OpenSSL

```
openssl s_client -connect meetup.sberbank.ru:443
```

This command will show certificates, protocol and ciphers If F5 does not know this certificate, there will be a error:

```
verify error:num=20:unable to get local issuer certificate
verify error:num=21:unable to verify the first certificate
```

Show SAN for cert:

* `openssl s_client -connect wpcp-dc1.corp.ad.ctc:636 -showcerts </dev/null`
* Create a file called cert.pem, and paste the first certificate block contents into it
* `openssl x509 -in cert.pem -noout -text`

## Nmap

* Show SSL cert - `nmap -p 443 --script ssl-cert example.com`
