blob: 23478f8fc0e8ab375d292ef5518dc1d84f5a931f [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.io.UnsupportedEncodingException;
29import java.security.InvalidKeyException;
30import java.security.spec.KeySpec;
31import java.security.spec.InvalidKeySpecException;
32import javax.crypto.SecretKey;
33import javax.crypto.SecretKeyFactorySpi;
34import javax.crypto.spec.PBEKeySpec;
35import java.util.HashSet;
36
37/**
38 * This class implements a key factory for PBE keys according to PKCS#5,
39 * meaning that the password must consist of printable ASCII characters
40 * (values 32 to 126 decimal inclusive) and only the low order 8 bits
41 * of each password character are used.
42 *
43 * @author Jan Luehe
44 *
45 */
46abstract class PBEKeyFactory extends SecretKeyFactorySpi {
47
48 private String type;
49 private static HashSet<String> validTypes;
50
51 /**
52 * Verify the SunJCE provider in the constructor.
53 *
54 * @exception SecurityException if fails to verify
55 * its own integrity
56 */
57 private PBEKeyFactory(String keytype) {
58 if (!SunJCE.verifySelfIntegrity(this.getClass())) {
59 throw new SecurityException("The SunJCE provider may have " +
60 "been tampered.");
61 }
62 type = keytype;
63 }
64
65 static {
66 validTypes = new HashSet<String>(4);
67 validTypes.add("PBEWithMD5AndDES".toUpperCase());
68 validTypes.add("PBEWithSHA1AndDESede".toUpperCase());
69 validTypes.add("PBEWithSHA1AndRC2_40".toUpperCase());
70 // Proprietary algorithm.
71 validTypes.add("PBEWithMD5AndTripleDES".toUpperCase());
72 }
73
74 public static final class PBEWithMD5AndDES
75 extends PBEKeyFactory {
76 public PBEWithMD5AndDES() {
77 super("PBEWithMD5AndDES");
78 }
79 }
80
81 public static final class PBEWithSHA1AndDESede
82 extends PBEKeyFactory {
83 public PBEWithSHA1AndDESede() {
84 super("PBEWithSHA1AndDESede");
85 }
86 }
87
88 public static final class PBEWithSHA1AndRC2_40
89 extends PBEKeyFactory {
90 public PBEWithSHA1AndRC2_40() {
91 super("PBEWithSHA1AndRC2_40");
92 }
93 }
94
95 /*
96 * Private proprietary algorithm for supporting JCEKS.
97 */
98 public static final class PBEWithMD5AndTripleDES
99 extends PBEKeyFactory {
100 public PBEWithMD5AndTripleDES() {
101 super("PBEWithMD5AndTripleDES");
102 }
103 }
104
105
106 /**
107 * Generates a <code>SecretKey</code> object from the provided key
108 * specification (key material).
109 *
110 * @param keySpec the specification (key material) of the secret key
111 *
112 * @return the secret key
113 *
114 * @exception InvalidKeySpecException if the given key specification
115 * is inappropriate for this key factory to produce a public key.
116 */
117 protected SecretKey engineGenerateSecret(KeySpec keySpec)
118 throws InvalidKeySpecException
119 {
120 if (!(keySpec instanceof PBEKeySpec)) {
121 throw new InvalidKeySpecException("Invalid key spec");
122 }
123 return new PBEKey((PBEKeySpec)keySpec, type);
124 }
125
126 /**
127 * Returns a specification (key material) of the given key
128 * in the requested format.
129 *
130 * @param key the key
131 *
132 * @param keySpec the requested format in which the key material shall be
133 * returned
134 *
135 * @return the underlying key specification (key material) in the
136 * requested format
137 *
138 * @exception InvalidKeySpecException if the requested key specification is
139 * inappropriate for the given key, or the given key cannot be processed
140 * (e.g., the given key has an unrecognized algorithm or format).
141 */
142 protected KeySpec engineGetKeySpec(SecretKey key, Class keySpecCl)
143 throws InvalidKeySpecException {
144 if ((key instanceof SecretKey)
145 && (validTypes.contains(key.getAlgorithm().toUpperCase()))
146 && (key.getFormat().equalsIgnoreCase("RAW"))) {
147
148 // Check if requested key spec is amongst the valid ones
149 if ((keySpecCl != null)
150 && PBEKeySpec.class.isAssignableFrom(keySpecCl)) {
151 byte[] passwdBytes = key.getEncoded();
152 char[] passwdChars = new char[passwdBytes.length];
153 for (int i=0; i<passwdChars.length; i++)
154 passwdChars[i] = (char) (passwdBytes[i] & 0x7f);
155 PBEKeySpec ret = new PBEKeySpec(passwdChars);
156 // password char[] was cloned in PBEKeySpec constructor,
157 // so we can zero it out here
158 java.util.Arrays.fill(passwdChars, ' ');
159 java.util.Arrays.fill(passwdBytes, (byte)0x00);
160 return ret;
161 } else {
162 throw new InvalidKeySpecException("Invalid key spec");
163 }
164 } else {
165 throw new InvalidKeySpecException("Invalid key "
166 + "format/algorithm");
167 }
168 }
169
170 /**
171 * Translates a <code>SecretKey</code> object, whose provider may be
172 * unknown or potentially untrusted, into a corresponding
173 * <code>SecretKey</code> object of this key factory.
174 *
175 * @param key the key whose provider is unknown or untrusted
176 *
177 * @return the translated key
178 *
179 * @exception InvalidKeyException if the given key cannot be processed by
180 * this key factory.
181 */
182 protected SecretKey engineTranslateKey(SecretKey key)
183 throws InvalidKeyException
184 {
185 try {
186 if ((key != null) &&
187 (validTypes.contains(key.getAlgorithm().toUpperCase())) &&
188 (key.getFormat().equalsIgnoreCase("RAW"))) {
189
190 // Check if key originates from this factory
191 if (key instanceof com.sun.crypto.provider.PBEKey) {
192 return key;
193 }
194
195 // Convert key to spec
196 PBEKeySpec pbeKeySpec = (PBEKeySpec)engineGetKeySpec
197 (key, PBEKeySpec.class);
198
199 // Create key from spec, and return it
200 return engineGenerateSecret(pbeKeySpec);
201 } else {
202 throw new InvalidKeyException("Invalid key format/algorithm");
203 }
204
205 } catch (InvalidKeySpecException ikse) {
206 throw new InvalidKeyException("Cannot translate key: "
207 + ikse.getMessage());
208 }
209 }
210}