Path: blob/master/src/java.base/share/classes/sun/security/provider/DSAPublicKey.java
41159 views
/*1* Copyright (c) 1996, 2019, 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 */60@java.io.Serial61private static final long serialVersionUID = -2994193307391104133L;6263/* the public key */64private BigInteger y;6566/*67* Keep this constructor for backwards compatibility with JDK1.1.68*/69public DSAPublicKey() {70}7172/**73* Make a DSA public key out of a public key and three parameters.74* The p, q, and g parameters may be null, but if so, parameters will need75* to be supplied from some other source before this key can be used in76* cryptographic operations. PKIX RFC2459bis explicitly allows DSA public77* keys without parameters, where the parameters are provided in the78* issuer's DSA public key.79*80* @param y the actual key bits81* @param p DSA parameter p, may be null if all of p, q, and g are null.82* @param q DSA parameter q, may be null if all of p, q, and g are null.83* @param g DSA parameter g, may be null if all of p, q, and g are null.84*/85public DSAPublicKey(BigInteger y, BigInteger p, BigInteger q,86BigInteger g)87throws InvalidKeyException {88this.y = y;89algid = new AlgIdDSA(p, q, g);9091try {92byte[] keyArray = new DerValue(DerValue.tag_Integer,93y.toByteArray()).toByteArray();94setKey(new BitArray(keyArray.length*8, keyArray));95encode();96} catch (IOException e) {97throw new InvalidKeyException("could not DER encode y: " +98e.getMessage());99}100}101102/**103* Make a DSA public key from its DER encoding (X.509).104*/105public DSAPublicKey(byte[] encoded) throws InvalidKeyException {106decode(encoded);107}108109/**110* Returns the DSA parameters associated with this key, or null if the111* parameters could not be parsed.112*/113public DSAParams getParams() {114try {115if (algid instanceof DSAParams) {116return (DSAParams)algid;117} else {118DSAParameterSpec paramSpec;119AlgorithmParameters algParams = algid.getParameters();120if (algParams == null) {121return null;122}123paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);124return (DSAParams)paramSpec;125}126} catch (InvalidParameterSpecException e) {127return null;128}129}130131/**132* Get the raw public value, y, without the parameters.133*134* @see getParameters135*/136public BigInteger getY() {137return y;138}139140public String toString() {141return "Sun DSA Public Key\n Parameters:" + algid142+ "\n y:\n" + Debug.toHexString(y) + "\n";143}144145protected void parseKeyBits() throws InvalidKeyException {146try {147DerInputStream in = new DerInputStream(getKey().toByteArray());148y = in.getBigInteger();149} catch (IOException e) {150throw new InvalidKeyException("Invalid key: y value\n" +151e.getMessage());152}153}154}155156157