com.ca.commons.cbutil
Class CBSecurity

java.lang.Object
  |
  +--com.ca.commons.cbutil.CBSecurity

public class CBSecurity
extends java.lang.Object

This is a grab bag of useful static functions related to security - mainly doing conversions between PEM and DER.

(nb: PEM = 'Privacy Enhanced Mail' format,
while DER = 'Destinguished Encoding Rules' ASN1 data.
- PEM is usually base64 encoded DER data, with some minor frills.)


Field Summary
static byte[] PEM_BEGIN
          Standard header for the base 64 encoded info block of a pem file.
static byte[] PEM_CERT_FOOTER
          Standard footer for a pem encoded certificate block
static byte[] PEM_CERT_HEADER
          Standard header for a pem encoded certificate block
static byte[] PEM_ENC_KEY_FOOTER
          Standard header for a pem encoded encrypted private key block
static byte[] PEM_ENC_KEY_HEADER
          Standard header for a pem encoded encrypted private key block
static byte[] PEM_END
          Standard footer for the base 64 encoded info block of a pem file.
static byte[] PEM_RSA_KEY_FOOTER
          Standard header for a pem encoded RSA private key block
static byte[] PEM_RSA_KEY_HEADER
          Standard header for a pem encoded RSA private key block
 
Constructor Summary
CBSecurity()
           
 
Method Summary
static byte[] convertFromPEM(byte[] pem)
          This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data, such as might be read as raw bytes from a text file, and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).
static byte[] convertFromPEM(byte[] pem, byte[] header)
          This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data, such as might be read as raw bytes from a text file, and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).
static byte[] convertFromPEM(byte[] pem, byte[] header, byte[] footer)
          This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data, such as might be read as raw bytes from a text file, and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).
static byte[] convertFromPEMCertificate(byte[] pem)
          This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data representing an X509 certificate and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).
protected static byte[] convertToPEM(byte[] der, byte[] header, byte[] footer)
           
static byte[] convertToPEMCertificate(byte[] der)
          This takes an array of raw data representing a DER encoded X509 certificate, and base64 encodes it, adding PEM style -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- tags.
static byte[] convertToPEMEncryptedPrivateKey(byte[] der)
          This takes an array of raw data representing an Encrypted DER encoded private key (probably pkcs 8), and base64 encodes it, adding PEM style -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- tags.
static byte[] convertToPEMRSAPrivateKey(byte[] der)
          This takes an array of raw data representing a DER encoded RSA private key, and base64 encodes it, adding PEM style -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- tags.
static int indexOf(byte[] mainArray, byte searchByte)
          Returns the position that a searchByte first appears in a byte array.
static int indexOf(byte[] mainArray, byte[] searchSequence)
          Tries to match a byte sequence within a larger byte array.
static int indexOf(byte[] mainArray, byte[] searchSequence, int fromIndex)
          Tries to match a byte sequence within a larger byte array.
static int indexOf(byte[] mainArray, byte searchByte, int fromIndex)
          Returns the first position, greater than a given index, that a searchByte first appears at within an array.
static boolean isPEM(byte[] test)
          A simple check to see if a file is a PEM file, by looking for PEM '------BEGIN...' and PEM '-----END' tags.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PEM_BEGIN

public static final byte[] PEM_BEGIN
Standard header for the base 64 encoded info block of a pem file.


PEM_END

public static final byte[] PEM_END
Standard footer for the base 64 encoded info block of a pem file.


PEM_CERT_HEADER

public static final byte[] PEM_CERT_HEADER
Standard header for a pem encoded certificate block


PEM_CERT_FOOTER

public static final byte[] PEM_CERT_FOOTER
Standard footer for a pem encoded certificate block


PEM_ENC_KEY_HEADER

public static final byte[] PEM_ENC_KEY_HEADER
Standard header for a pem encoded encrypted private key block


PEM_ENC_KEY_FOOTER

public static final byte[] PEM_ENC_KEY_FOOTER
Standard header for a pem encoded encrypted private key block


PEM_RSA_KEY_HEADER

public static final byte[] PEM_RSA_KEY_HEADER
Standard header for a pem encoded RSA private key block


PEM_RSA_KEY_FOOTER

public static final byte[] PEM_RSA_KEY_FOOTER
Standard header for a pem encoded RSA private key block

Constructor Detail

CBSecurity

public CBSecurity()
Method Detail

indexOf

public static int indexOf(byte[] mainArray,
                          byte searchByte)
Returns the position that a searchByte first appears in a byte array.

Parameters:
mainArray - the byte array to search within
searchByte - the byte to look for

indexOf

public static int indexOf(byte[] mainArray,
                          byte searchByte,
                          int fromIndex)
Returns the first position, greater than a given index, that a searchByte first appears at within an array.

Parameters:
mainArray - the byte array to search within
searchByte - the byte to look for

indexOf

public static int indexOf(byte[] mainArray,
                          byte[] searchSequence)

Tries to match a byte sequence within a larger byte array.

Students of Sun's java.lang.String class may recognise some of this code :-).

Parameters:
mainArray - the base array to search within.
searchSequence - the short sequence to find the position of within the main array.
Returns:
the index of the searchSequence within the main Array, or -1 if not found.

indexOf

public static int indexOf(byte[] mainArray,
                          byte[] searchSequence,
                          int fromIndex)

Tries to match a byte sequence within a larger byte array.

Students of Sun's java.lang.String class may recognise some of this code :-).

Parameters:
mainArray - the base array to search within.
searchSequence - the short sequence to find the position of within the main array.
fromIndex - the position to start searching from.
Returns:
the index of the searchSequence within the main Array, or -1 if not found.

isPEM

public static boolean isPEM(byte[] test)
A simple check to see if a file is a PEM file, by looking for PEM '------BEGIN...' and PEM '-----END' tags. Note that this is Not Conclusive!


convertFromPEM

public static byte[] convertFromPEM(byte[] pem)
This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data, such as might be read as raw bytes from a text file, and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).

Parameters:
pem - the pem data to convert
Returns:
the converted raw data

convertFromPEM

public static byte[] convertFromPEM(byte[] pem,
                                    byte[] header)

This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data, such as might be read as raw bytes from a text file, and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).

In addition, this method allows the start of the PEM header tag to be explicitly specified. This is useful when a single file contains multiple data blocks (e.g. a cert *and* a private key). Only the beginning of the stard header needs to be specified; e.g. '-----BEGIN RSA PRIVATE' is sufficient, the full header is not required. (The footer is assumed to be the first block starting with '-----END...')

Parameters:
pem - the pem data to convert
Returns:
the converted raw data

convertFromPEMCertificate

public static byte[] convertFromPEMCertificate(byte[] pem)

This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data representing an X509 certificate and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).

Parameters:
pem - the pem data containing a certificate to convert
Returns:
the converted raw data

convertFromPEM

public static byte[] convertFromPEM(byte[] pem,
                                    byte[] header,
                                    byte[] footer)

This takes a byte array of PEM (originally rfc 1421-1424, but has drifted a bit) encoded data, such as might be read as raw bytes from a text file, and converts it to 'raw' DER binary data (i.e. a byte array with values from 0x0 to 0xFF).

In addition, this method allows the start of the PEM header and footer tag to be explicitly specified. This is useful when a single file contains multiple data blocks (e.g. a cert *and* a private key). Only the beginning of the headers needs to be specified; e.g. '-----BEGIN RSA PRIVATE' is sufficient, the full header/footer is not required.

Parameters:
pem - the pem data to convert
Returns:
the converted raw data

convertToPEMCertificate

public static byte[] convertToPEMCertificate(byte[] der)
This takes an array of raw data representing a DER encoded X509 certificate, and base64 encodes it, adding PEM style -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- tags.

Parameters:
der - the DER encoded data

convertToPEMRSAPrivateKey

public static byte[] convertToPEMRSAPrivateKey(byte[] der)
This takes an array of raw data representing a DER encoded RSA private key, and base64 encodes it, adding PEM style -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- tags.

Parameters:
der - the DER encoded data

convertToPEMEncryptedPrivateKey

public static byte[] convertToPEMEncryptedPrivateKey(byte[] der)
This takes an array of raw data representing an Encrypted DER encoded private key (probably pkcs 8), and base64 encodes it, adding PEM style -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- tags.

Parameters:
der - the DER encoded data

convertToPEM

protected static byte[] convertToPEM(byte[] der,
                                     byte[] header,
                                     byte[] footer)