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/sun/security/pkcs11/P11ECUtil.java
38919 views
1
/*
2
* Copyright (c) 2015, 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 sun.security.pkcs11;
27
28
import java.io.IOException;
29
import java.math.BigInteger;
30
import java.security.*;
31
import java.security.interfaces.*;
32
import java.security.spec.*;
33
34
import sun.security.ec.ECPublicKeyImpl;
35
import sun.security.ec.ECPrivateKeyImpl;
36
import sun.security.x509.X509Key;
37
38
final class P11ECUtil {
39
40
static ECPublicKey decodeX509ECPublicKey(byte[] encoded)
41
throws InvalidKeySpecException {
42
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
43
44
return (ECPublicKey)ECGeneratePublic(keySpec);
45
}
46
47
static byte[] x509EncodeECPublicKey(ECPoint w,
48
ECParameterSpec params) throws InvalidKeySpecException {
49
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, params);
50
X509Key key = (X509Key)ECGeneratePublic(keySpec);
51
52
return key.getEncoded();
53
}
54
55
static ECPrivateKey decodePKCS8ECPrivateKey(byte[] encoded)
56
throws InvalidKeySpecException {
57
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
58
59
return (ECPrivateKey)ECGeneratePrivate(keySpec);
60
}
61
62
static ECPrivateKey generateECPrivateKey(BigInteger s,
63
ECParameterSpec params) throws InvalidKeySpecException {
64
ECPrivateKeySpec keySpec = new ECPrivateKeySpec(s, params);
65
66
return (ECPrivateKey)ECGeneratePrivate(keySpec);
67
}
68
69
private static PublicKey ECGeneratePublic(KeySpec keySpec)
70
throws InvalidKeySpecException {
71
try {
72
if (keySpec instanceof X509EncodedKeySpec) {
73
X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec;
74
return new ECPublicKeyImpl(x509Spec.getEncoded());
75
} else if (keySpec instanceof ECPublicKeySpec) {
76
ECPublicKeySpec ecSpec = (ECPublicKeySpec)keySpec;
77
return new ECPublicKeyImpl(
78
ecSpec.getW(),
79
ecSpec.getParams()
80
);
81
} else {
82
throw new InvalidKeySpecException("Only ECPublicKeySpec "
83
+ "and X509EncodedKeySpec supported for EC public keys");
84
}
85
} catch (InvalidKeySpecException e) {
86
throw e;
87
} catch (GeneralSecurityException e) {
88
throw new InvalidKeySpecException(e);
89
}
90
}
91
92
private static PrivateKey ECGeneratePrivate(KeySpec keySpec)
93
throws InvalidKeySpecException {
94
try {
95
if (keySpec instanceof PKCS8EncodedKeySpec) {
96
PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec;
97
return new ECPrivateKeyImpl(pkcsSpec.getEncoded());
98
} else if (keySpec instanceof ECPrivateKeySpec) {
99
ECPrivateKeySpec ecSpec = (ECPrivateKeySpec)keySpec;
100
return new ECPrivateKeyImpl(ecSpec.getS(), ecSpec.getParams());
101
} else {
102
throw new InvalidKeySpecException("Only ECPrivateKeySpec "
103
+ "and PKCS8EncodedKeySpec supported for EC private keys");
104
}
105
} catch (InvalidKeySpecException e) {
106
throw e;
107
} catch (GeneralSecurityException e) {
108
throw new InvalidKeySpecException(e);
109
}
110
}
111
112
private P11ECUtil() {}
113
114
}
115
116