Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/security/mscapi/CKey.java
32288 views
/*1* Copyright (c) 2005, 2020, 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}6566} finally {67super.finalize();68}69}70}7172protected final NativeHandles handles;7374protected final int keyLength;7576protected final String algorithm;7778protected CKey(String algorithm, NativeHandles handles, int keyLength) {79this.algorithm = algorithm;80this.handles = handles;81this.keyLength = keyLength;82}8384// Native method to cleanup the key handle.85private native static void cleanUp(long hCryptProv, long hCryptKey);8687@Override88public int length() {89return keyLength;90}9192public long getHCryptKey() {93return handles.hCryptKey;94}9596public long getHCryptProvider() {97return handles.hCryptProv;98}99100public String getAlgorithm() {101return algorithm;102}103104protected native static String getContainerName(long hCryptProv);105106protected native static String getKeyType(long hCryptKey);107108// This java method generates EC BLOBs for public key or private key.109// See https://docs.microsoft.com/en-us/windows/desktop/api/bcrypt/ns-bcrypt-_bcrypt_ecckey_blob110static byte[] generateECBlob(Key k) {111112int keyBitLength = KeyUtil.getKeySize(k);113int keyLen = (keyBitLength + 7) / 8;114boolean isPrivate = k instanceof ECPrivateKey;115116byte[] keyBlob = new byte[8 + keyLen * (isPrivate ? 3 : 2)];117keyBlob[0] = 'E';118keyBlob[1] = 'C';119keyBlob[2] = 'S';120if (isPrivate) {121keyBlob[3] = (byte) (keyBitLength == 256 ? '2'122: (keyBitLength == 384 ? '4' : '6'));123} else {124keyBlob[3] = (byte) (keyBitLength == 256 ? '1'125: (keyBitLength == 384 ? '3' : '5'));126}127BigInteger x;128BigInteger y;129// Fill the array in reverse order (s -> y -> x -> len) in case130// one BigInteger encoding has an extra 0 at the beginning131if (isPrivate) {132// We can keep X and Y zero and it still works133ECPrivateKey prk = (ECPrivateKey)k;134BigInteger s = prk.getS();135byte[] bs = s.toByteArray();136System.arraycopy(137bs, 0,138keyBlob, 8 + keyLen + keyLen + keyLen - bs.length,139bs.length);140} else {141ECPublicKey puk = (ECPublicKey)k;142x = puk.getW().getAffineX();143y = puk.getW().getAffineY();144byte[] by = y.toByteArray();145System.arraycopy(by, 0, keyBlob, 8 + keyLen + keyLen - by.length,146by.length);147byte[] bx = x.toByteArray();148System.arraycopy(bx, 0, keyBlob, 8 + keyLen - bx.length, bx.length);149}150keyBlob[4] = (byte) keyLen;151keyBlob[5] = keyBlob[6] = keyBlob[7] = 0;152return keyBlob;153}154}155156157