<commit_before>/**
* OpenCPS PKI is the open source PKI Integration software
* Copyright (C) 2016-present OpenCPS community

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.opencps.pki;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.security.BouncyCastleDigest;
import com.itextpdf.text.pdf.security.CertificateUtil;
import com.itextpdf.text.pdf.security.DigestAlgorithms;
import com.itextpdf.text.pdf.security.ExternalSignatureContainer;
import com.itextpdf.text.pdf.security.MakeSignature.CryptoStandard;
import com.itextpdf.text.pdf.security.PdfPKCS7;
import com.itextpdf.text.pdf.security.TSAClient;
import com.itextpdf.text.pdf.security.TSAClientBouncyCastle;

/**
 * Produces a signed data from client. Useful for deferred signing
 * 
 * @author <NAME> <<EMAIL>>
 */
public class ClientSignatureContainer implements ExternalSignatureContainer {

    private byte[] signedData;
    
    private PdfSigner signer;

    /**
     * Constructor
     */
    public ClientSignatureContainer(PdfSigner signer, byte[] data) {
    	this.signer = signer;
        signedData = data;
    }

    @Override
    public void modifySigningDictionary(PdfDictionary pd) {
    }

    @Override
    public byte[] sign(InputStream is) throws GeneralSecurityException {
    	X509Certificate cert = signer.getCertificate();
		BouncyCastleDigest digest = new BouncyCastleDigest();
		PdfPKCS7 sgn = new PdfPKCS7(null, new Certificate[] { cert }, signer.getHashAlgorithm().toString(), null, digest, false);
		sgn.setExternalDigest(signedData, null, cert.getPublicKey().getAlgorithm());

		byte[] hash = null;
		try {
			hash = DigestAlgorithms.digest(is, digest.getMessageDigest(signer.getHashAlgorithm().toString()));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		TSAClient tsaClient = null;
        String tsaUrl = CertificateUtil.getTSAURL(cert);
        if (tsaUrl != null) {
            tsaClient = new TSAClientBouncyCastle(tsaUrl);
        }

        return sgn.getEncodedPKCS7(hash, tsaClient, null, null, CryptoStandard.CMS);
    }

}<commit_msg>Convert tab to space<commit_after>/**
* OpenCPS PKI is the open source PKI Integration software
* Copyright (C) 2016-present OpenCPS community

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.opencps.pki;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;

import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.security.BouncyCastleDigest;
import com.itextpdf.text.pdf.security.CertificateUtil;
import com.itextpdf.text.pdf.security.DigestAlgorithms;
import com.itextpdf.text.pdf.security.ExternalSignatureContainer;
import com.itextpdf.text.pdf.security.MakeSignature.CryptoStandard;
import com.itextpdf.text.pdf.security.PdfPKCS7;
import com.itextpdf.text.pdf.security.TSAClient;
import com.itextpdf.text.pdf.security.TSAClientBouncyCastle;

/**
 * Produces a signed data from client. Useful for deferred signing
 * 
 * @author <NAME> <<EMAIL>>
 */
public class ClientSignatureContainer implements ExternalSignatureContainer {

    private byte[] signedData;
    
    private PdfSigner signer;

    /**
     * Constructor
     */
    public ClientSignatureContainer(PdfSigner signer, byte[] data) {
        this.signer = signer;
        signedData = data;
    }

    @Override
    public void modifySigningDictionary(PdfDictionary pd) {
    }

    @Override
    public byte[] sign(InputStream is) throws GeneralSecurityException {
        X509Certificate cert = signer.getCertificate();
        BouncyCastleDigest digest = new BouncyCastleDigest();
        PdfPKCS7 sgn = new PdfPKCS7(null, new Certificate[] { cert }, signer.getHashAlgorithm().toString(), null, digest, false);
        sgn.setExternalDigest(signedData, null, cert.getPublicKey().getAlgorithm());

        byte[] hash = null;
        try {
            hash = DigestAlgorithms.digest(is, digest.getMessageDigest(signer.getHashAlgorithm().toString()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        TSAClient tsaClient = null;
        String tsaUrl = CertificateUtil.getTSAURL(cert);
        if (tsaUrl != null) {
            tsaClient = new TSAClientBouncyCastle(tsaUrl);
        }

        return sgn.getEncodedPKCS7(hash, tsaClient, null, null, CryptoStandard.CMS);
    }

}
{'fraction_non_alphanumeric': 0.09045571797076526, 'fraction_numerical': 0.005159071367153913, 'mean_word_length': 4.0137931034482754, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7059662', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1674, 'n_words': 487}