1 10 11 package org.mule.extras.pgp; 12 13 import java.io.InputStream ; 14 import java.security.Principal ; 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.mule.config.i18n.Messages; 22 import org.mule.umo.lifecycle.InitialisationException; 23 import org.mule.util.IOUtils; 24 25 import cryptix.pki.ExtendedKeyStore; 26 import cryptix.pki.KeyBundle; 27 28 31 public class PGPKeyRingImpl implements PGPKeyRing 32 { 33 protected static Log logger = LogFactory.getLog(PGPKeyRingImpl.class); 34 35 private String publicKeyRingFileName; 36 37 private HashMap principalsKeyBundleMap; 38 39 private String secretKeyRingFileName; 40 41 private String secretAliasId; 42 43 private KeyBundle secretKeyBundle; 44 45 private String secretPassphrase; 46 47 public PGPKeyRingImpl() 48 { 49 super(); 50 } 51 52 public String getSecretKeyRingFileName() 53 { 54 return secretKeyRingFileName; 55 } 56 57 public void setSecretKeyRingFileName(String value) 58 { 59 this.secretKeyRingFileName = value; 60 } 61 62 public String getSecretAliasId() 63 { 64 return secretAliasId; 65 } 66 67 public void setSecretAliasId(String value) 68 { 69 this.secretAliasId = value; 70 } 71 72 public String getSecretPassphrase() 73 { 74 return secretPassphrase; 75 } 76 77 public void setSecretPassphrase(String value) 78 { 79 this.secretPassphrase = value; 80 } 81 82 private void readPrivateKeyBundle() throws Exception 83 { 84 InputStream in = IOUtils.getResourceAsStream(secretKeyRingFileName, getClass()); 85 86 ExtendedKeyStore ring = (ExtendedKeyStore)ExtendedKeyStore.getInstance("OpenPGP/KeyRing"); 87 ring.load(in, null); 88 89 in.close(); 90 91 secretKeyBundle = ring.getKeyBundle(secretAliasId); 92 } 93 94 public KeyBundle getSecretKeyBundle() 95 { 96 return secretKeyBundle; 97 } 98 99 102 public String getPublicKeyRingFileName() 103 { 104 return publicKeyRingFileName; 105 } 106 107 110 public void setPublicKeyRingFileName(String value) 111 { 112 this.publicKeyRingFileName = value; 113 } 114 115 public KeyBundle getKeyBundle(String principalId) 116 { 117 return (KeyBundle)principalsKeyBundleMap.get(principalId); 118 } 119 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 principalsKeyBundleMap = new HashMap (); 128 129 readPublicKeyRing(); 130 readPrivateKeyBundle(); 131 } 132 catch (Exception e) 133 { 134 logger.error("errore in inizializzazione:" + e.getMessage(), e); 135 throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X, 136 "PGPKeyRingImpl"), e); 137 } 138 } 139 140 private void readPublicKeyRing() throws Exception 141 { 142 logger.debug(System.getProperties().get("user.dir")); 143 InputStream in = IOUtils.getResourceAsStream(publicKeyRingFileName, getClass()); 144 145 ExtendedKeyStore ring = (ExtendedKeyStore)ExtendedKeyStore.getInstance("OpenPGP/KeyRing"); 146 ring.load(in, null); 147 in.close(); 148 149 for (Enumeration e = ring.aliases(); e.hasMoreElements();) 150 { 151 String aliasId = (String )e.nextElement(); 152 153 KeyBundle bundle = ring.getKeyBundle(aliasId); 154 155 if (bundle != null) 156 { 157 for (Iterator users = bundle.getPrincipals(); users.hasNext();) 158 { 159 Principal princ = (Principal )users.next(); 160 161 principalsKeyBundleMap.put(princ.getName(), bundle); 162 } 163 } 164 } 165 } 166 } 167 | Popular Tags |