Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/security/provider/DSAPrivateKey.java
41159 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
24
*/
25
26
package sun.security.provider;
27
28
import java.io.*;
29
import java.math.BigInteger;
30
import java.security.InvalidKeyException;
31
import java.security.AlgorithmParameters;
32
import java.security.spec.DSAParameterSpec;
33
import java.security.spec.InvalidParameterSpecException;
34
import java.security.interfaces.DSAParams;
35
import java.util.Arrays;
36
37
import sun.security.x509.AlgIdDSA;
38
import sun.security.pkcs.PKCS8Key;
39
import sun.security.util.DerValue;
40
import sun.security.util.DerInputStream;
41
42
/**
43
* A PKCS#8 private key for the Digital Signature Algorithm.
44
*
45
* @author Benjamin Renaud
46
*
47
*
48
* @see DSAPublicKey
49
* @see AlgIdDSA
50
* @see DSA
51
*/
52
53
public final class DSAPrivateKey extends PKCS8Key
54
implements java.security.interfaces.DSAPrivateKey, Serializable {
55
56
/** use serialVersionUID from JDK 1.1. for interoperability */
57
@java.io.Serial
58
private static final long serialVersionUID = -3244453684193605938L;
59
60
/* the private key */
61
private BigInteger x;
62
63
/**
64
* Make a DSA private key out of a private key and three parameters.
65
*/
66
public DSAPrivateKey(BigInteger x, BigInteger p,
67
BigInteger q, BigInteger g) {
68
this.x = x;
69
algid = new AlgIdDSA(p, q, g);
70
71
try {
72
byte[] xbytes = x.toByteArray();
73
DerValue val = new DerValue(DerValue.tag_Integer, xbytes);
74
key = val.toByteArray();
75
val.clear();
76
Arrays.fill(xbytes, (byte)0);
77
} catch (IOException e) {
78
throw new AssertionError("Should not happen", e);
79
}
80
}
81
82
/**
83
* Make a DSA private key from its DER encoding (PKCS #8).
84
*/
85
public DSAPrivateKey(byte[] encoded) throws InvalidKeyException {
86
super(encoded);
87
try {
88
DerInputStream in = new DerInputStream(key);
89
x = in.getBigInteger();
90
} catch (IOException e) {
91
throw new InvalidKeyException(e.getMessage(), e);
92
}
93
}
94
95
/**
96
* Returns the DSA parameters associated with this key, or null if the
97
* parameters could not be parsed.
98
*/
99
public DSAParams getParams() {
100
try {
101
if (algid instanceof DSAParams) {
102
return (DSAParams)algid;
103
} else {
104
DSAParameterSpec paramSpec;
105
AlgorithmParameters algParams = algid.getParameters();
106
if (algParams == null) {
107
return null;
108
}
109
paramSpec = algParams.getParameterSpec(DSAParameterSpec.class);
110
return paramSpec;
111
}
112
} catch (InvalidParameterSpecException e) {
113
return null;
114
}
115
}
116
117
/**
118
* Get the raw private key, x, without the parameters.
119
*/
120
public BigInteger getX() {
121
return x;
122
}
123
}
124
125