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