001package org.apache.commons.ssl.org.bouncycastle.asn1.crmf; 002 003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable; 004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector; 005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object; 006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive; 007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence; 008import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence; 009import org.apache.commons.ssl.org.bouncycastle.asn1.DERUTF8String; 010import org.apache.commons.ssl.org.bouncycastle.asn1.pkcs.PrivateKeyInfo; 011import org.apache.commons.ssl.org.bouncycastle.asn1.x509.GeneralName; 012 013public class EncKeyWithID 014 extends ASN1Object 015{ 016 private final PrivateKeyInfo privKeyInfo; 017 private final ASN1Encodable identifier; 018 019 public static EncKeyWithID getInstance(Object o) 020 { 021 if (o instanceof EncKeyWithID) 022 { 023 return (EncKeyWithID)o; 024 } 025 else if (o != null) 026 { 027 return new EncKeyWithID(ASN1Sequence.getInstance(o)); 028 } 029 030 return null; 031 } 032 033 private EncKeyWithID(ASN1Sequence seq) 034 { 035 this.privKeyInfo = PrivateKeyInfo.getInstance(seq.getObjectAt(0)); 036 037 if (seq.size() > 1) 038 { 039 if (!(seq.getObjectAt(1) instanceof DERUTF8String)) 040 { 041 this.identifier = GeneralName.getInstance(seq.getObjectAt(1)); 042 } 043 else 044 { 045 this.identifier = (ASN1Encodable)seq.getObjectAt(1); 046 } 047 } 048 else 049 { 050 this.identifier = null; 051 } 052 } 053 054 public EncKeyWithID(PrivateKeyInfo privKeyInfo) 055 { 056 this.privKeyInfo = privKeyInfo; 057 this.identifier = null; 058 } 059 060 public EncKeyWithID(PrivateKeyInfo privKeyInfo, DERUTF8String str) 061 { 062 this.privKeyInfo = privKeyInfo; 063 this.identifier = str; 064 } 065 066 public EncKeyWithID(PrivateKeyInfo privKeyInfo, GeneralName generalName) 067 { 068 this.privKeyInfo = privKeyInfo; 069 this.identifier = generalName; 070 } 071 072 public PrivateKeyInfo getPrivateKey() 073 { 074 return privKeyInfo; 075 } 076 077 public boolean hasIdentifier() 078 { 079 return identifier != null; 080 } 081 082 public boolean isIdentifierUTF8String() 083 { 084 return identifier instanceof DERUTF8String; 085 } 086 087 public ASN1Encodable getIdentifier() 088 { 089 return identifier; 090 } 091 092 /** 093 * <pre> 094 * EncKeyWithID ::= SEQUENCE { 095 * privateKey PrivateKeyInfo, 096 * identifier CHOICE { 097 * string UTF8String, 098 * generalName GeneralName 099 * } OPTIONAL 100 * } 101 * </pre> 102 * @return 103 */ 104 public ASN1Primitive toASN1Primitive() 105 { 106 ASN1EncodableVector v = new ASN1EncodableVector(); 107 108 v.add(privKeyInfo); 109 110 if (identifier != null) 111 { 112 v.add(identifier); 113 } 114 115 return new DERSequence(v); 116 } 117}