Get a Pentest and security assessment of your IT network.

Cyber Security

Device Authentication Guide

TL;DR

This guide shows you how to check a device is who it says it is before letting it send data to your system. We’ll cover using SSH keys, API tokens and mutual TLS (mTLS) for secure authentication.

1. Understanding the Problem

Without checking a device’s identity, anyone could pretend to be a legitimate source and send malicious or incorrect data. Authentication verifies that devices are authorised before they can access your system.

2. Method 1: SSH Key Authentication (for command-line devices)

  1. Generate a key pair on the device: Use the ssh-keygen command.
    ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N "your_passphrase"

    This creates a private key (~/.ssh/id_rsa) and a public key (~/.ssh/id_rsa.pub). Keep the private key secret on the device.

  2. Copy the public key to your server: Use ssh-copy-id or manually append the contents of ~/.ssh/id_rsa.pub to the ~/.ssh/authorized_keys file on your server for the relevant user.
    ssh-copy-id user@yourserver
  3. Configure your server to require key authentication: Edit the /etc/ssh/sshd_config file. Set these options:
    • PubkeyAuthentication yes
    • PasswordAuthentication no (strongly recommended)
    • ChallengeResponseAuthentication no
  4. Restart the SSH service:
    sudo systemctl restart sshd

Now, only devices with the correct private key can connect.

3. Method 2: API Tokens (for applications)

  1. Generate unique tokens: Create a system to generate long, random strings for each device.
    openssl rand -base64 32

    Store these tokens securely in your database.

  2. Distribute tokens to devices: Provide the token to the application on each device during setup or registration.
  3. Require tokens with every API request: The device must include the token in the Authorization header of its requests.
    Authorization: Bearer YOUR_API_TOKEN
  4. Validate tokens on your server: Before processing any request, check if the provided token exists and is valid.

4. Method 3: Mutual TLS (mTLS) (for high security)

  1. Create a Certificate Authority (CA): This acts as a trusted root for your device certificates.
    openssl req -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -days 365
  2. Generate Device Certificates: Create a certificate for each device, signed by your CA.
    openssl req -newkey rsa:4096 -nodes -keyout device1.key -out device1.csr -days 365
    openssl x509 -req -in device1.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out device1.crt -days 365 -sha256
  3. Install CA Certificate on your Server: Configure your web server (e.g., Nginx, Apache) to trust the CA certificate.
  4. Configure Devices with Certificates: Install the device certificate and private key on each device.
  5. Enable mTLS on your Server: Configure your server to require client certificates for all connections.

mTLS provides strong authentication because both the server and client verify each other’s identities using certificates.

5. Important Considerations

  • Secure Storage: Always store private keys, tokens, and certificates securely.
  • Token Expiration: Implement token expiration to limit the impact of compromised credentials.
  • Revocation: Have a mechanism to revoke access for devices if they are lost or compromised.
  • Regular Audits: Regularly review your authentication system and security practices.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation