// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/exception/SmartCardException.java
// public class SmartCardException extends Exception {
// 
//     public SmartCardException(String s, Throwable t) {
//         super(s, t);
//     }
// }
// 
// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/support/ExceptionSupport.java
// public class ExceptionSupport {
// 
//     private static ResourceBundle bundle = null;
// 
//     public static String getValue(final String key) {
//         if (bundle == null) {
//             Locale locale = new Locale("tr", "TR");
//             bundle = ResourceBundle.getBundle("exception/SmartCardExceptions", locale);
//         }
//         return bundle.getString(key);
//     }
//     
// }


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.asn1.DERTaggedObject;
import tr.gov.turkiye.esign.exception.SmartCardException;
import tr.gov.turkiye.esign.support.ExceptionSupport;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tr.gov.turkiye.esign.cms;


/**
 * The class identifies the signed-data content.<br>
 * <br>
 * SignedData ::= DERSequence {<br>
 * version CMSVersion,<br>
 * digestAlgorithms DigestAlgorithmIdentifiers,<br>
 * encapContentInfo EncapsulatedContentInfo,<br>
 * certificates [0] IMPLICIT CertificateSet OPTIONAL,<br>
 * signerInfos SignerInfos }<br>
 * <br>
 * DigestAlgorithmIdentifiers ::= DERSet OF DigestAlgorithmIdentifier<br>
 * <br>
 * EncapsulatedContentInfo ::= DERSequence {<br>
        eContentType ContentType,<br>
        eContent [0] EXPLICIT DEROctetString OPTIONAL }<br>
 * <br>
      ContentType ::= ASN1ObjectIdentifier
 * <br>
 * CertificateSet ::= DERSet OF CertificateChoices<br>
 * <br>
 * CertificateChoices ::= CHOICE {<br>
 * certificate Certificate<br>
 * }<br>
 * <br>
 * <br>
 * SignerInfos ::= DERSet OF SignerInfo<br>
 *
 * @author sercan
 */
public class SignedData implements CMSObject {

    /**
     * the bytes of content to be signed.
     */
    private byte[] content; 
    
    /**
     * list of signer certificates
     */
    private List<Certificate> certs;
    
    /**
     * list of signer infos
     */
    private List<SignerInfo> signerInfos;
    
    /**
     * the hash algorithms used by the signers for digesting the content data is SHA256 or not
     */
    private boolean isSHA256;

    /**
     * Creates instance of SignerData
     * @param content the bytes of content to be signed.
     * @param isSHA256 the hash algorithms used by the signers for digesting the content data is SHA256 or not
     */
    public SignedData(byte[] content, boolean isSHA256) {
        signerInfos = new ArrayList<>();
        certs = new ArrayList<>();
        this.content = content;
        this.isSHA256 = isSHA256;
    }

    @Override
// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/exception/SmartCardException.java
// public class SmartCardException extends Exception {
// 
//     public SmartCardException(String s, Throwable t) {
//         super(s, t);
//     }
// }
// 
// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/support/ExceptionSupport.java
// public class ExceptionSupport {
// 
//     private static ResourceBundle bundle = null;
// 
//     public static String getValue(final String key) {
//         if (bundle == null) {
//             Locale locale = new Locale("tr", "TR");
//             bundle = ResourceBundle.getBundle("exception/SmartCardExceptions", locale);
//         }
//         return bundle.getString(key);
//     }
//     
// }


// Path: ESignCore/src/main/java/tr/gov/turkiye/esign/cms/SignedData.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.asn1.DERTaggedObject;
import tr.gov.turkiye.esign.exception.SmartCardException;
import tr.gov.turkiye.esign.support.ExceptionSupport;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tr.gov.turkiye.esign.cms;


/**
 * The class identifies the signed-data content.<br>
 * <br>
 * SignedData ::= DERSequence {<br>
 * version CMSVersion,<br>
 * digestAlgorithms DigestAlgorithmIdentifiers,<br>
 * encapContentInfo EncapsulatedContentInfo,<br>
 * certificates [0] IMPLICIT CertificateSet OPTIONAL,<br>
 * signerInfos SignerInfos }<br>
 * <br>
 * DigestAlgorithmIdentifiers ::= DERSet OF DigestAlgorithmIdentifier<br>
 * <br>
 * EncapsulatedContentInfo ::= DERSequence {<br>
        eContentType ContentType,<br>
        eContent [0] EXPLICIT DEROctetString OPTIONAL }<br>
 * <br>
      ContentType ::= ASN1ObjectIdentifier
 * <br>
 * CertificateSet ::= DERSet OF CertificateChoices<br>
 * <br>
 * CertificateChoices ::= CHOICE {<br>
 * certificate Certificate<br>
 * }<br>
 * <br>
 * <br>
 * SignerInfos ::= DERSet OF SignerInfo<br>
 *
 * @author sercan
 */
public class SignedData implements CMSObject {

    /**
     * the bytes of content to be signed.
     */
    private byte[] content; 
    
    /**
     * list of signer certificates
     */
    private List<Certificate> certs;
    
    /**
     * list of signer infos
     */
    private List<SignerInfo> signerInfos;
    
    /**
     * the hash algorithms used by the signers for digesting the content data is SHA256 or not
     */
    private boolean isSHA256;

    /**
     * Creates instance of SignerData
     * @param content the bytes of content to be signed.
     * @param isSHA256 the hash algorithms used by the signers for digesting the content data is SHA256 or not
     */
    public SignedData(byte[] content, boolean isSHA256) {
        signerInfos = new ArrayList<>();
        certs = new ArrayList<>();
        this.content = content;
        this.isSHA256 = isSHA256;
    }

    @Override
    public ASN1Object toASN1Object() throws SmartCardException {