Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/rsa/RSAPublicKeyImpl.java
38830 views
1
/*
2
* Copyright (c) 2003, 2020, 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.rsa;
27
28
import java.io.IOException;
29
import java.math.BigInteger;
30
31
import java.security.*;
32
import java.security.spec.*;
33
import java.security.interfaces.*;
34
35
import sun.security.util.*;
36
import sun.security.x509.X509Key;
37
import sun.security.x509.AlgorithmId;
38
39
import static sun.security.rsa.RSAUtil.KeyType;
40
41
/**
42
* RSA public key implementation for "RSA", "RSASSA-PSS" algorithms.
43
*
44
* Note: RSA keys must be at least 512 bits long
45
*
46
* @see RSAPrivateCrtKeyImpl
47
* @see RSAPrivateKeyImpl
48
* @see RSAKeyFactory
49
*
50
* @since 1.5
51
* @author Andreas Sterbenz
52
*/
53
public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey {
54
55
private static final long serialVersionUID = 2644735423591199609L;
56
private static final BigInteger THREE = BigInteger.valueOf(3);
57
58
private BigInteger n; // modulus
59
private BigInteger e; // public exponent
60
61
// optional parameters associated with this RSA key
62
// specified in the encoding of its AlgorithmId
63
// must be null for "RSA" keys.
64
private AlgorithmParameterSpec keyParams;
65
66
/**
67
* Generate a new RSAPublicKey from the specified encoding.
68
* Used by SunPKCS11 provider.
69
*/
70
public static RSAPublicKey newKey(byte[] encoded)
71
throws InvalidKeyException {
72
return new RSAPublicKeyImpl(encoded);
73
}
74
75
/**
76
* Generate a new RSAPublicKey from the specified type and components.
77
* Used by SunPKCS11 provider.
78
*/
79
public static RSAPublicKey newKey(KeyType type,
80
AlgorithmParameterSpec params, BigInteger n, BigInteger e)
81
throws InvalidKeyException {
82
AlgorithmId rsaId = RSAUtil.createAlgorithmId(type, params);
83
return new RSAPublicKeyImpl(rsaId, n, e);
84
}
85
86
/**
87
* Construct a RSA key from AlgorithmId and its components. Used by
88
* RSAKeyFactory and RSAKeyPairGenerator.
89
*/
90
RSAPublicKeyImpl(AlgorithmId rsaId, BigInteger n, BigInteger e)
91
throws InvalidKeyException {
92
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
93
checkExponentRange(n, e);
94
95
this.n = n;
96
this.e = e;
97
this.keyParams = RSAUtil.getParamSpec(rsaId);
98
99
// generate the encoding
100
algid = rsaId;
101
try {
102
DerOutputStream out = new DerOutputStream();
103
out.putInteger(n);
104
out.putInteger(e);
105
byte[] keyArray =
106
new DerValue(DerValue.tag_Sequence,
107
out.toByteArray()).toByteArray();
108
setKey(new BitArray(keyArray.length*8, keyArray));
109
} catch (IOException exc) {
110
// should never occur
111
throw new InvalidKeyException(exc);
112
}
113
}
114
115
/**
116
* Construct a key from its encoding. Used by RSAKeyFactory.
117
*/
118
RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException {
119
if (encoded == null || encoded.length == 0) {
120
throw new InvalidKeyException("Missing key encoding");
121
}
122
decode(encoded); // this sets n and e value
123
RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
124
checkExponentRange(n, e);
125
126
try {
127
// this will check the validity of params
128
this.keyParams = RSAUtil.getParamSpec(algid);
129
} catch (ProviderException e) {
130
throw new InvalidKeyException(e);
131
}
132
}
133
134
// pkg private utility method for checking RSA modulus and public exponent
135
static void checkExponentRange(BigInteger mod, BigInteger exp)
136
throws InvalidKeyException {
137
// the exponent should be smaller than the modulus
138
if (exp.compareTo(mod) >= 0) {
139
throw new InvalidKeyException("exponent is larger than modulus");
140
}
141
142
// the exponent should be at least 3
143
if (exp.compareTo(THREE) < 0) {
144
throw new InvalidKeyException("exponent is smaller than 3");
145
}
146
}
147
148
// see JCA doc
149
@Override
150
public String getAlgorithm() {
151
return algid.getName();
152
}
153
154
// see JCA doc
155
@Override
156
public BigInteger getModulus() {
157
return n;
158
}
159
160
// see JCA doc
161
@Override
162
public BigInteger getPublicExponent() {
163
return e;
164
}
165
166
// see JCA doc
167
@Override
168
public AlgorithmParameterSpec getParams() {
169
return keyParams;
170
}
171
172
/**
173
* Parse the key. Called by X509Key.
174
*/
175
protected void parseKeyBits() throws InvalidKeyException {
176
try {
177
DerInputStream in = new DerInputStream(getKey().toByteArray());
178
DerValue derValue = in.getDerValue();
179
if (derValue.tag != DerValue.tag_Sequence) {
180
throw new IOException("Not a SEQUENCE");
181
}
182
DerInputStream data = derValue.data;
183
n = data.getPositiveBigInteger();
184
e = data.getPositiveBigInteger();
185
if (derValue.data.available() != 0) {
186
throw new IOException("Extra data available");
187
}
188
} catch (IOException e) {
189
throw new InvalidKeyException("Invalid RSA public key", e);
190
}
191
}
192
193
// return a string representation of this key for debugging
194
@Override
195
public String toString() {
196
return "Sun " + getAlgorithm() + " public key, " + n.bitLength()
197
+ " bits" + "\n params: " + keyParams + "\n modulus: " + n
198
+ "\n public exponent: " + e;
199
}
200
201
protected Object writeReplace() throws java.io.ObjectStreamException {
202
return new KeyRep(KeyRep.Type.PUBLIC,
203
getAlgorithm(),
204
getFormat(),
205
getEncoded());
206
}
207
}
208
209