blob: a6b544477a2a375a51daeb80268920a6d4cf0f71 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.security.provider;
27
28import java.util.*;
29import java.io.*;
30import java.math.BigInteger;
31import java.security.InvalidKeyException;
32import java.security.ProviderException;
33import java.security.AlgorithmParameters;
34import java.security.spec.DSAParameterSpec;
35import java.security.spec.InvalidParameterSpecException;
36import java.security.interfaces.DSAParams;
37
38import sun.security.x509.AlgIdDSA;
39import sun.security.pkcs.PKCS8Key;
40import sun.security.util.Debug;
41import sun.security.util.DerValue;
42import sun.security.util.DerInputStream;
43import sun.security.util.DerOutputStream;
44
45/**
46 * A PKCS#8 private key for the Digital Signature Algorithm.
47 *
48 * @author Benjamin Renaud
49 *
50 *
51 * @see DSAPublicKey
52 * @see AlgIdDSA
53 * @see DSA
54 */
55
56public final class DSAPrivateKey extends PKCS8Key
57implements java.security.interfaces.DSAPrivateKey, Serializable {
58
59 /** use serialVersionUID from JDK 1.1. for interoperability */
60 private static final long serialVersionUID = -3244453684193605938L;
61
62 /* the private key */
63 private BigInteger x;
64
65 /*
66 * Keep this constructor for backwards compatibility with JDK1.1.
67 */
68 public DSAPrivateKey() {
69 }
70
71 /**
72 * Make a DSA private key out of a private key and three parameters.
73 */
74 public DSAPrivateKey(BigInteger x, BigInteger p,
75 BigInteger q, BigInteger g)
76 throws InvalidKeyException {
77 this.x = x;
78 algid = new AlgIdDSA(p, q, g);
79
80 try {
81 key = new DerValue(DerValue.tag_Integer,
82 x.toByteArray()).toByteArray();
83 encode();
84 } catch (IOException e) {
85 InvalidKeyException ike = new InvalidKeyException(
86 "could not DER encode x: " + e.getMessage());
87 ike.initCause(e);
88 throw ike;
89 }
90 }
91
92 /**
93 * Make a DSA private key from its DER encoding (PKCS #8).
94 */
95 public DSAPrivateKey(byte[] encoded) throws InvalidKeyException {
96 clearOldKey();
97 decode(encoded);
98 }
99
100 /**
101 * Returns the DSA parameters associated with this key, or null if the
102 * parameters could not be parsed.
103 */
104 public DSAParams getParams() {
105 try {
106 if (algid instanceof DSAParams) {
107 return (DSAParams)algid;
108 } else {
109 DSAParameterSpec paramSpec;
110 AlgorithmParameters algParams = algid.getParameters();
111 if (algParams == null) {
112 return null;
113 }
114 paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
115 return (DSAParams)paramSpec;
116 }
117 } catch (InvalidParameterSpecException e) {
118 return null;
119 }
120 }
121
122 /**
123 * Get the raw private key, x, without the parameters.
124 *
125 * @see getParameters
126 */
127 public BigInteger getX() {
128 return x;
129 }
130
131 private void clearOldKey() {
132 int i;
133 if (this.encodedKey != null) {
134 for (i = 0; i < this.encodedKey.length; i++) {
135 this.encodedKey[i] = (byte)0x00;
136 }
137 }
138 if (this.key != null) {
139 for (i = 0; i < this.key.length; i++) {
140 this.key[i] = (byte)0x00;
141 }
142 }
143 }
144
145 public String toString() {
146 return "Sun DSA Private Key \nparameters:" + algid + "\nx: " +
147 Debug.toHexString(x) + "\n";
148 }
149
150 protected void parseKeyBits() throws InvalidKeyException {
151 try {
152 DerInputStream in = new DerInputStream(key);
153 x = in.getBigInteger();
154 } catch (IOException e) {
155 InvalidKeyException ike = new InvalidKeyException(e.getMessage());
156 ike.initCause(e);
157 throw ike;
158 }
159 }
160}