0N/A/*
2362N/A * Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.provider;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport java.math.BigInteger;
0N/Aimport java.security.InvalidKeyException;
0N/Aimport java.security.ProviderException;
0N/Aimport java.security.AlgorithmParameters;
0N/Aimport java.security.spec.DSAParameterSpec;
0N/Aimport java.security.spec.InvalidParameterSpecException;
0N/Aimport java.security.interfaces.DSAParams;
0N/A
0N/Aimport sun.security.x509.AlgIdDSA;
0N/Aimport sun.security.pkcs.PKCS8Key;
0N/Aimport sun.security.util.Debug;
0N/Aimport sun.security.util.DerValue;
0N/Aimport sun.security.util.DerInputStream;
0N/Aimport sun.security.util.DerOutputStream;
0N/A
0N/A/**
0N/A * A PKCS#8 private key for the Digital Signature Algorithm.
0N/A *
0N/A * @author Benjamin Renaud
0N/A *
0N/A *
0N/A * @see DSAPublicKey
0N/A * @see AlgIdDSA
0N/A * @see DSA
0N/A */
0N/A
0N/Apublic final class DSAPrivateKey extends PKCS8Key
0N/Aimplements java.security.interfaces.DSAPrivateKey, Serializable {
0N/A
0N/A /** use serialVersionUID from JDK 1.1. for interoperability */
0N/A private static final long serialVersionUID = -3244453684193605938L;
0N/A
0N/A /* the private key */
0N/A private BigInteger x;
0N/A
0N/A /*
0N/A * Keep this constructor for backwards compatibility with JDK1.1.
0N/A */
0N/A public DSAPrivateKey() {
0N/A }
0N/A
0N/A /**
0N/A * Make a DSA private key out of a private key and three parameters.
0N/A */
0N/A public DSAPrivateKey(BigInteger x, BigInteger p,
0N/A BigInteger q, BigInteger g)
0N/A throws InvalidKeyException {
0N/A this.x = x;
0N/A algid = new AlgIdDSA(p, q, g);
0N/A
0N/A try {
0N/A key = new DerValue(DerValue.tag_Integer,
0N/A x.toByteArray()).toByteArray();
0N/A encode();
0N/A } catch (IOException e) {
0N/A InvalidKeyException ike = new InvalidKeyException(
0N/A "could not DER encode x: " + e.getMessage());
0N/A ike.initCause(e);
0N/A throw ike;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Make a DSA private key from its DER encoding (PKCS #8).
0N/A */
0N/A public DSAPrivateKey(byte[] encoded) throws InvalidKeyException {
0N/A clearOldKey();
0N/A decode(encoded);
0N/A }
0N/A
0N/A /**
0N/A * Returns the DSA parameters associated with this key, or null if the
0N/A * parameters could not be parsed.
0N/A */
0N/A public DSAParams getParams() {
0N/A try {
0N/A if (algid instanceof DSAParams) {
0N/A return (DSAParams)algid;
0N/A } else {
0N/A DSAParameterSpec paramSpec;
0N/A AlgorithmParameters algParams = algid.getParameters();
0N/A if (algParams == null) {
0N/A return null;
0N/A }
0N/A paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
0N/A return (DSAParams)paramSpec;
0N/A }
0N/A } catch (InvalidParameterSpecException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the raw private key, x, without the parameters.
0N/A *
0N/A * @see getParameters
0N/A */
0N/A public BigInteger getX() {
0N/A return x;
0N/A }
0N/A
0N/A private void clearOldKey() {
0N/A int i;
0N/A if (this.encodedKey != null) {
0N/A for (i = 0; i < this.encodedKey.length; i++) {
0N/A this.encodedKey[i] = (byte)0x00;
0N/A }
0N/A }
0N/A if (this.key != null) {
0N/A for (i = 0; i < this.key.length; i++) {
0N/A this.key[i] = (byte)0x00;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A return "Sun DSA Private Key \nparameters:" + algid + "\nx: " +
0N/A Debug.toHexString(x) + "\n";
0N/A }
0N/A
0N/A protected void parseKeyBits() throws InvalidKeyException {
0N/A try {
0N/A DerInputStream in = new DerInputStream(key);
0N/A x = in.getBigInteger();
0N/A } catch (IOException e) {
0N/A InvalidKeyException ike = new InvalidKeyException(e.getMessage());
0N/A ike.initCause(e);
0N/A throw ike;
0N/A }
0N/A }
0N/A}