Skip to content
midenok edited this page Nov 21, 2014 · 46 revisions

Terms

Determines which traffic is applied to IPSec.

View SPD table:
setkey -DP

Determines which algorithms to be used for specific IPSec traffic. Can be filled during IKE negotiation.

SA includes:
  • shared secret key (negotiated during IKE);
  • key lifetime
  • hashing algorithm
  • etc.
Watch SAD table:
setkey -lD

IKE phases

Phase 1

Set up IKE SA. Negotiates and agrees on how to do IKE. Does agreement on how securely the devices are going to be, how they exchange keys. Does mutual authentication with following methods:

  • pre-shared key;
  • encrypted RSA nonces;
  • RSA or DSS signatures;
  • Certificate Authority (CA).

Phase 2

Set up IPsec SA.

Transport vs Tunnel mode

  • Transport mode is used for connection to specific hosts. It does NOT encapsulate IP header.
  • Tunnel mode is used for connection to specific networks. It DOES encapsulate IP header. (more)

Transport or Tunnel mode is selected in SPD (in Linux spdadd command of setkey utility).

AH vs ESP

AH and ESP are both IP protocols (codes 51 and 50 respectively).

With AH, an HMAC keyed hash is transported in the AH header. That provides data integrity and source authentication: the data must come from an authentic source, one that knows the hash key. And the data cannot be changed by anyone "in the middle", since the keyed hash allows detection of any changes. Note however that AH does not provide for encryption of the actual data. So you would only use AH for things like placing small orders across the Internet, assuming that does not need to be done confidentially.

With ESP, you get the HMAC keyed hash plus encryption of the data. Generally, this is what we want when we run IPsec. (source)

AH or ESP is selected in SPD (in Linux spdadd command of setkey utility).

Racoon

Start in debug mode:
racoon -F -dd -v

Configuring racoon.conf

Section: 'remote'

  • Required options: exchange_mode, proposal
  • Useful options: ph1id (links with sainfo by correspinding remoteid)
proposal section
Optimal values:
encryption_algorithm aes;
hash_algorithm sha1;
dh_group 2;
Section: 'sainfo'
Required options:

encryption_algorithm, authentication_algorithm, compression_algorithm

Useful options:
  • remoteid: links with sainfo (by correspinding ph1id)

Tips

  • Negotiation will not start unless traffic is detected!
  • Host-wide SPD entries are required for transparent IPSec! /etc/racoon/racoon.conf does NOT specify such entries. Use /etc/ipsec-tools.conf additionally or /etc/racoon/racoon-tool.conf which supersedes both ipsec-tools.conf and racoon.conf.
  • IPsec-Tools Checklist
  • Configuration example
  • In RFC mode (default) setkey automatically adds fwd rules.
  • ISAKMP used for IKE negotiation is UDP protocol

Mikrotik RouterOS

Useful commands

View log continuously:
/log print follow
Change debug logging topics:
/system logging edit 4 topics
debug,!ssh,!ntp,!dhcp,!script
Open UDP in firewall:
/ip firewall filter add place-before=0 chain=input action=accept protocol=udp

Note: more precise rule will come later._

Add IPSec rule:
/ip ipsec peer add passive=yes auth-method=pre-shared-key secret="secret" nat-traversal=yes send-initial-contact=no hash-algorithm=sha1 enc-algorithm=aes-128 dh-group=modp1024 lifetime=1d

Note: IPSec is enabled when rule is enabled. See:

/log print
12:24:37 ipsec,debug 192.168.1.1[500] used as isakmp port (fd=21) 
12:24:37 ipsec,debug 192.168.1.1[4500] used as isakmp port with NAT-T (fd=22) 
12:24:37 ipsec,debug 192.168.2.1[500] used as isakmp port (fd=23) 
12:24:37 ipsec,debug 192.168.2.1[4500] used as isakmp port with NAT-T (fd=24) 
12:24:37 ipsec,debug 178.144.100.100[500] used as isakmp port (fd=25) 
12:24:37 ipsec,debug 178.144.100.100[4500] used as isakmp port with NAT-T (fd=26) 

Note: port option doesn't influence this (why?).

Troubleshooting

1. parameters in /ip ipsec peer and peer proposal packet must match

Good message:
13:02:08 ipsec,debug,packet Compared: Local:Peer 
13:02:08 ipsec,debug,packet (lifetime = 86400:28800) 
13:02:08 ipsec,debug,packet (lifebyte = 0:0) 
13:02:08 ipsec,debug,packet enctype = AES-CBC:AES-CBC 
13:02:08 ipsec,debug,packet (encklen = 128:128) 
13:02:08 ipsec,debug,packet hashtype = SHA:SHA 
13:02:08 ipsec,debug,packet authmethod = pre-shared key:pre-shared key 
13:02:08 ipsec,debug,packet dh_group = 1024-bit MODP group:1024-bit MODP group 
13:02:08 ipsec,debug,packet an acceptable proposal found. 
Bad message:
13:05:47 ipsec,debug,packet Compared: Local:Peer 
13:05:47 ipsec,debug,packet (lifetime = 86400:28800) 
13:05:47 ipsec,debug,packet (lifebyte = 0:0) 
13:05:47 ipsec,debug,packet enctype = AES-CBC:AES-CBC 
13:05:47 ipsec,debug,packet (encklen = 256:128) 
13:05:47 ipsec,debug,packet hashtype = SHA:SHA 
13:05:47 ipsec,debug,packet authmethod = pre-shared key:pre-shared key 
13:05:47 ipsec,debug,packet dh_group = 1024-bit MODP group:1024-bit MODP group 
13:05:47 ipsec,debug,packet type=Life Type, flag=0x8000, lorv=seconds 
13:05:47 ipsec,debug,packet type=Life Duration, flag=0x8000, lorv=28800 
13:05:47 ipsec,debug,packet type=Encryption Algorithm, flag=0x8000, lorv=AES-CBC 
13:05:47 ipsec,debug,packet type=Key Length, flag=0x8000, lorv=128 
13:05:47 ipsec,debug,packet type=Authentication Method, flag=0x8000, lorv=pre-shared key 
13:05:47 ipsec,debug,packet type=Hash Algorithm, flag=0x8000, lorv=SHA 
13:05:47 ipsec,debug,packet type=Group Description, flag=0x8000, lorv=1024-bit MODP group 
13:05:47 ipsec,debug no suitable proposal found. 

Note: error is in (encklen = 256:128), fix:

/ip ipsec peer set 0 enc-algorithm=aes-128

2.

04:41:52 ipsec,debug no policy found: 0.0.0.0/0[0] 192.168.88.0/24[0] proto=any dir=in 
04:41:52 ipsec,debug failed to get proposal for responder. 
04:41:52 ipsec,error failed to pre-process ph2 packet. 

Additional info

Encryption algorithms performance

$ time echo test| gpg --passphrase-fd 0 --cipher-algo aes256 --symmetric -o /dev/null yy
Reading passphrase from file descriptor 0    

real    0m26.417s
user    0m25.631s
sys     0m0.690s
$ time echo test| gpg --passphrase-fd 0 --cipher-algo twofish --symmetric -o /dev/null yy
Reading passphrase from file descriptor 0    

real    0m28.609s
user    0m27.781s
sys     0m0.714s
$ time echo test| gpg --passphrase-fd 0 --cipher-algo camellia256 --symmetric -o /dev/null yy
Reading passphrase from file descriptor 0    

real    0m47.360s
user    0m46.442s
sys     0m0.748s
  • The most efficient algorithm of above 3: AES (Rijndael)

On AES modes of operation (CBC, CTR, GCM)

From Wikipedia:

GCM is ideal for protecting packetized data, because it has minimum latency and minimum operation overhead.

Keyed hash algorithm: HMAC

Uses shared secret key to produce hashes: guarantees that third-party will not be able to spoof hashes. Hash proves that original message was not being altered. HMAC uses one of two algorithms as a base MD5 (HMAC-MD5) and SHA (HMAC-SHA).

Links: