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 
15*cdf0e10cSrcweir #include "securityenvironment_mscryptimpl.hxx"
16*cdf0e10cSrcweir #include "xmlelementwrapper_xmlsecimpl.hxx"
17*cdf0e10cSrcweir 
18*cdf0e10cSrcweir #include "nspr.h"
19*cdf0e10cSrcweir #include "prtypes.h"
20*cdf0e10cSrcweir 
21*cdf0e10cSrcweir #include "pk11func.h"
22*cdf0e10cSrcweir #include "cert.h"
23*cdf0e10cSrcweir #include "cryptohi.h"
24*cdf0e10cSrcweir #include "certdb.h"
25*cdf0e10cSrcweir #include "nss.h"
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir #include "xmlsec/strings.h"
28*cdf0e10cSrcweir #include "xmlsec/xmltree.h"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <rtl/ustring.hxx>
31*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
32*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace ::rtl ;
44*cdf0e10cSrcweir using namespace ::cppu ;
45*cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
46*cdf0e10cSrcweir using namespace ::com::sun::star::io ;
47*cdf0e10cSrcweir using namespace ::com::sun::star::ucb ;
48*cdf0e10cSrcweir using namespace ::com::sun::star::beans ;
49*cdf0e10cSrcweir using namespace ::com::sun::star::document ;
50*cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
51*cdf0e10cSrcweir using namespace ::com::sun::star::registry ;
52*cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ;
53*cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto ;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv )
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir 	CERTCertDBHandle*	certHandle = NULL ;
59*cdf0e10cSrcweir 	PK11SlotInfo*		slot = NULL ;
60*cdf0e10cSrcweir 	xmlDocPtr			doc = NULL ;
61*cdf0e10cSrcweir 	xmlNodePtr			tplNode ;
62*cdf0e10cSrcweir 	xmlNodePtr			tarNode ;
63*cdf0e10cSrcweir 	FILE*				dstFile = NULL ;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	if( argc != 5 ) {
67*cdf0e10cSrcweir 		fprintf( stderr, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv[0] ) ;
68*cdf0e10cSrcweir 		return 1 ;
69*cdf0e10cSrcweir 	}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	//Init libxml and libxslt libraries
72*cdf0e10cSrcweir 	xmlInitParser();
73*cdf0e10cSrcweir 	LIBXML_TEST_VERSION
74*cdf0e10cSrcweir 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
75*cdf0e10cSrcweir 	xmlSubstituteEntitiesDefault(1);
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
78*cdf0e10cSrcweir 	xmlIndentTreeOutput = 1;
79*cdf0e10cSrcweir 	#endif // XMLSEC_NO_XSLT
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	//Initialize NSPR and NSS
83*cdf0e10cSrcweir 	PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
84*cdf0e10cSrcweir 	PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
85*cdf0e10cSrcweir 	if( NSS_Init( argv[1] ) != SECSuccess ) {
86*cdf0e10cSrcweir 		fprintf( stderr , "### cannot intialize NSS!\n" ) ;
87*cdf0e10cSrcweir 		goto done ;
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	certHandle = CERT_GetDefaultCertDB() ;
91*cdf0e10cSrcweir 	slot = PK11_GetInternalKeySlot() ;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	//Load XML document
94*cdf0e10cSrcweir 	doc = xmlParseFile( argv[2] ) ;
95*cdf0e10cSrcweir 	if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
96*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot load template xml document!\n" ) ;
97*cdf0e10cSrcweir 		goto done ;
98*cdf0e10cSrcweir 	}
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	//Find the encryption template
101*cdf0e10cSrcweir 	tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
102*cdf0e10cSrcweir 	if( tplNode == NULL ) {
103*cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
104*cdf0e10cSrcweir 		goto done ;
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	try {
109*cdf0e10cSrcweir 		Reference< XMultiComponentFactory > xManager = NULL ;
110*cdf0e10cSrcweir 		Reference< XComponentContext > xContext = NULL ;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 		//Create encryption template
115*cdf0e10cSrcweir 		Reference< XInterface > tplElement =
116*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext ) ;
117*cdf0e10cSrcweir 		OSL_ENSURE( tplElement.is() ,
118*cdf0e10cSrcweir 			"Decryptor - "
119*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
122*cdf0e10cSrcweir 		OSL_ENSURE( xTplElement.is() ,
123*cdf0e10cSrcweir 			"Decryptor - "
124*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 		Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
127*cdf0e10cSrcweir 		OSL_ENSURE( xTplEleTunnel.is() ,
128*cdf0e10cSrcweir 			"Decryptor - "
129*cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
132*cdf0e10cSrcweir 		OSL_ENSURE( pTplElement != NULL ,
133*cdf0e10cSrcweir 			"Decryptor - "
134*cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 		pTplElement->setNativeElement( tplNode ) ;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 		//Build XML Encryption template
139*cdf0e10cSrcweir 		Reference< XInterface > enctpl =
140*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryptionTemplate"), xContext ) ;
141*cdf0e10cSrcweir 		OSL_ENSURE( enctpl.is() ,
142*cdf0e10cSrcweir 			"Decryptor - "
143*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 		Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
146*cdf0e10cSrcweir 		OSL_ENSURE( xTemplate.is() ,
147*cdf0e10cSrcweir 			"Decryptor - "
148*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 		//Import the encryption template
151*cdf0e10cSrcweir 		xTemplate->setTemplate( xTplElement ) ;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 		//Create security environment
154*cdf0e10cSrcweir 		//Build Security Environment
155*cdf0e10cSrcweir 		Reference< XInterface > xsecenv =
156*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.SecurityEnvironment"), xContext ) ;
157*cdf0e10cSrcweir 		OSL_ENSURE( xsecenv.is() ,
158*cdf0e10cSrcweir 			"Decryptor - "
159*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
162*cdf0e10cSrcweir 		OSL_ENSURE( xSecEnv.is() ,
163*cdf0e10cSrcweir 			"Decryptor - "
164*cdf0e10cSrcweir 			"Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 		//Setup key slot and certDb
167*cdf0e10cSrcweir 		Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
168*cdf0e10cSrcweir 		OSL_ENSURE( xEnvTunnel.is() ,
169*cdf0e10cSrcweir 			"Decryptor - "
170*cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 		SecurityEnvironment_XmlSecImpl* pSecEnv = ( SecurityEnvironment_XmlSecImpl* )xEnvTunnel->getSomething( SecurityEnvironment_XmlSecImpl::getUnoTunnelId() ) ;
173*cdf0e10cSrcweir 		OSL_ENSURE( pSecEnv != NULL ,
174*cdf0e10cSrcweir 			"Decryptor - "
175*cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 		pSecEnv->setCryptoSlot( slot ) ;
178*cdf0e10cSrcweir 		pSecEnv->setCertDb( certHandle ) ;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 		//Build XML Security Context
182*cdf0e10cSrcweir 		Reference< XInterface > xmlsecctx =
183*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLSecurityContext"), xContext ) ;
184*cdf0e10cSrcweir 		OSL_ENSURE( xmlsecctx.is() ,
185*cdf0e10cSrcweir 			"Decryptor - "
186*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 		Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
189*cdf0e10cSrcweir 		OSL_ENSURE( xSecCtx.is() ,
190*cdf0e10cSrcweir 			"Decryptor - "
191*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		xSecCtx->setSecurityEnvironment( xSecEnv ) ;
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 		//Get encrypter
197*cdf0e10cSrcweir 		Reference< XInterface > xmlencrypter =
198*cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext ) ;
199*cdf0e10cSrcweir 		OSL_ENSURE( xmlencrypter.is() ,
200*cdf0e10cSrcweir 			"Decryptor - "
201*cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 		Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
204*cdf0e10cSrcweir 		OSL_ENSURE( xEncrypter.is() ,
205*cdf0e10cSrcweir 			"Decryptor - "
206*cdf0e10cSrcweir 			"Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 		//Perform decryption
210*cdf0e10cSrcweir 		Reference< XXMLElementWrapper> xDecrRes = xEncrypter->decrypt( xTemplate , xSecCtx ) ;
211*cdf0e10cSrcweir 		OSL_ENSURE( xDecrRes.is() ,
212*cdf0e10cSrcweir 			"Decryptor - "
213*cdf0e10cSrcweir 			"Cannot decrypt the xml document" ) ;
214*cdf0e10cSrcweir 	} catch( Exception& e ) {
215*cdf0e10cSrcweir 		fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
216*cdf0e10cSrcweir 		goto done ;
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	dstFile = fopen( argv[3], "w" ) ;
220*cdf0e10cSrcweir 	if( dstFile == NULL ) {
221*cdf0e10cSrcweir 		fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
222*cdf0e10cSrcweir 		goto done ;
223*cdf0e10cSrcweir 	}
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 	//Save result
226*cdf0e10cSrcweir 	xmlDocDump( dstFile, doc ) ;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir done:
229*cdf0e10cSrcweir 	if( dstFile != NULL )
230*cdf0e10cSrcweir 		fclose( dstFile ) ;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	if( slot != NULL )
233*cdf0e10cSrcweir 		PK11_FreeSlot( slot ) ;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 	PK11_LogoutAll() ;
236*cdf0e10cSrcweir 	NSS_Shutdown() ;
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 	/* Shutdown libxslt/libxml */
239*cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
240*cdf0e10cSrcweir 	xsltCleanupGlobals();
241*cdf0e10cSrcweir 	#endif /* XMLSEC_NO_XSLT */
242*cdf0e10cSrcweir 	xmlCleanupParser();
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 	return 0;
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247