001: package org.bouncycastle.asn1.icao;
002:
003: import java.util.Enumeration;
004:
005: import org.bouncycastle.asn1.ASN1Encodable;
006: import org.bouncycastle.asn1.ASN1EncodableVector;
007: import org.bouncycastle.asn1.ASN1Sequence;
008: import org.bouncycastle.asn1.DERInteger;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERSequence;
011: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
012:
013: /**
014: * The LDSSecurityObject object.
015: * <pre>
016: * LDSSecurityObject ::= SEQUENCE {
017: * version LDSSecurityObjectVersion,
018: * hashAlgorithm DigestAlgorithmIdentifier,
019: * dataGroupHashValues SEQUENCE SIZE (2..ub-DataGroups) OF DataHashGroup}
020: *
021: * DigestAlgorithmIdentifier ::= AlgorithmIdentifier,
022: *
023: * LDSSecurityObjectVersion :: INTEGER {V0(0)}
024: * </pre>
025: */
026:
027: public class LDSSecurityObject extends ASN1Encodable implements
028: ICAOObjectIdentifiers {
029:
030: public static final int ub_DataGroups = 16;
031:
032: DERInteger version = new DERInteger(0);
033: AlgorithmIdentifier digestAlgorithmIdentifier;
034: DataGroupHash[] datagroupHash;
035:
036: public static LDSSecurityObject getInstance(Object obj) {
037: if (obj == null || obj instanceof LDSSecurityObject) {
038: return (LDSSecurityObject) obj;
039: }
040:
041: if (obj instanceof ASN1Sequence) {
042: return new LDSSecurityObject(ASN1Sequence.getInstance(obj));
043: }
044:
045: throw new IllegalArgumentException(
046: "unknown object in getInstance");
047: }
048:
049: public LDSSecurityObject(ASN1Sequence seq) {
050: if (seq == null || seq.size() == 0) {
051: throw new IllegalArgumentException(
052: "null or empty sequence passed.");
053: }
054:
055: Enumeration e = seq.getObjects();
056:
057: // version
058: version = DERInteger.getInstance(e.nextElement());
059: // digestAlgorithmIdentifier
060: digestAlgorithmIdentifier = AlgorithmIdentifier.getInstance(e
061: .nextElement());
062:
063: ASN1Sequence datagroupHashSeq = ASN1Sequence.getInstance(e
064: .nextElement());
065:
066: checkDatagroupHashSeqSize(datagroupHashSeq.size());
067:
068: datagroupHash = new DataGroupHash[datagroupHashSeq.size()];
069: for (int i = 0; i < datagroupHashSeq.size(); i++) {
070: datagroupHash[i] = DataGroupHash
071: .getInstance(datagroupHashSeq.getObjectAt(i));
072: }
073:
074: }
075:
076: public LDSSecurityObject(
077: AlgorithmIdentifier digestAlgorithmIdentifier,
078: DataGroupHash[] datagroupHash) {
079: this .digestAlgorithmIdentifier = digestAlgorithmIdentifier;
080: this .datagroupHash = datagroupHash;
081:
082: checkDatagroupHashSeqSize(datagroupHash.length);
083: }
084:
085: private void checkDatagroupHashSeqSize(int size) {
086: if ((size < 2) || (size > ub_DataGroups)) {
087: throw new IllegalArgumentException(
088: "wrong size in DataGroupHashValues : not in (2.."
089: + ub_DataGroups + ")");
090: }
091: }
092:
093: public AlgorithmIdentifier getDigestAlgorithmIdentifier() {
094: return digestAlgorithmIdentifier;
095: }
096:
097: public DataGroupHash[] getDatagroupHash() {
098: return datagroupHash;
099: }
100:
101: public DERObject toASN1Object() {
102: ASN1EncodableVector seq = new ASN1EncodableVector();
103:
104: seq.add(version);
105: seq.add(digestAlgorithmIdentifier);
106:
107: ASN1EncodableVector seqname = new ASN1EncodableVector();
108: for (int i = 0; i < datagroupHash.length; i++) {
109: seqname.add(datagroupHash[i]);
110: }
111: seq.add(new DERSequence(seqname));
112:
113: return new DERSequence(seq);
114: }
115: }
|