All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.asn1.DERSequence Maven / Gradle / Ivy

Go to download

The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 to JDK 1.8.

There is a newer version: 1.79
Show newest version
package org.bouncycastle.asn1;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Definite length SEQUENCE, encoding tells explicit number of bytes
 * that the content of this sequence occupies.
 * 

* For X.690 syntax rules, see {@link ASN1Sequence}. */ public class DERSequence extends ASN1Sequence { public static DERSequence convert(ASN1Sequence seq) { return (DERSequence)seq.toDERObject(); } private int bodyLength = -1; /** * Create an empty sequence */ public DERSequence() { } /** * Create a sequence containing one object * @param element the object to go in the sequence. */ public DERSequence(ASN1Encodable element) { super(element); } /** * Create a sequence containing a vector of objects. * @param elementVector the vector of objects to make up the sequence. */ public DERSequence(ASN1EncodableVector elementVector) { super(elementVector); } /** * Create a sequence containing an array of objects. * @param elements the array of objects to make up the sequence. */ public DERSequence(ASN1Encodable[] elements) { super(elements); } DERSequence(ASN1Encodable[] elements, boolean clone) { super(elements, clone); } private int getBodyLength() throws IOException { if (bodyLength < 0) { int count = elements.length; int totalLength = 0; for (int i = 0; i < count; ++i) { ASN1Primitive derObject = elements[i].toASN1Primitive().toDERObject(); totalLength += derObject.encodedLength(); } this.bodyLength = totalLength; } return bodyLength; } int encodedLength() throws IOException { int length = getBodyLength(); return 1 + StreamUtil.calculateBodyLength(length) + length; } /* * A note on the implementation: *

* As DER requires the constructed, definite-length model to * be used for structured types, this varies slightly from the * ASN.1 descriptions given. Rather than just outputting SEQUENCE, * we also have to specify CONSTRUCTED, and the objects length. */ void encode(ASN1OutputStream out, boolean withTag) throws IOException { if (withTag) { out.write(BERTags.SEQUENCE | BERTags.CONSTRUCTED); } DEROutputStream derOut = out.getDERSubStream(); int count = elements.length; if (bodyLength >= 0 || count > 16) { out.writeLength(getBodyLength()); for (int i = 0; i < count; ++i) { ASN1Primitive derObject = elements[i].toASN1Primitive().toDERObject(); derObject.encode(derOut, true); } } else { int totalLength = 0; ASN1Primitive[] derObjects = new ASN1Primitive[count]; for (int i = 0; i < count; ++i) { ASN1Primitive derObject = elements[i].toASN1Primitive().toDERObject(); derObjects[i] = derObject; totalLength += derObject.encodedLength(); } this.bodyLength = totalLength; out.writeLength(totalLength); for (int i = 0; i < count; ++i) { derObjects[i].encode(derOut, true); } } } ASN1Primitive toDERObject() { return this; } ASN1Primitive toDLObject() { return this; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy