Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/crypto/provider/CipherWithWrappingSpi.java
38922 views
1
/*
2
* Copyright (c) 1999, 2013, 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.security.Key;
29
import java.security.PublicKey;
30
import java.security.PrivateKey;
31
import java.security.KeyFactory;
32
import java.security.InvalidKeyException;
33
import java.security.NoSuchProviderException;
34
import java.security.NoSuchAlgorithmException;
35
import java.security.spec.PKCS8EncodedKeySpec;
36
import java.security.spec.X509EncodedKeySpec;
37
import java.security.spec.InvalidKeySpecException;
38
39
import javax.crypto.Cipher;
40
import javax.crypto.CipherSpi;
41
import javax.crypto.SecretKey;
42
import javax.crypto.IllegalBlockSizeException;
43
import javax.crypto.BadPaddingException;
44
import javax.crypto.spec.SecretKeySpec;
45
46
/**
47
* This class entends the javax.crypto.CipherSpi class with a concrete
48
* implementation of the methods for wrapping and unwrapping
49
* keys.
50
*
51
* @author Sharon Liu
52
*
53
*
54
* @see javax.crypto.CipherSpi
55
* @see BlowfishCipher
56
* @see DESCipher
57
* @see PBEWithMD5AndDESCipher
58
*/
59
60
public abstract class CipherWithWrappingSpi extends CipherSpi {
61
62
/**
63
* Wrap a key.
64
*
65
* @param key the key to be wrapped.
66
*
67
* @return the wrapped key.
68
*
69
* @exception IllegalBlockSizeException if this cipher is a block
70
* cipher, no padding has been requested, and the length of the
71
* encoding of the key to be wrapped is not a
72
* multiple of the block size.
73
*
74
* @exception InvalidKeyException if it is impossible or unsafe to
75
* wrap the key with this cipher (e.g., a hardware protected key is
76
* being passed to a software only cipher).
77
*/
78
protected final byte[] engineWrap(Key key)
79
throws IllegalBlockSizeException, InvalidKeyException
80
{
81
byte[] result = null;
82
83
try {
84
byte[] encodedKey = key.getEncoded();
85
if ((encodedKey == null) || (encodedKey.length == 0)) {
86
throw new InvalidKeyException("Cannot get an encoding of " +
87
"the key to be wrapped");
88
}
89
90
result = engineDoFinal(encodedKey, 0, encodedKey.length);
91
} catch (BadPaddingException e) {
92
// Should never happen
93
}
94
95
return result;
96
}
97
98
/**
99
* Unwrap a previously wrapped key.
100
*
101
* @param wrappedKey the key to be unwrapped.
102
*
103
* @param wrappedKeyAlgorithm the algorithm the wrapped key is for.
104
*
105
* @param wrappedKeyType the type of the wrapped key.
106
* This is one of <code>Cipher.SECRET_KEY</code>,
107
* <code>Cipher.PRIVATE_KEY</code>, or <code>Cipher.PUBLIC_KEY</code>.
108
*
109
* @return the unwrapped key.
110
*
111
* @exception InvalidKeyException if <code>wrappedKey</code> does not
112
* represent a wrapped key, or if the algorithm associated with the
113
* wrapped key is different from <code>wrappedKeyAlgorithm</code>
114
* and/or its key type is different from <code>wrappedKeyType</code>.
115
*
116
* @exception NoSuchAlgorithmException if no installed providers
117
* can create keys for the <code>wrappedKeyAlgorithm</code>.
118
*/
119
protected final Key engineUnwrap(byte[] wrappedKey,
120
String wrappedKeyAlgorithm,
121
int wrappedKeyType)
122
throws InvalidKeyException, NoSuchAlgorithmException
123
{
124
byte[] encodedKey;
125
Key result = null;
126
127
try {
128
encodedKey = engineDoFinal(wrappedKey, 0,
129
wrappedKey.length);
130
} catch (BadPaddingException ePadding) {
131
throw new InvalidKeyException();
132
} catch (IllegalBlockSizeException eBlockSize) {
133
throw new InvalidKeyException();
134
}
135
136
switch (wrappedKeyType) {
137
case Cipher.SECRET_KEY:
138
result = constructSecretKey(encodedKey,
139
wrappedKeyAlgorithm);
140
break;
141
case Cipher.PRIVATE_KEY:
142
result = constructPrivateKey(encodedKey,
143
wrappedKeyAlgorithm);
144
break;
145
case Cipher.PUBLIC_KEY:
146
result = constructPublicKey(encodedKey,
147
wrappedKeyAlgorithm);
148
break;
149
}
150
151
return result;
152
153
}
154
155
/**
156
* Construct a public key from its encoding.
157
*
158
* @param encodedKey the encoding of a public key.
159
*
160
* @param encodedKeyAlgorithm the algorithm the encodedKey is for.
161
*
162
* @return a public key constructed from the encodedKey.
163
*/
164
private final PublicKey constructPublicKey(byte[] encodedKey,
165
String encodedKeyAlgorithm)
166
throws InvalidKeyException, NoSuchAlgorithmException
167
{
168
PublicKey key = null;
169
170
try {
171
KeyFactory keyFactory =
172
KeyFactory.getInstance(encodedKeyAlgorithm,
173
SunJCE.getInstance());
174
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
175
key = keyFactory.generatePublic(keySpec);
176
} catch (NoSuchAlgorithmException nsae) {
177
// Try to see whether there is another
178
// provider which supports this algorithm
179
try {
180
KeyFactory keyFactory =
181
KeyFactory.getInstance(encodedKeyAlgorithm);
182
X509EncodedKeySpec keySpec =
183
new X509EncodedKeySpec(encodedKey);
184
key = keyFactory.generatePublic(keySpec);
185
} catch (NoSuchAlgorithmException nsae2) {
186
throw new NoSuchAlgorithmException("No installed providers " +
187
"can create keys for the " +
188
encodedKeyAlgorithm +
189
"algorithm");
190
} catch (InvalidKeySpecException ikse2) {
191
// Should never happen.
192
}
193
} catch (InvalidKeySpecException ikse) {
194
// Should never happen.
195
}
196
197
return key;
198
}
199
200
/**
201
* Construct a private key from its encoding.
202
*
203
* @param encodedKey the encoding of a private key.
204
*
205
* @param encodedKeyAlgorithm the algorithm the wrapped key is for.
206
*
207
* @return a private key constructed from the encodedKey.
208
*/
209
private final PrivateKey constructPrivateKey(byte[] encodedKey,
210
String encodedKeyAlgorithm)
211
throws InvalidKeyException, NoSuchAlgorithmException
212
{
213
PrivateKey key = null;
214
215
try {
216
KeyFactory keyFactory =
217
KeyFactory.getInstance(encodedKeyAlgorithm,
218
SunJCE.getInstance());
219
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
220
return keyFactory.generatePrivate(keySpec);
221
} catch (NoSuchAlgorithmException nsae) {
222
// Try to see whether there is another
223
// provider which supports this algorithm
224
try {
225
KeyFactory keyFactory =
226
KeyFactory.getInstance(encodedKeyAlgorithm);
227
PKCS8EncodedKeySpec keySpec =
228
new PKCS8EncodedKeySpec(encodedKey);
229
key = keyFactory.generatePrivate(keySpec);
230
} catch (NoSuchAlgorithmException nsae2) {
231
throw new NoSuchAlgorithmException("No installed providers " +
232
"can create keys for the " +
233
encodedKeyAlgorithm +
234
"algorithm");
235
} catch (InvalidKeySpecException ikse2) {
236
// Should never happen.
237
}
238
} catch (InvalidKeySpecException ikse) {
239
// Should never happen.
240
}
241
242
return key;
243
}
244
245
/**
246
* Construct a secret key from its encoding.
247
*
248
* @param encodedKey the encoding of a secret key.
249
*
250
* @param encodedKeyAlgorithm the algorithm the secret key is for.
251
*
252
* @return a secret key constructed from the encodedKey.
253
*/
254
private final SecretKey constructSecretKey(byte[] encodedKey,
255
String encodedKeyAlgorithm)
256
{
257
return (new SecretKeySpec(encodedKey, encodedKeyAlgorithm));
258
}
259
}
260
261