aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-12 12:19:49 +0800
committerYuuta Liang <yuutaw@students.cs.ubc.ca>2023-10-12 12:19:49 +0800
commitf369da34cf9aca151df0150d90e651e6a88ee700 (patch)
tree15407d1578fec2b7d40f90190415e92e9d02c93d
parentd342a45d98c4795b3a3fe1aaef5236ad4a782b55 (diff)
downloadjca-f369da34cf9aca151df0150d90e651e6a88ee700.tar
jca-f369da34cf9aca151df0150d90e651e6a88ee700.tar.gz
jca-f369da34cf9aca151df0150d90e651e6a88ee700.tar.bz2
jca-f369da34cf9aca151df0150d90e651e6a88ee700.zip
Fix lint
Signed-off-by: Yuuta Liang <yuutaw@students.cs.ubc.ca>
-rw-r--r--src/main/model/asn1/ASN1Object.java8
-rw-r--r--src/main/model/asn1/ObjectIdentifier.java65
2 files changed, 40 insertions, 33 deletions
diff --git a/src/main/model/asn1/ASN1Object.java b/src/main/model/asn1/ASN1Object.java
index 1af26ce..d1bce06 100644
--- a/src/main/model/asn1/ASN1Object.java
+++ b/src/main/model/asn1/ASN1Object.java
@@ -80,12 +80,8 @@ public class ASN1Object implements Encodable {
public ASN1Object(BytesReader encoded, boolean hasParentTag) throws ParseException {
if (hasParentTag) {
this.parentTag = new Tag(encoded);
- if (parentTag.getCls() != TagClass.CONTEXT_SPECIFIC) {
- throw new ParseException("Parent tag must be CONTEXT_SPECIFIC, but found "
- + parentTag.getCls() + "[" + parentTag.getNumber() + "].");
- }
- if (!parentTag.isConstructive()) {
- throw new ParseException("Parent tag must be constructive.");
+ if (parentTag.getCls() != TagClass.CONTEXT_SPECIFIC || !parentTag.isConstructive()) {
+ throw new ParseException("Parent tag must be CONTEXT_SPECIFIC and constructive.");
}
int parentLen = new ASN1Length(encoded).getLength();
// Validate length
diff --git a/src/main/model/asn1/ObjectIdentifier.java b/src/main/model/asn1/ObjectIdentifier.java
index e2b9dfe..c7278db 100644
--- a/src/main/model/asn1/ObjectIdentifier.java
+++ b/src/main/model/asn1/ObjectIdentifier.java
@@ -102,6 +102,16 @@ public class ObjectIdentifier extends ASN1Object {
nums.add(0);
nums.add((int) first);
}
+ nums.addAll(parse(raw));
+ this.ints = nums.toArray(new Integer[0]);
+ }
+
+ /**
+ * EFFECTS: Parse input OID bytes.
+ * REQUIRES: raw.length >= 1
+ */
+ private static List<Integer> parse(Byte[] raw) throws ParseException {
+ List<Integer> nums = new ArrayList<>();
List<BitSet> num = new ArrayList<>();
for (int i = 1; i < raw.length; i++) {
Byte b = raw[i];
@@ -123,13 +133,9 @@ public class ObjectIdentifier extends ASN1Object {
}
}
if (!num.isEmpty()) {
- throw new ParseException("Unterminated byte. Currently "
- + num.stream().map(BitSet::toByteArray).map(Utils::byteToByte)
- .flatMap(Arrays::stream)
- .map(b -> String.format("0x%02X", b))
- .collect(Collectors.toList()));
+ throw new ParseException("Unterminated byte.");
}
- this.ints = nums.toArray(new Integer[0]);
+ return nums;
}
/**
@@ -171,33 +177,38 @@ public class ObjectIdentifier extends ASN1Object {
Arrays.asList(Utils.valToByte(ints[0] * 40 + ints[1])),
Stream.of(ints)
.skip(2)
- .map(i -> {
- BigInteger bi = BigInteger.valueOf(i);
- List<Byte> bs = Arrays.asList(Utils.byteToByte(bi.toByteArray()));
- Collections.reverse(bs);
- final BitSet bitSetOriginal = BitSet.valueOf(Utils.byteToByte(bs.toArray(new Byte[0])));
- BitSet bitSet = new BitSet(bs.size() * 16);
- int k = 0;
- for (int j = 0; j < bs.size() * 8; j++) {
- if (j == 0 || j % 7 != 0) {
- bitSet.set(k++, bitSetOriginal.get(j));
- } else {
- bitSet.set(k++, j != 7);
- bitSet.set(k++, bitSetOriginal.get(j));
- }
- }
- byte[] bs1 = bitSet.toByteArray();
- List<Byte> res =
- Arrays.asList(Utils.byteToByte(bs1));
- Collections.reverse(res);
- return res;
- })
+ .map(ObjectIdentifier::encodeSingleInt)
.flatMap(Collection::stream)
.collect(Collectors.toList())
).flatMap(Collection::stream)
.toArray(Byte[]::new);
}
+ /**
+ * EFFECTS: Encode a single int component into OID-format bytes.
+ */
+ private static List<Byte> encodeSingleInt(int i) {
+ BigInteger bi = BigInteger.valueOf(i);
+ List<Byte> bs = Arrays.asList(Utils.byteToByte(bi.toByteArray()));
+ Collections.reverse(bs);
+ final BitSet bitSetOriginal = BitSet.valueOf(Utils.byteToByte(bs.toArray(new Byte[0])));
+ BitSet bitSet = new BitSet(bs.size() * 16);
+ int k = 0;
+ for (int j = 0; j < bs.size() * 8; j++) {
+ if (j == 0 || j % 7 != 0) {
+ bitSet.set(k++, bitSetOriginal.get(j));
+ } else {
+ bitSet.set(k++, j != 7);
+ bitSet.set(k++, bitSetOriginal.get(j));
+ }
+ }
+ byte[] bs1 = bitSet.toByteArray();
+ List<Byte> res =
+ Arrays.asList(Utils.byteToByte(bs1));
+ Collections.reverse(res);
+ return res;
+ }
+
public Integer[] getInts() {
return ints;
}