Bu sayfanın seçili sürümü ile mevcut sürümü arasındaki farkları gösterir.
esya:xades:kod-coklu-imza [2013/07/19 11:10] Ahmet Yetgin [Paralel İmza] |
esya:xades:kod-coklu-imza [2013/08/05 11:29] (mevcut) Süleyman Uslu |
||
---|---|---|---|
Satır 1: | Satır 1: | ||
====== XAdES Çoklu İmza Atma ====== | ====== XAdES Çoklu İmza Atma ====== | ||
- | Gerçek hayatta olduğu gibi, elektronik dökümanlarda da bir dökümanda birden fazla imzanın yer alması mümkündür. Bu bölümde yer alan örnekleri ''docs/samples'' altında ''xmlsig.samples.MultipleSignatures'' class'ında inceleyebilirsiniz. | + | Gerçek hayatta olduğu gibi, elektronik dökümanlarda da bir dökümanda birden fazla imzanın yer alması mümkündür. Bu bölümde yer alan örnekleri ''tr.gov.tubitak.uekae.esya.api.xades.example.multiple'' paketinin altında inceleyebilirsiniz. |
''esya.api.xmlsignature.SignedDocument.java'' sınıfı çoklu imzaları kolayca yönetmek için yazılmıştır. Bu sınıfı incelemenizde yarar var. Basitçe bu sınıfı kullandığımızda XML formatında bir imza oluşur. Bu format üzerinde görüldüğü gibi aynı döküman üzerinde birden fazla veri ve imza yer alabilir. | ''esya.api.xmlsignature.SignedDocument.java'' sınıfı çoklu imzaları kolayca yönetmek için yazılmıştır. Bu sınıfı incelemenizde yarar var. Basitçe bu sınıfı kullandığımızda XML formatında bir imza oluşur. Bu format üzerinde görüldüğü gibi aynı döküman üzerinde birden fazla veri ve imza yer alabilir. | ||
Satır 7: | Satır 7: | ||
<sxh xml> | <sxh xml> | ||
<?xml version="1.0" encoding="UTF-8" ?> | <?xml version="1.0" encoding="UTF-8" ?> | ||
- | <ma3:envelope xmlns:ma3="http://uekae.tubitak.gov.tr/xml/signature#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> | + | <ma3:envelope xmlns:ma3="http://uekae.tubitak.gov.tr/xml/signature#" |
+ | xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> | ||
<ma3:data> | <ma3:data> | ||
<ma3:data-item>...</ma3:data-item> | <ma3:data-item>...</ma3:data-item> | ||
Satır 23: | Satır 24: | ||
===== Paralel İmza ===== | ===== Paralel İmza ===== | ||
- | Aynı döküman içindeki birbirinden bağımsız imzalardır. Aşağıdaki örnekte ''SignedDocument'' classını kullanarak önce imzalanacak veri dökümana eklenmekte, daha sonra bu veriyi imzalayan birbirinden bağımsız iki imza yaratılmaktadır. | + | Aynı döküman içindeki birbirinden bağımsız imzalardır. Aşağıdaki örnekte ''SignedDocument'' classını kullanarak önce imzalanacak veri dökümana eklenmekte, daha sonra bu veriyi imzalayan birbirinden bağımsız iki imza yaratılmaktadır. ''ParallelEnveloped'' sınıfını inceleyebilirsiniz. |
<sxh java> | <sxh java> | ||
- | Context context = new Context(BASE_DIR); | + | Context context = createContext(); |
- | SignedDocument signatures = new SignedDocument(context); | + | |
- | Document doc = Resolver.resolve("./sample.txt", context); | + | |
- | String fragment = signatures.addDocument(doc); | + | |
- | XMLSignature signature1 = signatures.createSignature(); | + | SignedDocument signatures = new SignedDocument(context); |
- | // add document as inner reference | + | |
- | signature1.addDocument("#"+fragment, "text/plain", false); | + | |
- | // add certificate to show who signed the document | + | |
- | signature1.addKeyInfo(CERTIFICATE); | + | |
- | // now sign it by using private key | + | |
- | signature1.sign(PRIVATE_KEY); | + | |
- | XMLSignature signature2 = signatures.createSignature(); | + | Document doc = Resolver.resolve("./sample.txt", context); |
- | // add document as inner reference | + | String fragment = signatures.addDocument(doc); |
- | signature2.addDocument("#"+fragment, "text/plain", false); | + | |
- | // add certificate to show who signed the document | + | |
- | signature2.addKeyInfo(CERTIFICATE); | + | |
- | // now sign it by using private key | + | |
- | signature2.sign(PRIVATE_KEY); | + | |
- | // write combined document | + | XMLSignature signature1 = signatures.createSignature(); |
- | signatures.write(new FileOutputStream(FILENAME_PARALLEL_ENVELOPED)); | + | |
+ | // add document as inner reference | ||
+ | signature1.addDocument("#"+fragment, "text/plain", false); | ||
+ | |||
+ | signature1.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); | ||
+ | |||
+ | // false-true gets non-qualified certificates while true-false gets qualified ones | ||
+ | X509Certificate cert = JSmartCardManager.getInstance().getSignatureCertificate(true, false); | ||
+ | |||
+ | // add certificate to show who signed the document | ||
+ | signature1.addKeyInfo(new ECertificate(cert.getEncoded())); | ||
+ | |||
+ | // now sign it by using smart card | ||
+ | signature1.sign(JSmartCardManager.getInstance().getSigner(PIN, cert)); | ||
+ | |||
+ | XMLSignature signature2 = signatures.createSignature(); | ||
+ | |||
+ | // add document as inner reference | ||
+ | signature2.addDocument("#"+fragment, "text/plain", false); | ||
+ | |||
+ | signature2.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); | ||
+ | |||
+ | // add certificate to show who signed the document | ||
+ | signature2.addKeyInfo(new ECertificate(cert.getEncoded())); | ||
+ | |||
+ | // now sign it by using smart card | ||
+ | signature2.sign(JSmartCardManager.getInstance().getSigner(PIN, cert)); | ||
+ | |||
+ | // write combined document | ||
+ | signatures.write(new FileOutputStream(BASE_DIR + SIGNATURE_FILENAME)); | ||
</sxh> | </sxh> | ||
- | Dışarıdaki veriyi imzalayan iki imzayı örnekleyelim. | + | Dışarıdaki veriyi imzalayan iki imzayı örnekleyelim. Örnek kodları ''ParallelDetached'' sınıfı içindedir. |
<sxh java> | <sxh java> | ||
- | Context context = new Context(BASE_DIR); | + | Context context = createContext(); |
- | SignedDocument signatures = new SignedDocument(context); | + | |
- | XMLSignature signature1 = signatures.createSignature(); | + | |
- | // add document as reference, but do not embed it | + | SignedDocument signatures = new SignedDocument(context); |
- | // into the signature (embed=false) | + | |
- | signature1.addDocument("./sample.txt", "text/plain", false); | + | XMLSignature signature1 = signatures.createSignature(); |
- | // add certificate to show who signed the document | + | |
- | signature1.addKeyInfo(CERTIFICATE); | + | // add document as reference, but do not embed it |
- | // now sign it by using private key | + | // into the signature (embed=false) |
- | signature1.sign(PRIVATE_KEY); | + | signature1.addDocument("./sample.txt", "text/plain", false); |
+ | |||
+ | signature1.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); | ||
+ | |||
+ | // false-true gets non-qualified certificates while true-false gets qualified ones | ||
+ | X509Certificate cert = JSmartCardManager.getInstance().getSignatureCertificate(true, false); | ||
+ | |||
+ | // add certificate to show who signed the document | ||
+ | signature1.addKeyInfo(new ECertificate(cert.getEncoded())); | ||
+ | |||
+ | // now sign it by using smart card | ||
+ | signature1.sign(JSmartCardManager.getInstance().getSigner(PIN, cert)); | ||
XMLSignature signature2 = signatures.createSignature(); | XMLSignature signature2 = signatures.createSignature(); | ||
- | // add document as reference, but do not embed it | ||
- | // into the signature (embed=false) | ||
- | signature2.addDocument("./sample.txt", "text/plain", false); | ||
- | // add certificate to show who signed the document | ||
- | signature2.addKeyInfo(CERTIFICATE); | ||
- | // now sign it by using private key | ||
- | signature2.sign(PRIVATE_KEY); | ||
- | // write combined document | + | // add document as reference, but do not embed it |
- | signatures.write(new FileOutputStream(FILENAME_PARALLEL_DETACHED)); | + | // into the signature (embed=false) |
+ | signature2.addDocument("./sample.txt", "text/plain", false); | ||
+ | |||
+ | signature2.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); | ||
+ | |||
+ | // add certificate to show who signed the document | ||
+ | signature2.addKeyInfo(new ECertificate(cert.getEncoded())); | ||
+ | |||
+ | // now sign it by using smart card | ||
+ | signature2.sign(JSmartCardManager.getInstance().getSigner(PIN, cert)); | ||
+ | |||
+ | // write combined document | ||
+ | signatures.write(new FileOutputStream(BASE_DIR + SIGNATURE_FILENAME)); | ||
</sxh> | </sxh> | ||
Satır 83: | Satır 113: | ||
===== Seri İmza ===== | ===== Seri İmza ===== | ||
- | Veriyi imzalayan imzanın imzalanmasıdır. Buna "counter signature" denir. Aşağıdaki örnekte önceden yaratılmış imzanın okunup, seri imzalanması gösterilmektedir: | + | Veriyi imzalayan imzanın imzalanmasıdır. Buna "counter signature" denir. Aşağıdaki örnekte önceden yaratılmış imzanın okunup, seri imzalanması gösterilmektedir. Örnek kodlar ''CounterDetached'' sınıfındadır. |
<sxh java> | <sxh java> | ||
- | // read previously created signature, you need to run Detached.java first | + | Context context = createContext(); |
- | Document doc = Resolver.resolve(Detached.SIGNATURE_FILENAME, context); | + | |
- | XMLSignature signature = XMLSignature.parse(doc, context); | + | // read previously created signature, you need to run Detached.java first |
- | // create counter signature | + | Document doc = Resolver.resolve(Detached.SIGNATURE_FILENAME, context); |
- | XMLSignature counterSignature = signature.createCounterSignature(); | + | XMLSignature signature = XMLSignature.parse(doc, context); |
- | // sign | + | |
- | counterSignature.addKeyInfo(CERTIFICATE); | + | // create counter signature |
- | counterSignature.sign(PRIVATE_KEY); | + | XMLSignature counterSignature = signature.createCounterSignature(); |
- | // signature contains itself and counter signature | + | |
- | signature.write(new FileOutputStream(FILENAME_COUNTER_EXISTING)); | + | counterSignature.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); |
+ | |||
+ | // false-true gets non-qualified certificates while true-false gets qualified ones | ||
+ | X509Certificate cert = JSmartCardManager.getInstance().getSignatureCertificate(true, false); | ||
+ | |||
+ | // add certificate to show who signed the document | ||
+ | counterSignature.addKeyInfo(new ECertificate(cert.getEncoded())); | ||
+ | |||
+ | // now sign it by using smart card | ||
+ | counterSignature.sign(JSmartCardManager.getInstance().getSigner(PIN, cert)); | ||
+ | |||
+ | // signature contains itself and counter signature | ||
+ | signature.write(new FileOutputStream(BASE_DIR + SIGNATURE_FILENAME)); | ||
</sxh> | </sxh> | ||
- | Şimdi daha karışık bir senaryoda iki paralel imzanın olduğu bir dökümana bir de seri imza ekleyelim: | + | Şimdi daha karışık bir senaryoda iki paralel imzanın olduğu bir dökümana bir de seri imza ekleyelim. Kodları ''CounterParallel'' sınıfından inceleyebilirsiniz. |
<sxh java> | <sxh java> | ||
- | Context context = new Context(BASE_DIR); | + | Context context = createContext(); |
- | // read previously created signature | + | |
- | Document signatureFile = Resolver.resolve(FILENAME_PARALLEL_DETACHED, context); | + | // read previously created signature |
- | SignedDocument signedDocument = new SignedDocument(signatureFile, context); | + | Document signatureFile = Resolver.resolve(ParallelDetached.SIGNATURE_FILENAME, context); |
- | // get First signature | + | SignedDocument signedDocument = new SignedDocument(signatureFile, context); |
- | XMLSignature signature = signedDocument.getSignature(0); | + | |
- | // create counter signature | + | // get first signature |
+ | XMLSignature signature = signedDocument.getSignature(0); | ||
+ | |||
+ | // create counter signature to the first one | ||
XMLSignature counterSignature = signature.createCounterSignature(); | XMLSignature counterSignature = signature.createCounterSignature(); | ||
- | // sign counterSignature.addKeyInfo(CERTIFICATE); | + | |
- | counterSignature.sign(PRIVATE_KEY); | + | counterSignature.getSignedInfo().setSignatureMethod(SignatureMethod.RSA_SHA256); |
- | // signed doc contains both previous signature and now a counter signature | + | |
- | // in first signature signedDocument.write(new FileOutputStream(FILENAME_PARALLEL_COUNTER)); | + | // false-true gets non-qualified certificates while true-false gets qualified ones |
+ | X509Certificate cert = JSmartCardManager.getInstance().getSignatureCertificate(true, false); | ||
+ | |||
+ | // add certificate to show who signed the document | ||
+ | counterSignature.addKeyInfo(new ECertificate(cert.getEncoded())); | ||
+ | |||
+ | // now sign it by using smart card | ||
+ | counterSignature.sign(JSmartCardManager.getInstance().getSigner(PIN, cert)); | ||
+ | |||
+ | // signed doc contains both previous signature and now a counter signature | ||
+ | // in first signature | ||
+ | signedDocument.write(new FileOutputStream(BASE_DIR + SIGNATURE_FILENAME)); | ||
</sxh> | </sxh> | ||
Bir önceki örnekten pek de farklı değil. Paralel imzaların SignedDocument içinde tutulması dışında. | Bir önceki örnekten pek de farklı değil. Paralel imzaların SignedDocument içinde tutulması dışında. | ||