Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/DSAPrivateKey.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.AlgIdDSA;38import sun.security.pkcs.PKCS8Key;39import sun.security.util.Debug;40import sun.security.util.DerValue;41import sun.security.util.DerInputStream;42import sun.security.util.DerOutputStream;4344/**45* A PKCS#8 private key for the Digital Signature Algorithm.46*47* @author Benjamin Renaud48*49*50* @see DSAPublicKey51* @see AlgIdDSA52* @see DSA53*/5455public final class DSAPrivateKey extends PKCS8Key56implements java.security.interfaces.DSAPrivateKey, Serializable {5758/** use serialVersionUID from JDK 1.1. for interoperability */59private static final long serialVersionUID = -3244453684193605938L;6061/* the private key */62private BigInteger x;6364/*65* Keep this constructor for backwards compatibility with JDK1.1.66*/67public DSAPrivateKey() {68}6970/**71* Make a DSA private key out of a private key and three parameters.72*/73public DSAPrivateKey(BigInteger x, BigInteger p,74BigInteger q, BigInteger g)75throws InvalidKeyException {76this.x = x;77algid = new AlgIdDSA(p, q, g);7879try {80key = new DerValue(DerValue.tag_Integer,81x.toByteArray()).toByteArray();82encode();83} catch (IOException e) {84InvalidKeyException ike = new InvalidKeyException(85"could not DER encode x: " + e.getMessage());86ike.initCause(e);87throw ike;88}89}9091/**92* Make a DSA private key from its DER encoding (PKCS #8).93*/94public DSAPrivateKey(byte[] encoded) throws InvalidKeyException {95clearOldKey();96decode(encoded);97}9899/**100* Returns the DSA parameters associated with this key, or null if the101* parameters could not be parsed.102*/103public DSAParams getParams() {104try {105if (algid instanceof DSAParams) {106return (DSAParams)algid;107} else {108DSAParameterSpec paramSpec;109AlgorithmParameters algParams = algid.getParameters();110if (algParams == null) {111return null;112}113paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);114return (DSAParams)paramSpec;115}116} catch (InvalidParameterSpecException e) {117return null;118}119}120121/**122* Get the raw private key, x, without the parameters.123*124* @see getParameters125*/126public BigInteger getX() {127return x;128}129130private void clearOldKey() {131int i;132if (this.encodedKey != null) {133for (i = 0; i < this.encodedKey.length; i++) {134this.encodedKey[i] = (byte)0x00;135}136}137if (this.key != null) {138for (i = 0; i < this.key.length; i++) {139this.key[i] = (byte)0x00;140}141}142}143144protected void parseKeyBits() throws InvalidKeyException {145try {146DerInputStream in = new DerInputStream(key);147x = in.getBigInteger();148} catch (IOException e) {149InvalidKeyException ike = new InvalidKeyException(e.getMessage());150ike.initCause(e);151throw ike;152}153}154}155156157