Class C509PrivateKey

java.lang.Object
org.bouncycastle.cbor.c509.C509PrivateKey
All Implemented Interfaces:
Destroyable

public class C509PrivateKey extends Object implements Destroyable
A C509 private key structure (Section 3.6 of draft-ietf-cose-cbor-encoded-cert-20):
C509PrivateKey = [
   C509PrivateKeyType: int,
   subjectPrivateKeyAlgorithm: AlgorithmIdentifier,
   subjectPrivateKey: any,
]
For type 0 the subjectPrivateKey is the PrivateKey OCTET STRING value field of RFC 5958 as a CBOR byte string; for type 1 it is a COSE_Key structure (RFC 9052), carried here uninterpreted. The subjectPrivateKeyAlgorithm draws from the C509 Public Key Algorithms Registry (Section 8.15).

The structure transports secret material, so it is Destroyable: after destroy() the key bytes are zeroized and the accessors fail.

  • Field Details

    • TYPE_ASYMMETRIC_KEY_PACKAGE

      public static final int TYPE_ASYMMETRIC_KEY_PACKAGE
      subjectPrivateKey is the RFC 5958 PrivateKey OCTET STRING value field as a byte string.
      See Also:
    • TYPE_COSE_KEY

      public static final int TYPE_COSE_KEY
      subjectPrivateKey is a COSE_Key structure (RFC 9052) containing a private key.
      See Also:
  • Method Details

    • getInstance

      public static C509PrivateKey getInstance(byte[] encoding) throws IOException
      Parse a C509 private key structure from its CBOR encoding.
      Throws:
      IOException
    • fromPrivateKeyInfo

      public static C509PrivateKey fromPrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo privateKeyInfo) throws IOException
      Build a type 0 C509 private key from a PKCS#8 / RFC 5958 PrivateKeyInfo. The privateKeyAlgorithm becomes the subjectPrivateKeyAlgorithm and the PrivateKey OCTET STRING value field the subjectPrivateKey.
      Throws:
      IOException
    • toPrivateKeyInfo

      public org.bouncycastle.asn1.pkcs.PrivateKeyInfo toPrivateKeyInfo() throws IOException
      Return the PKCS#8 / RFC 5958 PrivateKeyInfo view of a type 0 C509 private key.
      Throws:
      IllegalStateException - if this is a type 1 (COSE_Key) structure or the key has been destroyed.
      IOException
    • getPrivateKeyType

      public int getPrivateKeyType()
      Return the private key type (TYPE_ASYMMETRIC_KEY_PACKAGE or TYPE_COSE_KEY).
    • getSubjectPrivateKeyAlgorithm

      public C509AlgorithmIdentifier getSubjectPrivateKeyAlgorithm()
      Return the subject private key algorithm.
    • getSubjectPrivateKeyEncoding

      public byte[] getSubjectPrivateKeyEncoding()
      Return the CBOR encoding of the subjectPrivateKey item - the key material.
    • getEncoded

      public byte[] getEncoded() throws IOException
      Return the complete CBOR encoding of this structure.
      Throws:
      IOException
    • destroy

      public void destroy() throws DestroyFailedException
      Specified by:
      destroy in interface Destroyable
      Throws:
      DestroyFailedException
    • isDestroyed

      public boolean isDestroyed()
      Specified by:
      isDestroyed in interface Destroyable
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object