1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54: public final class PublicKeyEntry
55: extends PrimitiveEntry
56: {
57: public static final int TYPE = 6;
58: private PublicKey key;
59:
60: public PublicKeyEntry(PublicKey key, Date creationDate, Properties properties)
61: {
62: super(TYPE, creationDate, properties);
63: if (key == null)
64: throw new IllegalArgumentException("no key specified");
65: this.key = key;
66: }
67:
68: private PublicKeyEntry()
69: {
70: super(TYPE);
71: }
72:
73: public static PublicKeyEntry decode(DataInputStream in) throws IOException
74: {
75: PublicKeyEntry entry = new PublicKeyEntry();
76: entry.defaultDecode(in);
77: String type = entry.properties.get("type");
78: if (type == null)
79: throw new MalformedKeyringException("no key type");
80: if (type.equalsIgnoreCase("RAW-DSS"))
81: {
82: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
83: entry.key = coder.decodePublicKey(entry.payload);
84: }
85: else if (type.equalsIgnoreCase("RAW-RSA"))
86: {
87: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
88: entry.key = coder.decodePublicKey(entry.payload);
89: }
90: else if (type.equalsIgnoreCase("RAW-DH"))
91: {
92: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
93: entry.key = coder.decodePublicKey(entry.payload);
94: }
95: else if (type.equalsIgnoreCase("X.509"))
96: {
97: try
98: {
99: KeyFactory kf = KeyFactory.getInstance("RSA");
100: entry.key = kf.generatePublic(new X509EncodedKeySpec(entry.payload));
101: }
102: catch (Exception x)
103: {
104: }
105: if (entry.key == null)
106: {
107: try
108: {
109: KeyFactory kf = KeyFactory.getInstance("DSA");
110: entry.key = kf.generatePublic(new X509EncodedKeySpec(entry.payload));
111: }
112: catch (Exception x)
113: {
114: }
115: if (entry.key == null)
116: throw new MalformedKeyringException("could not decode X.509 key");
117: }
118: }
119: else
120: throw new MalformedKeyringException("unsupported public key type: " + type);
121: return entry;
122: }
123:
124:
129: public PublicKey getKey()
130: {
131: return key;
132: }
133:
134: protected void encodePayload() throws IOException
135: {
136: if (key instanceof DSSPublicKey)
137: {
138: properties.put("type", "RAW-DSS");
139: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
140: payload = coder.encodePublicKey(key);
141: }
142: else if (key instanceof GnuRSAPublicKey)
143: {
144: properties.put("type", "RAW-RSA");
145: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
146: payload = coder.encodePublicKey(key);
147: }
148: else if (key instanceof GnuDHPublicKey)
149: {
150: properties.put("type", "RAW-DH");
151: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
152: payload = coder.encodePublicKey(key);
153: }
154: else if (key.getFormat() != null && key.getFormat().equals("X.509"))
155: {
156: properties.put("type", "X.509");
157: payload = key.getEncoded();
158: }
159: else
160: throw new IllegalArgumentException("cannot encode public key");
161: }
162: }