Author: jaz
Date: Mon Apr 23 11:05:36 2007 New Revision: 531546 URL: http://svn.apache.org/viewvc?view=rev&rev=531546 Log: changed loop to use Arrays method; added a few methods for reading/writing keystores (not fully tested yet) Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/KeyStoreUtil.java ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/SSLUtil.java Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/KeyStoreUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/KeyStoreUtil.java?view=diff&rev=531546&r1=531545&r2=531546 ============================================================================== --- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/KeyStoreUtil.java (original) +++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/KeyStoreUtil.java Mon Apr 23 11:05:36 2007 @@ -25,13 +25,16 @@ import java.io.*; import java.net.URL; import java.security.*; +import java.security.cert.*; import java.security.cert.Certificate; -import java.security.cert.CertificateEncodingException; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.util.Collection; +import java.util.Map; + +import javolution.util.FastMap; + +import javax.security.auth.x500.X500Principal; /** * KeyStoreUtil - Utilities for getting KeyManagers and TrustManagers @@ -41,6 +44,13 @@ public static final String module = KeyStoreUtil.class.getName(); + public static void storeComponentKeyStore(String componentName, String keyStoreName, KeyStore store) throws IOException, GenericConfigException, NoSuchAlgorithmException, CertificateException, KeyStoreException { + ComponentConfig.KeystoreInfo ks = ComponentConfig.getKeystoreInfo(componentName, keyStoreName); + File file = new File(ks.createResourceHandler().getFullLocation()); + FileOutputStream out = new FileOutputStream(file); + store.store(out, ks.getPassword().toCharArray()); + } + public static KeyStore getComponentKeyStore(String componentName, String keyStoreName) throws IOException, GeneralSecurityException, GenericConfigException { ComponentConfig.KeystoreInfo ks = ComponentConfig.getKeystoreInfo(componentName, keyStoreName); return getStore(ks.createResourceHandler().getURL(), ks.getType(), ks.getPassword()); @@ -73,6 +83,25 @@ return null; } + public static X509Certificate readCertificate(byte[] certChain) throws CertificateException { + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + ByteArrayInputStream bais = new ByteArrayInputStream(certChain); + return (X509Certificate) cf.generateCertificate(bais); + } + + public static Map getCertX500Map(X509Certificate cert) { + X500Principal x500 = cert.getSubjectX500Principal(); + Map x500Map = FastMap.newInstance(); + + String[] x500Opts = x500.getName().split("\\,"); + for (int x = 0; x < x500Opts.length; x++) { + String[] nv = x500Opts[x].split("\\="); + x500Map.put(nv[0], nv[1]); + } + + return x500Map; + } + public static void importPKCS8CertChain(KeyStore ks, String alias, byte[] keyBytes, String keyPass, byte[] certChain) throws InvalidKeySpecException, NoSuchAlgorithmException, CertificateException, KeyStoreException { // load the private key KeyFactory kf = KeyFactory.getInstance("RSA"); Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/SSLUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/SSLUtil.java?view=diff&rev=531546&r1=531545&r2=531546 ============================================================================== --- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/SSLUtil.java (original) +++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/SSLUtil.java Mon Apr 23 11:05:36 2007 @@ -89,11 +89,7 @@ } } - KeyManager[] arr = new KeyManager[keyMgrs.size()]; - for (int x = 0; x < arr.length; x++) { - arr[x] = (KeyManager) keyMgrs.get(x); - } - return arr; + return (KeyManager[]) keyMgrs.toArray(new KeyManager[keyMgrs.size()]); } public static KeyManager[] getKeyManagers() throws IOException, GeneralSecurityException, GenericConfigException { @@ -114,11 +110,7 @@ } } - TrustManager[] arr = new TrustManager[trustMgrs.size()]; - for (int x = 0; x < arr.length; x++) { - arr[x] = (TrustManager) trustMgrs.get(x); - } - return arr; + return (TrustManager[]) trustMgrs.toArray(new TrustManager[trustMgrs.size()]); } public static KeyManager[] getKeyManagers(KeyStore ks, String password, String alias) throws GeneralSecurityException { |
Free forum by Nabble | Edit this page |