2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.javax
.crypto
.jce
.keyring
;
41 import java
.io
.BufferedInputStream
;
42 import java
.io
.InputStream
;
43 import java
.io
.IOException
;
44 import java
.io
.OutputStream
;
46 import java
.security
.Key
;
47 import java
.security
.KeyStoreSpi
;
48 import java
.security
.KeyStoreException
;
49 import java
.security
.PrivateKey
;
50 import java
.security
.PublicKey
;
51 import java
.security
.UnrecoverableKeyException
;
52 import java
.security
.cert
.Certificate
;
53 import java
.security
.cert
.CertificateException
;
54 import java
.security
.cert
.CertificateFactory
;
56 import java
.util
.Arrays
;
57 import java
.util
.Date
;
58 import java
.util
.Enumeration
;
59 import java
.util
.HashMap
;
60 import java
.util
.Iterator
;
61 import java
.util
.List
;
62 import java
.util
.NoSuchElementException
;
64 import javax
.crypto
.SecretKey
;
66 import gnu
.java
.security
.Registry
;
67 import gnu
.javax
.crypto
.keyring
.IKeyring
;
68 import gnu
.javax
.crypto
.keyring
.IPrivateKeyring
;
69 import gnu
.javax
.crypto
.keyring
.IPublicKeyring
;
70 import gnu
.javax
.crypto
.keyring
.GnuPrivateKeyring
;
71 import gnu
.javax
.crypto
.keyring
.GnuPublicKeyring
;
72 import gnu
.javax
.crypto
.keyring
.MalformedKeyringException
;
73 import gnu
.javax
.crypto
.keyring
.PrimitiveEntry
;
75 public class GnuKeyring
extends KeyStoreSpi
78 // Constants and fields.
79 // ------------------------------------------------------------------------
81 private boolean loaded
;
83 private IKeyring keyring
;
86 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
95 public Enumeration
engineAliases()
99 throw new IllegalStateException ("not loaded");
103 return new Enumeration()
105 public boolean hasMoreElements()
110 public Object
nextElement()
112 throw new NoSuchElementException();
116 return keyring
.aliases();
119 public boolean engineContainsAlias(String alias
)
123 throw new IllegalStateException ("not loaded");
129 return keyring
.containsAlias(alias
);
132 public void engineDeleteEntry(String alias
)
136 throw new IllegalStateException ("not loaded");
140 keyring
.remove(alias
);
144 public Certificate
engineGetCertificate(String alias
)
148 throw new IllegalStateException ("not loaded");
154 if (!(keyring
instanceof IPublicKeyring
))
156 throw new IllegalStateException("not a public keyring");
158 return ((IPublicKeyring
) keyring
).getCertificate(alias
);
161 public String
engineGetCertificateAlias(Certificate cert
)
165 throw new IllegalStateException ("not loaded");
171 if (!(keyring
instanceof IPublicKeyring
))
173 throw new IllegalStateException("not a public keyring");
175 Enumeration aliases
= keyring
.aliases();
176 while (aliases
.hasMoreElements())
178 String alias
= (String
) aliases
.nextElement();
179 Certificate cert2
= ((IPublicKeyring
) keyring
).getCertificate(alias
);
180 if (cert
.equals(cert2
))
188 public void engineSetCertificateEntry(String alias
, Certificate cert
)
192 throw new IllegalStateException ("not loaded");
196 keyring
= new GnuPublicKeyring("HMAC-SHA-1", 20);
198 if (!(keyring
instanceof IPublicKeyring
))
200 throw new IllegalStateException("not a public keyring");
202 ((IPublicKeyring
) keyring
).putCertificate(alias
, cert
);
205 public Certificate
[] engineGetCertificateChain(String alias
)
209 throw new IllegalStateException ("not loaded");
215 if (!(keyring
instanceof IPrivateKeyring
))
217 throw new IllegalStateException("not a private keyring");
219 return ((IPrivateKeyring
) keyring
).getCertPath(alias
);
222 public Date
engineGetCreationDate(String alias
)
226 throw new IllegalStateException ("not loaded");
232 List entries
= keyring
.get(alias
);
233 if (entries
.size() == 0)
237 for (Iterator it
= entries
.iterator(); it
.hasNext();)
239 Object o
= it
.next();
240 if (o
instanceof PrimitiveEntry
)
242 return ((PrimitiveEntry
) o
).getCreationDate();
248 public Key
engineGetKey(String alias
, char[] password
)
249 throws UnrecoverableKeyException
253 throw new IllegalStateException ("not loaded");
259 if (!(keyring
instanceof IPrivateKeyring
))
261 throw new IllegalStateException("not a private keyring");
263 if (password
== null)
265 if (((IPrivateKeyring
) keyring
).containsPublicKey(alias
))
267 return ((IPrivateKeyring
) keyring
).getPublicKey(alias
);
270 if (((IPrivateKeyring
) keyring
).containsPrivateKey(alias
))
272 return ((IPrivateKeyring
) keyring
).getPrivateKey(alias
, password
);
277 public void engineSetKeyEntry(String alias
, Key key
, char[] password
,
278 Certificate
[] chain
) throws KeyStoreException
282 throw new IllegalStateException ("not loaded");
286 keyring
= new GnuPrivateKeyring("HMAC-SHA-1", 20, "AES", "OFB", 16);
288 if (!(keyring
instanceof IPrivateKeyring
))
290 throw new IllegalStateException("not a private keyring");
292 if (key
instanceof PublicKey
)
294 ((IPrivateKeyring
) keyring
).putPublicKey(alias
, (PublicKey
) key
);
297 if (!(key
instanceof PrivateKey
) && !(key
instanceof SecretKey
))
299 throw new KeyStoreException("cannot store keys of type "
300 + key
.getClass().getName());
304 CertificateFactory fact
= CertificateFactory
.getInstance("X.509");
305 ((IPrivateKeyring
) keyring
).putCertPath(alias
, chain
);
307 catch (CertificateException ce
)
309 throw new KeyStoreException(ce
.toString());
311 ((IPrivateKeyring
) keyring
).putPrivateKey(alias
, key
, password
);
314 public void engineSetKeyEntry(String alias
, byte[] key
, Certificate
[] chain
)
315 throws KeyStoreException
317 throw new KeyStoreException("method not supported");
320 public boolean engineIsCertificateEntry(String alias
)
324 throw new IllegalStateException ("not loaded");
330 if (!(keyring
instanceof IPublicKeyring
))
334 return ((IPublicKeyring
) keyring
).containsCertificate(alias
);
337 public boolean engineIsKeyEntry(String alias
)
341 throw new IllegalStateException ("not loaded");
347 if (!(keyring
instanceof IPrivateKeyring
))
351 return ((IPrivateKeyring
) keyring
).containsPublicKey(alias
)
352 || ((IPrivateKeyring
) keyring
).containsPrivateKey(alias
);
355 public void engineLoad(InputStream in
, char[] password
) throws IOException
359 if (!in
.markSupported())
361 in
= new BufferedInputStream(in
);
364 for (int i
= 0; i
< 4; i
++)
365 if (in
.read() != Registry
.GKR_MAGIC
[i
])
366 throw new MalformedKeyringException("incorrect magic");
367 int usage
= in
.read();
369 HashMap attr
= new HashMap();
370 attr
.put(IKeyring
.KEYRING_DATA_IN
, in
);
371 attr
.put(IKeyring
.KEYRING_PASSWORD
, password
);
374 case GnuPublicKeyring
.USAGE
:
375 keyring
= new GnuPublicKeyring();
377 case GnuPrivateKeyring
.USAGE
:
378 keyring
= new GnuPrivateKeyring();
381 throw new MalformedKeyringException("unsupported ring usage: "
382 + Integer
.toBinaryString(usage
));
389 public void engineStore(OutputStream out
, char[] password
) throws IOException
391 if (!loaded
|| keyring
== null)
393 throw new IllegalStateException ("not loaded");
395 HashMap attr
= new HashMap();
396 attr
.put(IKeyring
.KEYRING_DATA_OUT
, out
);
397 attr
.put(IKeyring
.KEYRING_PASSWORD
, password
);
401 public int engineSize()
405 throw new IllegalStateException ("not loaded");
411 return keyring
.size();