Skip to content

Commit b5dda54

Browse files
Alexey BakhtinRealCLanger
authored andcommitted
8298310: Enhance TLS session negotiation
Reviewed-by: mbalao Backport-of: 9a14b363feaaa1a1831fcc8620d41b4db2e0110a
1 parent af5c303 commit b5dda54

File tree

6 files changed

+115
-106
lines changed

6 files changed

+115
-106
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2023, 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
@@ -87,7 +87,7 @@ public class AdjacencyList {
8787
// the actual set of steps the AdjacencyList represents
8888
private ArrayList<BuildStep> mStepList;
8989

90-
// the original list, just for the toString method
90+
// the original list
9191
private List<List<Vertex>> mOrigList;
9292

9393
/**
@@ -114,6 +114,13 @@ public Iterator<BuildStep> iterator() {
114114
return Collections.unmodifiableList(mStepList).iterator();
115115
}
116116

117+
/**
118+
* Returns the number of attempted paths (useful for debugging).
119+
*/
120+
public int numAttemptedPaths() {
121+
return mOrigList.size();
122+
}
123+
117124
/**
118125
* Recursive, private method which actually builds the step list from
119126
* the given adjacency list. <code>Follow</code> is the parent BuildStep

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2023, 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
@@ -412,8 +412,7 @@ Set<String> getMatchingPolicies() {
412412

413413
/**
414414
* Search the specified CertStores and add all certificates matching
415-
* selector to resultCerts. Self-signed certs are not useful here
416-
* and therefore ignored.
415+
* selector to resultCerts.
417416
*
418417
* If the targetCert criterion of the selector is set, only that cert
419418
* is examined and the CertStores are not searched.
@@ -432,8 +431,7 @@ boolean addMatchingCerts(X509CertSelector selector,
432431
X509Certificate targetCert = selector.getCertificate();
433432
if (targetCert != null) {
434433
// no need to search CertStores
435-
if (selector.match(targetCert) && !X509CertImpl.isSelfSigned
436-
(targetCert, buildParams.sigProvider())) {
434+
if (selector.match(targetCert)) {
437435
if (debug != null) {
438436
debug.println("Builder.addMatchingCerts: " +
439437
"adding target cert" +
@@ -452,11 +450,8 @@ boolean addMatchingCerts(X509CertSelector selector,
452450
Collection<? extends Certificate> certs =
453451
store.getCertificates(selector);
454452
for (Certificate cert : certs) {
455-
if (!X509CertImpl.isSelfSigned
456-
((X509Certificate)cert, buildParams.sigProvider())) {
457-
if (resultCerts.add((X509Certificate)cert)) {
458-
add = true;
459-
}
453+
if (resultCerts.add((X509Certificate)cert)) {
454+
add = true;
460455
}
461456
}
462457
if (!checkAll && add) {

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

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2023, 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
@@ -48,6 +48,7 @@
4848
import sun.security.x509.AuthorityInfoAccessExtension;
4949
import sun.security.x509.AuthorityKeyIdentifierExtension;
5050
import static sun.security.x509.PKIXExtensions.*;
51+
import sun.security.x509.SubjectAlternativeNameExtension;
5152
import sun.security.x509.X500Name;
5253
import sun.security.x509.X509CertImpl;
5354

@@ -294,9 +295,7 @@ private void getMatchingCACerts(ForwardState currentState,
294295
"\n Issuer: " +
295296
trustedCert.getIssuerX500Principal());
296297
}
297-
if (caCerts.add(trustedCert) && !searchAllCertStores) {
298-
return;
299-
}
298+
caCerts.add(trustedCert);
300299
}
301300
}
302301

@@ -675,8 +674,7 @@ public int compare(X509Certificate oCert1, X509Certificate oCert2) {
675674
* only be executed in a reverse direction are deferred until the
676675
* complete path has been built.
677676
*
678-
* Trust anchor certs are not validated, but are used to verify the
679-
* signature and revocation status of the previous cert.
677+
* Trust anchor certs are not validated.
680678
*
681679
* If the last certificate is being verified (the one whose subject
682680
* matches the target subject, then steps in 6.1.4 of the PKIX
@@ -707,17 +705,15 @@ void verifyCert(X509Certificate cert, State currentState,
707705
currState.untrustedChecker.check(cert, Collections.<String>emptySet());
708706

709707
/*
710-
* check for looping - abort a loop if we encounter the same
711-
* certificate twice
708+
* Abort if we encounter the same certificate or a certificate with
709+
* the same public key, subject DN, and subjectAltNames as a cert
710+
* that is already in path.
712711
*/
713-
if (certPathList != null) {
714-
for (X509Certificate cpListCert : certPathList) {
715-
if (cert.equals(cpListCert)) {
716-
if (debug != null) {
717-
debug.println("loop detected!!");
718-
}
719-
throw new CertPathValidatorException("loop detected");
720-
}
712+
for (X509Certificate cpListCert : certPathList) {
713+
if (repeated(cpListCert, cert)) {
714+
throw new CertPathValidatorException(
715+
"cert with repeated subject, public key, and " +
716+
"subjectAltNames detected");
721717
}
722718
}
723719

@@ -796,21 +792,48 @@ void verifyCert(X509Certificate cert, State currentState,
796792
*/
797793
KeyChecker.verifyCAKeyUsage(cert);
798794
}
795+
}
799796

800-
/*
801-
* the following checks are performed even when the cert
802-
* is a trusted cert, since we are only extracting the
803-
* subjectDN, and publicKey from the cert
804-
* in order to verify a previous cert
805-
*/
797+
/**
798+
* Return true if two certificates are equal or have the same subject,
799+
* public key, and subject alternative names.
800+
*/
801+
private static boolean repeated(
802+
X509Certificate currCert, X509Certificate nextCert) {
803+
if (currCert.equals(nextCert)) {
804+
return true;
805+
}
806+
return (currCert.getSubjectX500Principal().equals(
807+
nextCert.getSubjectX500Principal()) &&
808+
currCert.getPublicKey().equals(nextCert.getPublicKey()) &&
809+
altNamesEqual(currCert, nextCert));
810+
}
806811

807-
/*
808-
* Check signature only if no key requiring key parameters has been
809-
* encountered.
810-
*/
811-
if (!currState.keyParamsNeeded()) {
812-
(currState.cert).verify(cert.getPublicKey(),
813-
buildParams.sigProvider());
812+
/**
813+
* Return true if two certificates have the same subject alternative names.
814+
*/
815+
private static boolean altNamesEqual(
816+
X509Certificate currCert, X509Certificate nextCert) {
817+
X509CertImpl curr, next;
818+
try {
819+
curr = X509CertImpl.toImpl(currCert);
820+
next = X509CertImpl.toImpl(nextCert);
821+
} catch (CertificateException ce) {
822+
return false;
823+
}
824+
825+
SubjectAlternativeNameExtension currAltNameExt =
826+
curr.getSubjectAlternativeNameExtension();
827+
SubjectAlternativeNameExtension nextAltNameExt =
828+
next.getSubjectAlternativeNameExtension();
829+
if (currAltNameExt != null) {
830+
if (nextAltNameExt == null) {
831+
return false;
832+
}
833+
return Arrays.equals(currAltNameExt.getExtensionValue(),
834+
nextAltNameExt.getExtensionValue());
835+
} else {
836+
return (nextAltNameExt == null);
814837
}
815838
}
816839

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

Lines changed: 8 additions & 26 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, 2023, 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
@@ -80,10 +80,8 @@ class ForwardState implements State {
8080
/* The list of user-defined checkers that support forward checking */
8181
ArrayList<PKIXCertPathChecker> forwardCheckers;
8282

83-
/* Flag indicating if key needing to inherit key parameters has been
84-
* encountered.
85-
*/
86-
boolean keyParamsNeededFlag = false;
83+
/* Flag indicating if last cert in path is self-issued */
84+
boolean selfIssued;
8785

8886
/**
8987
* Returns a boolean flag indicating if the state is initial
@@ -96,18 +94,6 @@ public boolean isInitial() {
9694
return init;
9795
}
9896

99-
/**
100-
* Return boolean flag indicating whether a public key that needs to inherit
101-
* key parameters has been encountered.
102-
*
103-
* @return boolean true if key needing to inherit parameters has been
104-
* encountered; false otherwise.
105-
*/
106-
@Override
107-
public boolean keyParamsNeeded() {
108-
return keyParamsNeededFlag;
109-
}
110-
11197
/**
11298
* Display state for debugging purposes
11399
*/
@@ -118,10 +104,10 @@ public String toString() {
118104
sb.append("\n issuerDN of last cert: ").append(issuerDN);
119105
sb.append("\n traversedCACerts: ").append(traversedCACerts);
120106
sb.append("\n init: ").append(String.valueOf(init));
121-
sb.append("\n keyParamsNeeded: ").append
122-
(String.valueOf(keyParamsNeededFlag));
123107
sb.append("\n subjectNamesTraversed: \n").append
124108
(subjectNamesTraversed);
109+
sb.append("\n selfIssued: ").append
110+
(String.valueOf(selfIssued));
125111
sb.append("]\n");
126112
return sb.toString();
127113
}
@@ -166,18 +152,14 @@ public void updateState(X509Certificate cert)
166152

167153
X509CertImpl icert = X509CertImpl.toImpl(cert);
168154

169-
/* see if certificate key has null parameters */
170-
if (PKIX.isDSAPublicKeyWithoutParams(icert.getPublicKey())) {
171-
keyParamsNeededFlag = true;
172-
}
173-
174155
/* update certificate */
175156
this.cert = icert;
176157

177158
/* update issuer DN */
178159
issuerDN = cert.getIssuerX500Principal();
179160

180-
if (!X509CertImpl.isSelfIssued(cert)) {
161+
selfIssued = X509CertImpl.isSelfIssued(cert);
162+
if (!selfIssued) {
181163

182164
/*
183165
* update traversedCACerts only if this is a non-self-issued
@@ -190,7 +172,7 @@ public void updateState(X509Certificate cert)
190172

191173
/* update subjectNamesTraversed only if this is the EE cert or if
192174
this cert is not self-issued */
193-
if (init || !X509CertImpl.isSelfIssued(cert)){
175+
if (init || !selfIssued) {
194176
X500Principal subjName = cert.getSubjectX500Principal();
195177
subjectNamesTraversed.add(X500Name.asX500Name(subjName));
196178

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

Lines changed: 1 addition & 9 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, 2023, 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
@@ -62,12 +62,4 @@ public void updateState(X509Certificate cert)
6262
* @return boolean flag indicating if the state is initial (just starting)
6363
*/
6464
public boolean isInitial();
65-
66-
/**
67-
* Returns a boolean flag indicating if a key lacking necessary key
68-
* algorithm parameters has been encountered.
69-
*
70-
* @return boolean flag indicating if key lacking parameters encountered.
71-
*/
72-
public boolean keyParamsNeeded();
7365
}

0 commit comments

Comments
 (0)