merge the formfield patch from ooo-build
[ooovba.git] / xmlsecurity / tools / standalone / csfit / encrypter.cxx
blob279144d19aacd25c5fb678f96c13e19ce3fe248d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: encrypter.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <stdio.h>
35 #include "helper.hxx"
37 #include "libxml/tree.h"
38 #include "libxml/parser.h"
39 #ifndef XMLSEC_NO_XSLT
40 #include "libxslt/xslt.h"
41 #endif
43 #include "securityenvironment_nssimpl.hxx"
44 #include "xmlelementwrapper_xmlsecimpl.hxx"
46 #include "nspr.h"
47 #include "prtypes.h"
49 #include "pk11func.h"
50 #include "cert.h"
51 #include "cryptohi.h"
52 #include "certdb.h"
53 #include "nss.h"
55 #include "xmlsec/strings.h"
56 #include "xmlsec/xmltree.h"
58 #include <rtl/ustring.hxx>
59 #include <cppuhelper/servicefactory.hxx>
61 #include <com/sun/star/lang/XComponent.hpp>
62 #include <com/sun/star/beans/PropertyValue.hpp>
63 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
64 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
65 #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
66 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
67 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
68 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
70 using namespace ::rtl ;
71 using namespace ::cppu ;
72 using namespace ::com::sun::star::uno ;
73 using namespace ::com::sun::star::io ;
74 using namespace ::com::sun::star::ucb ;
75 using namespace ::com::sun::star::beans ;
76 using namespace ::com::sun::star::document ;
77 using namespace ::com::sun::star::lang ;
78 using namespace ::com::sun::star::registry ;
79 using namespace ::com::sun::star::xml::wrapper ;
80 using namespace ::com::sun::star::xml::crypto ;
82 int SAL_CALL main( int argc, char **argv )
84 CERTCertDBHandle* certHandle ;
85 PK11SlotInfo* slot = NULL ;
86 PK11SymKey* symKey = NULL ;
87 xmlDocPtr doc = NULL ;
88 xmlNodePtr tplNode ;
89 xmlNodePtr tarNode ;
90 FILE* dstFile = NULL ;
92 if( argc != 7 ) {
93 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] ) ;
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
107 //Initialize NSPR and NSS
108 PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
109 PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
110 if( NSS_Init( argv[1] ) != SECSuccess ) {
111 fprintf( stderr , "### cannot intialize NSS!\n" ) ;
112 goto done ;
115 certHandle = CERT_GetDefaultCertDB() ;
116 slot = PK11_GetInternalKeySlot() ;
118 symKey = PK11_KeyGen( slot , CKM_DES3_CBC, NULL, 128, NULL ) ;
119 if( symKey == NULL ) {
120 fprintf( stderr , "### cannot create symmetric key!\n" ) ;
121 goto done ;
124 //Load XML document
125 doc = xmlParseFile( argv[2] ) ;
126 if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
127 fprintf( stderr , "### Cannot load template xml document!\n" ) ;
128 goto done ;
131 //Find the encryption template
132 tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
133 if( tplNode == NULL ) {
134 fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
135 goto done ;
138 //Find the encryption template
139 tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( const unsigned char*)argv[4], ( const unsigned char*)argv[5] ) ;
140 if( tarNode == NULL ) {
141 fprintf( stderr , "### Cannot find the encryption target!\n" ) ;
142 goto done ;
145 try {
146 Reference< XMultiComponentFactory > xManager = NULL ;
147 Reference< XComponentContext > xContext = NULL ;
149 xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[6] ) ) ;
151 //Create encryption template
152 Reference< XInterface > tplElement =
153 xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
154 OSL_ENSURE( tplElement.is() ,
155 "Encryptor - "
156 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
158 Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
159 OSL_ENSURE( xTplElement.is() ,
160 "Encryptor - "
161 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
163 Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
164 OSL_ENSURE( xTplEleTunnel.is() ,
165 "Encryptor - "
166 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
168 XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
169 OSL_ENSURE( pTplElement != NULL ,
170 "Encryptor - "
171 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
173 pTplElement->setNativeElement( tplNode ) ;
175 //Create encryption target element
176 Reference< XInterface > tarElement =
177 xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
178 OSL_ENSURE( tarElement.is() ,
179 "Encryptor - "
180 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
182 Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ;
183 OSL_ENSURE( xTarElement.is() ,
184 "Encryptor - "
185 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
187 Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ;
188 OSL_ENSURE( xTarEleTunnel.is() ,
189 "Encryptor - "
190 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
192 XMLElementWrapper_XmlSecImpl* pTarElement = ( XMLElementWrapper_XmlSecImpl* )xTarEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
193 OSL_ENSURE( pTarElement != NULL ,
194 "Encryptor - "
195 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
197 pTarElement->setNativeElement( tarNode ) ;
200 //Build XML Encryption template
201 Reference< XInterface > enctpl =
202 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext ) ;
203 OSL_ENSURE( enctpl.is() ,
204 "Encryptor - "
205 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
207 Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
208 OSL_ENSURE( xTemplate.is() ,
209 "Encryptor - "
210 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
212 //Import the encryption template
213 xTemplate->setTemplate( xTplElement ) ;
214 xTemplate->setTarget( xTarElement ) ;
216 //Create security environment
217 //Build Security Environment
218 Reference< XInterface > xsecenv =
219 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ;
220 OSL_ENSURE( xsecenv.is() ,
221 "Encryptor - "
222 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
224 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
225 OSL_ENSURE( xSecEnv.is() ,
226 "Encryptor - "
227 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
229 //Setup key slot and certDb
230 Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
231 OSL_ENSURE( xEnvTunnel.is() ,
232 "Encryptor - "
233 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
235 SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
236 OSL_ENSURE( pSecEnv != NULL ,
237 "Encryptor - "
238 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
240 pSecEnv->setCryptoSlot( slot ) ;
241 pSecEnv->setCertDb( certHandle ) ;
242 pSecEnv->adoptSymKey( symKey ) ;
245 //Build XML Security Context
246 Reference< XInterface > xmlsecctx =
247 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ;
248 OSL_ENSURE( xmlsecctx.is() ,
249 "Encryptor - "
250 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
252 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
253 OSL_ENSURE( xSecCtx.is() ,
254 "Encryptor - "
255 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
257 xSecCtx->setSecurityEnvironment( xSecEnv ) ;
259 //Get encrypter
260 Reference< XInterface > xmlencrypter =
261 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl"), xContext ) ;
262 OSL_ENSURE( xmlencrypter.is() ,
263 "Encryptor - "
264 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
266 Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
267 OSL_ENSURE( xEncrypter.is() ,
268 "Encryptor - "
269 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
271 //perform encryption
272 xTemplate = xEncrypter->encrypt( xTemplate , xSecCtx ) ;
273 OSL_ENSURE( xTemplate.is() ,
274 "Encryptor - "
275 "Cannot encrypt the xml document" ) ;
276 } catch( Exception& e ) {
277 fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
278 goto done ;
281 dstFile = fopen( argv[3], "w" ) ;
282 if( dstFile == NULL ) {
283 fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
284 goto done ;
287 //Save result
288 xmlDocDump( dstFile, doc ) ;
290 done:
291 if( dstFile != NULL )
292 fclose( dstFile ) ;
294 if( symKey != NULL ) {
295 PK11_FreeSymKey( symKey ) ;
298 if( slot != NULL )
299 PK11_FreeSlot( slot ) ;
301 PK11_LogoutAll() ;
302 NSS_Shutdown() ;
304 /* Shutdown libxslt/libxml */
305 #ifndef XMLSEC_NO_XSLT
306 xsltCleanupGlobals();
307 #endif /* XMLSEC_NO_XSLT */
308 xmlCleanupParser();
310 return 0;