Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs11/P11ECUtil.java
38919 views
/*1* Copyright (c) 2015, 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.pkcs11;2627import java.io.IOException;28import java.math.BigInteger;29import java.security.*;30import java.security.interfaces.*;31import java.security.spec.*;3233import sun.security.ec.ECPublicKeyImpl;34import sun.security.ec.ECPrivateKeyImpl;35import sun.security.x509.X509Key;3637final class P11ECUtil {3839static ECPublicKey decodeX509ECPublicKey(byte[] encoded)40throws InvalidKeySpecException {41X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);4243return (ECPublicKey)ECGeneratePublic(keySpec);44}4546static byte[] x509EncodeECPublicKey(ECPoint w,47ECParameterSpec params) throws InvalidKeySpecException {48ECPublicKeySpec keySpec = new ECPublicKeySpec(w, params);49X509Key key = (X509Key)ECGeneratePublic(keySpec);5051return key.getEncoded();52}5354static ECPrivateKey decodePKCS8ECPrivateKey(byte[] encoded)55throws InvalidKeySpecException {56PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);5758return (ECPrivateKey)ECGeneratePrivate(keySpec);59}6061static ECPrivateKey generateECPrivateKey(BigInteger s,62ECParameterSpec params) throws InvalidKeySpecException {63ECPrivateKeySpec keySpec = new ECPrivateKeySpec(s, params);6465return (ECPrivateKey)ECGeneratePrivate(keySpec);66}6768private static PublicKey ECGeneratePublic(KeySpec keySpec)69throws InvalidKeySpecException {70try {71if (keySpec instanceof X509EncodedKeySpec) {72X509EncodedKeySpec x509Spec = (X509EncodedKeySpec)keySpec;73return new ECPublicKeyImpl(x509Spec.getEncoded());74} else if (keySpec instanceof ECPublicKeySpec) {75ECPublicKeySpec ecSpec = (ECPublicKeySpec)keySpec;76return new ECPublicKeyImpl(77ecSpec.getW(),78ecSpec.getParams()79);80} else {81throw new InvalidKeySpecException("Only ECPublicKeySpec "82+ "and X509EncodedKeySpec supported for EC public keys");83}84} catch (InvalidKeySpecException e) {85throw e;86} catch (GeneralSecurityException e) {87throw new InvalidKeySpecException(e);88}89}9091private static PrivateKey ECGeneratePrivate(KeySpec keySpec)92throws InvalidKeySpecException {93try {94if (keySpec instanceof PKCS8EncodedKeySpec) {95PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec)keySpec;96return new ECPrivateKeyImpl(pkcsSpec.getEncoded());97} else if (keySpec instanceof ECPrivateKeySpec) {98ECPrivateKeySpec ecSpec = (ECPrivateKeySpec)keySpec;99return new ECPrivateKeyImpl(ecSpec.getS(), ecSpec.getParams());100} else {101throw new InvalidKeySpecException("Only ECPrivateKeySpec "102+ "and PKCS8EncodedKeySpec supported for EC private keys");103}104} catch (InvalidKeySpecException e) {105throw e;106} catch (GeneralSecurityException e) {107throw new InvalidKeySpecException(e);108}109}110111private P11ECUtil() {}112113}114115116