1 10 11 package org.mule.extras.pgp; 12 13 import cryptix.message.EncryptedMessage; 14 import cryptix.message.EncryptedMessageBuilder; 15 import cryptix.message.LiteralMessageBuilder; 16 import cryptix.message.Message; 17 import cryptix.message.MessageFactory; 18 import cryptix.message.SignedMessageBuilder; 19 import cryptix.openpgp.PGPArmouredMessage; 20 import cryptix.pki.KeyBundle; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.mule.config.i18n.Messages; 25 import org.mule.umo.UMOEncryptionStrategy; 26 import org.mule.umo.lifecycle.InitialisationException; 27 import org.mule.umo.security.CryptoFailureException; 28 29 import java.io.ByteArrayInputStream ; 30 import java.util.Collection ; 31 32 35 public class KeyBasedEncryptionStrategy implements UMOEncryptionStrategy 36 { 37 protected static Log logger = LogFactory.getLog(KeyBasedEncryptionStrategy.class); 38 39 private PGPKeyRing keyManager; 40 41 46 public byte[] encrypt(byte[] data, Object cryptInfo) throws CryptoFailureException 47 { 48 try 49 { 50 PGPCryptInfo pgpCryptInfo = (PGPCryptInfo)cryptInfo; 51 KeyBundle publicKey = pgpCryptInfo.getKeyBundle(); 52 53 LiteralMessageBuilder lmb = LiteralMessageBuilder.getInstance("OpenPGP"); 54 55 lmb.init(data); 56 57 Message msg = lmb.build(); 58 59 if (pgpCryptInfo.isSignRequested()) 60 { 61 SignedMessageBuilder smb = SignedMessageBuilder.getInstance("OpenPGP"); 62 63 smb.init(msg); 64 smb.addSigner(keyManager.getSecretKeyBundle(), keyManager.getSecretPassphrase().toCharArray()); 65 66 msg = smb.build(); 67 } 68 69 EncryptedMessageBuilder emb = EncryptedMessageBuilder.getInstance("OpenPGP"); 70 emb.init(msg); 71 emb.addRecipient(publicKey); 72 msg = emb.build(); 73 74 return new PGPArmouredMessage(msg).getEncoded(); 75 } 76 catch (Exception e) 77 { 78 throw new CryptoFailureException(this, e); 79 } 80 } 81 82 87 public byte[] decrypt(byte[] data, Object cryptInfo) throws CryptoFailureException 88 { 89 try 90 { 91 MessageFactory mf = MessageFactory.getInstance("OpenPGP"); 92 93 ByteArrayInputStream in = new ByteArrayInputStream (data); 94 95 Collection msgs = mf.generateMessages(in); 96 97 Message msg = (Message)msgs.iterator().next(); 98 99 if (msg instanceof EncryptedMessage) 100 { 101 msg = ((EncryptedMessage)msg).decrypt(keyManager.getSecretKeyBundle(), 102 keyManager.getSecretPassphrase().toCharArray()); 103 104 return new PGPArmouredMessage(msg).getEncoded(); 105 } 106 } 107 catch (Exception e) 108 { 109 throw new CryptoFailureException(this, e); 110 } 111 112 return data; 113 } 114 115 120 public void initialise() throws InitialisationException 121 { 122 try 123 { 124 java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto()); 125 java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP()); 126 } 127 catch (Exception e) 128 { 129 throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X, 130 "KeyBasedEncryptionStrategy"), e, this); 131 } 132 } 133 134 public PGPKeyRing getKeyManager() 135 { 136 return keyManager; 137 } 138 139 public void setKeyManager(PGPKeyRing keyManager) 140 { 141 this.keyManager = keyManager; 142 } 143 } 144 | Popular Tags |