Topic Overview

VPN (IPSec, SSL VPN)

Understand VPN technologies: IPSec and SSL VPN for secure remote access, tunneling, and encrypted communication over public networks.

VPN (Virtual Private Network) creates a secure, encrypted connection over a public network. IPSec and SSL VPN are two main technologies for implementing VPNs.


What is a VPN?

VPN provides:

  • Encrypted tunnel: Secure communication over public network
  • Remote access: Access private network from anywhere
  • Privacy: Hide traffic from ISP
  • Security: Encrypt all data in transit

Use cases:

  • Remote employee access
  • Site-to-site connections
  • Bypass geo-restrictions
  • Secure public Wi-Fi

IPSec VPN

IPSec (IP Security) operates at network layer (Layer 3).

IPSec Modes

1. Transport Mode

Encrypts only payload (data)
Original IP header preserved

2. Tunnel Mode

Encrypts entire IP packet
New IP header added

IPSec Components

1. AH (Authentication Header)

  • Authentication (integrity)
  • No encryption

2. ESP (Encapsulating Security Payload)

  • Encryption
  • Authentication

IPSec Phases

Phase 1 (IKE - Internet Key Exchange):

Establish secure channel
Authenticate peers
Exchange keys

Phase 2:

Establish IPSec SA (Security Association)
Negotiate encryption algorithms

SSL VPN

SSL VPN operates at application layer (Layer 7).

SSL VPN Types

1. SSL Portal VPN

Web-based access
Browser-based
Limited to web applications

2. SSL Tunnel VPN

Full network access
Client software required
Access to all applications

SSL VPN Process

1. Client connects to VPN gateway
2. SSL/TLS handshake
3. Authenticate user
4. Establish encrypted tunnel
5. Route traffic through tunnel

Comparison

FeatureIPSec VPNSSL VPN
LayerNetwork (Layer 3)Application (Layer 7)
SetupComplex (client software)Simple (browser)
AccessFull networkApplication-specific
FirewallMay need port openingWorks through firewalls (HTTPS)
PerformanceFaster (lower overhead)Slower (higher overhead)
Use caseSite-to-site, full accessRemote access, web apps

Examples

IPSec VPN Configuration

# IPSec VPN setup
ipsec.conf:
  conn myvpn
    type=tunnel
    left=192.168.1.1
    leftsubnet=192.168.1.0/24
    right=203.0.113.1
    rightsubnet=10.0.0.0/24
    authby=secret
    ike=aes256-sha256-modp2048
    esp=aes256-sha256

SSL VPN Client

import ssl
import socket

class SSLVPNClient:
    def __init__(self, vpn_server, port=443):
        self.vpn_server = vpn_server
        self.port = port
    
    def connect(self, username, password):
        # Create SSL context
        context = ssl.create_default_context()
        
        # Connect to VPN gateway
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ssl_sock = context.wrap_socket(sock, server_hostname=self.vpn_server)
        ssl_sock.connect((self.vpn_server, self.port))
        
        # Authenticate
        auth_request = f"USER {username}\nPASS {password}\n"
        ssl_sock.send(auth_request.encode())
        
        response = ssl_sock.recv(1024)
        if response.startswith(b"OK"):
            return ssl_sock
        else:
            raise AuthenticationError()
    
    def tunnel_traffic(self, ssl_sock, destination):
        # Route traffic through VPN tunnel
        while True:
            data = ssl_sock.recv(4096)
            if not data:
                break
            
            # Forward to destination
            destination.send(data)

Common Pitfalls

  • IPSec NAT traversal: IPSec doesn't work well with NAT. Fix: Use NAT-T (NAT Traversal)
  • SSL VPN performance: Slower than IPSec. Fix: Optimize, use hardware acceleration
  • Firewall configuration: Blocking VPN traffic. Fix: Open required ports (UDP 500, 4500 for IPSec)
  • Key management: Weak keys compromise security. Fix: Use strong keys, rotate regularly

Interview Questions

Beginner

Q: What is a VPN and what are the differences between IPSec and SSL VPN?

A:

VPN (Virtual Private Network) creates secure, encrypted connection over public network.

IPSec VPN:

  • Layer: Network layer (Layer 3)
  • Setup: Complex (client software)
  • Access: Full network access
  • Performance: Faster
  • Use case: Site-to-site, full network access

SSL VPN:

  • Layer: Application layer (Layer 7)
  • Setup: Simple (browser)
  • Access: Application-specific
  • Performance: Slower
  • Use case: Remote access, web applications

Key Differences:

FeatureIPSecSSL VPN
LayerNetworkApplication
SetupComplexSimple
AccessFull networkApps
FirewallMay need portsWorks through firewalls

When to use:

  • IPSec: Site-to-site, full network access
  • SSL VPN: Remote access, web apps, easy setup

Intermediate

Q: Explain how IPSec VPN works. What are the phases and modes?

A:

IPSec Process:

Phase 1 (IKE - Internet Key Exchange):

1. Establish secure channel
2. Authenticate peers (pre-shared key or certificates)
3. Exchange keys
4. Create IKE SA (Security Association)

Phase 2:

1. Establish IPSec SA
2. Negotiate encryption algorithms (AES, 3DES)
3. Negotiate authentication (SHA, MD5)
4. Create encrypted tunnel

Modes:

1. Transport Mode:

Encrypts only payload (data)
Original IP header preserved
Use: Host-to-host

2. Tunnel Mode:

Encrypts entire IP packet
New IP header added
Use: Site-to-site, gateway-to-gateway

Components:

  • AH (Authentication Header): Authentication only
  • ESP (Encapsulating Security Payload): Encryption + authentication

Example:

Site A → IPSec Tunnel → Site B
  Encrypted: Entire IP packet
  Authenticated: ESP provides integrity

Senior

Q: Design a VPN system that supports both IPSec and SSL VPN for a large organization. How do you handle authentication, key management, and ensure security?

A:

class EnterpriseVPNSystem {
  private ipsecGateway: IPSecGateway;
  private sslvpnGateway: SSLVPNGateway;
  private authServer: AuthServer;
  private keyManager: KeyManager;
  
  constructor() {
    this.ipsecGateway = new IPSecGateway();
    this.sslvpnGateway = new SSLVPNGateway();
    this.authServer = new AuthServer();
    this.keyManager = new KeyManager();
  }
  
  // 1. IPSec Gateway
  class IPSecGateway {
    async establishConnection(clientIP: string): Promise<IPSecConnection> {
      // Phase 1: IKE
      const ikeSA = await this.establishIKE(clientIP);
      
      // Phase 2: IPSec SA
      const ipsecSA = await this.establishIPSec(ikeSA);
      
      return new IPSecConnection(ipsecSA);
    }
    
    async establishIKE(clientIP: string): Promise<IKESA> {
      // Authenticate client
      const auth = await this.authServer.authenticate(clientIP);
      
      // Exchange keys
      const keys = await this.keyManager.exchangeKeys(clientIP);
      
      return {
        clientIP,
        keys,
        algorithms: { encryption: 'AES-256', auth: 'SHA-256' }
      };
    }
  }
  
  // 2. SSL VPN Gateway
  class SSLVPNGateway {
    async establishConnection(client: SSLClient): Promise<SSLVPNConnection> {
      // SSL/TLS handshake
      await this.sslHandshake(client);
      
      // Authenticate user
      const user = await this.authServer.authenticateUser(client.credentials);
      
      // Create tunnel
      const tunnel = await this.createTunnel(user);
      
      return new SSLVPNConnection(tunnel);
    }
  }
  
  // 3. Authentication
  class AuthServer {
    async authenticate(credentials: Credentials): Promise<User> {
      // Multi-factor authentication
      if (credentials.type === 'certificate') {
        return await this.authenticateCertificate(credentials);
      } else if (credentials.type === 'username_password') {
        return await this.authenticatePassword(credentials);
      }
    }
  }
  
  // 4. Key Management
  class KeyManager {
    async generateKeys(): Promise<Keys> {
      // Generate strong keys
      return {
        encryptionKey: this.generateKey(256),
        authKey: this.generateKey(256)
      };
    }
    
    async rotateKeys(): Promise<void> {
      // Rotate keys periodically
      const newKeys = await this.generateKeys();
      await this.distributeKeys(newKeys);
    }
  }
}

Features:

  1. Dual VPN support: IPSec and SSL VPN
  2. Authentication: Multi-factor, certificates
  3. Key management: Generate, rotate, distribute
  4. Security: Strong encryption, authentication

Key Takeaways

  • VPN: Secure, encrypted connection over public network
  • IPSec: Network layer VPN, full network access, faster
  • SSL VPN: Application layer VPN, browser-based, easier setup
  • IPSec phases: Phase 1 (IKE), Phase 2 (IPSec SA)
  • IPSec modes: Transport (host-to-host), Tunnel (site-to-site)
  • Use cases: Remote access, site-to-site, secure communication
  • Best practices: Strong authentication, key rotation, monitor connections

About the author

InterviewCrafted helps you master system design with patience. We believe in curiosity-led engineering, reflective writing, and designing systems that make future changes feel calm.