1 /* 2 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.security.ec; 27 28 import java.io.IOException; 29 30 import java.security.*; 31 import java.security.interfaces.*; 32 import java.security.spec.*; 33 34 import sun.security.util.*; 35 import sun.security.x509.*; 36 37 /** 38 * Key implementation for EC public keys. 39 * 40 * @since 1.6 41 * @author Andreas Sterbenz 42 */ 43 public final class ECPublicKeyImpl extends X509Key implements ECPublicKey { 44 45 private static final long serialVersionUID = -2462037275160462289L; 46 47 private ECPoint w; 48 private ECParameterSpec params; 49 50 /** 51 * Construct a key from its components. Used by the 52 * ECKeyFactory and SunPKCS11. 53 */ 54 public ECPublicKeyImpl(ECPoint w, ECParameterSpec params) 55 throws InvalidKeyException { 56 this.w = w; 57 this.params = params; 58 // generate the encoding 59 algid = new AlgorithmId 60 (AlgorithmId.EC_oid, ECParameters.getAlgorithmParameters(params)); 61 key = ECParameters.encodePoint(w, params.getCurve()); 62 } 63 64 /** 65 * Construct a key from its encoding. Used by RSAKeyFactory. 66 */ 67 public ECPublicKeyImpl(byte[] encoded) throws InvalidKeyException { 68 decode(encoded); 69 } 70 71 // see JCA doc 72 public String getAlgorithm() { 73 return "EC"; 74 } 75 76 // see JCA doc 77 public ECPoint getW() { 78 return w; 79 } 80 81 // see JCA doc 82 public ECParameterSpec getParams() { 83 return params; 84 } 85 86 // Internal API to get the encoded point. Currently used by SunPKCS11. 87 // This may change/go away depending on what we do with the public API. 88 public byte[] getEncodedPublicValue() { 89 return key.clone(); 90 } 91 92 /** 93 * Parse the key. Called by X509Key. 94 */ 95 protected void parseKeyBits() throws InvalidKeyException { 96 try { 97 AlgorithmParameters algParams = this.algid.getParameters(); 98 params = algParams.getParameterSpec(ECParameterSpec.class); 99 w = ECParameters.decodePoint(key, params.getCurve()); 100 } catch (IOException e) { 101 throw new InvalidKeyException("Invalid EC key", e); 102 } catch (InvalidParameterSpecException e) { 103 throw new InvalidKeyException("Invalid EC key", e); 104 } 105 } 106 107 // return a string representation of this key for debugging 108 public String toString() { 109 return "Sun EC public key, " + params.getCurve().getField().getFieldSize() 110 + " bits\n public x coord: " + w.getAffineX() 111 + "\n public y coord: " + w.getAffineY() 112 + "\n parameters: " + params; 113 } 114 115 protected Object writeReplace() throws java.io.ObjectStreamException { 116 return new KeyRep(KeyRep.Type.PUBLIC, 117 getAlgorithm(), 118 getFormat(), 119 getEncoded()); 120 } 121 } 122