Path: blob/master/src/java.base/share/classes/sun/security/provider/DSAPrivateKey.java
41159 views
/*1* Copyright (c) 1996, 2021, 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.io.*;28import java.math.BigInteger;29import java.security.InvalidKeyException;30import java.security.AlgorithmParameters;31import java.security.spec.DSAParameterSpec;32import java.security.spec.InvalidParameterSpecException;33import java.security.interfaces.DSAParams;34import java.util.Arrays;3536import sun.security.x509.AlgIdDSA;37import sun.security.pkcs.PKCS8Key;38import sun.security.util.DerValue;39import sun.security.util.DerInputStream;4041/**42* A PKCS#8 private key for the Digital Signature Algorithm.43*44* @author Benjamin Renaud45*46*47* @see DSAPublicKey48* @see AlgIdDSA49* @see DSA50*/5152public final class DSAPrivateKey extends PKCS8Key53implements java.security.interfaces.DSAPrivateKey, Serializable {5455/** use serialVersionUID from JDK 1.1. for interoperability */56@java.io.Serial57private static final long serialVersionUID = -3244453684193605938L;5859/* the private key */60private BigInteger x;6162/**63* Make a DSA private key out of a private key and three parameters.64*/65public DSAPrivateKey(BigInteger x, BigInteger p,66BigInteger q, BigInteger g) {67this.x = x;68algid = new AlgIdDSA(p, q, g);6970try {71byte[] xbytes = x.toByteArray();72DerValue val = new DerValue(DerValue.tag_Integer, xbytes);73key = val.toByteArray();74val.clear();75Arrays.fill(xbytes, (byte)0);76} catch (IOException e) {77throw new AssertionError("Should not happen", e);78}79}8081/**82* Make a DSA private key from its DER encoding (PKCS #8).83*/84public DSAPrivateKey(byte[] encoded) throws InvalidKeyException {85super(encoded);86try {87DerInputStream in = new DerInputStream(key);88x = in.getBigInteger();89} catch (IOException e) {90throw new InvalidKeyException(e.getMessage(), e);91}92}9394/**95* Returns the DSA parameters associated with this key, or null if the96* parameters could not be parsed.97*/98public DSAParams getParams() {99try {100if (algid instanceof DSAParams) {101return (DSAParams)algid;102} else {103DSAParameterSpec paramSpec;104AlgorithmParameters algParams = algid.getParameters();105if (algParams == null) {106return null;107}108paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);109return paramSpec;110}111} catch (InvalidParameterSpecException e) {112return null;113}114}115116/**117* Get the raw private key, x, without the parameters.118*/119public BigInteger getX() {120return x;121}122}123124125