001: /*
002: * $Id: KeyBasedEncryptionStrategy.java 11389 2008-03-17 14:29:31Z marie.rizzo $
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.RequestContext;
014: import org.mule.api.MuleEvent;
015: import org.mule.api.lifecycle.InitialisationException;
016: import org.mule.api.lifecycle.LifecycleTransitionResult;
017: import org.mule.api.security.CredentialsAccessor;
018: import org.mule.api.security.CryptoFailureException;
019: import org.mule.config.i18n.CoreMessages;
020: import org.mule.security.AbstractNamedEncryptionStrategy;
021:
022: import java.io.ByteArrayInputStream;
023: import java.util.Collection;
024:
025: import cryptix.message.EncryptedMessage;
026: import cryptix.message.EncryptedMessageBuilder;
027: import cryptix.message.LiteralMessageBuilder;
028: import cryptix.message.Message;
029: import cryptix.message.MessageFactory;
030: import cryptix.message.SignedMessageBuilder;
031: import cryptix.openpgp.PGPArmouredMessage;
032: import cryptix.pki.KeyBundle;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: public class KeyBasedEncryptionStrategy extends
037: AbstractNamedEncryptionStrategy {
038: /**
039: * logger used by this class
040: */
041: protected static final Log logger = LogFactory
042: .getLog(KeyBasedEncryptionStrategy.class);
043:
044: private PGPKeyRing keyManager;
045: private CredentialsAccessor credentialsAccessor;
046:
047: public byte[] encrypt(byte[] data, Object cryptInfo)
048: throws CryptoFailureException {
049: try {
050: PGPCryptInfo pgpCryptInfo;
051: KeyBundle publicKey;
052:
053: if (cryptInfo == null) {
054: MuleEvent event = RequestContext.getEvent();
055: publicKey = keyManager
056: .getKeyBundle((String) credentialsAccessor
057: .getCredentials(event));
058:
059: pgpCryptInfo = new PGPCryptInfo(publicKey, false);
060: } else {
061: pgpCryptInfo = (PGPCryptInfo) cryptInfo;
062: publicKey = pgpCryptInfo.getKeyBundle();
063: }
064:
065: LiteralMessageBuilder lmb = LiteralMessageBuilder
066: .getInstance("OpenPGP");
067:
068: lmb.init(data);
069:
070: Message msg = lmb.build();
071:
072: if (pgpCryptInfo.isSignRequested()) {
073: SignedMessageBuilder smb = SignedMessageBuilder
074: .getInstance("OpenPGP");
075:
076: smb.init(msg);
077: smb.addSigner(keyManager.getSecretKeyBundle(),
078: keyManager.getSecretPassphrase().toCharArray());
079:
080: msg = smb.build();
081: }
082:
083: EncryptedMessageBuilder emb = EncryptedMessageBuilder
084: .getInstance("OpenPGP");
085: emb.init(msg);
086: emb.addRecipient(publicKey);
087: msg = emb.build();
088:
089: return new PGPArmouredMessage(msg).getEncoded();
090: } catch (Exception e) {
091: throw new CryptoFailureException(this , e);
092: }
093: }
094:
095: public byte[] decrypt(byte[] data, Object cryptInfo)
096: throws CryptoFailureException {
097: try {
098: MessageFactory mf = MessageFactory.getInstance("OpenPGP");
099:
100: ByteArrayInputStream in = new ByteArrayInputStream(data);
101:
102: Collection msgs = mf.generateMessages(in);
103:
104: Message msg = (Message) msgs.iterator().next();
105:
106: if (msg instanceof EncryptedMessage) {
107: msg = ((EncryptedMessage) msg).decrypt(keyManager
108: .getSecretKeyBundle(), keyManager
109: .getSecretPassphrase().toCharArray());
110:
111: return new PGPArmouredMessage(msg).getEncoded();
112: }
113: } catch (Exception e) {
114: throw new CryptoFailureException(this , e);
115: }
116:
117: return data;
118: }
119:
120: public LifecycleTransitionResult initialise()
121: throws InitialisationException {
122: try {
123: java.security.Security
124: .addProvider(new cryptix.jce.provider.CryptixCrypto());
125: java.security.Security
126: .addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
127: } catch (Exception e) {
128: throw new InitialisationException(CoreMessages
129: .failedToCreate("KeyBasedEncryptionStrategy"), e,
130: this );
131: }
132: return LifecycleTransitionResult.OK;
133: }
134:
135: public PGPKeyRing getKeyManager() {
136: return keyManager;
137: }
138:
139: public void setKeyManager(PGPKeyRing keyManager) {
140: this .keyManager = keyManager;
141: }
142:
143: public CredentialsAccessor getCredentialsAccessor() {
144: return credentialsAccessor;
145: }
146:
147: public void setCredentialsAccessor(
148: CredentialsAccessor credentialsAccessor) {
149: this.credentialsAccessor = credentialsAccessor;
150: }
151: }
|