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