Suppose that you have some XML document you wish to sign. It turns out it's very easy to do so because cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365re is
xmlsec library, and in particular
xmlsec1 command line tool that's standard part of Fedora Linux distribution. The only problem is that its very picky and not very informative when it comes to error logging, finally, cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365re are a lot of small details that can catch you. Since I had to sign a document I spent some time trying to figure out how to do that. In cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 end, I managed to do it and I'll write here how to for a future reference. Before I continue you'll need certificate and a key to be used for verification and signing. They are not cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 topic of this post so I'll just give you cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365m:
private key,
certificate, and
CA certificate.
Ok, let's assume that you have cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 following XML document you wish to sign:
Content of first element.
Content of cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 second element.
And cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 content of cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 third element.
Basically, you can take any XML document you wish. I'll suppose that this XML document is stored in cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 file
tosign.xml. If you typed yourself XML document, or if you just want to be sure, you can check if XML is well formed. There is
xmllint tool that surves that purpose. Just run it like this:
$ xmllint tosign.xml
And if you don't get any error messages, or warnings, that cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 XML document is well formed. You can also check if cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 document is valid by providing schema, or DTD, via appropriate command line switches.
In order to sign this document you have to add XML Signature fragment to cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 XML file. That fragment defines how cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 document will be signed, what will be signed, and, where cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 signature, along with certificate, will be placed. The fragment has cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 following form:
Note that this (quite verbose) fragment has to be placed somewhere within cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 root element. Now, lets sign this, newly created document. To do so invoke
xmlsec1 command like this (this is one line in case it is broken into two due to cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 formatting):
xmlsec1 --sign --privkey-pem privkey.pem,cert.pem --output signed.xml tosign.xml
After this, cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 signed XML document will be in cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 file named
signed.xml. Take a look into it, cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 placeholders within signature fragment are filled up with signature data, and with a certificate who's private key was used to sign cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 XML document.
Note that signature itself is generated using private key (
privkey.pem) which, as its name suggest, has to be private for a signer. Ocá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365rwise, anyone can falsify signature.
Now, to verify signed XML document you have to specify trusted CA that will be used to verify signature. It has to be certificate of cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 certificate authority (CA) that issued signer's certificate. In my case that's cacert.pem, i.e.:
$ xmlsec1 --verify --trusted-pem cacert.pem signed.xml
OK
SignedInfo References (ok/all): 1/1
Manifests References (ok/all): 0/0
As you can see, cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 signature was verified OK. You can try now to change something in cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 XML document and see if cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 verification passes or not.
I'll mentioned one more thing before concluding this post. Namely, in cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 previous example cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 whole XML document was signed. But, you can sign only a part. To do so, you have to do two things. The first one is to mark cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 element that you wish to sign (its content will also be signed) and cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 second is to tell xmlsec1 to sign only that element.
The first step is accomplished by adding attribute to cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 element that should be signed. Let's assume that in our case we only want
secondelement to be signed. Modify cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 appropriate opening tag to have cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 following form:
Note that I added attribute
id, but basically any name can be used (unless you use some predefined schema or DTD).
The second step is to tell xmlsec1 that only this element should be signed. This is accomplished by modifying
Reference element to have cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 following form:
If you now try to sign this modified XML document using command I gave above, you'll receive an error message:
$ xmlsec1 --sign --privkey-pem cert.key,cert.pem --output test_signed.xml tosign.xml
func=xmlSecXPathDataExecute:file=xpath.c:line=273:obj=unknown:subj=xmlXPtrEval:error=5:libxml2 library function failed:expr=xpointer(id('signonlythiselement'))
func=xmlSecXPathDataListExecute:file=xpath.c:line=356:obj=unknown:subj=xmlSecXPathDataExecute:error=1:xmlsec library function failed:
func=xmlSecTransformXPathExecute:file=xpath.c:line=466:obj=xpointer:subj=xmlSecXPathDataExecute:error=1:xmlsec library function failed:
func=xmlSecTransformDefaultPushXml:file=transforms.c:line=2405:obj=xpointer:subj=xmlSecTransformExecute:error=1:xmlsec library function failed:
func=xmlSecTransformCtxXmlExecute:file=transforms.c:line=1236:obj=unknown:subj=xmlSecTransformPushXml:error=1:xmlsec library function failed:transform=xpointer
func=xmlSecTransformCtxExecute:file=transforms.c:line=1296:obj=unknown:subj=xmlSecTransformCtxXmlExecute:error=1:xmlsec library function failed:
func=xmlSecDSigReferenceCtxProcessNode:file=xmldsig.c:line=1571:obj=unknown:subj=xmlSecTransformCtxExecute:error=1:xmlsec library function failed:
func=xmlSecDSigCtxProcessSignedInfoNode:file=xmldsig.c:line=804:obj=unknown:subj=xmlSecDSigReferenceCtxProcessNode:error=1:xmlsec library function failed:node=Reference
func=xmlSecDSigCtxProcessSignatureNode:file=xmldsig.c:line=547:obj=unknown:subj=xmlSecDSigCtxProcessSignedInfoNode:error=1:xmlsec library function failed:
func=xmlSecDSigCtxSign:file=xmldsig.c:line=303:obj=unknown:subj=xmlSecDSigCtxSigantureProcessNode:error=1:xmlsec library function failed:
Error: signature failed
Error: failed to sign file "tosign.xml"
The problem is that URI attribute references ID attribute of an element. But, ID element isn't recognized by name but has to be specified in DTD or in schema, depending what you have. In our case cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365re is neicá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365r schema nor DTD and thus ID isn't recognized by xmlsec1. So, we have to tell it what is cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 name of cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 ID attribute, and that can be done in two ways. The first one is by using command line switch
--id-attr, and so cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 command to sign this document is:
xmlsec1 --sign --privkey-pem privkey.pem,cert.pem --id-attr:id secondelement --output signed.xml tosign.xml
The name after cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 column is cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 attribute name that is ID. Default value is "id", but can be anything else. If it is "id", cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365n it can be omitted. The argument to
--id-attr is element whose attribute should be treated as an id. You should also be careful of namespaces. If cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365y are used cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365n cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 namespace of cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 element has to be specified too, and not shorthand but cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 full namespace name. Finally, note that XML is case sensitive!
The ocá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365r possibility is to create DTD file and to give it as an argument to xmlsec1. In this case, DTD should look like this (I'll assume that this is cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 content of a file
tosign.dtd):
And you would invoke xmlsec1 like this:
xmlsec1 --sign --privkey-pem privkey.pem,cert.pem --dtd-file tosign.dtd --output signed.xml tosign.xml
Note that you'll receive a lot of warnings (DTD is incomplete) but cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 file will be signed. To check cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365 signature, you again have to specify eicá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365r
--dtd-file or
--id-attr options, e.g.
xmlsec1 --verify --trusted-pem cacert.pem --id-attr:id secondelement signed.xml
Now, you can experiment to check that really only
secondelement was signed and nothing else.
Final note. You have to put XML signature fragment in XML file you are signing. What can confuse you (and confused me) is that cá cược thể thao bet365_cách nạp tiền vào bet365_ đăng ký bet365re is an option
sign-tmpl that adds this fragment, but it is very specific and used only for
testing purposes.