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 org.bouncycastle.asn1.ASN1EncodableVector;
023: import org.bouncycastle.asn1.ASN1Sequence;
024: import org.bouncycastle.asn1.ASN1TaggedObject;
025: import org.bouncycastle.asn1.DEREncodable;
026: import org.bouncycastle.asn1.DERObject;
027: import org.bouncycastle.asn1.DEROctetString;
028: import org.bouncycastle.asn1.DERSequence;
029: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
030:
031: /**
032: * ASN.1 structure DER En/DeCoder.
033: *
034: * <pre>
035: * Challenge ::= SEQUENCE {
036: * owf AlgorithmIdentifier OPTIONAL, -- MUST be present in the first Challenge; MAY be omitted in any subsequent Challenge in POPODecKeyChallContent (if omitted,
037: * -- then the owf used in the immediately preceding Challenge is to be used).
038: * 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.]
039: * 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}
040: * -- rand --> the randomly-generated INTEGER A (above); sender --> the sender's name (as included in PKIHeader)
041: * }
042: *
043: * </pre>
044: */
045: public class Challenge implements DEREncodable {
046: AlgorithmIdentifier owf;
047: DEROctetString witness;
048: DEROctetString challenge;
049:
050: public static Challenge getInstance(ASN1TaggedObject obj,
051: boolean explicit) {
052: return getInstance(ASN1Sequence.getInstance(obj, explicit));
053: }
054:
055: public static Challenge getInstance(Object obj) {
056: if (obj instanceof Challenge) {
057: return (Challenge) obj;
058: } else if (obj instanceof ASN1Sequence) {
059: return new Challenge((ASN1Sequence) obj);
060: }
061:
062: throw new IllegalArgumentException("unknown object in factory");
063: }
064:
065: public Challenge(ASN1Sequence seq) {
066: int idx = 0;
067: Object obj = seq.getObjectAt(idx);
068:
069: if (!(obj instanceof DEROctetString)) {
070: owf = AlgorithmIdentifier.getInstance(obj);
071: idx++;
072: }
073:
074: this .witness = (DEROctetString) seq.getObjectAt(idx++);
075: this .challenge = (DEROctetString) seq.getObjectAt(idx);
076: }
077:
078: public Challenge(DEROctetString witness, DEROctetString challenge) {
079: this .witness = witness;
080: this .challenge = challenge;
081: }
082:
083: public AlgorithmIdentifier getOwf() {
084: return owf;
085: }
086:
087: public void setOwf(AlgorithmIdentifier owf) {
088: this .owf = owf;
089: }
090:
091: public DEROctetString getWitness() {
092: return witness;
093: }
094:
095: public DEROctetString getChallenge() {
096: return challenge;
097: }
098:
099: public DERObject getDERObject() {
100: ASN1EncodableVector v = new ASN1EncodableVector();
101:
102: if (owf != null)
103: v.add(owf);
104:
105: v.add(witness);
106: v.add(challenge);
107:
108: return new DERSequence(v);
109: }
110:
111: public String toString() {
112: String s = "Challenge: (";
113:
114: if (this .getOwf() != null)
115: s += "owf: " + this .getOwf() + ", ";
116:
117: s += "witness: " + this .getWitness();
118: s += "challenge: " + this .getChallenge();
119:
120: s += ")";
121:
122: return s;
123: }
124: }
|