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