001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.node;
028:
029: import java.io.File;
030: import java.io.InputStream;
031: import java.security.KeyStore;
032: import java.security.PrivateKey;
033: import java.util.HashMap;
034:
035: import org.cougaar.bootstrap.SystemProperties;
036: import org.cougaar.util.ConfigFinder;
037:
038: /**
039: * A container for security keystore information and functionality.
040: *
041: * @property org.cougaar.install.path
042: * Used to find keystore as "org.cougaar.install.path/configs/common/.keystore"
043: *
044: * @property org.cougaar.security.keystore.password
045: * The password to the cougaar keystore.
046: *
047: * @property org.cougaar.security.keystore
048: * The URL of the cougaar keystore.
049: */
050: public final class KeyRing {
051: private static String ksPass;
052: private static String ksPath;
053:
054: private static KeyStore keystore = null;
055:
056: static {
057: String installpath = SystemProperties
058: .getProperty("org.cougaar.install.path");
059: String defaultKeystorePath = installpath + File.separatorChar
060: + "configs" + File.separatorChar + "common"
061: + File.separatorChar + ".keystore";
062:
063: ksPass = SystemProperties.getProperty(
064: "org.cougaar.security.keystore.password", "alpalp");
065: ksPath = SystemProperties.getProperty(
066: "org.cougaar.security.keystore", defaultKeystorePath);
067:
068: System.out.println("Secure message keystore: path=" + ksPath
069: + ", pass=" + ksPass);
070: }
071:
072: private static void init() {
073: try {
074: keystore = KeyStore.getInstance(KeyStore.getDefaultType());
075: InputStream kss = ConfigFinder.getInstance().open(ksPath);
076: keystore.load(kss, ksPass.toCharArray());
077: kss.close();
078: } catch (Exception e) {
079: e.printStackTrace();
080: }
081: }
082:
083: private static Object guard = new Object();
084:
085: public static KeyStore getKeyStore() {
086: synchronized (guard) {
087: if (keystore == null)
088: init();
089: return keystore;
090: }
091: }
092:
093: private static HashMap privateKeys = new HashMap(89);
094:
095: static PrivateKey getPrivateKey(String name) {
096: PrivateKey pk = null;
097: try {
098: synchronized (privateKeys) {
099: pk = (PrivateKey) privateKeys.get(name);
100: if (pk == null) {
101: pk = (PrivateKey) getKeyStore().getKey(name,
102: ksPass.toCharArray());
103: privateKeys.put(name, pk);
104: }
105: }
106: } catch (Exception e) {
107: System.err.println("Failed to get PrivateKey for \"" + name
108: + "\": " + e);
109: e.printStackTrace();
110: }
111: return pk;
112: }
113:
114: private static HashMap certs = new HashMap(89);
115:
116: static java.security.cert.Certificate getCert(String name) {
117: java.security.cert.Certificate cert = null;
118: try {
119: synchronized (certs) {
120: cert = (java.security.cert.Certificate) certs.get(name);
121: if (cert == null) {
122: cert = getKeyStore().getCertificate(name);
123: certs.put(name, cert);
124: }
125: }
126: } catch (Exception e) {
127: System.err.println("Failed to get Certificate for \""
128: + name + "\": " + e);
129: }
130: return cert;
131: }
132: }
|