1*06b3ce53SAndrew Rist /**************************************************************
2*06b3ce53SAndrew Rist  *
3*06b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*06b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*06b3ce53SAndrew Rist  * distributed with this work for additional information
6*06b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*06b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*06b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
9*06b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*06b3ce53SAndrew Rist  *
11*06b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*06b3ce53SAndrew Rist  *
13*06b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*06b3ce53SAndrew Rist  * software distributed under the License is distributed on an
15*06b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*06b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
17*06b3ce53SAndrew Rist  * specific language governing permissions and limitations
18*06b3ce53SAndrew Rist  * under the License.
19*06b3ce53SAndrew Rist  *
20*06b3ce53SAndrew Rist  *************************************************************/
21*06b3ce53SAndrew Rist 
22cdf0e10cSrcweir /** -- C++ Source File -- **/
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir #include <stdio.h>
27cdf0e10cSrcweir #include "helper.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "libxml/tree.h"
30cdf0e10cSrcweir #include "libxml/parser.h"
31cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT
32cdf0e10cSrcweir #include "libxslt/xslt.h"
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "securityenvironment_mscryptimpl.hxx"
37cdf0e10cSrcweir #include "xmlelementwrapper_xmlsecimpl.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "nspr.h"
40cdf0e10cSrcweir #include "prtypes.h"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include "pk11func.h"
43cdf0e10cSrcweir #include "cert.h"
44cdf0e10cSrcweir #include "cryptohi.h"
45cdf0e10cSrcweir #include "certdb.h"
46cdf0e10cSrcweir #include "nss.h"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include "xmlsec/strings.h"
49cdf0e10cSrcweir #include "xmlsec/xmltree.h"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include <rtl/ustring.hxx>
52cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
53cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
56cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
57cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
58cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
59cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
60cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
61cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
64cdf0e10cSrcweir using namespace ::rtl ;
65cdf0e10cSrcweir using namespace ::cppu ;
66cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
67cdf0e10cSrcweir using namespace ::com::sun::star::io ;
68cdf0e10cSrcweir using namespace ::com::sun::star::ucb ;
69cdf0e10cSrcweir using namespace ::com::sun::star::beans ;
70cdf0e10cSrcweir using namespace ::com::sun::star::document ;
71cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
72cdf0e10cSrcweir using namespace ::com::sun::star::registry ;
73cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ;
74cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto ;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
main(int argc,char ** argv)77cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	CERTCertDBHandle*	certHandle = NULL ;
80cdf0e10cSrcweir 	PK11SlotInfo*		slot = NULL ;
81cdf0e10cSrcweir 	xmlDocPtr			doc = NULL ;
82cdf0e10cSrcweir 	xmlNodePtr			tplNode ;
83cdf0e10cSrcweir 	xmlNodePtr			tarNode ;
84cdf0e10cSrcweir 	FILE*				dstFile = NULL ;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	if( argc != 5 ) {
88cdf0e10cSrcweir 		fprintf( stderr, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv[0] ) ;
89cdf0e10cSrcweir 		return 1 ;
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	//Init libxml and libxslt libraries
93cdf0e10cSrcweir 	xmlInitParser();
94cdf0e10cSrcweir 	LIBXML_TEST_VERSION
95cdf0e10cSrcweir 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
96cdf0e10cSrcweir 	xmlSubstituteEntitiesDefault(1);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
99cdf0e10cSrcweir 	xmlIndentTreeOutput = 1;
100cdf0e10cSrcweir 	#endif // XMLSEC_NO_XSLT
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	//Initialize NSPR and NSS
104cdf0e10cSrcweir 	PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
105cdf0e10cSrcweir 	PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
106cdf0e10cSrcweir 	if( NSS_Init( argv[1] ) != SECSuccess ) {
107cdf0e10cSrcweir 		fprintf( stderr , "### cannot intialize NSS!\n" ) ;
108cdf0e10cSrcweir 		goto done ;
109cdf0e10cSrcweir 	}
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	certHandle = CERT_GetDefaultCertDB() ;
112cdf0e10cSrcweir 	slot = PK11_GetInternalKeySlot() ;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	//Load XML document
115cdf0e10cSrcweir 	doc = xmlParseFile( argv[2] ) ;
116cdf0e10cSrcweir 	if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
117cdf0e10cSrcweir 		fprintf( stderr , "### Cannot load template xml document!\n" ) ;
118cdf0e10cSrcweir 		goto done ;
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	//Find the encryption template
122cdf0e10cSrcweir 	tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
123cdf0e10cSrcweir 	if( tplNode == NULL ) {
124cdf0e10cSrcweir 		fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
125cdf0e10cSrcweir 		goto done ;
126cdf0e10cSrcweir 	}
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	try {
130cdf0e10cSrcweir 		Reference< XMultiComponentFactory > xManager = NULL ;
131cdf0e10cSrcweir 		Reference< XComponentContext > xContext = NULL ;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 		//Create encryption template
136cdf0e10cSrcweir 		Reference< XInterface > tplElement =
137cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext ) ;
138cdf0e10cSrcweir 		OSL_ENSURE( tplElement.is() ,
139cdf0e10cSrcweir 			"Decryptor - "
140cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 		Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
143cdf0e10cSrcweir 		OSL_ENSURE( xTplElement.is() ,
144cdf0e10cSrcweir 			"Decryptor - "
145cdf0e10cSrcweir 			"Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 		Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
148cdf0e10cSrcweir 		OSL_ENSURE( xTplEleTunnel.is() ,
149cdf0e10cSrcweir 			"Decryptor - "
150cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 		XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
153cdf0e10cSrcweir 		OSL_ENSURE( pTplElement != NULL ,
154cdf0e10cSrcweir 			"Decryptor - "
155cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 		pTplElement->setNativeElement( tplNode ) ;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 		//Build XML Encryption template
160cdf0e10cSrcweir 		Reference< XInterface > enctpl =
161cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryptionTemplate"), xContext ) ;
162cdf0e10cSrcweir 		OSL_ENSURE( enctpl.is() ,
163cdf0e10cSrcweir 			"Decryptor - "
164cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 		Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
167cdf0e10cSrcweir 		OSL_ENSURE( xTemplate.is() ,
168cdf0e10cSrcweir 			"Decryptor - "
169cdf0e10cSrcweir 			"Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 		//Import the encryption template
172cdf0e10cSrcweir 		xTemplate->setTemplate( xTplElement ) ;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		//Create security environment
175cdf0e10cSrcweir 		//Build Security Environment
176cdf0e10cSrcweir 		Reference< XInterface > xsecenv =
177cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.SecurityEnvironment"), xContext ) ;
178cdf0e10cSrcweir 		OSL_ENSURE( xsecenv.is() ,
179cdf0e10cSrcweir 			"Decryptor - "
180cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 		Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
183cdf0e10cSrcweir 		OSL_ENSURE( xSecEnv.is() ,
184cdf0e10cSrcweir 			"Decryptor - "
185cdf0e10cSrcweir 			"Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 		//Setup key slot and certDb
188cdf0e10cSrcweir 		Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
189cdf0e10cSrcweir 		OSL_ENSURE( xEnvTunnel.is() ,
190cdf0e10cSrcweir 			"Decryptor - "
191cdf0e10cSrcweir 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 		SecurityEnvironment_XmlSecImpl* pSecEnv = ( SecurityEnvironment_XmlSecImpl* )xEnvTunnel->getSomething( SecurityEnvironment_XmlSecImpl::getUnoTunnelId() ) ;
194cdf0e10cSrcweir 		OSL_ENSURE( pSecEnv != NULL ,
195cdf0e10cSrcweir 			"Decryptor - "
196cdf0e10cSrcweir 			"Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 		pSecEnv->setCryptoSlot( slot ) ;
199cdf0e10cSrcweir 		pSecEnv->setCertDb( certHandle ) ;
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 		//Build XML Security Context
203cdf0e10cSrcweir 		Reference< XInterface > xmlsecctx =
204cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLSecurityContext"), xContext ) ;
205cdf0e10cSrcweir 		OSL_ENSURE( xmlsecctx.is() ,
206cdf0e10cSrcweir 			"Decryptor - "
207cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 		Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
210cdf0e10cSrcweir 		OSL_ENSURE( xSecCtx.is() ,
211cdf0e10cSrcweir 			"Decryptor - "
212cdf0e10cSrcweir 			"Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 		xSecCtx->setSecurityEnvironment( xSecEnv ) ;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 		//Get encrypter
218cdf0e10cSrcweir 		Reference< XInterface > xmlencrypter =
219cdf0e10cSrcweir 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext ) ;
220cdf0e10cSrcweir 		OSL_ENSURE( xmlencrypter.is() ,
221cdf0e10cSrcweir 			"Decryptor - "
222cdf0e10cSrcweir 			"Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 		Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
225cdf0e10cSrcweir 		OSL_ENSURE( xEncrypter.is() ,
226cdf0e10cSrcweir 			"Decryptor - "
227cdf0e10cSrcweir 			"Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 		//Perform decryption
231cdf0e10cSrcweir 		Reference< XXMLElementWrapper> xDecrRes = xEncrypter->decrypt( xTemplate , xSecCtx ) ;
232cdf0e10cSrcweir 		OSL_ENSURE( xDecrRes.is() ,
233cdf0e10cSrcweir 			"Decryptor - "
234cdf0e10cSrcweir 			"Cannot decrypt the xml document" ) ;
235cdf0e10cSrcweir 	} catch( Exception& e ) {
236cdf0e10cSrcweir 		fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
237cdf0e10cSrcweir 		goto done ;
238cdf0e10cSrcweir 	}
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 	dstFile = fopen( argv[3], "w" ) ;
241cdf0e10cSrcweir 	if( dstFile == NULL ) {
242cdf0e10cSrcweir 		fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
243cdf0e10cSrcweir 		goto done ;
244cdf0e10cSrcweir 	}
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	//Save result
247cdf0e10cSrcweir 	xmlDocDump( dstFile, doc ) ;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir done:
250cdf0e10cSrcweir 	if( dstFile != NULL )
251cdf0e10cSrcweir 		fclose( dstFile ) ;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 	if( slot != NULL )
254cdf0e10cSrcweir 		PK11_FreeSlot( slot ) ;
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 	PK11_LogoutAll() ;
257cdf0e10cSrcweir 	NSS_Shutdown() ;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	/* Shutdown libxslt/libxml */
260cdf0e10cSrcweir 	#ifndef XMLSEC_NO_XSLT
261cdf0e10cSrcweir 	xsltCleanupGlobals();
262cdf0e10cSrcweir 	#endif /* XMLSEC_NO_XSLT */
263cdf0e10cSrcweir 	xmlCleanupParser();
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 	return 0;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268