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