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.DERNull; 11 import org.bouncycastle.asn1.DERSequence; 12 13 public class AlgorithmIdentifier 14 extends ASN1Object 15 { 16 private ASN1ObjectIdentifier objectId; 17 private ASN1Encodable parameters; 18 private boolean parametersDefined = false; 19 getInstance( ASN1TaggedObject obj, boolean explicit)20 public static AlgorithmIdentifier getInstance( 21 ASN1TaggedObject obj, 22 boolean explicit) 23 { 24 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 25 } 26 getInstance( Object obj)27 public static AlgorithmIdentifier getInstance( 28 Object obj) 29 { 30 if (obj== null || obj instanceof AlgorithmIdentifier) 31 { 32 return (AlgorithmIdentifier)obj; 33 } 34 35 // TODO: delete 36 if (obj instanceof ASN1ObjectIdentifier) 37 { 38 return new AlgorithmIdentifier((ASN1ObjectIdentifier)obj); 39 } 40 41 // TODO: delete 42 if (obj instanceof String) 43 { 44 return new AlgorithmIdentifier((String)obj); 45 } 46 47 return new AlgorithmIdentifier(ASN1Sequence.getInstance(obj)); 48 } 49 AlgorithmIdentifier( ASN1ObjectIdentifier objectId)50 public AlgorithmIdentifier( 51 ASN1ObjectIdentifier objectId) 52 { 53 this.objectId = objectId; 54 } 55 56 /** 57 * @deprecated use ASN1ObjectIdentifier 58 * @param objectId 59 */ AlgorithmIdentifier( String objectId)60 public AlgorithmIdentifier( 61 String objectId) 62 { 63 this.objectId = new ASN1ObjectIdentifier(objectId); 64 } 65 AlgorithmIdentifier( ASN1ObjectIdentifier objectId, ASN1Encodable parameters)66 public AlgorithmIdentifier( 67 ASN1ObjectIdentifier objectId, 68 ASN1Encodable parameters) 69 { 70 parametersDefined = true; 71 this.objectId = objectId; 72 this.parameters = parameters; 73 } 74 75 /** 76 * @deprecated use AlgorithmIdentifier.getInstance() 77 * @param seq 78 */ AlgorithmIdentifier( ASN1Sequence seq)79 public AlgorithmIdentifier( 80 ASN1Sequence seq) 81 { 82 if (seq.size() < 1 || seq.size() > 2) 83 { 84 throw new IllegalArgumentException("Bad sequence size: " 85 + seq.size()); 86 } 87 88 objectId = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0)); 89 90 if (seq.size() == 2) 91 { 92 parametersDefined = true; 93 parameters = seq.getObjectAt(1); 94 } 95 else 96 { 97 parameters = null; 98 } 99 } 100 getAlgorithm()101 public ASN1ObjectIdentifier getAlgorithm() 102 { 103 return new ASN1ObjectIdentifier(objectId.getId()); 104 } 105 106 /** 107 * @deprecated use getAlgorithm 108 * @return 109 */ getObjectId()110 public ASN1ObjectIdentifier getObjectId() 111 { 112 return objectId; 113 } 114 getParameters()115 public ASN1Encodable getParameters() 116 { 117 return parameters; 118 } 119 120 /** 121 * Produce an object suitable for an ASN1OutputStream. 122 * <pre> 123 * AlgorithmIdentifier ::= SEQUENCE { 124 * algorithm OBJECT IDENTIFIER, 125 * parameters ANY DEFINED BY algorithm OPTIONAL } 126 * </pre> 127 */ toASN1Primitive()128 public ASN1Primitive toASN1Primitive() 129 { 130 ASN1EncodableVector v = new ASN1EncodableVector(); 131 132 v.add(objectId); 133 134 if (parametersDefined) 135 { 136 if (parameters != null) 137 { 138 v.add(parameters); 139 } 140 else 141 { 142 v.add(DERNull.INSTANCE); 143 } 144 } 145 146 return new DERSequence(v); 147 } 148 } 149