Skip to content

Commit 671f84b

Browse files
committed
8296143: CertAttrSet's set/get mechanism is not type-safe
Reviewed-by: mullan
1 parent d04d656 commit 671f84b

File tree

66 files changed

+632
-2891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+632
-2891
lines changed

src/java.base/share/classes/java/security/cert/CertificateRevokedException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public Date getInvalidityDate() {
155155
return null;
156156
} else {
157157
try {
158-
Date invalidity = InvalidityDateExtension.toImpl(ext).get("DATE");
158+
Date invalidity = InvalidityDateExtension.toImpl(ext).getDate();
159159
return new Date(invalidity.getTime());
160160
} catch (IOException ioe) {
161161
return null;

src/java.base/share/classes/java/security/cert/X509CRLSelector.java

Lines changed: 3 additions & 3 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
@@ -323,7 +323,7 @@ private static HashSet<Object> cloneAndCheckIssuerNames(Collection<?> names)
323323
else
324324
namesCopy.add(nameObject);
325325
}
326-
return(namesCopy);
326+
return namesCopy;
327327
}
328328

329329
/**
@@ -630,7 +630,7 @@ public boolean match(CRL crl) {
630630
byte[] encoded = in.getOctetString();
631631
CRLNumberExtension crlNumExt =
632632
new CRLNumberExtension(Boolean.FALSE, encoded);
633-
crlNum = crlNumExt.get(CRLNumberExtension.NUMBER);
633+
crlNum = crlNumExt.getCrlNumber();
634634
} catch (IOException ex) {
635635
if (debug != null) {
636636
debug.println("X509CRLSelector.match: exception in "

src/java.base/share/classes/java/security/cert/X509CertSelector.java

Lines changed: 17 additions & 29 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
@@ -1288,7 +1288,7 @@ public X500Principal getIssuer() {
12881288
*/
12891289
@Deprecated(since="16")
12901290
public String getIssuerAsString() {
1291-
return (issuer == null ? null : issuer.getName());
1291+
return issuer == null ? null : issuer.getName();
12921292
}
12931293

12941294
/**
@@ -1310,7 +1310,7 @@ public String getIssuerAsString() {
13101310
* @throws IOException if an encoding error occurs
13111311
*/
13121312
public byte[] getIssuerAsBytes() throws IOException {
1313-
return (issuer == null ? null: issuer.getEncoded());
1313+
return issuer == null ? null : issuer.getEncoded();
13141314
}
13151315

13161316
/**
@@ -1347,7 +1347,7 @@ public X500Principal getSubject() {
13471347
*/
13481348
@Deprecated(since="16")
13491349
public String getSubjectAsString() {
1350-
return (subject == null ? null : subject.getName());
1350+
return subject == null ? null : subject.getName();
13511351
}
13521352

13531353
/**
@@ -1369,7 +1369,7 @@ public String getSubjectAsString() {
13691369
* @throws IOException if an encoding error occurs
13701370
*/
13711371
public byte[] getSubjectAsBytes() throws IOException {
1372-
return (subject == null ? null : subject.getEncoded());
1372+
return subject == null ? null : subject.getEncoded();
13731373
}
13741374

13751375
/**
@@ -1868,7 +1868,7 @@ private static String keyUsageToString(boolean[] k) {
18681868

18691869
s += "]\n";
18701870

1871-
return (s);
1871+
return s;
18721872
}
18731873

18741874
/**
@@ -2120,12 +2120,8 @@ private boolean matchPrivateKeyValid(X509Certificate xcert) {
21202120
} catch (CertificateExpiredException e1) {
21212121
if (debug != null) {
21222122
String time = "n/a";
2123-
try {
2124-
Date notAfter = ext.get(PrivateKeyUsageExtension.NOT_AFTER);
2125-
time = notAfter.toString();
2126-
} catch (CertificateException ex) {
2127-
// not able to retrieve notAfter value
2128-
}
2123+
Date notAfter = ext.getNotAfter();
2124+
time = notAfter.toString();
21292125
debug.println("X509CertSelector.match: private key usage not "
21302126
+ "within validity date; ext.NOT_After: "
21312127
+ time + "; X509CertSelector: "
@@ -2136,12 +2132,8 @@ private boolean matchPrivateKeyValid(X509Certificate xcert) {
21362132
} catch (CertificateNotYetValidException e2) {
21372133
if (debug != null) {
21382134
String time = "n/a";
2139-
try {
2140-
Date notBefore = ext.get(PrivateKeyUsageExtension.NOT_BEFORE);
2141-
time = notBefore.toString();
2142-
} catch (CertificateException ex) {
2143-
// not able to retrieve notBefore value
2144-
}
2135+
Date notBefore = ext.getNotBefore();
2136+
time = notBefore.toString();
21452137
debug.println("X509CertSelector.match: private key usage not "
21462138
+ "within validity date; ext.NOT_BEFORE: "
21472139
+ time + "; X509CertSelector: "
@@ -2227,8 +2219,7 @@ private boolean matchExtendedKeyUsage(X509Certificate xcert) {
22272219
(ExtendedKeyUsageExtension)getExtensionObject(xcert,
22282220
KnownOIDs.extendedKeyUsage);
22292221
if (ext != null) {
2230-
Vector<ObjectIdentifier> certKeyPurposeVector =
2231-
ext.get(ExtendedKeyUsageExtension.USAGES);
2222+
Vector<ObjectIdentifier> certKeyPurposeVector = ext.getUsages();
22322223
if (!certKeyPurposeVector.contains(ANY_EXTENDED_KEY_USAGE)
22332224
&& !certKeyPurposeVector.containsAll(keyPurposeOIDSet)) {
22342225
if (debug != null) {
@@ -2264,8 +2255,7 @@ private boolean matchSubjectAlternativeNames(X509Certificate xcert) {
22642255
}
22652256
return false;
22662257
}
2267-
GeneralNames certNames =
2268-
sanExt.get(SubjectAlternativeNameExtension.SUBJECT_NAME);
2258+
GeneralNames certNames = sanExt.getNames();
22692259
Iterator<GeneralNameInterface> i =
22702260
subjectAlternativeGeneralNames.iterator();
22712261
while (i.hasNext()) {
@@ -2333,7 +2323,7 @@ private boolean matchPolicy(X509Certificate xcert) {
23332323
}
23342324
return false;
23352325
}
2336-
List<PolicyInformation> policies = ext.get(CertificatePoliciesExtension.POLICIES);
2326+
List<PolicyInformation> policies = ext.getCertPolicies();
23372327
/*
23382328
* Convert the Vector of PolicyInformation to a Vector
23392329
* of CertificatePolicyIds for easier comparison.
@@ -2401,17 +2391,15 @@ private boolean matchPathToNames(X509Certificate xcert) {
24012391
}
24022392
}
24032393

2404-
GeneralSubtrees permitted =
2405-
ext.get(NameConstraintsExtension.PERMITTED_SUBTREES);
2406-
GeneralSubtrees excluded =
2407-
ext.get(NameConstraintsExtension.EXCLUDED_SUBTREES);
2394+
GeneralSubtrees permitted = ext.getPermittedSubtrees();
2395+
GeneralSubtrees excluded = ext.getExcludedSubtrees();
24082396
if (excluded != null) {
2409-
if (matchExcluded(excluded) == false) {
2397+
if (!matchExcluded(excluded)) {
24102398
return false;
24112399
}
24122400
}
24132401
if (permitted != null) {
2414-
if (matchPermitted(permitted) == false) {
2402+
if (!matchPermitted(permitted)) {
24152403
return false;
24162404
}
24172405
}

src/java.base/share/classes/sun/security/pkcs/PKCS7.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,7 @@ private void populateCertIssuerNames() {
684684
try {
685685
X509CertInfo tbsCert =
686686
new X509CertInfo(cert.getTBSCertificate());
687-
certIssuerName = (Principal)
688-
tbsCert.get(X509CertInfo.ISSUER + "." +
689-
X509CertInfo.DN_NAME);
687+
certIssuerName = tbsCert.getIssuer();
690688
} catch (Exception e) {
691689
// error generating X500Name object from the cert's
692690
// issuer DN, leave name as is.

src/java.base/share/classes/sun/security/pkcs/PKCS9Attribute.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,7 @@ public void derEncode(DerOutputStream out) throws IOException {
617617
{
618618
DerOutputStream temp2 = new DerOutputStream();
619619
CertificateExtensions exts = (CertificateExtensions)value;
620-
try {
621-
exts.encode(temp2, true);
622-
} catch (CertificateException ex) {
623-
throw new IOException(ex.toString());
624-
}
620+
exts.encode(temp2, true);
625621
temp.write(DerValue.tag_Set, temp2.toByteArray());
626622
}
627623
break;
@@ -687,7 +683,7 @@ public ObjectIdentifier getOID() {
687683
public String getName() {
688684
String n = oid.toString();
689685
KnownOIDs os = KnownOIDs.findMatch(n);
690-
return (os == null? n : os.stdName());
686+
return os == null ? n : os.stdName();
691687
}
692688

693689
/**

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ void setSkiAndSerialNumber(AuthorityKeyIdentifierExtension ext)
131131

132132
if (ext != null) {
133133
ski = ext.getEncodedKeyIdentifier();
134-
SerialNumber asn = (SerialNumber)ext.get(
135-
AuthorityKeyIdentifierExtension.SERIAL_NUMBER);
134+
SerialNumber asn = ext.getSerialNumber();
136135
if (asn != null) {
137136
serial = asn.getNumber();
138137
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void check(Certificate cert,
190190
AlgorithmId algorithmId;
191191
try {
192192
x509Cert = X509CertImpl.toImpl((X509Certificate)cert);
193-
algorithmId = (AlgorithmId)x509Cert.get(X509CertImpl.SIG_ALG);
193+
algorithmId = x509Cert.getSigAlg();
194194
} catch (CertificateException ce) {
195195
throw new CertPathValidatorException(ce);
196196
}

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static int hops(GeneralNameInterface base, GeneralNameInterface test,
204204
/* base is ancestor of test */
205205
case GeneralNameInterface.NAME_NARROWS:
206206
/* base is descendant of test */
207-
return (test.subtreeDepth()-base.subtreeDepth());
207+
return test.subtreeDepth() - base.subtreeDepth();
208208
default: // should never occur
209209
return incomparable;
210210
}
@@ -230,7 +230,7 @@ static int hops(GeneralNameInterface base, GeneralNameInterface test,
230230
int commonDistance = commonName.subtreeDepth();
231231
int baseDistance = baseName.subtreeDepth();
232232
int testDistance = testName.subtreeDepth();
233-
return (baseDistance + testDistance - (2 * commonDistance));
233+
return baseDistance + testDistance - (2 * commonDistance);
234234
}
235235
}
236236

@@ -300,8 +300,7 @@ static int targetDistance(NameConstraintsExtension constraints,
300300
SubjectAlternativeNameExtension altNameExt =
301301
certImpl.getSubjectAlternativeNameExtension();
302302
if (altNameExt != null) {
303-
GeneralNames altNames = altNameExt.get(
304-
SubjectAlternativeNameExtension.SUBJECT_NAME);
303+
GeneralNames altNames = altNameExt.getNames();
305304
/* see if any alternative name matches target */
306305
if (altNames != null) {
307306
for (int j = 0, n = altNames.size(); j < n; j++) {
@@ -337,10 +336,8 @@ static int targetDistance(NameConstraintsExtension constraints,
337336
+ constraints);
338337
}
339338
/* reduce permitted by excluded */
340-
GeneralSubtrees permitted =
341-
constraints.get(NameConstraintsExtension.PERMITTED_SUBTREES);
342-
GeneralSubtrees excluded =
343-
constraints.get(NameConstraintsExtension.EXCLUDED_SUBTREES);
339+
GeneralSubtrees permitted = constraints.getPermittedSubtrees();
340+
GeneralSubtrees excluded = constraints.getExcludedSubtrees();
344341
if (permitted != null) {
345342
permitted.reduce(excluded);
346343
}
@@ -362,7 +359,7 @@ static int targetDistance(NameConstraintsExtension constraints,
362359
GeneralNameInterface perName = permitted.get(i).getName().getName();
363360
int distance = distance(perName, target, -1);
364361
if (distance >= 0) {
365-
return (distance + 1);
362+
return distance + 1;
366363
}
367364
}
368365
/* no matching type in permitted; cert holder could certify target */

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static Collection<X509CRL> getCRLs(X509CRLSelector selector,
102102
return Collections.emptySet();
103103
}
104104
List<DistributionPoint> points =
105-
ext.get(CRLDistributionPointsExtension.POINTS);
105+
ext.getDistributionPoints();
106106
Set<X509CRL> results = new HashSet<>();
107107
for (Iterator<DistributionPoint> t = points.iterator();
108108
t.hasNext() && !Arrays.equals(reasonsMask, ALL_REASONS); ) {
@@ -116,7 +116,7 @@ public static Collection<X509CRL> getCRLs(X509CRLSelector selector,
116116
debug.println("Returning " + results.size() + " CRLs");
117117
}
118118
return results;
119-
} catch (CertificateException | IOException e) {
119+
} catch (CertificateException e) {
120120
return Collections.emptySet();
121121
}
122122
}
@@ -333,9 +333,7 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
333333
GeneralNames pointCrlIssuers = point.getCRLIssuer();
334334
X500Name pointCrlIssuer = null;
335335
if (pointCrlIssuers != null) {
336-
if (idpExt == null ||
337-
idpExt.get(IssuingDistributionPointExtension.INDIRECT_CRL)
338-
== Boolean.FALSE) {
336+
if (idpExt == null || !idpExt.isIndirectCRL()) {
339337
return false;
340338
}
341339
boolean match = false;
@@ -398,8 +396,7 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
398396
}
399397

400398
if (idpExt != null) {
401-
DistributionPointName idpPoint = (DistributionPointName)
402-
idpExt.get(IssuingDistributionPointExtension.POINT);
399+
DistributionPointName idpPoint = idpExt.getDistributionPoint();
403400
if (idpPoint != null) {
404401
GeneralNames idpNames = idpPoint.getFullName();
405402
if (idpNames == null) {
@@ -495,9 +492,8 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
495492

496493
// if the onlyContainsUserCerts boolean is asserted, verify that the
497494
// cert is not a CA cert
498-
Boolean b = (Boolean)
499-
idpExt.get(IssuingDistributionPointExtension.ONLY_USER_CERTS);
500-
if (b.equals(Boolean.TRUE) && certImpl.getBasicConstraints() != -1) {
495+
boolean b = idpExt.hasOnlyUserCerts();
496+
if (b && certImpl.getBasicConstraints() != -1) {
501497
if (debug != null) {
502498
debug.println("cert must be a EE cert");
503499
}
@@ -506,9 +502,8 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
506502

507503
// if the onlyContainsCACerts boolean is asserted, verify that the
508504
// cert is a CA cert
509-
b = (Boolean)
510-
idpExt.get(IssuingDistributionPointExtension.ONLY_CA_CERTS);
511-
if (b.equals(Boolean.TRUE) && certImpl.getBasicConstraints() == -1) {
505+
b = idpExt.hasOnlyCACerts();
506+
if (b && certImpl.getBasicConstraints() == -1) {
512507
if (debug != null) {
513508
debug.println("cert must be a CA cert");
514509
}
@@ -517,9 +512,8 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
517512

518513
// verify that the onlyContainsAttributeCerts boolean is not
519514
// asserted
520-
b = (Boolean) idpExt.get
521-
(IssuingDistributionPointExtension.ONLY_ATTRIBUTE_CERTS);
522-
if (b.equals(Boolean.TRUE)) {
515+
b = idpExt.hasOnlyAttributeCerts();
516+
if (b) {
523517
if (debug != null) {
524518
debug.println("cert must not be an AA cert");
525519
}
@@ -531,8 +525,7 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
531525
boolean[] interimReasonsMask = new boolean[9];
532526
ReasonFlags reasons = null;
533527
if (idpExt != null) {
534-
reasons = (ReasonFlags)
535-
idpExt.get(IssuingDistributionPointExtension.REASONS);
528+
reasons = idpExt.getRevocationReasons();
536529
}
537530

538531
boolean[] pointReasonFlags = point.getReasonFlags();
@@ -603,8 +596,7 @@ static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,
603596
certSel.setSubjectKeyIdentifier(kid);
604597
}
605598

606-
SerialNumber asn = (SerialNumber)akidext.get(
607-
AuthorityKeyIdentifierExtension.SERIAL_NUMBER);
599+
SerialNumber asn = akidext.getSerialNumber();
608600
if (asn != null) {
609601
certSel.setSerialNumber(asn.getNumber());
610602
}

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,27 +187,17 @@ public void updateState(X509Certificate cert)
187187

188188
/* update subjectNamesTraversed only if this is the EE cert or if
189189
this cert is not self-issued */
190-
if (init || !X509CertImpl.isSelfIssued(cert)){
190+
if (init || !X509CertImpl.isSelfIssued(cert)) {
191191
X500Principal subjName = cert.getSubjectX500Principal();
192192
subjectNamesTraversed.add(X500Name.asX500Name(subjName));
193193

194-
try {
195-
SubjectAlternativeNameExtension subjAltNameExt
194+
SubjectAlternativeNameExtension subjAltNameExt
196195
= icert.getSubjectAlternativeNameExtension();
197-
if (subjAltNameExt != null) {
198-
GeneralNames gNames = subjAltNameExt.get(
199-
SubjectAlternativeNameExtension.SUBJECT_NAME);
200-
for (GeneralName gName : gNames.names()) {
201-
subjectNamesTraversed.add(gName.getName());
202-
}
196+
if (subjAltNameExt != null) {
197+
GeneralNames gNames = subjAltNameExt.getNames();
198+
for (GeneralName gName : gNames.names()) {
199+
subjectNamesTraversed.add(gName.getName());
203200
}
204-
} catch (IOException e) {
205-
if (debug != null) {
206-
debug.println("ForwardState.updateState() unexpected "
207-
+ "exception");
208-
e.printStackTrace();
209-
}
210-
throw new CertPathValidatorException(e);
211201
}
212202
}
213203

0 commit comments

Comments
 (0)