1 package org.bouncycastle.asn1.x509; 2 3 import org.bouncycastle.asn1.ASN1Encodable; 4 import org.bouncycastle.asn1.ASN1EncodableVector; 5 import org.bouncycastle.asn1.ASN1Object; 6 import org.bouncycastle.asn1.ASN1ObjectIdentifier; 7 import org.bouncycastle.asn1.ASN1Primitive; 8 import org.bouncycastle.asn1.ASN1Sequence; 9 import org.bouncycastle.asn1.ASN1TaggedObject; 10 import org.bouncycastle.asn1.DERSequence; 11 12 public class AlgorithmIdentifier 13 extends ASN1Object 14 { 15 private ASN1ObjectIdentifier algorithm; 16 private ASN1Encodable parameters; 17 getInstance( ASN1TaggedObject obj, boolean explicit)18 public static AlgorithmIdentifier getInstance( 19 ASN1TaggedObject obj, 20 boolean explicit) 21 { 22 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 23 } 24 getInstance( Object obj)25 public static AlgorithmIdentifier getInstance( 26 Object obj) 27 { 28 if (obj instanceof AlgorithmIdentifier) 29 { 30 return (AlgorithmIdentifier)obj; 31 } 32 else if (obj != null) 33 { 34 return new AlgorithmIdentifier(ASN1Sequence.getInstance(obj)); 35 } 36 37 return null; 38 } 39 AlgorithmIdentifier( ASN1ObjectIdentifier algorithm)40 public AlgorithmIdentifier( 41 ASN1ObjectIdentifier algorithm) 42 { 43 this.algorithm = algorithm; 44 } 45 AlgorithmIdentifier( ASN1ObjectIdentifier algorithm, ASN1Encodable parameters)46 public AlgorithmIdentifier( 47 ASN1ObjectIdentifier algorithm, 48 ASN1Encodable parameters) 49 { 50 this.algorithm = algorithm; 51 this.parameters = parameters; 52 } 53 AlgorithmIdentifier( ASN1Sequence seq)54 private AlgorithmIdentifier( 55 ASN1Sequence seq) 56 { 57 if (seq.size() < 1 || seq.size() > 2) 58 { 59 throw new IllegalArgumentException("Bad sequence size: " 60 + seq.size()); 61 } 62 63 algorithm = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0)); 64 65 if (seq.size() == 2) 66 { 67 parameters = seq.getObjectAt(1); 68 } 69 else 70 { 71 parameters = null; 72 } 73 } 74 getAlgorithm()75 public ASN1ObjectIdentifier getAlgorithm() 76 { 77 return algorithm; 78 } 79 getParameters()80 public ASN1Encodable getParameters() 81 { 82 return parameters; 83 } 84 85 /** 86 * Produce an object suitable for an ASN1OutputStream. 87 * <pre> 88 * AlgorithmIdentifier ::= SEQUENCE { 89 * algorithm OBJECT IDENTIFIER, 90 * parameters ANY DEFINED BY algorithm OPTIONAL } 91 * </pre> 92 */ toASN1Primitive()93 public ASN1Primitive toASN1Primitive() 94 { 95 ASN1EncodableVector v = new ASN1EncodableVector(); 96 97 v.add(algorithm); 98 99 if (parameters != null) 100 { 101 v.add(parameters); 102 } 103 104 return new DERSequence(v); 105 } 106 } 107