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