001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.mscapi;
027:
028: /**
029: * The handle for an RSA or DSA key using the Microsoft Crypto API.
030: *
031: * @see DSAPrivateKey
032: * @see RSAPrivateKey
033: * @see RSAPublicKey
034: *
035: * @since 1.6
036: * @author Stanley Man-Kit Ho
037: */
038: abstract class Key implements java.security.Key {
039:
040: // Native handle
041: protected long hCryptProv = 0;
042: protected long hCryptKey = 0;
043:
044: // Key length
045: protected int keyLength = 0;
046:
047: /**
048: * Construct a Key object.
049: */
050: protected Key(long hCryptProv, long hCryptKey, int keyLength) {
051: this .hCryptProv = hCryptProv;
052: this .hCryptKey = hCryptKey;
053: this .keyLength = keyLength;
054: }
055:
056: /**
057: * Finalization method
058: */
059: protected void finalize() throws Throwable {
060: try {
061: synchronized (this ) {
062: cleanUp(hCryptProv, hCryptKey);
063: hCryptProv = 0;
064: hCryptKey = 0;
065: }
066:
067: } finally {
068: super .finalize();
069: }
070: }
071:
072: /**
073: * Native method to cleanup the key handle.
074: */
075: private native static void cleanUp(long hCryptProv, long hCryptKey);
076:
077: /**
078: * Return bit length of the key.
079: */
080: public int bitLength() {
081: return keyLength;
082: }
083:
084: /**
085: * Return native HCRYPTKEY handle.
086: */
087: public long getHCryptKey() {
088: return hCryptKey;
089: }
090:
091: /**
092: * Return native HCRYPTPROV handle.
093: */
094: public long getHCryptProvider() {
095: return hCryptProv;
096: }
097:
098: /**
099: * Returns the standard algorithm name for this key. For
100: * example, "DSA" would indicate that this key is a DSA key.
101: * See Appendix A in the <a href=
102: * "../../../guide/security/CryptoSpec.html#AppA">
103: * Java Cryptography Architecture API Specification & Reference </a>
104: * for information about standard algorithm names.
105: *
106: * @return the name of the algorithm associated with this key.
107: */
108: public abstract String getAlgorithm();
109:
110: /**
111: * Returns the name of the primary encoding format of this key,
112: * or null if this key does not support encoding.
113: * The primary encoding format is
114: * named in terms of the appropriate ASN.1 data format, if an
115: * ASN.1 specification for this key exists.
116: * For example, the name of the ASN.1 data format for public
117: * keys is <I>SubjectPublicKeyInfo</I>, as
118: * defined by the X.509 standard; in this case, the returned format is
119: * <code>"X.509"</code>. Similarly,
120: * the name of the ASN.1 data format for private keys is
121: * <I>PrivateKeyInfo</I>,
122: * as defined by the PKCS #8 standard; in this case, the returned format is
123: * <code>"PKCS#8"</code>.
124: *
125: * @return the primary encoding format of the key.
126: */
127: public String getFormat() {
128: return null;
129: }
130:
131: /**
132: * Returns the key in its primary encoding format, or null
133: * if this key does not support encoding.
134: *
135: * @return the encoded key, or null if the key does not support
136: * encoding.
137: */
138: public byte[] getEncoded() {
139: return null;
140: }
141:
142: protected native static String getContainerName(long hCryptProv);
143:
144: protected native static String getKeyType(long hCryptKey);
145: }
|