merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / standalone / mscsfit / encrypter.cxx
blob7402d5c3984f6227ac107660cf435041026eea68
1 /** -- C++ Source File -- **/
3 // MARKER(update_precomp.py): autogen include statement, do not remove
4 #include "precompiled_xmlsecurity.hxx"
5 #include <stdio.h>
6 #include "helper.hxx"
8 #include "libxml/tree.h"
9 #include "libxml/parser.h"
10 #ifndef XMLSEC_NO_XSLT
11 #include "libxslt/xslt.h"
12 #endif
14 #include "securityenvironment_mscryptimpl.hxx"
15 #include "xmlelementwrapper_xmlsecimpl.hxx"
17 #include "xmlsec/strings.h"
18 #include "xmlsec/mscrypto/app.h"
19 #include "xmlsec/xmltree.h"
21 #include <rtl/ustring.hxx>
22 #include <cppuhelper/servicefactory.hxx>
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>
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 ;
45 int SAL_CALL main( int argc, char **argv )
47 const char* n_pCertStore ;
48 HCERTSTORE n_hStoreHandle ;
50 xmlDocPtr doc = NULL ;
51 xmlNodePtr tplNode ;
52 xmlNodePtr tarNode ;
53 FILE* dstFile = NULL ;
55 HCRYPTPROV hCryptProv = NULL ;
56 HCRYPTKEY symKey = NULL ;
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 ;
64 //Init libxml and libxslt libraries
65 xmlInitParser();
66 LIBXML_TEST_VERSION
67 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
68 xmlSubstituteEntitiesDefault(1);
70 #ifndef XMLSEC_NO_XSLT
71 xmlIndentTreeOutput = 1;
72 #endif // XMLSEC_NO_XSLT
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 ;
82 } else {
83 n_pCertStore = NULL ;
84 n_hStoreHandle = NULL ;
86 xmlSecMSCryptoAppInit( n_pCertStore ) ;
88 //Create encryption key.
89 //CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ;
90 //CryptAcquireContext( &hCryptProv , "MyTempKeyContainer" , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ;
92 if( !CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_VERIFYCONTEXT ) ) {
93 fprintf( stderr, "### cannot get crypto provider context!\n" );
94 goto done ;
97 if( !CryptGenKey( hCryptProv, CALG_RC4, 0x00800000 | CRYPT_EXPORTABLE, &symKey ) ) {
98 fprintf( stderr , "### cannot create symmetric key!\n" ) ;
99 goto done ;
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 ;
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 ;
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 ;
123 try {
124 Reference< XMultiComponentFactory > xManager = NULL ;
125 Reference< XComponentContext > xContext = NULL ;
127 xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[5] ) ) ;
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\"" ) ;
136 Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
137 OSL_ENSURE( xTplElement.is() ,
138 "Encryptor - "
139 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
141 Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
142 OSL_ENSURE( xTplEleTunnel.is() ,
143 "Encryptor - "
144 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
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\"" ) ;
151 pTplElement->setNativeElement( tplNode ) ;
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\"" ) ;
160 Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ;
161 OSL_ENSURE( xTarElement.is() ,
162 "Encryptor - "
163 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
165 Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ;
166 OSL_ENSURE( xTarEleTunnel.is() ,
167 "Encryptor - "
168 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
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\"" ) ;
175 pTarElement->setNativeElement( tarNode ) ;
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\"" ) ;
185 Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
186 OSL_ENSURE( xTemplate.is() ,
187 "Encryptor - "
188 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
190 //Import the encryption template
191 xTemplate->setTemplate( xTplElement ) ;
192 xTemplate->setTarget( xTarElement ) ;
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\"" ) ;
202 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
203 OSL_ENSURE( xSecEnv.is() ,
204 "Encryptor - "
205 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
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\"" ) ;
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\"" ) ;
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 ) ;
226 pSecEnv->adoptSymKey( symKey ) ;
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\"" ) ;
236 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
237 OSL_ENSURE( xSecCtx.is() ,
238 "Encryptor - "
239 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
241 xSecCtx->addSecurityEnvironment( xSecEnv ) ;
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\"" ) ;
250 Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
251 OSL_ENSURE( xEncrypter.is() ,
252 "Encryptor - "
253 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
255 //perform encryption
256 xTemplate = xEncrypter->encrypt( xTemplate , xSecEnv ) ;
257 OSL_ENSURE( xTemplate.is() ,
258 "Encryptor - "
259 "Cannot encrypt the xml document" ) ;
262 com::sun::star::xml::crypto::SecurityOperationStatus m_nStatus = xTemplate->getStatus();
263 if (m_nStatus == SecurityOperationStatus_OPERATION_SUCCEEDED)
265 fprintf( stdout, "Operation succeeds.\n") ;
267 else
269 fprintf( stdout, "Operation fails.\n") ;
271 } catch( Exception& e ) {
272 fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
273 goto done ;
276 dstFile = fopen( argv[2], "w" ) ;
277 if( dstFile == NULL ) {
278 fprintf( stderr , "### Can not open file %s\n", argv[2] ) ;
279 goto done ;
282 //Save result
283 xmlDocDump( dstFile, doc ) ;
285 done:
286 if( dstFile != NULL )
287 fclose( dstFile ) ;
289 if( symKey != NULL ) {
290 CryptDestroyKey( symKey ) ;
293 if( hCryptProv != NULL ) {
294 CryptReleaseContext( hCryptProv, 0 ) ;
297 if( n_hStoreHandle != NULL )
298 CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
300 /* Shutdown libxslt/libxml */
301 #ifndef XMLSEC_NO_XSLT
302 xsltCleanupGlobals();
303 #endif /* XMLSEC_NO_XSLT */
304 xmlCleanupParser();
306 return 0;