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