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.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.DEROctetString;
031: import org.bouncycastle.asn1.DERSequence;
032: import org.bouncycastle.asn1.DERTaggedObject;
033: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
034:
035: /**
036: * ASN.1 structure DER En/DeCoder.
037: *
038: * <pre>
039: * EncryptedValue ::= SEQUENCE {
040: * intendedAlg [0] AlgorithmIdentifier OPTIONAL, -- the intended algorithm for which the value will be used
041: * symmAlg [1] AlgorithmIdentifier OPTIONAL, -- the symmetric algorithm used to encrypt the value
042: * encSymmKey [2] BIT STRING OPTIONAL, -- the (encrypted) symmetric key used to encrypt the value
043: * keyAlg [3] AlgorithmIdentifier OPTIONAL, -- algorithm used to encrypt the symmetric key
044: * valueHint [4] OCTET STRING OPTIONAL, -- a brief description or identifier of the encValue content (may be meaningful only to the sending entity, and used only if EncryptedValue might be re-examined by the sending entity in the future)
045: * encValue BIT STRING } -- the encrypted value itself
046: *
047: * </pre>
048: */
049:
050: public class EncryptedValue implements DEREncodable {
051: AlgorithmIdentifier intendedAlg;
052: AlgorithmIdentifier symmAlg;
053: DERBitString encSymmKey;
054: AlgorithmIdentifier keyAlg;
055: DEROctetString valueHint;
056: DERBitString encValue;
057:
058: public static EncryptedValue getInstance(ASN1TaggedObject obj,
059: boolean explicit) {
060: return getInstance(ASN1Sequence.getInstance(obj, explicit));
061: }
062:
063: public static EncryptedValue getInstance(Object obj) {
064: if (obj instanceof EncryptedValue) {
065: return (EncryptedValue) obj;
066: } else if (obj instanceof ASN1Sequence) {
067: return new EncryptedValue((ASN1Sequence) obj);
068: }
069:
070: throw new IllegalArgumentException("unknown object in factory");
071: }
072:
073: public EncryptedValue(ASN1Sequence seq) {
074: Enumeration e = seq.getObjects();
075: while (e.hasMoreElements()) {
076: Object obj = e.nextElement();
077:
078: if (obj instanceof DERTaggedObject) {
079: DERTaggedObject tagObj = (DERTaggedObject) obj;
080:
081: switch (tagObj.getTagNo()) {
082: case 0:
083: this .intendedAlg = AlgorithmIdentifier
084: .getInstance(tagObj.getObject());
085: break;
086: case 1:
087: this .symmAlg = AlgorithmIdentifier
088: .getInstance(tagObj.getObject());
089: break;
090: case 2:
091: this .encSymmKey = DERBitString.getInstance(tagObj
092: .getObject());
093: break;
094: case 3:
095: this .keyAlg = AlgorithmIdentifier
096: .getInstance(tagObj.getObject());
097: break;
098: case 4:
099: this .valueHint = (DEROctetString) DEROctetString
100: .getInstance(tagObj.getObject());
101: break;
102: }
103: } else {
104: encValue = DERBitString.getInstance(obj);
105: break;
106: }
107: }
108: }
109:
110: public EncryptedValue(DERBitString encValue) {
111: this .encValue = encValue;
112: }
113:
114: public AlgorithmIdentifier getIntendedAlg() {
115: return intendedAlg;
116: }
117:
118: public void setIntendedAlg(AlgorithmIdentifier intendedAlg) {
119: this .intendedAlg = intendedAlg;
120: }
121:
122: public AlgorithmIdentifier getSymmAlg() {
123: return symmAlg;
124: }
125:
126: public void setSymmAlg(AlgorithmIdentifier symmAlg) {
127: this .symmAlg = symmAlg;
128: }
129:
130: public DERBitString getEncSymmKey() {
131: return encSymmKey;
132: }
133:
134: public void setEncSymmKey(DERBitString encSymmKey) {
135: this .encSymmKey = encSymmKey;
136: }
137:
138: public AlgorithmIdentifier getKeyAlg() {
139: return keyAlg;
140: }
141:
142: public void setKeyAlg(AlgorithmIdentifier keyAlg) {
143: this .keyAlg = keyAlg;
144: }
145:
146: public DEROctetString getValueHint() {
147: return valueHint;
148: }
149:
150: public void setValueHint(DEROctetString valueHint) {
151: this .valueHint = valueHint;
152: }
153:
154: public DERBitString getEncValue() {
155: return encValue;
156: }
157:
158: public DERObject getDERObject() {
159: ASN1EncodableVector v = new ASN1EncodableVector();
160:
161: if (intendedAlg != null)
162: v.add(new DERTaggedObject(false, 0, intendedAlg));
163: if (symmAlg != null)
164: v.add(new DERTaggedObject(false, 1, symmAlg));
165: if (encSymmKey != null)
166: v.add(new DERTaggedObject(false, 2, encSymmKey));
167: if (keyAlg != null)
168: v.add(new DERTaggedObject(false, 3, keyAlg));
169: if (valueHint != null)
170: v.add(new DERTaggedObject(false, 4, valueHint));
171:
172: v.add(encValue);
173:
174: return new DERSequence(v);
175: }
176:
177: public String toString() {
178: String s = "EncryptedValue: (";
179:
180: if (this .getIntendedAlg() != null)
181: s += "intendedAlg: " + this .getIntendedAlg() + ", ";
182:
183: if (this .getSymmAlg() != null)
184: s += "symmAlg: " + this .getSymmAlg() + ", ";
185:
186: if (this .getEncSymmKey() != null)
187: s += "encSymmKey: " + this .getEncSymmKey() + ", ";
188:
189: if (this .getKeyAlg() != null)
190: s += "keyAlg: " + this .getKeyAlg() + ", ";
191:
192: if (this .getValueHint() != null)
193: s += "valueHint: " + this .getValueHint() + ", ";
194:
195: s += "encValue: " + this .getEncValue() + ")";
196:
197: return s;
198: }
199: }
|