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
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
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
;
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;
75 // -------------------------------------------------------------------------
78 * <p>Creates a new key entry.</p>
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
);
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");
100 private PrivateKeyEntry()
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");
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(
147 if (entry
.key
== null)
151 KeyFactory kf
= KeyFactory
.getInstance("DSA");
152 entry
.key
= kf
.generatePrivate(new PKCS8EncodedKeySpec(
158 if (entry
.key
== null)
160 throw new MalformedKeyringException(
161 "could not decode PKCS#8 key");
167 throw new MalformedKeyringException("unsupported key type " + type
);
173 // -------------------------------------------------------------------------
176 * <p>Returns this entry's key.</p>
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();
218 throw new IllegalArgumentException("unsupported private key");