001package org.apache.commons.ssl.org.bouncycastle.asn1.cms; 002 003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Choice; 004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable; 005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object; 006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1OctetString; 007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive; 008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject; 009import org.apache.commons.ssl.org.bouncycastle.asn1.DERTaggedObject; 010 011/** 012 * <a href="http://tools.ietf.org/html/rfc5652#section-6.2.1">RFC 5652</a>: 013 * Content encryption key delivery mechanisms. 014 * <pre> 015 * RecipientIdentifier ::= CHOICE { 016 * issuerAndSerialNumber IssuerAndSerialNumber, 017 * subjectKeyIdentifier [0] SubjectKeyIdentifier 018 * } 019 * 020 * SubjectKeyIdentifier ::= OCTET STRING 021 * </pre> 022 */ 023public class RecipientIdentifier 024 extends ASN1Object 025 implements ASN1Choice 026{ 027 private ASN1Encodable id; 028 029 public RecipientIdentifier( 030 IssuerAndSerialNumber id) 031 { 032 this.id = id; 033 } 034 035 public RecipientIdentifier( 036 ASN1OctetString id) 037 { 038 this.id = new DERTaggedObject(false, 0, id); 039 } 040 041 public RecipientIdentifier( 042 ASN1Primitive id) 043 { 044 this.id = id; 045 } 046 047 /** 048 * Return a RecipientIdentifier object from the given object. 049 * <p> 050 * Accepted inputs: 051 * <ul> 052 * <li> null → null 053 * <li> {@link RecipientIdentifier} object 054 * <li> {@link IssuerAndSerialNumber} object 055 * <li> {@link org.bouncycastle.asn1.ASN1OctetString#getInstance(java.lang.Object) ASN1OctetString} input formats (OctetString, byte[]) with value of KeyIdentifier in DER form 056 * <li> {@link org.bouncycastle.asn1.ASN1Primitive ASN1Primitive} for RecipientIdentifier constructor 057 * </ul> 058 * 059 * @param o the object we want converted. 060 * @exception IllegalArgumentException if the object cannot be converted. 061 */ 062 public static RecipientIdentifier getInstance( 063 Object o) 064 { 065 if (o == null || o instanceof RecipientIdentifier) 066 { 067 return (RecipientIdentifier)o; 068 } 069 070 if (o instanceof IssuerAndSerialNumber) 071 { 072 return new RecipientIdentifier((IssuerAndSerialNumber)o); 073 } 074 075 if (o instanceof ASN1OctetString) 076 { 077 return new RecipientIdentifier((ASN1OctetString)o); 078 } 079 080 if (o instanceof ASN1Primitive) 081 { 082 return new RecipientIdentifier((ASN1Primitive)o); 083 } 084 085 throw new IllegalArgumentException( 086 "Illegal object in RecipientIdentifier: " + o.getClass().getName()); 087 } 088 089 public boolean isTagged() 090 { 091 return (id instanceof ASN1TaggedObject); 092 } 093 094 public ASN1Encodable getId() 095 { 096 if (id instanceof ASN1TaggedObject) 097 { 098 return ASN1OctetString.getInstance((ASN1TaggedObject)id, false); 099 } 100 101 return IssuerAndSerialNumber.getInstance(id); 102 } 103 104 /** 105 * Produce an object suitable for an ASN1OutputStream. 106 */ 107 public ASN1Primitive toASN1Primitive() 108 { 109 return id.toASN1Primitive(); 110 } 111}