Class SecurityUtil


  • public class SecurityUtil
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      SecurityUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean checkTrustedCertsFile()
      Checks the user's home directory to see if the trusted.certs file exists.
      static java.security.KeyStore getCacertsKeyStore()  
      static java.lang.String getCN​(java.lang.String principal)
      Extracts the CN field from a Certificate principal string.
      static java.security.Key getKey​(java.security.KeyStore ks, java.lang.String alias)  
      static java.security.KeyStore getSystemCertStore()  
      static java.lang.String getTrustedCertsFilename()  
      static java.security.KeyStore getUserKeyStore()  
      static void initKeyManagerFactory​(javax.net.ssl.KeyManagerFactory kmf, java.security.KeyStore ks)  
      static void loadKeyStore​(java.security.KeyStore ks, java.io.File f)  
      static void setKeyEntry​(java.security.KeyStore ks, java.lang.String alias, java.security.Key key, java.security.cert.Certificate[] certChain)  
      static void storeKeyStore​(java.security.KeyStore ks, java.io.File f)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SecurityUtil

        public SecurityUtil()
    • Method Detail

      • getTrustedCertsFilename

        public static java.lang.String getTrustedCertsFilename()
                                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getCN

        public static java.lang.String getCN​(java.lang.String principal)
        Extracts the CN field from a Certificate principal string. Or, if it can't find that, return the principal unmodified. This is a simple (and hence 'wrong') version. See http://www.ietf.org/rfc/rfc2253.txt for all the gory details.
        Parameters:
        principal - string, CN to be extracted from
        Returns:
        extracted CN
      • checkTrustedCertsFile

        public static boolean checkTrustedCertsFile()
                                             throws java.lang.Exception
        Checks the user's home directory to see if the trusted.certs file exists. If it does not exist, it tries to create an empty keystore.
        Returns:
        true if the trusted.certs file exists or a new trusted.certs was created successfully, otherwise false.
        Throws:
        java.lang.Exception - if check goes wrong
      • getUserKeyStore

        public static java.security.KeyStore getUserKeyStore()
                                                      throws java.lang.Exception
        Returns:
        the keystore associated with the user's trusted.certs file, or null otherwise.
        Throws:
        java.lang.Exception - if getting fails
      • getCacertsKeyStore

        public static java.security.KeyStore getCacertsKeyStore()
                                                         throws java.lang.Exception
        Returns:
        the keystore associated with the JDK cacerts file, or null otherwise.
        Throws:
        java.lang.Exception - if get fails
      • getSystemCertStore

        public static java.security.KeyStore getSystemCertStore()
                                                         throws java.lang.Exception
        Returns:
        the keystore associated with the system certs file, or null otherwise.
        Throws:
        java.lang.Exception - if get goes wrong
      • initKeyManagerFactory

        public static void initKeyManagerFactory​(javax.net.ssl.KeyManagerFactory kmf,
                                                 java.security.KeyStore ks)
                                          throws java.security.KeyStoreException,
                                                 java.security.NoSuchAlgorithmException,
                                                 java.security.UnrecoverableKeyException
        Throws:
        java.security.KeyStoreException
        java.security.NoSuchAlgorithmException
        java.security.UnrecoverableKeyException
      • setKeyEntry

        public static void setKeyEntry​(java.security.KeyStore ks,
                                       java.lang.String alias,
                                       java.security.Key key,
                                       java.security.cert.Certificate[] certChain)
                                throws java.security.KeyStoreException
        Throws:
        java.security.KeyStoreException
      • getKey

        public static java.security.Key getKey​(java.security.KeyStore ks,
                                               java.lang.String alias)
                                        throws java.security.KeyStoreException,
                                               java.security.NoSuchAlgorithmException,
                                               java.security.UnrecoverableKeyException
        Throws:
        java.security.KeyStoreException
        java.security.NoSuchAlgorithmException
        java.security.UnrecoverableKeyException
      • loadKeyStore

        public static void loadKeyStore​(java.security.KeyStore ks,
                                        java.io.File f)
                                 throws java.io.IOException,
                                        java.security.NoSuchAlgorithmException,
                                        java.security.cert.CertificateException
        Throws:
        java.io.IOException
        java.security.NoSuchAlgorithmException
        java.security.cert.CertificateException
      • storeKeyStore

        public static void storeKeyStore​(java.security.KeyStore ks,
                                         java.io.File f)
                                  throws java.security.KeyStoreException,
                                         java.io.IOException,
                                         java.security.NoSuchAlgorithmException,
                                         java.security.cert.CertificateException
        Throws:
        java.security.KeyStoreException
        java.io.IOException
        java.security.NoSuchAlgorithmException
        java.security.cert.CertificateException