xref: /AOO41X/main/xmlsecurity/tools/standalone/csfit/signer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include "helper.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "libxml/tree.h"
35*cdf0e10cSrcweir #include "libxml/parser.h"
36*cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT
37*cdf0e10cSrcweir #include "libxslt/xslt.h"
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "securityenvironment_nssimpl.hxx"
41*cdf0e10cSrcweir #include "xmlelementwrapper_xmlsecimpl.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "nspr.h"
44*cdf0e10cSrcweir #include "prtypes.h"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include "pk11func.h"
47*cdf0e10cSrcweir #include "cert.h"
48*cdf0e10cSrcweir #include "cryptohi.h"
49*cdf0e10cSrcweir #include "certdb.h"
50*cdf0e10cSrcweir #include "nss.h"
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include "xmlsec/strings.h"
53*cdf0e10cSrcweir #include "xmlsec/xmltree.h"
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include <rtl/ustring.hxx>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
62*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
63*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir using namespace ::rtl ;
66*cdf0e10cSrcweir using namespace ::cppu ;
67*cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
68*cdf0e10cSrcweir using namespace ::com::sun::star::io ;
69*cdf0e10cSrcweir using namespace ::com::sun::star::ucb ;
70*cdf0e10cSrcweir using namespace ::com::sun::star::beans ;
71*cdf0e10cSrcweir using namespace ::com::sun::star::document ;
72*cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
73*cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ;
74*cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto ;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv )
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir 	CERTCertDBHandle*	certHandle ;
80*cdf0e10cSrcweir 	PK11SlotInfo*		slot ;
81*cdf0e10cSrcweir 	xmlDocPtr			doc ;
82*cdf0e10cSrcweir 	xmlNodePtr			tplNode ;
83*cdf0e10cSrcweir 	xmlNodePtr			tarNode ;
84*cdf0e10cSrcweir 	xmlAttrPtr			idAttr ;
85*cdf0e10cSrcweir 	xmlChar*			idValue ;
86*cdf0e10cSrcweir 	xmlAttrPtr			uriAttr ;
87*cdf0e10cSrcweir 	xmlChar*			uriValue ;
88*cdf0e10cSrcweir 	OUString*			uri ;
89*cdf0e10cSrcweir 	Reference< XUriBinding >	xUriBinding ;
90*cdf0e10cSrcweir 	FILE*				dstFile ;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	if( argc != 5 ) {
93*cdf0e10cSrcweir 		fprintf( stderr, "Usage: %s < CertDir > <file_url of template> <file_url of result> <rdb file>\n\n" , argv[0] ) ;
94*cdf0e10cSrcweir 		return 1 ;
95*cdf0e10cSrcweir 	}
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	for( int hhh = 0 ; hhh < 10 ; hhh ++ ) {
98*cdf0e10cSrcweir 		slot = NULL ;
99*cdf0e10cSrcweir 		doc = NULL ;
100*cdf0e10cSrcweir 		uri = NULL ;
101*cdf0e10cSrcweir 		dstFile = NULL ;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	//Init libxml and libxslt libraries
105*cdf0e10cSrcweir 	xmlInitParser();
106*cdf0e10cSrcweir 	LIBXML_TEST_VERSION
107*cdf0e10cSrcweir 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
108*cdf0e10cSrcweir 	xmlSubstituteEntitiesDefault(1);
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
111*cdf0e10cSrcweir 	xmlIndentTreeOutput = 1;
112*cdf0e10cSrcweir 	#endif // XMLSEC_NO_XSLT
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 	//Initialize NSPR and NSS
116*cdf0e10cSrcweir 	PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
117*cdf0e10cSrcweir 	PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
118*cdf0e10cSrcweir 	if( NSS_Init( argv[1] ) != SECSuccess ) {
119*cdf0e10cSrcweir 		fprintf( stderr , "### cannot intialize NSS!\n" ) ;
120*cdf0e10cSrcweir 		goto done ;
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	certHandle = CERT_GetDefaultCertDB() ;
124*cdf0e10cSrcweir 	slot = PK11_GetInternalKeySlot() ;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	if( PK11_NeedLogin( slot ) ) {
127*cdf0e10cSrcweir 		SECStatus nRet = PK11_Authenticate( slot, PR_TRUE, NULL );
128*cdf0e10cSrcweir 		if( nRet != SECSuccess ) {
129*cdf0e10cSrcweir 			fprintf( stderr , "### cannot authehticate the crypto token!\n" ) ;
130*cdf0e10cSrcweir 			goto done ;
131*cdf0e10cSrcweir 		}
132*cdf0e10cSrcweir 	}
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 	//Load XML document
135*cdf0e10cSrcweir 	doc = xmlParseFile( argv[2] ) ;
136*cdf0e10cSrcweir 	if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
137*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot load template xml document!\n" ) ;
138*cdf0e10cSrcweir 		goto done ;
139*cdf0e10cSrcweir 	}
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 	//Find the signature template
142*cdf0e10cSrcweir 	tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ;
143*cdf0e10cSrcweir 	if( tplNode == NULL ) {
144*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find the signature template!\n" ) ;
145*cdf0e10cSrcweir 		goto done ;
146*cdf0e10cSrcweir 	}
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	//Find the element with ID attribute
149*cdf0e10cSrcweir 	//Here we only try to find the "document" node.
150*cdf0e10cSrcweir 	tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ;
151*cdf0e10cSrcweir 	if( tarNode == NULL ) {
152*cdf0e10cSrcweir 		tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ;
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 	//Find the "id" attrbute in the element
156*cdf0e10cSrcweir 	if( tarNode != NULL ) {
157*cdf0e10cSrcweir 		if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) {
158*cdf0e10cSrcweir 			//NULL
159*cdf0e10cSrcweir 		} else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) {
160*cdf0e10cSrcweir 			//NULL
161*cdf0e10cSrcweir 		} else {
162*cdf0e10cSrcweir 			idAttr = NULL ;
163*cdf0e10cSrcweir 		}
164*cdf0e10cSrcweir 	}
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	//Add ID to DOM
167*cdf0e10cSrcweir 	if( idAttr != NULL ) {
168*cdf0e10cSrcweir 		idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ;
169*cdf0e10cSrcweir 		if( idValue == NULL ) {
170*cdf0e10cSrcweir 			fprintf( stderr , "### the ID value is NULL!\n" ) ;
171*cdf0e10cSrcweir 			goto done ;
172*cdf0e10cSrcweir 		}
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 		if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) {
175*cdf0e10cSrcweir 			fprintf( stderr , "### Can not add the ID value!\n" ) ;
176*cdf0e10cSrcweir 			goto done ;
177*cdf0e10cSrcweir 		}
178*cdf0e10cSrcweir 	}
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	//Reference handler
181*cdf0e10cSrcweir 	//Find the signature reference
182*cdf0e10cSrcweir 	tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ;
183*cdf0e10cSrcweir 	if( tarNode == NULL ) {
184*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find the signature reference!\n" ) ;
185*cdf0e10cSrcweir 		goto done ;
186*cdf0e10cSrcweir 	}
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	//Find the "URI" attrbute in the reference
189*cdf0e10cSrcweir 	uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ;
190*cdf0e10cSrcweir 	if( tarNode == NULL ) {
191*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find URI of the reference!\n" ) ;
192*cdf0e10cSrcweir 		goto done ;
193*cdf0e10cSrcweir 	}
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 	//Get the "URI" attrbute value
196*cdf0e10cSrcweir 	uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ;
197*cdf0e10cSrcweir 	if( uriValue == NULL ) {
198*cdf0e10cSrcweir 		fprintf( stderr , "### the URI value is NULL!\n" ) ;
199*cdf0e10cSrcweir 		goto done ;
200*cdf0e10cSrcweir 	}
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) {
203*cdf0e10cSrcweir 		fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ;
204*cdf0e10cSrcweir 	//	uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ;
205*cdf0e10cSrcweir 		uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ;
206*cdf0e10cSrcweir 	}
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 	if( uri != NULL ) {
209*cdf0e10cSrcweir 		fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
210*cdf0e10cSrcweir 		Reference< XInputStream > xStream = createStreamFromFile( *uri ) ;
211*cdf0e10cSrcweir 		if( !xStream.is() ) {
212*cdf0e10cSrcweir 			fprintf( stderr , "### Can not get the URI stream!\n" ) ;
213*cdf0e10cSrcweir 			goto done ;
214*cdf0e10cSrcweir 		}
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 		xUriBinding = new OUriBinding( *uri, xStream ) ;
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	try {
220*cdf0e10cSrcweir 		Reference< XMultiComponentFactory > xManager = NULL ;
221*cdf0e10cSrcweir 		Reference< XComponentContext > xContext = NULL ;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 		xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ;
224*cdf0e10cSrcweir 		OSL_ENSURE( xManager.is() ,
225*cdf0e10cSrcweir 			"ServicesManager - "
226*cdf0e10cSrcweir 			"Cannot get service manager" ) ;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 		//Create signature template
229*cdf0e10cSrcweir 		Reference< XInterface > element =
230*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
231*cdf0e10cSrcweir 		OSL_ENSURE( element.is() ,
232*cdf0e10cSrcweir 			"Signer - "
233*cdf0e10cSrcweir 			"Cannot get service instance of \"wrapper.XMLElementWrapper\"" ) ;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 		Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ;
236*cdf0e10cSrcweir 		OSL_ENSURE( xElement.is() ,
237*cdf0e10cSrcweir 			"Signer - "
238*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLElement\" from service \"xsec.XMLElement\"" ) ;
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 		Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ;
241*cdf0e10cSrcweir 		OSL_ENSURE( xEleTunnel.is() ,
242*cdf0e10cSrcweir 			"Signer - "
243*cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 		XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
246*cdf0e10cSrcweir 		OSL_ENSURE( pElement != NULL ,
247*cdf0e10cSrcweir 			"Signer - "
248*cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.XMLElement\"" ) ;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 		//Set signature template
251*cdf0e10cSrcweir 		pElement->setNativeElement( tplNode ) ;
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 		//Build XML Signature template
254*cdf0e10cSrcweir 		Reference< XInterface > signtpl =
255*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSignatureTemplate" ) , xContext ) ;
256*cdf0e10cSrcweir 		OSL_ENSURE( signtpl.is() ,
257*cdf0e10cSrcweir 			"Signer - "
258*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 		Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ;
261*cdf0e10cSrcweir 		OSL_ENSURE( xTemplate.is() ,
262*cdf0e10cSrcweir 			"Signer - "
263*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 		//Import the signature template
266*cdf0e10cSrcweir 		xTemplate->setTemplate( xElement ) ;
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 		//Import the URI/Stream binding
269*cdf0e10cSrcweir 		if( xUriBinding.is() )
270*cdf0e10cSrcweir 			xTemplate->setBinding( xUriBinding ) ;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 		//Create security environment
273*cdf0e10cSrcweir 		//Build Security Environment
274*cdf0e10cSrcweir 		Reference< XInterface > xsecenv =
275*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ;
276*cdf0e10cSrcweir 		OSL_ENSURE( xsecenv.is() ,
277*cdf0e10cSrcweir 			"Signer - "
278*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir 		Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
281*cdf0e10cSrcweir 		OSL_ENSURE( xSecEnv.is() ,
282*cdf0e10cSrcweir 			"Signer - "
283*cdf0e10cSrcweir 			"Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 		//Setup key slot and certDb
286*cdf0e10cSrcweir 		Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
287*cdf0e10cSrcweir 		OSL_ENSURE( xElement.is() ,
288*cdf0e10cSrcweir 			"Signer - "
289*cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 		SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
292*cdf0e10cSrcweir 		OSL_ENSURE( pSecEnv != NULL ,
293*cdf0e10cSrcweir 			"Signer - "
294*cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 		pSecEnv->setCryptoSlot( slot ) ;
297*cdf0e10cSrcweir 		pSecEnv->setCertDb( certHandle ) ;
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir 		//Build XML Security Context
300*cdf0e10cSrcweir 		Reference< XInterface > xmlsecctx =
301*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ;
302*cdf0e10cSrcweir 		OSL_ENSURE( xsecenv.is() ,
303*cdf0e10cSrcweir 			"Signer - "
304*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 		Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
307*cdf0e10cSrcweir 		OSL_ENSURE( xSecCtx.is() ,
308*cdf0e10cSrcweir 			"Signer - "
309*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir 		xSecCtx->setSecurityEnvironment( xSecEnv ) ;
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 		//Generate XML signature
314*cdf0e10cSrcweir 		Reference< XInterface > xmlsigner =
315*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl"), xContext ) ;
316*cdf0e10cSrcweir 		OSL_ENSURE( xmlsigner.is() ,
317*cdf0e10cSrcweir 			"Signer - "
318*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSignature\"" ) ;
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 		Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ;
321*cdf0e10cSrcweir 		OSL_ENSURE( xSigner.is() ,
322*cdf0e10cSrcweir 			"Signer - "
323*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 		//perform signature
326*cdf0e10cSrcweir 		xTemplate = xSigner->generate( xTemplate , xSecCtx ) ;
327*cdf0e10cSrcweir 		OSL_ENSURE( xTemplate.is() ,
328*cdf0e10cSrcweir 			"Signer - "
329*cdf0e10cSrcweir 			"Cannot generate the xml signature" ) ;
330*cdf0e10cSrcweir 	} catch( Exception& e ) {
331*cdf0e10cSrcweir 		fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
332*cdf0e10cSrcweir 		goto done ;
333*cdf0e10cSrcweir 	}
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 	dstFile = fopen( argv[3], "w" ) ;
336*cdf0e10cSrcweir 	if( dstFile == NULL ) {
337*cdf0e10cSrcweir 		fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
338*cdf0e10cSrcweir 		goto done ;
339*cdf0e10cSrcweir 	}
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir 	//Save result
342*cdf0e10cSrcweir 	xmlDocDump( dstFile, doc ) ;
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir done:
345*cdf0e10cSrcweir 	if( uri != NULL )
346*cdf0e10cSrcweir 		delete uri ;
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 	if( dstFile != NULL )
349*cdf0e10cSrcweir 		fclose( dstFile ) ;
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir 	if( doc != NULL )
352*cdf0e10cSrcweir 		xmlFreeDoc( doc ) ;
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir 	if( slot != NULL )
355*cdf0e10cSrcweir 		PK11_FreeSlot( slot ) ;
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir 	PK11_LogoutAll() ;
358*cdf0e10cSrcweir 	NSS_Shutdown() ;
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir 	/* Shutdown libxslt/libxml */
361*cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
362*cdf0e10cSrcweir 	xsltCleanupGlobals();
363*cdf0e10cSrcweir 	#endif /* XMLSEC_NO_XSLT */
364*cdf0e10cSrcweir 	xmlCleanupParser();
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir 	}
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir 	return 0;
369*cdf0e10cSrcweir }
370*cdf0e10cSrcweir 
371