Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/DSAPublicKey.java
38830 views
/*1* Copyright (c) 1996, 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.provider;2627import java.util.*;28import java.io.*;29import java.math.BigInteger;30import java.security.InvalidKeyException;31import java.security.ProviderException;32import java.security.AlgorithmParameters;33import java.security.spec.DSAParameterSpec;34import java.security.spec.InvalidParameterSpecException;35import java.security.interfaces.DSAParams;3637import sun.security.x509.X509Key;38import sun.security.x509.AlgIdDSA;39import sun.security.util.BitArray;40import sun.security.util.Debug;41import sun.security.util.DerValue;42import sun.security.util.DerInputStream;43import sun.security.util.DerOutputStream;4445/**46* An X.509 public key for the Digital Signature Algorithm.47*48* @author Benjamin Renaud49*50*51* @see DSAPrivateKey52* @see AlgIdDSA53* @see DSA54*/5556public class DSAPublicKey extends X509Key57implements java.security.interfaces.DSAPublicKey, Serializable {5859/** use serialVersionUID from JDK 1.1. for interoperability */60private static final long serialVersionUID = -2994193307391104133L;6162/* the public key */63private BigInteger y;6465/*66* Keep this constructor for backwards compatibility with JDK1.1.67*/68public DSAPublicKey() {69}7071/**72* Make a DSA public key out of a public key and three parameters.73* The p, q, and g parameters may be null, but if so, parameters will need74* to be supplied from some other source before this key can be used in75* cryptographic operations. PKIX RFC2459bis explicitly allows DSA public76* keys without parameters, where the parameters are provided in the77* issuer's DSA public key.78*79* @param y the actual key bits80* @param p DSA parameter p, may be null if all of p, q, and g are null.81* @param q DSA parameter q, may be null if all of p, q, and g are null.82* @param g DSA parameter g, may be null if all of p, q, and g are null.83*/84public DSAPublicKey(BigInteger y, BigInteger p, BigInteger q,85BigInteger g)86throws InvalidKeyException {87this.y = y;88algid = new AlgIdDSA(p, q, g);8990try {91byte[] keyArray = new DerValue(DerValue.tag_Integer,92y.toByteArray()).toByteArray();93setKey(new BitArray(keyArray.length*8, keyArray));94encode();95} catch (IOException e) {96throw new InvalidKeyException("could not DER encode y: " +97e.getMessage());98}99}100101/**102* Make a DSA public key from its DER encoding (X.509).103*/104public DSAPublicKey(byte[] encoded) throws InvalidKeyException {105decode(encoded);106}107108/**109* Returns the DSA parameters associated with this key, or null if the110* parameters could not be parsed.111*/112public DSAParams getParams() {113try {114if (algid instanceof DSAParams) {115return (DSAParams)algid;116} else {117DSAParameterSpec paramSpec;118AlgorithmParameters algParams = algid.getParameters();119if (algParams == null) {120return null;121}122paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);123return (DSAParams)paramSpec;124}125} catch (InvalidParameterSpecException e) {126return null;127}128}129130/**131* Get the raw public value, y, without the parameters.132*133* @see getParameters134*/135public BigInteger getY() {136return y;137}138139public String toString() {140return "Sun DSA Public Key\n Parameters:" + algid141+ "\n y:\n" + Debug.toHexString(y) + "\n";142}143144protected void parseKeyBits() throws InvalidKeyException {145try {146DerInputStream in = new DerInputStream(getKey().toByteArray());147y = in.getBigInteger();148} catch (IOException e) {149throw new InvalidKeyException("Invalid key: y value\n" +150e.getMessage());151}152}153}154155156