Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/ECPublicKeyImpl.java
41161 views
/*1* Copyright (c) 2006, 2013, 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.ec;2627import java.io.IOException;2829import java.security.*;30import java.security.interfaces.*;31import java.security.spec.*;3233import sun.security.util.ECParameters;34import sun.security.util.ECUtil;3536import sun.security.x509.*;3738/**39* Key implementation for EC public keys.40*41* @since 1.642* @author Andreas Sterbenz43*/44public final class ECPublicKeyImpl extends X509Key implements ECPublicKey {4546private static final long serialVersionUID = -2462037275160462289L;4748private ECPoint w;49private ECParameterSpec params;5051/**52* Construct a key from its components. Used by the53* ECKeyFactory.54*/55@SuppressWarnings("deprecation")56ECPublicKeyImpl(ECPoint w, ECParameterSpec params)57throws InvalidKeyException {58this.w = w;59this.params = params;60// generate the encoding61algid = new AlgorithmId62(AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params));63key = ECUtil.encodePoint(w, params.getCurve());64}6566/**67* Construct a key from its encoding.68*/69ECPublicKeyImpl(byte[] encoded) throws InvalidKeyException {70decode(encoded);71}7273// see JCA doc74public String getAlgorithm() {75return "EC";76}7778// see JCA doc79public ECPoint getW() {80return w;81}8283// see JCA doc84public ECParameterSpec getParams() {85return params;86}8788// Internal API to get the encoded point. Currently used by SunPKCS11.89// This may change/go away depending on what we do with the public API.90@SuppressWarnings("deprecation")91public byte[] getEncodedPublicValue() {92return key.clone();93}9495/**96* Parse the key. Called by X509Key.97*/98@SuppressWarnings("deprecation")99protected void parseKeyBits() throws InvalidKeyException {100AlgorithmParameters algParams = this.algid.getParameters();101if (algParams == null) {102throw new InvalidKeyException("EC domain parameters must be " +103"encoded in the algorithm identifier");104}105106try {107params = algParams.getParameterSpec(ECParameterSpec.class);108w = ECUtil.decodePoint(key, params.getCurve());109} catch (IOException e) {110throw new InvalidKeyException("Invalid EC key", e);111} catch (InvalidParameterSpecException e) {112throw new InvalidKeyException("Invalid EC key", e);113}114}115116// return a string representation of this key for debugging117public String toString() {118return "Sun EC public key, " + params.getCurve().getField().getFieldSize()119+ " bits\n public x coord: " + w.getAffineX()120+ "\n public y coord: " + w.getAffineY()121+ "\n parameters: " + params;122}123124protected Object writeReplace() throws java.io.ObjectStreamException {125return new KeyRep(KeyRep.Type.PUBLIC,126getAlgorithm(),127getFormat(),128getEncoded());129}130}131132133