001: /*
002: * $Id: PGPKeyRingImpl.java 10808 2008-02-14 20:36:57Z acooke $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.pgp;
012:
013: import org.mule.api.lifecycle.Initialisable;
014: import org.mule.api.lifecycle.InitialisationException;
015: import org.mule.api.lifecycle.LifecycleTransitionResult;
016: import org.mule.config.i18n.CoreMessages;
017: import org.mule.util.IOUtils;
018:
019: import java.io.InputStream;
020: import java.security.Principal;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Iterator;
024:
025: import cryptix.pki.ExtendedKeyStore;
026: import cryptix.pki.KeyBundle;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: public class PGPKeyRingImpl implements PGPKeyRing, Initialisable {
031: protected static final Log logger = LogFactory
032: .getLog(PGPKeyRingImpl.class);
033:
034: private String publicKeyRingFileName;
035:
036: private HashMap principalsKeyBundleMap;
037:
038: private String secretKeyRingFileName;
039:
040: private String secretAliasId;
041:
042: private KeyBundle secretKeyBundle;
043:
044: private String secretPassphrase;
045:
046: public PGPKeyRingImpl() {
047: super ();
048: }
049:
050: public String getSecretKeyRingFileName() {
051: return secretKeyRingFileName;
052: }
053:
054: public void setSecretKeyRingFileName(String value) {
055: this .secretKeyRingFileName = value;
056: }
057:
058: public String getSecretAliasId() {
059: return secretAliasId;
060: }
061:
062: public void setSecretAliasId(String value) {
063: this .secretAliasId = value;
064: }
065:
066: public String getSecretPassphrase() {
067: return secretPassphrase;
068: }
069:
070: public void setSecretPassphrase(String value) {
071: this .secretPassphrase = value;
072: }
073:
074: private void readPrivateKeyBundle() throws Exception {
075: InputStream in = IOUtils.getResourceAsStream(
076: secretKeyRingFileName, getClass());
077:
078: ExtendedKeyStore ring = (ExtendedKeyStore) ExtendedKeyStore
079: .getInstance("OpenPGP/KeyRing");
080: ring.load(in, null);
081:
082: in.close();
083:
084: secretKeyBundle = ring.getKeyBundle(secretAliasId);
085: }
086:
087: public KeyBundle getSecretKeyBundle() {
088: return secretKeyBundle;
089: }
090:
091: /** @return */
092: public String getPublicKeyRingFileName() {
093: return publicKeyRingFileName;
094: }
095:
096: /** @param value */
097: public void setPublicKeyRingFileName(String value) {
098: this .publicKeyRingFileName = value;
099: }
100:
101: public KeyBundle getKeyBundle(String principalId) {
102: return (KeyBundle) principalsKeyBundleMap.get(principalId);
103: }
104:
105: public LifecycleTransitionResult initialise()
106: throws InitialisationException {
107: try {
108: java.security.Security
109: .addProvider(new cryptix.jce.provider.CryptixCrypto());
110: java.security.Security
111: .addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
112:
113: principalsKeyBundleMap = new HashMap();
114:
115: readPublicKeyRing();
116: readPrivateKeyBundle();
117: } catch (Exception e) {
118: logger.error(
119: "errore in inizializzazione:" + e.getMessage(), e);
120: throw new InitialisationException(CoreMessages
121: .failedToCreate("PGPKeyRingImpl"), e, this );
122: }
123: return LifecycleTransitionResult.OK;
124: }
125:
126: private void readPublicKeyRing() throws Exception {
127: logger.debug(System.getProperties().get("user.dir"));
128: InputStream in = IOUtils.getResourceAsStream(
129: publicKeyRingFileName, getClass());
130:
131: ExtendedKeyStore ring = (ExtendedKeyStore) ExtendedKeyStore
132: .getInstance("OpenPGP/KeyRing");
133: ring.load(in, null);
134: in.close();
135:
136: for (Enumeration e = ring.aliases(); e.hasMoreElements();) {
137: String aliasId = (String) e.nextElement();
138:
139: KeyBundle bundle = ring.getKeyBundle(aliasId);
140:
141: if (bundle != null) {
142: for (Iterator users = bundle.getPrincipals(); users
143: .hasNext();) {
144: Principal princ = (Principal) users.next();
145:
146: principalsKeyBundleMap.put(princ.getName(), bundle);
147: }
148: }
149: }
150: }
151: }
|