pk.edu.niit.clarens.util
Class PEMBlock

java.lang.Object
  extended bypk.edu.niit.clarens.util.PEMBlock
Direct Known Subclasses:
CertificateBlock, PKCS8UnencryptedPrivateKeyBlock, RSAPrivateKeyBlock

public abstract class PEMBlock
extends java.lang.Object

This class represents an arbitrary block of PEM encoded data. The getInstance(java.lang.String) method is used to create a type specific instance representing the actual PEM block type. PEM block types include PKCS8 private keys and PEM encoded certificates.


Field Summary
protected  java.lang.String base64BlockData
           
protected  java.security.cert.Certificate certificate
           
protected  byte[] derBlockData
           
protected  java.lang.String footer
           
protected  java.lang.String header
           
static int PEM_CERTIFICATE
          A PEM-encoded certificate.
static int PEM_OPENSSL_DSA
          An openssl traditional format private DSA key.
static int PEM_OPENSSL_RSA
          An openssl traditional format private RSA key.
static int PEM_PKCS8_ENCRYPTED
          A PKCS#8 encrypted private key.
static int PEM_PKCS8_UNENCRYPTED
          A PKCS#8 unencrypted private key.
static int PEM_UNKNOWN
          An unknown PEM-encoded block.
protected  java.lang.String PEMdata
           
protected  java.security.PrivateKey privateKey
           
protected  int type
           
protected  java.lang.String typeString
           
 
Method Summary
abstract  java.security.cert.Certificate getCertificate()
          Get the certificate represented by this block.
static PEMBlock getInstance(java.io.BufferedReader data)
          Load a PEM-encoded certificate or key block from an input reader.
static PEMBlock getInstance(java.lang.String data)
          Load a PEM-encoded certificate or key block from a string.
 java.lang.String getPem()
          Get the PEM data as a string.
abstract  java.security.PrivateKey getPrivateKey()
          Get the unencrypted private key structure represented by this block.
abstract  java.security.PrivateKey getPrivateKey(java.lang.String passphrase)
          Get the encrypted private key structure represented by this block.
 int getType()
          Get the type of block.
 java.lang.String getTypeString()
          Get a string description of this block type.
protected  void load(java.lang.String s)
          Decode a PEM encoded block into more usable data.
protected static java.lang.String ReadBeginEnd(java.io.BufferedReader inputReader)
          Returns the text between -----BEGIN XXXX----- and -----END XXXX----- read from the provided inputReader.
protected static java.lang.String ReadBeginEnd(java.lang.String inputString)
          Returns the text between -----BEGIN XXXX----- and -----END XXXX----- read from the provided string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PEM_UNKNOWN

public static final int PEM_UNKNOWN
An unknown PEM-encoded block.

See Also:
Constant Field Values

PEM_PKCS8_UNENCRYPTED

public static final int PEM_PKCS8_UNENCRYPTED
A PKCS#8 unencrypted private key.

See Also:
Constant Field Values

PEM_PKCS8_ENCRYPTED

public static final int PEM_PKCS8_ENCRYPTED
A PKCS#8 encrypted private key.

See Also:
Constant Field Values

PEM_OPENSSL_RSA

public static final int PEM_OPENSSL_RSA
An openssl traditional format private RSA key.

See Also:
Constant Field Values

PEM_OPENSSL_DSA

public static final int PEM_OPENSSL_DSA
An openssl traditional format private DSA key.

See Also:
Constant Field Values

PEM_CERTIFICATE

public static final int PEM_CERTIFICATE
A PEM-encoded certificate.

See Also:
Constant Field Values

header

protected java.lang.String header

footer

protected java.lang.String footer

base64BlockData

protected java.lang.String base64BlockData

PEMdata

protected java.lang.String PEMdata

derBlockData

protected byte[] derBlockData

type

protected int type

typeString

protected java.lang.String typeString

privateKey

protected java.security.PrivateKey privateKey

certificate

protected java.security.cert.Certificate certificate
Method Detail

getInstance

public static PEMBlock getInstance(java.lang.String data)
                            throws java.security.cert.CertificateException,
                                   java.io.IOException
Load a PEM-encoded certificate or key block from a string. Any data preceding the -----BEGIN line is discarded before passing it on to the type-specific object constructor (such as CertificateBlock.

Parameters:
data - The string containing the PEM-encoded certificate or key.
Returns:
A type-specific data structure.
Throws:
java.io.IOException - Thrown if there was a problem decoding the base64 PEM data.
java.security.cert.CertificateException - Thrown if a valid PEM block could not be found. This may occur if no BEGIN line is found, or no matching END line is found.

getInstance

public static PEMBlock getInstance(java.io.BufferedReader data)
                            throws java.io.IOException,
                                   java.security.cert.CertificateException
Load a PEM-encoded certificate or key block from an input reader.

Parameters:
data - The string containing the PEM-encoded certificate or key.
Returns:
A type-specific data structure.
Throws:
java.io.IOException - Thrown if there was a problem decoding the base64 PEM data.
java.security.cert.CertificateException - Thrown if a valid PEM block could not be found. This may occur if no BEGIN line is found, or no matching END line is found.

load

protected void load(java.lang.String s)
             throws java.security.cert.CertificateException,
                    java.io.IOException
Decode a PEM encoded block into more usable data. The header and footer are removed from the block and the data is base64 decoded back into DER data.

Parameters:
s - The string containing the PEM encoded data.
Throws:
java.security.cert.CertificateException - Thrown if there was a problem in the PEM structure.
java.io.IOException - Thrown if there was a problem decoding the base64 data.

getPem

public java.lang.String getPem()
Get the PEM data as a string.

Returns:
A string containing the entire PEM block, including the BEGIN and END markers.

getType

public int getType()
Get the type of block.

Returns:
An integer describing the type of block that has been loaded. PEM_UNKNOWN is returned if nothing has been loaded or if the block type could not be determined.

getTypeString

public java.lang.String getTypeString()
Get a string description of this block type.

Returns:
A string describing the data in the block.

getPrivateKey

public abstract java.security.PrivateKey getPrivateKey()
                                                throws java.security.cert.CertificateException
Get the unencrypted private key structure represented by this block. The private key is extracted from the block data.

Returns:
The private key structure represented by the block.
Throws:
java.security.cert.CertificateException - Thrown if there was a problem extracting the private key from the block data. This can occur if the private key format is unsupported (openssl DSA) or if the private key was encrypted.

getPrivateKey

public abstract java.security.PrivateKey getPrivateKey(java.lang.String passphrase)
                                                throws java.security.cert.CertificateException
Get the encrypted private key structure represented by this block. Not currently supported.

Returns:
The private key structure represented by the block.
Throws:
java.security.cert.CertificateException - Thrown if there was a problem extracting the private key from the block data.

getCertificate

public abstract java.security.cert.Certificate getCertificate()
Get the certificate represented by this block.

Returns:
The certificate represented by the block.

ReadBeginEnd

protected static java.lang.String ReadBeginEnd(java.io.BufferedReader inputReader)
                                        throws java.io.IOException
Returns the text between -----BEGIN XXXX----- and -----END XXXX----- read from the provided inputReader. INCLUDING the BEGIN and END marker lines. This can be called multiple times to load multiple blocks from and inputReader. See ReadBase64 from a version that does not include the markers.

Parameters:
inputReader - A Reader connected to the source of the input.
Throws:
java.io.IOException - Thrown if there was a problem reading from the input source.

ReadBeginEnd

protected static java.lang.String ReadBeginEnd(java.lang.String inputString)
                                        throws java.io.IOException
Returns the text between -----BEGIN XXXX----- and -----END XXXX----- read from the provided string. INCLUDING the BEGIN and END marker lines.

Parameters:
inputString - The string containing the block of text.
Returns:
A new String containing the markers and the text between them.
Throws:
java.io.IOException - Thrown if there was a problem reading from the input source.