diff --git a/core/src/main/java/org/bouncycastle/asn1/ASN1Set.java b/core/src/main/java/org/bouncycastle/asn1/ASN1Set.java index 078c78871e..8cd3ad583b 100644 --- a/core/src/main/java/org/bouncycastle/asn1/ASN1Set.java +++ b/core/src/main/java/org/bouncycastle/asn1/ASN1Set.java @@ -166,14 +166,13 @@ public static ASN1Set getInstance(ASN1TaggedObject taggedObject, boolean explici } protected final ASN1Encodable[] elements; - protected final boolean isSorted; protected ASN1Encodable[] sortedElements; protected ASN1Set() { this.elements = ASN1EncodableVector.EMPTY_ELEMENTS; - this.isSorted = true; + this.sortedElements = elements; } /** @@ -188,7 +187,7 @@ protected ASN1Set(ASN1Encodable element) } this.elements = new ASN1Encodable[]{ element }; - this.isSorted = true; + this.sortedElements = elements; } /** @@ -215,7 +214,7 @@ protected ASN1Set(ASN1EncodableVector elementVector, boolean doSort) } this.elements = tmp; - this.isSorted = doSort || tmp.length < 2; + this.sortedElements = (doSort || tmp.length < 2) ? elements : null; } /** @@ -237,13 +236,19 @@ protected ASN1Set(ASN1Encodable[] elements, boolean doSort) } this.elements = tmp; - this.isSorted = doSort || tmp.length < 2; + this.sortedElements = (doSort || tmp.length < 2) ? elements : null; } ASN1Set(boolean isSorted, ASN1Encodable[] elements) { this.elements = elements; - this.isSorted = isSorted || elements.length < 2; + this.sortedElements = (isSorted || elements.length < 2) ? elements : null; + } + + ASN1Set(ASN1Encodable[] elements, ASN1Encodable[] sortedElements) + { + this.elements = elements; + this.sortedElements = sortedElements; } public Enumeration getObjects() @@ -355,22 +360,13 @@ public int hashCode() */ ASN1Primitive toDERObject() { - ASN1Encodable[] tmp; - if (isSorted) + if (sortedElements == null) { - tmp = elements; - } - else - { - if (sortedElements == null) - { - sortedElements = (ASN1Encodable[])elements.clone(); - sort(sortedElements); - } - tmp = sortedElements; + sortedElements = (ASN1Encodable[])elements.clone(); + sort(sortedElements); } - return new DERSet(true, tmp); + return new DERSet(true, sortedElements); } /** @@ -379,7 +375,7 @@ ASN1Primitive toDERObject() */ ASN1Primitive toDLObject() { - return new DLSet(isSorted, elements); + return new DLSet(elements, sortedElements); } boolean asn1Equals(ASN1Primitive other) @@ -478,8 +474,8 @@ private static boolean lessThanOrEqual(byte[] a, byte[] b) * primitive form accordingly. Failing to ignore the CONSTRUCTED bit could therefore lead to * ordering inversions. */ - int a0 = a[0] & ~BERTags.CONSTRUCTED & 0xFF; - int b0 = b[0] & ~BERTags.CONSTRUCTED & 0xFF; + int a0 = a[0] & (~BERTags.CONSTRUCTED & 0xff); + int b0 = b[0] & (~BERTags.CONSTRUCTED & 0xff); if (a0 != b0) { return a0 < b0; diff --git a/core/src/main/java/org/bouncycastle/asn1/DERSet.java b/core/src/main/java/org/bouncycastle/asn1/DERSet.java index 7aa5c791a2..f86689b70f 100644 --- a/core/src/main/java/org/bouncycastle/asn1/DERSet.java +++ b/core/src/main/java/org/bouncycastle/asn1/DERSet.java @@ -134,7 +134,7 @@ void encode(ASN1OutputStream out, boolean withTag) throws IOException ASN1Primitive toDERObject() { - return isSorted ? this : super.toDERObject(); + return (sortedElements != null) ? this : super.toDERObject(); } ASN1Primitive toDLObject() diff --git a/core/src/main/java/org/bouncycastle/asn1/DLSet.java b/core/src/main/java/org/bouncycastle/asn1/DLSet.java index ab18324dab..695b1e36b0 100644 --- a/core/src/main/java/org/bouncycastle/asn1/DLSet.java +++ b/core/src/main/java/org/bouncycastle/asn1/DLSet.java @@ -91,6 +91,11 @@ public DLSet(ASN1Encodable[] elements) super(isSorted, elements); } + DLSet(ASN1Encodable[] elements, ASN1Encodable[] sortedElements) + { + super(elements, sortedElements); + } + private int getContentsLength() throws IOException { if (contentsLength < 0)