Class X509Key

  • All Implemented Interfaces:
    java.io.Serializable, java.security.Key, java.security.PublicKey
    Direct Known Subclasses:
    DSAPublicKey, RSAPublicKey

    public class X509Key
    extends java.lang.Object
    implements java.security.PublicKey
    Holds an X.509 key, for example a public key found in an X.509 certificate. Includes a description of the algorithm to be used with the key; these keys normally are used as "SubjectPublicKeyInfo".

    While this class can represent any kind of X.509 key, it may be desirable to provide subclasses which understand how to parse keying data. For example, RSA public keys have two members, one for the public modulus and one for the prime exponent. If such a class is provided, it is used when parsing X.509 keys. If one is not provided, the key still parses correctly.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      X509Key()
      Default constructor.
      X509Key​(AlgorithmId algid, byte[] key)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void decode​(byte[] encodedKey)  
      void decode​(java.io.InputStream in)
      Initialize an X509Key object from an input stream.
      byte[] encode()
      Returns the DER-encoded form of the key as a byte array.
      void encode​(DerOutputStream out)
      Encode SubjectPublicKeyInfo sequence on the DER output stream.
      boolean equals​(java.lang.Object object)  
      java.lang.String getAlgorithm()
      Returns the algorithm to be used with this key.
      AlgorithmId getAlgorithmId()
      Returns the algorithm ID to be used with this key.
      byte[] getEncoded()
      Returns the DER-encoded form of the key as a byte array.
      java.lang.String getFormat()
      Returns the format for this key: "X.509"
      byte[] getKey()
      Returns the raw key as a byte array
      int hashCode()
      Calculates a hash code value for the object.
      static X509Key parse​(DerValue in)
      Construct X.509 subject public key from a DER value.
      protected void parseKeyBits()
      Parse the key bits.
      static java.security.PublicKey parsePublicKey​(DerValue in)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • key

        protected byte[] key
      • encodedKey

        protected byte[] encodedKey
    • Constructor Detail

      • X509Key

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

        public X509Key​(AlgorithmId algid,
                       byte[] key)
                throws java.security.InvalidKeyException
        Throws:
        java.security.InvalidKeyException
    • Method Detail

      • parse

        public static X509Key parse​(DerValue in)
                             throws java.io.IOException
        Construct X.509 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 X509Key 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:
        java.io.IOException - on data format errors
      • parseKeyBits

        protected void parseKeyBits()
                             throws java.io.IOException,
                                    java.security.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 X.509 SubjectPublicKeyInfo values using the X509Key member functions, such as parse and decode.

        Throws:
        java.io.IOException - on parsing errors.
        java.security.InvalidKeyException - on invalid key encodings.
      • getAlgorithm

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

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

        public final void encode​(DerOutputStream out)
                          throws java.io.IOException
        Encode SubjectPublicKeyInfo sequence on the DER output stream.
        Throws:
        java.io.IOException - on encoding errors.
      • getEncoded

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

        public java.lang.String getFormat()
        Returns the format for this key: "X.509"
        Specified by:
        getFormat in interface java.security.Key
      • getKey

        public byte[] getKey()
        Returns the raw key as a byte array
      • encode

        public byte[] encode()
                      throws java.security.InvalidKeyException
        Returns the DER-encoded form of the key as a byte array.
        Throws:
        java.security.InvalidKeyException - on encoding errors.
      • toString

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

        public void decode​(java.io.InputStream in)
                    throws java.security.InvalidKeyException
        Initialize an X509Key object from an input stream. The data on that input stream must be encoded using DER, obeying the X.509 SubjectPublicKeyInfo format. That is, the data is a sequence consisting of 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.

        The exception to this rule is that since private keys need not be encoded using the X.509 SubjectPublicKeyInfo format, private keys may override this method, encode, and of course getFormat.

        Parameters:
        in - an input stream with a DER-encoded X.509 SubjectPublicKeyInfo value
        Throws:
        java.security.InvalidKeyException - on parsing errors.
      • decode

        public void decode​(byte[] encodedKey)
                    throws java.security.InvalidKeyException
        Throws:
        java.security.InvalidKeyException
      • equals

        public boolean equals​(java.lang.Object object)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public static java.security.PublicKey parsePublicKey​(DerValue in)
                                                      throws java.io.IOException
        Throws:
        java.io.IOException