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.DERObject;
030: import org.bouncycastle.asn1.DERSequence;
031: import org.bouncycastle.asn1.DERTaggedObject;
032: import org.bouncycastle.asn1.x509.X509CertificateStructure;
033:
034: /**
035: * ASN.1 structure DER En/DeCoder.
036: *
037: * <pre>
038: * CertRepMessage ::= SEQUENCE {
039: * caPubs [1] SEQUENCE SIZE (1..MAX) OF Certificate OPTIONAL, (X509CertificateStructure)
040: * response SEQUENCE OF CertResponse
041: * }
042: *
043: * </pre>
044: */
045: public class CertRepMessage implements DEREncodable {
046: Vector caPubs = new Vector();
047: Vector responses = new Vector();
048:
049: public static CertRepMessage getInstance(ASN1TaggedObject obj,
050: boolean explicit) {
051: return getInstance(ASN1Sequence.getInstance(obj, explicit));
052: }
053:
054: public static CertRepMessage getInstance(Object obj) {
055: if (obj instanceof CertRepMessage) {
056: return (CertRepMessage) obj;
057: } else if (obj instanceof ASN1Sequence) {
058: return new CertRepMessage((ASN1Sequence) obj);
059: }
060:
061: throw new IllegalArgumentException("unknown object in factory");
062: }
063:
064: public CertRepMessage(ASN1Sequence seq) {
065: Enumeration e = seq.getObjects();
066:
067: Object obj = e.nextElement();
068:
069: if (obj instanceof ASN1TaggedObject) {
070: ASN1Sequence s = (ASN1Sequence) (((ASN1TaggedObject) obj)
071: .getObject());
072:
073: for (int i = 0; i < s.size(); i++)
074: caPubs.add(X509CertificateStructure.getInstance(s
075: .getObjectAt(i)));
076:
077: obj = e.nextElement();
078: }
079:
080: ASN1Sequence s = (ASN1Sequence) obj;
081:
082: for (int i = 0; i < s.size(); i++)
083: responses.add(CertResponse.getInstance(s.getObjectAt(i)));
084: }
085:
086: public CertRepMessage(CertResponse response) {
087: responses.addElement(response);
088: }
089:
090: public void addCaPubs(X509CertificateStructure caPub) {
091: caPubs.addElement(caPub);
092: }
093:
094: public X509CertificateStructure getCaPubs(int nr) {
095: if (nr < caPubs.size())
096: return (X509CertificateStructure) caPubs.elementAt(nr);
097:
098: return null;
099: }
100:
101: public void addResponse(CertResponse response) {
102: responses.addElement(response);
103: }
104:
105: public CertResponse getResponse(int nr) {
106: if (nr < responses.size())
107: return (CertResponse) responses.elementAt(nr);
108:
109: return null;
110: }
111:
112: public DERObject getDERObject() {
113: ASN1EncodableVector v = new ASN1EncodableVector();
114:
115: if (caPubs.size() > 0) {
116: ASN1EncodableVector capv = new ASN1EncodableVector();
117:
118: for (int i = 0; i < caPubs.size(); i++)
119: capv
120: .add((X509CertificateStructure) caPubs
121: .elementAt(i));
122:
123: v.add(new DERTaggedObject(true, 1, new DERSequence(capv)));
124: }
125:
126: ASN1EncodableVector resp = new ASN1EncodableVector();
127:
128: for (int i = 0; i < responses.size(); i++)
129: resp.add((CertResponse) responses.elementAt(i));
130:
131: v.add(new DERSequence(resp));
132:
133: return new DERSequence(v);
134: }
135:
136: public String toString() {
137: String s = "CertRepMessage: ( ";
138:
139: if (caPubs.size() > 0) {
140: s += "caPubs: (";
141:
142: for (int i = 0; i < caPubs.size(); i++)
143: s += (X509CertificateStructure) caPubs.elementAt(i);
144:
145: s += "), ";
146: }
147:
148: s += "responses: (";
149:
150: for (int i = 0; i < responses.size(); i++)
151: s += (CertResponse) responses.elementAt(i);
152:
153: s += ")";
154:
155: return s;
156: }
157: }
|