Example usage for org.bouncycastle.asn1.x509 UserNotice getNoticeRef

List of usage examples for org.bouncycastle.asn1.x509 UserNotice getNoticeRef

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 UserNotice getNoticeRef.

Prototype

public NoticeReference getNoticeRef() 

Source Link

Usage

From source file:com.otterca.common.crypto.SimplePolicyGeneratorTest.java

License:Apache License

/**
 * Test behavior when user notice is set.
 * // w  ww.j a  v a 2 s .c  o  m
 * @throws IOException
 */
@Test
@edu.umd.cs.findbugs.annotations.SuppressWarnings("NP_NONNULL_PARAM_VIOLATION")
public void testUserNoticePolicy() throws IOException {
    SimplePolicyGeneratorImpl generator = new SimplePolicyGeneratorImpl(null, ORGANIZATION, USER_NOTICE,
            Integer.valueOf(1));

    // get policy extensions
    byte[] policyBytes = generator.getExtension(SUBJECT, ISSUER);
    assertNotNull(policyBytes);

    X509Extensions exts = X509Extensions.getInstance(DLSequence.fromByteArray(policyBytes));
    ASN1Encodable asn1 = exts.getExtension(X509Extensions.CertificatePolicies).getParsedValue();
    CertificatePolicies policies = CertificatePolicies.getInstance(asn1);
    assertNotNull(policies, "unable to find CertificatePolicies extension");

    for (PolicyInformation info : policies.getPolicyInformation()) {
        if (id_qt_unotice.equals(info.getPolicyIdentifier())) {
            DLSequence dls = (DLSequence) info.getPolicyQualifiers();
            for (int i = 0; i < dls.size(); i++) {
                UserNotice userNotice = UserNotice.getInstance((DLSequence) dls.getObjectAt(i));
                assertEquals(userNotice.getNoticeRef().getOrganization().getString(), ORGANIZATION);
                assertEquals(userNotice.getNoticeRef().getNoticeNumbers()[0].getValue(), BigInteger.ONE);
                assertEquals(userNotice.getExplicitText().getString(), USER_NOTICE);
            }
        } else {
            fail("unknown policy identifier: " + info.getPolicyIdentifier());
        }
    }
}

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

License:Open Source License

/**
 * Get string representation of user notice.
 *
 * @param userNotice//from w w  w. j  a  va  2s  .  c o m
 *            User notice
 * @return String representation of user notice
 */
public static String toString(UserNotice userNotice) {
    StringBuffer sbUserNotice = new StringBuffer();

    NoticeReference noticeReference = userNotice.getNoticeRef();

    if (noticeReference != null) {
        DisplayText organization = noticeReference.getOrganization();

        if (organization != null) {
            sbUserNotice.append(MessageFormat.format(res.getString("PolicyInformationUtil.Organization"),
                    organization.getString()));

            if ((noticeReference.getNoticeNumbers() != null) || (userNotice.getExplicitText() != null)) {
                sbUserNotice.append(", ");
            }
        }

        ASN1Integer[] noticeNumbers = noticeReference.getNoticeNumbers();

        StringBuffer sbNoticeNumbers = new StringBuffer();

        if (noticeNumbers != null) {
            for (int i = 0; i < noticeNumbers.length; i++) {
                ASN1Integer noticeNumber = noticeNumbers[i];

                sbNoticeNumbers.append(noticeNumber.getValue().intValue());

                if ((i + 1) < noticeNumbers.length) {
                    sbNoticeNumbers.append(" ");
                }
            }

            sbUserNotice.append(MessageFormat.format(res.getString("PolicyInformationUtil.NoticeNumbers"),
                    sbNoticeNumbers.toString()));

            if (userNotice.getExplicitText() != null) {
                sbUserNotice.append(", ");
            }
        }
    }

    DisplayText explicitText = userNotice.getExplicitText();

    if (explicitText != null) {
        sbUserNotice.append(MessageFormat.format(res.getString("PolicyInformationUtil.ExplicitText"),
                explicitText.getString()));
    }

    return sbUserNotice.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

    /*/* ww w .j  a v a  2  s .c o  m*/
     * 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();
}

From source file:net.sf.keystore_explorer.gui.crypto.policyinformation.DUserNoticeChooser.java

License:Open Source License

private void populate(UserNotice userNotice) {
    if (userNotice != null) {
        NoticeReference noticeReference = userNotice.getNoticeRef();

        if (noticeReference != null) {
            DisplayText organization = noticeReference.getOrganization();

            if (organization != null) {
                jtfOrganization.setText(organization.getString());
                jtfOrganization.setCaretPosition(0);
            }//from  w w  w  .  jav  a2  s  .co m

            populateNoticeNumbers(noticeReference);
        }

        DisplayText explicitText = userNotice.getExplicitText();

        if (explicitText != null) {
            jtfExplicitText.setText(explicitText.getString());
            jtfExplicitText.setCaretPosition(0);
        }
    }
}