Path: blob/master/src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/CKey.java
41159 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.mscapi;2627import sun.security.util.KeyUtil;28import sun.security.util.Length;2930import java.math.BigInteger;31import java.security.Key;32import java.security.interfaces.ECPrivateKey;33import java.security.interfaces.ECPublicKey;3435/**36* The handle for a key using the Microsoft Crypto API.37*38* @see CPrivateKey39* @see CPublicKey40*41* @since 1.642* @author Stanley Man-Kit Ho43*/44abstract class CKey implements Key, Length {45private static final long serialVersionUID = -1088859394025049194L;4647static class NativeHandles {4849long hCryptProv = 0;50long hCryptKey = 0;5152public NativeHandles(long hCryptProv, long hCryptKey) {53this.hCryptProv = hCryptProv;54this.hCryptKey = hCryptKey;55}5657@SuppressWarnings("deprecation")58protected void finalize() throws Throwable {59try {60synchronized(this) {61cleanUp(hCryptProv, hCryptKey);62hCryptProv = 0;63hCryptKey = 0;64}65} finally {66super.finalize();67}68}69}7071protected final NativeHandles handles;7273protected final int keyLength;7475protected final String algorithm;7677protected CKey(String algorithm, NativeHandles handles, int keyLength) {78this.algorithm = algorithm;79this.handles = handles;80this.keyLength = keyLength;81}8283// Native method to cleanup the key handle.84private native static void cleanUp(long hCryptProv, long hCryptKey);8586@Override87public int length() {88return keyLength;89}9091public long getHCryptKey() {92return handles.hCryptKey;93}9495public long getHCryptProvider() {96return handles.hCryptProv;97}9899public String getAlgorithm() {100return algorithm;101}102103protected native static String getContainerName(long hCryptProv);104105protected native static String getKeyType(long hCryptKey);106107// This java method generates EC BLOBs for public key or private key.108// See https://docs.microsoft.com/en-us/windows/desktop/api/bcrypt/ns-bcrypt-_bcrypt_ecckey_blob109static byte[] generateECBlob(Key k) {110111int keyBitLength = KeyUtil.getKeySize(k);112int keyLen = (keyBitLength + 7) / 8;113boolean isPrivate = k instanceof ECPrivateKey;114115byte[] keyBlob = new byte[8 + keyLen * (isPrivate ? 3 : 2)];116keyBlob[0] = 'E';117keyBlob[1] = 'C';118keyBlob[2] = 'S';119if (isPrivate) {120keyBlob[3] = (byte) (keyBitLength == 256 ? '2'121: (keyBitLength == 384 ? '4' : '6'));122} else {123keyBlob[3] = (byte) (keyBitLength == 256 ? '1'124: (keyBitLength == 384 ? '3' : '5'));125}126BigInteger x;127BigInteger y;128// Fill the array in reverse order (s -> y -> x -> len) in case129// one BigInteger encoding has an extra 0 at the beginning130if (isPrivate) {131// We can keep X and Y zero and it still works132ECPrivateKey prk = (ECPrivateKey)k;133BigInteger s = prk.getS();134byte[] bs = s.toByteArray();135System.arraycopy(136bs, 0,137keyBlob, 8 + keyLen + keyLen + keyLen - bs.length,138bs.length);139} else {140ECPublicKey puk = (ECPublicKey)k;141x = puk.getW().getAffineX();142y = puk.getW().getAffineY();143byte[] by = y.toByteArray();144System.arraycopy(by, 0, keyBlob, 8 + keyLen + keyLen - by.length,145by.length);146byte[] bx = x.toByteArray();147System.arraycopy(bx, 0, keyBlob, 8 + keyLen - bx.length, bx.length);148}149keyBlob[4] = (byte) keyLen;150keyBlob[5] = keyBlob[6] = keyBlob[7] = 0;151return keyBlob;152}153}154155156