com.bouncycastle.asn1.x509.sigi.NameOrPseudonym Maven / Gradle / Ivy
The newest version!
package com.bouncycastle.asn1.x509.sigi;
import java.util.Enumeration;
import com.bouncycastle.asn1.ASN1Choice;
import com.bouncycastle.asn1.ASN1Encodable;
import com.bouncycastle.asn1.ASN1EncodableVector;
import com.bouncycastle.asn1.ASN1Sequence;
import com.bouncycastle.asn1.ASN1String;
import com.bouncycastle.asn1.DERObject;
import com.bouncycastle.asn1.DERSequence;
import com.bouncycastle.asn1.x500.DirectoryString;
/**
* Structure for a name or pseudonym.
*
*
* NameOrPseudonym ::= CHOICE {
* surAndGivenName SEQUENCE {
* surName DirectoryString,
* givenName SEQUENCE OF DirectoryString
* },
* pseudonym DirectoryString
* }
*
*
* @see PersonalData
*
*/
public class NameOrPseudonym
extends ASN1Encodable
implements ASN1Choice
{
private DirectoryString pseudonym;
private DirectoryString surname;
private ASN1Sequence givenName;
public static NameOrPseudonym getInstance(Object obj)
{
if (obj == null || obj instanceof NameOrPseudonym)
{
return (NameOrPseudonym)obj;
}
if (obj instanceof ASN1String)
{
return new NameOrPseudonym(DirectoryString.getInstance(obj));
}
if (obj instanceof ASN1Sequence)
{
return new NameOrPseudonym((ASN1Sequence)obj);
}
throw new IllegalArgumentException("illegal object in getInstance: "
+ obj.getClass().getName());
}
/**
* Constructor from DERString.
*
* The sequence is of type NameOrPseudonym:
*
*
* NameOrPseudonym ::= CHOICE {
* surAndGivenName SEQUENCE {
* surName DirectoryString,
* givenName SEQUENCE OF DirectoryString
* },
* pseudonym DirectoryString
* }
*
* @param pseudonym pseudonym value to use.
*/
public NameOrPseudonym(DirectoryString pseudonym)
{
this.pseudonym = pseudonym;
}
/**
* Constructor from ASN1Sequence.
*
* The sequence is of type NameOrPseudonym:
*
*
* NameOrPseudonym ::= CHOICE {
* surAndGivenName SEQUENCE {
* surName DirectoryString,
* givenName SEQUENCE OF DirectoryString
* },
* pseudonym DirectoryString
* }
*
*
* @param seq The ASN.1 sequence.
*/
private NameOrPseudonym(ASN1Sequence seq)
{
if (seq.size() != 2)
{
throw new IllegalArgumentException("Bad sequence size: "
+ seq.size());
}
if (!(seq.getObjectAt(0) instanceof ASN1String))
{
throw new IllegalArgumentException("Bad object encountered: "
+ seq.getObjectAt(0).getClass());
}
surname = DirectoryString.getInstance(seq.getObjectAt(0));
givenName = ASN1Sequence.getInstance(seq.getObjectAt(1));
}
/**
* Constructor from a given details.
*
* @param pseudonym The pseudonym.
*/
public NameOrPseudonym(String pseudonym)
{
this(new DirectoryString(pseudonym));
}
/**
* Constructor from a given details.
*
* @param surname The surname.
* @param givenName A sequence of directory strings making up the givenName
*/
public NameOrPseudonym(DirectoryString surname, ASN1Sequence givenName)
{
this.surname = surname;
this.givenName = givenName;
}
public DirectoryString getPseudonym()
{
return pseudonym;
}
public DirectoryString getSurname()
{
return surname;
}
public DirectoryString[] getGivenName()
{
DirectoryString[] items = new DirectoryString[givenName.size()];
int count = 0;
for (Enumeration e = givenName.getObjects(); e.hasMoreElements();)
{
items[count++] = DirectoryString.getInstance(e.nextElement());
}
return items;
}
/**
* Produce an object suitable for an ASN1OutputStream.
*
* Returns:
*
*
* NameOrPseudonym ::= CHOICE {
* surAndGivenName SEQUENCE {
* surName DirectoryString,
* givenName SEQUENCE OF DirectoryString
* },
* pseudonym DirectoryString
* }
*
*
* @return a DERObject
*/
public DERObject toASN1Object()
{
if (pseudonym != null)
{
return pseudonym.toASN1Object();
}
else
{
ASN1EncodableVector vec1 = new ASN1EncodableVector();
vec1.add(surname);
vec1.add(givenName);
return new DERSequence(vec1);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy