blob: e7c5f53b4d4291d7517efb9d75748c8c81a2a032 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.crypto.provider;
27
28import java.util.*;
29import java.lang.*;
30import java.security.Key;
31import java.security.PublicKey;
32import java.security.PrivateKey;
33import java.security.KeyFactorySpi;
34import java.security.InvalidKeyException;
35import java.security.spec.KeySpec;
36import java.security.spec.InvalidKeySpecException;
37import java.security.spec.X509EncodedKeySpec;
38import java.security.spec.PKCS8EncodedKeySpec;
39import javax.crypto.spec.DHPublicKeySpec;
40import javax.crypto.spec.DHPrivateKeySpec;
41import javax.crypto.spec.DHParameterSpec;
42
43/**
44 * This class implements the Diffie-Hellman key factory of the Sun provider.
45 *
46 * @author Jan Luehe
47 *
48 */
49public final class DHKeyFactory extends KeyFactorySpi {
50
51 /**
52 * Verify the SunJCE provider in the constructor.
53 *
54 * @exception SecurityException if fails to verify
55 * its own integrity
56 */
57 public DHKeyFactory() {
58 if (!SunJCE.verifySelfIntegrity(this.getClass())) {
59 throw new SecurityException("The SunJCE provider may have " +
60 "been tampered.");
61 }
62 }
63
64 /**
65 * Generates a public key object from the provided key specification
66 * (key material).
67 *
68 * @param keySpec the specification (key material) of the public key
69 *
70 * @return the public key
71 *
72 * @exception InvalidKeySpecException if the given key specification
73 * is inappropriate for this key factory to produce a public key.
74 */
75 protected PublicKey engineGeneratePublic(KeySpec keySpec)
76 throws InvalidKeySpecException
77 {
78 try {
79 if (keySpec instanceof DHPublicKeySpec) {
80 DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
81 return new DHPublicKey(dhPubKeySpec.getY(),
82 dhPubKeySpec.getP(),
83 dhPubKeySpec.getG());
84
85 } else if (keySpec instanceof X509EncodedKeySpec) {
86 return new DHPublicKey
87 (((X509EncodedKeySpec)keySpec).getEncoded());
88
89 } else {
90 throw new InvalidKeySpecException
91 ("Inappropriate key specification");
92 }
93 } catch (InvalidKeyException e) {
94 throw new InvalidKeySpecException
95 ("Inappropriate key specification");
96 }
97 }
98
99 /**
100 * Generates a private key object from the provided key specification
101 * (key material).
102 *
103 * @param keySpec the specification (key material) of the private key
104 *
105 * @return the private key
106 *
107 * @exception InvalidKeySpecException if the given key specification
108 * is inappropriate for this key factory to produce a private key.
109 */
110 protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
111 throws InvalidKeySpecException
112 {
113 try {
114 if (keySpec instanceof DHPrivateKeySpec) {
115 DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
116 return new DHPrivateKey(dhPrivKeySpec.getX(),
117 dhPrivKeySpec.getP(),
118 dhPrivKeySpec.getG());
119
120 } else if (keySpec instanceof PKCS8EncodedKeySpec) {
121 return new DHPrivateKey
122 (((PKCS8EncodedKeySpec)keySpec).getEncoded());
123
124 } else {
125 throw new InvalidKeySpecException
126 ("Inappropriate key specification");
127 }
128 } catch (InvalidKeyException e) {
129 throw new InvalidKeySpecException
130 ("Inappropriate key specification");
131 }
132 }
133
134 /**
135 * Returns a specification (key material) of the given key object
136 * in the requested format.
137 *
138 * @param key the key
139 *
140 * @param keySpec the requested format in which the key material shall be
141 * returned
142 *
143 * @return the underlying key specification (key material) in the
144 * requested format
145 *
146 * @exception InvalidKeySpecException if the requested key specification is
147 * inappropriate for the given key, or the given key cannot be processed
148 * (e.g., the given key has an unrecognized algorithm or format).
149 */
150 protected KeySpec engineGetKeySpec(Key key, Class keySpec)
151 throws InvalidKeySpecException {
152 DHParameterSpec params;
153
154 if (key instanceof javax.crypto.interfaces.DHPublicKey) {
155
156 if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
157 javax.crypto.interfaces.DHPublicKey dhPubKey
158 = (javax.crypto.interfaces.DHPublicKey) key;
159 params = dhPubKey.getParams();
160 return new DHPublicKeySpec(dhPubKey.getY(),
161 params.getP(),
162 params.getG());
163
164 } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
165 return new X509EncodedKeySpec(key.getEncoded());
166
167 } else {
168 throw new InvalidKeySpecException
169 ("Inappropriate key specification");
170 }
171
172 } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
173
174 if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
175 javax.crypto.interfaces.DHPrivateKey dhPrivKey
176 = (javax.crypto.interfaces.DHPrivateKey)key;
177 params = dhPrivKey.getParams();
178 return new DHPrivateKeySpec(dhPrivKey.getX(),
179 params.getP(),
180 params.getG());
181
182 } else if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
183 return new PKCS8EncodedKeySpec(key.getEncoded());
184
185 } else {
186 throw new InvalidKeySpecException
187 ("Inappropriate key specification");
188 }
189
190 } else {
191 throw new InvalidKeySpecException("Inappropriate key type");
192 }
193 }
194
195 /**
196 * Translates a key object, whose provider may be unknown or potentially
197 * untrusted, into a corresponding key object of this key factory.
198 *
199 * @param key the key whose provider is unknown or untrusted
200 *
201 * @return the translated key
202 *
203 * @exception InvalidKeyException if the given key cannot be processed by
204 * this key factory.
205 */
206 protected Key engineTranslateKey(Key key)
207 throws InvalidKeyException
208 {
209 try {
210
211 if (key instanceof javax.crypto.interfaces.DHPublicKey) {
212 // Check if key originates from this factory
213 if (key instanceof com.sun.crypto.provider.DHPublicKey) {
214 return key;
215 }
216 // Convert key to spec
217 DHPublicKeySpec dhPubKeySpec
218 = (DHPublicKeySpec)engineGetKeySpec
219 (key, DHPublicKeySpec.class);
220 // Create key from spec, and return it
221 return engineGeneratePublic(dhPubKeySpec);
222
223 } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
224 // Check if key originates from this factory
225 if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
226 return key;
227 }
228 // Convert key to spec
229 DHPrivateKeySpec dhPrivKeySpec
230 = (DHPrivateKeySpec)engineGetKeySpec
231 (key, DHPrivateKeySpec.class);
232 // Create key from spec, and return it
233 return engineGeneratePrivate(dhPrivKeySpec);
234
235 } else {
236 throw new InvalidKeyException("Wrong algorithm type");
237 }
238
239 } catch (InvalidKeySpecException e) {
240 throw new InvalidKeyException("Cannot translate key");
241 }
242 }
243}