1 /** -- C++ Source File -- **/
3 // MARKER(update_precomp.py): autogen include statement, do not remove
4 #include "precompiled_xmlsecurity.hxx"
8 #include "libxml/tree.h"
9 #include "libxml/parser.h"
10 #ifndef XMLSEC_NO_XSLT
11 #include "libxslt/xslt.h"
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
;
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] ) ;
64 //Init libxml and libxslt libraries
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
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
) ;
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" );
97 if( !CryptGenKey( hCryptProv
, CALG_RC4
, 0x00800000 | CRYPT_EXPORTABLE
, &symKey
) ) {
98 fprintf( stderr
, "### cannot create symmetric key!\n" ) ;
103 doc
= xmlParseFile( argv
[1] ) ;
104 if( doc
== NULL
|| xmlDocGetRootElement( doc
) == NULL
) {
105 fprintf( stderr
, "### Cannot load template xml document!\n" ) ;
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" ) ;
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" ) ;
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() ,
134 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
136 Reference
< XXMLElementWrapper
> xTplElement( tplElement
, UNO_QUERY
) ;
137 OSL_ENSURE( xTplElement
.is() ,
139 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
141 Reference
< XUnoTunnel
> xTplEleTunnel( xTplElement
, UNO_QUERY
) ;
142 OSL_ENSURE( xTplEleTunnel
.is() ,
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
,
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() ,
158 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
160 Reference
< XXMLElementWrapper
> xTarElement( tarElement
, UNO_QUERY
) ;
161 OSL_ENSURE( xTarElement
.is() ,
163 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
165 Reference
< XUnoTunnel
> xTarEleTunnel( xTarElement
, UNO_QUERY
) ;
166 OSL_ENSURE( xTarEleTunnel
.is() ,
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
,
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() ,
183 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
185 Reference
< XXMLEncryptionTemplate
> xTemplate( enctpl
, UNO_QUERY
) ;
186 OSL_ENSURE( xTemplate
.is() ,
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() ,
200 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
202 Reference
< XSecurityEnvironment
> xSecEnv( xsecenv
, UNO_QUERY
) ;
203 OSL_ENSURE( xSecEnv
.is() ,
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() ,
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
,
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
) ;
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() ,
234 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
236 Reference
< XXMLSecurityContext
> xSecCtx( xmlsecctx
, UNO_QUERY
) ;
237 OSL_ENSURE( xSecCtx
.is() ,
239 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
241 xSecCtx
->addSecurityEnvironment( xSecEnv
) ;
244 Reference
< XInterface
> xmlencrypter
=
245 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_MSCryptImpl"), xContext
) ;
246 OSL_ENSURE( xmlencrypter
.is() ,
248 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
250 Reference
< XXMLEncryption
> xEncrypter( xmlencrypter
, UNO_QUERY
) ;
251 OSL_ENSURE( xEncrypter
.is() ,
253 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
256 xTemplate
= xEncrypter
->encrypt( xTemplate
, xSecEnv
) ;
257 OSL_ENSURE( xTemplate
.is() ,
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") ;
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() ) ;
276 dstFile
= fopen( argv
[2], "w" ) ;
277 if( dstFile
== NULL
) {
278 fprintf( stderr
, "### Can not open file %s\n", argv
[2] ) ;
283 xmlDocDump( dstFile
, doc
) ;
286 if( dstFile
!= NULL
)
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 */