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.Vector;
023: import java.util.Enumeration;
024:
025: import java.io.ByteArrayOutputStream;
026:
027: import org.bouncycastle.asn1.*;
028: import org.bouncycastle.asn1.x509.*;
029:
030: /**
031: * ASN.1 structure DER En/DeCoder.
032: *
033: * <pre>
034: * PKIMessage ::= SEQUENCE {
035: * header PKIMessage,
036: * body PKIBody,
037: * protection [0] PKIProtection OPTIONAL, -- (BIT STRING)
038: * extraCerts [1] SEQUENCE SIZE (1..MAX) OF Certificate OPTIONAL
039: * }
040: *
041: * </pre>
042: */
043: public class PKIMessage implements DEREncodable {
044: PKIHeader header;
045: PKIBody body;
046: DERBitString protection;
047: Vector extraCerts = new Vector();
048: byte protectedBytes[];
049:
050: public static PKIMessage getInstance(ASN1TaggedObject obj,
051: boolean explicit) {
052: return getInstance(ASN1Sequence.getInstance(obj, explicit));
053: }
054:
055: public static PKIMessage getInstance(Object obj) {
056: if (obj instanceof PKIMessage) {
057: return (PKIMessage) obj;
058: } else if (obj instanceof ASN1Sequence) {
059: return new PKIMessage((ASN1Sequence) obj);
060: }
061:
062: throw new IllegalArgumentException("unknown object in factory");
063: }
064:
065: public PKIMessage(ASN1Sequence seq) {
066: Enumeration e = seq.getObjects();
067:
068: /*
069: header = PKIHeader.getInstance( e.nextElement() );
070: body = PKIBody.getInstance( (ASN1TaggedObject)e.nextElement() );
071: */
072:
073: DEREncodable derHeader = (DEREncodable) e.nextElement();
074: DEREncodable derBody = (DEREncodable) e.nextElement();
075:
076: try {
077: //store protected part in unmodified form...
078: ASN1EncodableVector v = new ASN1EncodableVector();
079: v.add(derHeader);
080: v.add(derBody);
081:
082: ByteArrayOutputStream bao = new ByteArrayOutputStream();
083: DEROutputStream out = new DEROutputStream(bao);
084: out.writeObject(new DERSequence(v));
085:
086: protectedBytes = bao.toByteArray();
087: } catch (Exception ex) {
088: }
089:
090: header = PKIHeader.getInstance(derHeader);
091: body = PKIBody.getInstance((ASN1TaggedObject) derBody);
092:
093: while (e.hasMoreElements()) {
094: ASN1TaggedObject tagObj = (ASN1TaggedObject) e
095: .nextElement();
096:
097: switch (tagObj.getTagNo()) {
098: case 0:
099: protection = DERBitString.getInstance(tagObj
100: .getObject());
101: break;
102: case 1:
103: ASN1Sequence s = (ASN1Sequence) tagObj.getObject();
104: for (int i = 0; i < s.size(); i++)
105: extraCerts.addElement(X509CertificateStructure
106: .getInstance(s.getObjectAt(i)));
107: break;
108: }
109: }
110: }
111:
112: public PKIMessage(PKIHeader header, PKIBody body) {
113: this .header = header;
114: this .body = body;
115: }
116:
117: public PKIHeader getHeader() {
118: return header;
119: }
120:
121: public PKIBody getBody() {
122: return body;
123: }
124:
125: public void setProtection(DERBitString protection) {
126: this .protection = protection;
127: }
128:
129: public DERBitString getProtection() {
130: return protection;
131: }
132:
133: public void addExtraCert(X509CertificateStructure extraCert) {
134: this .extraCerts.addElement(extraCert);
135: }
136:
137: public X509CertificateStructure getExtraCert(int nr) {
138: if (extraCerts.size() > nr)
139: return (X509CertificateStructure) extraCerts.elementAt(nr);
140:
141: return null;
142: }
143:
144: public DERObject getDERObject() {
145: ASN1EncodableVector v = new ASN1EncodableVector();
146:
147: v.add(header);
148: v.add(body);
149:
150: if (protection != null)
151: v.add(new DERTaggedObject(true, 0, protection));
152:
153: if (extraCerts.size() > 0) {
154: ASN1EncodableVector giv = new ASN1EncodableVector();
155:
156: for (int i = 0; i < extraCerts.size(); i++)
157: giv.add((X509CertificateStructure) extraCerts
158: .elementAt(i));
159:
160: v.add(new DERTaggedObject(true, 1, new DERSequence(giv)));
161: }
162:
163: return new DERSequence(v);
164: }
165:
166: public byte[] getProtectedBytes() {
167: if (protectedBytes != null)
168: return protectedBytes;
169:
170: try {
171: ByteArrayOutputStream bao = new ByteArrayOutputStream();
172: DEROutputStream out = new DEROutputStream(bao);
173: out.writeObject(getProtectedPart());
174: return bao.toByteArray();
175: } catch (Exception ex) {
176: }
177:
178: return null;
179: }
180:
181: public ProtectedPart getProtectedPart() {
182: return new ProtectedPart(header, body);
183: }
184:
185: public String toString() {
186: String s = "PKIMessage: ( header: " + this .getHeader()
187: + ", body: " + this .getBody() + ", ";
188:
189: if (this .getProtection() != null)
190: s += "protection: " + this .getProtection() + ", ";
191:
192: if (extraCerts.size() > 0) {
193: s += "extraCerts: (";
194: for (int i = 0; i < extraCerts.size(); i++)
195: s += extraCerts.elementAt(i) + ", ";
196: s += ")";
197: }
198:
199: s += ")";
200:
201: return s;
202: }
203: }
|