1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
59: public final class PrivateKeyEntry
60: extends PrimitiveEntry
61: {
62: public static final int TYPE = 7;
63:
64: private Key key;
65:
66:
74: public PrivateKeyEntry(Key key, Date creationDate, Properties properties)
75: {
76: super(TYPE, creationDate, properties);
77: if (key == null)
78: throw new IllegalArgumentException("no private key");
79: if (! (key instanceof PrivateKey) && ! (key instanceof GnuSecretKey))
80: throw new IllegalArgumentException("not a private or secret key");
81: this.key = key;
82: }
83:
84: private PrivateKeyEntry()
85: {
86: super(TYPE);
87: }
88:
89: public static PrivateKeyEntry decode(DataInputStream in) throws IOException
90: {
91: PrivateKeyEntry entry = new PrivateKeyEntry();
92: entry.defaultDecode(in);
93: String type = entry.properties.get("type");
94: if (type == null)
95: throw new MalformedKeyringException("no key type");
96: if (type.equalsIgnoreCase("RAW-DSS"))
97: {
98: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
99: entry.key = coder.decodePrivateKey(entry.payload);
100: }
101: else if (type.equalsIgnoreCase("RAW-RSA"))
102: {
103: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
104: entry.key = coder.decodePrivateKey(entry.payload);
105: }
106: else if (type.equalsIgnoreCase("RAW-DH"))
107: {
108: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
109: entry.key = coder.decodePrivateKey(entry.payload);
110: }
111: else if (type.equalsIgnoreCase("RAW"))
112: entry.key = new GnuSecretKey(entry.payload, null);
113: else if (type.equalsIgnoreCase("PKCS8"))
114: {
115: try
116: {
117: KeyFactory kf = KeyFactory.getInstance("RSA");
118: PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(entry.payload);
119: entry.key = kf.generatePrivate(ks);
120: }
121: catch (Exception ignored)
122: {
123: }
124: if (entry.key == null)
125: {
126: try
127: {
128: KeyFactory kf = KeyFactory.getInstance("DSA");
129: PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(entry.payload);
130: entry.key = kf.generatePrivate(ks);
131: }
132: catch (Exception ignored)
133: {
134: }
135: if (entry.key == null)
136: throw new MalformedKeyringException("could not decode PKCS#8 key");
137: }
138: }
139: else
140: throw new MalformedKeyringException("unsupported key type " + type);
141: return entry;
142: }
143:
144:
149: public Key getKey()
150: {
151: return key;
152: }
153:
154: protected void encodePayload() throws IOException
155: {
156: String format = key.getFormat();
157: if (key instanceof DSSPrivateKey)
158: {
159: properties.put("type", "RAW-DSS");
160: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
161: payload = coder.encodePrivateKey((PrivateKey) key);
162: }
163: else if (key instanceof GnuRSAPrivateKey)
164: {
165: properties.put("type", "RAW-RSA");
166: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
167: payload = coder.encodePrivateKey((PrivateKey) key);
168: }
169: else if (key instanceof GnuDHPrivateKey)
170: {
171: properties.put("type", "RAW-DH");
172: IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
173: payload = coder.encodePrivateKey((PrivateKey) key);
174: }
175: else if (key instanceof GnuSecretKey)
176: {
177: properties.put("type", "RAW");
178: payload = key.getEncoded();
179: }
180: else if (format != null && format.equals("PKCS#8"))
181: {
182: properties.put("type", "PKCS8");
183: payload = key.getEncoded();
184: }
185: else
186: throw new IllegalArgumentException("unsupported private key");
187: }
188:
189: public String toString()
190: {
191: return "PrivateKeyEntry{key="
192: + (key == null ? "-" : key.getClass().getName()) + "}";
193: }
194: }