001: // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
002: //
003: // Author: Maik Stohn
004: //
005: // Permission is hereby granted, free of charge, to any person obtaining a copy of this
006: // software and associated documentation files (the "Software"), to deal in the Software
007: // without restriction, including without limitation the rights to use, copy, modify, merge,
008: // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
009: // to whom the Software is furnished to do so, subject to the following conditions:
010: //
011: // The above copyright notice and this permission notice shall be included in all copies or
012: // substantial portions of the Software.
013: //
014: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
015: // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
016: // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
017: // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
018: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
019:
020: package com.novosec.pkix.asn1.cmp;
021:
022: import java.util.Enumeration;
023: import java.util.Vector;
024:
025: import org.bouncycastle.asn1.ASN1EncodableVector;
026: import org.bouncycastle.asn1.ASN1Sequence;
027: import org.bouncycastle.asn1.ASN1TaggedObject;
028: import org.bouncycastle.asn1.DEREncodable;
029: import org.bouncycastle.asn1.DERGeneralizedTime;
030: import org.bouncycastle.asn1.DERInteger;
031: import org.bouncycastle.asn1.DERObject;
032: import org.bouncycastle.asn1.DEROctetString;
033: import org.bouncycastle.asn1.DERSequence;
034: import org.bouncycastle.asn1.DERTaggedObject;
035: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
036: import org.bouncycastle.asn1.x509.GeneralName;
037:
038: /**
039: * ASN.1 structure DER En/DeCoder.
040: *
041: * <pre>
042: * PKIHeader ::= SEQUENCE {
043: * pvno INTEGER { ietf-version2 (1) },
044: * sender GeneralName, -- identifies the sender
045: * recipient GeneralName, -- identifies the intended recipient
046: * messageTime [0] GeneralizedTime OPTIONAL, -- time of production of this message
047: * protectionAlg [1] AlgorithmIdentifier OPTIONAL, -- algorithm used for calculation of protection bits
048: * senderKID [2] KeyIdentifier OPTIONAL, -- (OCTET STRING)
049: * recipKID [3] KeyIdentifier OPTIONAL, -- (OCTET STRING) to identify specific keys used for protection
050: * transactionID [4] OCTET STRING OPTIONAL, -- identifies the transaction; i.e., this will be the same in corresponding request, response and confirmation messages
051: * senderNonce [5] OCTET STRING OPTIONAL,
052: * recipNonce [6] OCTET STRING OPTIONAL, -- nonces used to provide replay protection, senderNonce is inserted by the creator of this message; recipNonce is a nonce previously inserted in a related message by the intended recipient of this message
053: * freeText [7] PKIFreeText OPTIONAL, -- this may be used to indicate context-specific instructions (this field is intended for human consumption)
054: * generalInfo [8] SEQUENCE SIZE (1..MAX) OF
055: * InfoTypeAndValue OPTIONAL -- this may be used to convey context-specific information (this field not primarily intended for human consumption)
056: * }
057: *
058: * </pre>
059: */
060: public class PKIHeader implements DEREncodable {
061: DERInteger pvno;
062: GeneralName sender;
063: GeneralName recipient;
064: DERGeneralizedTime messageTime;
065: AlgorithmIdentifier protectionAlg;
066: DEROctetString senderKID;
067: DEROctetString recipKID;
068: DEROctetString transactionID;
069: DEROctetString senderNonce;
070: DEROctetString recipNonce;
071: PKIFreeText freeText;
072: Vector generalInfos = new Vector();
073:
074: public static PKIHeader getInstance(ASN1TaggedObject obj,
075: boolean explicit) {
076: return getInstance(ASN1Sequence.getInstance(obj, explicit));
077: }
078:
079: public static PKIHeader getInstance(Object obj) {
080: if (obj instanceof PKIHeader) {
081: return (PKIHeader) obj;
082: } else if (obj instanceof ASN1Sequence) {
083: return new PKIHeader((ASN1Sequence) obj);
084: }
085:
086: throw new IllegalArgumentException("unknown object in factory");
087: }
088:
089: public PKIHeader(ASN1Sequence seq) {
090: Enumeration e = seq.getObjects();
091:
092: pvno = DERInteger.getInstance(e.nextElement());
093: sender = GeneralName.getInstance(e.nextElement());
094: recipient = GeneralName.getInstance(e.nextElement());
095:
096: while (e.hasMoreElements()) {
097: ASN1TaggedObject tagObj = (ASN1TaggedObject) e
098: .nextElement();
099:
100: switch (tagObj.getTagNo()) {
101: case 0:
102: messageTime = DERGeneralizedTime.getInstance(tagObj
103: .getObject());
104: break;
105: case 1:
106: protectionAlg = AlgorithmIdentifier.getInstance(tagObj
107: .getObject());
108: break;
109: case 2:
110: senderKID = (DEROctetString) DEROctetString
111: .getInstance(tagObj);
112: break;
113: case 3:
114: recipKID = (DEROctetString) DEROctetString
115: .getInstance(tagObj);
116: break;
117: case 4:
118: transactionID = (DEROctetString) DEROctetString
119: .getInstance(tagObj);
120: break;
121: case 5:
122: senderNonce = (DEROctetString) DEROctetString
123: .getInstance(tagObj);
124: break;
125: case 6:
126: recipNonce = (DEROctetString) DEROctetString
127: .getInstance(tagObj);
128: break;
129: case 7:
130: freeText = PKIFreeText.getInstance(tagObj.getObject());
131: break;
132: case 8:
133: ASN1Sequence s = (ASN1Sequence) tagObj.getObject();
134: for (int i = 0; i < s.size(); i++)
135: generalInfos.addElement(InfoTypeAndValue
136: .getInstance(s.getObjectAt(i)));
137: break;
138: }
139: }
140: }
141:
142: public PKIHeader(DERInteger pvno, GeneralName sender,
143: GeneralName recipient) {
144: this .pvno = pvno;
145: this .sender = sender;
146: this .recipient = recipient;
147: }
148:
149: public DERInteger getPvno() {
150: return pvno;
151: }
152:
153: public GeneralName getSender() {
154: return sender;
155: }
156:
157: public GeneralName getRecipient() {
158: return recipient;
159: }
160:
161: public void setMessageTime(DERGeneralizedTime messageTime) {
162: this .messageTime = messageTime;
163: }
164:
165: public DERGeneralizedTime getMessageTime() {
166: return messageTime;
167: }
168:
169: public void setProtectionAlg(AlgorithmIdentifier protectionAlg) {
170: this .protectionAlg = protectionAlg;
171: }
172:
173: public AlgorithmIdentifier getProtectionAlg() {
174: return protectionAlg;
175: }
176:
177: public void setSenderKID(DEROctetString senderKID) {
178: this .senderKID = senderKID;
179: }
180:
181: public DEROctetString getSenderKID() {
182: return senderKID;
183: }
184:
185: public void setRecipKID(DEROctetString recipKID) {
186: this .recipKID = recipKID;
187: }
188:
189: public DEROctetString getRecipKID() {
190: return recipKID;
191: }
192:
193: public void setTransactionID(DEROctetString transactionID) {
194: this .transactionID = transactionID;
195: }
196:
197: public DEROctetString getTransactionID() {
198: return transactionID;
199: }
200:
201: public void setSenderNonce(DEROctetString senderNonce) {
202: this .senderNonce = senderNonce;
203: }
204:
205: public DEROctetString getSenderNonce() {
206: return senderNonce;
207: }
208:
209: public void setRecipNonce(DEROctetString recipNonce) {
210: this .recipNonce = recipNonce;
211: }
212:
213: public DEROctetString getRecipNonce() {
214: return recipNonce;
215: }
216:
217: public void setFreeText(PKIFreeText freeText) {
218: this .freeText = freeText;
219: }
220:
221: public PKIFreeText getFreeText() {
222: return freeText;
223: }
224:
225: public void addGeneralInfo(InfoTypeAndValue generalInfo) {
226: this .generalInfos.addElement(generalInfo);
227: }
228:
229: public InfoTypeAndValue getGeneralInfo(int nr) {
230: if (generalInfos.size() > nr)
231: return (InfoTypeAndValue) generalInfos.elementAt(nr);
232:
233: return null;
234: }
235:
236: public DERObject getDERObject() {
237: ASN1EncodableVector v = new ASN1EncodableVector();
238:
239: v.add(pvno);
240: v.add(sender);
241: v.add(recipient);
242:
243: if (messageTime != null)
244: v.add(new DERTaggedObject(true, 0, messageTime));
245:
246: if (protectionAlg != null)
247: v.add(new DERTaggedObject(true, 1, protectionAlg));
248:
249: if (senderKID != null)
250: v.add(new DERTaggedObject(true, 2, senderKID));
251:
252: if (recipKID != null)
253: v.add(new DERTaggedObject(true, 3, recipKID));
254:
255: if (transactionID != null)
256: v.add(new DERTaggedObject(true, 4, transactionID));
257:
258: if (senderNonce != null)
259: v.add(new DERTaggedObject(true, 5, senderNonce));
260:
261: if (recipNonce != null)
262: v.add(new DERTaggedObject(true, 6, recipNonce));
263:
264: if (freeText != null)
265: v.add(new DERTaggedObject(true, 7, freeText));
266:
267: if (generalInfos.size() > 0) {
268: ASN1EncodableVector giv = new ASN1EncodableVector();
269:
270: for (int i = 0; i < generalInfos.size(); i++)
271: giv.add((InfoTypeAndValue) generalInfos.elementAt(i));
272:
273: v.add(new DERTaggedObject(true, 8, new DERSequence(giv)));
274: }
275:
276: return new DERSequence(v);
277: }
278:
279: public String toString() {
280: String s = "PKIHeader: ( pvno: " + pvno + ", sender: " + sender
281: + ", recipient: " + recipient + ", ";
282:
283: if (messageTime != null)
284: s += "messageTime: " + messageTime + ", ";
285:
286: if (protectionAlg != null)
287: s += "protectionAlg: " + protectionAlg + ", ";
288:
289: if (senderKID != null)
290: s += "senderKID: " + senderKID + ", ";
291:
292: if (recipKID != null)
293: s += "recipKID: " + recipKID + ", ";
294:
295: if (transactionID != null)
296: s += "transactionID: " + transactionID + ", ";
297:
298: if (senderNonce != null)
299: s += "senderNonce: " + senderNonce + ", ";
300:
301: if (recipNonce != null)
302: s += "recipNonce: " + recipNonce + ", ";
303:
304: if (freeText != null)
305: s += "freeText: " + freeText + ", ";
306:
307: if (generalInfos.size() > 0) {
308: s += "generalInfo: (";
309: for (int i = 0; i < generalInfos.size(); i++)
310: s += generalInfos.elementAt(i) + ", ";
311: s += ")";
312: }
313:
314: return s;
315: }
316: }
|