org.kth.pdc.grid.utility
Class X509Util

java.lang.Object
  extended by org.kth.pdc.grid.utility.X509Util

public class X509Util
extends Object

Author:
mehrana TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates

Constructor Summary
X509Util()
           
 
Method Summary
static byte[] certChainToByte(X509Certificate[] x509Cert)
          Convert array of x509certificates into byte format of PEMs
static String certProxyResponse(String proxyCert)
          Returns a proxy certificate in HTTP MIME type format
static String certReqResponse(String certReq)
          Returns a certificate request in HTTP MIME type format
static boolean changeFileMode(String file, int mode)
          Change fime permission
static boolean checkDName(String dName, String commoName, String realm)
           
static byte[] concatenate(byte[] a, byte[] b)
           
static String digestFileName(String delegationid_in, String DN_in)
          Returns SHA1 hash digest of file name based on given delegationID and DER encoded DN in form of SHA1_HASH(DelegationID)+"-"+SHA1_HASH(DN)
static String errorResponse(String errorMsg)
          Returns an error message in case of fails
static boolean fileExist(String file)
           
static String findPrivateKeyInCache(String strDirCache, String delegationID, String userDN)
          Search for associated private key in cache
static String findProxyInCache(String strDirCache, String delegationID, String userDN)
          Search for a generated proxy in cache
static String getDefaultCertFile()
          Retrieves the location of the user cert file.
static String getDefaultCertLocation()
          Retrieves the location of the CA cert files.
static String getDefaultKeyFile()
          Retrieves the location of the user key file.
static String getDefaultProxyFile()
          Retrieves the location of the proxy file.
static String getDlgeePropertyFile()
          Retrieve the path to the delegatee property file
static String getDlgorPropertyFile()
          Retrieve the path to the delegator property file
static byte[] getFilesBytes(File file)
          Reading IO file in byte
static String getOSEnvironment(String envName)
          Retrieve the value of environment variable
static Properties getProp(String filename)
           
static byte[] getSecretKeyFromkeyStore(String enteryPass, String ksPath, String ksFilename, String ksAlias, String ksPass)
           
static String getSTSClientPropertyFile()
          Retrieve the path to the STS Client property file
static String getSTSPropertyFile()
          Retrieve the path to the STS property file
static X509Certificate loadCertificate(InputStream cert)
          Load x509 certificate
static X509Certificate[] loadCertificateChain(BufferedInputStream bisCerts)
          Load a chain of certificates from BIS
static X509Certificate[] loadCertificateChain(byte[] bCerts)
          Load chain of certificates from byte
static Vector loadCertificateVector(BufferedInputStream bisCerts)
          Load a chain of certificates from BIS, returning a vector
static org.bouncycastle.asn1.x509.X509Name makeGridCertDN(String DN)
          Create an X509 Certificate DN
static org.bouncycastle.asn1.x509.X509Name makeGridCertDN(String organization, String orgUnit1, String orgUnit2, String country, String email, String commonName)
          Create an X509 Certificate DN
static void makeGSIProxy(X509Certificate[] certchain, PrivateKey pk, String fileLocation)
           
static Vector mergeVectors(Vector a, Vector b)
           
static byte[] PEMtoDER(byte[] bytes, String headfoot)
           
static byte[] readPEM(InputStream is, String hdr, String ftr)
          Read a PEM encoded base64 stream and decode it
static void saveCertificateTokeyStore(X509Certificate[] certChain, PrivateKey pk, String ksPath, String ksFilename, String ksAlias, String ksPass)
           
static void saveCertProxyTofile(String certProxy, String fileLocation, String delegationID, String userDN, boolean append)
          save a proxy certificate in specific location
static void saveCertProxyTofile(X509Certificate certProxy, String fileLocation)
          save a proxy certificate in specific location
static void saveCertReqToFile(String certReq, String fileLocation)
          Save a certificate request in specific location
static void savePrivateKey(PrivateKey pk, String fileLocation, String delegationID, String userDN)
          save a private key in specific location
static void saveSecretKeyTokeyStore(SecretKey key, String enteryPass, String ksPath, String ksFilename, String ksAlias, String ksPass)
           
static boolean SecretKeyStoreExists(String enteryPass, String ksPath, String ksFilename, String ksAlias, String ksPass)
           
static byte[] toBytes(Object object)
           
static Object toObject(byte[] bytes)
           
static void writeFileBytes(String filename, byte[] toWrite)
          Writing bytes to file
static String writePEM(byte[] bytes, String hdr, String ftr)
          Generate a PEM encoded string of certificate from a header and a footer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

X509Util

public X509Util()
Method Detail

writePEM

public static String writePEM(byte[] bytes,
                              String hdr,
                              String ftr)
                       throws IOException
Generate a PEM encoded string of certificate from a header and a footer

Parameters:
bytes - input stream
hdr - Header delimeter of certificate
ftr - footer delimeter of certificate
Returns:
encoded byte in pem
Throws:
IOException

readPEM

public static byte[] readPEM(InputStream is,
                             String hdr,
                             String ftr)
                      throws IOException
Read a PEM encoded base64 stream and decode it

Parameters:
is - Base64 PEM encoded stream
hdr - Header delimeter
ftr - Footer delimeter
Returns:
decoded DER bytes
Throws:
IOException - if a read error occurs

makeGridCertDN

public static org.bouncycastle.asn1.x509.X509Name makeGridCertDN(String organization,
                                                                 String orgUnit1,
                                                                 String orgUnit2,
                                                                 String country,
                                                                 String email,
                                                                 String commonName)
Create an X509 Certificate DN

Parameters:
CommonName - X509 Common Name
orgUnit, - Organization Unit
Returns:
X509Name of generated DN

makeGridCertDN

public static org.bouncycastle.asn1.x509.X509Name makeGridCertDN(String DN)
Create an X509 Certificate DN

Parameters:
CommonName - X509 Common Name
DN, - String of user DN
Returns:
X509Name of DN

checkDName

public static boolean checkDName(String dName,
                                 String commoName,
                                 String realm)

saveCertReqToFile

public static void saveCertReqToFile(String certReq,
                                     String fileLocation)
                              throws IOException
Save a certificate request in specific location

Parameters:
certReq - given certificate request to save
fileLocation - location of certificare request
Throws:
IOException

saveCertProxyTofile

public static void saveCertProxyTofile(X509Certificate certProxy,
                                       String fileLocation)
save a proxy certificate in specific location

Parameters:
certProxy - Given proxy certificate to save
fileLocation - location of proxy certificate

saveCertProxyTofile

public static void saveCertProxyTofile(String certProxy,
                                       String fileLocation,
                                       String delegationID,
                                       String userDN,
                                       boolean append)
save a proxy certificate in specific location

Parameters:
certProxy - Given proxy certificate to save
fileLocation - location of proxy certificate
delegationID -
userDN -

savePrivateKey

public static void savePrivateKey(PrivateKey pk,
                                  String fileLocation,
                                  String delegationID,
                                  String userDN)
save a private key in specific location

Parameters:
pk - Given private key to save
fileLocation - location of private key
delegationID -
userDN -

makeGSIProxy

public static void makeGSIProxy(X509Certificate[] certchain,
                                PrivateKey pk,
                                String fileLocation)

findProxyInCache

public static String findProxyInCache(String strDirCache,
                                      String delegationID,
                                      String userDN)
Search for a generated proxy in cache

Parameters:
strDirCache - cache directory
delegationID - Delegation ID
userDN - UserDN
Returns:
File name of proxy

findPrivateKeyInCache

public static String findPrivateKeyInCache(String strDirCache,
                                           String delegationID,
                                           String userDN)
Search for associated private key in cache

Parameters:
strDirCache - cache directory
delegationID - Delegation ID
userDN - UserDN
Returns:
File name of private key

loadCertificate

public static X509Certificate loadCertificate(InputStream cert)
                                       throws IOException,
                                              GeneralSecurityException
Load x509 certificate

Parameters:
cert - certificate to load
Returns:
X509 Certificate
Throws:
IOException
GeneralSecurityException

loadCertificateChain

public static X509Certificate[] loadCertificateChain(byte[] bCerts)
                                              throws IOException,
                                                     GeneralSecurityException
Load chain of certificates from byte

Parameters:
bCerts -
Returns:
Array of loaded certificates
Throws:
IOException
GeneralSecurityException

loadCertificateChain

public static X509Certificate[] loadCertificateChain(BufferedInputStream bisCerts)
                                              throws IOException,
                                                     GeneralSecurityException
Load a chain of certificates from BIS

Parameters:
bisCerts -
Returns:
Array of loaded certificates
Throws:
IOException
GeneralSecurityException

loadCertificateVector

public static Vector loadCertificateVector(BufferedInputStream bisCerts)
                                    throws IOException,
                                           GeneralSecurityException
Load a chain of certificates from BIS, returning a vector

Parameters:
bisCerts -
Returns:
Array of loaded certificates
Throws:
IOException
GeneralSecurityException

changeFileMode

public static boolean changeFileMode(String file,
                                     int mode)
Change fime permission

Parameters:
file - File name
mode - New Permission
Returns:
True if file mode has changed

getDefaultCertFile

public static String getDefaultCertFile()
Retrieves the location of the user cert file. from X509_USER_CERT.

Returns:
String the location of the user cert file

getDefaultKeyFile

public static String getDefaultKeyFile()
Retrieves the location of the user key file. from X509_USER_KEY.

Returns:
String the location of the user key file

getDefaultCertLocation

public static String getDefaultCertLocation()
Retrieves the location of the CA cert files. from X509_CERT_DIR.

Returns:
String the locations of the CA certificates

getDefaultProxyFile

public static String getDefaultProxyFile()
Retrieves the location of the proxy file. from X509_USER_PROXY.

Returns:
String the location of the proxy file

digestFileName

public static String digestFileName(String delegationid_in,
                                    String DN_in)
Returns SHA1 hash digest of file name based on given delegationID and DER encoded DN in form of SHA1_HASH(DelegationID)+"-"+SHA1_HASH(DN)

Parameters:
delegationid_in - delegationID of proxy certificate
DN_in - DER encoded DN
Returns:

fileExist

public static boolean fileExist(String file)

certReqResponse

public static String certReqResponse(String certReq)
Returns a certificate request in HTTP MIME type format

Parameters:
certReq - certificate request to response
Returns:
http response format
Throws:
Exception

certProxyResponse

public static String certProxyResponse(String proxyCert)
Returns a proxy certificate in HTTP MIME type format

Parameters:
proxyCert - proxy certificate to response
Returns:
http response format
Throws:
Exception

errorResponse

public static String errorResponse(String errorMsg)
Returns an error message in case of fails

Parameters:
errorMsg - to send
Returns:
error
Throws:
Exception

getFilesBytes

public static byte[] getFilesBytes(File file)
                            throws IOException
Reading IO file in byte

Parameters:
file - File name
Returns:
File contents in byte
Throws:
IOException

writeFileBytes

public static void writeFileBytes(String filename,
                                  byte[] toWrite)
Writing bytes to file

Parameters:
file - File name
contents - in byte
Throws:
IOException

certChainToByte

public static byte[] certChainToByte(X509Certificate[] x509Cert)
Convert array of x509certificates into byte format of PEMs

Parameters:
x509Cert -
Returns:
x509Certificates in byte format

getDlgeePropertyFile

public static String getDlgeePropertyFile()
Retrieve the path to the delegatee property file

Returns:
Path to the porperty file

getDlgorPropertyFile

public static String getDlgorPropertyFile()
Retrieve the path to the delegator property file

Returns:
Path to the porperty file

getSTSPropertyFile

public static String getSTSPropertyFile()
Retrieve the path to the STS property file

Returns:
Path to the porperty file

getSTSClientPropertyFile

public static String getSTSClientPropertyFile()
Retrieve the path to the STS Client property file

Returns:
Path to the porperty file

getOSEnvironment

public static String getOSEnvironment(String envName)
Retrieve the value of environment variable

Parameters:
envName - Environment variable name
Returns:
Value of environment variable

PEMtoDER

public static byte[] PEMtoDER(byte[] bytes,
                              String headfoot)

concatenate

public static byte[] concatenate(byte[] a,
                                 byte[] b)

saveCertificateTokeyStore

public static void saveCertificateTokeyStore(X509Certificate[] certChain,
                                             PrivateKey pk,
                                             String ksPath,
                                             String ksFilename,
                                             String ksAlias,
                                             String ksPass)

saveSecretKeyTokeyStore

public static void saveSecretKeyTokeyStore(SecretKey key,
                                           String enteryPass,
                                           String ksPath,
                                           String ksFilename,
                                           String ksAlias,
                                           String ksPass)

SecretKeyStoreExists

public static boolean SecretKeyStoreExists(String enteryPass,
                                           String ksPath,
                                           String ksFilename,
                                           String ksAlias,
                                           String ksPass)
Parameters:
object -
Returns:

getSecretKeyFromkeyStore

public static byte[] getSecretKeyFromkeyStore(String enteryPass,
                                              String ksPath,
                                              String ksFilename,
                                              String ksAlias,
                                              String ksPass)

toBytes

public static byte[] toBytes(Object object)

toObject

public static Object toObject(byte[] bytes)

mergeVectors

public static Vector mergeVectors(Vector a,
                                  Vector b)

getProp

public static Properties getProp(String filename)


Copyright © 2001-2008 University of Southampton IT Innovation Centre. All Rights Reserved.