Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / keyring / GnuPrivateKeyring.java
blobd49bd09636d617cf7315c1335233c2a8118a8c8c
1 /* GnuPrivateKeyring.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.keyring;
41 import gnu.java.security.Registry;
43 import java.io.DataInputStream;
44 import java.io.DataOutputStream;
45 import java.io.InputStream;
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.security.Key;
49 import java.security.PublicKey;
50 import java.security.UnrecoverableKeyException;
51 import java.security.cert.Certificate;
52 import java.util.Date;
53 import java.util.Iterator;
54 import java.util.List;
56 /**
57 * <p>.</p>
59 public class GnuPrivateKeyring extends BaseKeyring implements IPrivateKeyring
62 // Constants and variables
63 // -------------------------------------------------------------------------
65 public static final int USAGE = Registry.GKR_PRIVATE_KEYS
66 | Registry.GKR_PUBLIC_CREDENTIALS;
68 protected String mac;
70 protected int maclen;
72 protected String cipher;
74 protected String mode;
76 protected int keylen;
78 // Constructor(s)
79 // -------------------------------------------------------------------------
81 public GnuPrivateKeyring(String mac, int maclen, String cipher, String mode,
82 int keylen)
84 keyring = new PasswordAuthenticatedEntry(mac, maclen, new Properties());
85 keyring2 = new CompressedEntry(new Properties());
86 keyring.add(keyring2);
87 this.mac = mac;
88 this.maclen = maclen;
89 this.cipher = cipher;
90 this.mode = mode;
91 this.keylen = keylen;
94 public GnuPrivateKeyring()
96 this("HMAC-SHA-1", 20, "AES", "OFB", 16);
99 // Class methods
100 // -------------------------------------------------------------------------
102 // Instance methods
103 // -------------------------------------------------------------------------
105 public boolean containsPrivateKey(String alias)
107 if (!containsAlias(alias))
109 return false;
111 List l = get(alias);
112 for (Iterator it = l.iterator(); it.hasNext();)
114 if (it.next() instanceof PasswordAuthenticatedEntry)
116 return true;
119 return false;
122 public Key getPrivateKey(String alias, char[] password)
123 throws UnrecoverableKeyException
125 if (!containsAlias(alias))
127 return null;
129 List l = get(alias);
130 PasswordAuthenticatedEntry e1 = null;
131 PasswordEncryptedEntry e2 = null;
132 for (Iterator it = l.iterator(); it.hasNext();)
134 Entry e = (Entry) it.next();
135 if (e instanceof PasswordAuthenticatedEntry)
137 e1 = (PasswordAuthenticatedEntry) e;
138 break;
141 if (e1 == null)
143 return null;
147 e1.verify(password);
149 catch (Exception e)
151 throw new UnrecoverableKeyException("authentication failed");
153 for (Iterator it = e1.getEntries().iterator(); it.hasNext();)
155 Entry e = (Entry) it.next();
156 if (e instanceof PasswordEncryptedEntry)
158 e2 = (PasswordEncryptedEntry) e;
159 break;
162 if (e2 == null)
164 return null;
168 e2.decrypt(password);
170 catch (Exception e)
172 throw new UnrecoverableKeyException("decryption failed");
174 for (Iterator it = e2.get(alias).iterator(); it.hasNext();)
176 Entry e = (Entry) it.next();
177 if (e instanceof PrivateKeyEntry)
179 return ((PrivateKeyEntry) e).getKey();
182 return null;
185 public void putPrivateKey(String alias, Key key, char[] password)
187 if (containsPrivateKey(alias))
189 return;
191 alias = fixAlias(alias);
192 Properties p = new Properties();
193 p.put("alias", alias);
194 PrivateKeyEntry pke = new PrivateKeyEntry(key, new Date(), p);
195 PasswordEncryptedEntry enc = new PasswordEncryptedEntry(cipher, mode,
196 keylen,
197 new Properties());
198 PasswordAuthenticatedEntry auth = new PasswordAuthenticatedEntry(
199 mac,
200 maclen,
201 new Properties());
202 enc.add(pke);
203 auth.add(enc);
206 enc.encode(null, password);
207 auth.encode(null, password);
209 catch (IOException ioe)
211 throw new IllegalArgumentException(ioe.toString());
213 keyring.add(auth);
216 public boolean containsPublicKey(String alias)
218 if (!containsAlias(alias))
220 return false;
222 List l = get(alias);
223 for (Iterator it = l.iterator(); it.hasNext();)
225 if (it.next() instanceof PublicKeyEntry)
227 return true;
230 return false;
233 public PublicKey getPublicKey(String alias)
235 if (!containsAlias(alias))
237 return null;
239 List l = get(alias);
240 for (Iterator it = l.iterator(); it.hasNext();)
242 Entry e = (Entry) it.next();
243 if (e instanceof PublicKeyEntry)
245 return ((PublicKeyEntry) e).getKey();
248 return null;
251 public void putPublicKey(String alias, PublicKey key)
253 if (containsPublicKey(alias))
255 return;
257 Properties p = new Properties();
258 p.put("alias", fixAlias(alias));
259 add(new PublicKeyEntry(key, new Date(), p));
262 public boolean containsCertPath(String alias)
264 if (!containsAlias(alias))
266 return false;
268 List l = get(alias);
269 for (Iterator it = l.iterator(); it.hasNext();)
271 if (it.next() instanceof CertPathEntry)
273 return true;
276 return false;
279 public Certificate[] getCertPath(String alias)
281 if (!containsAlias(alias))
283 return null;
285 List l = get(alias);
286 for (Iterator it = l.iterator(); it.hasNext();)
288 Entry e = (Entry) it.next();
289 if (e instanceof CertPathEntry)
291 return ((CertPathEntry) e).getCertPath();
294 return null;
297 public void putCertPath(String alias, Certificate[] path)
299 if (containsCertPath(alias))
301 return;
303 Properties p = new Properties();
304 p.put("alias", fixAlias(alias));
305 add(new CertPathEntry(path, new Date(), p));
308 protected void load(InputStream in, char[] password) throws IOException
310 if (in.read() != USAGE)
312 throw new MalformedKeyringException("incompatible keyring usage");
314 if (in.read() != PasswordAuthenticatedEntry.TYPE)
316 throw new MalformedKeyringException(
317 "expecting password-authenticated entry tag");
319 keyring = PasswordAuthenticatedEntry.decode(new DataInputStream(in),
320 password);
323 protected void store(OutputStream out, char[] password) throws IOException
325 out.write(USAGE);
326 keyring.encode(new DataOutputStream(out), password);