IPsec basics: IKE, phase 1 and phase 2
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- IPsec encrypts traffic (confidentiality), checks that it was not altered (integrity) and confirms who the other side is (authentication).
- IKE is the protocol that negotiates the tunnel's keys (UDP 500, or UDP 4500 behind NAT). The data itself is carried by ESP.
- Phase 1 builds a trusted control channel between two devices: the sides identify each other and agree on a key. Phase 2 defines the encryption keys for real traffic and which networks go through the tunnel.
- Both sides must have the same IKE version, proposal (encryption and hash), DH group, authentication (PSK or certificate) and phase 2 selectors.
- On a FortiGate you build a route-based VPN: the tunnel appears as a virtual interface and traffic is steered to it with an ordinary route and policy.
Why IPsec?
The head office and a branch communicate over the internet. The internet is an open network: someone on the way could read the packets, change them or pretend to be the branch.
IPsec solves all three:
| Requirement | What IPsec does | With what |
|---|---|---|
| Confidentiality | Encrypts the inside of the packet | AES |
| Integrity | Checks that the packet was not changed on the way | SHA hash |
| Authentication | Confirms that the other side really is who it claims | Pre-shared key or certificate |
As a result the private networks of the two offices (10.10.10.0/24 and 10.20.0.0/24) see each other directly, as if a cable ran between them.
The protocols
| Protocol | Port | What it does |
|---|---|---|
| IKE | UDP 500 | Identifies the sides and negotiates keys |
| IKE NAT-T | UDP 4500 | If one side is behind NAT, IKE and ESP move to this port |
| ESP | IP protocol 50 | Carries the encrypted data |
| AH | IP protocol 51 | Integrity only, no encryption. Hardly ever used |
If the provider or a firewall in between blocks UDP 500, UDP 4500 and ESP, the tunnel is not established.
Two phases
Phase 1 answers the questions "who are you?" and "how do we talk to each other securely?" The result is an IKE SA (security association): an encrypted control channel between the two devices. No user traffic passes through this channel yet.
Phase 2 answers, inside that channel, the questions "which traffic will be encrypted?" and "with which key?" The result is an IPsec SA, and data starts to flow in ESP. In fact there is one SA per direction, so two.
The parameters of phase 1
| Parameter | Meaning | Recommendation |
|---|---|---|
| IKE version | 1 or 2 | 2 |
| Remote gateway | The public IP of the other side | |
| Authentication | Pre-shared key (PSK) or certificate | A long random PSK, or a certificate |
| Proposal | Encryption + hash | aes256-sha256 |
| DH group | The strength of the key exchange | 14 or higher (19, 20, 21) |
| Key lifetime | How often the key is renewed | 86400 seconds (default) |
| Mode (IKEv1 only) | Main or aggressive | Main |
Diffie-Hellman (DH) lets two sides create a shared secret key over an open channel: the key itself never crosses the network. The group number shows the mathematical strength. Groups 1, 2 and 5 are weak; do not use them.
IKEv1 and IKEv2
| IKEv1 | IKEv2 | |
|---|---|---|
| Number of messages | Main mode 6, aggressive 3, then 3 for phase 2 | 4 in total |
| NAT traversal | As an add-on | Built in |
| Remote-access authentication | XAuth (an add-on) | EAP (built in) |
| Stability, reconnection | Weak | Better |
Choose IKEv2 for new tunnels. IKEv1 is needed only when an old device on the other side supports nothing else. In IKEv1, aggressive mode sends the identity unencrypted and is less secure.
The parameters of phase 2
| Parameter | Meaning |
|---|---|
| Proposal | Encryption and hash of the data (aes256-sha256) |
| PFS (perfect forward secrecy) | A fresh DH exchange for phase 2. If one key leaks, earlier traffic cannot be opened. Keep it on |
| Selectors | The local and remote network that go through the tunnel |
| Key lifetime | 43200 seconds (default) |
The selector is where most mistakes are made. If HQ has "local 10.10.10.0/24, remote 10.20.0.0/24", BR must have the exact mirror: "local 10.20.0.0/24, remote 10.10.10.0/24". If they do not match, phase 1 comes up and phase 2 does not.
Between two FortiGates you can also leave the selectors at 0.0.0.0/0 and control which traffic passes with routes and policies. When working with another vendor's device, however, the selectors must match exactly.
What must match
| Phase | Must be the same on both sides |
|---|---|
| Phase 1 | IKE version, proposal, DH group, PSK, (in IKEv1) the mode |
| Phase 2 | Proposal, PFS and DH group, selectors (mirrored) |
The lifetime may differ: the sides take the smaller one.
Route-based VPN
On a FortiGate a tunnel creates a virtual interface (named after phase 1). From then on the tunnel is like an ordinary port:
- A route: remote network → tunnel interface.
- A policy: LAN port → tunnel interface (and the reverse direction).
The advantages: you can put the tunnel in a zone or in SD-WAN, run OSPF over it, and follow it with the sniffer and debug flow.
The old method, policy-based VPN, defined the tunnel inside a policy with action ipsec. It is not used in new deployments.
Topologies
| Type | Description | Lesson |
|---|---|---|
| Site-to-site | Two offices, both with a fixed address | 37 |
| Hub-and-spoke | All branches connect to the head office | |
| Dial-up (remote access) | A staff laptop connects with FortiClient; its address is not known in advance | 38 |
Extra mechanisms
- NAT traversal: if one side is behind NAT, ESP packets are wrapped in UDP 4500. It is on by default.
- DPD (dead peer detection): checks whether the other side still answers. If not, the tunnel is torn down and a new attempt is made.
- Auto-negotiate: brings phase 2 up by itself without waiting for traffic.
A phase 1 example
FGT-HQ # config vpn ipsec phase1-interface FGT-HQ (phase1-interface) # edit "to-BR" FGT-HQ (to-BR) # set interface "port1" FGT-HQ (to-BR) # set ike-version 2 FGT-HQ (to-BR) # set peertype any FGT-HQ (to-BR) # set proposal aes256-sha256 FGT-HQ (to-BR) # set dhgrp 14 FGT-HQ (to-BR) # set remote-gw 198.51.100.2 FGT-HQ (to-BR) # set psksecret <key> FGT-HQ (to-BR) # next FGT-HQ (phase1-interface) # end
In the next lesson we complete this with phase 2, a route and policies and build a working tunnel.
Watch out on a real device: a pre-shared key that is short or a dictionary word is the weakest link of IPsec. Make the PSK a random string of at least 20 characters and use a different one for each tunnel. Do not send it openly by email. In a network with many tunnels, certificate authentication is more reliable.
🧪 Hands-on lab · Device: FortiGate-VM
IPsec phase 1
Create phase 1 for a tunnel to the branch (public address 198.51.100.2). Name to-BR, outgoing port port1, IKEv2, proposal aes256-sha256, DH group 14, any pre-shared key. After next type show system interface: a new tunnel interface named after phase 1 has been created.
Checked:
- to-BR: interface port1
- IKEv2
- remote-gw 198.51.100.2
- proposal aes256-sha256
- dhgrp 14
- Tunnel interface to-BR
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What is the result of phase 1?
2Which parameter defines which networks go through the tunnel?
3One of the sides is behind NAT. Which port does IPsec move to?
4Phase 1 is up but phase 2 is not. What is the most likely cause?
5In a route-based VPN on a FortiGate, how is traffic steered into the tunnel?
Comments
Sign in to comment.