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