1 20 package com.novosec.pkix.asn1.cmp; 21 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 25 import org.bouncycastle.asn1.ASN1EncodableVector; 26 import org.bouncycastle.asn1.ASN1Sequence; 27 import org.bouncycastle.asn1.ASN1TaggedObject; 28 import org.bouncycastle.asn1.DEREncodable; 29 import org.bouncycastle.asn1.DERGeneralizedTime; 30 import org.bouncycastle.asn1.DERInteger; 31 import org.bouncycastle.asn1.DERObject; 32 import org.bouncycastle.asn1.DEROctetString; 33 import org.bouncycastle.asn1.DERSequence; 34 import org.bouncycastle.asn1.DERTaggedObject; 35 import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 36 import org.bouncycastle.asn1.x509.GeneralName; 37 38 60 public class PKIHeader implements DEREncodable 61 { 62 DERInteger pvno; 63 GeneralName sender; 64 GeneralName recipient; 65 DERGeneralizedTime messageTime; 66 AlgorithmIdentifier protectionAlg; 67 DEROctetString senderKID; 68 DEROctetString recipKID; 69 DEROctetString transactionID; 70 DEROctetString senderNonce; 71 DEROctetString recipNonce; 72 PKIFreeText freeText; 73 Vector generalInfos = new Vector (); 74 75 public static PKIHeader getInstance( ASN1TaggedObject obj, boolean explicit ) 76 { 77 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 78 } 79 80 public static PKIHeader getInstance( Object obj ) 81 { 82 if (obj instanceof PKIHeader) 83 { 84 return (PKIHeader)obj; 85 } 86 else if (obj instanceof ASN1Sequence) 87 { 88 return new PKIHeader((ASN1Sequence)obj); 89 } 90 91 throw new IllegalArgumentException ("unknown object in factory"); 92 } 93 94 public PKIHeader( ASN1Sequence seq ) 95 { 96 Enumeration e = seq.getObjects(); 97 98 pvno = DERInteger.getInstance(e.nextElement()); 99 sender = GeneralName.getInstance(e.nextElement()); 100 recipient = GeneralName.getInstance(e.nextElement()); 101 102 while (e.hasMoreElements()) 103 { 104 ASN1TaggedObject tagObj = (ASN1TaggedObject)e.nextElement(); 105 106 switch (tagObj.getTagNo()) 107 { 108 case 0: messageTime = DERGeneralizedTime.getInstance(tagObj.getObject()); break; 109 case 1: protectionAlg = AlgorithmIdentifier.getInstance(tagObj.getObject()); break; 110 case 2: senderKID = (DEROctetString)DEROctetString.getInstance(tagObj); break; 111 case 3: recipKID = (DEROctetString)DEROctetString.getInstance(tagObj); break; 112 case 4: transactionID = (DEROctetString)DEROctetString.getInstance(tagObj); break; 113 case 5: senderNonce = (DEROctetString)DEROctetString.getInstance(tagObj); break; 114 case 6: recipNonce = (DEROctetString)DEROctetString.getInstance(tagObj); break; 115 case 7: freeText = PKIFreeText.getInstance(tagObj.getObject()); break; 116 case 8: 117 ASN1Sequence s = (ASN1Sequence)tagObj.getObject(); 118 for( int i=0; i<s.size(); i++ ) 119 generalInfos.addElement( InfoTypeAndValue.getInstance(s.getObjectAt(i)) ); 120 break; 121 } 122 } 123 } 124 125 public PKIHeader( DERInteger pvno, GeneralName sender, GeneralName recipient ) 126 { 127 this.pvno = pvno; 128 this.sender = sender; 129 this.recipient = recipient; 130 } 131 132 public DERInteger getPvno() 133 { 134 return pvno; 135 } 136 137 public GeneralName getSender() 138 { 139 return sender; 140 } 141 142 public GeneralName getRecipient() 143 { 144 return recipient; 145 } 146 147 public void setMessageTime( DERGeneralizedTime messageTime ) 148 { 149 this.messageTime = messageTime; 150 } 151 152 public DERGeneralizedTime getMessageTime() 153 { 154 return messageTime; 155 } 156 157 public void setProtectionAlg( AlgorithmIdentifier protectionAlg ) 158 { 159 this.protectionAlg = protectionAlg; 160 } 161 162 public AlgorithmIdentifier getProtectionAlg() 163 { 164 return protectionAlg; 165 } 166 167 public void setSenderKID( DEROctetString senderKID ) 168 { 169 this.senderKID = senderKID; 170 } 171 172 public DEROctetString getSenderKID() 173 { 174 return senderKID; 175 } 176 177 public void setRecipKID( DEROctetString recipKID ) 178 { 179 this.recipKID = recipKID; 180 } 181 182 public DEROctetString getRecipKID() 183 { 184 return recipKID; 185 } 186 187 public void setTransactionID( DEROctetString transactionID ) 188 { 189 this.transactionID = transactionID; 190 } 191 192 public DEROctetString getTransactionID() 193 { 194 return transactionID; 195 } 196 197 public void setSenderNonce( DEROctetString senderNonce ) 198 { 199 this.senderNonce = senderNonce; 200 } 201 202 public DEROctetString getSenderNonce() 203 { 204 return senderNonce; 205 } 206 207 public void setRecipNonce( DEROctetString recipNonce ) 208 { 209 this.recipNonce = recipNonce; 210 } 211 212 public DEROctetString getRecipNonce() 213 { 214 return recipNonce; 215 } 216 217 public void setFreeText( PKIFreeText freeText ) 218 { 219 this.freeText = freeText; 220 } 221 222 public PKIFreeText getFreeText() 223 { 224 return freeText; 225 } 226 227 public void addGeneralInfo( InfoTypeAndValue generalInfo ) 228 { 229 this.generalInfos.addElement(generalInfo); 230 } 231 232 public InfoTypeAndValue getGeneralInfo(int nr) 233 { 234 if (generalInfos.size() > nr) 235 return (InfoTypeAndValue)generalInfos.elementAt(nr); 236 237 return null; 238 } 239 240 public DERObject getDERObject() 241 { 242 ASN1EncodableVector v = new ASN1EncodableVector(); 243 244 v.add( pvno ); 245 v.add( sender ); 246 v.add( recipient ); 247 248 if( messageTime != null ) 249 v.add( new DERTaggedObject( true, 0, messageTime ) ); 250 251 if( protectionAlg != null ) 252 v.add( new DERTaggedObject( true, 1, protectionAlg ) ); 253 254 if( senderKID != null ) 255 v.add( new DERTaggedObject( true, 2, senderKID ) ); 256 257 if( recipKID != null ) 258 v.add( new DERTaggedObject( true, 3, recipKID ) ); 259 260 if( transactionID != null ) 261 v.add( new DERTaggedObject( true, 4, transactionID ) ); 262 263 if( senderNonce != null ) 264 v.add( new DERTaggedObject( true, 5, senderNonce ) ); 265 266 if( recipNonce != null ) 267 v.add( new DERTaggedObject( true, 6, recipNonce ) ); 268 269 if( freeText != null ) 270 v.add( new DERTaggedObject( true, 7, freeText ) ); 271 272 if( generalInfos.size() > 0 ) 273 { 274 ASN1EncodableVector giv = new ASN1EncodableVector(); 275 276 for (int i=0;i<generalInfos.size();i++) 277 giv.add((InfoTypeAndValue)generalInfos.elementAt(i)); 278 279 v.add( new DERTaggedObject( true, 8, new DERSequence(giv) ) ); 280 } 281 282 return new DERSequence(v); 283 } 284 285 public String toString() 286 { 287 String s = "PKIHeader: ( pvno: " + pvno + ", sender: " + sender + ", recipient: " + recipient + ", "; 288 289 if( messageTime != null ) 290 s += "messageTime: " + messageTime + ", "; 291 292 if( protectionAlg != null ) 293 s += "protectionAlg: " + protectionAlg + ", "; 294 295 if( senderKID != null ) 296 s += "senderKID: " + senderKID + ", "; 297 298 if( recipKID != null ) 299 s += "recipKID: " + recipKID + ", "; 300 301 if( transactionID != null ) 302 s += "transactionID: " + transactionID + ", "; 303 304 if( senderNonce != null ) 305 s += "senderNonce: " + senderNonce + ", "; 306 307 if( recipNonce != null ) 308 s += "recipNonce: " + recipNonce + ", "; 309 310 if( freeText != null ) 311 s += "freeText: " + freeText + ", "; 312 313 if( generalInfos.size() > 0 ) 314 { 315 s += "generalInfo: ("; 316 for (int i=0;i<generalInfos.size();i++) 317 s += generalInfos.elementAt(i) + ", "; 318 s += ")"; 319 } 320 321 return s; 322 } 323 } 324 | Popular Tags |