001: package org.bouncycastle.asn1.cmp;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1OctetString;
006: import org.bouncycastle.asn1.ASN1Sequence;
007: import org.bouncycastle.asn1.ASN1TaggedObject;
008: import org.bouncycastle.asn1.DERGeneralizedTime;
009: import org.bouncycastle.asn1.DERInteger;
010: import org.bouncycastle.asn1.DERObject;
011: import org.bouncycastle.asn1.DERSequence;
012: import org.bouncycastle.asn1.DERTaggedObject;
013: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
014: import org.bouncycastle.asn1.x509.GeneralName;
015:
016: import java.util.Enumeration;
017:
018: public class PKIHeader extends ASN1Encodable {
019: private DERInteger pvno;
020: private GeneralName sender;
021: private GeneralName recipient;
022: private DERGeneralizedTime messageTime;
023: private AlgorithmIdentifier protectionAlg;
024: private ASN1OctetString senderKID; // KeyIdentifier
025: private ASN1OctetString recipKID; // KeyIdentifier
026: private ASN1OctetString transactionID;
027: private ASN1OctetString senderNonce;
028: private ASN1OctetString recipNonce;
029: private PKIFreeText freeText;
030: private ASN1Sequence generalInfo;
031:
032: private PKIHeader(ASN1Sequence seq) {
033: Enumeration en = seq.getObjects();
034:
035: pvno = DERInteger.getInstance(en.nextElement());
036: sender = GeneralName.getInstance(en.nextElement());
037: recipient = GeneralName.getInstance(en.nextElement());
038:
039: while (en.hasMoreElements()) {
040: ASN1TaggedObject tObj = (ASN1TaggedObject) en.nextElement();
041:
042: switch (tObj.getTagNo()) {
043: case 0:
044: messageTime = DERGeneralizedTime
045: .getInstance(tObj, true);
046: break;
047: case 1:
048: protectionAlg = AlgorithmIdentifier.getInstance(tObj,
049: true);
050: break;
051: case 2:
052: senderKID = ASN1OctetString.getInstance(tObj, true);
053: break;
054: case 3:
055: recipKID = ASN1OctetString.getInstance(tObj, true);
056: break;
057: case 4:
058: transactionID = ASN1OctetString.getInstance(tObj, true);
059: break;
060: case 5:
061: senderNonce = ASN1OctetString.getInstance(tObj, true);
062: break;
063: case 6:
064: recipNonce = ASN1OctetString.getInstance(tObj, true);
065: break;
066: case 7:
067: freeText = PKIFreeText.getInstance(tObj, true);
068: break;
069: case 8:
070: generalInfo = ASN1Sequence.getInstance(tObj, true);
071: break;
072: default:
073: throw new IllegalArgumentException(
074: "unknown tag number: " + tObj.getTagNo());
075: }
076: }
077: }
078:
079: public static PKIHeader getInstance(Object o) {
080: if (o instanceof PKIHeader) {
081: return (PKIHeader) o;
082: }
083:
084: if (o instanceof ASN1Sequence) {
085: return new PKIHeader((ASN1Sequence) o);
086: }
087:
088: throw new IllegalArgumentException("Invalid object: "
089: + o.getClass().getName());
090: }
091:
092: public DERInteger getPvno() {
093: return pvno;
094: }
095:
096: public GeneralName getSender() {
097: return sender;
098: }
099:
100: public GeneralName getRecipient() {
101: return recipient;
102: }
103:
104: /**
105: * <pre>
106: * PKIHeader ::= SEQUENCE {
107: * pvno INTEGER { cmp1999(1), cmp2000(2) },
108: * sender GeneralName,
109: * -- identifies the sender
110: * recipient GeneralName,
111: * -- identifies the intended recipient
112: * messageTime [0] GeneralizedTime OPTIONAL,
113: * -- time of production of this message (used when sender
114: * -- believes that the transport will be "suitable"; i.e.,
115: * -- that the time will still be meaningful upon receipt)
116: * protectionAlg [1] AlgorithmIdentifier OPTIONAL,
117: * -- algorithm used for calculation of protection bits
118: * senderKID [2] KeyIdentifier OPTIONAL,
119: * recipKID [3] KeyIdentifier OPTIONAL,
120: * -- to identify specific keys used for protection
121: * transactionID [4] OCTET STRING OPTIONAL,
122: * -- identifies the transaction; i.e., this will be the same in
123: * -- corresponding request, response, certConf, and PKIConf
124: * -- messages
125: * senderNonce [5] OCTET STRING OPTIONAL,
126: * recipNonce [6] OCTET STRING OPTIONAL,
127: * -- nonces used to provide replay protection, senderNonce
128: * -- is inserted by the creator of this message; recipNonce
129: * -- is a nonce previously inserted in a related message by
130: * -- the intended recipient of this message
131: * freeText [7] PKIFreeText OPTIONAL,
132: * -- this may be used to indicate context-specific instructions
133: * -- (this field is intended for human consumption)
134: * generalInfo [8] SEQUENCE SIZE (1..MAX) OF
135: * InfoTypeAndValue OPTIONAL
136: * -- this may be used to convey context-specific information
137: * -- (this field not primarily intended for human consumption)
138: * }
139: * </pre>
140: * @return a basic ASN.1 object representation.
141: */
142: public DERObject toASN1Object() {
143: ASN1EncodableVector v = new ASN1EncodableVector();
144:
145: v.add(pvno);
146: v.add(sender);
147: v.add(recipient);
148: addOptional(v, 0, messageTime);
149: addOptional(v, 1, protectionAlg);
150: addOptional(v, 2, senderKID);
151: addOptional(v, 3, recipKID);
152: addOptional(v, 4, transactionID);
153: addOptional(v, 5, senderNonce);
154: addOptional(v, 6, recipNonce);
155: addOptional(v, 7, freeText);
156: addOptional(v, 8, generalInfo);
157:
158: return new DERSequence(v);
159: }
160:
161: private void addOptional(ASN1EncodableVector v, int tagNo,
162: ASN1Encodable obj) {
163: if (obj != null) {
164: v.add(new DERTaggedObject(true, tagNo, obj));
165: }
166: }
167: }
|