Oracle’s guidance for US Government Cloud and US Defense Cloud describes using CAC/PIV authentication with third-party tools to secure access to compute hosts. Examples include PuTTY-CAC for Windows and OpenSC for macOS. Under Oracle’s shared responsibility model, customers are responsible for securely configuring their cloud resources. This tutorial demonstrates how to configure token-based SSH access to a Linux VM.


This tutorial is based on a customer requirement to access an OCI Linux VM using a smart card and PIN. The customer wanted the SSH private key to remain on the smart card instead of being stored as a file on a laptop.

We show two options: a physical YubiKey configured for PIV, and SoftHSM2 for testing without physical hardware. We use a Mac for this demonstration. The overall steps are similar on other operating systems, although installation commands and library paths may differ.

How PIV/CAC SSH Authentication Works on OCI

The SSH client uses a PKCS#11 library to communicate with the token. After you enter the token’s PIN, the token uses its private key to sign authentication data.

The Linux VM checks the signature against the public key in the user’s authorized_keys file. For the physical-token option, the private key remains on the card. With SoftHSM2, the token is stored in software.

Note: This tutorial uses an OCI Linux VM. The same SSH authentication approach can be used with a compatible Linux VM on AWS, Azure, Google Cloud, or your local computer. The steps for creating the VM and configuring network access will depend on your environment.

Prerequisites

For this tutorial (VM in OCI), you will need:

  • An OCI tenancy with permission to create a Linux instance and configure its network access.
  • A computer with an SSH client that supports PKCS#11.
  • One of the following:
    • A YubiKey configured for PIV.
    • A PIV/CAC smart card with an existing key and certificate, plus a compatible card reader if needed.
    • A software HSM such as SoftHSM2.
  • OpenSC, which provides the pkcs11-tool command and support for compatible physical smart cards.
  • YubiKey Manager (ykman) if you are preparing a test YubiKey.

We use a Mac for this demonstration. The overall steps are similar on other operating systems, although installation commands and library paths may differ.

Install the required tools (Steps for a Mac):

Use Homebrew to install OpenSSH and OpenSC:

    brew install openssh
    brew install --cask opensc

    Then install the tool for your chosen option, we have 2 options here:

    Option A: Physical YubiKey:

    brew install ykman

    Use a dedicated test YubiKey with slot 9a available for testing. In Step 1 below we generate a key and certificate in that slot.

    Option B: SoftHSM2:

    brew install softhsm

    This can be a good option to understand the authentication process; there is no need for a physical card or card reader for this option. In Step 1 below we create the software token, set its PINs, and generate a key pair.

    Step 1: Prepare a Token and export its Public Key

    Choose one of these two options:

    • Option A: Use a physical YubiKey configured for PIV.
      • Option B: Use SoftHSM2 for testing.

    Both options produce an OpenSSH public key that you will use when creating the OCI Linux instance.

    If you already have an organization-issued PIV/CAC card, use its existing credentials and approved middleware to export the public key. The YubiKey setup commands should not be used to prepare that card.

    Option A: Use a physical YubiKey configured for PIV

    Connect a dedicated test YubiKey.


    Note: The following commands replace the key and certificate in slot 9a. Use them only if that slot is available for testing. If your YubiKey or organization-issued PIV/CAC card already has suitable credentials, skip key generation and export its public key using your approved middleware.

    1. Generate an ECDSA P-256 key pair in the YubiKey’s PIV authentication slot:
    ykman piv keys generate --algorithm ECCP256 9a pubkey.pem
    
    • Create a self-signed certificate for the test key:
    ykman piv certificates generate --subject "CN=OCI-SSH" 9a pubkey.pem

    Enter the management key and PIN when prompted. These commands follow Yubico’s documented PIV setup. YubiKey Manager (ykman) CLI Guide documentation

    • Export the public key in OpenSSH format:
    "$(brew --prefix openssh)/bin/ssh-keygen" -D /Library/OpenSC/lib/opensc-pkcs11.so

    Copy the complete output line beginning with:

    ecdsa-sha2-nistp256 AAAA...

    You will paste this public key into OCI when creating the Linux instance.

    Note: The library path above is the standard location for the OpenSC macOS installer.

    Option B: Use SoftHSM2 for testing

    SoftHSM2 lets you test the PKCS#11 SSH workflow without a physical card. It stores the token in software and does not provide the hardware protection of a YubiKey or smart card.

    1.  Create a software token
    softhsm2-util --init-token --slot 0 --label "Virtual-PIV"

    This command initializes a token in slot 0 and names it Virtual-PIV. Use an unused slot for this demonstration. When prompted, create a Security Officer (SO) PIN for administering the token and a User PIN for accessing its private key. For other initialization options, see the SoftHSM2 documentation.

    • Set the SoftHSM2 library path

    SOFTHSM_MODULE="$(brew --prefix softhsm)/lib/softhsm/libsofthsm2.so"

    • Generate a key pair inside the software token
    /Library/OpenSC/bin/pkcs11-tool \
      --module "$SOFTHSM_MODULE" \
      --token-label "Virtual-PIV" \
      --login \
      --keypairgen \
      --key-type EC:prime256v1 \
      --id 01 \
      --label "OCI-SSH-Key"

    Enter the User PIN (example 3223224455). This generates an ECDSA P-256 key pair in the selected token.

    • Export the public key in OpenSSH format
    "$(brew --prefix openssh)/bin/ssh-keygen" -D "$SOFTHSM_MODULE"

    Copy the complete output line beginning with:

    ecdsa-sha2-nistp256 AAAA...

    You will paste this public key into OCI when creating the Linux instance.

    After completing either Option A or Option B, you have an OpenSSH public key. Next, create the VM, allow SSH access, and connect using your token.

    Step 2: Create the OCI Linux instance

    1. Sign in to the OCI Console.
    2. Open Compute → Instances and select Create instance.
    3. Enter an instance name and select your compartment.
    4. Select Oracle Linux 9 and a suitable shape.
    5. For this demonstration, select a public subnet and assign a public IPv4 address. The subnet must have a route to an internet gateway.
    6. Under Add SSH keys, select Paste public keys.
    7. Paste the complete public-key line exported in Step 1: “ecdsa-sha2-nistp256 AAAA...“
    8. Select Create.
    9. When the instance is running, copy its public IP address.

    OCI adds the supplied public key to /home/opc/.ssh/authorized_keys for Oracle-provided Oracle Linux images.

    Step 3: Allow SSH access to the VM

    Check the security list associated with the instance’s subnet, or a network security group attached to the instance. Add an ingress rule if SSH access is not already allowed:

    Allow inbound TCP traffic on destination port 22 from your workstation’s public IPv4 address, using a /32 source CIDR.

    Step 4: Connect to the VM

    Connect using CLI:

    In the below commands replace <VM_PUBLIC_IP> with your instance’s public IP address. Run the command for your chosen option.

    Option A: Using OpenSC with a physical PIV/CAC card or YubiKey:

    /opt/homebrew/bin/ssh -I /Library/OpenSC/lib/opensc-pkcs11.so opc@<VM_PUBLIC_IP>

    Option B: SoftHSM2:

    /opt/homebrew/bin/ssh -I /opt/homebrew/lib/softhsm/libsofthsm2.so opc@<VM_PUBLIC_IP>

    Note: These commands use Homebrew OpenSSH and the library paths for our demonstration. Adjust the paths for your installation.

    On the first connection, verify the server’s host-key fingerprint before entering yes.

    When prompted with Enter PIN for ‘Virtual-PIV’, enter the User PIN (Example from above 3223224455) you created during token setup. For a physical card or YubiKey, enter its PIV PIN.

    Optional:
    To simplify future connections and avoid typing the PKCS#11 library path on every connection, add a host alias to ~/.ssh/config:

    Host oci-piv-vm

        HostName <VM_PUBLIC_IP>

        User opc

        PKCS11Provider /opt/homebrew/lib/softhsm/libsofthsm2.so

    You can now connect simply by running:

    /opt/homebrew/bin/ssh oci-piv-vm