Example usage for org.bouncycastle.asn1 ASN1Sequence getObjectAt

List of usage examples for org.bouncycastle.asn1 ASN1Sequence getObjectAt

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1Sequence getObjectAt.

Prototype

public ASN1Encodable getObjectAt(int index) 

Source Link

Document

Return the object at the sequence position indicated by index.

Usage

From source file:net.sf.keystore_explorer.crypto.x509.CRLDistributionPoints.java

License:Open Source License

private CRLDistributionPoints(ASN1Sequence seq) {
    distributionPointList = new ArrayList<DistributionPoint>();
    for (int i = 0; i != seq.size(); i++) {
        distributionPointList.add(DistributionPoint.getInstance(seq.getObjectAt(i)));
    }/* w w  w .  ja va2 s  .c o  m*/
}

From source file:net.sf.keystore_explorer.crypto.x509.GeneralNameUtil.java

License:Open Source License

/**
 * Parse UPN/otherName/*from   w  w w  .java 2 s . co  m*/
 *
 * @param generalName otherName object
 * @return UPN as string
 */
public static String parseUPN(GeneralName generalName) {
    // OtherName ::= SEQUENCE {
    //    type-id OBJECT IDENTIFIER,
    //    value [0] EXPLICIT ANY DEFINED BY type-id }

    ASN1Sequence otherName = (ASN1Sequence) generalName.getName();
    ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) otherName.getObjectAt(0);

    if (UPN_OID.equals(oid.getId())) {
        DERTaggedObject derTaggedObject = (DERTaggedObject) otherName.getObjectAt(1);
        DERUTF8String upn = DERUTF8String.getInstance(derTaggedObject.getObject());
        return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"), "UPN", upn.getString());
    }

    // fallback to generic handling
    ASN1Encodable value = otherName.getObjectAt(1);
    try {
        return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"),
                ObjectIdUtil.toString(oid),
                HexUtil.getHexString(value.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
    } catch (IOException e) {
        return MessageFormat.format(res.getString("GeneralNameUtil.OtherGeneralName"),
                ObjectIdUtil.toString(oid), "");
    }
}

From source file:net.sf.keystore_explorer.crypto.x509.GeneralNameUtil.java

License:Open Source License

/**
 * Get string representation for all General Names.
 *
 * @param generalName/*from w ww. j  av a 2  s. c o  m*/
 *            General name
 * @return String representation of general name
 * @throws IOException
 *             If general name is invalid
 */
public static String toString(GeneralName generalName) throws IOException {

    if (generalName == null) {
        return "";
    }

    switch (generalName.getTagNo()) {
    case GeneralName.ediPartyName: {

        /* EDIPartyName ::= SEQUENCE {
         *      nameAssigner            [0]     DirectoryString OPTIONAL,
         *      partyName               [1]     DirectoryString }
         */
        ASN1Sequence ediPartyName = (ASN1Sequence) generalName.getName();

        DirectoryString nameAssigner = DirectoryString.getInstance(ediPartyName.getObjectAt(0));
        DirectoryString partyName = DirectoryString.getInstance(ediPartyName.getObjectAt(1));

        String nameAssignerStr = null;
        if (nameAssigner != null) { // Optional
            nameAssignerStr = nameAssigner.getString();
        }

        String partyNameStr = partyName.getString();
        if (nameAssignerStr != null) {
            return MessageFormat.format(res.getString("GeneralNameUtil.EdiPartyGeneralName"), nameAssignerStr,
                    partyNameStr);
        } else {
            return MessageFormat.format(res.getString("GeneralNameUtil.EdiPartyGeneralNameNoAssigner"),
                    partyNameStr);
        }
    }
    case GeneralName.otherName: {

        return parseUPN(generalName);
    }
    case GeneralName.x400Address: {
        /*
         * No support for this at the moment - just get a hex dump
         * The Oracle CertificateFactory blows up if a certificate extension contains this anyway
         */
        ASN1Encodable x400Address = generalName.getName();

        return MessageFormat.format(res.getString("GeneralNameUtil.X400AddressGeneralName"),
                HexUtil.getHexString(x400Address.toASN1Primitive().getEncoded(ASN1Encoding.DER)));
    }
    default: {
        return safeToString(generalName, true);
    }
    }
}

From source file:net.sf.keystore_explorer.crypto.x509.GeneralSubtrees.java

License:Open Source License

private GeneralSubtrees(ASN1Sequence seq) {
    subtrees = new ArrayList<GeneralSubtree>();
    for (int i = 0; i < seq.size(); i++) {
        subtrees.add(GeneralSubtree.getInstance(seq.getObjectAt(i)));
    }//w  ww  . java2  s  .com
}

From source file:net.sf.keystore_explorer.crypto.x509.PolicyConstraints.java

License:Open Source License

private PolicyConstraints(ASN1Sequence seq) {
    if (seq.size() > 2) {
        throw new IllegalArgumentException("sequence length > 2");
    }//from w  w  w.j  a va  2 s .  c o m

    for (int i = 0; i < seq.size(); i++) {
        ASN1TaggedObject taggedObj = ASN1TaggedObject.getInstance(seq.getObjectAt(i));
        switch (taggedObj.getTagNo()) {
        case 0:
            requireExplicitPolicy = ASN1Integer.getInstance(taggedObj.getObject()).getValue().intValue();
            break;
        case 1:
            inhibitPolicyMapping = ASN1Integer.getInstance(taggedObj.getObject()).getValue().intValue();
            break;
        default:
            throw new IllegalArgumentException("wrong tag number");
        }
    }
}

From source file:net.sf.keystore_explorer.crypto.x509.PolicyInformationUtil.java

License:Open Source License

/**
 * Get string representation of policy information.
 *
 * @param policyInformation/*  ww w .  j a  v  a  2 s. c  om*/
 *            Policy information
 * @return String representation of policy information
 * @throws IOException
 *             If policy information is invalid
 */
public static String toString(PolicyInformation policyInformation) throws IOException {
    StringBuffer sbPolicyInformation = new StringBuffer();

    ASN1ObjectIdentifier policyIdentifier = policyInformation.getPolicyIdentifier();

    sbPolicyInformation.append(MessageFormat.format(res.getString("PolicyInformationUtil.PolicyIdentifier"),
            policyIdentifier.getId()));

    ASN1Sequence policyQualifiers = policyInformation.getPolicyQualifiers();

    if (policyQualifiers != null) {
        sbPolicyInformation.append(", ");

        StringBuffer sbPolicyQualifiers = new StringBuffer();

        for (int i = 0; i < policyQualifiers.size(); i++) {
            PolicyQualifierInfo policyQualifierInfo = PolicyQualifierInfo
                    .getInstance(policyQualifiers.getObjectAt(i));

            sbPolicyQualifiers.append(toString(policyQualifierInfo));

            if ((i + 1) < policyQualifiers.size()) {
                sbPolicyQualifiers.append(", ");
            }
        }

        sbPolicyInformation.append(MessageFormat.format(res.getString("PolicyInformationUtil.PolicyQualifiers"),
                sbPolicyQualifiers));
    }

    return sbPolicyInformation.toString();
}

From source file:net.sf.keystore_explorer.crypto.x509.PolicyMapping.java

License:Open Source License

private PolicyMapping(ASN1Sequence seq) {
    // java object in sequence is actually not ASN1ObjectIdentifier but CertPolicyId,
    // so we do a conversion in order to avoid possible class cast exception here
    this.issuerDomainPolicy = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0).toASN1Primitive());
    this.subjectDomainPolicy = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(1).toASN1Primitive());
}

From source file:net.sf.keystore_explorer.crypto.x509.SubjectInfoAccess.java

License:Open Source License

private SubjectInfoAccess(ASN1Sequence seq) {
    accessDescriptions = new Vector<AccessDescription>();

    for (int i = 0; i != seq.size(); i++) {
        accessDescriptions.add(AccessDescription.getInstance(seq.getObjectAt(i)));
    }//from w w w .j  a va2 s  .  c  o  m
}

From source file:net.sf.keystore_explorer.crypto.x509.X509Ext.java

License:Open Source License

private String getEntrustVersionInformationStringValue(byte[] value) throws IOException {
    // @formatter:off

    /*/*from  w w w. java 2  s.  c o  m*/
     * EntrustVersInfoSyntax ::= OCTET STRING
     *
     * entrustVersInfo EXTENSION ::= { SYNTAX EntrustVersInfoSyntax,
     * IDENTIFIED BY {id-entrust 0} }
     *
     * EntrustVersInfoSyntax ::= ASN1Sequence { entrustVers GeneralString,
     * entrustInfoFlags EntrustInfoFlags }
     *
     * EntrustInfoFlags ::= BIT STRING { keyUpdateAllowed newExtensions (1),
     * pKIXCertificate (2) }
     */

    // @formatter:on

    StringBuilder sb = new StringBuilder();

    ASN1Sequence entrustVersInfo = (ASN1Sequence) ASN1Primitive.fromByteArray(value);

    DERGeneralString entrustVers = (DERGeneralString) entrustVersInfo.getObjectAt(0);
    DERBitString entrustInfoFlags = (DERBitString) entrustVersInfo.getObjectAt(1);

    sb.append(MessageFormat.format(res.getString("EntrustVersion"), entrustVers.getString()));
    sb.append(NEWLINE);
    sb.append(MessageFormat.format(res.getString("EntrustInformationFlags"), entrustInfoFlags.getString()));
    sb.append(NEWLINE);

    return sb.toString();
}

From source file:net.sf.keystore_explorer.crypto.x509.X509Ext.java

License:Open Source License

private String getCertificatePoliciesStringValue(byte[] value) throws IOException {
    // @formatter:off

    /*/*www .ja  v  a 2  s.  c  om*/
     * CertificatePolicies ::= ASN1Sequence SIZE (1..MAX) OF PolicyInformation
     *
     * PolicyInformation ::= ASN1Sequence
     * {
     *      policyIdentifier CertPolicyId,
     *      policyQualifiers ASN1Sequence SIZE (1..MAX) OF PolicyQualifierInfo OPTIONAL
     * }
     *
     * CertPolicyId ::= OBJECT IDENTIFIER
     *
     * PolicyQualifierInfo ::= ASN1Sequence
     * {
     *      policyQualifierId PolicyQualifierId,
     *      qualifier ANY DEFINED BY policyQualifierId
     * }
     *
     * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
     *
     * Qualifier ::= CHOICE
     * {
     *      cPSuri CPSuri,
     *      userNotice UserNotice
     * }
     *
     * CPSuri ::= DERIA5String
     *
     * UserNotice ::= ASN1Sequence
     * {
     *      noticeRef NoticeReference OPTIONAL,
     *      explicitText DisplayText OPTIONAL
     * }
     *
     * NoticeReference ::= ASN1Sequence
     * {
     *      organization DisplayText,
     *      noticeNumbers ASN1Sequence OF ASN1Integer
     * }
     *
     * DisplayText ::= CHOICE
     * {
     *      ia5String DERIA5String (SIZE (1..200)),
     *      visibleString VisibleString (SIZE (1..200)),
     *      bmpString BMPString (SIZE (1..200)),
     *      utf8String UTF8String (SIZE (1..200))
     * }
     */

    // @formatter:on

    StringBuilder sb = new StringBuilder();

    CertificatePolicies certificatePolicies = CertificatePolicies.getInstance(value);

    int certPolicy = 0;

    for (PolicyInformation policyInformation : certificatePolicies.getPolicyInformation()) {
        certPolicy++;

        sb.append(MessageFormat.format(res.getString("CertificatePolicy"), certPolicy));
        sb.append(NEWLINE);

        ASN1ObjectIdentifier policyIdentifier = policyInformation.getPolicyIdentifier();
        String policyIdentifierStr = ObjectIdUtil.toString(policyIdentifier);

        sb.append(INDENT);
        sb.append(MessageFormat.format(res.getString("PolicyIdentifier"), policyIdentifierStr));
        sb.append(NEWLINE);

        ASN1Sequence policyQualifiers = policyInformation.getPolicyQualifiers();

        if (policyQualifiers != null) { // Optional
            int policyQual = 0;

            for (ASN1Encodable policyQualifier : policyQualifiers.toArray()) {

                ASN1Sequence policyQualifierInfo = (ASN1Sequence) policyQualifier;

                sb.append(INDENT.toString(1));
                sb.append(MessageFormat.format(res.getString("PolicyQualifierInformation"), certPolicy,
                        ++policyQual));
                sb.append(NEWLINE);

                ASN1ObjectIdentifier policyQualifierId = (ASN1ObjectIdentifier) policyQualifierInfo
                        .getObjectAt(0);

                CertificatePolicyQualifierType certificatePolicyQualifierType = CertificatePolicyQualifierType
                        .resolveOid(policyQualifierId.getId());

                if (certificatePolicyQualifierType != null) {
                    sb.append(INDENT.toString(2));
                    sb.append(certificatePolicyQualifierType.friendly());
                    sb.append(NEWLINE);

                    if (certificatePolicyQualifierType == PKIX_CPS_POINTER_QUALIFIER) {
                        DERIA5String cpsPointer = (DERIA5String) policyQualifierInfo.getObjectAt(1);

                        sb.append(INDENT.toString(2));
                        sb.append(MessageFormat.format(res.getString("CpsPointer"),
                                "<a href=\"" + cpsPointer + "\">" + cpsPointer + "</a>"));
                        sb.append(NEWLINE);
                    } else if (certificatePolicyQualifierType == PKIX_USER_NOTICE_QUALIFIER) {
                        ASN1Encodable userNoticeObj = policyQualifierInfo.getObjectAt(1);

                        UserNotice userNotice = UserNotice.getInstance(userNoticeObj);

                        sb.append(INDENT.toString(2));
                        sb.append(res.getString("UserNotice"));
                        sb.append(NEWLINE);

                        NoticeReference noticeReference = userNotice.getNoticeRef();

                        DisplayText explicitText = userNotice.getExplicitText();

                        if (noticeReference != null) { // Optional
                            sb.append(INDENT.toString(3));
                            sb.append(res.getString("NoticeReference"));
                            sb.append(NEWLINE);

                            DisplayText organization = noticeReference.getOrganization();
                            String organizationString = organization.getString();

                            sb.append(INDENT.toString(4));
                            sb.append(MessageFormat.format(res.getString("Organization"), organizationString));
                            sb.append(NEWLINE);

                            ASN1Integer[] noticeNumbers = noticeReference.getNoticeNumbers();

                            StringBuilder sbNoticeNumbers = new StringBuilder();
                            for (ASN1Integer noticeNumber : noticeNumbers) {
                                sbNoticeNumbers.append(noticeNumber.getValue().intValue());
                                sbNoticeNumbers.append(", ");
                            }
                            sbNoticeNumbers.setLength(sbNoticeNumbers.length() - 2);

                            sb.append(INDENT.toString(4));
                            sb.append(MessageFormat.format(res.getString("NoticeNumbers"),
                                    sbNoticeNumbers.toString()));
                            sb.append(NEWLINE);
                        }

                        if (explicitText != null) { // Optional
                            String explicitTextString = explicitText.getString();

                            sb.append(INDENT.toString(3));
                            sb.append(MessageFormat.format(res.getString("ExplicitText"), explicitTextString));
                            sb.append(NEWLINE);
                        }
                    }
                }
            }
        }
    }

    return sb.toString();
}