Bu sayfanın seçili sürümü ile mevcut sürümü arasındaki farkları gösterir.
esya:cades:imza-atma [2013/07/22 09:59] Beytullah Yiğit [İmzasız Bir Verinin İmzalanması] |
esya:cades:imza-atma [2013/08/28 08:31] (mevcut) Beytullah Yiğit |
||
---|---|---|---|
Satır 1: | Satır 1: | ||
- | ====== İmza Atma İşlemleri ====== | + | ===== İmza Atma İşlemleri ===== |
İmzalama işlemi genel olarak iki aşamada gerçekleşmektedir. Öncelikle imzacının sertifikasının doğrulanması yapılmaktadır, sonra imza atma işlemi gerçekleşmektedir. | İmzalama işlemi genel olarak iki aşamada gerçekleşmektedir. Öncelikle imzacının sertifikasının doğrulanması yapılmaktadır, sonra imza atma işlemi gerçekleşmektedir. | ||
- | ===== İmzasız Bir Verinin İmzalanması ===== | + | ==== İmzasız Bir Verinin İmzalanması ==== |
- | Veriyi imzalama işleminden BaseSignedData sınıfı sorumludur. Bu sınıfa öncelikle addContent(…) fonksiyonu ile imzalanacak veri eklenmelidir. addContent(…) fonksiyonu yalnızca bir kere çağrılmalıdır. İmzalanacak veri addContent(…) ile eklendikten sonra değiştirilemez. addSigner(…) fonksiyonu ile veriye imza bilgileri eklenir. | + | Veriyi imzalama işleminden ''BaseSignedData'' sınıfı sorumludur. Bu sınıfa öncelikle ''addContent(…)'' fonksiyonu ile imzalanacak veri eklenmelidir. ''addContent(…)'' fonksiyonu yalnızca bir kere çağrılmalıdır. İmzalanacak veri ''addContent(…)'' ile eklendikten sonra değiştirilemez. ''addSigner(…)'' fonksiyonu ile veriye imza bilgileri eklenir. |
- | İmza eklenirken imzanın türü, imzacının sertifikası, imza işlemini gerçekleştirecek kripto nesnesi, varsa ekstra imza özellikleri ve imza üretiminde kullanılması gereken parametreler addSigner(…) fonksiyonuna parametre olarak geçilmelidir. İmza atan örnek kod bloğu: | + | İmza eklenirken imzanın türü, imzacının sertifikası, imza işlemini gerçekleştirecek kripto nesnesi, varsa ekstra imza özellikleri ve imza üretiminde kullanılması gereken parametreler ''addSigner(…)'' fonksiyonuna parametre olarak geçilmelidir. İmza atan örnek kod bloğu: |
<sxh java;title:Java> | <sxh java;title:Java> | ||
Satır 43: | Satır 43: | ||
AsnIO.dosyayaz(signedDocument,TestConstants.getDirectory() + "testdata/BES-1.p7s"); | AsnIO.dosyayaz(signedDocument,TestConstants.getDirectory() + "testdata/BES-1.p7s"); | ||
</sxh> | </sxh> | ||
+ | |||
<sxh csharp;title:C#> | <sxh csharp;title:C#> | ||
BaseSignedData bs = new BaseSignedData(); | BaseSignedData bs = new BaseSignedData(); | ||
Satır 85: | Satır 86: | ||
AsnIO.dosyayaz(signedDocument, di.FullName + @"\BES-1.p7s"); | AsnIO.dosyayaz(signedDocument, di.FullName + @"\BES-1.p7s"); | ||
</sxh> | </sxh> | ||
+ | |||
+ | ==== İmzalı Bir Veriye İmza Eklenmesi ==== | ||
+ | |||
+ | Bir veri birkaç kişi tarafından imzalanabilir. İmzalar iki şekilde atılabilir. | ||
+ | - Paralel İmza Ekleme | ||
+ | - Seri İmza Ekleme | ||
+ | |||
+ | === Paralel İmza === | ||
+ | |||
+ | Bu tür imzalarda bütün imzacıların imzaladıkları veri aynıdır. Bütün imzalar aynı seviyededir. Bir imzacının imzası dokümandan çıkartılırsa fark edilemez. | ||
+ | |||
+ | <sxh java;title:Java> | ||
+ | byte [] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE); | ||
+ | BaseSignedData bs = new BaseSignedData(signature); | ||
+ | |||
+ | //create parameters necessary for signature creation | ||
+ | HashMap<String, Object> params = new HashMap<String, Object>(); | ||
+ | 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(); | ||
+ | </sxh> | ||
+ | |||
+ | <sxh csharp;title:C#> | ||
+ | byte[] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE); | ||
+ | BaseSignedData bs = new BaseSignedData(signature); | ||
+ | |||
+ | //create parameters necessary for signature creation | ||
+ | Dictionary<String, Object> params_ = new Dictionary<String, Object>(); | ||
+ | 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(); | ||
+ | </sxh> | ||
+ | |||
+ | === Seri İmza === | ||
+ | |||
+ | Veriyi imzalayan imzanın imzalanmasıdır. Dolayısıyla bir imzacının imzası çıkartılırsa o imzacıdan sonra imza atan imzacıların da imzalarının çıkartılması gerekmektedir. | ||
+ | Aşağıdaki kod örneğinde ilk imzacıya seri imza ekleniyor. | ||
+ | |||
+ | <sxh java;title:Java> | ||
+ | byte [] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE); | ||
+ | BaseSignedData bs = new BaseSignedData(signature); | ||
+ | |||
+ | //create parameters necessary for signature creation | ||
+ | HashMap<String, Object> params = new HashMap<String, Object>(); | ||
+ | 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(); | ||
+ | </sxh> | ||
+ | |||
+ | <sxh csharp;title:C#> | ||
+ | byte[] signature = AsnIO.dosyadanOKU(SIGNATURE_FILE); | ||
+ | BaseSignedData bs = new BaseSignedData(signature); | ||
+ | |||
+ | //create parameters necessary for signature creation | ||
+ | Dictionary<String, Object> params_ = new Dictionary<String, Object>(); | ||
+ | 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(); | ||
+ | </sxh> | ||
+ | |||
+ | ==== Ayrık İmza ==== | ||
+ | |||
+ | Ayrık imzada imzalanacak veri ''BaseSignedData.addContent(…)'' fonksiyonunun ikinci parametresi false verilerek atanır. | ||
+ | Bütünleşik imza ile büyük boyutlu dosyalar imzalanamaz. İmzanın yapısı gereği imzalanacak verinin hepsi belleğe alınmaktadır. Bundan dolayı büyük boyutlu dosyaların imzalanması için ayrık imza kullanmak gerekmektedir. | ||
+ | |||
+ | <sxh java;title:Java> | ||
+ | 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<String, Object> params = new HashMap<String, Object>(); | ||
+ | |||
+ | 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(); | ||
+ | </sxh> | ||
+ | <sxh csharp;title:C#> | ||
+ | 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<String, Object> params_ = new Dictionary<String, Object>(); | ||
+ | |||
+ | 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(); | ||
+ | </sxh> | ||
+ | |||
+ | === Ayrık İmzanın Bütünleşik İmzaya Çevrilmesi === | ||
+ | |||
+ | İmzalanan içerik ''attachExternalContent'' fonksiyonuna verilerek, ayrık imza bütünleşik imzaya dönüştürülebilir. | ||
+ | |||
+ | <sxh java;title:Java> | ||
+ | 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); | ||
+ | </sxh> | ||
+ | |||
+ | <sxh csharp;title:C#> | ||
+ | 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); | ||
+ | </sxh> | ||
+ |