[VPN] FortiGate 장비와 MacOS 사이의 IPSec 터널 설정

FortiGate 설정

IPSec Tunnels 생성

Network

  • Remote Gateway: “Dialup User”, Interface: “wan1″(공인 IP가 연결된 WAN 포트)
  • Local Gateway: Disable
  • Mode Config: Disable
  • NAT Traversal: Enable
  • Dead Peer Detection: On Idle
  • Add route: Disable
  • Auto discovery sender / receiver: Disable
  • Device creation: Enabled
  • Tunnel search: Selectors

Authentication

  • Method: Pre-shared Key
  • Pre-shared Key: 정한 PSK를 적는다. 아래 MacOS 설정의 ipsec.secrets 와 당연히 동일(e.g. mySecretKey)해야 한다.
  • IKE Version: 1
  • IKE Mode Main(ID protection)
  • Peer Options: Accept Types는 Specific peer ID로 하고 Peer ID로 아래 ipsec.confleftid와 동일(e.g. dasomoli.org)해야 한다.

Phase 1 Proposal

  • Encryption: AES256
  • Integrity: SHA256
  • DH Group: 14
  • Key Lifetime (seconds): 86400

XAUTH

  • Type: Disabled

Phase 2 Selectors

  • Subnet1
    • Local Address: 192.168.64.0/19 (192.168.64.0/255.255.224.0)
    • RemoteAddress: 192.168.0.0/24 (192.168.0.0/255.255.255.0)
    • Encryption: AES256, Integrity: SHA256, DH Group: 14
    • Enable Replay Detection: Enabled
    • Enable Perfect Forward Secrecy (PFS): Enabled
    • Local Port/Remote Port/Protocol: Enabled
    • Key Lifetime: 3600
  • Subnet2
    • Local Address: 10.30.0.0/16 (10.30.0.0/255.255.0.0)
    • RemoteAddress: 192.168.0.0/24 (192.168.0.0/255.255.255.0)
    • Encryption: AES256, Integrity: SHA256, DH Group: 14
    • Enable Replay Detection: Enabled
    • Enable Perfect Forward Secrecy (PFS): Enabled
    • Local Port/Remote Port/Protocol: Enabled
    • Key Lifetime: 3600

Log 확인

FortiGate CLI에서 확인

diagnose vpn ike gateway list

또는:

diagnose vpn tunnel list

그 외.

  • Static Routes에 터널 연결된 PC로의 routing이 추가되어야 한다.
  • Firewall Policy가 추가된 IPSec 터널을 위한 인터페이스에 incoming/outgoing 모두 허용되어야 한다.
    • NAT는 이 경우 Disable하여야 한다.

MacOS 설정

1. 사전 준비

1.1. strongswan 설치

strongswan 을 설치한다.

brew install strongswan

2. 설정 파일 작성

/opt/homebrew/etc/ipsec.conf 설정 파일을 작성한다.

  • left: 내 Mac
  • right: FortiGate의 공인 IP
  • leftid, rightid: FortiGate와 일치해야 함
  • leftsubnet: 집 네트워크
  • rightsubnet: FortiGate 내부 네트워크
  • ike나 esp의 aes256-sha256-modp2048 는 Encryption: AES256, Integrity: SHA256, DH Group: 14를 의미한다. 느낌표(!)는 강제 적용을 의미한다.
config setup
    charondebug="ike 2, knl 2, cfg 2, net 2, esp 2"

conn fortigate
    keyexchange=ikev1
    aggressive=no
    authby=psk
    forceencaps=yes
    installpolicy=yes
    type=tunnel
    left=%defaultroute
    leftid=@dasomoli.org
    right=11.13.3.1
    rightid=11.13.3.1
    leftsubnet=192.168.0.0/24
    rightsubnet=192.168.64.0/19,10.30.0.0/16
    leftupdown=/opt/homebrew/etc/ipsec-updown.sh
    ike=aes256-sha256-modp2048!
    esp=aes256-sha256-modp2048!
    ikelifetime=86400s
    lifetime=3600s
    dpdaction=restart
    auto=start

설정 파일에서 사용하는 /opt/homebrew/etc/ipsec-updown.sh 를 작성한다. 이는 strongSwan은 macOS에서 커널 라우팅 테이블에 자동으로 라우트를 추가하지 않기 때문이다. 이는 macOS 커널이 PF_ROUTE를 통해 정책 기반 라우팅을 제공하지 않기 때문이다. 따라서 updown 스크립트를 활용해 수동 라우팅을 추가한다.

#!/bin/sh
case "$PLUTO_VERB" in
    up-client)
        /sbin/route add -net 10.30.0.0/16 -interface $PLUTO_INTERFACE
        /sbin/route add -net 192.168.64.0/19 -interface $PLUTO_INTERFACE
        ;;
    down-client)
        /sbin/route delete -net 10.30.0.0/16
        /sbin/route delete -net 192.168.64.0/19
        ;;
esac

PSK를 /opt/homebrew/etc/ipsec.secrets 파일 안에 다음과 같이 넣는다.

@dasomoli.org @fortigate : PSK "mySecretKey"

3. 실행 방법

macOS에서는 시스템 서비스로 실행되지 않기 때문에, 수동으로 아래와 같이 실행한다.

sudo ipsec start

또는

sudo ipsec down fortigate
sudo ipsec up fortigate

로그 확인:

log show --predicate 'process == "charon"' --info --last 5m

연결 확인:

ipsec statusall

종료:

sudo ipsec stop

라우팅 확인:

route -n get 10.30.0.1

혹은:

netstat -rn | grep 10.

[Security] TLS-PSK

일단 기본 개념부터.

PSK는 Pre-Shared Key의 줄임말로, 보안이 필요한 통신을 하는 양자 간에 미리 공유된 symmetric 키를 말한다.

TLS는 Transport Layer Security의 줄임말로, SSL이 표준화되면서 쓰이는 이름이다.

TLS-PSK는 PSK를 이용해서 TLS연결을 맺는 것으로 RFC 4279에 문서화되어 있다.

RFC 4279의 내용을 살펴보자.

      Client                                               Server
      ------                                               ------

      ClientHello                  -------->
                                                      ServerHello
                                                    (Certificate)
                                               ServerKeyExchange*
                                             (CertificateRequest)
                                   <--------      ServerHelloDone
      (Certificate)
      ClientKeyExchange
      (CertificateVerify)
      ChangeCipherSpec
      Finished                     -------->
                                                 ChangeCipherSpec
                                   <--------             Finished
      Application Data             <------->     Application Data

위 그림이 TLS handshake과정을 나타낸다.

1. 클라이언트는 ClientHello 메시지에 TLS Version과 하나 이상의 cipher-suite을 실어 보낸다.

2. 서버는 ServerHello 메시지에 사용할 TLS Version과 사용할 cipher-suite를 실어 보낸다. ServerKeyExchange 메시지로 클라이언트가 어떤 PSK를 사용할 지, PSK identity hint를 함께 보낸다. ServerHelloDone도 보낸다.

3. 클라이언트는 ServerKeyExchange에 포함된 PSK identity hint로 사용할 PSK를 고른다. 고른 PSK의 PSK identity를 ClientKeyExchange 메시지에 실어 보낸다. Premaster secret은 PSK가 N octets라면, uint16 타입의 N, N개의 0 octets, 두번째 uint16타입의 N, 그리고 PSK N octets.

OpenSSL에서는 Client 쪽에서는 PSK callback을 SSL_CTX_set_psk_client_callback()이나 SSL_set_psk_client_callback()을 통해 설정하도록 되어있는데, 이 callback을 Server로부터 받은 PSK Identity Hint를 읽어 valid한지를 check하고, 해당 hint에 맞는 PSK를 psk에, PSK identity를 identity argument에 채워주는 역할을 한다. callback은 psk의 length를 return해야 한다. 0이나 그 이하는 error를 나타낸다.

Server쪽에서는 SSL_CTX_use_psk_identity_hint()나 SSL_use_psk_identity_hint()를 통해서 사용할 PSK Identity Hint를 Setting하고, SSL_CTX_set_psk_server_callback()나 SSL_set_psk_server_callback()을 통해 callback을 셋팅한다. 이 callback은 PSK identity가 valid한지를 체크하고, PSK identity가 있다면, pre-shared key를 psk argument에 채우고, PSK의 길이를 return한다.

 

* 참고

RFC 4279 – PSK Ciphersuites for TLS(https://tools.ietf.org/html/rfc4279)

TLS: https://en.wikipedia.org/wiki/Transport_Layer_Security, https://namu.wiki/w/TLS

TLS-PSK: https://en.wikipedia.org/wiki/TLS-PSK

OpenSSL

SSL_CTX_set_psk_client_callback: https://www.openssl.org/docs/manmaster/ssl/SSL_CTX_set_psk_client_callback.html

SSL_CTX_use_psk_identity_hint: https://www.openssl.org/docs/manmaster/ssl/SSL_CTX_use_psk_identity_hint.html