author | darcy |
Thu, 29 Aug 2019 10:52:21 -0700 | |
changeset 57950 | 4612a3cfb927 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.provider; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
import java.io.*; |
|
30 |
import java.math.BigInteger; |
|
31 |
import java.security.InvalidKeyException; |
|
32 |
import java.security.ProviderException; |
|
33 |
import java.security.AlgorithmParameters; |
|
34 |
import java.security.spec.DSAParameterSpec; |
|
35 |
import java.security.spec.InvalidParameterSpecException; |
|
36 |
import java.security.interfaces.DSAParams; |
|
37 |
||
38 |
import sun.security.x509.AlgIdDSA; |
|
39 |
import sun.security.pkcs.PKCS8Key; |
|
40 |
import sun.security.util.Debug; |
|
41 |
import sun.security.util.DerValue; |
|
42 |
import sun.security.util.DerInputStream; |
|
43 |
import 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 |
||
56 |
public final class DSAPrivateKey extends PKCS8Key |
|
57 |
implements java.security.interfaces.DSAPrivateKey, Serializable { |
|
58 |
||
59 |
/** use serialVersionUID from JDK 1.1. for interoperability */ |
|
57950
4612a3cfb927
8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents:
47216
diff
changeset
|
60 |
@java.io.Serial |
2 | 61 |
private static final long serialVersionUID = -3244453684193605938L; |
62 |
||
63 |
/* the private key */ |
|
64 |
private BigInteger x; |
|
65 |
||
66 |
/* |
|
67 |
* Keep this constructor for backwards compatibility with JDK1.1. |
|
68 |
*/ |
|
69 |
public DSAPrivateKey() { |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Make a DSA private key out of a private key and three parameters. |
|
74 |
*/ |
|
75 |
public DSAPrivateKey(BigInteger x, BigInteger p, |
|
76 |
BigInteger q, BigInteger g) |
|
77 |
throws InvalidKeyException { |
|
78 |
this.x = x; |
|
79 |
algid = new AlgIdDSA(p, q, g); |
|
80 |
||
81 |
try { |
|
82 |
key = new DerValue(DerValue.tag_Integer, |
|
83 |
x.toByteArray()).toByteArray(); |
|
84 |
encode(); |
|
85 |
} catch (IOException e) { |
|
86 |
InvalidKeyException ike = new InvalidKeyException( |
|
87 |
"could not DER encode x: " + e.getMessage()); |
|
88 |
ike.initCause(e); |
|
89 |
throw ike; |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Make a DSA private key from its DER encoding (PKCS #8). |
|
95 |
*/ |
|
96 |
public DSAPrivateKey(byte[] encoded) throws InvalidKeyException { |
|
97 |
clearOldKey(); |
|
98 |
decode(encoded); |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Returns the DSA parameters associated with this key, or null if the |
|
103 |
* parameters could not be parsed. |
|
104 |
*/ |
|
105 |
public DSAParams getParams() { |
|
106 |
try { |
|
107 |
if (algid instanceof DSAParams) { |
|
108 |
return (DSAParams)algid; |
|
109 |
} else { |
|
110 |
DSAParameterSpec paramSpec; |
|
111 |
AlgorithmParameters algParams = algid.getParameters(); |
|
112 |
if (algParams == null) { |
|
113 |
return null; |
|
114 |
} |
|
115 |
paramSpec = algParams.getParameterSpec(DSAParameterSpec.class); |
|
116 |
return (DSAParams)paramSpec; |
|
117 |
} |
|
118 |
} catch (InvalidParameterSpecException e) { |
|
119 |
return null; |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Get the raw private key, x, without the parameters. |
|
125 |
* |
|
126 |
* @see getParameters |
|
127 |
*/ |
|
128 |
public BigInteger getX() { |
|
129 |
return x; |
|
130 |
} |
|
131 |
||
132 |
private void clearOldKey() { |
|
133 |
int i; |
|
134 |
if (this.encodedKey != null) { |
|
135 |
for (i = 0; i < this.encodedKey.length; i++) { |
|
136 |
this.encodedKey[i] = (byte)0x00; |
|
137 |
} |
|
138 |
} |
|
139 |
if (this.key != null) { |
|
140 |
for (i = 0; i < this.key.length; i++) { |
|
141 |
this.key[i] = (byte)0x00; |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
protected void parseKeyBits() throws InvalidKeyException { |
|
147 |
try { |
|
148 |
DerInputStream in = new DerInputStream(key); |
|
149 |
x = in.getBigInteger(); |
|
150 |
} catch (IOException e) { |
|
151 |
InvalidKeyException ike = new InvalidKeyException(e.getMessage()); |
|
152 |
ike.initCause(e); |
|
153 |
throw ike; |
|
154 |
} |
|
155 |
} |
|
156 |
} |