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

8249783: Simplify DerValue and DerInputStream #232

Closed
wants to merge 5 commits into from

Conversation

wangweij
Copy link
Contributor

@wangweij wangweij commented Sep 17, 2020

This code change rewrites DerValue into a mostly immutable class and simplifies DerInputStream as a wrapper for a series of DerValues objects. DerInputBuffer is removed.

All existing methods of DerValue and DerInputStream should still work with the exact same behavior, except for a few places where bugs are fixed. For example, Indefinite length must be used with a constructed tag.

Except for the ObjectIdentifier class where DerInputBuffer is directly referenced, no other code is touched.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

Reviewers

Download

$ git fetch https://git.openjdk.java.net/jdk pull/232/head:pull/232
$ git checkout pull/232

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 17, 2020

👋 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 Sep 17, 2020
@openjdk
Copy link

openjdk bot commented Sep 17, 2020

@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 (add|remove) "label" command.

@openjdk openjdk bot added the security security-dev@openjdk.org label Sep 17, 2020
@mlbridge
Copy link

mlbridge bot commented Sep 17, 2020

Webrevs

*/
public DerInputStream(byte[] data) throws IOException {
init(data, 0, data.length, true);
public DerInputStream(byte[] data, int start, int length, boolean allowBER) {

Choose a reason for hiding this comment

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

Add javadoc for the new impl? Do we need to validate things like data != null, start and length have valid values? Or assuming that since all its callers are internal, no need to check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't intend to add any check because callers are internal. I can add a comment.

Copy link

Choose a reason for hiding this comment

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

Adding a comment would be good.

*/
public DerInputStream(byte[] data, int offset, int len,
boolean allowBER) throws IOException {
init(data, offset, len, allowBER);

Choose a reason for hiding this comment

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

Add javadoc for the new impl?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK.

DerInputStream(DerInputBuffer buf) {
buffer = buf;
buffer.mark(Integer.MAX_VALUE);
public byte[] toByteArray() {

Choose a reason for hiding this comment

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

Add javadoc? Returns the remaining unread bytes? The name may lead people to expect the bytes returned are the one passing to the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well. This method is sometimes used to read remaining bytes and sometimes used to read all (suppose none has been read yet). I will clarify.

// The static part
final byte[] data;
final int start;
final int end;

Choose a reason for hiding this comment

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

Well, end is really "start + length", the data range is from start to (end -1) (inclusive).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add some comments.

public byte[] toByteArray() {
return buffer.toByteArray();
public DerValue getDerValue() throws IOException {
DerValue result = new DerValue(

Choose a reason for hiding this comment

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

Check (this.end - this.pos > 0) before calling DerValue()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

new DerValue() would fail in this case.

Choose a reason for hiding this comment

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

Sure.

* @param generalized true if Generalized Time is to be read, false
* if UTC Time is to be read.
*/
private Date getTime(int len, boolean generalized) throws IOException {

Choose a reason for hiding this comment

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

You have the Internal suffix on other private helper routines. Maybe add it here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea.

throw new IOException("DER UTC Time length error");

data.pos = data.end; // Compatibility. Reach end.
return getTime(end - start, false);

Choose a reason for hiding this comment

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

Given that getTime() have access to all the fields, it seems redundant to have to specify (end-start) when calling it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea.

} catch (IOException e) {
throw new IllegalArgumentException("misformatted DER value");
}
return String.format("DerValue(%02x, %s, %d, 5d)",

Choose a reason for hiding this comment

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

5d should be %d?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ouch.

* @param newTag the new tag
* @return a new DerValue
*/
public DerValue withTag(byte newTag) {

Choose a reason for hiding this comment

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

Seems like a somewhat dangerous method. The value may not match the new tag? The caller is expected to know what it's doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intend to use it to turn back IMPLICIT element to its original tag, otherwise getXyz() calls would fail because they are checking the tag. In the old design, checking the tag (in DerValue) and reading the value (in DerInputBuffer) are separated but now they are in a single method (in DerValue).

The caller pattern should always look like withTag(INTEGER).getInteger().

Choose a reason for hiding this comment

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

Hmm, I see, maybe add more comments then.

}

DerValue[] subs(byte expectedTag, int startLen) throws IOException {
if (expectedTag != 0 && expectedTag != tag) {

Choose a reason for hiding this comment

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

so if expectedTag == 0, then we don't check for tag match? Looks strange. If this is intentional, we should add comment to clarify why.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For IMPLICIT. Will comment.

Copy link
Contributor Author

@wangweij wangweij left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your detailed code review. I'll push a new commit early next week.

// The static part
final byte[] data;
final int start;
final int end;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add some comments.

*/
public DerInputStream(byte[] data) throws IOException {
init(data, 0, data.length, true);
public DerInputStream(byte[] data, int start, int length, boolean allowBER) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't intend to add any check because callers are internal. I can add a comment.

*/
public DerInputStream(byte[] data, int offset, int len,
boolean allowBER) throws IOException {
init(data, offset, len, allowBER);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK.

DerInputStream(DerInputBuffer buf) {
buffer = buf;
buffer.mark(Integer.MAX_VALUE);
public byte[] toByteArray() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well. This method is sometimes used to read remaining bytes and sometimes used to read all (suppose none has been read yet). I will clarify.

public byte[] toByteArray() {
return buffer.toByteArray();
public DerValue getDerValue() throws IOException {
DerValue result = new DerValue(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

new DerValue() would fail in this case.

throw new IOException("DER UTC Time length error");

data.pos = data.end; // Compatibility. Reach end.
return getTime(end - start, false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea.

} catch (IOException e) {
throw new IllegalArgumentException("misformatted DER value");
}
return String.format("DerValue(%02x, %s, %d, 5d)",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ouch.

* @param newTag the new tag
* @return a new DerValue
*/
public DerValue withTag(byte newTag) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I intend to use it to turn back IMPLICIT element to its original tag, otherwise getXyz() calls would fail because they are checking the tag. In the old design, checking the tag (in DerValue) and reading the value (in DerInputBuffer) are separated but now they are in a single method (in DerValue).

The caller pattern should always look like withTag(INTEGER).getInteger().

}

DerValue[] subs(byte expectedTag, int startLen) throws IOException {
if (expectedTag != 0 && expectedTag != tag) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For IMPLICIT. Will comment.

// DerInputStream::getOctetString does not
Utils.runAndCheckException(
() -> new DerInputStream(input).getOctetString(),
IOException.class);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The existing impl already differs, but DerValue::getOctetString can only read one level of constructed OCTET STRING. My second commit can read arbitrary levels and hence this this regression test. There are two levels of 0x24 here.

@wangweij
Copy link
Contributor Author

wangweij commented Sep 29, 2020

New change pushed. I haven't really moved the "deprecated" codes together in DerValue. I just put the data-related and tag-related methods together and add more comments.

Besides following your suggestions in the comment, this time I made some fine tuned changes for indefinite length parsing, esp, for the uncertainty about remaining data after conversion (see the updated Indefinite.java test). In the future, we can enhance DerIndefLenConverter to be more precise and efficient.


int value, tmp;
String mdName = "DerInputStream.getLength(): ";
tmp = lenByte;
if ((tmp & 0x080) == 0x00) { // short form, 1 byte datum
value = tmp;
} else { // long form or indefinite

Choose a reason for hiding this comment

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

Can't be indefinite here given the new check (line 233-235 above?) Move the indefinite comment upward?

Copy link

@valeriepeng valeriepeng left a comment

Choose a reason for hiding this comment

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

Updated webrev looks fine. Only a minor nit regarding a comment remaining.

@openjdk
Copy link

openjdk bot commented Oct 1, 2020

@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 more details.

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

8249783: Simplify DerValue and DerInputStream

Reviewed-by: valeriep

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 110 new commits pushed to the master branch:

  • 05a764f: 8253883: Problem list jdk/test/lib/hexdump/ASN1Formatter on Windows
  • 55c282b: 8253878: clean up nsk/share/jvmti/ArgumentHandler
  • 776acfd: 8253880: clean up sun/hotspot/tools/ctw/Utils class
  • 79d70f6: 8253869: sun/hotspot/whitebox/CPUInfoTest.java fails after JDK-8239090
  • ca0e014: 8252003: remove usage of PropertyResolvingWrapper in vmTestbase/nsk/jvmti
  • 092c227: 8252523: Add ASN.1 Formatter to work with test utility HexPrinter
  • 06d8cf6: 8253812: Cleanup AbstractMemberWriter
  • 424d7d6: 8252881: [JVMCI] ResolvedJavaType.resolveMethod fails in fastdebug when invoked with a constructor
  • 2a406f3: 8138732: Rename @HotSpotIntrinsicCandidate to @IntrinsicCandidate and move it to the jdk.internal.vm.annotation package
  • 4b16f8a: 8253872: ArgumentHandler must use the same delimiters as in jvmti_tools.cpp
  • ... and 100 more: https://git.openjdk.java.net/jdk/compare/b8ea80af33ecc9d4ccfa3c3dc366e1a88c3607b4...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 Oct 1, 2020
@wangweij
Copy link
Contributor Author

wangweij commented Oct 1, 2020

Added a new commit. Mostly comment or style change. Several always-false if (lenByte < 0) check removed. result.toArray(new DerValue[0]) used because new knowledge shows it's faster than result.toArray(new DerValue[result.size()]). getSet(int,boolean implicit) added check for implicit to be semantically correct (although it's always called with implicit == true.

@wangweij
Copy link
Contributor Author

wangweij commented Oct 1, 2020

/test tier1, tier2

@openjdk
Copy link

openjdk bot commented Oct 1, 2020

@wangweij the test group tier2 does not exist

@wangweij
Copy link
Contributor Author

wangweij commented Oct 1, 2020

/integrate

@openjdk openjdk bot closed this Oct 1, 2020
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 1, 2020
@openjdk
Copy link

openjdk bot commented Oct 1, 2020

@wangweij Since your change was applied there have been 128 commits pushed to the master branch:

  • 9230c2a: 8253747: tools/jpackage/share/AppImagePackageTest.java fails with InstalledPackageSize: 0
  • cfd41c0: 8232840: java/math/BigInteger/largeMemory/SymmetricRangeTests.java fails due to "OutOfMemoryError: Requested array size exceeds VM limit"
  • 8fda5b8: 8253904: Revert Tokenizer improvements JDK-8224225
  • 60ec2a5: 8253824: Revert JDK-8253089 since VS warning C4307 has been disabled
  • 90c131f: 8224225: Tokenizer improvements
  • 9670425: 8253822: Remove unused exception_address_is_unpack_entry
  • 8440279: 8180514: TestPrintMdo.java test fails with -XX:-TieredCompilation
  • 44e6820: 8253650: Cleanup: remove alignment_hint parameter from os::reserve_memory
  • ed62b01: 6646602: Spelling error in javadoc for javax.swing.tree.TreeModel
  • 87276bc: 6690021: typos in TransferHandler Javadoc
  • ... and 118 more: https://git.openjdk.java.net/jdk/compare/b8ea80af33ecc9d4ccfa3c3dc366e1a88c3607b4...master

Your commit was automatically rebased without conflicts.

Pushed as commit 3c4e824.

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

@wangweij wangweij deleted the 8249783 branch October 1, 2020 19:01
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
2 participants