author | valeriep |
Tue, 08 May 2012 17:57:48 -0700 | |
changeset 12685 | 8a448b5b9006 |
parent 11835 | c9e7cfc908b3 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.crypto.provider; |
|
27 |
||
28 |
import java.security.KeyRep; |
|
29 |
import java.security.spec.InvalidKeySpecException; |
|
30 |
import javax.crypto.SecretKey; |
|
31 |
import javax.crypto.spec.PBEKeySpec; |
|
32 |
||
33 |
/** |
|
34 |
* This class represents a PBE key. |
|
35 |
* |
|
36 |
* @author Jan Luehe |
|
37 |
* |
|
38 |
*/ |
|
39 |
final class PBEKey implements SecretKey { |
|
40 |
||
41 |
static final long serialVersionUID = -2234768909660948176L; |
|
42 |
||
43 |
private byte[] key; |
|
44 |
||
45 |
private String type; |
|
46 |
||
47 |
/** |
|
48 |
* Creates a PBE key from a given PBE key specification. |
|
49 |
* |
|
50 |
* @param key the given PBE key specification |
|
51 |
*/ |
|
52 |
PBEKey(PBEKeySpec keySpec, String keytype) throws InvalidKeySpecException { |
|
53 |
char[] passwd = keySpec.getPassword(); |
|
54 |
if (passwd == null) { |
|
55 |
// Should allow an empty password. |
|
56 |
passwd = new char[0]; |
|
57 |
} |
|
11835
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
10336
diff
changeset
|
58 |
// Accept "\0" to signify "zero-length password with no terminator". |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
10336
diff
changeset
|
59 |
if (!(passwd.length == 1 && passwd[0] == 0)) { |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
10336
diff
changeset
|
60 |
for (int i=0; i<passwd.length; i++) { |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
10336
diff
changeset
|
61 |
if ((passwd[i] < '\u0020') || (passwd[i] > '\u007E')) { |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
10336
diff
changeset
|
62 |
throw new InvalidKeySpecException("Password is not ASCII"); |
c9e7cfc908b3
6879539: enable empty password support for pkcs12 keystore
weijun
parents:
10336
diff
changeset
|
63 |
} |
2 | 64 |
} |
65 |
} |
|
66 |
this.key = new byte[passwd.length]; |
|
67 |
for (int i=0; i<passwd.length; i++) |
|
68 |
this.key[i] = (byte) (passwd[i] & 0x7f); |
|
69 |
java.util.Arrays.fill(passwd, ' '); |
|
70 |
type = keytype; |
|
71 |
} |
|
72 |
||
73 |
public byte[] getEncoded() { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
74 |
return this.key.clone(); |
2 | 75 |
} |
76 |
||
77 |
public String getAlgorithm() { |
|
78 |
return type; |
|
79 |
} |
|
80 |
||
81 |
public String getFormat() { |
|
82 |
return "RAW"; |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Calculates a hash code value for the object. |
|
87 |
* Objects that are equal will also have the same hashcode. |
|
88 |
*/ |
|
89 |
public int hashCode() { |
|
90 |
int retval = 0; |
|
91 |
for (int i = 1; i < this.key.length; i++) { |
|
92 |
retval += this.key[i] * i; |
|
93 |
} |
|
94 |
return(retval ^= getAlgorithm().toLowerCase().hashCode()); |
|
95 |
} |
|
96 |
||
97 |
public boolean equals(Object obj) { |
|
98 |
if (obj == this) |
|
99 |
return true; |
|
100 |
||
101 |
if (!(obj instanceof SecretKey)) |
|
102 |
return false; |
|
103 |
||
104 |
SecretKey that = (SecretKey)obj; |
|
105 |
||
106 |
if (!(that.getAlgorithm().equalsIgnoreCase(type))) |
|
107 |
return false; |
|
108 |
||
109 |
byte[] thatEncoded = that.getEncoded(); |
|
110 |
boolean ret = java.util.Arrays.equals(this.key, thatEncoded); |
|
111 |
java.util.Arrays.fill(thatEncoded, (byte)0x00); |
|
112 |
return ret; |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* readObject is called to restore the state of this key from |
|
117 |
* a stream. |
|
118 |
*/ |
|
119 |
private void readObject(java.io.ObjectInputStream s) |
|
120 |
throws java.io.IOException, ClassNotFoundException |
|
121 |
{ |
|
122 |
s.defaultReadObject(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
123 |
key = key.clone(); |
2 | 124 |
} |
125 |
||
126 |
||
127 |
/** |
|
128 |
* Replace the PBE key to be serialized. |
|
129 |
* |
|
130 |
* @return the standard KeyRep object to be serialized |
|
131 |
* |
|
132 |
* @throws java.io.ObjectStreamException if a new object representing |
|
133 |
* this PBE key could not be created |
|
134 |
*/ |
|
135 |
private Object writeReplace() throws java.io.ObjectStreamException { |
|
136 |
return new KeyRep(KeyRep.Type.SECRET, |
|
137 |
getAlgorithm(), |
|
138 |
getFormat(), |
|
139 |
getEncoded()); |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Ensures that the password bytes of this key are |
|
144 |
* set to zero when there are no more references to it. |
|
145 |
*/ |
|
146 |
protected void finalize() throws Throwable { |
|
147 |
try { |
|
148 |
if (this.key != null) { |
|
149 |
java.util.Arrays.fill(this.key, (byte)0x00); |
|
150 |
this.key = null; |
|
151 |
} |
|
152 |
} finally { |
|
153 |
super.finalize(); |
|
154 |
} |
|
155 |
} |
|
156 |
} |