xref: /AOO41X/main/xmlsecurity/tools/standalone/mscsfit/signer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /** -- C++ Source File -- **/
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
4*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
5*cdf0e10cSrcweir #include <stdio.h>
6*cdf0e10cSrcweir #include "helper.hxx"
7*cdf0e10cSrcweir 
8*cdf0e10cSrcweir #include "libxml/tree.h"
9*cdf0e10cSrcweir #include "libxml/parser.h"
10*cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT
11*cdf0e10cSrcweir #include "libxslt/xslt.h"
12*cdf0e10cSrcweir #endif
13*cdf0e10cSrcweir 
14*cdf0e10cSrcweir #include "securityenvironment_mscryptimpl.hxx"
15*cdf0e10cSrcweir #include "xmlelementwrapper_xmlsecimpl.hxx"
16*cdf0e10cSrcweir 
17*cdf0e10cSrcweir #include "xmlsec/strings.h"
18*cdf0e10cSrcweir #include "xmlsec/mscrypto/app.h"
19*cdf0e10cSrcweir #include "xmlsec/xmltree.h"
20*cdf0e10cSrcweir 
21*cdf0e10cSrcweir #include <rtl/ustring.hxx>
22*cdf0e10cSrcweir 
23*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
24*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
25*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
26*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
27*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
28*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
29*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir using namespace ::rtl ;
32*cdf0e10cSrcweir using namespace ::cppu ;
33*cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
34*cdf0e10cSrcweir using namespace ::com::sun::star::io ;
35*cdf0e10cSrcweir using namespace ::com::sun::star::ucb ;
36*cdf0e10cSrcweir using namespace ::com::sun::star::beans ;
37*cdf0e10cSrcweir using namespace ::com::sun::star::document ;
38*cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
39*cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ;
40*cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto ;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv )
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir 	const char* 		n_pCertStore ;
46*cdf0e10cSrcweir 	HCERTSTORE			n_hStoreHandle ;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 	xmlDocPtr			doc = NULL ;
49*cdf0e10cSrcweir 	xmlNodePtr			tplNode ;
50*cdf0e10cSrcweir 	xmlNodePtr			tarNode ;
51*cdf0e10cSrcweir 	xmlAttrPtr			idAttr ;
52*cdf0e10cSrcweir 	xmlChar*			idValue ;
53*cdf0e10cSrcweir 	xmlAttrPtr			uriAttr ;
54*cdf0e10cSrcweir 	xmlChar*			uriValue ;
55*cdf0e10cSrcweir 	OUString*			uri = NULL ;
56*cdf0e10cSrcweir 	Reference< XUriBinding >	xUriBinding ;
57*cdf0e10cSrcweir 	FILE*				dstFile = NULL ;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	if( argc !=4 && argc != 5 ) {
60*cdf0e10cSrcweir 		fprintf( stderr, "Usage: %s <file_url of template> <file_url of result> <rdb file>\n" , argv[0] ) ;
61*cdf0e10cSrcweir 		fprintf( stderr, "Or: \t%s <file_url of template> <file_url of result> <rdb file> < Cert Store Name >\n\n" , argv[0] ) ;
62*cdf0e10cSrcweir 		return 1 ;
63*cdf0e10cSrcweir 	}
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	for( int hhh = 0 ; hhh < 4 ; hhh ++ ) {
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 	//Init libxml and libxslt libraries
68*cdf0e10cSrcweir 	xmlInitParser();
69*cdf0e10cSrcweir 	LIBXML_TEST_VERSION
70*cdf0e10cSrcweir 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
71*cdf0e10cSrcweir 	xmlSubstituteEntitiesDefault(1);
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
74*cdf0e10cSrcweir 	xmlIndentTreeOutput = 1;
75*cdf0e10cSrcweir 	#endif // XMLSEC_NO_XSLT
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	//Initialize the crypto engine
78*cdf0e10cSrcweir 	if( argc == 5 ) {
79*cdf0e10cSrcweir 		n_pCertStore = argv[4] ;
80*cdf0e10cSrcweir 		n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ;
81*cdf0e10cSrcweir 		if( n_hStoreHandle == NULL ) {
82*cdf0e10cSrcweir 			fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ;
83*cdf0e10cSrcweir 			return 1 ;
84*cdf0e10cSrcweir 		}
85*cdf0e10cSrcweir 	} else {
86*cdf0e10cSrcweir 		n_pCertStore = NULL ;
87*cdf0e10cSrcweir 		n_hStoreHandle = NULL ;
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 	xmlSecMSCryptoAppInit( n_pCertStore ) ;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	//Load XML document
92*cdf0e10cSrcweir 	doc = xmlParseFile( argv[1] ) ;
93*cdf0e10cSrcweir 	if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
94*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot load template xml document!\n" ) ;
95*cdf0e10cSrcweir 		goto done ;
96*cdf0e10cSrcweir 	}
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	//Find the signature template
99*cdf0e10cSrcweir 	tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ;
100*cdf0e10cSrcweir 	if( tplNode == NULL ) {
101*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find the signature template!\n" ) ;
102*cdf0e10cSrcweir 		goto done ;
103*cdf0e10cSrcweir 	}
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	//Find the element with ID attribute
106*cdf0e10cSrcweir 	//Here we only try to find the "document" node.
107*cdf0e10cSrcweir 	tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ;
108*cdf0e10cSrcweir 	if( tarNode == NULL ) {
109*cdf0e10cSrcweir 		tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ;
110*cdf0e10cSrcweir 	}
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	//Find the "id" attrbute in the element
113*cdf0e10cSrcweir 	if( tarNode != NULL ) {
114*cdf0e10cSrcweir 		if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) {
115*cdf0e10cSrcweir 			//NULL
116*cdf0e10cSrcweir 		} else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) {
117*cdf0e10cSrcweir 			//NULL
118*cdf0e10cSrcweir 		} else {
119*cdf0e10cSrcweir 			idAttr = NULL ;
120*cdf0e10cSrcweir 		}
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	//Add ID to DOM
124*cdf0e10cSrcweir 	if( idAttr != NULL ) {
125*cdf0e10cSrcweir 		idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ;
126*cdf0e10cSrcweir 		if( idValue == NULL ) {
127*cdf0e10cSrcweir 			fprintf( stderr , "### the ID value is NULL!\n" ) ;
128*cdf0e10cSrcweir 			goto done ;
129*cdf0e10cSrcweir 		}
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) {
132*cdf0e10cSrcweir 			fprintf( stderr , "### Can not add the ID value!\n" ) ;
133*cdf0e10cSrcweir 			goto done ;
134*cdf0e10cSrcweir 		}
135*cdf0e10cSrcweir 	}
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	//Reference handler
138*cdf0e10cSrcweir 	//Find the signature reference
139*cdf0e10cSrcweir 	tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ;
140*cdf0e10cSrcweir 	if( tarNode == NULL ) {
141*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find the signature reference!\n" ) ;
142*cdf0e10cSrcweir 		goto done ;
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	//Find the "URI" attrbute in the reference
146*cdf0e10cSrcweir 	uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ;
147*cdf0e10cSrcweir 	if( tarNode == NULL ) {
148*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find URI of the reference!\n" ) ;
149*cdf0e10cSrcweir 		goto done ;
150*cdf0e10cSrcweir 	}
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	//Get the "URI" attrbute value
153*cdf0e10cSrcweir 	uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ;
154*cdf0e10cSrcweir 	if( uriValue == NULL ) {
155*cdf0e10cSrcweir 		fprintf( stderr , "### the URI value is NULL!\n" ) ;
156*cdf0e10cSrcweir 		goto done ;
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) {
160*cdf0e10cSrcweir 		fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ;
161*cdf0e10cSrcweir 	//	uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ;
162*cdf0e10cSrcweir 		uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ;
163*cdf0e10cSrcweir 	}
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	if( uri != NULL ) {
166*cdf0e10cSrcweir 		fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
167*cdf0e10cSrcweir 		Reference< XInputStream > xStream = createStreamFromFile( *uri ) ;
168*cdf0e10cSrcweir 		if( !xStream.is() ) {
169*cdf0e10cSrcweir 			fprintf( stderr , "### Can not get the URI stream!\n" ) ;
170*cdf0e10cSrcweir 			goto done ;
171*cdf0e10cSrcweir 		}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 		xUriBinding = new OUriBinding( *uri, xStream ) ;
174*cdf0e10cSrcweir 	}
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	try {
177*cdf0e10cSrcweir 		Reference< XMultiComponentFactory > xManager = NULL ;
178*cdf0e10cSrcweir 		Reference< XComponentContext > xContext = NULL ;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 		xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[3] ) ) ;
181*cdf0e10cSrcweir 		OSL_ENSURE( xManager.is() ,
182*cdf0e10cSrcweir 			"ServicesManager - "
183*cdf0e10cSrcweir 			"Cannot get service manager" ) ;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 		//Create signature template
186*cdf0e10cSrcweir 		Reference< XInterface > element =
187*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
188*cdf0e10cSrcweir 		OSL_ENSURE( element.is() ,
189*cdf0e10cSrcweir 			"Signer - "
190*cdf0e10cSrcweir 			"Cannot get service instance of \"wrapper.XMLElementWrapper\"" ) ;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 		Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ;
193*cdf0e10cSrcweir 		OSL_ENSURE( xElement.is() ,
194*cdf0e10cSrcweir 			"Signer - "
195*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLElement\" from service \"xsec.XMLElement\"" ) ;
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 		Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ;
198*cdf0e10cSrcweir 		OSL_ENSURE( xEleTunnel.is() ,
199*cdf0e10cSrcweir 			"Signer - "
200*cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ;
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 		XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
203*cdf0e10cSrcweir 		OSL_ENSURE( pElement != NULL ,
204*cdf0e10cSrcweir 			"Signer - "
205*cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.XMLElement\"" ) ;
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		//Set signature template
208*cdf0e10cSrcweir 		pElement->setNativeElement( tplNode ) ;
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 		//Build XML Signature template
211*cdf0e10cSrcweir 		Reference< XInterface > signtpl =
212*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSignatureTemplate" ) , xContext ) ;
213*cdf0e10cSrcweir 		OSL_ENSURE( signtpl.is() ,
214*cdf0e10cSrcweir 			"Signer - "
215*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 		Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ;
218*cdf0e10cSrcweir 		OSL_ENSURE( xTemplate.is() ,
219*cdf0e10cSrcweir 			"Signer - "
220*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ;
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 		//Import the signature template
223*cdf0e10cSrcweir 		xTemplate->setTemplate( xElement ) ;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 		//Import the URI/Stream binding
226*cdf0e10cSrcweir 		if( xUriBinding.is() )
227*cdf0e10cSrcweir 			xTemplate->setBinding( xUriBinding ) ;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 		//Create security environment
230*cdf0e10cSrcweir 		//Build Security Environment
231*cdf0e10cSrcweir 		Reference< XInterface > xsecenv =
232*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ;
233*cdf0e10cSrcweir 		OSL_ENSURE( xsecenv.is() ,
234*cdf0e10cSrcweir 			"Signer - "
235*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 		Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
238*cdf0e10cSrcweir 		OSL_ENSURE( xSecEnv.is() ,
239*cdf0e10cSrcweir 			"Signer - "
240*cdf0e10cSrcweir 			"Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 		//Setup key slot and certDb
243*cdf0e10cSrcweir 		Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
244*cdf0e10cSrcweir 		OSL_ENSURE( xElement.is() ,
245*cdf0e10cSrcweir 			"Signer - "
246*cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 		SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
249*cdf0e10cSrcweir 		OSL_ENSURE( pSecEnv != NULL ,
250*cdf0e10cSrcweir 			"Signer - "
251*cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 		//Setup key slot and certDb
254*cdf0e10cSrcweir 		if( n_hStoreHandle != NULL ) {
255*cdf0e10cSrcweir 			pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
256*cdf0e10cSrcweir 			pSecEnv->setCertDb( n_hStoreHandle ) ;
257*cdf0e10cSrcweir 		} else {
258*cdf0e10cSrcweir 			pSecEnv->enableDefaultCrypt( sal_True ) ;
259*cdf0e10cSrcweir 		}
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 		//Build XML Security Context
262*cdf0e10cSrcweir 		Reference< XInterface > xmlsecctx =
263*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl"), xContext ) ;
264*cdf0e10cSrcweir 		OSL_ENSURE( xsecenv.is() ,
265*cdf0e10cSrcweir 			"Signer - "
266*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 		Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
269*cdf0e10cSrcweir 		OSL_ENSURE( xSecCtx.is() ,
270*cdf0e10cSrcweir 			"Signer - "
271*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 		xSecCtx->addSecurityEnvironment( xSecEnv ) ;
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 		//Generate XML signature
276*cdf0e10cSrcweir 		Reference< XInterface > xmlsigner =
277*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_MSCryptImpl"), xContext ) ;
278*cdf0e10cSrcweir 		OSL_ENSURE( xmlsigner.is() ,
279*cdf0e10cSrcweir 			"Signer - "
280*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSignature\"" ) ;
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 		Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ;
283*cdf0e10cSrcweir 		OSL_ENSURE( xSigner.is() ,
284*cdf0e10cSrcweir 			"Signer - "
285*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir 		//perform signature
288*cdf0e10cSrcweir 		xTemplate = xSigner->generate( xTemplate , xSecEnv ) ;
289*cdf0e10cSrcweir 		OSL_ENSURE( xTemplate.is() ,
290*cdf0e10cSrcweir 			"Signer - "
291*cdf0e10cSrcweir 			"Cannot generate the xml signature" ) ;
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir 		SecurityOperationStatus m_nStatus = xTemplate->getStatus();
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 		if (m_nStatus == SecurityOperationStatus_OPERATION_SUCCEEDED)
296*cdf0e10cSrcweir 		{
297*cdf0e10cSrcweir 			fprintf( stdout, "Operation succeeds.\n") ;
298*cdf0e10cSrcweir 		}
299*cdf0e10cSrcweir 		else
300*cdf0e10cSrcweir 		{
301*cdf0e10cSrcweir 			fprintf( stdout, "Operation fails.\n") ;
302*cdf0e10cSrcweir 		}
303*cdf0e10cSrcweir 	} catch( Exception& e ) {
304*cdf0e10cSrcweir 		fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
305*cdf0e10cSrcweir 		goto done ;
306*cdf0e10cSrcweir 	}
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir 	dstFile = fopen( argv[2], "w" ) ;
309*cdf0e10cSrcweir 	if( dstFile == NULL ) {
310*cdf0e10cSrcweir 		fprintf( stderr , "### Can not open file %s\n", argv[2] ) ;
311*cdf0e10cSrcweir 		goto done ;
312*cdf0e10cSrcweir 	}
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir 	//Save result
315*cdf0e10cSrcweir 	xmlDocDump( dstFile, doc ) ;
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir done:
318*cdf0e10cSrcweir 	if( uri != NULL )
319*cdf0e10cSrcweir 		delete uri ;
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir 	if( dstFile != NULL )
322*cdf0e10cSrcweir 		fclose( dstFile ) ;
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir 	if( doc != NULL )
325*cdf0e10cSrcweir 		xmlFreeDoc( doc ) ;
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir 	if( n_hStoreHandle != NULL )
328*cdf0e10cSrcweir 		CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir 	xmlSecMSCryptoAppShutdown() ;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 	/* Shutdown libxslt/libxml */
333*cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
334*cdf0e10cSrcweir 	xsltCleanupGlobals();
335*cdf0e10cSrcweir 	#endif /* XMLSEC_NO_XSLT */
336*cdf0e10cSrcweir 	xmlCleanupParser();
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir 	}
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir 	return 0;
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir 
343