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 java.util.Enumeration;
023:
024: import org.bouncycastle.asn1.ASN1EncodableVector;
025: import org.bouncycastle.asn1.ASN1Sequence;
026: import org.bouncycastle.asn1.ASN1TaggedObject;
027: import org.bouncycastle.asn1.DERBitString;
028: import org.bouncycastle.asn1.DEREncodable;
029: import org.bouncycastle.asn1.DERObject;
030: import org.bouncycastle.asn1.DERSequence;
031: import org.bouncycastle.asn1.DERTaggedObject;
032: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
033: import com.novosec.pkix.asn1.crmf.CertId;
034:
035: /**
036: * ASN.1 structure DER En/DeCoder.
037: *
038: * <pre>
039: * OOBCertHash ::= SEQUENCE {
040: * hashAlg [0] AlgorithmIdentifier OPTIONAL,
041: * certId [1] CertId OPTIONAL,
042: * hashVal BIT STRING -- hashVal is calculated over DER encoding of the subjectPublicKey field of the corresponding cert.
043: * }
044: *
045: * </pre>
046: */
047: public class OOBCertHash implements DEREncodable {
048: AlgorithmIdentifier hashAlg;
049: CertId certId;
050: DERBitString hashVal;
051:
052: public static OOBCertHash getInstance(ASN1TaggedObject obj,
053: boolean explicit) {
054: return getInstance(ASN1Sequence.getInstance(obj, explicit));
055: }
056:
057: public static OOBCertHash getInstance(Object obj) {
058: if (obj instanceof OOBCertHash) {
059: return (OOBCertHash) obj;
060: } else if (obj instanceof ASN1Sequence) {
061: return new OOBCertHash((ASN1Sequence) obj);
062: }
063:
064: throw new IllegalArgumentException("unknown object in factory");
065: }
066:
067: public OOBCertHash(ASN1Sequence seq) {
068: Enumeration e = seq.getObjects();
069:
070: while (e.hasMoreElements()) {
071: Object obj = e.nextElement();
072:
073: if (obj instanceof ASN1TaggedObject) {
074: ASN1TaggedObject tagObj = (ASN1TaggedObject) obj;
075:
076: switch (tagObj.getTagNo()) {
077: case 0:
078: hashAlg = AlgorithmIdentifier.getInstance(tagObj
079: .getObject());
080: break;
081: case 1:
082: certId = CertId.getInstance(tagObj.getObject());
083: break;
084: }
085: } else {
086: hashVal = DERBitString.getInstance(obj);
087:
088: break;
089: }
090: }
091: }
092:
093: public OOBCertHash(DERBitString hashVal) {
094: this .hashVal = hashVal;
095: }
096:
097: public DERBitString getHashVal() {
098: return hashVal;
099: }
100:
101: public void setHashAlg(AlgorithmIdentifier hashAlg) {
102: this .hashAlg = hashAlg;
103: }
104:
105: public AlgorithmIdentifier getHashAlg() {
106: return hashAlg;
107: }
108:
109: public void setCertId(CertId certId) {
110: this .certId = certId;
111: }
112:
113: public CertId getCertId() {
114: return certId;
115: }
116:
117: public DERObject getDERObject() {
118: ASN1EncodableVector v = new ASN1EncodableVector();
119:
120: if (hashAlg != null)
121: v.add(new DERTaggedObject(true, 0, hashAlg));
122:
123: if (certId != null)
124: v.add(new DERTaggedObject(true, 1, certId));
125:
126: v.add(hashVal);
127:
128: return new DERSequence(v);
129: }
130:
131: public String toString() {
132: String s = "OOBCertHash: ( ";
133:
134: if (this .getHashAlg() != null)
135: s += "hashAlg: " + this .getHashAlg() + ", ";
136:
137: if (this .getCertId() != null)
138: s += "certId: " + this .getCertId() + ", ";
139:
140: s += "hashVal: " + this .getHashVal();
141:
142: s += ")";
143:
144: return s;
145: }
146: }
|