001: package org.bouncycastle.asn1.crmf;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1OctetString;
006: import org.bouncycastle.asn1.ASN1Sequence;
007: import org.bouncycastle.asn1.ASN1TaggedObject;
008: import org.bouncycastle.asn1.DERBitString;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERSequence;
011: import org.bouncycastle.asn1.DERTaggedObject;
012: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
013:
014: public class EncryptedValue extends ASN1Encodable {
015: private AlgorithmIdentifier intendedAlg;
016: private AlgorithmIdentifier symmAlg;
017: private DERBitString encSymmKey;
018: private AlgorithmIdentifier keyAlg;
019: private ASN1OctetString valueHint;
020: private DERBitString encValue;
021:
022: private EncryptedValue(ASN1Sequence seq) {
023: int index = 0;
024: while (seq.getObjectAt(index) instanceof ASN1TaggedObject) {
025: ASN1TaggedObject tObj = (ASN1TaggedObject) seq
026: .getObjectAt(index);
027:
028: switch (tObj.getTagNo()) {
029: case 0:
030: intendedAlg = AlgorithmIdentifier.getInstance(tObj,
031: false);
032: break;
033: case 1:
034: symmAlg = AlgorithmIdentifier.getInstance(tObj, false);
035: break;
036: case 2:
037: encSymmKey = DERBitString.getInstance(tObj, false);
038: break;
039: case 3:
040: keyAlg = AlgorithmIdentifier.getInstance(tObj, false);
041: break;
042: case 4:
043: valueHint = ASN1OctetString.getInstance(tObj, false);
044: break;
045: }
046: index++;
047: }
048:
049: encValue = DERBitString.getInstance(seq.getObjectAt(index));
050: }
051:
052: public static EncryptedValue getInstance(Object o) {
053: if (o instanceof EncryptedValue) {
054: return (EncryptedValue) o;
055: }
056:
057: if (o instanceof ASN1Sequence) {
058: return new EncryptedValue((ASN1Sequence) o);
059: }
060:
061: throw new IllegalArgumentException("Invalid object: "
062: + o.getClass().getName());
063: }
064:
065: /**
066: * <pre>
067: * EncryptedValue ::= SEQUENCE {
068: * intendedAlg [0] AlgorithmIdentifier OPTIONAL,
069: * -- the intended algorithm for which the value will be used
070: * symmAlg [1] AlgorithmIdentifier OPTIONAL,
071: * -- the symmetric algorithm used to encrypt the value
072: * encSymmKey [2] BIT STRING OPTIONAL,
073: * -- the (encrypted) symmetric key used to encrypt the value
074: * keyAlg [3] AlgorithmIdentifier OPTIONAL,
075: * -- algorithm used to encrypt the symmetric key
076: * valueHint [4] OCTET STRING OPTIONAL,
077: * -- a brief description or identifier of the encValue content
078: * -- (may be meaningful only to the sending entity, and used only
079: * -- if EncryptedValue might be re-examined by the sending entity
080: * -- in the future)
081: * encValue BIT STRING }
082: * -- the encrypted value itself
083: * </pre>
084: * @return a basic ASN.1 object representation.
085: */
086: public DERObject toASN1Object() {
087: ASN1EncodableVector v = new ASN1EncodableVector();
088:
089: addOptional(v, 0, intendedAlg);
090: addOptional(v, 1, symmAlg);
091: addOptional(v, 2, encSymmKey);
092: addOptional(v, 3, keyAlg);
093: addOptional(v, 4, valueHint);
094:
095: v.add(encValue);
096:
097: return new DERSequence(v);
098: }
099:
100: private void addOptional(ASN1EncodableVector v, int tagNo,
101: ASN1Encodable obj) {
102: if (obj != null) {
103: v.add(new DERTaggedObject(false, tagNo, obj));
104: }
105: }
106: }
|