1 20 package com.novosec.pkix.asn1.cmp; 21 22 import java.util.Vector ; 23 import java.util.Enumeration ; 24 25 import java.io.ByteArrayOutputStream ; 26 27 import org.bouncycastle.asn1.*; 28 import org.bouncycastle.asn1.x509.*; 29 30 43 public class PKIMessage implements DEREncodable 44 { 45 PKIHeader header; 46 PKIBody body; 47 DERBitString protection; 48 Vector extraCerts = new Vector (); 49 byte protectedBytes[]; 50 51 public static PKIMessage getInstance( ASN1TaggedObject obj, boolean explicit ) 52 { 53 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 54 } 55 56 public static PKIMessage getInstance( Object obj ) 57 { 58 if (obj instanceof PKIMessage) 59 { 60 return (PKIMessage)obj; 61 } 62 else if (obj instanceof ASN1Sequence) 63 { 64 return new PKIMessage((ASN1Sequence)obj); 65 } 66 67 throw new IllegalArgumentException ("unknown object in factory"); 68 } 69 70 public PKIMessage( ASN1Sequence seq ) 71 { 72 Enumeration e = seq.getObjects(); 73 74 78 79 DEREncodable derHeader = (DEREncodable)e.nextElement(); 80 DEREncodable derBody = (DEREncodable)e.nextElement(); 81 82 try 83 { 84 ASN1EncodableVector v = new ASN1EncodableVector(); 86 v.add( derHeader ); 87 v.add( derBody ); 88 89 ByteArrayOutputStream bao = new ByteArrayOutputStream (); 90 DEROutputStream out = new DEROutputStream( bao ); 91 out.writeObject( new DERSequence(v) ); 92 93 protectedBytes = bao.toByteArray(); 94 } 95 catch( Exception ex ) {} 96 97 header = PKIHeader.getInstance( derHeader ); 98 body = PKIBody.getInstance( (ASN1TaggedObject)derBody ); 99 100 while (e.hasMoreElements()) 101 { 102 ASN1TaggedObject tagObj = (ASN1TaggedObject)e.nextElement(); 103 104 switch (tagObj.getTagNo()) 105 { 106 case 0: protection = DERBitString.getInstance( tagObj.getObject() ); break; 107 case 1: 108 ASN1Sequence s = (ASN1Sequence)tagObj.getObject(); 109 for( int i=0; i<s.size(); i++ ) 110 extraCerts.addElement( X509CertificateStructure.getInstance(s.getObjectAt(i)) ); 111 break; 112 } 113 } 114 } 115 116 public PKIMessage( PKIHeader header, PKIBody body ) 117 { 118 this.header = header; 119 this.body = body; 120 } 121 122 public PKIHeader getHeader() 123 { 124 return header; 125 } 126 127 public PKIBody getBody() 128 { 129 return body; 130 } 131 132 public void setProtection( DERBitString protection ) 133 { 134 this.protection = protection; 135 } 136 137 public DERBitString getProtection() 138 { 139 return protection; 140 } 141 142 public void addExtraCert( X509CertificateStructure extraCert ) 143 { 144 this.extraCerts.addElement(extraCert); 145 } 146 147 public X509CertificateStructure getExtraCert(int nr) 148 { 149 if (extraCerts.size() > nr) 150 return (X509CertificateStructure)extraCerts.elementAt(nr); 151 152 return null; 153 } 154 155 public DERObject getDERObject() 156 { 157 ASN1EncodableVector v = new ASN1EncodableVector(); 158 159 v.add( header ); 160 v.add( body ); 161 162 if( protection != null ) 163 v.add( new DERTaggedObject( true, 0, protection ) ); 164 165 if( extraCerts.size() > 0 ) 166 { 167 ASN1EncodableVector giv = new ASN1EncodableVector(); 168 169 for (int i=0;i<extraCerts.size();i++) 170 giv.add((X509CertificateStructure)extraCerts.elementAt(i)); 171 172 v.add( new DERTaggedObject( true, 1, new DERSequence(giv) ) ); 173 } 174 175 return new DERSequence(v); 176 } 177 178 public byte[] getProtectedBytes() 179 { 180 if( protectedBytes != null ) 181 return protectedBytes; 182 183 try 184 { 185 ByteArrayOutputStream bao = new ByteArrayOutputStream (); 186 DEROutputStream out = new DEROutputStream( bao ); 187 out.writeObject( getProtectedPart() ); 188 return bao.toByteArray(); 189 } 190 catch(Exception ex){} 191 192 return null; 193 } 194 195 public ProtectedPart getProtectedPart() 196 { 197 return new ProtectedPart( header, body ); 198 } 199 200 public String toString() 201 { 202 String s = "PKIMessage: ( header: " + this.getHeader() + ", body: " + this.getBody() + ", "; 203 204 if( this.getProtection() != null ) 205 s += "protection: "+ this.getProtection() + ", "; 206 207 if( extraCerts.size() > 0 ) 208 { 209 s += "extraCerts: ("; 210 for (int i=0;i<extraCerts.size();i++) 211 s += extraCerts.elementAt(i) + ", "; 212 s += ")"; 213 } 214 215 s += ")"; 216 217 return s; 218 } 219 } 220 | Popular Tags |