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