|
PERMISION NOTICE AND DISCLAIMER This website contains certain downloadable software. This software is copyrighted and the copyrighter claims all exclusive rights to such software. The copyright owner of the software that you download through this site may be indicated in the accompanying read-me file and in the accompanying source code as well as in the area of this Web Site from which the software is downloaded. Permission to use, copy, modify and distribute this software and its source code for non commercial purposes and without fee is hereby granted, provided that the name of the copyright owner or related contributors not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. The copyright owner and contributors makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. The copyright owner and its contributors disclaim all warranties with regard to this software, including all implied warranties of merchantability and fitness. In no event shall the author and the contributors be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortuous action, arising out of or in connection with the use or performance of this software. AcceptCancel
The objective of this example is to sign an XML document using the TrustedX signature service. In order to understand this example, it is recommended that first you understand the previously seen example: CMS/PKCS#7 Signature Generation.
To try this example, you must download the file called sampleAxis3.zip and follow the instructions in Configuration of the Environment.
As in the case of the CMS/PKCS#7 signature example, we must use a SignRequest object to perform a signature request; however, in this case it will be an XML signature profile.
SignRequest sr = new SignRequest();
sr.setProfile(new URI(PROF_SIGN_XML));
In this example, the data to be signed have an XML format and can be found in the Demo.xml file. The data will be included in the request encoded in Base64. In the same way as in the previous example, an InputDocuments object is created. This object includes a Document object containing the data. In this case, the data are inserted using the setBase64XML() method.
InputDocuments idoc = new InputDocuments();
Document doc = new Document();
doc.setBase64XML(Util.readBinaryFile(path_in + filename));
idoc.setDocument(doc);sr.setInputDocuments(idoc);
For this request, it is stated that the data are to be returned encoded in Base64. This information must be included in an OptionalInputs object via the setReturnBase64XML() method. This object must also contain the data about the certificate to be used, as we saw in the previous example.
OptionalInputs opt = new OptionalInputs();
opt.setReturnBase64XML("");
sr.setOptionalInputs(opt);
As no other details were specified, the resulting signature will be detached. Later, in other signature requests, we will show the other two types of requests possible with an XML signature, i.e. the signature request type for adding the resulting signature inside the original document, known as enveloped signature, and the signature request type for adding the document inside the generated signature, known as enveloping signature.
Then, the request is sent by invoking the sign() operation and the response is collected in a SignResponse object.
SignResponse srs = bindingDs.sign(sr);
Once the request has been sent, we can check if it has been processed correctly by consulting the response parameters. The functions of the UtilTrustedX auxiliary class are used for this. If all goes well, the signature is retrieved from the response and is stored in a file. In the XML Signature Verification example, you will see how the validity of the generated signature must be checked. Given that it was indicated that the response should return a Base64 encoded signature and that it is a XADES profile request, we must use the getBase64XMLSignature() method to retrieve the signature.
if (UtilTrustedXAxis.check(srs.getResult(), bindingDs)) {
byte[] data = srs.getSignatureObject().getBase64XMLSignature().get_value();
String destFilename = path_out + filename.substring(0, filename.lastIndexOf("."))
+ "SignedDetached.xml";
Util.writeBinaryFile(destFilename, data);
}
After executing the example, we obtain a response, such as the one below, and a file with the signature.
File saved successfully on: data/output/DemoSignedDetached.xml
|