edu.vt.middleware.crypt.util
Class CryptReader

java.lang.Object
  extended by edu.vt.middleware.crypt.util.CryptReader

public class CryptReader
extends Object

Helper class for performing I/O read operations on cryptographic data.

Version:
$Revision: 2744 $
Author:
Middleware Services

Field Summary
static String DEFAULT_CERTIFICATE_TYPE
          X.509 certificate type.
 
Constructor Summary
protected CryptReader()
          Protected constructor of utility class.
 
Method Summary
static Certificate readCertificate(byte[] certBytes)
          Reads a PEM or DER-encoded certificate of the default type from a byte array into a Certificate object.
static Certificate readCertificate(byte[] certBytes, String type)
          Reads a PEM or DER-encoded certificate of the default type from a byte array into a Certificate object.
static Certificate readCertificate(File certFile)
          Reads a PEM or DER-encoded certificate of the default type from a file into a Certificate object.
static Certificate readCertificate(File certFile, String type)
          Reads a PEM or DER-encoded certificate of the given type from a file into a Certificate object.
static Certificate readCertificate(InputStream certStream)
          Reads a PEM or DER-encoded certificate of the default type from an input stream into a Certificate object.
static Certificate readCertificate(InputStream certStream, String type)
          Reads a PEM or DER-encoded certificate of the default type from an input stream into a Certificate object.
static Certificate[] readCertificateChain(byte[] chainBytes)
          Reads a certificate chain of the default certificate type from a byte array containing data in any of the formats supported by readCertificateChain(InputStream, String).
static Certificate[] readCertificateChain(byte[] chainBytes, String type)
          Reads a certificate chain of the given type from a byte array containing data in any of the formats supported by readCertificateChain(InputStream, String).
static Certificate[] readCertificateChain(File chainFile)
          Reads a certificate chain of the default certificate type from a file containing data in any of the formats supported by readCertificateChain(InputStream, String).
static Certificate[] readCertificateChain(File chainFile, String type)
          Reads a certificate chain of the given type from a file containing data in any of the formats supported by readCertificateChain(InputStream, String).
static Certificate[] readCertificateChain(InputStream chainStream)
          Reads a certificate chain of the default certificate type from an input stream containing data in any of the formats supported by readCertificateChain(InputStream, String).
static Certificate[] readCertificateChain(InputStream chainStream, String type)
          Reads a certificate chain of the default certificate type from an input stream containing data in any of the following formats: Sequence of DER-encoded certificates Concatenation of PEM-encoded certificates PKCS#7 certificate chain
static X509CRL readCRL(byte[] crlBytes)
          Reads a PEM or DER-encoded X.509 CRL from a byte array into an X509CRL object.
static X509CRL readCRL(File crlFile)
          Reads a PEM or DER-encoded X.509 CRL from a file into an X509CRL object.
static X509CRL readCRL(InputStream crlStream)
          Reads a PEM or DER-encoded X.509 CRL from an input stream into an X509CRL object.
static PrivateKey readPrivateKey(byte[] keyBytes)
          Reads a DER-encoded private key in PKCS#8 or OpenSSL "traditional" format from a byte array into a PrivateKey object.
static PrivateKey readPrivateKey(byte[] keyBytes, char[] password)
          Reads an encrypted private key in PKCS#8 or OpenSSL "traditional" format from a byte array into a PrivateKey object.
static PrivateKey readPrivateKey(File keyFile)
          Reads a DER-encoded private key in PKCS#8 or OpenSSL "traditional" format from a file into a PrivateKey object.
static PrivateKey readPrivateKey(File keyFile, char[] password)
          Reads an encrypted private key in PKCS#8 or OpenSSL "traditional" format from a file into a PrivateKey object.
static PrivateKey readPrivateKey(InputStream keyStream)
          Reads a DER-encoded private key in PKCS#8 or OpenSSL "traditional" format from an input stream into a PrivateKey object.
static PrivateKey readPrivateKey(InputStream keyStream, char[] password)
          Reads an encrypted private key in PKCS#8 or OpenSSL "traditional" format from an input stream into a PrivateKey object.
static PublicKey readPublicKey(byte[] keyBytes)
          Reads a PEM or DER-encoded X.509 public key from a byte array into a PublicKey object.
static PublicKey readPublicKey(File keyFile)
          Reads a DER-encoded X.509 public key from an input stream into a PublicKey object.
static PublicKey readPublicKey(InputStream keyStream)
          Reads a PEM or DER-encoded X.509 public key from an input stream into a PublicKey object.
static SecretKey readSecretKey(byte[] keyBytes, String algorithm)
          Reads the raw bytes of a symmetric encryption key from a byte array.
static SecretKey readSecretKey(File keyFile, String algorithm)
          Reads the raw bytes of a symmetric encryption key from a file.
static SecretKey readSecretKey(InputStream keyStream, String algorithm)
          Reads the raw bytes of a symmetric encryption key from an input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CERTIFICATE_TYPE

public static final String DEFAULT_CERTIFICATE_TYPE
X.509 certificate type.

See Also:
Constant Field Values
Constructor Detail

CryptReader

protected CryptReader()
Protected constructor of utility class.

Method Detail

readSecretKey

public static SecretKey readSecretKey(File keyFile,
                                      String algorithm)
                               throws CryptException,
                                      IOException
Reads the raw bytes of a symmetric encryption key from a file.

Parameters:
keyFile - File containing key data.
algorithm - Symmetric cipher algorithm for which key is used.
Returns:
Secret key.
Throws:
CryptException - On cryptography errors such as invalid formats, unsupported ciphers, illegal settings.
IOException - On IO errors.

readSecretKey

public static SecretKey readSecretKey(InputStream keyStream,
                                      String algorithm)
                               throws CryptException,
                                      IOException
Reads the raw bytes of a symmetric encryption key from an input stream.

Parameters:
keyStream - Stream containing key data.
algorithm - Symmetric cipher algorithm for which key is used.
Returns:
Secret key.
Throws:
CryptException - On cryptography errors such as invalid formats, unsupported ciphers, illegal settings.
IOException - On IO errors.

readSecretKey

public static SecretKey readSecretKey(byte[] keyBytes,
                                      String algorithm)
                               throws CryptException,
                                      IOException
Reads the raw bytes of a symmetric encryption key from a byte array.

Parameters:
keyBytes - Byte array containing key data.
algorithm - Symmetric cipher algorithm for which key is used.
Returns:
Secret key.
Throws:
CryptException - On cryptography errors such as invalid formats, unsupported ciphers, illegal settings.
IOException - On IO errors.

readPrivateKey

public static PrivateKey readPrivateKey(File keyFile)
                                 throws CryptException,
                                        IOException
Reads a DER-encoded private key in PKCS#8 or OpenSSL "traditional" format from a file into a PrivateKey object.

Parameters:
keyFile - Private key file.
Returns:
Private key containing data read from file.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPrivateKey

public static PrivateKey readPrivateKey(InputStream keyStream)
                                 throws CryptException,
                                        IOException
Reads a DER-encoded private key in PKCS#8 or OpenSSL "traditional" format from an input stream into a PrivateKey object.

Parameters:
keyStream - Input stream containing private key data.
Returns:
Private key containing data read from stream.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPrivateKey

public static PrivateKey readPrivateKey(byte[] keyBytes)
                                 throws CryptException,
                                        IOException
Reads a DER-encoded private key in PKCS#8 or OpenSSL "traditional" format from a byte array into a PrivateKey object.

Parameters:
keyBytes - Byte array containing private key data.
Returns:
Private key of type in byte array.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPrivateKey

public static PrivateKey readPrivateKey(File keyFile,
                                        char[] password)
                                 throws CryptException,
                                        IOException
Reads an encrypted private key in PKCS#8 or OpenSSL "traditional" format from a file into a PrivateKey object. Both DER and PEM encoded keys are supported.

Parameters:
keyFile - Private key file.
password - Password to decrypt private key.
Returns:
Private key containing data read from file.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPrivateKey

public static PrivateKey readPrivateKey(InputStream keyStream,
                                        char[] password)
                                 throws CryptException,
                                        IOException
Reads an encrypted private key in PKCS#8 or OpenSSL "traditional" format from an input stream into a PrivateKey object. Both DER and PEM encoded keys are supported.

Parameters:
keyStream - Input stream containing private key data.
password - Password to decrypt private key; MUST NOT be null.
Returns:
Private key containing data read from file.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPrivateKey

public static PrivateKey readPrivateKey(byte[] keyBytes,
                                        char[] password)
                                 throws CryptException,
                                        IOException
Reads an encrypted private key in PKCS#8 or OpenSSL "traditional" format from a byte array into a PrivateKey object. Both DER and PEM encoded keys are supported.

Parameters:
keyBytes - Byte array containing private key data.
password - Password to decrypt private key; MUST NOT be null.
Returns:
Private key of type read from byte array.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPublicKey

public static PublicKey readPublicKey(File keyFile)
                               throws CryptException,
                                      IOException
Reads a DER-encoded X.509 public key from an input stream into a PublicKey object.

Parameters:
keyFile - File containing DER-encoded X.509 public key.
Returns:
Public key containing data read from file.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPublicKey

public static PublicKey readPublicKey(InputStream keyStream)
                               throws CryptException,
                                      IOException
Reads a PEM or DER-encoded X.509 public key from an input stream into a PublicKey object.

Parameters:
keyStream - Input stream containing DER-encoded X.509 public key.
Returns:
Public key containing data read from stream.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readPublicKey

public static PublicKey readPublicKey(byte[] keyBytes)
                               throws CryptException,
                                      IOException
Reads a PEM or DER-encoded X.509 public key from a byte array into a PublicKey object.

Parameters:
keyBytes - Byte array containing DER-encoded X.509 public key.
Returns:
Public key containing data in byte array.
Throws:
CryptException - On key format errors.
IOException - On key read errors.

readCertificate

public static Certificate readCertificate(File certFile)
                                   throws CryptException,
                                          IOException
Reads a PEM or DER-encoded certificate of the default type from a file into a Certificate object.

Parameters:
certFile - Path to certificate file.
Returns:
Certificate containing data read from file.
Throws:
CryptException - On certificate format errors.
IOException - On read errors.

readCertificate

public static Certificate readCertificate(File certFile,
                                          String type)
                                   throws CryptException,
                                          IOException
Reads a PEM or DER-encoded certificate of the given type from a file into a Certificate object.

Parameters:
certFile - Path to certificate file.
type - Type of certificate to read, e.g. X.509.
Returns:
Certificate containing data read from file.
Throws:
CryptException - On certificate format errors.
IOException - On read errors.

readCertificate

public static Certificate readCertificate(InputStream certStream)
                                   throws CryptException,
                                          IOException
Reads a PEM or DER-encoded certificate of the default type from an input stream into a Certificate object.

Parameters:
certStream - Input stream with certificate data.
Returns:
Certificate created from data read from stream.
Throws:
CryptException - On certificate read or format errors.
IOException - On read errors.

readCertificate

public static Certificate readCertificate(InputStream certStream,
                                          String type)
                                   throws CryptException,
                                          IOException
Reads a PEM or DER-encoded certificate of the default type from an input stream into a Certificate object.

Parameters:
certStream - Input stream with certificate data.
type - Type of certificate to read, e.g. X.509.
Returns:
Certificate created from data read from stream.
Throws:
IOException - On read errors.
CryptException - On certificate read or format errors.

readCertificate

public static Certificate readCertificate(byte[] certBytes)
                                   throws CryptException,
                                          IOException
Reads a PEM or DER-encoded certificate of the default type from a byte array into a Certificate object.

Parameters:
certBytes - Byte array containing certificate data.
Returns:
Certificate created from data in byte array.
Throws:
CryptException - On certificate read or format errors.
IOException - On read errors.

readCertificate

public static Certificate readCertificate(byte[] certBytes,
                                          String type)
                                   throws CryptException,
                                          IOException
Reads a PEM or DER-encoded certificate of the default type from a byte array into a Certificate object.

Parameters:
certBytes - Byte array containing certificate data.
type - Type of certificate to read, e.g. X.509.
Returns:
Certificate created from data in byte array.
Throws:
IOException - On read errors.
CryptException - On certificate read or format errors.

readCertificateChain

public static Certificate[] readCertificateChain(File chainFile)
                                          throws CryptException,
                                                 IOException
Reads a certificate chain of the default certificate type from a file containing data in any of the formats supported by readCertificateChain(InputStream, String).

Parameters:
chainFile - Path to certificate chain file.
Returns:
Array of certificates in the order in which they appear in the given file.
Throws:
CryptException - On certificate format errors.
IOException - On read errors.

readCertificateChain

public static Certificate[] readCertificateChain(File chainFile,
                                                 String type)
                                          throws CryptException,
                                                 IOException
Reads a certificate chain of the given type from a file containing data in any of the formats supported by readCertificateChain(InputStream, String).

Parameters:
chainFile - Path to certificate chain file.
type - Type of certificate to read, e.g. X.509.
Returns:
Array of certificates in the order in which they appear in the given file.
Throws:
CryptException - On certificate format errors.
IOException - On read errors.

readCertificateChain

public static Certificate[] readCertificateChain(InputStream chainStream)
                                          throws CryptException,
                                                 IOException
Reads a certificate chain of the default certificate type from an input stream containing data in any of the formats supported by readCertificateChain(InputStream, String).

Parameters:
chainStream - Stream containing certificate chain data.
Returns:
Array of certificates in the order in which they appear in the stream.
Throws:
CryptException - On certificate read or format errors.
IOException - On read errors.

readCertificateChain

public static Certificate[] readCertificateChain(InputStream chainStream,
                                                 String type)
                                          throws CryptException,
                                                 IOException
Reads a certificate chain of the default certificate type from an input stream containing data in any of the following formats:

Parameters:
chainStream - Stream containing certificate chain data.
type - Type of certificate to read, e.g. X.509.
Returns:
Array of certificates in the order in which they appear in the stream.
Throws:
CryptException - On certificate read or format errors.
IOException - On read errors.

readCertificateChain

public static Certificate[] readCertificateChain(byte[] chainBytes)
                                          throws CryptException,
                                                 IOException
Reads a certificate chain of the default certificate type from a byte array containing data in any of the formats supported by readCertificateChain(InputStream, String).

Parameters:
chainBytes - Byte array containing certificate chain data.
Returns:
Array of certificates in the order in which they appear in the given byte array.
Throws:
CryptException - On certificate format errors.
IOException - On read errors.

readCertificateChain

public static Certificate[] readCertificateChain(byte[] chainBytes,
                                                 String type)
                                          throws CryptException,
                                                 IOException
Reads a certificate chain of the given type from a byte array containing data in any of the formats supported by readCertificateChain(InputStream, String).

Parameters:
chainBytes - Byte array containing certificate chain data.
type - Type of certificate to read, e.g. X.509.
Returns:
Array of certificates in the order in which they appear in the given file.
Throws:
CryptException - On certificate format errors.
IOException - On read errors.

readCRL

public static X509CRL readCRL(InputStream crlStream)
                       throws CryptException,
                              IOException
Reads a PEM or DER-encoded X.509 CRL from an input stream into an X509CRL object.

Parameters:
crlStream - Input stream with CRL data.
Returns:
CRL created from data read from stream.
Throws:
IOException - On read errors.
CryptException - On CRL read or format errors.

readCRL

public static X509CRL readCRL(File crlFile)
                       throws CryptException,
                              IOException
Reads a PEM or DER-encoded X.509 CRL from a file into an X509CRL object.

Parameters:
crlFile - Path to CRL file.
Returns:
CRL created from data read from file.
Throws:
IOException - On read errors.
CryptException - On CRL read or format errors.

readCRL

public static X509CRL readCRL(byte[] crlBytes)
                       throws CryptException,
                              IOException
Reads a PEM or DER-encoded X.509 CRL from a byte array into an X509CRL object.

Parameters:
crlBytes - Byte array containing CRL data.
Returns:
CRL created from data read from byte array.
Throws:
IOException - On read errors.
CryptException - On CRL read or format errors.


Copyright © 2003-2013 Virginia Tech. All Rights Reserved.