Java Code Examples for org.bouncycastle.asn1.ASN1Primitive

The following are top voted examples for showing how to use org.bouncycastle.asn1.ASN1Primitive. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: mDL-ILP   File: DrivingLicence.java   Source Code and License 7 votes vote down vote up
private void parseDG1(byte[] DG1) {
    try {
        ASN1InputStream bIn = new ASN1InputStream(DG1);
        org.bouncycastle.asn1.DERApplicationSpecific app = (DERApplicationSpecific) bIn.readObject();

        ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE);
        Enumeration secEnum = seq.getObjects();
        while (secEnum.hasMoreElements()) {
            ASN1Primitive seqObj = (ASN1Primitive) secEnum.nextElement();
            byte[] data = seqObj.getEncoded();
            if (data[0]== 0x41) {
                Log.d("type approval number", ByteUtils.bytesToHex(data));
                this.set5F01(data);
            } else if (data[0] == 0x42) {
                byte[] input = Arrays.copyOfRange(data, 3, data.length);
                parse5F02(input);
            } else if (data[0] == 0x7F) {
                parse7F63(data);
            }
        }
        bIn.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
Example 2
Project: ipack   File: ProcurationSyntax.java   Source Code and License 7 votes vote down vote up
/**
 * Produce an object suitable for an ASN1OutputStream.
 * <p/>
 * Returns:
 * <p/>
 * <pre>
 *               ProcurationSyntax ::= SEQUENCE {
 *                 country [1] EXPLICIT PrintableString(SIZE(2)) OPTIONAL,
 *                 typeOfSubstitution [2] EXPLICIT DirectoryString (SIZE(1..128)) OPTIONAL,
 *                 signingFor [3] EXPLICIT SigningFor
 *               }
 * <p/>
 *               SigningFor ::= CHOICE
 *               {
 *                 thirdPerson GeneralName,
 *                 certRef IssuerSerial
 *               }
 * </pre>
 *
 * @return a DERObject
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector vec = new ASN1EncodableVector();
    if (country != null)
    {
        vec.add(new DERTaggedObject(true, 1, new DERPrintableString(country, true)));
    }
    if (typeOfSubstitution != null)
    {
        vec.add(new DERTaggedObject(true, 2, typeOfSubstitution));
    }
    if (thirdPerson != null)
    {
        vec.add(new DERTaggedObject(true, 3, thirdPerson));
    }
    else
    {
        vec.add(new DERTaggedObject(true, 3, certRef));
    }

    return new DERSequence(vec);
}
 
Example 3
Project: ipack   File: PEMReader.java   Source Code and License 7 votes vote down vote up
public Object parseObject(PemObject obj)
    throws IOException
{
    try
    {
        PrivateKeyInfo info = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(obj.getContent()));
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(obj.getContent());

        KeyFactory keyFact = KeyFactory.getInstance(info.getPrivateKeyAlgorithm().getAlgorithm().getId(), provider);

        return keyFact.generatePrivate(keySpec);
    }
    catch (Exception e)
    {
        throw new PEMException("problem parsing PRIVATE KEY: " + e.toString(), e);
    }
}
 
Example 4
Project: ipack   File: PrincipalUtil.java   Source Code and License 7 votes vote down vote up
/**
 * return the issuer of the given cert as an X509PrincipalObject.
 */
public static X509Principal getIssuerX509Principal(
    X509Certificate cert)
    throws CertificateEncodingException
{
    try
    {
        TBSCertificateStructure tbsCert = TBSCertificateStructure.getInstance(
                ASN1Primitive.fromByteArray(cert.getTBSCertificate()));

        return new X509Principal(X509Name.getInstance(tbsCert.getIssuer()));
    }
    catch (IOException e)
    {
        throw new CertificateEncodingException(e.toString());
    }
}
 
Example 5
Project: ipack   File: OriginatorInfo.java   Source Code and License 7 votes vote down vote up
/** 
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * OriginatorInfo ::= SEQUENCE {
 *     certs [0] IMPLICIT CertificateSet OPTIONAL,
 *     crls [1] IMPLICIT CertificateRevocationLists OPTIONAL 
 * }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    if (certs != null)
    {
        v.add(new DERTaggedObject(false, 0, certs));
    }
    
    if (crls != null)
    {
        v.add(new DERTaggedObject(false, 1, crls));
    }
    
    return new DERSequence(v);
}
 
Example 6
Project: ipack   File: BasicConstraints.java   Source Code and License 7 votes vote down vote up
/**
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * BasicConstraints := SEQUENCE {
 *    cA                  BOOLEAN DEFAULT FALSE,
 *    pathLenConstraint   INTEGER (0..MAX) OPTIONAL
 * }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    if (cA != null)
    {
        v.add(cA);
    }

    if (pathLenConstraint != null)  // yes some people actually do this when cA is false...
    {
        v.add(pathLenConstraint);
    }

    return new DERSequence(v);
}
 
Example 7
Project: ipack   File: NameConstraints.java   Source Code and License 6 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (permitted != null)
    {
        v.add(new DERTaggedObject(false, 0, new DERSequence(permitted)));
    }

    if (excluded != null)
    {
        v.add(new DERTaggedObject(false, 1, new DERSequence(excluded)));
    }

    return new DERSequence(v);
}
 
Example 8
Project: mDL-ILP   File: DrivingLicence.java   Source Code and License 6 votes vote down vote up
private void parseDG1(byte[] DG1) {
    try (ASN1InputStream bIn = new ASN1InputStream(DG1)) {

        org.bouncycastle.asn1.DERApplicationSpecific app = (DERApplicationSpecific) bIn.readObject();

        ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE);
        Enumeration secEnum = seq.getObjects();
        while (secEnum.hasMoreElements()) {
            ASN1Primitive seqObj = (ASN1Primitive) secEnum.nextElement();
            byte[] data = seqObj.getEncoded();
            if (data[0]== 0x41) {
                Log.d("type approval number", ByteUtils.bytesToHex(data));
                this.set5F01(Bytes.allButFirst(data, 1));
            } else if (data[0] == 0x42) {
                byte[] input = Arrays.copyOfRange(data, 3, data.length);
                parse5F02(input);
            } else if (data[0] == 0x7F) {
                parse7F63(data);
            }
        }
    } catch (IOException e) {
        Log.e(getClass().getName(), e.getMessage(), e);
    }
}
 
Example 9
Project: mDL-ILP   File: DrivingLicence.java   Source Code and License 6 votes vote down vote up
private void parse7F63(byte[] input) {
    Log.d("input", ByteUtils.bytesToHex(input));
    try (ASN1InputStream bIn = new ASN1InputStream(input)) {
        ASN1Primitive obj = bIn.readObject();
        DERApplicationSpecific app = (DERApplicationSpecific) obj;
        ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE);
        Enumeration secEnum = seq.getObjects();
        List<byte[]> categories = new ArrayList<>();
        while (secEnum.hasMoreElements()) {
            ASN1Primitive seqObj = (ASN1Primitive) secEnum.nextElement();
            byte[] data = seqObj.getEncoded();
            Log.d("5F02data", ByteUtils.bytesToHex(data));
            switch (data[0]) {
                case 0x02:
                    Log.d("#CATEGORY","number of categories:" + data[data.length-1]);
                    break;
                case (byte) 0x87:
                    categories.add(Arrays.copyOfRange(data, 2, data.length));
                    break;
            }
        }
        this.set7F63(categories);
    } catch (IOException e) {
        Log.e(getClass().getName(), e.getMessage(), e);
    }
}
 
Example 10
Project: ipack   File: KEKIdentifier.java   Source Code and License 6 votes vote down vote up
/** 
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * KEKIdentifier ::= SEQUENCE {
 *     keyIdentifier OCTET STRING,
 *     date GeneralizedTime OPTIONAL,
 *     other OtherKeyAttribute OPTIONAL 
 * }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    v.add(keyIdentifier);
    
    if (date != null)
    {
        v.add(date);
    }

    if (other != null)
    {
        v.add(other);
    }
    
    return new DERSequence(v);
}
 
Example 11
Project: ipack   File: PollRepContent.java   Source Code and License 6 votes vote down vote up
/**
 * <pre>
 * PollRepContent ::= SEQUENCE OF SEQUENCE {
 *         certReqId              INTEGER,
 *         checkAfter             INTEGER,  -- time in seconds
 *         reason                 PKIFreeText OPTIONAL
 *     }
 * </pre>
 * @return a basic ASN.1 object representation.
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector outer = new ASN1EncodableVector();

    for (int i = 0; i != certReqId.length; i++)
    {
        ASN1EncodableVector v = new ASN1EncodableVector();

        v.add(certReqId[i]);
        v.add(checkAfter[i]);

        if (reason[i] != null)
        {
            v.add(reason[i]);
        }

        outer.add(new DERSequence(v));
    }
    
    return new DERSequence(outer);
}
 
Example 12
Project: xitk   File: Asn1CreateSecretKeyParams.java   Source Code and License 6 votes vote down vote up
public static Asn1CreateSecretKeyParams getInstance(Object obj) throws BadAsn1ObjectException {
    if (obj == null || obj instanceof Asn1CreateSecretKeyParams) {
        return (Asn1CreateSecretKeyParams) obj;
    }

    try {
        if (obj instanceof ASN1Sequence) {
            return new Asn1CreateSecretKeyParams((ASN1Sequence) obj);
        } else if (obj instanceof byte[]) {
            return getInstance(ASN1Primitive.fromByteArray((byte[]) obj));
        } else {
            throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName());
        }
    } catch (IOException | IllegalArgumentException ex) {
        throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(),
                ex);
    }
}
 
Example 13
Project: ipack   File: IETFUtils.java   Source Code and License 6 votes vote down vote up
public static String canonicalize(String s)
{
    String value = Strings.toLowerCase(s.trim());

    if (value.length() > 0 && value.charAt(0) == '#')
    {
        ASN1Primitive obj = decodeObject(value);

        if (obj instanceof ASN1String)
        {
            value = Strings.toLowerCase(((ASN1String)obj).getString().trim());
        }
    }

    value = stripInternalSpaces(value);

    return value;
}
 
Example 14
Project: xitk   File: Asn1P11Params.java   Source Code and License 6 votes vote down vote up
public static Asn1P11Params getInstance(Object obj) throws BadAsn1ObjectException {
    if (obj == null || obj instanceof Asn1P11Params) {
        return (Asn1P11Params) obj;
    }

    try {
        if (obj instanceof ASN1TaggedObject) {
            return new Asn1P11Params((ASN1TaggedObject) obj);
        } else if (obj instanceof byte[]) {
            return getInstance(ASN1Primitive.fromByteArray((byte[]) obj));
        } else {
            throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName());
        }
    } catch (IOException | IllegalArgumentException ex) {
        throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(),
                ex);
    }
}
 
Example 15
Project: ipack   File: X9Curve.java   Source Code and License 6 votes vote down vote up
/**
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 *  Curve ::= SEQUENCE {
 *      a               FieldElement,
 *      b               FieldElement,
 *      seed            BIT STRING      OPTIONAL
 *  }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (fieldIdentifier.equals(prime_field)) 
    { 
        v.add(new X9FieldElement(curve.getA()).toASN1Primitive());
        v.add(new X9FieldElement(curve.getB()).toASN1Primitive());
    } 
    else if (fieldIdentifier.equals(characteristic_two_field)) 
    {
        v.add(new X9FieldElement(curve.getA()).toASN1Primitive());
        v.add(new X9FieldElement(curve.getB()).toASN1Primitive());
    }

    if (seed != null)
    {
        v.add(new DERBitString(seed));
    }

    return new DERSequence(v);
}
 
Example 16
Project: ipack   File: OtherRevRefs.java   Source Code and License 6 votes vote down vote up
private OtherRevRefs(ASN1Sequence seq)
{
    if (seq.size() != 2)
    {
        throw new IllegalArgumentException("Bad sequence size: "
            + seq.size());
    }
    this.otherRevRefType = new ASN1ObjectIdentifier(((ASN1ObjectIdentifier)seq.getObjectAt(0)).getId());
    try
    {
        this.otherRevRefs = ASN1Primitive.fromByteArray(seq.getObjectAt(1)
            .toASN1Primitive().getEncoded(ASN1Encoding.DER));
    }
    catch (IOException e)
    {
        throw new IllegalStateException();
    }
}
 
Example 17
Project: ipack   File: OriginatorInformation.java   Source Code and License 6 votes vote down vote up
/**
 * Return the certificates stored in the underlying OriginatorInfo object.
 *
 * @return a Store of X509CertificateHolder objects.
 */
public Store getCertificates()
{
    ASN1Set certSet = originatorInfo.getCertificates();

    if (certSet != null)
    {
        List certList = new ArrayList(certSet.size());

        for (Enumeration en = certSet.getObjects(); en.hasMoreElements();)
        {
            ASN1Primitive obj = ((ASN1Encodable)en.nextElement()).toASN1Primitive();

            if (obj instanceof ASN1Sequence)
            {
                certList.add(new X509CertificateHolder(Certificate.getInstance(obj)));
            }
        }

        return new CollectionStore(certList);
    }

    return new CollectionStore(new ArrayList());
}
 
Example 18
Project: ipack   File: ExtendedKeyUsage.java   Source Code and License 6 votes vote down vote up
/**
 * @deprecated use KeyPurposeId[] constructor.
 */
public ExtendedKeyUsage(
    Vector usages)
{
    ASN1EncodableVector v = new ASN1EncodableVector();
    Enumeration         e = usages.elements();

    while (e.hasMoreElements())
    {
        ASN1Primitive  o = (ASN1Primitive)e.nextElement();

        v.add(o);
        this.usageTable.put(o, o);
    }

    this.seq = new DERSequence(v);
}
 
Example 19
Project: ipack   File: PrincipalUtil.java   Source Code and License 6 votes vote down vote up
/**
 * return the subject of the given cert as an X509PrincipalObject.
 */
public static X509Principal getSubjectX509Principal(
    X509Certificate cert)
    throws CertificateEncodingException
{
    try
    {
        TBSCertificateStructure tbsCert = TBSCertificateStructure.getInstance(
                ASN1Primitive.fromByteArray(cert.getTBSCertificate()));
        return new X509Principal(X509Name.getInstance(tbsCert.getSubject()));
    }
    catch (IOException e)
    {
        throw new CertificateEncodingException(e.toString());
    }
}
 
Example 20
Project: xitk   File: Asn1ServerCaps.java   Source Code and License 6 votes vote down vote up
public static Asn1ServerCaps getInstance(Object obj) throws BadAsn1ObjectException {
    if (obj == null || obj instanceof Asn1ServerCaps) {
        return (Asn1ServerCaps) obj;
    }

    try {
        if (obj instanceof ASN1Sequence) {
            return new Asn1ServerCaps((ASN1Sequence) obj);
        } else if (obj instanceof byte[]) {
            return getInstance(ASN1Primitive.fromByteArray((byte[]) obj));
        } else {
            throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName());
        }
    } catch (IOException | IllegalArgumentException ex) {
        throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(),
                ex);
    }
}
 
Example 21
Project: ipack   File: ParSet.java   Source Code and License 6 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector seqOfPSh = new ASN1EncodableVector();
    ASN1EncodableVector seqOfPSw = new ASN1EncodableVector();
    ASN1EncodableVector seqOfPSK = new ASN1EncodableVector();

    for (int i = 0; i < h.length; i++)
    {
        seqOfPSh.add(new ASN1Integer(h[i]));
        seqOfPSw.add(new ASN1Integer(w[i]));
        seqOfPSK.add(new ASN1Integer(k[i]));
    }

    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(new ASN1Integer(t));
    v.add(new DERSequence(seqOfPSh));
    v.add(new DERSequence(seqOfPSw));
    v.add(new DERSequence(seqOfPSK));

    return new DERSequence(v);
}
 
Example 22
Project: ipack   File: CertificateHolderAuthorization.java   Source Code and License 6 votes vote down vote up
private void setPrivateData(ASN1InputStream cha)
    throws IOException
{
    ASN1Primitive obj;
    obj = cha.readObject();
    if (obj instanceof ASN1ObjectIdentifier)
    {
        this.oid = (ASN1ObjectIdentifier)obj;
    }
    else
    {
        throw new IllegalArgumentException("no Oid in CerticateHolderAuthorization");
    }
    obj = cha.readObject();
    if (obj instanceof DERApplicationSpecific)
    {
        this.accessRights = (DERApplicationSpecific)obj;
    }
    else
    {
        throw new IllegalArgumentException("No access rights in CerticateHolderAuthorization");
    }
}
 
Example 23
Project: xitk   File: Asn1RemoveObjectsParams.java   Source Code and License 6 votes vote down vote up
public static Asn1RemoveObjectsParams getInstance(Object obj) throws BadAsn1ObjectException {
    if (obj == null || obj instanceof Asn1RemoveObjectsParams) {
        return (Asn1RemoveObjectsParams) obj;
    }

    try {
        if (obj instanceof ASN1Sequence) {
            return new Asn1RemoveObjectsParams((ASN1Sequence) obj);
        } else if (obj instanceof byte[]) {
            return getInstance(ASN1Primitive.fromByteArray((byte[]) obj));
        } else {
            throw new BadAsn1ObjectException("unknown object: " + obj.getClass().getName());
        }
    } catch (IOException | IllegalArgumentException ex) {
        throw new BadAsn1ObjectException("unable to parse encoded object: " + ex.getMessage(),
                ex);
    }
}
 
Example 24
Project: ipack   File: CVCertificate.java   Source Code and License 6 votes vote down vote up
/**
 * @see org.bouncycastle.asn1.ASN1Object#toASN1Primitive()
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (valid != (signValid | bodyValid))
    {
        return null;
    }
    v.add(certificateBody);

    try
    {
        v.add(new DERApplicationSpecific(false, EACTags.STATIC_INTERNAL_AUTHENTIFICATION_ONE_STEP, new DEROctetString(signature)));
    }
    catch (IOException e)
    {
        throw new IllegalStateException("unable to convert signature!");
    }

    return new DERApplicationSpecific(EACTags.CARDHOLDER_CERTIFICATE, v);
}
 
Example 25
Project: ipack   File: ECPrivateKeyStructure.java   Source Code and License 6 votes vote down vote up
private ASN1Primitive getObjectInTag(int tagNo)
{
    Enumeration e = seq.getObjects();

    while (e.hasMoreElements())
    {
        ASN1Encodable obj = (ASN1Encodable)e.nextElement();

        if (obj instanceof ASN1TaggedObject)
        {
            ASN1TaggedObject tag = (ASN1TaggedObject)obj;
            if (tag.getTagNo() == tagNo)
            {
                return (ASN1Primitive)((ASN1Encodable)tag.getObject()).toASN1Primitive();
            }
        }
    }
    return null;
}
 
Example 26
Project: ipack   File: DSTU4145Params.java   Source Code and License 6 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    if (namedCurve != null)
    {
        v.add(namedCurve);
    }
    else
    {
        v.add(ecbinary);
    }

    if (!org.bouncycastle.util.Arrays.areEqual(dke, DEFAULT_DKE))
    {
        v.add(new DEROctetString(dke));
    }

    return new DERSequence(v);
}
 
Example 27
Project: ipack   File: DigestInfo.java   Source Code and License 6 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    v.add(algId);
    v.add(new DEROctetString(digest));

    return new DERSequence(v);
}
 
Example 28
Project: ipack   File: EnvelopedDataHelper.java   Source Code and License 6 votes vote down vote up
AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier encryptionOID, AlgorithmParameters params)
    throws CMSException
{
    ASN1Encodable asn1Params;
    if (params != null)
    {
        try
        {
            asn1Params = ASN1Primitive.fromByteArray(params.getEncoded("ASN.1"));
        }
        catch (IOException e)
        {
            throw new CMSException("cannot encode parameters: " + e.getMessage(), e);
        }
    }
    else
    {
        asn1Params = DERNull.INSTANCE;
    }

    return new AlgorithmIdentifier(
        encryptionOID,
        asn1Params);
}
 
Example 29
Project: ipack   File: NamingAuthority.java   Source Code and License 6 votes vote down vote up
/**
 * Produce an object suitable for an ASN1OutputStream.
 * <p/>
 * Returns:
 * <p/>
 * <pre>
 *             NamingAuthority ::= SEQUENCE
 *             {
 *               namingAuthorityId OBJECT IDENTIFIER OPTIONAL,
 *               namingAuthorityUrl IA5String OPTIONAL,
 *               namingAuthorityText DirectoryString(SIZE(1..128)) OPTIONAL
 *             }
 * </pre>
 *
 * @return a DERObject
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector vec = new ASN1EncodableVector();
    if (namingAuthorityId != null)
    {
        vec.add(namingAuthorityId);
    }
    if (namingAuthorityUrl != null)
    {
        vec.add(new DERIA5String(namingAuthorityUrl, true));
    }
    if (namingAuthorityText != null)
    {
        vec.add(namingAuthorityText);
    }
    return new DERSequence(vec);
}
 
Example 30
Project: ipack   File: CVCertificate.java   Source Code and License 6 votes vote down vote up
private void initFrom(ASN1InputStream aIS)
    throws IOException
{
    ASN1Primitive obj;
    while ((obj = aIS.readObject()) != null)
    {
        if (obj instanceof DERApplicationSpecific)
        {
            setPrivateData((DERApplicationSpecific)obj);
        }
        else
        {
            throw new IOException("Invalid Input Stream for creating an Iso7816CertificateStructure");
        }
    }
}
 
Example 31
Project: ipack   File: Extensions.java   Source Code and License 6 votes vote down vote up
/**
 * <pre>
 *     Extensions        ::=   SEQUENCE SIZE (1..MAX) OF Extension
 *
 *     Extension         ::=   SEQUENCE {
 *        extnId            EXTENSION.&amp;id ({ExtensionSet}),
 *        critical          BOOLEAN DEFAULT FALSE,
 *        extnValue         OCTET STRING }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector vec = new ASN1EncodableVector();
    Enumeration e = ordering.elements();

    while (e.hasMoreElements())
    {
        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier)e.nextElement();
        Extension ext = (Extension)extensions.get(oid);

        vec.add(ext);
    }

    return new DERSequence(vec);
}
 
Example 32
Project: ipack   File: X509DefaultEntryConverter.java   Source Code and License 5 votes vote down vote up
/**
 * Apply default coversion for the given value depending on the oid
 * and the character range of the value.
 * 
 * @param oid the object identifier for the DN entry
 * @param value the value associated with it
 * @return the ASN.1 equivalent for the string value.
 */
public ASN1Primitive getConvertedValue(
    ASN1ObjectIdentifier  oid,
    String               value)
{
    if (value.length() != 0 && value.charAt(0) == '#')
    {
        try
        {
            return convertHexEncoded(value, 1);
        }
        catch (IOException e)
        {
            throw new RuntimeException("can't recode value for oid " + oid.getId());
        }
    }
    else
    {
        if (value.length() != 0 && value.charAt(0) == '\\')
        {
            value = value.substring(1);
        }
        if (oid.equals(X509Name.EmailAddress) || oid.equals(X509Name.DC))
        {
            return new DERIA5String(value);
        }
        else if (oid.equals(X509Name.DATE_OF_BIRTH))  // accept time string as well as # (for compatibility)
        {
            return new DERGeneralizedTime(value);
        }
        else if (oid.equals(X509Name.C) || oid.equals(X509Name.SN) || oid.equals(X509Name.DN_QUALIFIER)
            || oid.equals(X509Name.TELEPHONE_NUMBER))
        {
             return new DERPrintableString(value);
        }
    }
    
    return new DERUTF8String(value);
}
 
Example 33
Project: mDL-ILP   File: DrivingLicence.java   Source Code and License 5 votes vote down vote up
private void parseDG15(byte[] DG15) {

        try (ASN1InputStream bIn = new ASN1InputStream(DG15)) {
            DERApplicationSpecific app = (DERApplicationSpecific) bIn.readObject();

            ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE);
            byte[] data = ((ASN1Primitive)seq.getObjects().nextElement()).getEncoded();

            Log.d(getClass().getName(), "Data = "+ Bytes.hexString(data));

            try (ASN1InputStream in = new ASN1InputStream(data)) {
                Enumeration seq1 = ((DLSequence) in.readObject()).getObjects();

                while (seq1.hasMoreElements()) {
                    ASN1Primitive obj = (ASN1Primitive)seq1.nextElement();
                    byte[] data1 = obj.getEncoded();
                    Log.d(getClass().getName(), "data1 = "+ Bytes.hexString(data1));

                    if (data1[0] == (byte) 0x01) {
                        this.set18(data1[2] == 0x01);
                    } else if (data1[0] == (byte) 0x02) {
                        this.setAge(Bytes.toInt(data1[2]));
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
Example 34
Project: xitk   File: Asn1ServerCaps.java   Source Code and License 5 votes vote down vote up
@Override
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector vecVersions = new ASN1EncodableVector();
    for (Short version : versions) {
        vecVersions.add(new ASN1Integer(BigInteger.valueOf(version)));
    }

    ASN1EncodableVector vec = new ASN1EncodableVector();
    vec.add(ASN1Boolean.getInstance(readOnly));
    vec.add(new DERSequence(vecVersions));
    return new DERSequence(vec);
}
 
Example 35
Project: ipack   File: BCECGOST3410PrivateKey.java   Source Code and License 5 votes vote down vote up
private DERBitString getPublicKeyDetails(BCECGOST3410PublicKey pub)
{
    try
    {
        SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(pub.getEncoded()));

        return info.getPublicKeyData();
    }
    catch (IOException e)
    {   // should never happen
        return null;
    }
}
 
Example 36
Project: ipack   File: ContentInfo.java   Source Code and License 5 votes vote down vote up
/**
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * ContentInfo ::= SEQUENCE {
 *          contentType ContentType,
 *          content
 *          [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    v.add(contentType);

    if (content != null)
    {
        v.add(new BERTaggedObject(0, content));
    }

    return new BERSequence(v);
}
 
Example 37
Project: ipack   File: CertificationRequestInfo.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    v.add(version);
    v.add(subject);
    v.add(subjectPKInfo);

    if (attributes != null)
    {
        v.add(new DERTaggedObject(false, 0, attributes));
    }

    return new DERSequence(v);
}
 
Example 38
Project: ipack   File: ResponderID.java   Source Code and License 5 votes vote down vote up
/**
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * ResponderID ::= CHOICE {
 *      byName          [1] Name,
 *      byKey           [2] KeyHash }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    if (value instanceof ASN1OctetString)
    {
        return new DERTaggedObject(true, 2, value);
    }

    return new DERTaggedObject(true, 1, value);
}
 
Example 39
Project: ipack   File: SignerInformation.java   Source Code and License 5 votes vote down vote up
private ASN1Primitive getSingleValuedSignedAttribute(
    ASN1ObjectIdentifier attrOID, String printableName)
    throws CMSException
{
    AttributeTable unsignedAttrTable = this.getUnsignedAttributes();
    if (unsignedAttrTable != null
        && unsignedAttrTable.getAll(attrOID).size() > 0)
    {
        throw new CMSException("The " + printableName
            + " attribute MUST NOT be an unsigned attribute");
    }

    AttributeTable signedAttrTable = this.getSignedAttributes();
    if (signedAttrTable == null)
    {
        return null;
    }

    ASN1EncodableVector v = signedAttrTable.getAll(attrOID);
    switch (v.size())
    {
        case 0:
            return null;
        case 1:
        {
            Attribute t = (Attribute)v.get(0);
            ASN1Set attrValues = t.getAttrValues();
            if (attrValues.size() != 1)
            {
                throw new CMSException("A " + printableName
                    + " attribute MUST have a single attribute value");
            }

            return attrValues.getObjectAt(0).toASN1Primitive();
        }
        default:
            throw new CMSException("The SignedAttributes in a signerInfo MUST NOT include multiple instances of the "
                + printableName + " attribute");
    }
}
 
Example 40
Project: ipack   File: PKCS12KeyStoreSpi.java   Source Code and License 5 votes vote down vote up
private SubjectKeyIdentifier createSubjectKeyId(
    PublicKey pubKey)
{
    try
    {
        SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
            (ASN1Sequence)ASN1Primitive.fromByteArray(pubKey.getEncoded()));

        return new SubjectKeyIdentifier(info);
    }
    catch (Exception e)
    {
        throw new RuntimeException("error creating key");
    }
}
 
Example 41
Project: ipack   File: KeyAgreeRecipientIdentifier.java   Source Code and License 5 votes vote down vote up
/** 
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * KeyAgreeRecipientIdentifier ::= CHOICE {
 *     issuerAndSerialNumber IssuerAndSerialNumber,
 *     rKeyId [0] IMPLICIT RecipientKeyIdentifier
 * }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    if (issuerSerial != null)
    {
        return issuerSerial.toASN1Primitive();
    }

    return new DERTaggedObject(false, 0, rKeyID);
}
 
Example 42
Project: ipack   File: RecipientEncryptedKey.java   Source Code and License 5 votes vote down vote up
/** 
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * RecipientEncryptedKey ::= SEQUENCE {
 *     rid KeyAgreeRecipientIdentifier,
 *     encryptedKey EncryptedKey
 * }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    v.add(identifier);
    v.add(encryptedKey);

    return new DERSequence(v);
}
 
Example 43
Project: ipack   File: DVCSTime.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{

    if (genTime != null)
    {
        return genTime;
    }

    if (timeStampToken != null)
    {
        return timeStampToken.toASN1Primitive();
    }

    return null;
}
 
Example 44
Project: ipack   File: Evidence.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
   if (tstEvidence != null)
   {
       return new DERTaggedObject(false, 0, tstEvidence);
   }

   return null;
}
 
Example 45
Project: ipack   File: DHParameter.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    v.add(p);
    v.add(g);

    if (this.getL() != null)
    {
        v.add(l);
    }

    return new DERSequence(v);
}
 
Example 46
Project: ipack   File: DVCSErrorNotice.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(transactionStatus);
    if (transactionIdentifier != null)
    {
        v.add(transactionIdentifier);
    }
    return new DERSequence(v);
}
 
Example 47
Project: ipack   File: BCECGOST3410PrivateKey.java   Source Code and License 5 votes vote down vote up
private void readObject(
    ObjectInputStream in)
    throws IOException, ClassNotFoundException
{
    in.defaultReadObject();

    byte[] enc = (byte[])in.readObject();

    populateFromPrivKeyInfo(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(enc)));

    this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
}
 
Example 48
Project: ipack   File: TSTInfo.java   Source Code and License 5 votes vote down vote up
/**
 * <pre>
 * 
 *     TSTInfo ::= SEQUENCE  {
 *        version                      INTEGER  { v1(1) },
 *        policy                       TSAPolicyId,
 *        messageImprint               MessageImprint,
 *          -- MUST have the same value as the similar field in
 *          -- TimeStampReq
 *        serialNumber                 INTEGER,
 *         -- Time-Stamping users MUST be ready to accommodate integers
 *         -- up to 160 bits.
 *        genTime                      GeneralizedTime,
 *        accuracy                     Accuracy                 OPTIONAL,
 *        ordering                     BOOLEAN             DEFAULT FALSE,
 *        nonce                        INTEGER                  OPTIONAL,
 *          -- MUST be present if the similar field was present
 *          -- in TimeStampReq.  In that case it MUST have the same value.
 *        tsa                          [0] GeneralName          OPTIONAL,
 *        extensions                   [1] IMPLICIT Extensions   OPTIONAL  }
 * 
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector seq = new ASN1EncodableVector();
    seq.add(version);

    seq.add(tsaPolicyId);
    seq.add(messageImprint);
    seq.add(serialNumber);
    seq.add(genTime);

    if (accuracy != null)
    {
        seq.add(accuracy);
    }
    
    if (ordering != null && ordering.isTrue())
    {
        seq.add(ordering);
    }
    
    if (nonce != null)
    {
        seq.add(nonce);
    }
    
    if (tsa != null)
    {
        seq.add(new DERTaggedObject(true, 0, tsa));
    }
    
    if (extensions != null)
    {
        seq.add(new DERTaggedObject(false, 1, extensions));
    }

    return new DERSequence(seq);
}
 
Example 49
Project: ipack   File: X9FieldID.java   Source Code and License 5 votes vote down vote up
/**
 * Produce a DER encoding of the following structure.
 * <pre>
 *  FieldID ::= SEQUENCE {
 *      fieldType       FIELD-ID.&amp;id({IOSet}),
 *      parameters      FIELD-ID.&amp;Type({IOSet}{&#64;fieldType})
 *  }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(this.id);
    v.add(this.parameters);

    return new DERSequence(v);
}
 
Example 50
Project: ipack   File: X962Parameters.java   Source Code and License 5 votes vote down vote up
public static X962Parameters getInstance(
    Object obj)
{
    if (obj == null || obj instanceof X962Parameters) 
    {
        return (X962Parameters)obj;
    }
    
    if (obj instanceof ASN1Primitive) 
    {
        return new X962Parameters((ASN1Primitive)obj);
    }
    
    throw new IllegalArgumentException("unknown object in getInstance()");
}
 
Example 51
Project: ipack   File: PolicyQualifierInfo.java   Source Code and License 5 votes vote down vote up
/**
 * Returns a DER-encodable representation of this instance. 
 *
 * @return a <code>ASN1Primitive</code> value
 */
public ASN1Primitive toASN1Primitive()
{
   ASN1EncodableVector dev = new ASN1EncodableVector();
   dev.add(policyQualifierId);
   dev.add(qualifier);

   return new DERSequence(dev);
}
 
Example 52
Project: xitk   File: Asn1P11ObjectIdentifier.java   Source Code and License 5 votes vote down vote up
@Override
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector vec = new ASN1EncodableVector();
    vec.add(new DEROctetString(objectId.id()));
    vec.add(new DERUTF8String(objectId.label()));
    return new DERSequence(vec);
}
 
Example 53
Project: ipack   File: AuthEnvelopedData.java   Source Code and License 5 votes vote down vote up
/**
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * AuthEnvelopedData ::= SEQUENCE {
 *   version CMSVersion,
 *   originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
 *   recipientInfos RecipientInfos,
 *   authEncryptedContentInfo EncryptedContentInfo,
 *   authAttrs [1] IMPLICIT AuthAttributes OPTIONAL,
 *   mac MessageAuthenticationCode,
 *   unauthAttrs [2] IMPLICIT UnauthAttributes OPTIONAL }
 * </pre>
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(version);

    if (originatorInfo != null)
    {
        v.add(new DERTaggedObject(false, 0, originatorInfo));
    }

    v.add(recipientInfos);
    v.add(authEncryptedContentInfo);

    // "authAttrs optionally contains the authenticated attributes."
    if (authAttrs != null)
    {
        // "AuthAttributes MUST be DER encoded, even if the rest of the
        // AuthEnvelopedData structure is BER encoded."
        v.add(new DERTaggedObject(false, 1, authAttrs));
    }

    v.add(mac);

    // "unauthAttrs optionally contains the unauthenticated attributes."
    if (unauthAttrs != null)
    {
        v.add(new DERTaggedObject(false, 2, unauthAttrs));
    }

    return new BERSequence(v);
}
 
Example 54
Project: ipack   File: ProtectedPart.java   Source Code and License 5 votes vote down vote up
/**
 * <pre>
 * ProtectedPart ::= SEQUENCE {
 *                    header    PKIHeader,
 *                    body      PKIBody
 * }
 * </pre>
 * @return a basic ASN.1 object representation.
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(header);
    v.add(body);

    return new DERSequence(v);
}
 
Example 55
Project: ipack   File: DVCSResponse.java   Source Code and License 5 votes vote down vote up
public static DVCSResponse getInstance(Object obj)
{
    if (obj == null || obj instanceof DVCSResponse)
    {
        return (DVCSResponse)obj;
    }
    else
    {
        if (obj instanceof byte[])
        {
            try
            {
                return getInstance(ASN1Primitive.fromByteArray((byte[])obj));
            }
            catch (IOException e)
            {
                throw new IllegalArgumentException("failed to construct sequence from byte[]: " + e.getMessage());
            }
        }
        if (obj instanceof ASN1Sequence)
        {
            DVCSCertInfo dvCertInfo = DVCSCertInfo.getInstance(obj);

            return new DVCSResponse(dvCertInfo);
        }
        if (obj instanceof ASN1TaggedObject)
        {
            ASN1TaggedObject t = ASN1TaggedObject.getInstance(obj);
            DVCSErrorNotice dvErrorNote = DVCSErrorNotice.getInstance(t, false);

            return new DVCSResponse(dvErrorNote);
        }
    }

    throw new IllegalArgumentException("Couldn't convert from object to DVCSResponse: " + obj.getClass().getName());
}
 
Example 56
Project: ipack   File: CrlValidatedID.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(this.crlHash.toASN1Primitive());
    if (null != this.crlIdentifier)
    {
        v.add(this.crlIdentifier.toASN1Primitive());
    }
    return new DERSequence(v);
}
 
Example 57
Project: ipack   File: LDSVersionInfo.java   Source Code and License 5 votes vote down vote up
/**
 * <pre>
 * LDSVersionInfo ::= SEQUENCE {
 *    ldsVersion PRINTABLE STRING
 *    unicodeVersion PRINTABLE STRING
 *  }
 * </pre>
 * @return
 */
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector v = new ASN1EncodableVector();

    v.add(ldsVersion);
    v.add(unicodeVersion);

    return new DERSequence(v);
}
 
Example 58
Project: xitk   File: Asn1GenSM2KeypairParams.java   Source Code and License 5 votes vote down vote up
@Override
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector vector = new ASN1EncodableVector();
    vector.add(new Asn1P11SlotIdentifier(slotId));
    vector.add(new DERUTF8String(label));
    return new DERSequence(vector);
}
 
Example 59
Project: ipack   File: DataGroupHash.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector seq = new ASN1EncodableVector();
    seq.add(dataGroupNumber);
    seq.add(dataGroupHashValue);  

    return new DERSequence(seq);
}
 
Example 60
Project: ipack   File: ElGamalParameter.java   Source Code and License 5 votes vote down vote up
public ASN1Primitive toASN1Primitive()
{
    ASN1EncodableVector  v = new ASN1EncodableVector();

    v.add(p);
    v.add(g);

    return new DERSequence(v);
}