merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / standalone / csfit / encrypter.cxx
blob37821b6ed54eb3f7e0de3fac876a18c859bfbc11
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
40 #include "securityenvironment_nssimpl.hxx"
41 #include "xmlelementwrapper_xmlsecimpl.hxx"
43 #include "nspr.h"
44 #include "prtypes.h"
46 #include "pk11func.h"
47 #include "cert.h"
48 #include "cryptohi.h"
49 #include "certdb.h"
50 #include "nss.h"
52 #include "xmlsec/strings.h"
53 #include "xmlsec/xmltree.h"
55 #include <rtl/ustring.hxx>
56 #include <cppuhelper/servicefactory.hxx>
58 #include <com/sun/star/lang/XComponent.hpp>
59 #include <com/sun/star/beans/PropertyValue.hpp>
60 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
61 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
62 #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
63 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
64 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
65 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
67 using namespace ::rtl ;
68 using namespace ::cppu ;
69 using namespace ::com::sun::star::uno ;
70 using namespace ::com::sun::star::io ;
71 using namespace ::com::sun::star::ucb ;
72 using namespace ::com::sun::star::beans ;
73 using namespace ::com::sun::star::document ;
74 using namespace ::com::sun::star::lang ;
75 using namespace ::com::sun::star::registry ;
76 using namespace ::com::sun::star::xml::wrapper ;
77 using namespace ::com::sun::star::xml::crypto ;
79 int SAL_CALL main( int argc, char **argv )
81 CERTCertDBHandle* certHandle ;
82 PK11SlotInfo* slot = NULL ;
83 PK11SymKey* symKey = NULL ;
84 xmlDocPtr doc = NULL ;
85 xmlNodePtr tplNode ;
86 xmlNodePtr tarNode ;
87 FILE* dstFile = NULL ;
89 if( argc != 7 ) {
90 fprintf( stderr, "Usage: %s < CertDir > <file_url of template> <file_url of result> <target element name> <target element namespace> <rdb file>\n\n" , argv[0] ) ;
91 return 1 ;
94 //Init libxml and libxslt libraries
95 xmlInitParser();
96 LIBXML_TEST_VERSION
97 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
98 xmlSubstituteEntitiesDefault(1);
100 #ifndef XMLSEC_NO_XSLT
101 xmlIndentTreeOutput = 1;
102 #endif // XMLSEC_NO_XSLT
104 //Initialize NSPR and NSS
105 PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
106 PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
107 if( NSS_Init( argv[1] ) != SECSuccess ) {
108 fprintf( stderr , "### cannot intialize NSS!\n" ) ;
109 goto done ;
112 certHandle = CERT_GetDefaultCertDB() ;
113 slot = PK11_GetInternalKeySlot() ;
115 symKey = PK11_KeyGen( slot , CKM_DES3_CBC, NULL, 128, NULL ) ;
116 if( symKey == NULL ) {
117 fprintf( stderr , "### cannot create symmetric key!\n" ) ;
118 goto done ;
121 //Load XML document
122 doc = xmlParseFile( argv[2] ) ;
123 if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
124 fprintf( stderr , "### Cannot load template xml document!\n" ) ;
125 goto done ;
128 //Find the encryption template
129 tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
130 if( tplNode == NULL ) {
131 fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
132 goto done ;
135 //Find the encryption template
136 tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( const unsigned char*)argv[4], ( const unsigned char*)argv[5] ) ;
137 if( tarNode == NULL ) {
138 fprintf( stderr , "### Cannot find the encryption target!\n" ) ;
139 goto done ;
142 try {
143 Reference< XMultiComponentFactory > xManager = NULL ;
144 Reference< XComponentContext > xContext = NULL ;
146 xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[6] ) ) ;
148 //Create encryption template
149 Reference< XInterface > tplElement =
150 xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
151 OSL_ENSURE( tplElement.is() ,
152 "Encryptor - "
153 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
155 Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
156 OSL_ENSURE( xTplElement.is() ,
157 "Encryptor - "
158 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
160 Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
161 OSL_ENSURE( xTplEleTunnel.is() ,
162 "Encryptor - "
163 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
165 XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
166 OSL_ENSURE( pTplElement != NULL ,
167 "Encryptor - "
168 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
170 pTplElement->setNativeElement( tplNode ) ;
172 //Create encryption target element
173 Reference< XInterface > tarElement =
174 xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
175 OSL_ENSURE( tarElement.is() ,
176 "Encryptor - "
177 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
179 Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ;
180 OSL_ENSURE( xTarElement.is() ,
181 "Encryptor - "
182 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
184 Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ;
185 OSL_ENSURE( xTarEleTunnel.is() ,
186 "Encryptor - "
187 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
189 XMLElementWrapper_XmlSecImpl* pTarElement = ( XMLElementWrapper_XmlSecImpl* )xTarEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
190 OSL_ENSURE( pTarElement != NULL ,
191 "Encryptor - "
192 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
194 pTarElement->setNativeElement( tarNode ) ;
197 //Build XML Encryption template
198 Reference< XInterface > enctpl =
199 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext ) ;
200 OSL_ENSURE( enctpl.is() ,
201 "Encryptor - "
202 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
204 Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
205 OSL_ENSURE( xTemplate.is() ,
206 "Encryptor - "
207 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
209 //Import the encryption template
210 xTemplate->setTemplate( xTplElement ) ;
211 xTemplate->setTarget( xTarElement ) ;
213 //Create security environment
214 //Build Security Environment
215 Reference< XInterface > xsecenv =
216 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ;
217 OSL_ENSURE( xsecenv.is() ,
218 "Encryptor - "
219 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
221 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
222 OSL_ENSURE( xSecEnv.is() ,
223 "Encryptor - "
224 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
226 //Setup key slot and certDb
227 Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
228 OSL_ENSURE( xEnvTunnel.is() ,
229 "Encryptor - "
230 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
232 SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
233 OSL_ENSURE( pSecEnv != NULL ,
234 "Encryptor - "
235 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
237 pSecEnv->setCryptoSlot( slot ) ;
238 pSecEnv->setCertDb( certHandle ) ;
239 pSecEnv->adoptSymKey( symKey ) ;
242 //Build XML Security Context
243 Reference< XInterface > xmlsecctx =
244 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ;
245 OSL_ENSURE( xmlsecctx.is() ,
246 "Encryptor - "
247 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
249 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
250 OSL_ENSURE( xSecCtx.is() ,
251 "Encryptor - "
252 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
254 xSecCtx->setSecurityEnvironment( xSecEnv ) ;
256 //Get encrypter
257 Reference< XInterface > xmlencrypter =
258 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl"), xContext ) ;
259 OSL_ENSURE( xmlencrypter.is() ,
260 "Encryptor - "
261 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
263 Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
264 OSL_ENSURE( xEncrypter.is() ,
265 "Encryptor - "
266 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
268 //perform encryption
269 xTemplate = xEncrypter->encrypt( xTemplate , xSecCtx ) ;
270 OSL_ENSURE( xTemplate.is() ,
271 "Encryptor - "
272 "Cannot encrypt the xml document" ) ;
273 } catch( Exception& e ) {
274 fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
275 goto done ;
278 dstFile = fopen( argv[3], "w" ) ;
279 if( dstFile == NULL ) {
280 fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
281 goto done ;
284 //Save result
285 xmlDocDump( dstFile, doc ) ;
287 done:
288 if( dstFile != NULL )
289 fclose( dstFile ) ;
291 if( symKey != NULL ) {
292 PK11_FreeSymKey( symKey ) ;
295 if( slot != NULL )
296 PK11_FreeSlot( slot ) ;
298 PK11_LogoutAll() ;
299 NSS_Shutdown() ;
301 /* Shutdown libxslt/libxml */
302 #ifndef XMLSEC_NO_XSLT
303 xsltCleanupGlobals();
304 #endif /* XMLSEC_NO_XSLT */
305 xmlCleanupParser();
307 return 0;