author | igerasim |
Mon, 15 Aug 2016 23:45:32 +0300 | |
changeset 40389 | c6df8bba0b71 |
parent 25859 | 3317bb8137f4 |
child 44534 | a076dffbc2c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, 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 |
||
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
5506
diff
changeset
|
28 |
import sun.security.util.Length; |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
5506
diff
changeset
|
29 |
|
2 | 30 |
/** |
31 |
* The handle for an RSA or DSA key using the Microsoft Crypto API. |
|
32 |
* |
|
33 |
* @see RSAPrivateKey |
|
34 |
* @see RSAPublicKey |
|
35 |
* |
|
36 |
* @since 1.6 |
|
37 |
* @author Stanley Man-Kit Ho |
|
38 |
*/ |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
5506
diff
changeset
|
39 |
abstract class Key implements java.security.Key, Length |
2 | 40 |
{ |
19832
80fff95a633a
8024501: sun.security.mscapi.Key has no definition of serialVersionUID
xuelei
parents:
11521
diff
changeset
|
41 |
private static final long serialVersionUID = -1088859394025049194L; |
2 | 42 |
|
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
43 |
static class NativeHandles { |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
44 |
long hCryptProv = 0; |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
45 |
long hCryptKey = 0; |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
46 |
|
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
47 |
public NativeHandles(long hCryptProv, long hCryptKey) { |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
48 |
this.hCryptProv = hCryptProv; |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
49 |
this.hCryptKey = hCryptKey; |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
50 |
} |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
51 |
|
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
52 |
/** |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
53 |
* Finalization method |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
54 |
*/ |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
55 |
protected void finalize() throws Throwable |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
56 |
{ |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
57 |
try { |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
58 |
synchronized(this) |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
59 |
{ |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
60 |
cleanUp(hCryptProv, hCryptKey); |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
61 |
hCryptProv = 0; |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
62 |
hCryptKey = 0; |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
63 |
} |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
64 |
|
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
65 |
} finally { |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
66 |
super.finalize(); |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
67 |
} |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
68 |
} |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
69 |
} |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
70 |
|
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
71 |
protected NativeHandles handles; |
2 | 72 |
|
73 |
// Key length |
|
74 |
protected int keyLength = 0; |
|
75 |
||
76 |
/** |
|
77 |
* Construct a Key object. |
|
78 |
*/ |
|
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
79 |
protected Key(NativeHandles handles, int keyLength) |
2 | 80 |
{ |
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
81 |
this.handles = handles; |
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
82 |
this.keyLength = keyLength; |
2 | 83 |
} |
84 |
||
85 |
/** |
|
86 |
* Native method to cleanup the key handle. |
|
87 |
*/ |
|
88 |
private native static void cleanUp(long hCryptProv, long hCryptKey); |
|
89 |
||
90 |
/** |
|
91 |
* Return bit length of the key. |
|
92 |
*/ |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
5506
diff
changeset
|
93 |
@Override |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
5506
diff
changeset
|
94 |
public int length() |
2 | 95 |
{ |
96 |
return keyLength; |
|
97 |
} |
|
98 |
||
99 |
||
100 |
/** |
|
101 |
* Return native HCRYPTKEY handle. |
|
102 |
*/ |
|
103 |
public long getHCryptKey() |
|
104 |
{ |
|
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
105 |
return handles.hCryptKey; |
2 | 106 |
} |
107 |
||
108 |
/** |
|
109 |
* Return native HCRYPTPROV handle. |
|
110 |
*/ |
|
111 |
public long getHCryptProvider() |
|
112 |
{ |
|
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
113 |
return handles.hCryptProv; |
2 | 114 |
} |
115 |
||
116 |
/** |
|
117 |
* Returns the standard algorithm name for this key. For |
|
40389
c6df8bba0b71
8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
25859
diff
changeset
|
118 |
* example, "RSA" would indicate that this key is a RSA key. |
2 | 119 |
* See Appendix A in the <a href= |
120 |
* "../../../guide/security/CryptoSpec.html#AppA"> |
|
121 |
* Java Cryptography Architecture API Specification & Reference </a> |
|
122 |
* for information about standard algorithm names. |
|
123 |
* |
|
124 |
* @return the name of the algorithm associated with this key. |
|
125 |
*/ |
|
126 |
public abstract String getAlgorithm(); |
|
127 |
||
128 |
/** |
|
129 |
* Returns the name of the primary encoding format of this key, |
|
130 |
* or null if this key does not support encoding. |
|
131 |
* The primary encoding format is |
|
132 |
* named in terms of the appropriate ASN.1 data format, if an |
|
133 |
* ASN.1 specification for this key exists. |
|
134 |
* For example, the name of the ASN.1 data format for public |
|
135 |
* keys is <I>SubjectPublicKeyInfo</I>, as |
|
136 |
* defined by the X.509 standard; in this case, the returned format is |
|
137 |
* <code>"X.509"</code>. Similarly, |
|
138 |
* the name of the ASN.1 data format for private keys is |
|
139 |
* <I>PrivateKeyInfo</I>, |
|
140 |
* as defined by the PKCS #8 standard; in this case, the returned format is |
|
141 |
* <code>"PKCS#8"</code>. |
|
142 |
* |
|
143 |
* @return the primary encoding format of the key. |
|
144 |
*/ |
|
145 |
public String getFormat() |
|
146 |
{ |
|
147 |
return null; |
|
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Returns the key in its primary encoding format, or null |
|
152 |
* if this key does not support encoding. |
|
153 |
* |
|
154 |
* @return the encoded key, or null if the key does not support |
|
155 |
* encoding. |
|
156 |
*/ |
|
157 |
public byte[] getEncoded() |
|
158 |
{ |
|
159 |
return null; |
|
160 |
} |
|
161 |
||
162 |
protected native static String getContainerName(long hCryptProv); |
|
163 |
||
164 |
protected native static String getKeyType(long hCryptKey); |
|
165 |
} |