===== Signature Creation =====
Signature creation consists of two steps which are the validation of the signing certificate and creation of the signature.
==== Signing an Unsigned Document ====
''BaseSignedData'' is responsible for data signing. Firstly, by using the method ''addContent(...)'' the data to be signed must be defined. This method must be called only once. The data to be signed can not be modified once it is assigned by ''addContent(...)''. The signer information is defined by using ''addSigner(...)''. This function takes the type of the signature, the certificate of the signer, the properties of the crypto device that will perform the signature creation, and other signature properties as parameters. The sample code for signature creation is as follows:
BaseSignedData bs = new BaseSignedData();
ISignable content = new SignableByteArray("test".getBytes());
bs.addContent(content);
HashMap params = new HashMap();
//if the user does not want certificate validation at generating signature,he can add
//P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false
//params.put(EParameters.P_VALIDATE_CERTIFICATE_BEFORE_SIGNING, false);
//necessary for certificate validation.By default,certificate validation is done
params.put(EParameters.P_CERT_VALIDATION_POLICY, TestConstants.getPolicy());
//By default, QC statement is checked,and signature wont be created if it is not a
//qualified certificate.
boolean checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
//add signer
//Since the specified attributes are mandatory for bes,null is given as parameter
//for optional attributes
bs.addSigner(ESignatureType.TYPE_BES, cert , signer, null, params);
SmartCardManager.getInstance().logout();
byte [] signedDocument = bs.getEncoded();
//write the contentinfo to file
AsnIO.dosyayaz(signedDocument,TestConstants.getDirectory() + "testdata/BES-1.p7s");
BaseSignedData bs = new BaseSignedData();
ISignable content = new SignableByteArray(ASCIIEncoding.ASCII.GetBytes("test"));
bs.addContent(content);
Dictionary params_ = new Dictionary();
//if the user does not want certificate validation at generating signature,he can add
//P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false
params_[EParameters.P_VALIDATE_CERTIFICATE_BEFORE_SIGNING] = false;
//necessary for certificate validation.By default,certificate validation is done
params_[EParameters.P_CERT_VALIDATION_POLICY] = TestConstants.getPolicy();
//By default, QC statement is checked,and signature wont be created if it is not a
//qualified certificate.
bool checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
//add signer
//Since the specified attributes are mandatory for bes,null is given as parameter
//for optional attributes
try
{
bs.addSigner(ESignatureType.TYPE_BES, cert, signer, null, params_);
}
catch (CertificateValidationException cve)
{ Console.WriteLine(cve.getCertStatusInfo().getDetailedMessage());
}
SmartCardManager.getInstance().logout();
byte[] signedDocument = bs.getEncoded();
//write the contentinfo to file
DirectoryInfo di = Directory.CreateDirectory(TestConstants.getDirectory()+@"\testVerileri");
AsnIO.dosyayaz(signedDocument, di.FullName + @"\BES-1.p7s");
==== Signing an Already Signed Document ====
A document can be signed by more than one person. Multiple signatures can be in two forms:
- Parallel Signature
- Counter Signature
=== Parallel Signature ===
In this form, all signers sign the same data and all signatures are in the same level in the sign tree. All signatures are independent meaning that extraction of one signature does not affects the validity of the others.
byte [] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE);
BaseSignedData bs = new BaseSignedData(signature);
//create parameters necessary for signature creation
HashMap params = new HashMap();
ValidationPolicy policy = PolicyReader.readValidationPolicy(new FileInputStream(POLICY_FILE));
params.put(EParameters.P_CERT_VALIDATION_POLICY, policy);
/*necessary for certificate validation.By default,certificate validation is done.But if the user does not want certificate validation,he can add P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false*/
//By default, QC statement is checked,and signature wont be created if it is not a
//qualified certificate.
boolean checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
//add signer. Since the specified attributes are mandatory for bes,null is given as parameter for
//optional attributes
bs.addSigner(ESignatureType.TYPE_BES, cert , signer, null, params);
//write the contentinfo to file
AsnIO.dosyayaz(bs.getEncoded(),NEW_SIGNATURE_ADDED_FILE);
sc.logout(session);
SmartCardManager.getInstance().logout();
byte[] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE);
BaseSignedData bs = new BaseSignedData(signature);
//create parameters necessary for signature creation
Dictionary params_ = new Dictionary();
ValidationPolicy policy = PolicyReader.readValidationPolicy(new FileStream(POLICY_FILE,
FileMode.Open, FileAccess.Read));
params_[EParameters.P_CERT_VALIDATION_POLICY] = policy;
/*necessary for certificate validation.By default,certificate validation is done.But if the user does not want certificate validation,he can add P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false*/
bool checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
//add signer. Since the specified attributes are mandatory for bes,null is given as parameter for
//optional attributes
bs.addSigner(ESignatureType.TYPE_BES, cert, signer, null, params_);
//write the contentinfo to file
AsnIO.dosyayaz(bs.getEncoded(), NEW_SIGNATURE_ADDED_FILE);
SmartCardManager.getInstance().logout();
=== Counter Signature ===
It is signing another signature. Extracting a signature from a signed documents affects all of its counter signatures.
The code below demonstrates adding a counter signature to the first signature in a document.
byte [] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE);
BaseSignedData bs = new BaseSignedData(signature);
//create parameters necessary for signature creation
HashMap params = new HashMap();
ValidationPolicy policy = PolicyReader.readValidationPolicy(new FileInputStream(POLICY_FILE));
params.put(EParameters.P_CERT_VALIDATION_POLICY, policy);
/*necessary for certificate validation.By default,certificate validation is done.But if the user does not want certificate validation,he can add P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false*/
//By default, QC statement is checked,and signature wont be created if it is not a
//qualified certificate.
boolean checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
Signer firstSigner = bs.getSignerList().get(0);
firstSigner.addCounterSigner(ESignatureType.TYPE_BES, cert , signer, null, params);
//write the contentinfo to file
AsnIO.dosyayaz(bs.getEncoded(),NEW_SIGNATURE_ADDED_FILE);
SmartCardManager.getInstance().logout();
byte[] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE);
BaseSignedData bs = new BaseSignedData(signature);
//create parameters necessary for signature creation
Dictionary params_ = new Dictionary();
ValidationPolicy policy = PolicyReader.readValidationPolicy(new FileStream(POLICY_FILE,
FileMode.Open,
FileAccess.Read));
params_[EParameters.P_CERT_VALIDATION_POLICY] = policy;
/*necessary for certificate validation.By default,certificate validation is done.But if the user does not want certificate validation,he can add P_VALIDATE_CERTIFICATE_BEFORE_SIGNING parameter with its value set to false*/
bool checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
Signer firstSigner = bs.getSignerList()[0];
firstSigner.addCounterSigner(ESignatureType.TYPE_BES, cert, signer, null, params_);
//write the contentinfo to file
AsnIO.dosyayaz(bs.getEncoded(), NEW_SIGNATURE_ADDED_FILE);
SmartCardManager.getInstance().logout();
==== Detached Signature ====
If the second parameter of ''BaseSignedData.addContent(…)'' is set to ''false'' then the created signature will be detached. Large files can not be signed as attached. This is because the atached signature creation operation requires the whole content to be signed takes place in the memory.
BaseSignedData bs = new BaseSignedData();
File file = new File(MOVIE_FILE);
ISignable signable = new SignableFile(file,2048);
bs.addContent(signable,false);
//create parameters necessary for signature creation
HashMap params = new HashMap();
ValidationPolicy policy = PolicyReader.readValidationPolicy(new FileInputStream(POLICY_FILE));
params.put(EParameters.P_CERT_VALIDATION_POLICY, policy);
//By default, QC statement is checked,and signature wont be created if it is not a
//qualified certificate.
boolean checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
bs.addSigner(ESignatureType.TYPE_BES, cert, signer, null, params);
AsnIO.dosyayaz(bs.getEncoded(), SIGNATURE_FILE);
SmartCardManager.getInstance().logout();
BaseSignedData bs = new BaseSignedData();
FileInfo file = new FileInfo(MOVIE_FILE);
ISignable signable = new SignableFile(file, 2048);
bs.addContent(signable, false);
//create parameters necessary for signature creation
Dictionary params_ = new Dictionary();
ValidationPolicy policy = PolicyReader.readValidationPolicy(new FileStream(POLICY_FILE,
FileMode.Open,
FileAccess.Read));
params_[EParameters.P_CERT_VALIDATION_POLICY] = policy;
bool checkQCStatement = TestConstants.getCheckQCStatement();
//Get qualified or non-qualified certificate.
ECertificate cert = SmartCardManager.getInstance().getSignatureCertificate(checkQCStatement, !checkQCStatement);
BaseSigner signer = SmartCardManager.getInstance().getSigner(TestConstants.getPIN(), cert);
bs.addSigner(ESignatureType.TYPE_BES, cert, signer, null, params_);
AsnIO.dosyayaz(bs.getEncoded(), SIGNATURE_FILE);
SmartCardManager.getInstance().logout();
=== Converting a Detached Signature To an Attached Signature ===
Detached signature can be converted into attached signature by using the function ''attachExternalContent'' and
passing the signed content as a parameter.
byte[] input = AsnIO.dosyadanOKU(AYRIK_IMZA);
BaseSignedData bs = new BaseSignedData(input);
File file = new File(IMZALANAN_ICERIK);
ISignable signable = new SignableFile(file,2048);
bs.attachExternalContent(signable);
byte[] input = AsnIO.dosyadanOKU(AYRIK_IMZA);
BaseSignedData bs = new BaseSignedData(input);
FileInfo file = new FileInfo(IMZALANAN_ICERIK);
ISignable signable = new SignableFile(file, 2048);
bs. attachExternalContent(signable);