Java Code Examples for sun.security.x509.X500Name#asX500Name()

The following examples show how to use sun.security.x509.X500Name#asX500Name() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PKCS7.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(tsa, tSAPolicyID, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 2
Source File: ReverseBuilder.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(X509Certificate cert1, X509Certificate cert2) {

    /*
     * if either cert certifies the target, always
     * put at head of list.
     */
    X500Principal targetSubject = buildParams.targetSubject();
    if (cert1.getSubjectX500Principal().equals(targetSubject)) {
        return -1;
    }
    if (cert2.getSubjectX500Principal().equals(targetSubject)) {
        return 1;
    }

    int targetDist1;
    int targetDist2;
    try {
        X500Name targetSubjectName = X500Name.asX500Name(targetSubject);
        targetDist1 = Builder.targetDistance(
            null, cert1, targetSubjectName);
        targetDist2 = Builder.targetDistance(
            null, cert2, targetSubjectName);
    } catch (IOException e) {
        if (debug != null) {
            debug.println("IOException in call to Builder.targetDistance");
            e.printStackTrace();
        }
        throw new ClassCastException
            ("Invalid target subject distinguished name");
    }

    if (targetDist1 == targetDist2)
        return 0;

    if (targetDist1 == -1)
        return 1;

    if (targetDist1 < targetDist2)
        return -1;

    return 1;
}
 
Example 3
Source File: ReverseBuilder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int compare(X509Certificate cert1, X509Certificate cert2) {

    /*
     * if either cert certifies the target, always
     * put at head of list.
     */
    X500Principal targetSubject = buildParams.targetSubject();
    if (cert1.getSubjectX500Principal().equals(targetSubject)) {
        return -1;
    }
    if (cert2.getSubjectX500Principal().equals(targetSubject)) {
        return 1;
    }

    int targetDist1;
    int targetDist2;
    try {
        X500Name targetSubjectName = X500Name.asX500Name(targetSubject);
        targetDist1 = Builder.targetDistance(
            null, cert1, targetSubjectName);
        targetDist2 = Builder.targetDistance(
            null, cert2, targetSubjectName);
    } catch (IOException e) {
        if (debug != null) {
            debug.println("IOException in call to Builder.targetDistance");
            e.printStackTrace();
        }
        throw new ClassCastException
            ("Invalid target subject distinguished name");
    }

    if (targetDist1 == targetDist2)
        return 0;

    if (targetDist1 == -1)
        return 1;

    if (targetDist1 < targetDist2)
        return -1;

    return 1;
}
 
Example 4
Source File: PKCS7.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(tsa, tSAPolicyID, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 5
Source File: ReverseBuilder.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int compare(X509Certificate cert1, X509Certificate cert2) {

    /*
     * if either cert certifies the target, always
     * put at head of list.
     */
    X500Principal targetSubject = buildParams.targetSubject();
    if (cert1.getSubjectX500Principal().equals(targetSubject)) {
        return -1;
    }
    if (cert2.getSubjectX500Principal().equals(targetSubject)) {
        return 1;
    }

    int targetDist1;
    int targetDist2;
    try {
        X500Name targetSubjectName = X500Name.asX500Name(targetSubject);
        targetDist1 = Builder.targetDistance(
            null, cert1, targetSubjectName);
        targetDist2 = Builder.targetDistance(
            null, cert2, targetSubjectName);
    } catch (IOException e) {
        if (debug != null) {
            debug.println("IOException in call to Builder.targetDistance");
            e.printStackTrace();
        }
        throw new ClassCastException
            ("Invalid target subject distinguished name");
    }

    if (targetDist1 == targetDist2)
        return 0;

    if (targetDist1 == -1)
        return 1;

    if (targetDist1 < targetDist2)
        return -1;

    return 1;
}
 
Example 6
Source File: PKCS7.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(tsa, tSAPolicyID, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 7
Source File: PKCS7.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 8
Source File: ReverseBuilder.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int compare(X509Certificate cert1, X509Certificate cert2) {

    /*
     * if either cert certifies the target, always
     * put at head of list.
     */
    X500Principal targetSubject = buildParams.targetSubject();
    if (cert1.getSubjectX500Principal().equals(targetSubject)) {
        return -1;
    }
    if (cert2.getSubjectX500Principal().equals(targetSubject)) {
        return 1;
    }

    int targetDist1;
    int targetDist2;
    try {
        X500Name targetSubjectName = X500Name.asX500Name(targetSubject);
        targetDist1 = Builder.targetDistance(
            null, cert1, targetSubjectName);
        targetDist2 = Builder.targetDistance(
            null, cert2, targetSubjectName);
    } catch (IOException e) {
        if (debug != null) {
            debug.println("IOException in call to Builder.targetDistance");
            e.printStackTrace();
        }
        throw new ClassCastException
            ("Invalid target subject distinguished name");
    }

    if (targetDist1 == targetDist2)
        return 0;

    if (targetDist1 == -1)
        return 1;

    if (targetDist1 < targetDist2)
        return -1;

    return 1;
}
 
Example 9
Source File: PKCS7.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(tsa, tSAPolicyID, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 10
Source File: ReverseBuilder.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int compare(X509Certificate cert1, X509Certificate cert2) {

    /*
     * if either cert certifies the target, always
     * put at head of list.
     */
    X500Principal targetSubject = buildParams.targetSubject();
    if (cert1.getSubjectX500Principal().equals(targetSubject)) {
        return -1;
    }
    if (cert2.getSubjectX500Principal().equals(targetSubject)) {
        return 1;
    }

    int targetDist1;
    int targetDist2;
    try {
        X500Name targetSubjectName = X500Name.asX500Name(targetSubject);
        targetDist1 = Builder.targetDistance(
            null, cert1, targetSubjectName);
        targetDist2 = Builder.targetDistance(
            null, cert2, targetSubjectName);
    } catch (IOException e) {
        if (debug != null) {
            debug.println("IOException in call to Builder.targetDistance");
            e.printStackTrace();
        }
        throw new ClassCastException
            ("Invalid target subject distinguished name");
    }

    if (targetDist1 == targetDist2)
        return 0;

    if (targetDist1 == -1)
        return 1;

    if (targetDist1 < targetDist2)
        return -1;

    return 1;
}
 
Example 11
Source File: PKCS7.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 12
Source File: PKCS7.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 13
Source File: PKCS7.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 14
Source File: PKCS7.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 15
Source File: PKCS7.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 16
Source File: PKCS7.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 17
Source File: PKCS7.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 18
Source File: ReverseBuilder.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int compare(X509Certificate cert1, X509Certificate cert2) {

    /*
     * if either cert certifies the target, always
     * put at head of list.
     */
    X500Principal targetSubject = buildParams.targetSubject();
    if (cert1.getSubjectX500Principal().equals(targetSubject)) {
        return -1;
    }
    if (cert2.getSubjectX500Principal().equals(targetSubject)) {
        return 1;
    }

    int targetDist1;
    int targetDist2;
    try {
        X500Name targetSubjectName = X500Name.asX500Name(targetSubject);
        targetDist1 = Builder.targetDistance(
            null, cert1, targetSubjectName);
        targetDist2 = Builder.targetDistance(
            null, cert2, targetSubjectName);
    } catch (IOException e) {
        if (debug != null) {
            debug.println("IOException in call to Builder.targetDistance");
            e.printStackTrace();
        }
        throw new ClassCastException
            ("Invalid target subject distinguished name");
    }

    if (targetDist1 == targetDist2)
        return 0;

    if (targetDist1 == -1)
        return 1;

    if (targetDist1 < targetDist2)
        return -1;

    return 1;
}
 
Example 19
Source File: PKCS7.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(tsa, tSAPolicyID, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}
 
Example 20
Source File: PKCS7.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Assembles a PKCS #7 signed data message that optionally includes a
 * signature timestamp.
 *
 * @param signature the signature bytes
 * @param signerChain the signer's X.509 certificate chain
 * @param content the content that is signed; specify null to not include
 *        it in the PKCS7 data
 * @param signatureAlgorithm the name of the signature algorithm
 * @param tsaURI the URI of the Timestamping Authority; or null if no
 *         timestamp is requested
 * @param tSAPolicyID the TSAPolicyID of the Timestamping Authority as a
 *         numerical object identifier; or null if we leave the TSA server
 *         to choose one. This argument is only used when tsaURI is provided
 * @return the bytes of the encoded PKCS #7 signed data message
 * @throws NoSuchAlgorithmException The exception is thrown if the signature
 *         algorithm is unrecognised.
 * @throws CertificateException The exception is thrown if an error occurs
 *         while processing the signer's certificate or the TSA's
 *         certificate.
 * @throws IOException The exception is thrown if an error occurs while
 *         generating the signature timestamp or while generating the signed
 *         data message.
 */
public static byte[] generateSignedData(byte[] signature,
                                        X509Certificate[] signerChain,
                                        byte[] content,
                                        String signatureAlgorithm,
                                        URI tsaURI,
                                        String tSAPolicyID,
                                        String tSADigestAlg)
    throws CertificateException, IOException, NoSuchAlgorithmException
{

    // Generate the timestamp token
    PKCS9Attributes unauthAttrs = null;
    if (tsaURI != null) {
        // Timestamp the signature
        HttpTimestamper tsa = new HttpTimestamper(tsaURI);
        byte[] tsToken = generateTimestampToken(
                tsa, tSAPolicyID, tSADigestAlg, signature);

        // Insert the timestamp token into the PKCS #7 signer info element
        // (as an unsigned attribute)
        unauthAttrs =
            new PKCS9Attributes(new PKCS9Attribute[]{
                new PKCS9Attribute(
                    PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_STR,
                    tsToken)});
    }

    // Create the SignerInfo
    X500Name issuerName =
        X500Name.asX500Name(signerChain[0].getIssuerX500Principal());
    BigInteger serialNumber = signerChain[0].getSerialNumber();
    String encAlg = AlgorithmId.getEncAlgFromSigAlg(signatureAlgorithm);
    String digAlg = AlgorithmId.getDigAlgFromSigAlg(signatureAlgorithm);
    SignerInfo signerInfo = new SignerInfo(issuerName, serialNumber,
                                           AlgorithmId.get(digAlg), null,
                                           AlgorithmId.get(encAlg),
                                           signature, unauthAttrs);

    // Create the PKCS #7 signed data message
    SignerInfo[] signerInfos = {signerInfo};
    AlgorithmId[] algorithms = {signerInfo.getDigestAlgorithmId()};
    // Include or exclude content
    ContentInfo contentInfo = (content == null)
        ? new ContentInfo(ContentInfo.DATA_OID, null)
        : new ContentInfo(content);
    PKCS7 pkcs7 = new PKCS7(algorithms, contentInfo,
                            signerChain, signerInfos);
    ByteArrayOutputStream p7out = new ByteArrayOutputStream();
    pkcs7.encodeSignedData(p7out);

    return p7out.toByteArray();
}