author | weijun |
Sat, 27 Apr 2019 18:21:57 +0800 | |
changeset 54639 | 2cac7d48db4c |
parent 53007 | e2798bf6318a |
child 54827 | 01fa7f06f806 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54639 | 2 |
* Copyright (c) 2005, 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 sun.security.mscapi; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
53007 | 29 |
import java.security.AlgorithmParameters; |
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
30 |
import java.security.KeyException; |
53007 | 31 |
import java.security.KeyFactory; |
2 | 32 |
import java.security.KeyRep; |
9508
310b4f6c8e61
6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents:
9505
diff
changeset
|
33 |
import java.security.ProviderException; |
53006 | 34 |
import java.security.PublicKey; |
53007 | 35 |
import java.security.interfaces.ECPublicKey; |
53006 | 36 |
import java.security.interfaces.RSAPublicKey; |
53007 | 37 |
import java.security.spec.ECParameterSpec; |
38 |
import java.security.spec.ECPoint; |
|
39 |
import java.security.spec.ECPublicKeySpec; |
|
40 |
import java.util.Arrays; |
|
2 | 41 |
|
50204
3195a713e24d
8146293: Add support for RSASSA-PSS Signature algorithm
valeriep
parents:
47216
diff
changeset
|
42 |
import sun.security.rsa.RSAUtil.KeyType; |
2 | 43 |
import sun.security.rsa.RSAPublicKeyImpl; |
53007 | 44 |
import sun.security.util.ECKeySizeParameterSpec; |
2 | 45 |
|
46 |
/** |
|
47 |
* The handle for an RSA public key using the Microsoft Crypto API. |
|
48 |
* |
|
49 |
* @since 1.6 |
|
50 |
*/ |
|
53006 | 51 |
public abstract class CPublicKey extends CKey implements PublicKey { |
52 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
53 |
private static final long serialVersionUID = -2289561342425825391L; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9508
diff
changeset
|
54 |
|
53006 | 55 |
protected byte[] encoding = null; |
56 |
||
53007 | 57 |
public static class CECPublicKey extends CPublicKey implements ECPublicKey { |
58 |
||
59 |
private ECPoint w = null; |
|
60 |
private static final long serialVersionUID = 12L; |
|
61 |
||
54639 | 62 |
CECPublicKey(NativeHandles handles, int keyLength) { |
63 |
super("EC", handles, keyLength); |
|
53007 | 64 |
} |
65 |
||
66 |
@Override |
|
67 |
public ECPoint getW() { |
|
68 |
if (w == null) { |
|
69 |
// See CKey::generateECBlob. |
|
70 |
try { |
|
71 |
byte[] blob = getPublicKeyBlob( |
|
72 |
handles.hCryptProv, handles.hCryptKey); |
|
73 |
int len = blob[8] & 0xff; |
|
74 |
byte[] x = Arrays.copyOfRange(blob, 8, 8 + len); |
|
75 |
byte[] y = Arrays.copyOfRange(blob, 8 + len, 8 + len + len); |
|
76 |
w = new ECPoint(new BigInteger(1, x), new BigInteger(1, y)); |
|
77 |
} catch (KeyException e) { |
|
78 |
throw new ProviderException(e); |
|
79 |
} |
|
80 |
} |
|
81 |
return w; |
|
82 |
} |
|
83 |
||
84 |
@Override |
|
85 |
public byte[] getEncoded() { |
|
86 |
if (encoding == null) { |
|
87 |
try { |
|
88 |
encoding = KeyFactory.getInstance("EC").generatePublic( |
|
89 |
new ECPublicKeySpec(getW(), getParams())) |
|
90 |
.getEncoded(); |
|
91 |
} catch (Exception e) { |
|
92 |
// ignore |
|
93 |
} |
|
94 |
} |
|
95 |
return encoding; |
|
96 |
} |
|
97 |
||
98 |
@Override |
|
99 |
public ECParameterSpec getParams() { |
|
100 |
try { |
|
101 |
AlgorithmParameters ap = AlgorithmParameters.getInstance("EC"); |
|
102 |
ap.init(new ECKeySizeParameterSpec(keyLength)); |
|
103 |
return ap.getParameterSpec(ECParameterSpec.class); |
|
104 |
} catch (Exception e) { |
|
105 |
throw new ProviderException(e); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
public String toString() { |
|
110 |
StringBuffer sb = new StringBuffer(); |
|
111 |
sb.append(algorithm + "PublicKey [size=").append(keyLength) |
|
112 |
.append("]\n ECPoint: ").append(getW()) |
|
113 |
.append("\n params: ").append(getParams()); |
|
114 |
return sb.toString(); |
|
115 |
} |
|
116 |
} |
|
117 |
||
53006 | 118 |
public static class CRSAPublicKey extends CPublicKey implements RSAPublicKey { |
119 |
||
120 |
private BigInteger modulus = null; |
|
121 |
private BigInteger exponent = null; |
|
122 |
private static final long serialVersionUID = 12L; |
|
2 | 123 |
|
54639 | 124 |
CRSAPublicKey(NativeHandles handles, int keyLength) { |
125 |
super("RSA", handles, keyLength); |
|
53006 | 126 |
} |
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
127 |
|
53006 | 128 |
public String toString() { |
129 |
StringBuffer sb = new StringBuffer(); |
|
130 |
sb.append(algorithm + "PublicKey [size=").append(keyLength) |
|
131 |
.append(" bits, type=").append(getKeyType(handles.hCryptKey)) |
|
132 |
.append(", container=").append(getContainerName(handles.hCryptProv)) |
|
133 |
.append("]\n modulus: ").append(getModulus()) |
|
134 |
.append("\n public exponent: ").append(getPublicExponent()); |
|
135 |
return sb.toString(); |
|
136 |
} |
|
2 | 137 |
|
53006 | 138 |
@Override |
139 |
public BigInteger getPublicExponent() { |
|
140 |
if (exponent == null) { |
|
141 |
try { |
|
53007 | 142 |
byte[] publicKeyBlob = getPublicKeyBlob( |
143 |
handles.hCryptProv, handles.hCryptKey); |
|
53006 | 144 |
exponent = new BigInteger(1, getExponent(publicKeyBlob)); |
145 |
} catch (KeyException e) { |
|
146 |
throw new ProviderException(e); |
|
147 |
} |
|
148 |
} |
|
149 |
return exponent; |
|
150 |
} |
|
2 | 151 |
|
53006 | 152 |
@Override |
153 |
public BigInteger getModulus() { |
|
154 |
if (modulus == null) { |
|
155 |
try { |
|
53007 | 156 |
byte[] publicKeyBlob = getPublicKeyBlob( |
157 |
handles.hCryptProv, handles.hCryptKey); |
|
53006 | 158 |
modulus = new BigInteger(1, getModulus(publicKeyBlob)); |
159 |
} catch (KeyException e) { |
|
160 |
throw new ProviderException(e); |
|
161 |
} |
|
162 |
} |
|
163 |
return modulus; |
|
164 |
} |
|
2 | 165 |
|
53006 | 166 |
@Override |
167 |
public byte[] getEncoded() { |
|
168 |
if (encoding == null) { |
|
169 |
try { |
|
170 |
encoding = RSAPublicKeyImpl.newKey(KeyType.RSA, null, |
|
171 |
getModulus(), getPublicExponent()).getEncoded(); |
|
172 |
} catch (KeyException e) { |
|
173 |
// ignore |
|
174 |
} |
|
175 |
} |
|
176 |
return encoding; |
|
177 |
} |
|
2 | 178 |
|
53006 | 179 |
private native byte[] getExponent(byte[] keyBlob) throws KeyException; |
180 |
||
181 |
private native byte[] getModulus(byte[] keyBlob) throws KeyException; |
|
2 | 182 |
} |
183 |
||
54639 | 184 |
// Called by native code inside security.cpp |
185 |
static CPublicKey of( |
|
186 |
String alg, long hCryptProv, long hCryptKey, int keyLength) { |
|
187 |
return of(alg, new NativeHandles(hCryptProv, hCryptKey), keyLength); |
|
188 |
} |
|
189 |
||
53007 | 190 |
public static CPublicKey of( |
54639 | 191 |
String alg, NativeHandles handles, int keyLength) { |
53006 | 192 |
switch (alg) { |
193 |
case "RSA": |
|
54639 | 194 |
return new CRSAPublicKey(handles, keyLength); |
53007 | 195 |
case "EC": |
54639 | 196 |
return new CECPublicKey(handles, keyLength); |
53006 | 197 |
default: |
198 |
throw new AssertionError("Unsupported algorithm: " + alg); |
|
2 | 199 |
} |
200 |
} |
|
201 |
||
53007 | 202 |
protected CPublicKey( |
54639 | 203 |
String alg, NativeHandles handles, int keyLength) { |
204 |
super(alg, handles, keyLength); |
|
2 | 205 |
} |
206 |
||
53006 | 207 |
@Override |
208 |
public String getFormat() { |
|
209 |
return "X.509"; |
|
2 | 210 |
} |
211 |
||
212 |
protected Object writeReplace() throws java.io.ObjectStreamException { |
|
213 |
return new KeyRep(KeyRep.Type.PUBLIC, |
|
214 |
getAlgorithm(), |
|
215 |
getFormat(), |
|
216 |
getEncoded()); |
|
217 |
} |
|
218 |
||
53007 | 219 |
// Returns the CAPI or CNG representation of the key. |
220 |
native byte[] getPublicKeyBlob(long hCryptProv, long hCryptKey) |
|
221 |
throws KeyException; |
|
2 | 222 |
} |