KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > crmf > CertRequest


1 // CMP implementation copyright (c) 2003 NOVOSEC AG (http://www.novosec.com)
2
//
3
// Author: Maik Stohn
4
//
5
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
6
// software and associated documentation files (the "Software"), to deal in the Software
7
// without restriction, including without limitation the rights to use, copy, modify, merge,
8
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
9
// to whom the Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in all copies or
12
// substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
15
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19

20 package com.novosec.pkix.asn1.crmf;
21
22 import java.util.Vector JavaDoc;
23
24 import org.bouncycastle.asn1.ASN1EncodableVector;
25 import org.bouncycastle.asn1.ASN1Sequence;
26 import org.bouncycastle.asn1.ASN1TaggedObject;
27 import org.bouncycastle.asn1.DEREncodable;
28 import org.bouncycastle.asn1.DERInteger;
29 import org.bouncycastle.asn1.DERObject;
30 import org.bouncycastle.asn1.DERSequence;
31
32 /**
33  * ASN.1 structure DER En/DeCoder.
34  *
35  * <pre>
36  * CertRequest ::= SEQUENCE {
37  * certReqId INTEGER, -- ID for matching request and reply
38  * certTemplate CertTemplate, -- Selected fields of cert to be issued
39  * controls Controls OPTIONAL } -- Attributes affecting issuance
40  *
41  * </pre>
42  */

43 public class CertRequest implements DEREncodable
44 {
45     DERInteger certReqId;
46     CertTemplate certTemplate;
47     Vector JavaDoc controls = new Vector JavaDoc();
48
49     public static CertRequest getInstance( ASN1TaggedObject obj, boolean explicit )
50     {
51         return getInstance(ASN1Sequence.getInstance(obj, explicit));
52     }
53
54     public static CertRequest getInstance( Object JavaDoc obj )
55     {
56         if (obj instanceof CertRequest)
57         {
58             return (CertRequest)obj;
59         }
60         else if (obj instanceof ASN1Sequence)
61         {
62             return new CertRequest((ASN1Sequence)obj);
63         }
64
65         throw new IllegalArgumentException JavaDoc("unknown object in factory");
66     }
67     
68     public CertRequest( ASN1Sequence seq )
69     {
70       this.certReqId = DERInteger.getInstance(seq.getObjectAt(0));
71       this.certTemplate = CertTemplate.getInstance(seq.getObjectAt(1));
72       if( seq.size()>2 )
73       {
74         ASN1Sequence s = (ASN1Sequence)seq.getObjectAt(2);
75         for( int i=0; i<s.size(); i++ )
76           controls.addElement( AttributeTypeAndValue.getInstance(s.getObjectAt(i)) );
77       }
78     }
79
80     public CertRequest( DERInteger certReqId, CertTemplate certTemplate )
81     {
82       this.certReqId = certReqId;
83       this.certTemplate = certTemplate;
84     }
85
86     public DERInteger getCertReqId()
87     {
88         return certReqId;
89     }
90
91     public CertTemplate getCertTemplate()
92     {
93         return certTemplate;
94     }
95
96     public AttributeTypeAndValue getControls(int nr)
97     {
98       if( controls.size() > nr )
99         return (AttributeTypeAndValue)controls.elementAt(nr);
100         
101       return null;
102     }
103
104     public void addControls(AttributeTypeAndValue control)
105     {
106       controls.addElement( control );
107     }
108
109     public DERObject getDERObject()
110     {
111       ASN1EncodableVector v = new ASN1EncodableVector();
112
113       v.add( certReqId );
114       v.add( certTemplate );
115
116       if( controls.size() > 0 )
117       {
118         ASN1EncodableVector pubiv = new ASN1EncodableVector();
119         for (int i=0;i<controls.size();i++)
120           pubiv.add( (AttributeTypeAndValue)controls.elementAt(i) );
121           
122         v.add( new DERSequence( pubiv ) );
123       }
124
125       return new DERSequence(v);
126     }
127
128     public String JavaDoc toString()
129     {
130       String JavaDoc s = "CertRequest: (certReqId = " + this.getCertReqId() + ", ";
131       
132       s += "certTemplate: " + this.getCertTemplate() + ", ";
133       
134       if( controls.size() > 0 )
135       {
136         s += "controls : (";
137         
138         for (int i=0;i<controls.size();i++)
139           s += (AttributeTypeAndValue)controls.elementAt(i);
140           
141         s += ")";
142       }
143
144       s += ")";
145       
146       return s;
147     }
148 }
149
Popular Tags