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
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
.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
;
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
;
72 protected String cipher
;
74 protected String mode
;
79 // -------------------------------------------------------------------------
81 public GnuPrivateKeyring(String mac
, int maclen
, String cipher
, String mode
,
84 keyring
= new PasswordAuthenticatedEntry(mac
, maclen
, new Properties());
85 keyring2
= new CompressedEntry(new Properties());
86 keyring
.add(keyring2
);
94 public GnuPrivateKeyring()
96 this("HMAC-SHA-1", 20, "AES", "OFB", 16);
100 // -------------------------------------------------------------------------
103 // -------------------------------------------------------------------------
105 public boolean containsPrivateKey(String alias
)
107 if (!containsAlias(alias
))
112 for (Iterator it
= l
.iterator(); it
.hasNext();)
114 if (it
.next() instanceof PasswordAuthenticatedEntry
)
122 public Key
getPrivateKey(String alias
, char[] password
)
123 throws UnrecoverableKeyException
125 if (!containsAlias(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
;
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
;
168 e2
.decrypt(password
);
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();
185 public void putPrivateKey(String alias
, Key key
, char[] password
)
187 if (containsPrivateKey(alias
))
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
,
198 PasswordAuthenticatedEntry auth
= new PasswordAuthenticatedEntry(
206 enc
.encode(null, password
);
207 auth
.encode(null, password
);
209 catch (IOException ioe
)
211 throw new IllegalArgumentException(ioe
.toString());
216 public boolean containsPublicKey(String alias
)
218 if (!containsAlias(alias
))
223 for (Iterator it
= l
.iterator(); it
.hasNext();)
225 if (it
.next() instanceof PublicKeyEntry
)
233 public PublicKey
getPublicKey(String alias
)
235 if (!containsAlias(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();
251 public void putPublicKey(String alias
, PublicKey key
)
253 if (containsPublicKey(alias
))
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
))
269 for (Iterator it
= l
.iterator(); it
.hasNext();)
271 if (it
.next() instanceof CertPathEntry
)
279 public Certificate
[] getCertPath(String alias
)
281 if (!containsAlias(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();
297 public void putCertPath(String alias
, Certificate
[] path
)
299 if (containsCertPath(alias
))
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
),
323 protected void store(OutputStream out
, char[] password
) throws IOException
326 keyring
.encode(new DataOutputStream(out
), password
);