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