Skip to content

Commit dc6ec2a

Browse files
committed
8284105: Update security libraries to use sealed classes
Reviewed-by: darcy, weijun, xuelei
1 parent 470a668 commit dc6ec2a

File tree

23 files changed

+101
-89
lines changed

23 files changed

+101
-89
lines changed

make/jdk/src/classes/build/tools/intpoly/FieldGen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -614,7 +614,7 @@ private String generate(FieldParams params) throws IOException {
614614
}
615615
result.appendLine("import java.math.BigInteger;");
616616

617-
result.appendLine("public class " + params.getClassName()
617+
result.appendLine("public final class " + params.getClassName()
618618
+ " extends " + this.parentName + " {");
619619
result.incrIndent();
620620

src/java.base/share/classes/sun/security/provider/JavaKeyStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -54,7 +54,7 @@
5454
* @since 1.2
5555
*/
5656

57-
public abstract class JavaKeyStore extends KeyStoreSpi {
57+
public abstract sealed class JavaKeyStore extends KeyStoreSpi {
5858

5959
// regular JKS
6060
public static final class JKS extends JavaKeyStore {

src/java.base/share/classes/sun/security/provider/certpath/Builder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -50,7 +50,7 @@
5050
* @author Yassir Elley
5151
*/
5252

53-
public abstract class Builder {
53+
abstract class Builder {
5454

5555
private static final Debug debug = Debug.getInstance("certpath");
5656
private Set<String> matchingPolicies;

src/java.base/share/classes/sun/security/provider/certpath/ForwardBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -59,7 +59,7 @@
5959
* @author Yassir Elley
6060
* @author Sean Mullan
6161
*/
62-
class ForwardBuilder extends Builder {
62+
final class ForwardBuilder extends Builder {
6363

6464
private static final Debug debug = Debug.getInstance("certpath");
6565
private final Set<X509Certificate> trustedCerts;

src/java.base/share/classes/sun/security/provider/certpath/SunCertPathBuilderResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@
4747
//@@@ Note: this class is not in public API and access to adjacency list is
4848
//@@@ intended for debugging/replay of Sun PKIX CertPathBuilder implementation.
4949

50-
public class SunCertPathBuilderResult extends PKIXCertPathBuilderResult {
50+
public final class SunCertPathBuilderResult extends PKIXCertPathBuilderResult {
5151

5252
private static final Debug debug = Debug.getInstance("certpath");
5353

src/java.base/share/classes/sun/security/provider/certpath/Vertex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -46,7 +46,7 @@
4646
* @author Sean Mullan
4747
* @since 1.4
4848
*/
49-
public class Vertex {
49+
final class Vertex {
5050

5151
private static final Debug debug = Debug.getInstance("certpath");
5252
private X509Certificate cert;

src/java.base/share/classes/sun/security/rsa/RSAKeyPairGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@
4747
* @since 1.5
4848
* @author Andreas Sterbenz
4949
*/
50-
public abstract class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
50+
abstract class RSAKeyPairGenerator extends KeyPairGeneratorSpi {
5151

5252
private static final BigInteger SQRT_2048;
5353
private static final BigInteger SQRT_3072;

src/java.base/share/classes/sun/security/rsa/RSASignature.java

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@
4747
* @since 1.5
4848
* @author Andreas Sterbenz
4949
*/
50-
public abstract class RSASignature extends SignatureSpi {
50+
abstract class RSASignature extends SignatureSpi {
5151

5252
// we sign an ASN.1 SEQUENCE of AlgorithmId and digest
5353
// it has the form 30:xx:30:xx:[digestOID]:05:00:04:xx:[digest]
@@ -188,7 +188,7 @@ protected byte[] engineSign() throws SignatureException {
188188
}
189189
byte[] digest = getDigestValue();
190190
try {
191-
byte[] encoded = encodeSignature(digestOID, digest);
191+
byte[] encoded = RSAUtil.encodeSignature(digestOID, digest);
192192
byte[] padded = padding.pad(encoded);
193193
byte[] encrypted = RSACore.rsa(padded, privateKey, true);
194194
return encrypted;
@@ -215,7 +215,7 @@ protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
215215
byte[] digest = getDigestValue();
216216
byte[] decrypted = RSACore.rsa(sigBytes, publicKey);
217217
byte[] unpadded = padding.unpad(decrypted);
218-
byte[] decodedDigest = decodeSignature(digestOID, unpadded);
218+
byte[] decodedDigest = RSAUtil.decodeSignature(digestOID, unpadded);
219219
return MessageDigest.isEqual(digest, decodedDigest);
220220
} catch (javax.crypto.BadPaddingException e) {
221221
// occurs if the app has used the wrong RSA public key
@@ -230,44 +230,6 @@ protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
230230
}
231231
}
232232

233-
/**
234-
* Encode the digest, return the to-be-signed data.
235-
* Also used by the PKCS#11 provider.
236-
*/
237-
public static byte[] encodeSignature(ObjectIdentifier oid, byte[] digest)
238-
throws IOException {
239-
DerOutputStream out = new DerOutputStream();
240-
new AlgorithmId(oid).encode(out);
241-
out.putOctetString(digest);
242-
DerValue result =
243-
new DerValue(DerValue.tag_Sequence, out.toByteArray());
244-
return result.toByteArray();
245-
}
246-
247-
/**
248-
* Decode the signature data. Verify that the object identifier matches
249-
* and return the message digest.
250-
*/
251-
public static byte[] decodeSignature(ObjectIdentifier oid, byte[] sig)
252-
throws IOException {
253-
// Enforce strict DER checking for signatures
254-
DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
255-
DerValue[] values = in.getSequence(2);
256-
if ((values.length != 2) || (in.available() != 0)) {
257-
throw new IOException("SEQUENCE length error");
258-
}
259-
AlgorithmId algId = AlgorithmId.parse(values[0]);
260-
if (algId.getOID().equals(oid) == false) {
261-
throw new IOException("ObjectIdentifier mismatch: "
262-
+ algId.getOID());
263-
}
264-
if (algId.getEncodedParams() != null) {
265-
throw new IOException("Unexpected AlgorithmId parameters");
266-
}
267-
byte[] digest = values[1].getOctetString();
268-
return digest;
269-
}
270-
271233
// set parameter, not supported. See JCA doc
272234
@Deprecated
273235
@Override

src/java.base/share/classes/sun/security/rsa/RSAUtil.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,12 @@
2525

2626
package sun.security.rsa;
2727

28+
import java.io.IOException;
2829
import java.security.*;
2930
import java.security.spec.*;
31+
import sun.security.util.DerInputStream;
32+
import sun.security.util.DerOutputStream;
33+
import sun.security.util.DerValue;
3034
import sun.security.util.ObjectIdentifier;
3135
import sun.security.x509.AlgorithmId;
3236

@@ -161,4 +165,42 @@ public static Object[] getTypeAndParamSpec(AlgorithmId algid)
161165
result[1] = getParamSpec(algid.getParameters());
162166
return result;
163167
}
168+
169+
/**
170+
* Encode the digest, return the to-be-signed data.
171+
* Also used by the PKCS#11 provider.
172+
*/
173+
public static byte[] encodeSignature(ObjectIdentifier oid, byte[] digest)
174+
throws IOException {
175+
DerOutputStream out = new DerOutputStream();
176+
new AlgorithmId(oid).encode(out);
177+
out.putOctetString(digest);
178+
DerValue result =
179+
new DerValue(DerValue.tag_Sequence, out.toByteArray());
180+
return result.toByteArray();
181+
}
182+
183+
/**
184+
* Decode the signature data. Verify that the object identifier matches
185+
* and return the message digest.
186+
*/
187+
public static byte[] decodeSignature(ObjectIdentifier oid, byte[] sig)
188+
throws IOException {
189+
// Enforce strict DER checking for signatures
190+
DerInputStream in = new DerInputStream(sig, 0, sig.length, false);
191+
DerValue[] values = in.getSequence(2);
192+
if ((values.length != 2) || (in.available() != 0)) {
193+
throw new IOException("SEQUENCE length error");
194+
}
195+
AlgorithmId algId = AlgorithmId.parse(values[0]);
196+
if (algId.getOID().equals(oid) == false) {
197+
throw new IOException("ObjectIdentifier mismatch: "
198+
+ algId.getOID());
199+
}
200+
if (algId.getEncodedParams() != null) {
201+
throw new IOException("Unexpected AlgorithmId parameters");
202+
}
203+
byte[] digest = values[1].getOctetString();
204+
return digest;
205+
}
164206
}

src/java.base/share/classes/sun/security/timestamp/TSResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -99,7 +99,7 @@
9999
* @see Timestamper
100100
*/
101101

102-
public class TSResponse {
102+
public final class TSResponse {
103103

104104
// Status codes (from RFC 3161)
105105

0 commit comments

Comments
 (0)