Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / jce / keyring / GnuKeyring.java
blobd74d386b4f017291fd611af4f5cd0c79a15485d9
1 /* GnuKeyring.java --
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
19 USA
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
24 combination.
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;
85 // Constructor.
86 // ------------------------------------------------------------------------
88 public GnuKeyring()
92 // Instance methods.
93 // ------------------------------------------------------------------------
95 public Enumeration engineAliases()
97 if (!loaded)
99 throw new IllegalStateException ("not loaded");
101 if (keyring == null)
103 return new Enumeration()
105 public boolean hasMoreElements()
107 return false;
110 public Object nextElement()
112 throw new NoSuchElementException();
116 return keyring.aliases();
119 public boolean engineContainsAlias(String alias)
121 if (!loaded)
123 throw new IllegalStateException ("not loaded");
125 if (keyring == null)
127 return false;
129 return keyring.containsAlias(alias);
132 public void engineDeleteEntry(String alias)
134 if (!loaded)
136 throw new IllegalStateException ("not loaded");
138 if (keyring != null)
140 keyring.remove(alias);
144 public Certificate engineGetCertificate(String alias)
146 if (!loaded)
148 throw new IllegalStateException ("not loaded");
150 if (keyring == null)
152 return null;
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)
163 if (!loaded)
165 throw new IllegalStateException ("not loaded");
167 if (keyring == null)
169 return null;
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))
182 return alias;
185 return null;
188 public void engineSetCertificateEntry(String alias, Certificate cert)
190 if (!loaded)
192 throw new IllegalStateException ("not loaded");
194 if (keyring == null)
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)
207 if (!loaded)
209 throw new IllegalStateException ("not loaded");
211 if (keyring == null)
213 return null;
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)
224 if (!loaded)
226 throw new IllegalStateException ("not loaded");
228 if (keyring == null)
230 return null;
232 List entries = keyring.get(alias);
233 if (entries.size() == 0)
235 return null;
237 for (Iterator it = entries.iterator(); it.hasNext();)
239 Object o = it.next();
240 if (o instanceof PrimitiveEntry)
242 return ((PrimitiveEntry) o).getCreationDate();
245 return null;
248 public Key engineGetKey(String alias, char[] password)
249 throws UnrecoverableKeyException
251 if (!loaded)
253 throw new IllegalStateException ("not loaded");
255 if (keyring == null)
257 return null;
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);
274 return null;
277 public void engineSetKeyEntry(String alias, Key key, char[] password,
278 Certificate[] chain) throws KeyStoreException
280 if (!loaded)
282 throw new IllegalStateException ("not loaded");
284 if (keyring == null)
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);
295 return;
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)
322 if (!loaded)
324 throw new IllegalStateException ("not loaded");
326 if (keyring == null)
328 return false;
330 if (!(keyring instanceof IPublicKeyring))
332 return false;
334 return ((IPublicKeyring) keyring).containsCertificate(alias);
337 public boolean engineIsKeyEntry(String alias)
339 if (!loaded)
341 throw new IllegalStateException ("not loaded");
343 if (keyring == null)
345 return false;
347 if (!(keyring instanceof IPrivateKeyring))
349 return false;
351 return ((IPrivateKeyring) keyring).containsPublicKey(alias)
352 || ((IPrivateKeyring) keyring).containsPrivateKey(alias);
355 public void engineLoad(InputStream in, char[] password) throws IOException
357 if (in != null)
359 if (!in.markSupported())
361 in = new BufferedInputStream(in);
363 in.mark(5);
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();
368 in.reset();
369 HashMap attr = new HashMap();
370 attr.put(IKeyring.KEYRING_DATA_IN, in);
371 attr.put(IKeyring.KEYRING_PASSWORD, password);
372 switch (usage)
374 case GnuPublicKeyring.USAGE:
375 keyring = new GnuPublicKeyring();
376 break;
377 case GnuPrivateKeyring.USAGE:
378 keyring = new GnuPrivateKeyring();
379 break;
380 default:
381 throw new MalformedKeyringException("unsupported ring usage: "
382 + Integer.toBinaryString(usage));
384 keyring.load(attr);
386 loaded = true;
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);
398 keyring.store(attr);
401 public int engineSize()
403 if (!loaded)
405 throw new IllegalStateException ("not loaded");
407 if (keyring == null)
409 return 0;
411 return keyring.size();