1 20 package com.novosec.pkix.asn1.crmf; 21 22 import org.bouncycastle.asn1.ASN1TaggedObject; 23 import org.bouncycastle.asn1.DEREncodable; 24 import org.bouncycastle.asn1.DERObject; 25 import org.bouncycastle.asn1.DERTaggedObject; 26 import org.bouncycastle.asn1.cms.EnvelopedData; 27 28 39 public class EncryptedKey implements DEREncodable 40 { 41 public static final int TAGNO_ENV_DATA = 0; 42 public static final int TAGNO_ENC_VALUE = 1; 43 44 private int tagNo = -1; 45 private DEREncodable obj = null; 46 private EncryptedValue encryptedValue = null; 47 48 public static EncryptedKey getInstance( DEREncodable derObj ) 49 { 50 if(derObj instanceof EnvelopedData) 51 return new EncryptedKey( (EnvelopedData)derObj ); 52 else if(derObj instanceof EncryptedValue) 53 return new EncryptedKey( (EncryptedValue)derObj ); 54 else if(derObj instanceof ASN1TaggedObject) 55 return getInstance( (ASN1TaggedObject)derObj, false ); 56 else 57 return new EncryptedKey( EncryptedValue.getInstance(derObj) ); } 59 60 public static EncryptedKey getInstance( ASN1TaggedObject tagObj, boolean explicit ) 61 { 62 int tag = (tagObj == null ? -1 : tagObj.getTagNo()); 63 switch (tag) 64 { 65 case TAGNO_ENV_DATA: return new EncryptedKey(EnvelopedData.getInstance(tagObj, explicit)); 66 default: return new EncryptedKey(EncryptedValue.getInstance(tagObj, explicit)); 67 } 68 } 69 70 public EncryptedKey( DEREncodable derObj, int tag ) 71 { 72 this.tagNo = tag; 73 74 if(derObj instanceof EnvelopedData) 75 this.obj = (EnvelopedData)derObj; 76 else if(derObj instanceof EncryptedValue) 77 this.encryptedValue = (EncryptedValue)derObj; 78 else { 79 switch( this.tagNo ) { 80 case TAGNO_ENV_DATA: this.obj = EnvelopedData.getInstance(derObj); break; 81 default: this.encryptedValue = EncryptedValue.getInstance(derObj); break; 82 } 83 } 84 } 85 86 public EncryptedKey( EnvelopedData envelopedData ) 87 { 88 this( envelopedData, TAGNO_ENV_DATA ); 89 } 90 91 public EncryptedKey( EncryptedValue encryptedValue ) 92 { 93 this( encryptedValue, TAGNO_ENC_VALUE ); 94 } 95 96 public void setEncryptedValue(EncryptedValue value) { 97 encryptedValue = value; 98 } 99 100 public EncryptedValue getEncryptedValue() 101 { 102 return encryptedValue; 103 } 104 105 public void setTagNo(int tn) { 106 this.tagNo = tn; 107 } 108 109 public int getTagNo() { 110 return this.tagNo; 111 } 112 113 public EnvelopedData getEnvelopedData() { 114 return EnvelopedData.getInstance(obj); 115 } 116 117 public DERObject getDERObject() 118 { 119 if( this.encryptedValue != null ) 120 return encryptedValue.getDERObject(); 121 else if( this.obj != null ) 122 return new DERTaggedObject(true, this.tagNo, this.obj); else 124 return null; 125 } 126 127 public String toString() { 128 StringBuffer sb = new StringBuffer (this.getClass().getName()); 129 sb.append(" ("); 130 131 sb.append("tagNo: " + this.tagNo + ", "); 132 133 if( this.encryptedValue != null ) 134 sb.append("encryptedValue: " + this.encryptedValue + ", "); 135 136 if( this.obj != null ) { 137 sb.append("envelopedData: " + this.obj + ", "); 138 } 139 140 sb.append("hashCode: " + Integer.toHexString(this.hashCode()) + ")"); 141 return sb.toString(); 142 } 143 } 144 | Popular Tags |