1 |
769 |
jeremybenn |
/* GnuPublicKeyring.java --
|
2 |
|
|
Copyright (C) 2003, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is a part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.javax.crypto.keyring;
|
40 |
|
|
|
41 |
|
|
import gnu.java.security.Configuration;
|
42 |
|
|
import gnu.java.security.Registry;
|
43 |
|
|
|
44 |
|
|
import java.io.DataInputStream;
|
45 |
|
|
import java.io.DataOutputStream;
|
46 |
|
|
import java.io.IOException;
|
47 |
|
|
import java.io.InputStream;
|
48 |
|
|
import java.io.OutputStream;
|
49 |
|
|
import java.security.cert.Certificate;
|
50 |
|
|
import java.util.Date;
|
51 |
|
|
import java.util.Iterator;
|
52 |
|
|
import java.util.logging.Logger;
|
53 |
|
|
|
54 |
|
|
public class GnuPublicKeyring
|
55 |
|
|
extends BaseKeyring
|
56 |
|
|
implements IPublicKeyring
|
57 |
|
|
{
|
58 |
|
|
private static final Logger log = Logger.getLogger(GnuPublicKeyring.class.getName());
|
59 |
|
|
public static final int USAGE = Registry.GKR_CERTIFICATES;
|
60 |
|
|
|
61 |
|
|
public GnuPublicKeyring(String mac, int macLen)
|
62 |
|
|
{
|
63 |
|
|
keyring = new PasswordAuthenticatedEntry(mac, macLen, new Properties());
|
64 |
|
|
keyring2 = new CompressedEntry(new Properties());
|
65 |
|
|
keyring.add(keyring2);
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
public GnuPublicKeyring()
|
69 |
|
|
{
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
public boolean containsCertificate(String alias)
|
73 |
|
|
{
|
74 |
|
|
if (Configuration.DEBUG)
|
75 |
|
|
log.entering(this.getClass().getName(), "containsCertificate", alias);
|
76 |
|
|
boolean result = false;
|
77 |
|
|
if (containsAlias(alias))
|
78 |
|
|
for (Iterator it = get(alias).iterator(); it.hasNext();)
|
79 |
|
|
if (it.next() instanceof CertificateEntry)
|
80 |
|
|
{
|
81 |
|
|
result = true;
|
82 |
|
|
break;
|
83 |
|
|
}
|
84 |
|
|
if (Configuration.DEBUG)
|
85 |
|
|
log.exiting(this.getClass().getName(), "containsCertificate",
|
86 |
|
|
Boolean.valueOf(result));
|
87 |
|
|
return result;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
public Certificate getCertificate(String alias)
|
91 |
|
|
{
|
92 |
|
|
if (Configuration.DEBUG)
|
93 |
|
|
log.entering(this.getClass().getName(), "getCertificate", alias);
|
94 |
|
|
Certificate result = null;
|
95 |
|
|
if (containsAlias(alias))
|
96 |
|
|
for (Iterator it = get(alias).iterator(); it.hasNext();)
|
97 |
|
|
{
|
98 |
|
|
Entry e = (Entry) it.next();
|
99 |
|
|
if (e instanceof CertificateEntry)
|
100 |
|
|
{
|
101 |
|
|
result = ((CertificateEntry) e).getCertificate();
|
102 |
|
|
break;
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
if (Configuration.DEBUG)
|
106 |
|
|
log.exiting(this.getClass().getName(), "getCertificate", result);
|
107 |
|
|
return result;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
public void putCertificate(String alias, Certificate cert)
|
111 |
|
|
{
|
112 |
|
|
if (Configuration.DEBUG)
|
113 |
|
|
log.entering(this.getClass().getName(), "putCertificate",
|
114 |
|
|
new Object[] { alias, cert });
|
115 |
|
|
if (! containsCertificate(alias))
|
116 |
|
|
{
|
117 |
|
|
Properties p = new Properties();
|
118 |
|
|
p.put("alias", fixAlias(alias));
|
119 |
|
|
add(new CertificateEntry(cert, new Date(), p));
|
120 |
|
|
}
|
121 |
|
|
else if (Configuration.DEBUG)
|
122 |
|
|
log.fine("Keyring already contains alias: " + alias);
|
123 |
|
|
if (Configuration.DEBUG)
|
124 |
|
|
log.exiting(this.getClass().getName(), "putCertificate");
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
protected void load(InputStream in, char[] password) throws IOException
|
128 |
|
|
{
|
129 |
|
|
if (Configuration.DEBUG)
|
130 |
|
|
log.entering(this.getClass().getName(), "load");
|
131 |
|
|
if (in.read() != USAGE)
|
132 |
|
|
throw new MalformedKeyringException("incompatible keyring usage");
|
133 |
|
|
if (in.read() != PasswordAuthenticatedEntry.TYPE)
|
134 |
|
|
throw new MalformedKeyringException(
|
135 |
|
|
"expecting password-authenticated entry tag");
|
136 |
|
|
DataInputStream dis = new DataInputStream(in);
|
137 |
|
|
keyring = PasswordAuthenticatedEntry.decode(dis, password);
|
138 |
|
|
if (Configuration.DEBUG)
|
139 |
|
|
log.exiting(this.getClass().getName(), "load");
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
protected void store(OutputStream out, char[] password) throws IOException
|
143 |
|
|
{
|
144 |
|
|
if (Configuration.DEBUG)
|
145 |
|
|
log.entering(this.getClass().getName(), "store");
|
146 |
|
|
out.write(USAGE);
|
147 |
|
|
keyring.encode(new DataOutputStream(out), password);
|
148 |
|
|
if (Configuration.DEBUG)
|
149 |
|
|
log.exiting(this.getClass().getName(), "store");
|
150 |
|
|
}
|
151 |
|
|
}
|