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"
15 #include "securityenvironment_mscryptimpl.hxx"
16 #include "xmlelementwrapper_xmlsecimpl.hxx"
27 #include "xmlsec/strings.h"
28 #include "xmlsec/xmltree.h"
30 #include <rtl/ustring.hxx>
31 #include <cppuhelper/bootstrap.hxx>
32 #include <cppuhelper/servicefactory.hxx>
34 #include <com/sun/star/beans/PropertyValue.hpp>
35 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
36 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
37 #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
38 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
39 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
40 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
43 using namespace ::rtl
;
44 using namespace ::cppu
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::io
;
47 using namespace ::com::sun::star::ucb
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::document
;
50 using namespace ::com::sun::star::lang
;
51 using namespace ::com::sun::star::registry
;
52 using namespace ::com::sun::star::xml::wrapper
;
53 using namespace ::com::sun::star::xml::crypto
;
56 int SAL_CALL
main( int argc
, char **argv
)
58 CERTCertDBHandle
* certHandle
= NULL
;
59 PK11SlotInfo
* slot
= NULL
;
60 xmlDocPtr doc
= NULL
;
63 FILE* dstFile
= NULL
;
67 fprintf( stderr
, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv
[0] ) ;
71 //Init libxml and libxslt libraries
74 xmlLoadExtDtdDefaultValue
= XML_DETECT_IDS
| XML_COMPLETE_ATTRS
;
75 xmlSubstituteEntitiesDefault(1);
77 #ifndef XMLSEC_NO_XSLT
78 xmlIndentTreeOutput
= 1;
79 #endif // XMLSEC_NO_XSLT
82 //Initialize NSPR and NSS
83 PR_Init( PR_SYSTEM_THREAD
, PR_PRIORITY_NORMAL
, 1 ) ;
84 PK11_SetPasswordFunc( PriPK11PasswordFunc
) ;
85 if( NSS_Init( argv
[1] ) != SECSuccess
) {
86 fprintf( stderr
, "### cannot intialize NSS!\n" ) ;
90 certHandle
= CERT_GetDefaultCertDB() ;
91 slot
= PK11_GetInternalKeySlot() ;
94 doc
= xmlParseFile( argv
[2] ) ;
95 if( doc
== NULL
|| xmlDocGetRootElement( doc
) == NULL
) {
96 fprintf( stderr
, "### Cannot load template xml document!\n" ) ;
100 //Find the encryption template
101 tplNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), xmlSecNodeEncryptedData
, xmlSecEncNs
) ;
102 if( tplNode
== NULL
) {
103 fprintf( stderr
, "### Cannot find the encryption template!\n" ) ;
109 Reference
< XMultiComponentFactory
> xManager
= NULL
;
110 Reference
< XComponentContext
> xContext
= NULL
;
112 xManager
= serviceManager( xContext
, OUString::createFromAscii( "local" ), OUString::createFromAscii( argv
[4] ) ) ;
114 //Create encryption template
115 Reference
< XInterface
> tplElement
=
116 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext
) ;
117 OSL_ENSURE( tplElement
.is() ,
119 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
121 Reference
< XXMLElementWrapper
> xTplElement( tplElement
, UNO_QUERY
) ;
122 OSL_ENSURE( xTplElement
.is() ,
124 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
126 Reference
< XUnoTunnel
> xTplEleTunnel( xTplElement
, UNO_QUERY
) ;
127 OSL_ENSURE( xTplEleTunnel
.is() ,
129 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
131 XMLElementWrapper_XmlSecImpl
* pTplElement
= ( XMLElementWrapper_XmlSecImpl
* )xTplEleTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
132 OSL_ENSURE( pTplElement
!= NULL
,
134 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
136 pTplElement
->setNativeElement( tplNode
) ;
138 //Build XML Encryption template
139 Reference
< XInterface
> enctpl
=
140 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryptionTemplate"), xContext
) ;
141 OSL_ENSURE( enctpl
.is() ,
143 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
145 Reference
< XXMLEncryptionTemplate
> xTemplate( enctpl
, UNO_QUERY
) ;
146 OSL_ENSURE( xTemplate
.is() ,
148 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
150 //Import the encryption template
151 xTemplate
->setTemplate( xTplElement
) ;
153 //Create security environment
154 //Build Security Environment
155 Reference
< XInterface
> xsecenv
=
156 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.SecurityEnvironment"), xContext
) ;
157 OSL_ENSURE( xsecenv
.is() ,
159 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
161 Reference
< XSecurityEnvironment
> xSecEnv( xsecenv
, UNO_QUERY
) ;
162 OSL_ENSURE( xSecEnv
.is() ,
164 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
166 //Setup key slot and certDb
167 Reference
< XUnoTunnel
> xEnvTunnel( xsecenv
, UNO_QUERY
) ;
168 OSL_ENSURE( xEnvTunnel
.is() ,
170 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
172 SecurityEnvironment_XmlSecImpl
* pSecEnv
= ( SecurityEnvironment_XmlSecImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_XmlSecImpl::getUnoTunnelId() ) ;
173 OSL_ENSURE( pSecEnv
!= NULL
,
175 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
177 pSecEnv
->setCryptoSlot( slot
) ;
178 pSecEnv
->setCertDb( certHandle
) ;
181 //Build XML Security Context
182 Reference
< XInterface
> xmlsecctx
=
183 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLSecurityContext"), xContext
) ;
184 OSL_ENSURE( xmlsecctx
.is() ,
186 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
188 Reference
< XXMLSecurityContext
> xSecCtx( xmlsecctx
, UNO_QUERY
) ;
189 OSL_ENSURE( xSecCtx
.is() ,
191 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
193 xSecCtx
->setSecurityEnvironment( xSecEnv
) ;
197 Reference
< XInterface
> xmlencrypter
=
198 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext
) ;
199 OSL_ENSURE( xmlencrypter
.is() ,
201 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
203 Reference
< XXMLEncryption
> xEncrypter( xmlencrypter
, UNO_QUERY
) ;
204 OSL_ENSURE( xEncrypter
.is() ,
206 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
210 Reference
< XXMLElementWrapper
> xDecrRes
= xEncrypter
->decrypt( xTemplate
, xSecCtx
) ;
211 OSL_ENSURE( xDecrRes
.is() ,
213 "Cannot decrypt the xml document" ) ;
214 } catch( Exception
& e
) {
215 fprintf( stderr
, "Error Message: %s\n" , OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() ) ;
219 dstFile
= fopen( argv
[3], "w" ) ;
220 if( dstFile
== NULL
) {
221 fprintf( stderr
, "### Can not open file %s\n", argv
[3] ) ;
226 xmlDocDump( dstFile
, doc
) ;
229 if( dstFile
!= NULL
)
233 PK11_FreeSlot( slot
) ;
238 /* Shutdown libxslt/libxml */
239 #ifndef XMLSEC_NO_XSLT
240 xsltCleanupGlobals();
241 #endif /* XMLSEC_NO_XSLT */