1 package org.bouncycastle.asn1.cmp; 2 3 import java.util.Enumeration; 4 5 import org.bouncycastle.asn1.ASN1Encodable; 6 import org.bouncycastle.asn1.ASN1EncodableVector; 7 import org.bouncycastle.asn1.ASN1GeneralizedTime; 8 import org.bouncycastle.asn1.ASN1Integer; 9 import org.bouncycastle.asn1.ASN1Object; 10 import org.bouncycastle.asn1.ASN1OctetString; 11 import org.bouncycastle.asn1.ASN1Primitive; 12 import org.bouncycastle.asn1.ASN1Sequence; 13 import org.bouncycastle.asn1.ASN1TaggedObject; 14 import org.bouncycastle.asn1.DERSequence; 15 import org.bouncycastle.asn1.DERTaggedObject; 16 import org.bouncycastle.asn1.x500.X500Name; 17 import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 18 import org.bouncycastle.asn1.x509.GeneralName; 19 20 public class PKIHeader 21 extends ASN1Object 22 { 23 /** 24 * Value for a "null" recipient or sender. 25 */ 26 public static final GeneralName NULL_NAME = new GeneralName(X500Name.getInstance(new DERSequence())); 27 28 public static final int CMP_1999 = 1; 29 public static final int CMP_2000 = 2; 30 31 private ASN1Integer pvno; 32 private GeneralName sender; 33 private GeneralName recipient; 34 private ASN1GeneralizedTime messageTime; 35 private AlgorithmIdentifier protectionAlg; 36 private ASN1OctetString senderKID; // KeyIdentifier 37 private ASN1OctetString recipKID; // KeyIdentifier 38 private ASN1OctetString transactionID; 39 private ASN1OctetString senderNonce; 40 private ASN1OctetString recipNonce; 41 private PKIFreeText freeText; 42 private ASN1Sequence generalInfo; 43 PKIHeader(ASN1Sequence seq)44 private PKIHeader(ASN1Sequence seq) 45 { 46 Enumeration en = seq.getObjects(); 47 48 pvno = ASN1Integer.getInstance(en.nextElement()); 49 sender = GeneralName.getInstance(en.nextElement()); 50 recipient = GeneralName.getInstance(en.nextElement()); 51 52 while (en.hasMoreElements()) 53 { 54 ASN1TaggedObject tObj = (ASN1TaggedObject)en.nextElement(); 55 56 switch (tObj.getTagNo()) 57 { 58 case 0: 59 messageTime = ASN1GeneralizedTime.getInstance(tObj, true); 60 break; 61 case 1: 62 protectionAlg = AlgorithmIdentifier.getInstance(tObj, true); 63 break; 64 case 2: 65 senderKID = ASN1OctetString.getInstance(tObj, true); 66 break; 67 case 3: 68 recipKID = ASN1OctetString.getInstance(tObj, true); 69 break; 70 case 4: 71 transactionID = ASN1OctetString.getInstance(tObj, true); 72 break; 73 case 5: 74 senderNonce = ASN1OctetString.getInstance(tObj, true); 75 break; 76 case 6: 77 recipNonce = ASN1OctetString.getInstance(tObj, true); 78 break; 79 case 7: 80 freeText = PKIFreeText.getInstance(tObj, true); 81 break; 82 case 8: 83 generalInfo = ASN1Sequence.getInstance(tObj, true); 84 break; 85 default: 86 throw new IllegalArgumentException("unknown tag number: " + tObj.getTagNo()); 87 } 88 } 89 } 90 getInstance(Object o)91 public static PKIHeader getInstance(Object o) 92 { 93 if (o instanceof PKIHeader) 94 { 95 return (PKIHeader)o; 96 } 97 98 if (o != null) 99 { 100 return new PKIHeader(ASN1Sequence.getInstance(o)); 101 } 102 103 return null; 104 } 105 PKIHeader( int pvno, GeneralName sender, GeneralName recipient)106 public PKIHeader( 107 int pvno, 108 GeneralName sender, 109 GeneralName recipient) 110 { 111 this(new ASN1Integer(pvno), sender, recipient); 112 } 113 PKIHeader( ASN1Integer pvno, GeneralName sender, GeneralName recipient)114 private PKIHeader( 115 ASN1Integer pvno, 116 GeneralName sender, 117 GeneralName recipient) 118 { 119 this.pvno = pvno; 120 this.sender = sender; 121 this.recipient = recipient; 122 } 123 getPvno()124 public ASN1Integer getPvno() 125 { 126 return pvno; 127 } 128 getSender()129 public GeneralName getSender() 130 { 131 return sender; 132 } 133 getRecipient()134 public GeneralName getRecipient() 135 { 136 return recipient; 137 } 138 getMessageTime()139 public ASN1GeneralizedTime getMessageTime() 140 { 141 return messageTime; 142 } 143 getProtectionAlg()144 public AlgorithmIdentifier getProtectionAlg() 145 { 146 return protectionAlg; 147 } 148 getSenderKID()149 public ASN1OctetString getSenderKID() 150 { 151 return senderKID; 152 } 153 getRecipKID()154 public ASN1OctetString getRecipKID() 155 { 156 return recipKID; 157 } 158 getTransactionID()159 public ASN1OctetString getTransactionID() 160 { 161 return transactionID; 162 } 163 getSenderNonce()164 public ASN1OctetString getSenderNonce() 165 { 166 return senderNonce; 167 } 168 getRecipNonce()169 public ASN1OctetString getRecipNonce() 170 { 171 return recipNonce; 172 } 173 getFreeText()174 public PKIFreeText getFreeText() 175 { 176 return freeText; 177 } 178 getGeneralInfo()179 public InfoTypeAndValue[] getGeneralInfo() 180 { 181 if (generalInfo == null) 182 { 183 return null; 184 } 185 InfoTypeAndValue[] results = new InfoTypeAndValue[generalInfo.size()]; 186 for (int i = 0; i < results.length; i++) 187 { 188 results[i] 189 = InfoTypeAndValue.getInstance(generalInfo.getObjectAt(i)); 190 } 191 return results; 192 } 193 194 /** 195 * <pre> 196 * PKIHeader ::= SEQUENCE { 197 * pvno INTEGER { cmp1999(1), cmp2000(2) }, 198 * sender GeneralName, 199 * -- identifies the sender 200 * recipient GeneralName, 201 * -- identifies the intended recipient 202 * messageTime [0] GeneralizedTime OPTIONAL, 203 * -- time of production of this message (used when sender 204 * -- believes that the transport will be "suitable"; i.e., 205 * -- that the time will still be meaningful upon receipt) 206 * protectionAlg [1] AlgorithmIdentifier OPTIONAL, 207 * -- algorithm used for calculation of protection bits 208 * senderKID [2] KeyIdentifier OPTIONAL, 209 * recipKID [3] KeyIdentifier OPTIONAL, 210 * -- to identify specific keys used for protection 211 * transactionID [4] OCTET STRING OPTIONAL, 212 * -- identifies the transaction; i.e., this will be the same in 213 * -- corresponding request, response, certConf, and PKIConf 214 * -- messages 215 * senderNonce [5] OCTET STRING OPTIONAL, 216 * recipNonce [6] OCTET STRING OPTIONAL, 217 * -- nonces used to provide replay protection, senderNonce 218 * -- is inserted by the creator of this message; recipNonce 219 * -- is a nonce previously inserted in a related message by 220 * -- the intended recipient of this message 221 * freeText [7] PKIFreeText OPTIONAL, 222 * -- this may be used to indicate context-specific instructions 223 * -- (this field is intended for human consumption) 224 * generalInfo [8] SEQUENCE SIZE (1..MAX) OF 225 * InfoTypeAndValue OPTIONAL 226 * -- this may be used to convey context-specific information 227 * -- (this field not primarily intended for human consumption) 228 * } 229 * </pre> 230 * 231 * @return a basic ASN.1 object representation. 232 */ toASN1Primitive()233 public ASN1Primitive toASN1Primitive() 234 { 235 ASN1EncodableVector v = new ASN1EncodableVector(12); 236 237 v.add(pvno); 238 v.add(sender); 239 v.add(recipient); 240 addOptional(v, 0, messageTime); 241 addOptional(v, 1, protectionAlg); 242 addOptional(v, 2, senderKID); 243 addOptional(v, 3, recipKID); 244 addOptional(v, 4, transactionID); 245 addOptional(v, 5, senderNonce); 246 addOptional(v, 6, recipNonce); 247 addOptional(v, 7, freeText); 248 addOptional(v, 8, generalInfo); 249 250 return new DERSequence(v); 251 } 252 addOptional(ASN1EncodableVector v, int tagNo, ASN1Encodable obj)253 private void addOptional(ASN1EncodableVector v, int tagNo, ASN1Encodable obj) 254 { 255 if (obj != null) 256 { 257 v.add(new DERTaggedObject(true, tagNo, obj)); 258 } 259 } 260 } 261