Class OpenPGPSmartCard

java.lang.Object
org.bouncycastle.openpgp.smartcard.OpenPGPSmartCard
Direct Known Subclasses:
SimulatorOpenPGPSmartCard, YubikeyOpenPGPSmartCard

public abstract class OpenPGPSmartCard extends Object
Abstract OpenPGP Smart Card class.
  • Field Details

  • Constructor Details

  • Method Details

    • getBackend

      public OpenPGPSmartCardBackend getBackend()
      Return the OpenPGPSmartCardBackend that manages this smart card.
      Returns:
      backend
    • getSerialNumber

      public abstract Integer getSerialNumber()
      Return the serial number of this smart card.
      Returns:
      serial number
    • getVersion

      public abstract String getVersion()
      Return the version number of this smart card.
      Returns:
      version number
    • putKey

      protected void putKey(OpenPGPHardwareKey key)
    • isKeySupported

      public abstract boolean isKeySupported(byte keyRef, org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey key) throws CardException
      Return true, if the smart card supports the given OpenPGPCertificate.OpenPGPComponentKey.
      Parameters:
      keyRef - key reference
      key - OpenPGP key
      Returns:
      true if the card supports the key
      Throws:
      CardException - if communication with the card fails
    • getKeyByKeyRef

      public OpenPGPHardwareKey getKeyByKeyRef(byte keyRef)
      Return the OpenPGPHardwareKey identified by the given key reference. If the card does not contain a key for the given keyRef, this method throws a NoSuchElementException.
      Parameters:
      keyRef - key reference
      Returns:
      hardware key
    • getKeyByFingerprint

      public OpenPGPHardwareKey getKeyByFingerprint(byte[] fingerprint)
      Return the OpenPGPHardwareKey from the slot that contains the given fingerprint. If no such key is found, this method throws a NoSuchElementException.

      Note: The fingerprint field of OpenPGP smart cards is a 20-octet field that can contain arbitrary data. Since the smart card does not make use of this field and does not validate its contents, you MUST NOT rely on this field to identify keys. Notably OpenPGP v6 keys, which have a 32-octet fingerprint, will cause mismatches with the 20-octet field.

      Parameters:
      fingerprint - fingerprint
      Returns:
      hardware key
    • getKeys

      public List<OpenPGPHardwareKey> getKeys()
      Return a list of all keys on the card.
      Returns:
      list of all keys
    • hasKey

      public boolean hasKey(byte keyRef)
      Return true, if the card has a key with the given key reference.
      Parameters:
      keyRef - key reference
      Returns:
      true if card has a key for keyRef, false otherwise
    • hasKeyWithFingerprint

      public boolean hasKeyWithFingerprint(byte[] fingerprint)
      Returns true, if the card has a key with the given fingerprint.

      Note: The fingerprint field of OpenPGP smart cards is a 20-octet field that can contain arbitrary data. Since the smart card does not make use of this field and does not validate its contents, you MUST NOT rely on this field to identify keys. Notably OpenPGP v6 keys, which have a 32-octet fingerprint, will cause mismatches with the 20-octet field.

      Parameters:
      fingerprint - fingerprint
      Returns:
      true if the card has a key with a matching fingerprint field
    • hasSignatureKey

      public boolean hasSignatureKey()
      Return true, if the card has a signature key (with keyRef OpenPGPHardwareKey.KEY_REF_SIGNATURE).
      Returns:
      true if card has signature key
    • getSignatureKey

      public OpenPGPHardwareKey getSignatureKey()
      Return the signing key of the card.
      Returns:
      signing key
      Throws:
      NoSuchElementException - if the card has no signing key
    • hasDecryptionKey

      public boolean hasDecryptionKey()
      Return true if the card has a decryption key (with keyRef OpenPGPHardwareKey.KEY_REF_DECRYPTION).
      Returns:
      true if card has decryption key
    • getDecryptionKey

      public OpenPGPHardwareKey getDecryptionKey()
      Return the decryption key of the card.
      Returns:
      decryption key
      Throws:
      NoSuchElementException - if the card has no decryption key
    • hasAuthenticationKey

      public boolean hasAuthenticationKey()
      Return true if the card has an authentication key (with keyRef OpenPGPHardwareKey.KEY_REF_AUTHENTICATION).
      Returns:
      true if card has authentication key
    • getAuthenticationKey

      public OpenPGPHardwareKey getAuthenticationKey()
      Return the authentication key of the card.
      Returns:
      authentication key
      Throws:
      NoSuchElementException - if the card has no authentication key
    • reset

      public abstract OpenPGPSmartCard reset() throws CardException
      Reset the smart card, clearing all key slots and resetting the admin PIN, user PIN to their defaults.
      Returns:
      this
      Throws:
      CardException - if communication with the card failed
    • uploadSigningKey

      public OpenPGPSmartCard uploadSigningKey(org.bouncycastle.openpgp.api.OpenPGPKey.OpenPGPPrivateKey key, char[] adminPin) throws CardException, org.bouncycastle.openpgp.PGPException
      Upload the given OpenPGPKey.OpenPGPPrivateKey to the signing key slot on the card.
      Parameters:
      key - OpenPGP private key
      adminPin - admin pin of the card
      Returns:
      card
      Throws:
      CardException - if communication with the card fails
      org.bouncycastle.openpgp.PGPException - if the key cannot be prepared for the card
    • uploadDecryptionKey

      public OpenPGPSmartCard uploadDecryptionKey(org.bouncycastle.openpgp.api.OpenPGPKey.OpenPGPPrivateKey key, char[] adminPin) throws CardException, org.bouncycastle.openpgp.PGPException
      Upload the given OpenPGPKey.OpenPGPPrivateKey to the decryption key slot on the card.
      Parameters:
      key - OpenPGP private key
      adminPin - admin pin of the card
      Returns:
      card
      Throws:
      CardException - if communication with the card fails
      org.bouncycastle.openpgp.PGPException - if the key cannot be prepared for the card
    • uploadAuthenticationKey

      public OpenPGPSmartCard uploadAuthenticationKey(org.bouncycastle.openpgp.api.OpenPGPKey.OpenPGPPrivateKey key, char[] adminPin) throws CardException, org.bouncycastle.openpgp.PGPException
      Upload the given OpenPGPKey.OpenPGPPrivateKey to the authentication key slot on the card.
      Parameters:
      key - OpenPGP private key
      adminPin - admin pin of the card
      Returns:
      card
      Throws:
      CardException - if communication with the card fails
      org.bouncycastle.openpgp.PGPException - if the key cannot be prepared for the card
    • uploadKey

      public abstract OpenPGPSmartCard uploadKey(byte keyRef, org.bouncycastle.openpgp.api.OpenPGPKey.OpenPGPPrivateKey key, char[] adminPin) throws CardException, org.bouncycastle.openpgp.PGPException
      Upload the given OpenPGPKey.OpenPGPPrivateKey to the given keyRef slot on the card.
      Parameters:
      keyRef - keyRef
      key - OpenPGP private key
      adminPin - admin pin of the card
      Returns:
      card
      Throws:
      CardException - if communication with the card fails
      org.bouncycastle.openpgp.PGPException - if the key cannot be prepared for the card
    • reconstructPGPPublicKey

      public abstract org.bouncycastle.openpgp.PGPPublicKey reconstructPGPPublicKey(byte keyRef) throws CardException, org.bouncycastle.openpgp.PGPException
      Return the PGPPublicKey component of the key in the slot identified by keyRef.

      Note: The key might be reconstructed on the fly. In this case, Public key algorithm and version number might be brute-forced by comparing the resulting keys fingerprint to the contents of the cards OpenPGPHardwareKey.getFingerprint() field.

      Parameters:
      keyRef - key reference
      Returns:
      PGPPublicKey representation
      Throws:
      CardException - if communication with the card fails
      org.bouncycastle.openpgp.PGPException - if the key cannot be reconstructed
    • getCardType

      public abstract String getCardType()
      Return a String representing the card type. This is an informative description of the device.
      Returns:
      card type
    • toString

      public String toString()
      Overrides:
      toString in class Object