Newer
Older
Patrick Dowler
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
************************************************************************
**** C A N A D I A N A S T R O N O M Y D A T A C E N T R E *****
*
* (c) 2010. (c) 2010.
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits reserves
*
* NRC disclaims any warranties Le CNRC denie toute garantie
* expressed, implied, or statu- enoncee, implicite ou legale,
* tory, of any kind with respect de quelque nature que se soit,
* to the software, including concernant le logiciel, y com-
* without limitation any war- pris sans restriction toute
* ranty of merchantability or garantie de valeur marchande
* fitness for a particular pur- ou de pertinence pour un usage
* pose. NRC shall not be liable particulier. Le CNRC ne
* in any event for any damages, pourra en aucun cas etre tenu
* whether direct or indirect, responsable de tout dommage,
* special or general, consequen- direct ou indirect, particul-
* tial or incidental, arising ier ou general, accessoire ou
* from the use of the software. fortuit, resultant de l'utili-
* sation du logiciel.
*
*
* @author adriand
*
* @version $Revision: $
*
*
**** C A N A D I A N A S T R O N O M Y D A T A C E N T R E *****
************************************************************************
*/
package org.astrogrid.security.delegation;
import java.io.IOException;
import java.io.Writer;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMWriter;
/**
* A collection of delegated credentials. For each key there is a private key,
* a certificate-signing request (CSR) and, optionally, a certificate.
* <p>
* This class is thread safe. Initializing an identity is idempotent. The
* name, keys and CSR for each identity are immutable, and access to the
* certificate is synchronized. Further, the class will reject an attempt
* to set a certificate whose public key does not match that set for the
* identity at initialization; therefore, if two threads delegate to the
* same identity concurrently, the credentials held are not disrupted.
*
* @author Guy Rixon
*/
public class InMemoryDelegations extends Delegations
{
/**
* All the delegations, partial or complete, known to this
* object. The key-pairs for delegated credentials live here.
* The keys are hashes of the delegated principals: see
* {@link #hash} for details.
*/
private Map<String, DelegatedIdentity> identities;
private KeyPairGenerator keyPairGenerator;
/**
* Constructs a Delegations object.
*/
protected InMemoryDelegations() {
// Add the Bouncy Castle JCE provider. This allows the CSR
// classes to work. The BC implementation of PKCS#10 depends on
// the ciphers in the BC provider.
if (Security.getProvider("BC") == null) {
Security.addProvider(new BouncyCastleProvider());
}
erase();
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
throw new RuntimeException("The JCE doesn't do RSA! Game over.");
}
keyPairGenerator.initialize(1024);
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#erase()
*/
public void erase() {
identities = new ConcurrentHashMap<String, DelegatedIdentity>();
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#initializeIdentity(java.lang.String)
*/
public String initializeIdentity(String identity) throws GeneralSecurityException {
X500Principal p = new X500Principal(identity);
return initializeIdentity(p);
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#initializeIdentity(javax.security.auth.x500.X500Principal)
*/
public String initializeIdentity(X500Principal principal) throws GeneralSecurityException {
String hashKey = hash(principal);
if (!identities.containsKey(hashKey)) {
DelegatedIdentity id =
new DelegatedIdentity(principal.getName(X500Principal.CANONICAL),
this.keyPairGenerator.generateKeyPair());
identities.put(hashKey, id);
}
return hashKey;
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#getCsr(java.lang.String)
*/
public CertificateSigningRequest getCsr(String hashKey) {
DelegatedIdentity id = identities.get(hashKey);
return (id == null)? null : id.csr;
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#getPrivateKey(java.lang.String)
*/
public PrivateKey getPrivateKey(String hashKey) {
DelegatedIdentity id = identities.get(hashKey);
return (id == null)? null: (PrivateKey) id.keys.getPrivate();
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#getCertificate(java.lang.String)
*/
public X509Certificate[] getCertificates(String hashKey)
{
DelegatedIdentity id = identities.get(hashKey);
if (id == null)
{
return null;
}
else
{
synchronized (id)
{
return new X509Certificate[] { id.certificate };
}
}
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#remove(java.lang.String)
*/
public void remove(String hashKey) {
identities.remove(hashKey);
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#isKnown(java.lang.String)
*/
public boolean isKnown(String hashKey) {
return identities.containsKey(hashKey);
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#setCertificate(java.lang.String, java.security.cert.X509Certificate)
*/
public void setCertificates(String hashKey,
X509Certificate[] certificates) throws InvalidKeyException {
DelegatedIdentity id = identities.get(hashKey);
if (id == null) {
throw new InvalidKeyException("No identity matches the hash key " + hashKey);
} else {
id.setCertificate(certificates[0]);
}
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#getPrincipals()
*/
public Object[] getPrincipals() {
return identities.keySet().toArray();
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#getName(java.lang.String)
*/
public String getName(String hashKey) {
DelegatedIdentity id = identities.get(hashKey);
return (id == null)? null : id.dn;
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#getKeys(java.lang.String)
*/
public KeyPair getKeys(String hashKey) {
DelegatedIdentity id = identities.get(hashKey);
return (id == null)? null : id.getKeys();
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#writeCertificate(java.lang.String, java.io.Writer)
*/
public void writeCertificate(String hashKey, Writer out) throws IOException {
PEMWriter pem = new PEMWriter(out);
for (X509Certificate cert : getCertificates(hashKey))
{
pem.writeObject(cert);
}
pem.flush();
pem.close();
}
/* (non-Javadoc)
* @see org.astrogrid.security.delegation.Delegations#hasCertificate(java.lang.String)
*/
public boolean hasCertificate(String hashKey) {
X509Certificate[] certs = this.getCertificates(hashKey);
return ( certs != null && certs.length > 0);
}
protected class DelegatedIdentity {
protected final String dn;
protected final KeyPair keys;
protected final CertificateSigningRequest csr;
protected X509Certificate certificate;
protected DelegatedIdentity(String dn,
KeyPair keys) throws GeneralSecurityException {
this.dn = dn;
this.keys = keys;
this.csr = new CertificateSigningRequest(dn, keys);
this.certificate = null;
}
protected synchronized X509Certificate getCertificate() {
return certificate;
}
protected synchronized void setCertificate(X509Certificate c) throws InvalidKeyException {
if (c.getPublicKey().equals(keys.getPublic())) {
certificate = c;
}
else {
throw new InvalidKeyException("This certificate does not match the cached private-key.");
}
}
protected KeyPair getKeys() {
return keys;
}
}
}