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