Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8296143: CertAttrSet's set/get mechanism is not type-safe #10959

Closed
wants to merge 7 commits into from

Conversation

wangweij
Copy link
Contributor

@wangweij wangweij commented Nov 2, 2022

The major change is to remove the get and set methods in various CertAttrSet child classes and change them to setXyz and getXyz methods. The Xyz words might come from the field name or the attribute name. For example, X509CertInfo now has setExtensions and setValidity instead of set("extensions", exts) and set("validity", validity). This also has the benefit to remove a lot of try-catch blocks on IOExceptions on "unknown attributes" because everything is known now. At the same time, all the identifier name and attribute names are removed from CertAttrSet child classes. The only left is NAME in extensions since it's still used as keys in CertificateExtensions.

Besides assigning a new value to an internal field, the original set methods might also re-encode by calling encodeThis, invalidate the cached encoding (in X509CertInfo), or check for read-only flag (in X509CertImp). Newly added setXyz methods are doing the same. This is one place that future new setter methods should remember.

Most get implementations simply return an internal field. One exception in X509CertImpl is that when getting something inside the X509CertInfo, it wraps exceptions into a new CertificateParsingException. This is actually related to the way CertificateExtensions::get is implemented where an exception is thrown when an extension does not exist. CertificateExtensions::getExtension has been rewritten to follow the CRLExtensions::getExtension style where null is returned in this case.

The only method left in CertAttrSet is encode, and it no longer throws a CertificateException.

Several classes do have their attributes, and still has get/set methods. This includes CertificateExtensions, CRLExtensions, ReasonFlags, KeyUsageExtension, and NetscapeCertTypeExtensions. Some methods are renamed to be clearer. For example, in CertificateExtensions, we have getExtension instead of get.

There are no more AttributeNameEnumeration.java and X509AttributeName.java.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8296143: CertAttrSet's set/get mechanism is not type-safe

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/10959/head:pull/10959
$ git checkout pull/10959

Update a local copy of the PR:
$ git checkout pull/10959
$ git pull https://git.openjdk.org/jdk pull/10959/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 10959

View PR using the GUI difftool:
$ git pr show -t 10959

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/10959.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 2, 2022

👋 Welcome back weijun! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 2, 2022
@openjdk
Copy link

openjdk bot commented Nov 2, 2022

@wangweij The following label will be automatically applied to this pull request:

  • security

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the security security-dev@openjdk.org label Nov 2, 2022
@mlbridge
Copy link

mlbridge bot commented Nov 2, 2022

@wangweij
Copy link
Contributor Author

wangweij commented Nov 2, 2022

In fact, setter methods in all extensions can be removed since they are only called inside tests.

@mcpowers
Copy link
Contributor

mcpowers commented Nov 4, 2022

Way easier to read. Amazing work!

"CertAttrSet: CertificateX509Key.");
}
public PublicKey getKey() {
return(key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return(key);
return key;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I was just copying lines from the old get method. Will fix.

*/
public interface CertAttrSet<T> {
public interface CertAttrSet {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename this to "CertAttr" since I think it now supports a single attribute?

Copy link
Contributor Author

@wangweij wangweij Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it only has encode now, I'll merge it with DerEncoder and remove it in my next step. Therefore I haven't cared about giving it a better name. I had thought about including the merge inside this change but there are a lot of s/derEncode/encode/ renaming and it's better to be done separately.

Copy link
Member

@seanjmullan seanjmullan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, just a few minor comments.

@@ -1310,7 +1310,7 @@ public String getIssuerAsString() {
* @throws IOException if an encoding error occurs
*/
public byte[] getIssuerAsBytes() throws IOException {
return (issuer == null ? null: issuer.getEncoded());
return issuer == null ? null: issuer.getEncoded();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, add space between "null" and ":".

@@ -687,7 +683,7 @@ public ObjectIdentifier getOID() {
public String getName() {
String n = oid.toString();
KnownOIDs os = KnownOIDs.findMatch(n);
return (os == null? n : os.stdName());
return os == null? n : os.stdName();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add space between "null" and "?".

@@ -204,7 +204,7 @@ static int hops(GeneralNameInterface base, GeneralNameInterface test,
/* base is ancestor of test */
case GeneralNameInterface.NAME_NARROWS:
/* base is descendant of test */
return (test.subtreeDepth()-base.subtreeDepth());
return test.subtreeDepth()-base.subtreeDepth();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: add spaces around "-".

info.set(X509CertInfo.SUBJECT,
dname==null?req.getSubjectName():new X500Name(dname));
info.setKey(new CertificateX509Key(req.getSubjectPublicKeyInfo()));
info.setSubject(dname==null?req.getSubjectName():new X500Name(dname));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add spaces between "?" and ":".

"CertAttrSet:CertificatePoliciesExtension.");
}
public List<PolicyInformation> getCertPolicies() {
//XXXX May want to consider cloning this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this comment. This method is internal and as long as the List is not exposed via a public API (please double-check), a clone is not necessary.

"CertAttrSet:ExtendedKeyUsageExtension.");
}
public Vector<ObjectIdentifier> getUsages() {
//XXXX May want to consider cloning this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment if returned Vector cannot be accessed via public APIs.

@openjdk
Copy link

openjdk bot commented Nov 8, 2022

@wangweij This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8296143: CertAttrSet's set/get mechanism is not type-safe

Reviewed-by: mullan

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 72 new commits pushed to the master branch:

  • 0ee25de: 8296504: Memory leak in G1PLABAllocator::PLABData
  • dd5d4df: 8295658: G1: Refactor G1SegmentedArray to indicate that it is an allocator
  • cf65605: 8296445: C++ syntax error in jdwpTransport.h
  • 1169dc0: 8296447: RISC-V: Make the operands order of vrsub_vx/vrsub_vi consistent with RVV 1.0 spec
  • 4c80dff: 8296435: RISC-V: Small refactoring for increment/decrement
  • 47d2c7b: 8295376: Improve debug agent virtual thread performance when no debugger is attached
  • 76790ad: 8295673: Deprecate and disable legacy parallel class loading workaround for non-parallel-capable class loaders
  • b6738c1: 8295663: Rephrase introduction to testing.md
  • 7e85b41: 8296154: [macos] Change "/Applications" to "Applications" in DMG image
  • 60db5f2: 8294020: improve errors for record declarations
  • ... and 62 more: https://git.openjdk.org/jdk/compare/16a041a67a30ad8f3160e211c629c055d3ff2f80...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 8, 2022
@wangweij
Copy link
Contributor Author

wangweij commented Nov 8, 2022

/integrate

@openjdk
Copy link

openjdk bot commented Nov 8, 2022

Going to push as commit 671f84b.
Since your change was applied there have been 74 commits pushed to the master branch:

  • d04d656: 8296433: Encountered null CLD while loading shared lambda proxy class
  • 74f2b16: 8295303: cleanup debug agent's confusing use of EI_GC_FINISH
  • 0ee25de: 8296504: Memory leak in G1PLABAllocator::PLABData
  • dd5d4df: 8295658: G1: Refactor G1SegmentedArray to indicate that it is an allocator
  • cf65605: 8296445: C++ syntax error in jdwpTransport.h
  • 1169dc0: 8296447: RISC-V: Make the operands order of vrsub_vx/vrsub_vi consistent with RVV 1.0 spec
  • 4c80dff: 8296435: RISC-V: Small refactoring for increment/decrement
  • 47d2c7b: 8295376: Improve debug agent virtual thread performance when no debugger is attached
  • 76790ad: 8295673: Deprecate and disable legacy parallel class loading workaround for non-parallel-capable class loaders
  • b6738c1: 8295663: Rephrase introduction to testing.md
  • ... and 64 more: https://git.openjdk.org/jdk/compare/16a041a67a30ad8f3160e211c629c055d3ff2f80...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Nov 8, 2022
@openjdk openjdk bot closed this Nov 8, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 8, 2022
@openjdk
Copy link

openjdk bot commented Nov 8, 2022

@wangweij Pushed as commit 671f84b.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@wangweij wangweij deleted the 8296143 branch November 8, 2022 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated security security-dev@openjdk.org
4 participants