View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.xml.registry.ebxml.util; 11 12 import java.io.BufferedInputStream; 13 import java.io.File; 14 import java.io.FileInputStream; 15 import java.io.IOException; 16 import java.io.InputStream; 17 import java.security.GeneralSecurityException; 18 import java.security.KeyStore; 19 import java.security.KeyStoreException; 20 import java.security.PrivateKey; 21 import java.security.cert.Certificate; 22 import java.security.cert.X509Certificate; 23 import java.util.Properties; 24 25 import javax.security.auth.x500.X500PrivateCredential; 26 import javax.xml.registry.JAXRException; 27 28 import com.sun.xml.registry.ebxml.util.KeystoreUtil; 29 30 /* 31 * Security related utility methods 32 * 33 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/ebxml/util/SecurityUtil.java,v 1.2 2003/07/18 02:16:16 farrukh_najmi Exp $ 34 * 35 */ 36 public class SecurityUtil { 37 38 private static final SecurityUtil INSTANCE = new SecurityUtil(); 39 40 private Properties aliasTable; 41 private KeyStore keyStore; 42 43 public static SecurityUtil getInstance() { 44 return INSTANCE; 45 } 46 47 private Properties loadAliasTable() throws JAXRException { 48 49 String jaxrHome = ProviderProperties.getInstance().getProperty("jaxr-ebxml.home"); 50 if ((jaxrHome == null) || (jaxrHome.length() == 0)) { 51 throw new JAXRException("Property jaxr-ebxml.home is undefined"); 52 } 53 54 Properties aliasTable = new Properties(); 55 File aliasFile = new File(jaxrHome, "security/alias.properties"); 56 if (aliasFile.exists()) { 57 try { 58 aliasTable.load( 59 new BufferedInputStream(new FileInputStream(aliasFile))); 60 } catch (IOException x) { 61 throw new JAXRException("Caught unexpected IOException", x); 62 } 63 } 64 return aliasTable; 65 } 66 67 68 private KeyStore loadKeyStore() throws JAXRException { 69 70 String storepass = 71 ProviderProperties.getInstance().getProperty("jaxr-ebxml.security.storepass"); 72 try { 73 keyStore = KeyStore.getInstance( 74 ProviderProperties.getInstance().getProperty("jaxr-ebxml.security.storetype")); 75 } catch (KeyStoreException x) { 76 throw new JAXRException(x); 77 } 78 File keyStoreFile = KeystoreUtil.getKeystoreFile(); 79 if (!keyStoreFile.exists()) { 80 throw new JAXRException("KeyStore file not found \"" 81 + keyStoreFile.toString() 82 + "\". Please create one."); 83 } 84 try { 85 InputStream keyIS = new BufferedInputStream( 86 new FileInputStream(keyStoreFile)); 87 keyStore.load(keyIS, storepass.toCharArray()); 88 } catch (IOException x) { 89 throw new JAXRException(x); 90 } catch (GeneralSecurityException x) { 91 throw new JAXRException(x); 92 } 93 return keyStore; 94 } 95 96 97 public String aliasToObjectId(String alias) throws JAXRException { 98 if (aliasTable == null) { 99 aliasTable = loadAliasTable(); 100 } 101 return aliasTable.getProperty(alias); 102 } 103 104 public KeyStore getKeyStore() throws JAXRException { 105 if (keyStore == null) { 106 keyStore = loadKeyStore(); 107 } 108 return keyStore; 109 } 110 111 public X500PrivateCredential aliasToX500PrivateCredential(String alias) 112 throws JAXRException { 113 114 getKeyStore(); 115 try { 116 X509Certificate cert = 117 (X509Certificate)keyStore.getCertificate(alias); 118 if (cert == null) { 119 throw new JAXRException("Unknown alias in keystore"); 120 } 121 String keypass = 122 ProviderProperties.getInstance().getProperty("jaxr-ebxml.security.keypass"); 123 PrivateKey privateKey = (PrivateKey)keyStore.getKey( 124 alias, keypass.toCharArray()); 125 return new X500PrivateCredential(cert, privateKey, alias); 126 } catch (GeneralSecurityException x) { 127 throw new JAXRException(x); 128 } 129 } 130 131 public Certificate[] getCertificateChain(java.security.cert.X509Certificate cert) 132 throws JAXRException { 133 134 Certificate[] certChain = null; 135 getKeyStore(); 136 try { 137 String alias = keyStore.getCertificateAlias(cert); 138 certChain = keyStore.getCertificateChain(alias); 139 if (certChain == null) { 140 certChain = new Certificate[1]; 141 certChain[0] = cert; 142 } 143 } catch (KeyStoreException x) { 144 throw new JAXRException(x); 145 } 146 147 return certChain; 148 } 149 150 public void addAlias(String alias, String objId) { 151 aliasTable.setProperty(alias, objId); 152 } 153 154 }

This page was automatically generated by Maven