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: * KeyRecRepContent ::= SEQUENCE {
039: * status PKIStatusInfo,
040: * newSigCert [0] Certificate OPTIONAL, (X509CertificateStructure)
041: * caCerts [1] SEQUENCE SIZE (1..MAX) OF Certificate OPTIONAL, (X509CertificateStructure)
042: * keyPairHist [2] SEQUENCE SIZE (1..MAX) OF CertifiedKeyPair OPTIONAL
043: * }
044: *
045: * </pre>
046: */
047: public class KeyRecRepContent implements DEREncodable {
048: PKIStatusInfo status;
049: X509CertificateStructure newSigCert;
050: Vector caCerts = new Vector();
051: Vector keyPairHists = new Vector();
052:
053: public static KeyRecRepContent getInstance(ASN1TaggedObject obj,
054: boolean explicit) {
055: return getInstance(ASN1Sequence.getInstance(obj, explicit));
056: }
057:
058: public static KeyRecRepContent getInstance(Object obj) {
059: if (obj instanceof KeyRecRepContent) {
060: return (KeyRecRepContent) obj;
061: } else if (obj instanceof ASN1Sequence) {
062: return new KeyRecRepContent((ASN1Sequence) obj);
063: }
064:
065: throw new IllegalArgumentException("unknown object in factory");
066: }
067:
068: public KeyRecRepContent(ASN1Sequence seq) {
069: Enumeration e = seq.getObjects();
070:
071: status = PKIStatusInfo.getInstance(e.nextElement());
072:
073: while (e.hasMoreElements()) {
074: ASN1TaggedObject tagObj = (ASN1TaggedObject) e
075: .nextElement();
076:
077: switch (tagObj.getTagNo()) {
078: case 0:
079: newSigCert = X509CertificateStructure
080: .getInstance(tagObj.getObject());
081: break;
082: case 1: {
083: ASN1Sequence s = (ASN1Sequence) tagObj.getObject();
084: for (int i = 0; i < s.size(); i++)
085: caCerts.addElement(X509CertificateStructure
086: .getInstance(s.getObjectAt(i)));
087: }
088: break;
089: case 2: {
090: ASN1Sequence s = (ASN1Sequence) tagObj.getObject();
091: for (int i = 0; i < s.size(); i++)
092: keyPairHists.addElement(CertifiedKeyPair
093: .getInstance(s.getObjectAt(i)));
094: }
095: break;
096: }
097: }
098: }
099:
100: public KeyRecRepContent(PKIStatusInfo status) {
101: this .status = status;
102: }
103:
104: public PKIStatusInfo getStatus() {
105: return status;
106: }
107:
108: public X509CertificateStructure getNewSigCert() {
109: return newSigCert;
110: }
111:
112: public void setNewSigCert(X509CertificateStructure newSigCert) {
113: this .newSigCert = newSigCert;
114: }
115:
116: public void addCaCerts(X509CertificateStructure caCert) {
117: caCerts.addElement(caCert);
118: }
119:
120: public X509CertificateStructure getCaCerts(int nr) {
121: if (nr < caCerts.size())
122: return (X509CertificateStructure) caCerts.elementAt(nr);
123:
124: return null;
125: }
126:
127: public void addKeyPairHist(CertifiedKeyPair keyPairHist) {
128: keyPairHists.addElement(keyPairHist);
129: }
130:
131: public CertifiedKeyPair getKeyPairHist(int nr) {
132: if (nr < keyPairHists.size())
133: return (CertifiedKeyPair) keyPairHists.elementAt(nr);
134:
135: return null;
136: }
137:
138: public DERObject getDERObject() {
139: ASN1EncodableVector v = new ASN1EncodableVector();
140:
141: v.add(status);
142:
143: if (newSigCert != null)
144: v.add(new DERTaggedObject(true, 0, newSigCert));
145:
146: if (caCerts.size() > 0) {
147: ASN1EncodableVector cacv = new ASN1EncodableVector();
148:
149: for (int i = 0; i < caCerts.size(); i++)
150: cacv.add((X509CertificateStructure) caCerts
151: .elementAt(i));
152:
153: v.add(new DERTaggedObject(true, 1, new DERSequence(cacv)));
154: }
155:
156: if (keyPairHists.size() > 0) {
157: ASN1EncodableVector keyphv = new ASN1EncodableVector();
158:
159: for (int i = 0; i < keyPairHists.size(); i++)
160: keyphv
161: .add((CertifiedKeyPair) keyPairHists
162: .elementAt(i));
163:
164: v
165: .add(new DERTaggedObject(true, 2, new DERSequence(
166: keyphv)));
167: }
168:
169: return new DERSequence(v);
170: }
171:
172: public String toString() {
173: String s = "CertifiedKeyPair: ( status: " + this .getStatus()
174: + ", ";
175:
176: if (this .getNewSigCert() != null)
177: s += "newSigCert: " + this .getNewSigCert() + ", ";
178:
179: if (caCerts.size() > 0) {
180: s += "caCerts: (";
181:
182: for (int i = 0; i < caCerts.size(); i++)
183: s += (X509CertificateStructure) caCerts.elementAt(i);
184:
185: s += "), ";
186: }
187:
188: if (keyPairHists.size() > 0) {
189: s += "keyPairHist: (";
190:
191: for (int i = 0; i < caCerts.size(); i++)
192: s += (CertifiedKeyPair) keyPairHists.elementAt(i);
193:
194: s += ")";
195: }
196:
197: s += ")";
198:
199: return s;
200: }
201: }
|