Class PKCS8Key

java.lang.Object
org.mozilla.jss.netscape.security.pkcs.PKCS8Key
All Implemented Interfaces:
Serializable, Key, PrivateKey, Destroyable
Direct Known Subclasses:
DSAPrivateKey

public class PKCS8Key extends Object implements PrivateKey
Holds a PKCS#8 key, for example a private key
Version:
1.30, 97/12/10
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      use serialVersionUID from JDK 1.1. for interoperability
      See Also:
    • algid

      protected AlgorithmId algid
    • key

      protected byte[] key
    • encodedKey

      protected byte[] encodedKey
    • VERSION

      public static final BigInteger VERSION
  • Constructor Details

    • PKCS8Key

      public PKCS8Key()
      Default constructor. The key constructed must have its key and algorithm initialized before it may be used, for example by using decode.
  • Method Details

    • parse

      public static PKCS8Key parse(DerValue in) throws IOException
      Construct PKCS#8 subject public key from a DER value. If the runtime environment is configured with a specific class for this kind of key, a subclass is returned. Otherwise, a generic PKCS8Key object is returned.

      This mechanism gurantees that keys (and algorithms) may be freely manipulated and transferred, without risk of losing information. Also, when a key (or algorithm) needs some special handling, that specific need can be accomodated.

      Parameters:
      in - the DER-encoded SubjectPublicKeyInfo value
      Throws:
      IOException - on data format errors
    • parseKeyBits

      protected void parseKeyBits() throws IOException, InvalidKeyException
      Parse the key bits. This may be redefined by subclasses to take advantage of structure within the key. For example, RSA public keys encapsulate two unsigned integers (modulus and exponent) as DER values within the key bits; Diffie-Hellman and DSS/DSA keys encapsulate a single unsigned integer.

      This function is called when creating PKCS#8 SubjectPublicKeyInfo values using the PKCS8Key member functions, such as parse and decode.

      Throws:
      IOException - if a parsing error occurs.
      InvalidKeyException - if the key encoding is invalid.
    • buildPKCS8Key

      public static PKCS8Key buildPKCS8Key(AlgorithmId algid, byte[] key) throws IOException, InvalidKeyException
      Throws:
      IOException
      InvalidKeyException
    • getAlgorithm

      public String getAlgorithm()
      Returns the algorithm to be used with this key.
      Specified by:
      getAlgorithm in interface Key
    • getAlgorithmId

      public AlgorithmId getAlgorithmId()
      Returns the algorithm ID to be used with this key.
    • encode

      public final void encode(DerOutputStream out) throws IOException
      PKCS#8 sequence on the DER output stream.
      Throws:
      IOException
    • getEncoded

      public byte[] getEncoded()
      Returns the DER-encoded form of the key as a byte array.
      Specified by:
      getEncoded in interface Key
    • getFormat

      public String getFormat()
      Returns the format for this key: "PKCS#8"
      Specified by:
      getFormat in interface Key
    • encode

      public byte[] encode() throws InvalidKeyException
      Returns the DER-encoded form of the key as a byte array.
      Throws:
      InvalidKeyException - if an encoding error occurs.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • decode

      public void decode(InputStream in) throws InvalidKeyException
      Initialize an PKCS8Key object from an input stream. The data on that input stream must be encoded using DER, obeying the PKCS#8 format: a sequence consisting of a version, an algorithm ID and a bit string which holds the key. (That bit string is often used to encapsulate another DER encoded sequence.)

      Subclasses should not normally redefine this method; they should instead provide a parseKeyBits method to parse any fields inside the key member.

      Parameters:
      in - an input stream with a DER-encoded PKCS#8 SubjectPublicKeyInfo value
      Throws:
      InvalidKeyException - if a parsing error occurs.
    • decode

      public void decode(byte[] encodedKey) throws InvalidKeyException
      Throws:
      InvalidKeyException
    • writeObject

      private void writeObject(ObjectOutputStream stream) throws IOException
      Serialization write ... PKCS#8 keys serialize as themselves, and they're parsed when they get read back.
      Throws:
      IOException
    • readObject

      private void readObject(ObjectInputStream stream) throws IOException
      Serialization read ... PKCS#8 keys serialize as themselves, and they're parsed when they get read back.
      Throws:
      IOException
    • copyEncodedKey

      private byte[] copyEncodedKey(byte[] encodedKey)
    • encode

      static void encode(DerOutputStream out, AlgorithmId algid, byte[] key) throws IOException
      Throws:
      IOException
    • equals

      public boolean equals(Object object)
      Compares two private keys. This returns false if the object with which to compare is not of type Key. Otherwise, the encoding of this key object is compared with the encoding of the given key object.
      Overrides:
      equals in class Object
      Parameters:
      object - the object with which to compare
      Returns:
      true if this key has the same encoding as the object argument; false otherwise.
    • hashCode

      public int hashCode()
      Calculates a hash code value for this object. Objects which are equal will also have the same hashcode.
      Overrides:
      hashCode in class Object