codec.pkcs8
Class PrivateKeyInfo

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--java.util.ArrayList
                    |
                    +--codec.asn1.ASN1AbstractCollection
                          |
                          +--codec.asn1.ASN1Sequence
                                |
                                +--codec.pkcs8.PrivateKeyInfo
All Implemented Interfaces:
ASN1Collection, ASN1Type, Cloneable, Collection, Externalizable, List, Serializable
Direct Known Subclasses:
KeyBag

public class PrivateKeyInfo
extends ASN1Sequence

This class represents a PrivateKeyInfo as defined in PKCS#8. The ASN.1 definition of this structure is

 PrivateKeyInfo ::= SEQUENCE (
   version Version,  -- 0 for version 1.2 Nov 93
   privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
   privateKey PrivateKey,
   attributes [0] IMPLICIT Attributes OPTIONAL
 }
 Version ::= INTEGER
 PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
 PrivateKey ::= OCTET STRING
 Attributes ::= SET OF Attribute
 
The following definitions are taken from the X501 standard:
 Attribute ::= SEQUENCE {
   type AttributeType
   values SET OF AttributeValue
   -- at least one value is required --
 }
 AttributeType ::= OBJECT IDENTIFIER
 AttributeValue ::= ANY
 

Version:
"$Id: PrivateKeyInfo.java,v 1.3 2004/08/24 10:01:21 pebinger Exp $"
Author:
Markus Tak, Volker Roth
See Also:
Serialized Form

Field Summary
protected  AlgorithmIdentifier algorithm_
          The OID of the private key algorithm used in this structure.
protected  ASN1Set attributes_
          Attributes are the extended information that is encrypted along with the private-key information.
static int VERSION
          The default version.
protected  ASN1Integer version_
          Version is the syntax version number, for compatibility with future revisions of the .
static int VERSION_1_2
          Version 1.2 November 1993 identifier.
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
PrivateKeyInfo()
          This constructor builds the data structure.
PrivateKeyInfo(AlgorithmIdentifier aid, ASN1Type key)
          Creates an instance with the given ASN.1 raw key.
PrivateKeyInfo(AlgorithmIdentifier aid, byte[] key)
          Creates an instance with the given pre encoded raw key.
PrivateKeyInfo(PrivateKey key)
          Creates an instance with the given private key.
 
Method Summary
 AlgorithmIdentifier getAlgorithmIdentifier()
          Returns the AlgorithmIdentifier of the embedded key.
 List getAttributes()
          Returns an unmodifiable list view on the attributes.
 ASN1Type getDecodedRawKey()
          Returns an ASN.1 type that represents the decoded raw key.
 PrivateKey getPrivateKey()
          Returns the private key embedded in this structure.
 byte[] getRawKey()
          Returns the raw key material.
 int getVersion()
          Returns the version number of this instance.
 void setAlgorithm(AlgorithmIdentifier aid)
          Sets the AlgorithmIdentifier of this instance.
 void setAttributes(Collection attributes)
          Sets the given attributes.
 void setPrivateKey(PrivateKey key)
          Initializes this instance with the given private key.
protected  void setRawKey(ASN1Type key)
          Encodes and sets the given ASN.1 key structure as the raw key.
 void setVersion(int version)
          Sets the version number of this instance.
 
Methods inherited from class codec.asn1.ASN1Sequence
getTag
 
Methods inherited from class codec.asn1.ASN1AbstractCollection
checkConstraints, decode, encode, getCollection, getConstraint, getTagClass, getValue, isExplicit, isOptional, isType, readExternal, setConstraint, setExplicit, setOptional, toString, writeExternal
 
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, get, indexOf, isEmpty, lastIndexOf, remove, removeRange, set, size, toArray, toArray, trimToSize
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, remove, removeAll, retainAll
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeAll, retainAll, subList
 

Field Detail

VERSION

public static final int VERSION
The default version.

VERSION_1_2

public static final int VERSION_1_2
Version 1.2 November 1993 identifier.

version_

protected ASN1Integer version_
Version is the syntax version number, for compatibility with future revisions of the . It shall be 0 for that version.

algorithm_

protected AlgorithmIdentifier algorithm_
The OID of the private key algorithm used in this structure.

attributes_

protected ASN1Set attributes_
Attributes are the extended information that is encrypted along with the private-key information.
Constructor Detail

PrivateKeyInfo

public PrivateKeyInfo()
This constructor builds the data structure.

PrivateKeyInfo

public PrivateKeyInfo(AlgorithmIdentifier aid,
                      byte[] key)
Creates an instance with the given pre encoded raw key. The encoded is embedded "as is", the key encoding can be either a DER compliant one or a special encoding. Please note that the byte array returned by the getEncoded() method of the Key interface must not be passed to this constructor because the bytes returned by this method do not contain a raw key but a complete PrivateKeyInfo structure (as this one).
Parameters:
aid - The AlgorithmIdentifier with the OID and parameters for the raw algorithm that belongs to the given key.
key - The raw key that shall be wrapped in this instance.

PrivateKeyInfo

public PrivateKeyInfo(AlgorithmIdentifier aid,
                      ASN1Type key)
Creates an instance with the given ASN.1 raw key. The given raw key is encoded using DER before it is set up in this instance.

Parameters:
aid - The AlgorithmIdentifier with the OID and parameters for the raw algorithm that belongs to the given key.
key - The raw key that shall be wrapped in this instance.
Throws:
InconsistentStateException - if an exception is thrown while encoding the given key. No such exception should ever happen.

PrivateKeyInfo

public PrivateKeyInfo(PrivateKey key)
               throws InvalidKeyException
Creates an instance with the given private key.
Parameters:
key - the actual private key as a java object
Throws:
NullPointerException - if the given key is null.
Method Detail

getAlgorithmIdentifier

public AlgorithmIdentifier getAlgorithmIdentifier()
Returns the AlgorithmIdentifier of the embedded key.
Returns:
The AlgorithmIdentifier.

getPrivateKey

public PrivateKey getPrivateKey()
                         throws NoSuchAlgorithmException
Returns the private key embedded in this structure.

This method creates an PKCS8EncodedKeySpec of this instance and feeds it into a key factory. In order to locate a suitable key factory, the installed providers must define appropriate OID mappings.

Returns:
The private key.
Throws:
InconsistentStateException - if the key spec generated by this method is rejected by the key factory that is used to generate the key.
NoSuchAlgorithmException - if there is no key factory registered for the algorithm of the embedded key or no appropriate OID mapping is defined by the installed providers.

setPrivateKey

public void setPrivateKey(PrivateKey key)
                   throws InvalidKeyException
Initializes this instance with the given private key.
Parameters:
key - The private key from which this instance is initialized.
Throws:
InvalidKeyException - if the given key cannot be decoded properly.
NullPointerException - if the given key is null.

getVersion

public int getVersion()
Returns the version number of this instance.
Returns:
The version number.

setVersion

public void setVersion(int version)
Sets the version number of this instance.
Parameters:
version - The version number.

setAlgorithm

public void setAlgorithm(AlgorithmIdentifier aid)
Sets the AlgorithmIdentifier of this instance. This algorithm identifier must match the raw key of this instance.

The given instance is set up in this structure. Side effects will occur if it is modified subsequently.

Parameters:
aid - The AlgorithmIdentifier.

setRawKey

protected void setRawKey(ASN1Type key)
Encodes and sets the given ASN.1 key structure as the raw key.
Throws:
InconsistentStateException - if an internal error occurs while the key is encoded. This should never happen.

getAttributes

public List getAttributes()
Returns an unmodifiable list view on the attributes.
Returns:
The attributes.

setAttributes

public void setAttributes(Collection attributes)
Sets the given attributes.
Parameters:
attributes - The attributes.

getRawKey

public byte[] getRawKey()
Returns the raw key material. The key material consists of an encoded key structure. ASN.1/DER is often used as the encoding. However, this need not always be the case. Elliptic curve cryptosystems use specific encodings.

If the key encoding is ASN.1/DER then the raw key can be retrieved as an ASN.1 type by means of the getDecodedRawKey() method.

The returned value is a copy. No side effects are caused by modifying it.

Returns:
The raw key bits as a byte array.

getDecodedRawKey

public ASN1Type getDecodedRawKey()
                          throws CorruptedCodeException
Returns an ASN.1 type that represents the decoded raw key. Decoding is done by means of ASN.1/DER. Be careful, not all public keys are encoded according to DER. Elliptic curve cryptosystems use specific encodings.
Returns:
The raw key decoded according to DER.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.