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
Conversation
👋 Welcome back weijun! A progress list of the required criteria for merging this PR into |
@wangweij The following label will be automatically applied to this pull request: 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 |
Webrevs
|
*/ | ||
public DerInputStream(byte[] data) throws IOException { | ||
init(data, 0, data.length, true); | ||
public DerInputStream(byte[] data, int start, int length, boolean allowBER) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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()?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5d should be %d?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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()
.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For IMPLICIT. Will comment.
There was a problem hiding this 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; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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)", |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this 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.
@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:
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
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 |
Added a new commit. Mostly comment or style change. Several always-false |
/test tier1, tier2 |
@wangweij the test group tier2 does not exist |
/integrate |
@wangweij Since your change was applied there have been 128 commits pushed to the
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. |
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
Issue
Reviewers
Download
$ git fetch https://git.openjdk.java.net/jdk pull/232/head:pull/232
$ git checkout pull/232