org.spongycastle.asn1.x509.sigi.NameOrPseudonym Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scprov-jdk15 Show documentation
Show all versions of scprov-jdk15 Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle intended for Android.
Android ships with a stripped-down version of Bouncy Castle - this causes classloader collisions if you try to add
an alternative (updated/complete) Bouncy Castle jar.
This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5.
package org.spongycastle.asn1.x509.sigi;
import java.util.Enumeration;
import org.spongycastle.asn1.ASN1Choice;
import org.spongycastle.asn1.ASN1Encodable;
import org.spongycastle.asn1.ASN1EncodableVector;
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.ASN1String;
import org.spongycastle.asn1.DERObject;
import org.spongycastle.asn1.DERSequence;
import org.spongycastle.asn1.x500.DirectoryString;
/**
* Structure for a name or pseudonym.
*
*
* NameOrPseudonym ::= CHOICE {
* surAndGivenName SEQUENCE {
* surName DirectoryString,
* givenName SEQUENCE OF DirectoryString
* },
* pseudonym DirectoryString
* }
*
*
* @see org.spongycastle.asn1.x509.sigi.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);
}
}
}