001: package org.bouncycastle.asn1.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DEREncodable;
008: import org.bouncycastle.asn1.DERObject;
009: import org.bouncycastle.asn1.DERObjectIdentifier;
010: import org.bouncycastle.asn1.DERSequence;
011:
012: public class AlgorithmIdentifier extends ASN1Encodable {
013: private DERObjectIdentifier objectId;
014: private DEREncodable parameters;
015: private boolean parametersDefined = false;
016:
017: public static AlgorithmIdentifier getInstance(ASN1TaggedObject obj,
018: boolean explicit) {
019: return getInstance(ASN1Sequence.getInstance(obj, explicit));
020: }
021:
022: public static AlgorithmIdentifier getInstance(Object obj) {
023: if (obj == null || obj instanceof AlgorithmIdentifier) {
024: return (AlgorithmIdentifier) obj;
025: }
026:
027: if (obj instanceof DERObjectIdentifier) {
028: return new AlgorithmIdentifier((DERObjectIdentifier) obj);
029: }
030:
031: if (obj instanceof String) {
032: return new AlgorithmIdentifier((String) obj);
033: }
034:
035: if (obj instanceof ASN1Sequence) {
036: return new AlgorithmIdentifier((ASN1Sequence) obj);
037: }
038:
039: throw new IllegalArgumentException("unknown object in factory");
040: }
041:
042: public AlgorithmIdentifier(DERObjectIdentifier objectId) {
043: this .objectId = objectId;
044: }
045:
046: public AlgorithmIdentifier(String objectId) {
047: this .objectId = new DERObjectIdentifier(objectId);
048: }
049:
050: public AlgorithmIdentifier(DERObjectIdentifier objectId,
051: DEREncodable parameters) {
052: parametersDefined = true;
053: this .objectId = objectId;
054: this .parameters = parameters;
055: }
056:
057: public AlgorithmIdentifier(ASN1Sequence seq) {
058: if (seq.size() < 1 || seq.size() > 2) {
059: throw new IllegalArgumentException("Bad sequence size: "
060: + seq.size());
061: }
062:
063: objectId = DERObjectIdentifier.getInstance(seq.getObjectAt(0));
064:
065: if (seq.size() == 2) {
066: parametersDefined = true;
067: parameters = seq.getObjectAt(1);
068: } else {
069: parameters = null;
070: }
071: }
072:
073: public DERObjectIdentifier getObjectId() {
074: return objectId;
075: }
076:
077: public DEREncodable getParameters() {
078: return parameters;
079: }
080:
081: /**
082: * Produce an object suitable for an ASN1OutputStream.
083: * <pre>
084: * AlgorithmIdentifier ::= SEQUENCE {
085: * algorithm OBJECT IDENTIFIER,
086: * parameters ANY DEFINED BY algorithm OPTIONAL }
087: * </pre>
088: */
089: public DERObject toASN1Object() {
090: ASN1EncodableVector v = new ASN1EncodableVector();
091:
092: v.add(objectId);
093:
094: if (parametersDefined) {
095: v.add(parameters);
096: }
097:
098: return new DERSequence(v);
099: }
100: }
|