1*06b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
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
22*06b3ce53SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include "helper.hxx"
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include "libxml/tree.h"
31cdf0e10cSrcweir #include "libxml/parser.h"
32cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT
33cdf0e10cSrcweir #include "libxslt/xslt.h"
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include "securityenvironment_nssimpl.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/servicefactory.hxx>
53cdf0e10cSrcweir
54cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
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 using namespace ::rtl ;
64cdf0e10cSrcweir using namespace ::cppu ;
65cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
66cdf0e10cSrcweir using namespace ::com::sun::star::io ;
67cdf0e10cSrcweir using namespace ::com::sun::star::ucb ;
68cdf0e10cSrcweir using namespace ::com::sun::star::beans ;
69cdf0e10cSrcweir using namespace ::com::sun::star::document ;
70cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
71cdf0e10cSrcweir using namespace ::com::sun::star::registry ;
72cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ;
73cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto ;
74cdf0e10cSrcweir
main(int argc,char ** argv)75cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir CERTCertDBHandle* certHandle ;
78cdf0e10cSrcweir PK11SlotInfo* slot = NULL ;
79cdf0e10cSrcweir PK11SymKey* symKey = NULL ;
80cdf0e10cSrcweir xmlDocPtr doc = NULL ;
81cdf0e10cSrcweir xmlNodePtr tplNode ;
82cdf0e10cSrcweir xmlNodePtr tarNode ;
83cdf0e10cSrcweir FILE* dstFile = NULL ;
84cdf0e10cSrcweir
85cdf0e10cSrcweir if( argc != 7 ) {
86cdf0e10cSrcweir 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] ) ;
87cdf0e10cSrcweir return 1 ;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
90cdf0e10cSrcweir //Init libxml and libxslt libraries
91cdf0e10cSrcweir xmlInitParser();
92cdf0e10cSrcweir LIBXML_TEST_VERSION
93cdf0e10cSrcweir xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
94cdf0e10cSrcweir xmlSubstituteEntitiesDefault(1);
95cdf0e10cSrcweir
96cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT
97cdf0e10cSrcweir xmlIndentTreeOutput = 1;
98cdf0e10cSrcweir #endif // XMLSEC_NO_XSLT
99cdf0e10cSrcweir
100cdf0e10cSrcweir //Initialize NSPR and NSS
101cdf0e10cSrcweir PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
102cdf0e10cSrcweir PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
103cdf0e10cSrcweir if( NSS_Init( argv[1] ) != SECSuccess ) {
104cdf0e10cSrcweir fprintf( stderr , "### cannot intialize NSS!\n" ) ;
105cdf0e10cSrcweir goto done ;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir
108cdf0e10cSrcweir certHandle = CERT_GetDefaultCertDB() ;
109cdf0e10cSrcweir slot = PK11_GetInternalKeySlot() ;
110cdf0e10cSrcweir
111cdf0e10cSrcweir symKey = PK11_KeyGen( slot , CKM_DES3_CBC, NULL, 128, NULL ) ;
112cdf0e10cSrcweir if( symKey == NULL ) {
113cdf0e10cSrcweir fprintf( stderr , "### cannot create symmetric key!\n" ) ;
114cdf0e10cSrcweir goto done ;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir
117cdf0e10cSrcweir //Load XML document
118cdf0e10cSrcweir doc = xmlParseFile( argv[2] ) ;
119cdf0e10cSrcweir if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
120cdf0e10cSrcweir fprintf( stderr , "### Cannot load template xml document!\n" ) ;
121cdf0e10cSrcweir goto done ;
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
124cdf0e10cSrcweir //Find the encryption template
125cdf0e10cSrcweir tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
126cdf0e10cSrcweir if( tplNode == NULL ) {
127cdf0e10cSrcweir fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
128cdf0e10cSrcweir goto done ;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
131cdf0e10cSrcweir //Find the encryption template
132cdf0e10cSrcweir tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( const unsigned char*)argv[4], ( const unsigned char*)argv[5] ) ;
133cdf0e10cSrcweir if( tarNode == NULL ) {
134cdf0e10cSrcweir fprintf( stderr , "### Cannot find the encryption target!\n" ) ;
135cdf0e10cSrcweir goto done ;
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir try {
139cdf0e10cSrcweir Reference< XMultiComponentFactory > xManager = NULL ;
140cdf0e10cSrcweir Reference< XComponentContext > xContext = NULL ;
141cdf0e10cSrcweir
142cdf0e10cSrcweir xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[6] ) ) ;
143cdf0e10cSrcweir
144cdf0e10cSrcweir //Create encryption template
145cdf0e10cSrcweir Reference< XInterface > tplElement =
146cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
147cdf0e10cSrcweir OSL_ENSURE( tplElement.is() ,
148cdf0e10cSrcweir "Encryptor - "
149cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
150cdf0e10cSrcweir
151cdf0e10cSrcweir Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
152cdf0e10cSrcweir OSL_ENSURE( xTplElement.is() ,
153cdf0e10cSrcweir "Encryptor - "
154cdf0e10cSrcweir "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
155cdf0e10cSrcweir
156cdf0e10cSrcweir Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
157cdf0e10cSrcweir OSL_ENSURE( xTplEleTunnel.is() ,
158cdf0e10cSrcweir "Encryptor - "
159cdf0e10cSrcweir "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
160cdf0e10cSrcweir
161cdf0e10cSrcweir XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
162cdf0e10cSrcweir OSL_ENSURE( pTplElement != NULL ,
163cdf0e10cSrcweir "Encryptor - "
164cdf0e10cSrcweir "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
165cdf0e10cSrcweir
166cdf0e10cSrcweir pTplElement->setNativeElement( tplNode ) ;
167cdf0e10cSrcweir
168cdf0e10cSrcweir //Create encryption target element
169cdf0e10cSrcweir Reference< XInterface > tarElement =
170cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
171cdf0e10cSrcweir OSL_ENSURE( tarElement.is() ,
172cdf0e10cSrcweir "Encryptor - "
173cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
174cdf0e10cSrcweir
175cdf0e10cSrcweir Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ;
176cdf0e10cSrcweir OSL_ENSURE( xTarElement.is() ,
177cdf0e10cSrcweir "Encryptor - "
178cdf0e10cSrcweir "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
179cdf0e10cSrcweir
180cdf0e10cSrcweir Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ;
181cdf0e10cSrcweir OSL_ENSURE( xTarEleTunnel.is() ,
182cdf0e10cSrcweir "Encryptor - "
183cdf0e10cSrcweir "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
184cdf0e10cSrcweir
185cdf0e10cSrcweir XMLElementWrapper_XmlSecImpl* pTarElement = ( XMLElementWrapper_XmlSecImpl* )xTarEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
186cdf0e10cSrcweir OSL_ENSURE( pTarElement != NULL ,
187cdf0e10cSrcweir "Encryptor - "
188cdf0e10cSrcweir "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
189cdf0e10cSrcweir
190cdf0e10cSrcweir pTarElement->setNativeElement( tarNode ) ;
191cdf0e10cSrcweir
192cdf0e10cSrcweir
193cdf0e10cSrcweir //Build XML Encryption template
194cdf0e10cSrcweir Reference< XInterface > enctpl =
195cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext ) ;
196cdf0e10cSrcweir OSL_ENSURE( enctpl.is() ,
197cdf0e10cSrcweir "Encryptor - "
198cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
199cdf0e10cSrcweir
200cdf0e10cSrcweir Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
201cdf0e10cSrcweir OSL_ENSURE( xTemplate.is() ,
202cdf0e10cSrcweir "Encryptor - "
203cdf0e10cSrcweir "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
204cdf0e10cSrcweir
205cdf0e10cSrcweir //Import the encryption template
206cdf0e10cSrcweir xTemplate->setTemplate( xTplElement ) ;
207cdf0e10cSrcweir xTemplate->setTarget( xTarElement ) ;
208cdf0e10cSrcweir
209cdf0e10cSrcweir //Create security environment
210cdf0e10cSrcweir //Build Security Environment
211cdf0e10cSrcweir Reference< XInterface > xsecenv =
212cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ;
213cdf0e10cSrcweir OSL_ENSURE( xsecenv.is() ,
214cdf0e10cSrcweir "Encryptor - "
215cdf0e10cSrcweir "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
216cdf0e10cSrcweir
217cdf0e10cSrcweir Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
218cdf0e10cSrcweir OSL_ENSURE( xSecEnv.is() ,
219cdf0e10cSrcweir "Encryptor - "
220cdf0e10cSrcweir "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
221cdf0e10cSrcweir
222cdf0e10cSrcweir //Setup key slot and certDb
223cdf0e10cSrcweir Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
224cdf0e10cSrcweir OSL_ENSURE( xEnvTunnel.is() ,
225cdf0e10cSrcweir "Encryptor - "
226cdf0e10cSrcweir "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
227cdf0e10cSrcweir
228cdf0e10cSrcweir SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
229cdf0e10cSrcweir OSL_ENSURE( pSecEnv != NULL ,
230cdf0e10cSrcweir "Encryptor - "
231cdf0e10cSrcweir "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
232cdf0e10cSrcweir
233cdf0e10cSrcweir pSecEnv->setCryptoSlot( slot ) ;
234cdf0e10cSrcweir pSecEnv->setCertDb( certHandle ) ;
235cdf0e10cSrcweir pSecEnv->adoptSymKey( symKey ) ;
236cdf0e10cSrcweir
237cdf0e10cSrcweir
238cdf0e10cSrcweir //Build XML Security Context
239cdf0e10cSrcweir Reference< XInterface > xmlsecctx =
240cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ;
241cdf0e10cSrcweir OSL_ENSURE( xmlsecctx.is() ,
242cdf0e10cSrcweir "Encryptor - "
243cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
244cdf0e10cSrcweir
245cdf0e10cSrcweir Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
246cdf0e10cSrcweir OSL_ENSURE( xSecCtx.is() ,
247cdf0e10cSrcweir "Encryptor - "
248cdf0e10cSrcweir "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
249cdf0e10cSrcweir
250cdf0e10cSrcweir xSecCtx->setSecurityEnvironment( xSecEnv ) ;
251cdf0e10cSrcweir
252cdf0e10cSrcweir //Get encrypter
253cdf0e10cSrcweir Reference< XInterface > xmlencrypter =
254cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl"), xContext ) ;
255cdf0e10cSrcweir OSL_ENSURE( xmlencrypter.is() ,
256cdf0e10cSrcweir "Encryptor - "
257cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
258cdf0e10cSrcweir
259cdf0e10cSrcweir Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
260cdf0e10cSrcweir OSL_ENSURE( xEncrypter.is() ,
261cdf0e10cSrcweir "Encryptor - "
262cdf0e10cSrcweir "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
263cdf0e10cSrcweir
264cdf0e10cSrcweir //perform encryption
265cdf0e10cSrcweir xTemplate = xEncrypter->encrypt( xTemplate , xSecCtx ) ;
266cdf0e10cSrcweir OSL_ENSURE( xTemplate.is() ,
267cdf0e10cSrcweir "Encryptor - "
268cdf0e10cSrcweir "Cannot encrypt the xml document" ) ;
269cdf0e10cSrcweir } catch( Exception& e ) {
270cdf0e10cSrcweir fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
271cdf0e10cSrcweir goto done ;
272cdf0e10cSrcweir }
273cdf0e10cSrcweir
274cdf0e10cSrcweir dstFile = fopen( argv[3], "w" ) ;
275cdf0e10cSrcweir if( dstFile == NULL ) {
276cdf0e10cSrcweir fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
277cdf0e10cSrcweir goto done ;
278cdf0e10cSrcweir }
279cdf0e10cSrcweir
280cdf0e10cSrcweir //Save result
281cdf0e10cSrcweir xmlDocDump( dstFile, doc ) ;
282cdf0e10cSrcweir
283cdf0e10cSrcweir done:
284cdf0e10cSrcweir if( dstFile != NULL )
285cdf0e10cSrcweir fclose( dstFile ) ;
286cdf0e10cSrcweir
287cdf0e10cSrcweir if( symKey != NULL ) {
288cdf0e10cSrcweir PK11_FreeSymKey( symKey ) ;
289cdf0e10cSrcweir }
290cdf0e10cSrcweir
291cdf0e10cSrcweir if( slot != NULL )
292cdf0e10cSrcweir PK11_FreeSlot( slot ) ;
293cdf0e10cSrcweir
294cdf0e10cSrcweir PK11_LogoutAll() ;
295cdf0e10cSrcweir NSS_Shutdown() ;
296cdf0e10cSrcweir
297cdf0e10cSrcweir /* Shutdown libxslt/libxml */
298cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT
299cdf0e10cSrcweir xsltCleanupGlobals();
300cdf0e10cSrcweir #endif /* XMLSEC_NO_XSLT */
301cdf0e10cSrcweir xmlCleanupParser();
302cdf0e10cSrcweir
303cdf0e10cSrcweir return 0;
304cdf0e10cSrcweir }
305cdf0e10cSrcweir
306