merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / standalone / csfit / decrypter.cxx
blob9c2422c19bb586fd383eb7556609290f0a9b26a0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include <stdio.h>
32 #include "helper.hxx"
34 #include "libxml/tree.h"
35 #include "libxml/parser.h"
36 #ifndef XMLSEC_NO_XSLT
37 #include "libxslt/xslt.h"
38 #endif
41 #include "securityenvironment_nssimpl.hxx"
42 #include "xmlelementwrapper_xmlsecimpl.hxx"
44 #include "nspr.h"
45 #include "prtypes.h"
47 #include "pk11func.h"
48 #include "cert.h"
49 #include "cryptohi.h"
50 #include "certdb.h"
51 #include "nss.h"
53 #include "xmlsec/strings.h"
54 #include "xmlsec/xmltree.h"
56 #include <rtl/ustring.hxx>
57 #include <cppuhelper/bootstrap.hxx>
58 #include <cppuhelper/servicefactory.hxx>
60 #include <com/sun/star/beans/PropertyValue.hpp>
61 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
62 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
63 #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
64 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
65 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
66 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
69 using namespace ::rtl ;
70 using namespace ::cppu ;
71 using namespace ::com::sun::star::uno ;
72 using namespace ::com::sun::star::io ;
73 using namespace ::com::sun::star::ucb ;
74 using namespace ::com::sun::star::beans ;
75 using namespace ::com::sun::star::document ;
76 using namespace ::com::sun::star::lang ;
77 using namespace ::com::sun::star::registry ;
78 using namespace ::com::sun::star::xml::wrapper ;
79 using namespace ::com::sun::star::xml::crypto ;
82 int SAL_CALL main( int argc, char **argv )
84 CERTCertDBHandle* certHandle = NULL ;
85 PK11SlotInfo* slot = NULL ;
86 xmlDocPtr doc = NULL ;
87 xmlNodePtr tplNode ;
88 xmlNodePtr tarNode ;
89 FILE* dstFile = NULL ;
92 if( argc != 5 ) {
93 fprintf( stderr, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv[0] ) ;
94 return 1 ;
97 //Init libxml and libxslt libraries
98 xmlInitParser();
99 LIBXML_TEST_VERSION
100 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
101 xmlSubstituteEntitiesDefault(1);
103 #ifndef XMLSEC_NO_XSLT
104 xmlIndentTreeOutput = 1;
105 #endif // XMLSEC_NO_XSLT
108 //Initialize NSPR and NSS
109 PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
110 PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
111 if( NSS_Init( argv[1] ) != SECSuccess ) {
112 fprintf( stderr , "### cannot intialize NSS!\n" ) ;
113 goto done ;
116 certHandle = CERT_GetDefaultCertDB() ;
117 slot = PK11_GetInternalKeySlot() ;
119 //Load XML document
120 doc = xmlParseFile( argv[2] ) ;
121 if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
122 fprintf( stderr , "### Cannot load template xml document!\n" ) ;
123 goto done ;
126 //Find the encryption template
127 tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
128 if( tplNode == NULL ) {
129 fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
130 goto done ;
134 try {
135 Reference< XMultiComponentFactory > xManager = NULL ;
136 Reference< XComponentContext > xContext = NULL ;
138 xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ;
140 //Create encryption template
141 Reference< XInterface > tplElement =
142 xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
143 OSL_ENSURE( tplElement.is() ,
144 "Decryptor - "
145 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
147 Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
148 OSL_ENSURE( xTplElement.is() ,
149 "Decryptor - "
150 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
152 Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
153 OSL_ENSURE( xTplEleTunnel.is() ,
154 "Decryptor - "
155 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
157 XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
158 OSL_ENSURE( pTplElement != NULL ,
159 "Decryptor - "
160 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
162 pTplElement->setNativeElement( tplNode ) ;
164 //Build XML Encryption template
165 Reference< XInterface > enctpl =
166 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext ) ;
167 OSL_ENSURE( enctpl.is() ,
168 "Decryptor - "
169 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
171 Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
172 OSL_ENSURE( xTemplate.is() ,
173 "Decryptor - "
174 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
176 //Import the encryption template
177 xTemplate->setTemplate( xTplElement ) ;
179 //Create security environment
180 //Build Security Environment
181 Reference< XInterface > xsecenv =
182 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ;
183 OSL_ENSURE( xsecenv.is() ,
184 "Decryptor - "
185 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
187 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
188 OSL_ENSURE( xSecEnv.is() ,
189 "Decryptor - "
190 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
192 //Setup key slot and certDb
193 Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
194 OSL_ENSURE( xEnvTunnel.is() ,
195 "Decryptor - "
196 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
198 SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
199 OSL_ENSURE( pSecEnv != NULL ,
200 "Decryptor - "
201 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
203 pSecEnv->setCryptoSlot( slot ) ;
204 pSecEnv->setCertDb( certHandle ) ;
207 //Build XML Security Context
208 Reference< XInterface > xmlsecctx =
209 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ;
210 OSL_ENSURE( xmlsecctx.is() ,
211 "Decryptor - "
212 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
214 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
215 OSL_ENSURE( xSecCtx.is() ,
216 "Decryptor - "
217 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
219 xSecCtx->setSecurityEnvironment( xSecEnv ) ;
222 //Get encrypter
223 Reference< XInterface > xmlencrypter =
224 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl"), xContext ) ;
225 OSL_ENSURE( xmlencrypter.is() ,
226 "Decryptor - "
227 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
229 Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
230 OSL_ENSURE( xEncrypter.is() ,
231 "Decryptor - "
232 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
235 //Perform decryption
236 Reference< XXMLElementWrapper> xDecrRes = xEncrypter->decrypt( xTemplate , xSecCtx ) ;
237 OSL_ENSURE( xDecrRes.is() ,
238 "Decryptor - "
239 "Cannot decrypt the xml document" ) ;
240 } catch( Exception& e ) {
241 fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
242 goto done ;
245 dstFile = fopen( argv[3], "w" ) ;
246 if( dstFile == NULL ) {
247 fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
248 goto done ;
251 //Save result
252 xmlDocDump( dstFile, doc ) ;
254 done:
255 if( dstFile != NULL )
256 fclose( dstFile ) ;
258 if( slot != NULL )
259 PK11_FreeSlot( slot ) ;
261 PK11_LogoutAll() ;
262 NSS_Shutdown() ;
264 /* Shutdown libxslt/libxml */
265 #ifndef XMLSEC_NO_XSLT
266 xsltCleanupGlobals();
267 #endif /* XMLSEC_NO_XSLT */
268 xmlCleanupParser();
270 return 0;