org.glite.slcs.pki.bouncycastle
Class Codec

java.lang.Object
  extended by org.glite.slcs.pki.bouncycastle.Codec

public class Codec
extends java.lang.Object

Codec utility to read and write PEM object using the BouncyCastle functions.

Version:
$Revision: 1.4 $
Author:
Valery Tschopp

Method Summary
static java.lang.String getPEMEncoded(java.security.Key key)
          PEM encode a Key.
static java.lang.String getPEMEncoded(java.security.Key key, char[] password)
          PEM encode the encrypted Key.
static java.lang.String getPEMEncoded(java.security.cert.X509Certificate cert)
          Returns the PEM encoded String of the X509 certificate.
static java.lang.String getPrincipalValue(java.security.cert.X509Certificate certificate, org.bouncycastle.asn1.DERObjectIdentifier oid)
          Gets the first value of the X509Principal corresponding to the given oid.
static java.security.cert.X509Certificate[] readPEMEncodedCertificates(java.io.Reader reader)
          Return an array of all X509Certificates stored in a PEM encoded source.
static void storePEMEncoded(java.security.Key key, char[] password, java.io.File file)
          Store the encrypted Key PEM encoded in a File.
static void storePEMEncoded(java.security.Key key, java.io.File file)
          Store the Key PEM encoded in a File.
static void storePEMEncoded(java.security.cert.X509Certificate cert, java.io.File file)
          Stores a X509 certificate in PEM format.
static void storePEMEncoded(java.security.cert.X509Certificate cert, java.security.cert.X509Certificate[] chain, java.io.File file)
          Stores a X509 certificate and its chain of certificate PEM encoded.
static void storePKCS12(java.security.PrivateKey privateKey, java.security.cert.X509Certificate certificate, java.security.cert.X509Certificate[] chain, java.io.File file, char[] password)
          Stores the private key and certificate in a PKCS12 file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPEMEncoded

public static java.lang.String getPEMEncoded(java.security.Key key)
PEM encode a Key. OpenSSL compatible.

Parameters:
key - The Key to PEM encode
Returns:
The PEM encode String representation of the key

getPEMEncoded

public static java.lang.String getPEMEncoded(java.security.Key key,
                                             char[] password)
PEM encode the encrypted Key. OpenSSL compatible.

Parameters:
key - The Key to encoded
password - The Key encryption password
Returns:
The PEM encode String representation of the key

storePEMEncoded

public static void storePEMEncoded(java.security.Key key,
                                   java.io.File file)
                            throws java.io.IOException
Store the Key PEM encoded in a File. OpenSSL compatible.

Parameters:
key - The Key to store PEM encoded.
file - The File to store into.
Throws:
java.io.IOException - If an error occurs.

storePEMEncoded

public static void storePEMEncoded(java.security.Key key,
                                   char[] password,
                                   java.io.File file)
                            throws java.io.IOException
Store the encrypted Key PEM encoded in a File. OpenSSL compatible.

Parameters:
key - The Key to store PEM encoded.
password - The Key encryption password.
file - The File to store into.
Throws:
java.io.IOException - If an error occurs.

storePEMEncoded

public static void storePEMEncoded(java.security.cert.X509Certificate cert,
                                   java.io.File file)
                            throws java.io.IOException
Stores a X509 certificate in PEM format. OpenSSL compatible.

Parameters:
cert - The X509 certificate to store PEM encoded.
file - The File to store into.
Throws:
java.io.IOException - If an error occurs while storing.

storePEMEncoded

public static void storePEMEncoded(java.security.cert.X509Certificate cert,
                                   java.security.cert.X509Certificate[] chain,
                                   java.io.File file)
                            throws java.io.IOException
Stores a X509 certificate and its chain of certificate PEM encoded. OpenSSL compatible.

Parameters:
cert - The X509 certificate to store PEM encoded.
chain - The X509 certificates chain array
file - The File to store into.
Throws:
java.io.IOException - If an IO error occurs while saving.

getPEMEncoded

public static java.lang.String getPEMEncoded(java.security.cert.X509Certificate cert)
Returns the PEM encoded String of the X509 certificate. OpenSSL compatible.

Parameters:
cert - The X509 certificate.
Returns:
The PEM encoded String.

readPEMEncodedCertificates

public static java.security.cert.X509Certificate[] readPEMEncodedCertificates(java.io.Reader reader)
                                                                       throws java.io.IOException
Return an array of all X509Certificates stored in a PEM encoded source. The certificate order of the source is respected.

Parameters:
reader - The Reader used to read the source.
Returns:
The array of all X509 certificates found in the PEM source.
Throws:
java.io.IOException - If an error occurs while reading the source.

storePKCS12

public static void storePKCS12(java.security.PrivateKey privateKey,
                               java.security.cert.X509Certificate certificate,
                               java.security.cert.X509Certificate[] chain,
                               java.io.File file,
                               char[] password)
                        throws java.security.GeneralSecurityException,
                               java.io.IOException
Stores the private key and certificate in a PKCS12 file. The certificate Subject CN is used as key alias in the PKCS12 store.

Parameters:
privateKey - The private key.
certificate - The X509 certificate.
chain - The X509 certificate chain.
file - The file object.
password - The password for the PKCS12 file.
Throws:
java.security.GeneralSecurityException - If a crypto error occurs.
java.io.IOException - If an IO error occurs.

getPrincipalValue

public static java.lang.String getPrincipalValue(java.security.cert.X509Certificate certificate,
                                                 org.bouncycastle.asn1.DERObjectIdentifier oid)
                                          throws java.security.GeneralSecurityException
Gets the first value of the X509Principal corresponding to the given oid.

Parameters:
certificate - The X509 certificate, containing the X509Principal.
oid - The OID of the desired value.
Returns:
The value or null if the principal doesn't contain the oid.
Throws:
java.security.GeneralSecurityException - If a crypto error occurs.