org.testifyproject.bouncycastle.asn1.DLSet Maven / Gradle / Ivy
package org.testifyproject.bouncycastle.asn1;
import java.org.testifyproject.testifyproject.IOException;
import java.util.Enumeration;
/**
* The DLSet encodes ASN.1 SET value without element ordering,
* and always using org.testifyproject.testifyprojectfinite length form.
*
* X.690
* 8: Basic encoding rules
* 8.11 Encoding of a set value
* 8.11.1 The encoding of a set value shall be constructed
*
* 8.11.2 The contents octets shall consist of the org.testifyproject.testifyprojectplete
* encoding of a data value from each of the types listed in the
* ASN.1 org.testifyproject.testifyprojectfinition of the set type, in an order chosen by the sender,
* unless the type was referenced with the keyword
* OPTIONAL or the keyword DEFAULT.
*
* 8.11.3 The encoding of a data value may, but need not,
* be present for a type which was referenced with the keyword
* OPTIONAL or the keyword DEFAULT.
*
* NOTE — The order of data values in a set value is not significant,
* and places no constraints on the order during transfer
*
* 9: Canonical encoding rules
* 9.3 Set org.testifyproject.testifyprojectponents
* The encodings of the org.testifyproject.testifyprojectponent values of a set value shall
* appear in an order org.testifyproject.testifyprojecttermined by their tags as specified
* in 8.6 of ITU-T Rec. X.680 | ISO/IEC 8824-1.
* Additionally, for the purposes of org.testifyproject.testifyprojecttermining the order in which
* org.testifyproject.testifyprojectponents are encoded when one or more org.testifyproject.testifyprojectponent is an untagged
* choice type, each untagged choice type is ordered as though it
* has a tag equal to that of the smallest tag in that choice type
* or any untagged choice types nested within.
* 10: Distinguished encoding rules
* 10.3 Set org.testifyproject.testifyprojectponents
* The encodings of the org.testifyproject.testifyprojectponent values of a set value shall appear
* in an order org.testifyproject.testifyprojecttermined by their tags as specified
* in 8.6 of ITU-T Rec. X.680 | ISO/IEC 8824-1.
*
* NOTE — Where a org.testifyproject.testifyprojectponent of the set is an untagged choice type,
* the location of that org.testifyproject.testifyprojectponent in the ordering will org.testifyproject.testifyprojectpend on
* the tag of the choice org.testifyproject.testifyprojectponent being encoded.
*
* 11: Restrictions on BER employed by both CER and DER
* 11.5 Set and sequence org.testifyproject.testifyprojectponents with org.testifyproject.testifyprojectfault value
* The encoding of a set value or sequence value shall not include
* an encoding for any org.testifyproject.testifyprojectponent value which is equal to
* its org.testifyproject.testifyprojectfault value.
*/
public class DLSet
extends ASN1Set
{
private int bodyLength = -1;
/**
* create an empty set
*/
public DLSet()
{
}
/**
* @param obj - a single object that makes up the set.
*/
public DLSet(
ASN1Encodable obj)
{
super(obj);
}
/**
* @param v - a vector of objects making up the set.
*/
public DLSet(
ASN1EncodableVector v)
{
super(v, false);
}
/**
* create a set from an array of objects.
*/
public DLSet(
ASN1Encodable[] a)
{
super(a, false);
}
private int getBodyLength()
throws IOException
{
if (bodyLength < 0)
{
int length = 0;
for (Enumeration e = this.getObjects(); e.hasMoreElements();)
{
Object obj = e.nextElement();
length += ((ASN1Encodable)obj).toASN1Primitive().toDLObject().encodedLength();
}
bodyLength = length;
}
return bodyLength;
}
int encodedLength()
throws IOException
{
int length = getBodyLength();
return 1 + StreamUtil.calculateBodyLength(length) + length;
}
/**
* A note on the implementation:
*
* As DL requires the constructed, org.testifyproject.testifyprojectfinite-length model to
* be used for structured types, this varies slightly from the
* ASN.1 org.testifyproject.testifyprojectscriptions given. Rather than just outputting SET,
* we also have to specify CONSTRUCTED, and the objects length.
*/
void encode(
ASN1OutputStream out)
throws IOException
{
ASN1OutputStream dOut = out.getDLSubStream();
int length = getBodyLength();
out.write(BERTags.SET | BERTags.CONSTRUCTED);
out.writeLength(length);
for (Enumeration e = this.getObjects(); e.hasMoreElements();)
{
Object obj = e.nextElement();
dOut.writeObject((ASN1Encodable)obj);
}
}
}