author | mbalao |
Tue, 12 Nov 2019 00:30:55 -0300 | |
changeset 59158 | 438337c846fb |
parent 57950 | 4612a3cfb927 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48560
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, 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 |
||
48560 | 28 |
import java.lang.ref.Reference; |
31695 | 29 |
import java.security.MessageDigest; |
2 | 30 |
import java.security.KeyRep; |
31 |
import java.security.InvalidKeyException; |
|
32 |
import javax.crypto.SecretKey; |
|
33 |
import javax.crypto.spec.DESKeySpec; |
|
34 |
||
46097
22316369c9b0
8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents:
44534
diff
changeset
|
35 |
import jdk.internal.ref.CleanerFactory; |
22316369c9b0
8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents:
44534
diff
changeset
|
36 |
|
2 | 37 |
/** |
38 |
* This class represents a DES key. |
|
39 |
* |
|
40 |
* @author Jan Luehe |
|
41 |
* |
|
42 |
*/ |
|
43 |
||
44 |
final class DESKey implements SecretKey { |
|
45 |
||
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48560
diff
changeset
|
46 |
@java.io.Serial |
2 | 47 |
static final long serialVersionUID = 7724971015953279128L; |
48 |
||
49 |
private byte[] key; |
|
50 |
||
51 |
/** |
|
52 |
* Uses the first 8 bytes of the given key as the DES key. |
|
53 |
* |
|
54 |
* @param key the buffer with the DES key bytes. |
|
55 |
* |
|
56 |
* @exception InvalidKeyException if less than 8 bytes are available for |
|
57 |
* the key. |
|
58 |
*/ |
|
59 |
DESKey(byte[] key) throws InvalidKeyException { |
|
60 |
this(key, 0); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Uses the first 8 bytes in <code>key</code>, beginning at |
|
65 |
* <code>offset</code>, as the DES key |
|
66 |
* |
|
67 |
* @param key the buffer with the DES key bytes. |
|
68 |
* @param offset the offset in <code>key</code>, where the DES key bytes |
|
69 |
* start. |
|
70 |
* |
|
71 |
* @exception InvalidKeyException if less than 8 bytes are available for |
|
72 |
* the key. |
|
73 |
*/ |
|
74 |
DESKey(byte[] key, int offset) throws InvalidKeyException { |
|
75 |
if (key == null || key.length - offset < DESKeySpec.DES_KEY_LEN) { |
|
76 |
throw new InvalidKeyException("Wrong key size"); |
|
77 |
} |
|
78 |
this.key = new byte[DESKeySpec.DES_KEY_LEN]; |
|
79 |
System.arraycopy(key, offset, this.key, 0, DESKeySpec.DES_KEY_LEN); |
|
80 |
DESKeyGenerator.setParityBit(this.key, 0); |
|
46097
22316369c9b0
8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents:
44534
diff
changeset
|
81 |
|
22316369c9b0
8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents:
44534
diff
changeset
|
82 |
// Use the cleaner to zero the key when no longer referenced |
22316369c9b0
8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents:
44534
diff
changeset
|
83 |
final byte[] k = this.key; |
22316369c9b0
8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents:
44534
diff
changeset
|
84 |
CleanerFactory.cleaner().register(this, |
22316369c9b0
8184744: Replace finalizer in crypto classes with Cleaner
rriggs
parents:
44534
diff
changeset
|
85 |
() -> java.util.Arrays.fill(k, (byte)0x00)); |
2 | 86 |
} |
87 |
||
88 |
public byte[] getEncoded() { |
|
89 |
// Return a copy of the key, rather than a reference, |
|
90 |
// so that the key data cannot be modified from outside |
|
48560 | 91 |
|
92 |
// The key is zeroized by finalize() |
|
93 |
// The reachability fence ensures finalize() isn't called early |
|
94 |
byte[] result = key.clone(); |
|
95 |
Reference.reachabilityFence(this); |
|
96 |
return result; |
|
2 | 97 |
} |
98 |
||
99 |
public String getAlgorithm() { |
|
100 |
return "DES"; |
|
101 |
} |
|
102 |
||
103 |
public String getFormat() { |
|
104 |
return "RAW"; |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Calculates a hash code value for the object. |
|
109 |
* Objects that are equal will also have the same hashcode. |
|
110 |
*/ |
|
111 |
public int hashCode() { |
|
112 |
int retval = 0; |
|
113 |
for (int i = 1; i < this.key.length; i++) { |
|
114 |
retval += this.key[i] * i; |
|
115 |
} |
|
116 |
return(retval ^= "des".hashCode()); |
|
117 |
} |
|
118 |
||
119 |
public boolean equals(Object obj) { |
|
120 |
if (this == obj) |
|
121 |
return true; |
|
122 |
||
123 |
if (!(obj instanceof SecretKey)) |
|
124 |
return false; |
|
125 |
||
126 |
String thatAlg = ((SecretKey)obj).getAlgorithm(); |
|
127 |
if (!(thatAlg.equalsIgnoreCase("DES"))) |
|
128 |
return false; |
|
129 |
||
130 |
byte[] thatKey = ((SecretKey)obj).getEncoded(); |
|
31695 | 131 |
boolean ret = MessageDigest.isEqual(this.key, thatKey); |
2 | 132 |
java.util.Arrays.fill(thatKey, (byte)0x00); |
133 |
return ret; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* readObject is called to restore the state of this key from |
|
138 |
* a stream. |
|
139 |
*/ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48560
diff
changeset
|
140 |
@java.io.Serial |
2 | 141 |
private void readObject(java.io.ObjectInputStream s) |
142 |
throws java.io.IOException, ClassNotFoundException |
|
143 |
{ |
|
144 |
s.defaultReadObject(); |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
145 |
key = key.clone(); |
2 | 146 |
} |
147 |
||
148 |
/** |
|
149 |
* Replace the DES key to be serialized. |
|
150 |
* |
|
151 |
* @return the standard KeyRep object to be serialized |
|
152 |
* |
|
153 |
* @throws java.io.ObjectStreamException if a new object representing |
|
154 |
* this DES key could not be created |
|
155 |
*/ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
48560
diff
changeset
|
156 |
@java.io.Serial |
2 | 157 |
private Object writeReplace() throws java.io.ObjectStreamException { |
158 |
return new KeyRep(KeyRep.Type.SECRET, |
|
159 |
getAlgorithm(), |
|
160 |
getFormat(), |
|
161 |
getEncoded()); |
|
162 |
} |
|
163 |
} |