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/DESedeKeyFactory.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/crypto/provider/DESedeKeyFactory.java+161−0
@@ -0,0 +1,161 @@
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 javax.crypto.SecretKey;
29+import javax.crypto.SecretKeyFactorySpi;
30+import javax.crypto.spec.DESedeKeySpec;
31+import java.security.InvalidKeyException;
32+import java.security.spec.KeySpec;
33+import java.security.spec.InvalidKeySpecException;
34+
35+/**
36+ * This class implements the DES-EDE key factory of the Sun provider.
37+ *
38+ * @author Jan Luehe
39+ *
40+ */
41+
42+public final class DESedeKeyFactory extends SecretKeyFactorySpi {
43+
44+ /**
45+ * Empty constructor
46+ */
47+ public DESedeKeyFactory() {
48+ }
49+
50+ /**
51+ * Generates a <code>SecretKey</code> object from the provided key
52+ * specification (key material).
53+ *
54+ * @param keySpec the specification (key material) of the secret key
55+ *
56+ * @return the secret key
57+ *
58+ * @exception InvalidKeySpecException if the given key specification
59+ * is inappropriate for this key factory to produce a public key.
60+ */
61+ protected SecretKey engineGenerateSecret(KeySpec keySpec)
62+ throws InvalidKeySpecException {
63+ DESedeKey desEdeKey = null;
64+
65+ try {
66+ if (keySpec instanceof DESedeKeySpec) {
67+ DESedeKeySpec desEdeKeySpec = (DESedeKeySpec)keySpec;
68+ desEdeKey = new DESedeKey(desEdeKeySpec.getKey());
69+
70+ } else {
71+ throw new InvalidKeySpecException
72+ ("Inappropriate key specification");
73+ }
74+ } catch (InvalidKeyException e) {
75+ }
76+ return desEdeKey;
77+ }
78+
79+ /**
80+ * Returns a specification (key material) of the given key
81+ * in the requested format.
82+ *
83+ * @param key the key
84+ *
85+ * @param keySpec the requested format in which the key material shall be
86+ * returned
87+ *
88+ * @return the underlying key specification (key material) in the
89+ * requested format
90+ *
91+ * @exception InvalidKeySpecException if the requested key specification is
92+ * inappropriate for the given key, or the given key cannot be processed
93+ * (e.g., the given key has an unrecognized algorithm or format).
94+ */
95+ protected KeySpec engineGetKeySpec(SecretKey key, Class keySpec)
96+ throws InvalidKeySpecException {
97+
98+ try {
99+ if ((key instanceof SecretKey)
100+ && (key.getAlgorithm().equalsIgnoreCase("DESede"))
101+ && (key.getFormat().equalsIgnoreCase("RAW"))) {
102+
103+ // Check if requested key spec is amongst the valid ones
104+ if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
105+ return new DESedeKeySpec(key.getEncoded());
106+
107+ } else {
108+ throw new InvalidKeySpecException
109+ ("Inappropriate key specification");
110+ }
111+
112+ } else {
113+ throw new InvalidKeySpecException
114+ ("Inappropriate key format/algorithm");
115+ }
116+ } catch (InvalidKeyException e) {
117+ throw new InvalidKeySpecException("Secret key has wrong size");
118+ }
119+ }
120+
121+ /**
122+ * Translates a <code>SecretKey</code> object, whose provider may be
123+ * unknown or potentially untrusted, into a corresponding
124+ * <code>SecretKey</code> object of this key factory.
125+ *
126+ * @param key the key whose provider is unknown or untrusted
127+ *
128+ * @return the translated key
129+ *
130+ * @exception InvalidKeyException if the given key cannot be processed by
131+ * this key factory.
132+ */
133+ protected SecretKey engineTranslateKey(SecretKey key)
134+ throws InvalidKeyException {
135+
136+ try {
137+
138+ if ((key != null)
139+ && (key.getAlgorithm().equalsIgnoreCase("DESede"))
140+ && (key.getFormat().equalsIgnoreCase("RAW"))) {
141+ // Check if key originates from this factory
142+ if (key instanceof com.sun.crypto.provider.DESedeKey) {
143+ return key;
144+ }
145+ // Convert key to spec
146+ DESedeKeySpec desEdeKeySpec
147+ = (DESedeKeySpec)engineGetKeySpec(key,
148+ DESedeKeySpec.class);
149+ // Create key from spec, and return it
150+ return engineGenerateSecret(desEdeKeySpec);
151+
152+ } else {
153+ throw new InvalidKeyException
154+ ("Inappropriate key format/algorithm");
155+ }
156+
157+ } catch (InvalidKeySpecException e) {
158+ throw new InvalidKeyException("Cannot translate key");
159+ }
160+ }
161+}