merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / standalone / mscsfit / decrypter.cxx
blobc0864ffb7b2eed48ffd6ed0f6e3747ad83dbe355
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
15 #include "securityenvironment_mscryptimpl.hxx"
16 #include "xmlelementwrapper_xmlsecimpl.hxx"
18 #include "nspr.h"
19 #include "prtypes.h"
21 #include "pk11func.h"
22 #include "cert.h"
23 #include "cryptohi.h"
24 #include "certdb.h"
25 #include "nss.h"
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 ;
61 xmlNodePtr tplNode ;
62 xmlNodePtr tarNode ;
63 FILE* dstFile = NULL ;
66 if( argc != 5 ) {
67 fprintf( stderr, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv[0] ) ;
68 return 1 ;
71 //Init libxml and libxslt libraries
72 xmlInitParser();
73 LIBXML_TEST_VERSION
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" ) ;
87 goto done ;
90 certHandle = CERT_GetDefaultCertDB() ;
91 slot = PK11_GetInternalKeySlot() ;
93 //Load XML document
94 doc = xmlParseFile( argv[2] ) ;
95 if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
96 fprintf( stderr , "### Cannot load template xml document!\n" ) ;
97 goto done ;
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" ) ;
104 goto done ;
108 try {
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() ,
118 "Decryptor - "
119 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
121 Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
122 OSL_ENSURE( xTplElement.is() ,
123 "Decryptor - "
124 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
126 Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
127 OSL_ENSURE( xTplEleTunnel.is() ,
128 "Decryptor - "
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 ,
133 "Decryptor - "
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() ,
142 "Decryptor - "
143 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
145 Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
146 OSL_ENSURE( xTemplate.is() ,
147 "Decryptor - "
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() ,
158 "Decryptor - "
159 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
161 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
162 OSL_ENSURE( xSecEnv.is() ,
163 "Decryptor - "
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() ,
169 "Decryptor - "
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 ,
174 "Decryptor - "
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() ,
185 "Decryptor - "
186 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
188 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
189 OSL_ENSURE( xSecCtx.is() ,
190 "Decryptor - "
191 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
193 xSecCtx->setSecurityEnvironment( xSecEnv ) ;
196 //Get encrypter
197 Reference< XInterface > xmlencrypter =
198 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext ) ;
199 OSL_ENSURE( xmlencrypter.is() ,
200 "Decryptor - "
201 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
203 Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
204 OSL_ENSURE( xEncrypter.is() ,
205 "Decryptor - "
206 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
209 //Perform decryption
210 Reference< XXMLElementWrapper> xDecrRes = xEncrypter->decrypt( xTemplate , xSecCtx ) ;
211 OSL_ENSURE( xDecrRes.is() ,
212 "Decryptor - "
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() ) ;
216 goto done ;
219 dstFile = fopen( argv[3], "w" ) ;
220 if( dstFile == NULL ) {
221 fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
222 goto done ;
225 //Save result
226 xmlDocDump( dstFile, doc ) ;
228 done:
229 if( dstFile != NULL )
230 fclose( dstFile ) ;
232 if( slot != NULL )
233 PK11_FreeSlot( slot ) ;
235 PK11_LogoutAll() ;
236 NSS_Shutdown() ;
238 /* Shutdown libxslt/libxml */
239 #ifndef XMLSEC_NO_XSLT
240 xsltCleanupGlobals();
241 #endif /* XMLSEC_NO_XSLT */
242 xmlCleanupParser();
244 return 0;