Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / keyring / PrivateKeyEntry.java
blob306349915706747d1119d7b7349432eb2d181c19
1 /* PrivateKeyEntry.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
19 USA
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.
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.key.IKeyPairCodec;
42 import gnu.java.security.key.KeyPairCodecFactory;
43 import gnu.java.security.key.dss.DSSPrivateKey;
44 import gnu.java.security.key.rsa.GnuRSAPrivateKey;
46 import gnu.javax.crypto.key.GnuSecretKey;
47 import gnu.javax.crypto.key.dh.GnuDHPrivateKey;
49 import java.io.DataInputStream;
50 import java.io.DataOutputStream;
51 import java.io.IOException;
52 import java.security.Key;
53 import java.security.KeyFactory;
54 import java.security.PrivateKey;
55 import java.security.spec.PKCS8EncodedKeySpec;
56 import java.util.Date;
58 /**
59 * <p>An immutable class representing a private or secret key entry.</p>
61 * @version $Revision: 1.1 $
63 public final class PrivateKeyEntry extends PrimitiveEntry
66 // Constants and variables
67 // -------------------------------------------------------------------------
69 public static final int TYPE = 7;
71 /** The key. */
72 private Key key;
74 // Constructor(s)
75 // -------------------------------------------------------------------------
77 /**
78 * <p>Creates a new key entry.</p>
80 * @param key The key.
81 * @param creationDate The entry creation date.
82 * @param properties The entry properties.
83 * @throws IllegalArgumentException If any parameter is null.
85 public PrivateKeyEntry(Key key, Date creationDate, Properties properties)
87 super(TYPE, creationDate, properties);
89 if (key == null)
91 throw new IllegalArgumentException("no private key");
93 if (!(key instanceof PrivateKey) && !(key instanceof GnuSecretKey))
95 throw new IllegalArgumentException("not a private or secret key");
97 this.key = key;
100 private PrivateKeyEntry()
102 super(TYPE);
105 // Class methods
106 // -------------------------------------------------------------------------
108 public static PrivateKeyEntry decode(DataInputStream in) throws IOException
110 PrivateKeyEntry entry = new PrivateKeyEntry();
111 entry.defaultDecode(in);
112 String type = entry.properties.get("type");
113 if (type == null)
115 throw new MalformedKeyringException("no key type");
117 if (type.equalsIgnoreCase("RAW-DSS"))
119 IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
120 entry.key = coder.decodePrivateKey(entry.payload);
122 else if (type.equalsIgnoreCase("RAW-RSA"))
124 IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
125 entry.key = coder.decodePrivateKey(entry.payload);
127 else if (type.equalsIgnoreCase("RAW-DH"))
129 IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
130 entry.key = coder.decodePrivateKey(entry.payload);
132 else if (type.equalsIgnoreCase("RAW"))
134 entry.key = new GnuSecretKey(entry.payload, null);
136 else if (type.equalsIgnoreCase("PKCS8"))
140 KeyFactory kf = KeyFactory.getInstance("RSA");
141 entry.key = kf.generatePrivate(new PKCS8EncodedKeySpec(
142 entry.payload));
144 catch (Exception x)
147 if (entry.key == null)
151 KeyFactory kf = KeyFactory.getInstance("DSA");
152 entry.key = kf.generatePrivate(new PKCS8EncodedKeySpec(
153 entry.payload));
155 catch (Exception x)
158 if (entry.key == null)
160 throw new MalformedKeyringException(
161 "could not decode PKCS#8 key");
165 else
167 throw new MalformedKeyringException("unsupported key type " + type);
169 return entry;
172 // Instance methods
173 // -------------------------------------------------------------------------
176 * <p>Returns this entry's key.</p>
178 * @return The key.
180 public Key getKey()
182 return key;
185 protected void encodePayload() throws IOException
187 String format = key.getFormat();
188 if (key instanceof DSSPrivateKey)
190 properties.put("type", "RAW-DSS");
191 IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dss");
192 payload = coder.encodePrivateKey((PrivateKey) key);
194 else if (key instanceof GnuRSAPrivateKey)
196 properties.put("type", "RAW-RSA");
197 IKeyPairCodec coder = KeyPairCodecFactory.getInstance("rsa");
198 payload = coder.encodePrivateKey((PrivateKey) key);
200 else if (key instanceof GnuDHPrivateKey)
202 properties.put("type", "RAW-DH");
203 IKeyPairCodec coder = KeyPairCodecFactory.getInstance("dh");
204 payload = coder.encodePrivateKey((PrivateKey) key);
206 else if (key instanceof GnuSecretKey)
208 properties.put("type", "RAW");
209 payload = key.getEncoded();
211 else if (format != null && format.equals("PKCS#8"))
213 properties.put("type", "PKCS8");
214 payload = key.getEncoded();
216 else
218 throw new IllegalArgumentException("unsupported private key");