KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > novosec > pkix > asn1 > cmp > Challenge


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.cmp;
21
22 import org.bouncycastle.asn1.ASN1EncodableVector;
23 import org.bouncycastle.asn1.ASN1Sequence;
24 import org.bouncycastle.asn1.ASN1TaggedObject;
25 import org.bouncycastle.asn1.DEREncodable;
26 import org.bouncycastle.asn1.DERObject;
27 import org.bouncycastle.asn1.DEROctetString;
28 import org.bouncycastle.asn1.DERSequence;
29 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
30
31 /**
32  * ASN.1 structure DER En/DeCoder.
33  *
34  * <pre>
35  * Challenge ::= SEQUENCE {
36  * owf AlgorithmIdentifier OPTIONAL, -- MUST be present in the first Challenge; MAY be omitted in any subsequent Challenge in POPODecKeyChallContent (if omitted,
37  * -- then the owf used in the immediately preceding Challenge is to be used).
38  * witness OCTET STRING, -- the result of applying the one-way function (owf) to a randomly-generated INTEGER, A. [Note that a different INTEGER MUST be used for each Challenge.]
39  * challenge OCTET STRING -- the encryption (under the public key for which the cert. request is being made) of Rand, where Rand is specified as Rand ::= SEQUENCE {int INTEGER, sender GeneralName}
40  * -- rand --> the randomly-generated INTEGER A (above); sender --> the sender's name (as included in PKIHeader)
41  * }
42  *
43  * </pre>
44  */

45 public class Challenge implements DEREncodable
46 {
47   AlgorithmIdentifier owf;
48   DEROctetString witness;
49   DEROctetString challenge;
50
51   public static Challenge getInstance(ASN1TaggedObject obj, boolean explicit)
52   {
53     return getInstance(ASN1Sequence.getInstance(obj, explicit));
54   }
55
56   public static Challenge getInstance(Object JavaDoc obj)
57   {
58     if (obj instanceof Challenge)
59     {
60       return (Challenge) obj;
61     }
62     else if (obj instanceof ASN1Sequence)
63     {
64       return new Challenge((ASN1Sequence) obj);
65     }
66
67     throw new IllegalArgumentException JavaDoc("unknown object in factory");
68   }
69
70   public Challenge(ASN1Sequence seq)
71   {
72     int idx = 0;
73     Object JavaDoc obj = seq.getObjectAt(idx);
74     
75     if( !(obj instanceof DEROctetString) )
76     {
77       owf = AlgorithmIdentifier.getInstance(obj);
78       idx++;
79     }
80     
81     this.witness = (DEROctetString)seq.getObjectAt(idx++);
82     this.challenge = (DEROctetString)seq.getObjectAt(idx);
83   }
84
85   public Challenge( DEROctetString witness, DEROctetString challenge )
86   {
87     this.witness = witness;
88     this.challenge = challenge;
89   }
90
91   public AlgorithmIdentifier getOwf()
92   {
93     return owf;
94   }
95
96   public void setOwf(AlgorithmIdentifier owf)
97   {
98     this.owf = owf;
99   }
100
101   public DEROctetString getWitness()
102   {
103     return witness;
104   }
105
106   public DEROctetString getChallenge()
107   {
108     return challenge;
109   }
110
111   public DERObject getDERObject()
112   {
113     ASN1EncodableVector v = new ASN1EncodableVector();
114
115     if( owf != null )
116       v.add(owf);
117       
118     v.add(witness);
119     v.add(challenge);
120
121     return new DERSequence(v);
122   }
123
124   public String JavaDoc toString()
125   {
126     String JavaDoc s = "Challenge: (";
127     
128     if( this.getOwf() != null )
129       s += "owf: "+ this.getOwf() + ", ";
130
131     s += "witness: " + this.getWitness();
132     s += "challenge: " + this.getChallenge();
133     
134     s += ")";
135     
136     return s;
137   }
138 }
139
Popular Tags