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