History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/crypto/provider/DHKeyFactory.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/crypto/provider/DHKeyFactory.java+236−0
@@ -0,0 +1,236 @@
1+/*
2+ * Copyright (c) 1997, 2009, Oracle and/or its affiliates. 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. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package com.sun.crypto.provider;
27+
28+import java.util.*;
29+import java.lang.*;
30+import java.security.Key;
31+import java.security.PublicKey;
32+import java.security.PrivateKey;
33+import java.security.KeyFactorySpi;
34+import java.security.InvalidKeyException;
35+import java.security.spec.KeySpec;
36+import java.security.spec.InvalidKeySpecException;
37+import java.security.spec.X509EncodedKeySpec;
38+import java.security.spec.PKCS8EncodedKeySpec;
39+import javax.crypto.spec.DHPublicKeySpec;
40+import javax.crypto.spec.DHPrivateKeySpec;
41+import 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+ */
49+public final class DHKeyFactory extends KeyFactorySpi {
50+
51+ /**
52+ * Empty constructor
53+ */
54+ public DHKeyFactory() {
55+ }
56+
57+ /**
58+ * Generates a public key object from the provided key specification
59+ * (key material).
60+ *
61+ * @param keySpec the specification (key material) of the public key
62+ *
63+ * @return the public key
64+ *
65+ * @exception InvalidKeySpecException if the given key specification
66+ * is inappropriate for this key factory to produce a public key.
67+ */
68+ protected PublicKey engineGeneratePublic(KeySpec keySpec)
69+ throws InvalidKeySpecException
70+ {
71+ try {
72+ if (keySpec instanceof DHPublicKeySpec) {
73+ DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)keySpec;
74+ return new DHPublicKey(dhPubKeySpec.getY(),
75+ dhPubKeySpec.getP(),
76+ dhPubKeySpec.getG());
77+
78+ } else if (keySpec instanceof X509EncodedKeySpec) {
79+ return new DHPublicKey
80+ (((X509EncodedKeySpec)keySpec).getEncoded());
81+
82+ } else {
83+ throw new InvalidKeySpecException
84+ ("Inappropriate key specification");
85+ }
86+ } catch (InvalidKeyException e) {
87+ throw new InvalidKeySpecException
88+ ("Inappropriate key specification");
89+ }
90+ }
91+
92+ /**
93+ * Generates a private key object from the provided key specification
94+ * (key material).
95+ *
96+ * @param keySpec the specification (key material) of the private key
97+ *
98+ * @return the private key
99+ *
100+ * @exception InvalidKeySpecException if the given key specification
101+ * is inappropriate for this key factory to produce a private key.
102+ */
103+ protected PrivateKey engineGeneratePrivate(KeySpec keySpec)
104+ throws InvalidKeySpecException
105+ {
106+ try {
107+ if (keySpec instanceof DHPrivateKeySpec) {
108+ DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec;
109+ return new DHPrivateKey(dhPrivKeySpec.getX(),
110+ dhPrivKeySpec.getP(),
111+ dhPrivKeySpec.getG());
112+
113+ } else if (keySpec instanceof PKCS8EncodedKeySpec) {
114+ return new DHPrivateKey
115+ (((PKCS8EncodedKeySpec)keySpec).getEncoded());
116+
117+ } else {
118+ throw new InvalidKeySpecException
119+ ("Inappropriate key specification");
120+ }
121+ } catch (InvalidKeyException e) {
122+ throw new InvalidKeySpecException
123+ ("Inappropriate key specification");
124+ }
125+ }
126+
127+ /**
128+ * Returns a specification (key material) of the given key object
129+ * in the requested format.
130+ *
131+ * @param key the key
132+ *
133+ * @param keySpec the requested format in which the key material shall be
134+ * returned
135+ *
136+ * @return the underlying key specification (key material) in the
137+ * requested format
138+ *
139+ * @exception InvalidKeySpecException if the requested key specification is
140+ * inappropriate for the given key, or the given key cannot be processed
141+ * (e.g., the given key has an unrecognized algorithm or format).
142+ */
143+ protected KeySpec engineGetKeySpec(Key key, Class keySpec)
144+ throws InvalidKeySpecException {
145+ DHParameterSpec params;
146+
147+ if (key instanceof javax.crypto.interfaces.DHPublicKey) {
148+
149+ if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
150+ javax.crypto.interfaces.DHPublicKey dhPubKey
151+ = (javax.crypto.interfaces.DHPublicKey) key;
152+ params = dhPubKey.getParams();
153+ return new DHPublicKeySpec(dhPubKey.getY(),
154+ params.getP(),
155+ params.getG());
156+
157+ } else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
158+ return new X509EncodedKeySpec(key.getEncoded());
159+
160+ } else {
161+ throw new InvalidKeySpecException
162+ ("Inappropriate key specification");
163+ }
164+
165+ } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
166+
167+ if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
168+ javax.crypto.interfaces.DHPrivateKey dhPrivKey
169+ = (javax.crypto.interfaces.DHPrivateKey)key;
170+ params = dhPrivKey.getParams();
171+ return new DHPrivateKeySpec(dhPrivKey.getX(),
172+ params.getP(),
173+ params.getG());
174+
175+ } else if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
176+ return new PKCS8EncodedKeySpec(key.getEncoded());
177+
178+ } else {
179+ throw new InvalidKeySpecException
180+ ("Inappropriate key specification");
181+ }
182+
183+ } else {
184+ throw new InvalidKeySpecException("Inappropriate key type");
185+ }
186+ }
187+
188+ /**
189+ * Translates a key object, whose provider may be unknown or potentially
190+ * untrusted, into a corresponding key object of this key factory.
191+ *
192+ * @param key the key whose provider is unknown or untrusted
193+ *
194+ * @return the translated key
195+ *
196+ * @exception InvalidKeyException if the given key cannot be processed by
197+ * this key factory.
198+ */
199+ protected Key engineTranslateKey(Key key)
200+ throws InvalidKeyException
201+ {
202+ try {
203+
204+ if (key instanceof javax.crypto.interfaces.DHPublicKey) {
205+ // Check if key originates from this factory
206+ if (key instanceof com.sun.crypto.provider.DHPublicKey) {
207+ return key;
208+ }
209+ // Convert key to spec
210+ DHPublicKeySpec dhPubKeySpec
211+ = (DHPublicKeySpec)engineGetKeySpec
212+ (key, DHPublicKeySpec.class);
213+ // Create key from spec, and return it
214+ return engineGeneratePublic(dhPubKeySpec);
215+
216+ } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
217+ // Check if key originates from this factory
218+ if (key instanceof com.sun.crypto.provider.DHPrivateKey) {
219+ return key;
220+ }
221+ // Convert key to spec
222+ DHPrivateKeySpec dhPrivKeySpec
223+ = (DHPrivateKeySpec)engineGetKeySpec
224+ (key, DHPrivateKeySpec.class);
225+ // Create key from spec, and return it
226+ return engineGeneratePrivate(dhPrivKeySpec);
227+
228+ } else {
229+ throw new InvalidKeyException("Wrong algorithm type");
230+ }
231+
232+ } catch (InvalidKeySpecException e) {
233+ throw new InvalidKeyException("Cannot translate key");
234+ }
235+ }
236+}