1 20 package com.novosec.pkix.asn1.crmf; 21 22 import java.io.ByteArrayOutputStream ; 23 24 import org.bouncycastle.asn1.ASN1EncodableVector; 25 import org.bouncycastle.asn1.ASN1Sequence; 26 import org.bouncycastle.asn1.ASN1TaggedObject; 27 import org.bouncycastle.asn1.DEREncodable; 28 import org.bouncycastle.asn1.DERObject; 29 import org.bouncycastle.asn1.DERObjectIdentifier; 30 import org.bouncycastle.asn1.DEROutputStream; 31 import org.bouncycastle.asn1.DERSequence; 32 33 42 public class AttributeTypeAndValue implements DEREncodable 43 { 44 private DERObjectIdentifier type; 45 private DEREncodable value; 46 47 public static AttributeTypeAndValue getInstance( 48 ASN1TaggedObject obj, 49 boolean explicit) 50 { 51 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 52 } 53 54 public static AttributeTypeAndValue getInstance( 55 Object obj) 56 { 57 if (obj instanceof AttributeTypeAndValue) 58 { 59 return (AttributeTypeAndValue)obj; 60 } 61 62 if (obj instanceof DERObjectIdentifier) 63 { 64 return new AttributeTypeAndValue((DERObjectIdentifier)obj); 65 } 66 67 if (obj instanceof String ) 68 { 69 return new AttributeTypeAndValue((String )obj); 70 } 71 72 if (obj instanceof ASN1Sequence) 73 { 74 return new AttributeTypeAndValue((ASN1Sequence)obj); 75 } 76 77 throw new IllegalArgumentException ("unknown object in factory"); 78 } 79 80 public AttributeTypeAndValue( 81 DERObjectIdentifier type) 82 { 83 this.type = type; 84 } 85 86 public AttributeTypeAndValue( 87 String type) 88 { 89 this.type = new DERObjectIdentifier(type); 90 } 91 92 public AttributeTypeAndValue(DERObjectIdentifier type, DEREncodable value ) 93 { 94 this.type = type; 95 this.value = value; 96 } 97 98 public AttributeTypeAndValue(ASN1Sequence seq) 99 { 100 type = (DERObjectIdentifier)seq.getObjectAt(0); 101 value = seq.getObjectAt(1); 102 } 103 104 public DERObjectIdentifier getObjectId() 105 { 106 return type; 107 } 108 109 public DEREncodable getParameters() 110 { 111 return value; 112 } 113 114 public DERObject getDERObject() 115 { 116 ASN1EncodableVector v = new ASN1EncodableVector(); 117 118 v.add(type); 119 v.add(value); 120 121 return new DERSequence(v); 122 } 123 124 public boolean equals( Object o ) 125 { 126 if ((o == null) || !(o instanceof AttributeTypeAndValue)) 127 { 128 return false; 129 } 130 131 AttributeTypeAndValue other = (AttributeTypeAndValue)o; 132 133 if (!this.getObjectId().equals(other.getObjectId())) 134 { 135 return false; 136 } 137 138 if (this.getParameters() == null && other.getParameters() == null) 139 { 140 return true; 141 } 142 143 if (this.getParameters() == null || other.getParameters() == null) 144 { 145 return false; 146 } 147 148 ByteArrayOutputStream b1Out = new ByteArrayOutputStream (); 149 ByteArrayOutputStream b2Out = new ByteArrayOutputStream (); 150 DEROutputStream d1Out = new DEROutputStream(b1Out); 151 DEROutputStream d2Out = new DEROutputStream(b2Out); 152 153 try 154 { 155 d1Out.writeObject(this.getParameters()); 156 d2Out.writeObject(other.getParameters()); 157 158 byte[] b1 = b1Out.toByteArray(); 159 byte[] b2 = b2Out.toByteArray(); 160 161 if (b1.length != b2.length) 162 { 163 return false; 164 } 165 166 for (int i = 0; i != b1.length; i++) 167 { 168 if (b1[i] != b2[i]) 169 { 170 return false; 171 } 172 } 173 } 174 catch (Exception e) 175 { 176 return false; 177 } 178 179 return true; 180 } 181 } 182 | Popular Tags |