Java Code Examples for sun.security.x509.AlgorithmId#encode()

The following examples show how to use sun.security.x509.AlgorithmId#encode() . 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: MacData.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 2
Source File: MacData.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 3
Source File: MacData.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 4
Source File: MacData.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 5
Source File: MacData.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 6
Source File: CspHelper.java    From julongchain with Apache License 2.0 5 votes vote down vote up
private static byte[] encodePrivateKeyPKCS8(byte[] privateKey, AlgorithmId algId) throws JulongChainException {
    DerOutputStream encodedPriKey = new DerOutputStream();
    DerOutputStream var = new DerOutputStream();
    try {
        var.putInteger(BigInteger.ZERO);
        algId.encode(var);
        var.putOctetString(privateKey);
        encodedPriKey.write((byte) ASN1_SEQUENCE, var);
        return encodedPriKey.toByteArray();
    } catch (IOException e) {
        throw new JulongChainException("An error occurred :" + e);
    }
}
 
Example 7
Source File: RsaSigningClient.java    From protect with MIT License 5 votes vote down vote up
static final X509Certificate createCertificateFromTbsAndSignature(X509CertInfo info, final byte[] signature)
		throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException,
		SignatureException {

	try (DerOutputStream out = new DerOutputStream(); DerOutputStream tmp = new DerOutputStream();) {

		// Append the certificate information
		info.encode(tmp);

		// Append the signature algorithm
		final AlgorithmId algId = AlgorithmId.get(CERTIFICATE_SIGNING_ALGORITHM);
		algId.encode(tmp);

		// Append the signature
		tmp.putBitString(signature);

		// Wrap the signed data in a SEQUENCE { data, algorithm, sig }
		out.write(DerValue.tag_Sequence, tmp);
		byte[] signedCert = out.toByteArray();

		// Create a certificate
		return new X509CertImpl(signedCert);

	} catch (IOException e) {
		throw new CertificateEncodingException(e.toString());
	}
}
 
Example 8
Source File: CertificateGeneration.java    From protect with MIT License 5 votes vote down vote up
/**
 * Creates a certificate from an X509Certificate info and a raw signature
 * 
 * @param toBeSignedCertificateInfo
 * @param certificateSigningAlgorithm
 * @param signature
 * @return
 * @throws CertificateException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws NoSuchProviderException
 * @throws SignatureException
 */
public static final X509Certificate createCertificateFromTbsAndSignature(
		final X509CertInfo toBeSignedCertificateInfo, final String certificateSigningAlgorithm,
		final byte[] signature) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
		NoSuchProviderException, SignatureException {

	try (DerOutputStream out = new DerOutputStream(); DerOutputStream tmp = new DerOutputStream();) {

		// Append the certificate information
		toBeSignedCertificateInfo.encode(tmp);

		// Append the signature algorithm
		final AlgorithmId algId = AlgorithmId.get(certificateSigningAlgorithm);
		algId.encode(tmp);

		// Append the signature
		tmp.putBitString(signature);

		// Wrap the signed data in a SEQUENCE { data, algorithm, sig }
		out.write(DerValue.tag_Sequence, tmp);
		byte[] signedCert = out.toByteArray();

		// Create a certificate
		return new X509CertImpl(signedCert);

	} catch (IOException e) {
		throw new CertificateEncodingException(e.toString());
	}
}
 
Example 9
Source File: MacData.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the ASN.1 encoding of this object.
 * @return the ASN.1 encoding.
 * @exception IOException if error occurs when constructing its
 * ASN.1 encoding.
 */
public byte[] getEncoded() throws NoSuchAlgorithmException, IOException
{
    if (this.encoded != null)
        return this.encoded.clone();

    DerOutputStream out = new DerOutputStream();
    DerOutputStream tmp = new DerOutputStream();

    DerOutputStream tmp2 = new DerOutputStream();
    // encode encryption algorithm
    AlgorithmId algid = AlgorithmId.get(digestAlgorithmName);
    algid.encode(tmp2);

    // encode digest data
    tmp2.putOctetString(digest);

    tmp.write(DerValue.tag_Sequence, tmp2);

    // encode salt
    tmp.putOctetString(macSalt);

    // encode iterations
    tmp.putInteger(iterations);

    // wrap everything into a SEQUENCE
    out.write(DerValue.tag_Sequence, tmp);
    this.encoded = out.toByteArray();

    return this.encoded.clone();
}
 
Example 10
Source File: PKCS12KeyStore.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;


    try {
        // create AlgorithmParameters
        AlgorithmParameters algParams = getPBEAlgorithmParameters(
                certProtectionAlgorithm, certPbeIterationCount);
        DerOutputStream bytes = new DerOutputStream();

        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance(certProtectionAlgorithm);
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        AlgorithmId algId = new AlgorithmId(
                mapPBEAlgorithmToOID(certProtectionAlgorithm),
                cipher.getParameters());
                // cipher.getParameters() now has IV
        algId.encode(bytes);
        byte[] encodedAlgId = bytes.toByteArray();

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                    ")");
        }

        // create EncryptedContentInfo
        DerOutputStream bytes2 = new DerOutputStream();
        bytes2.putOID(ContentInfo.DATA_OID);
        bytes2.write(encodedAlgId);

        // Wrap encrypted data in a context-specific tag.
        DerOutputStream tmpout2 = new DerOutputStream();
        tmpout2.putOctetString(encryptedData);
        bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                false, (byte) 0), tmpout2);

        // wrap EncryptedContentInfo in a Sequence
        DerOutputStream out = new DerOutputStream();
        out.write(DerValue.tag_Sequence, bytes2);
        return out.toByteArray();
    } catch (IOException ioe) {
        throw ioe;
    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }
}
 
Example 11
Source File: PKCS12KeyStore.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 12
Source File: PKCS12KeyStore.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 13
Source File: PKCS12KeyStore.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 14
Source File: PKCS10.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the signed certificate request.  This will later be
 * retrieved in either string or binary format.
 *
 * @param subject identifies the signer (by X.500 name).
 * @param signature private key and signing algorithm to use.
 * @exception IOException on errors.
 * @exception CertificateException on certificate handling errors.
 * @exception SignatureException on signature handling errors.
 */
public void encodeAndSign(X500Name subject, Signature signature)
throws CertificateException, IOException, SignatureException {
    DerOutputStream out, scratch;
    byte[]          certificateRequestInfo;
    byte[]          sig;

    if (encoded != null)
        throw new SignatureException("request is already signed");

    this.subject = subject;

    /*
     * Encode cert request info, wrap in a sequence for signing
     */
    scratch = new DerOutputStream();
    scratch.putInteger(BigInteger.ZERO);            // PKCS #10 v1.0
    subject.encode(scratch);                        // X.500 name
    scratch.write(subjectPublicKeyInfo.getEncoded()); // public key
    attributeSet.encode(scratch);

    out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, scratch);      // wrap it!
    certificateRequestInfo = out.toByteArray();
    scratch = out;

    /*
     * Sign it ...
     */
    signature.update(certificateRequestInfo, 0,
            certificateRequestInfo.length);
    sig = signature.sign();
    sigAlg = signature.getAlgorithm();

    /*
     * Build guts of SIGNED macro
     */
    AlgorithmId algId = null;
    try {
        algId = AlgorithmId.get(signature.getAlgorithm());
    } catch (NoSuchAlgorithmException nsae) {
        throw new SignatureException(nsae);
    }
    algId.encode(scratch);     // sig algorithm
    scratch.putBitString(sig);                      // sig

    /*
     * Wrap those guts in a sequence
     */
    out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, scratch);
    encoded = out.toByteArray();
}
 
Example 15
Source File: PKCS12KeyStore.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 16
Source File: PKCS10.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the signed certificate request.  This will later be
 * retrieved in either string or binary format.
 *
 * @param subject identifies the signer (by X.500 name).
 * @param signature private key and signing algorithm to use.
 * @exception IOException on errors.
 * @exception CertificateException on certificate handling errors.
 * @exception SignatureException on signature handling errors.
 */
public void encodeAndSign(X500Name subject, Signature signature)
throws CertificateException, IOException, SignatureException {
    DerOutputStream out, scratch;
    byte[]          certificateRequestInfo;
    byte[]          sig;

    if (encoded != null)
        throw new SignatureException("request is already signed");

    this.subject = subject;

    /*
     * Encode cert request info, wrap in a sequence for signing
     */
    scratch = new DerOutputStream();
    scratch.putInteger(BigInteger.ZERO);            // PKCS #10 v1.0
    subject.encode(scratch);                        // X.500 name
    scratch.write(subjectPublicKeyInfo.getEncoded()); // public key
    attributeSet.encode(scratch);

    out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, scratch);      // wrap it!
    certificateRequestInfo = out.toByteArray();
    scratch = out;

    /*
     * Sign it ...
     */
    signature.update(certificateRequestInfo, 0,
            certificateRequestInfo.length);
    sig = signature.sign();

    /*
     * Build guts of SIGNED macro
     */
    AlgorithmId algId = null;
    try {
        algId = AlgorithmId.get(signature.getAlgorithm());
    } catch (NoSuchAlgorithmException nsae) {
        throw new SignatureException(nsae);
    }
    algId.encode(scratch);     // sig algorithm
    scratch.putBitString(sig);                      // sig

    /*
     * Wrap those guts in a sequence
     */
    out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, scratch);
    encoded = out.toByteArray();
}
 
Example 17
Source File: PKCS12KeyStore.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 18
Source File: PKCS10.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the signed certificate request.  This will later be
 * retrieved in either string or binary format.
 *
 * @param subject identifies the signer (by X.500 name).
 * @param signature private key and signing algorithm to use.
 * @exception IOException on errors.
 * @exception CertificateException on certificate handling errors.
 * @exception SignatureException on signature handling errors.
 */
public void encodeAndSign(X500Name subject, Signature signature)
throws CertificateException, IOException, SignatureException {
    DerOutputStream out, scratch;
    byte[]          certificateRequestInfo;
    byte[]          sig;

    if (encoded != null)
        throw new SignatureException("request is already signed");

    this.subject = subject;

    /*
     * Encode cert request info, wrap in a sequence for signing
     */
    scratch = new DerOutputStream();
    scratch.putInteger(BigInteger.ZERO);            // PKCS #10 v1.0
    subject.encode(scratch);                        // X.500 name
    scratch.write(subjectPublicKeyInfo.getEncoded()); // public key
    attributeSet.encode(scratch);

    out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, scratch);      // wrap it!
    certificateRequestInfo = out.toByteArray();
    scratch = out;

    /*
     * Sign it ...
     */
    signature.update(certificateRequestInfo, 0,
            certificateRequestInfo.length);
    sig = signature.sign();
    sigAlg = signature.getAlgorithm();

    /*
     * Build guts of SIGNED macro
     */
    AlgorithmId algId = null;
    try {
        algId = AlgorithmId.get(signature.getAlgorithm());
    } catch (NoSuchAlgorithmException nsae) {
        throw new SignatureException(nsae);
    }
    algId.encode(scratch);     // sig algorithm
    scratch.putBitString(sig);                      // sig

    /*
     * Wrap those guts in a sequence
     */
    out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, scratch);
    encoded = out.toByteArray();
}
 
Example 19
Source File: PKCS12KeyStore.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}
 
Example 20
Source File: PKCS12KeyStore.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private byte[] encryptContent(byte[] data, char[] password)
    throws IOException {

    byte[] encryptedData = null;

    // create AlgorithmParameters
    AlgorithmParameters algParams =
            getPBEAlgorithmParameters("PBEWithSHA1AndRC2_40");
    DerOutputStream bytes = new DerOutputStream();
    AlgorithmId algId =
            new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
    algId.encode(bytes);
    byte[] encodedAlgId = bytes.toByteArray();

    try {
        // Use JCE
        SecretKey skey = getPBEKey(password);
        Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        encryptedData = cipher.doFinal(data);

        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
                ")");
        }

    } catch (Exception e) {
        throw new IOException("Failed to encrypt" +
                " safe contents entry: " + e, e);
    }

    // create EncryptedContentInfo
    DerOutputStream bytes2 = new DerOutputStream();
    bytes2.putOID(ContentInfo.DATA_OID);
    bytes2.write(encodedAlgId);

    // Wrap encrypted data in a context-specific tag.
    DerOutputStream tmpout2 = new DerOutputStream();
    tmpout2.putOctetString(encryptedData);
    bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
                                    false, (byte)0), tmpout2);

    // wrap EncryptedContentInfo in a Sequence
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, bytes2);
    return out.toByteArray();
}