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