1 /** -- C++ Source File -- **/
2 
3 // MARKER(update_precomp.py): autogen include statement, do not remove
4 #include "precompiled_xmlsecurity.hxx"
5 #include <stdio.h>
6 #include "helper.hxx"
7 
8 #include "libxml/tree.h"
9 #include "libxml/parser.h"
10 #ifndef XMLSEC_NO_XSLT
11 #include "libxslt/xslt.h"
12 #endif
13 
14 #include "securityenvironment_mscryptimpl.hxx"
15 #include "xmlelementwrapper_xmlsecimpl.hxx"
16 
17 #include "xmlsec/strings.h"
18 #include "xmlsec/mscrypto/app.h"
19 #include "xmlsec/xmltree.h"
20 
21 #include <rtl/ustring.hxx>
22 #include <cppuhelper/servicefactory.hxx>
23 
24 #include <com/sun/star/lang/XComponent.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
27 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
28 #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
29 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
30 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
31 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
32 
33 using namespace ::rtl ;
34 using namespace ::cppu ;
35 using namespace ::com::sun::star::uno ;
36 using namespace ::com::sun::star::io ;
37 using namespace ::com::sun::star::ucb ;
38 using namespace ::com::sun::star::beans ;
39 using namespace ::com::sun::star::document ;
40 using namespace ::com::sun::star::lang ;
41 using namespace ::com::sun::star::registry ;
42 using namespace ::com::sun::star::xml::wrapper ;
43 using namespace ::com::sun::star::xml::crypto ;
44 
45 int SAL_CALL main( int argc, char **argv )
46 {
47 	const char* 		n_pCertStore ;
48 	HCERTSTORE			n_hStoreHandle ;
49 
50 	xmlDocPtr			doc = NULL ;
51 	xmlNodePtr			tplNode ;
52 	xmlNodePtr			tarNode ;
53 	FILE*				dstFile = NULL ;
54 
55 	HCRYPTPROV			hCryptProv = NULL ;
56 	HCRYPTKEY			symKey = NULL ;
57 
58 	if( argc != 6 && argc != 7 ) {
59 		fprintf( stderr, "Usage: %s <file_url of template> <file_url of result> <target element name> <target element namespace> <rdb file>\n\n" , argv[0] ) ;
60 		fprintf( stderr, "Usage: %s <file_url of template> <file_url of result> <target element name> <target element namespace> <rdb file> < Cert Store Name >\n\n" , argv[0] ) ;
61 		return 1 ;
62 	}
63 
64 	//Init libxml and libxslt libraries
65 	xmlInitParser();
66 	LIBXML_TEST_VERSION
67 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
68 	xmlSubstituteEntitiesDefault(1);
69 
70 	#ifndef XMLSEC_NO_XSLT
71 	xmlIndentTreeOutput = 1;
72 	#endif // XMLSEC_NO_XSLT
73 
74 	//Initialize the crypto engine
75 	if( argc == 7 ) {
76 		n_pCertStore = argv[6] ;
77 		n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ;
78 		if( n_hStoreHandle == NULL ) {
79 			fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ;
80 			return 1 ;
81 		}
82 	} else {
83 		n_pCertStore = NULL ;
84 		n_hStoreHandle = NULL ;
85 	}
86 	xmlSecMSCryptoAppInit( n_pCertStore ) ;
87 
88 	//Create encryption key.
89 	//CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ;
90 	//CryptAcquireContext( &hCryptProv , "MyTempKeyContainer" , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ;
91 
92 	if( !CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_VERIFYCONTEXT ) ) {
93 		fprintf( stderr, "### cannot get crypto provider context!\n" );
94 		goto done ;
95 	}
96 
97 	if( !CryptGenKey( hCryptProv, CALG_RC4, 0x00800000 | CRYPT_EXPORTABLE, &symKey ) ) {
98 		fprintf( stderr , "### cannot create symmetric key!\n" ) ;
99 		goto done ;
100 	}
101 
102 	//Load XML document
103 	doc = xmlParseFile( argv[1] ) ;
104 	if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
105 		fprintf( stderr , "### Cannot load template xml document!\n" ) ;
106 		goto done ;
107 	}
108 
109 	//Find the encryption template
110 	tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
111 	if( tplNode == NULL ) {
112 		fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
113 		goto done ;
114 	}
115 
116 	//Find the encryption template
117 	tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( const unsigned char*)argv[3], ( const unsigned char*)argv[4] ) ;
118 	if( tarNode == NULL ) {
119 		fprintf( stderr , "### Cannot find the encryption target!\n" ) ;
120 		goto done ;
121 	}
122 
123 	try {
124 		Reference< XMultiComponentFactory > xManager = NULL ;
125 		Reference< XComponentContext > xContext = NULL ;
126 
127 		xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[5] ) ) ;
128 
129 		//Create encryption template
130 		Reference< XInterface > tplElement =
131 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
132 		OSL_ENSURE( tplElement.is() ,
133 			"Encryptor - "
134 			"Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
135 
136 		Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
137 		OSL_ENSURE( xTplElement.is() ,
138 			"Encryptor - "
139 			"Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
140 
141 		Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
142 		OSL_ENSURE( xTplEleTunnel.is() ,
143 			"Encryptor - "
144 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
145 
146 		XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
147 		OSL_ENSURE( pTplElement != NULL ,
148 			"Encryptor - "
149 			"Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
150 
151 		pTplElement->setNativeElement( tplNode ) ;
152 
153 		//Create encryption target element
154 		Reference< XInterface > tarElement =
155 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
156 		OSL_ENSURE( tarElement.is() ,
157 			"Encryptor - "
158 			"Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
159 
160 		Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ;
161 		OSL_ENSURE( xTarElement.is() ,
162 			"Encryptor - "
163 			"Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
164 
165 		Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ;
166 		OSL_ENSURE( xTarEleTunnel.is() ,
167 			"Encryptor - "
168 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
169 
170 		XMLElementWrapper_XmlSecImpl* pTarElement = ( XMLElementWrapper_XmlSecImpl* )xTarEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
171 		OSL_ENSURE( pTarElement != NULL ,
172 			"Encryptor - "
173 			"Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
174 
175 		pTarElement->setNativeElement( tarNode ) ;
176 
177 
178 		//Build XML Encryption template
179 		Reference< XInterface > enctpl =
180 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext ) ;
181 		OSL_ENSURE( enctpl.is() ,
182 			"Encryptor - "
183 			"Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
184 
185 		Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
186 		OSL_ENSURE( xTemplate.is() ,
187 			"Encryptor - "
188 			"Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
189 
190 		//Import the encryption template
191 		xTemplate->setTemplate( xTplElement ) ;
192 		xTemplate->setTarget( xTarElement ) ;
193 
194 		//Create security environment
195 		//Build Security Environment
196 		Reference< XInterface > xsecenv =
197 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ;
198 		OSL_ENSURE( xsecenv.is() ,
199 			"Encryptor - "
200 			"Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
201 
202 		Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
203 		OSL_ENSURE( xSecEnv.is() ,
204 			"Encryptor - "
205 			"Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
206 
207 		//Setup key slot and certDb
208 		Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
209 		OSL_ENSURE( xEnvTunnel.is() ,
210 			"Encryptor - "
211 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
212 
213 		SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
214 		OSL_ENSURE( pSecEnv != NULL ,
215 			"Encryptor - "
216 			"Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
217 
218 		//Setup key slot and certDb
219 		if( n_hStoreHandle != NULL ) {
220 			pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
221 			pSecEnv->setCertDb( n_hStoreHandle ) ;
222 		} else {
223 			pSecEnv->enableDefaultCrypt( sal_True ) ;
224 		}
225 
226 		pSecEnv->adoptSymKey( symKey ) ;
227 
228 
229 		//Build XML Security Context
230 		Reference< XInterface > xmlsecctx =
231 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl"), xContext ) ;
232 		OSL_ENSURE( xmlsecctx.is() ,
233 			"Encryptor - "
234 			"Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
235 
236 		Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
237 		OSL_ENSURE( xSecCtx.is() ,
238 			"Encryptor - "
239 			"Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
240 
241 		xSecCtx->addSecurityEnvironment( xSecEnv ) ;
242 
243 		//Get encrypter
244 		Reference< XInterface > xmlencrypter =
245 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_MSCryptImpl"), xContext ) ;
246 		OSL_ENSURE( xmlencrypter.is() ,
247 			"Encryptor - "
248 			"Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
249 
250 		Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
251 		OSL_ENSURE( xEncrypter.is() ,
252 			"Encryptor - "
253 			"Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
254 
255 		//perform encryption
256 		xTemplate = xEncrypter->encrypt( xTemplate , xSecEnv ) ;
257 		OSL_ENSURE( xTemplate.is() ,
258 			"Encryptor - "
259 			"Cannot encrypt the xml document" ) ;
260 
261 
262 		com::sun::star::xml::crypto::SecurityOperationStatus m_nStatus = xTemplate->getStatus();
263 		if (m_nStatus == SecurityOperationStatus_OPERATION_SUCCEEDED)
264 		{
265 			fprintf( stdout, "Operation succeeds.\n") ;
266 		}
267 		else
268 		{
269 			fprintf( stdout, "Operation fails.\n") ;
270 		}
271 	} catch( Exception& e ) {
272 		fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
273 		goto done ;
274 	}
275 
276 	dstFile = fopen( argv[2], "w" ) ;
277 	if( dstFile == NULL ) {
278 		fprintf( stderr , "### Can not open file %s\n", argv[2] ) ;
279 		goto done ;
280 	}
281 
282 	//Save result
283 	xmlDocDump( dstFile, doc ) ;
284 
285 done:
286 	if( dstFile != NULL )
287 		fclose( dstFile ) ;
288 
289 	if( symKey != NULL ) {
290 		CryptDestroyKey( symKey ) ;
291 	}
292 
293 	if( hCryptProv != NULL ) {
294 		CryptReleaseContext( hCryptProv, 0 ) ;
295 	}
296 
297 	if( n_hStoreHandle != NULL )
298 		CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
299 
300 	/* Shutdown libxslt/libxml */
301 	#ifndef XMLSEC_NO_XSLT
302 	xsltCleanupGlobals();
303 	#endif /* XMLSEC_NO_XSLT */
304 	xmlCleanupParser();
305 
306 	return 0;
307 }
308 
309