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