1
2
3
4
5
6
7 package org.mule.module.pgp;
8
9 import java.io.InputStream;
10 import java.util.HashMap;
11 import java.util.Iterator;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.bouncycastle.jce.provider.BouncyCastleProvider;
16 import org.bouncycastle.openpgp.PGPPublicKey;
17 import org.bouncycastle.openpgp.PGPPublicKeyRing;
18 import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
19 import org.bouncycastle.openpgp.PGPSecretKey;
20 import org.bouncycastle.openpgp.PGPSecretKeyRing;
21 import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
22 import org.mule.api.lifecycle.Initialisable;
23 import org.mule.api.lifecycle.InitialisationException;
24 import org.mule.config.i18n.CoreMessages;
25 import org.mule.module.pgp.i18n.PGPMessages;
26 import org.mule.util.IOUtils;
27
28 public class PGPKeyRingImpl implements PGPKeyRing, Initialisable
29 {
30 protected static final Log logger = LogFactory.getLog(PGPKeyRingImpl.class);
31
32 private String publicKeyRingFileName;
33
34 private HashMap<String, PGPPublicKey> principalsKeyBundleMap;
35
36 private String secretKeyRingFileName;
37
38 private String secretAliasId;
39
40 private PGPSecretKey secretKey;
41
42 private String secretPassphrase;
43
44 public void initialise() throws InitialisationException
45 {
46 try
47 {
48 java.security.Security.addProvider(new BouncyCastleProvider());
49
50 principalsKeyBundleMap = new HashMap<String, PGPPublicKey>();
51
52 readPublicKeyRing();
53 readPrivateKeyBundle();
54 }
55 catch (Exception e)
56 {
57 logger.error("Error in initialise:" + e.getMessage(), e);
58 throw new InitialisationException(CoreMessages.failedToCreate("PGPKeyRingImpl"), e, this);
59 }
60 }
61
62 private void readPublicKeyRing() throws Exception
63 {
64 InputStream in = IOUtils.getResourceAsStream(getPublicKeyRingFileName(), getClass());
65 PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(in);
66 in.close();
67
68 for (Iterator iterator = collection.getKeyRings(); iterator.hasNext();)
69 {
70 PGPPublicKeyRing ring = (PGPPublicKeyRing) iterator.next();
71 String userID = "";
72 for (Iterator iterator2 = ring.getPublicKeys(); iterator2.hasNext();)
73 {
74 PGPPublicKey publicKey = (PGPPublicKey) iterator2.next();
75 Iterator userIDs = publicKey.getUserIDs();
76 if (userIDs.hasNext())
77 {
78 userID = (String) userIDs.next();
79 }
80 principalsKeyBundleMap.put(userID, publicKey);
81 }
82 }
83 }
84
85 private void readPrivateKeyBundle() throws Exception
86 {
87 InputStream in = IOUtils.getResourceAsStream(getSecretKeyRingFileName(), getClass());
88 PGPSecretKeyRingCollection collection = new PGPSecretKeyRingCollection(in);
89 in.close();
90 secretKey = collection.getSecretKey(Long.valueOf(getSecretAliasId()));
91
92 if (secretKey == null)
93 {
94 StringBuffer message = new StringBuffer();
95 message.append('\n');
96 Iterator iterator = collection.getKeyRings();
97 while (iterator.hasNext())
98 {
99 PGPSecretKeyRing ring = (PGPSecretKeyRing) iterator.next();
100 Iterator secretKeysIterator = ring.getSecretKeys();
101 while (secretKeysIterator.hasNext())
102 {
103 PGPSecretKey k = (PGPSecretKey) secretKeysIterator.next();
104 message.append("Key: ");
105 message.append(k.getKeyID());
106 message.append('\n');
107 }
108 }
109 throw new InitialisationException(PGPMessages.noSecretKeyFoundButAvailable(message.toString()),
110 this);
111 }
112 }
113
114 public String getSecretKeyRingFileName()
115 {
116 return secretKeyRingFileName;
117 }
118
119 public void setSecretKeyRingFileName(String value)
120 {
121 this.secretKeyRingFileName = value;
122 }
123
124 public String getSecretAliasId()
125 {
126 return secretAliasId;
127 }
128
129 public void setSecretAliasId(String value)
130 {
131 this.secretAliasId = value;
132 }
133
134 public String getSecretPassphrase()
135 {
136 return secretPassphrase;
137 }
138
139 public void setSecretPassphrase(String value)
140 {
141 this.secretPassphrase = value;
142 }
143
144 public PGPSecretKey getSecretKey()
145 {
146 return secretKey;
147 }
148
149 public String getPublicKeyRingFileName()
150 {
151 return publicKeyRingFileName;
152 }
153
154 public void setPublicKeyRingFileName(String value)
155 {
156 this.publicKeyRingFileName = value;
157 }
158
159 public PGPPublicKey getPublicKey(String principalId)
160 {
161 return principalsKeyBundleMap.get(principalId);
162 }
163 }