Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/security/mscapi/CPublicKey.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 java.math.BigInteger;28import java.security.AlgorithmParameters;29import java.security.KeyException;30import java.security.KeyFactory;31import java.security.KeyRep;32import java.security.ProviderException;33import java.security.PublicKey;34import java.security.interfaces.ECPublicKey;35import java.security.interfaces.RSAPublicKey;36import java.security.spec.ECParameterSpec;37import java.security.spec.ECPoint;38import java.security.spec.ECPublicKeySpec;39import java.util.Arrays;4041import sun.security.rsa.RSAUtil.KeyType;42import sun.security.rsa.RSAPublicKeyImpl;43import sun.security.util.ECKeySizeParameterSpec;4445/**46* The handle for an RSA public key using the Microsoft Crypto API.47*48* @since 1.649*/50public abstract class CPublicKey extends CKey implements PublicKey {5152private static final long serialVersionUID = -2289561342425825391L;5354protected byte[] encoding = null;5556public static class CECPublicKey extends CPublicKey implements ECPublicKey {5758private ECPoint w = null;59private static final long serialVersionUID = 12L;6061CECPublicKey(NativeHandles handles, int keyLength) {62super("EC", handles, keyLength);63}6465@Override66public ECPoint getW() {67if (w == null) {68// See CKey::generateECBlob.69try {70byte[] blob = getPublicKeyBlob(71handles.hCryptProv, handles.hCryptKey);72int len = blob[8] & 0xff;73byte[] x = Arrays.copyOfRange(blob, 8, 8 + len);74byte[] y = Arrays.copyOfRange(blob, 8 + len, 8 + len + len);75w = new ECPoint(new BigInteger(1, x), new BigInteger(1, y));76} catch (KeyException e) {77throw new ProviderException(e);78}79}80return w;81}8283@Override84public byte[] getEncoded() {85if (encoding == null) {86try {87encoding = KeyFactory.getInstance("EC").generatePublic(88new ECPublicKeySpec(getW(), getParams()))89.getEncoded();90} catch (Exception e) {91// ignore92}93}94return encoding;95}9697@Override98public ECParameterSpec getParams() {99try {100AlgorithmParameters ap = AlgorithmParameters.getInstance("EC");101ap.init(new ECKeySizeParameterSpec(keyLength));102return ap.getParameterSpec(ECParameterSpec.class);103} catch (Exception e) {104throw new ProviderException(e);105}106}107108public String toString() {109StringBuffer sb = new StringBuffer();110sb.append(algorithm + "PublicKey [size=").append(keyLength)111.append("]\n ECPoint: ").append(getW())112.append("\n params: ").append(getParams());113return sb.toString();114}115}116117public static class CRSAPublicKey extends CPublicKey implements RSAPublicKey {118119private BigInteger modulus = null;120private BigInteger exponent = null;121private static final long serialVersionUID = 12L;122123CRSAPublicKey(NativeHandles handles, int keyLength) {124super("RSA", handles, keyLength);125}126127public String toString() {128StringBuffer sb = new StringBuffer();129sb.append(algorithm + "PublicKey [size=").append(keyLength)130.append(" bits, type=");131if (handles.hCryptKey != 0) {132sb.append(getKeyType(handles.hCryptKey))133.append(", container=").append(getContainerName(handles.hCryptProv));134} else {135sb.append("CNG");136}137sb.append("]\n modulus: ").append(getModulus())138.append("\n public exponent: ").append(getPublicExponent());139return sb.toString();140}141142@Override143public BigInteger getPublicExponent() {144if (exponent == null) {145try {146byte[] publicKeyBlob = getPublicKeyBlob(147handles.hCryptProv, handles.hCryptKey);148exponent = new BigInteger(1, getExponent(publicKeyBlob));149} catch (KeyException e) {150throw new ProviderException(e);151}152}153return exponent;154}155156@Override157public BigInteger getModulus() {158if (modulus == null) {159try {160byte[] publicKeyBlob = getPublicKeyBlob(161handles.hCryptProv, handles.hCryptKey);162modulus = new BigInteger(1, getModulus(publicKeyBlob));163} catch (KeyException e) {164throw new ProviderException(e);165}166}167return modulus;168}169170@Override171public byte[] getEncoded() {172if (encoding == null) {173try {174encoding = RSAPublicKeyImpl.newKey(KeyType.RSA, null,175getModulus(), getPublicExponent()).getEncoded();176} catch (KeyException e) {177// ignore178}179}180return encoding;181}182183private native byte[] getExponent(byte[] keyBlob) throws KeyException;184185private native byte[] getModulus(byte[] keyBlob) throws KeyException;186}187188// Called by native code inside security.cpp189static CPublicKey of(190String alg, long hCryptProv, long hCryptKey, int keyLength) {191return of(alg, new NativeHandles(hCryptProv, hCryptKey), keyLength);192}193194public static CPublicKey of(195String alg, NativeHandles handles, int keyLength) {196switch (alg) {197case "RSA":198return new CRSAPublicKey(handles, keyLength);199case "EC":200return new CECPublicKey(handles, keyLength);201default:202throw new AssertionError("Unsupported algorithm: " + alg);203}204}205206protected CPublicKey(207String alg, NativeHandles handles, int keyLength) {208super(alg, handles, keyLength);209}210211@Override212public String getFormat() {213return "X.509";214}215216protected Object writeReplace() throws java.io.ObjectStreamException {217return new KeyRep(KeyRep.Type.PUBLIC,218getAlgorithm(),219getFormat(),220getEncoded());221}222223// Returns the CAPI or CNG representation of the key.224native byte[] getPublicKeyBlob(long hCryptProv, long hCryptKey)225throws KeyException;226}227228229