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.Vector;
023:
024: import org.bouncycastle.asn1.ASN1EncodableVector;
025: import org.bouncycastle.asn1.ASN1Sequence;
026: import org.bouncycastle.asn1.ASN1TaggedObject;
027: import org.bouncycastle.asn1.DEREncodable;
028: import org.bouncycastle.asn1.DERInteger;
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: * CertRequest ::= SEQUENCE {
037: * certReqId INTEGER, -- ID for matching request and reply
038: * certTemplate CertTemplate, -- Selected fields of cert to be issued
039: * controls Controls OPTIONAL } -- Attributes affecting issuance
040: *
041: * </pre>
042: */
043: public class CertRequest implements DEREncodable {
044: DERInteger certReqId;
045: CertTemplate certTemplate;
046: Vector controls = new Vector();
047:
048: public static CertRequest getInstance(ASN1TaggedObject obj,
049: boolean explicit) {
050: return getInstance(ASN1Sequence.getInstance(obj, explicit));
051: }
052:
053: public static CertRequest getInstance(Object obj) {
054: if (obj instanceof CertRequest) {
055: return (CertRequest) obj;
056: } else if (obj instanceof ASN1Sequence) {
057: return new CertRequest((ASN1Sequence) obj);
058: }
059:
060: throw new IllegalArgumentException("unknown object in factory");
061: }
062:
063: public CertRequest(ASN1Sequence seq) {
064: this .certReqId = DERInteger.getInstance(seq.getObjectAt(0));
065: this .certTemplate = CertTemplate
066: .getInstance(seq.getObjectAt(1));
067: if (seq.size() > 2) {
068: ASN1Sequence s = (ASN1Sequence) seq.getObjectAt(2);
069: for (int i = 0; i < s.size(); i++)
070: controls.addElement(AttributeTypeAndValue.getInstance(s
071: .getObjectAt(i)));
072: }
073: }
074:
075: public CertRequest(DERInteger certReqId, CertTemplate certTemplate) {
076: this .certReqId = certReqId;
077: this .certTemplate = certTemplate;
078: }
079:
080: public DERInteger getCertReqId() {
081: return certReqId;
082: }
083:
084: public CertTemplate getCertTemplate() {
085: return certTemplate;
086: }
087:
088: public AttributeTypeAndValue getControls(int nr) {
089: if (controls.size() > nr)
090: return (AttributeTypeAndValue) controls.elementAt(nr);
091:
092: return null;
093: }
094:
095: public void addControls(AttributeTypeAndValue control) {
096: controls.addElement(control);
097: }
098:
099: public DERObject getDERObject() {
100: ASN1EncodableVector v = new ASN1EncodableVector();
101:
102: v.add(certReqId);
103: v.add(certTemplate);
104:
105: if (controls.size() > 0) {
106: ASN1EncodableVector pubiv = new ASN1EncodableVector();
107: for (int i = 0; i < controls.size(); i++)
108: pubiv
109: .add((AttributeTypeAndValue) controls
110: .elementAt(i));
111:
112: v.add(new DERSequence(pubiv));
113: }
114:
115: return new DERSequence(v);
116: }
117:
118: public String toString() {
119: String s = "CertRequest: (certReqId = " + this .getCertReqId()
120: + ", ";
121:
122: s += "certTemplate: " + this .getCertTemplate() + ", ";
123:
124: if (controls.size() > 0) {
125: s += "controls : (";
126:
127: for (int i = 0; i < controls.size(); i++)
128: s += (AttributeTypeAndValue) controls.elementAt(i);
129:
130: s += ")";
131: }
132:
133: s += ")";
134:
135: return s;
136: }
137: }
|