1*cdf0e10cSrcweir /** -- C++ Source File -- **/ 2*cdf0e10cSrcweir 3*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 4*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx" 5*cdf0e10cSrcweir #include <stdio.h> 6*cdf0e10cSrcweir #include "helper.hxx" 7*cdf0e10cSrcweir 8*cdf0e10cSrcweir #include "libxml/tree.h" 9*cdf0e10cSrcweir #include "libxml/parser.h" 10*cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT 11*cdf0e10cSrcweir #include "libxslt/xslt.h" 12*cdf0e10cSrcweir #endif 13*cdf0e10cSrcweir 14*cdf0e10cSrcweir #include "securityenvironment_mscryptimpl.hxx" 15*cdf0e10cSrcweir #include "xmlelementwrapper_xmlsecimpl.hxx" 16*cdf0e10cSrcweir 17*cdf0e10cSrcweir #include "xmlsec/strings.h" 18*cdf0e10cSrcweir #include "xmlsec/mscrypto/app.h" 19*cdf0e10cSrcweir #include "xmlsec/xmltree.h" 20*cdf0e10cSrcweir 21*cdf0e10cSrcweir #include <rtl/ustring.hxx> 22*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx> 23*cdf0e10cSrcweir 24*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp> 25*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 26*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> 27*cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> 28*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryption.hpp> 29*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp> 30*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> 31*cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir using namespace ::rtl ; 34*cdf0e10cSrcweir using namespace ::cppu ; 35*cdf0e10cSrcweir using namespace ::com::sun::star::uno ; 36*cdf0e10cSrcweir using namespace ::com::sun::star::io ; 37*cdf0e10cSrcweir using namespace ::com::sun::star::ucb ; 38*cdf0e10cSrcweir using namespace ::com::sun::star::beans ; 39*cdf0e10cSrcweir using namespace ::com::sun::star::document ; 40*cdf0e10cSrcweir using namespace ::com::sun::star::lang ; 41*cdf0e10cSrcweir using namespace ::com::sun::star::registry ; 42*cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ; 43*cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto ; 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv ) 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir const char* n_pCertStore ; 48*cdf0e10cSrcweir HCERTSTORE n_hStoreHandle ; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir xmlDocPtr doc = NULL ; 51*cdf0e10cSrcweir xmlNodePtr tplNode ; 52*cdf0e10cSrcweir xmlNodePtr tarNode ; 53*cdf0e10cSrcweir FILE* dstFile = NULL ; 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir HCRYPTPROV hCryptProv = NULL ; 56*cdf0e10cSrcweir HCRYPTKEY symKey = NULL ; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir if( argc != 6 && argc != 7 ) { 59*cdf0e10cSrcweir fprintf( stderr, "Usage: %s <file_url of template> <file_url of result> <target element name> <target element namespace> <rdb file>\n\n" , argv[0] ) ; 60*cdf0e10cSrcweir fprintf( stderr, "Usage: %s <file_url of template> <file_url of result> <target element name> <target element namespace> <rdb file> < Cert Store Name >\n\n" , argv[0] ) ; 61*cdf0e10cSrcweir return 1 ; 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir //Init libxml and libxslt libraries 65*cdf0e10cSrcweir xmlInitParser(); 66*cdf0e10cSrcweir LIBXML_TEST_VERSION 67*cdf0e10cSrcweir xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; 68*cdf0e10cSrcweir xmlSubstituteEntitiesDefault(1); 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT 71*cdf0e10cSrcweir xmlIndentTreeOutput = 1; 72*cdf0e10cSrcweir #endif // XMLSEC_NO_XSLT 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir //Initialize the crypto engine 75*cdf0e10cSrcweir if( argc == 7 ) { 76*cdf0e10cSrcweir n_pCertStore = argv[6] ; 77*cdf0e10cSrcweir n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ; 78*cdf0e10cSrcweir if( n_hStoreHandle == NULL ) { 79*cdf0e10cSrcweir fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ; 80*cdf0e10cSrcweir return 1 ; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir } else { 83*cdf0e10cSrcweir n_pCertStore = NULL ; 84*cdf0e10cSrcweir n_hStoreHandle = NULL ; 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir xmlSecMSCryptoAppInit( n_pCertStore ) ; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir //Create encryption key. 89*cdf0e10cSrcweir //CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ; 90*cdf0e10cSrcweir //CryptAcquireContext( &hCryptProv , "MyTempKeyContainer" , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ; 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir if( !CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_VERIFYCONTEXT ) ) { 93*cdf0e10cSrcweir fprintf( stderr, "### cannot get crypto provider context!\n" ); 94*cdf0e10cSrcweir goto done ; 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir if( !CryptGenKey( hCryptProv, CALG_RC4, 0x00800000 | CRYPT_EXPORTABLE, &symKey ) ) { 98*cdf0e10cSrcweir fprintf( stderr , "### cannot create symmetric key!\n" ) ; 99*cdf0e10cSrcweir goto done ; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir //Load XML document 103*cdf0e10cSrcweir doc = xmlParseFile( argv[1] ) ; 104*cdf0e10cSrcweir if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { 105*cdf0e10cSrcweir fprintf( stderr , "### Cannot load template xml document!\n" ) ; 106*cdf0e10cSrcweir goto done ; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir //Find the encryption template 110*cdf0e10cSrcweir tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ; 111*cdf0e10cSrcweir if( tplNode == NULL ) { 112*cdf0e10cSrcweir fprintf( stderr , "### Cannot find the encryption template!\n" ) ; 113*cdf0e10cSrcweir goto done ; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir //Find the encryption template 117*cdf0e10cSrcweir tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( const unsigned char*)argv[3], ( const unsigned char*)argv[4] ) ; 118*cdf0e10cSrcweir if( tarNode == NULL ) { 119*cdf0e10cSrcweir fprintf( stderr , "### Cannot find the encryption target!\n" ) ; 120*cdf0e10cSrcweir goto done ; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir try { 124*cdf0e10cSrcweir Reference< XMultiComponentFactory > xManager = NULL ; 125*cdf0e10cSrcweir Reference< XComponentContext > xContext = NULL ; 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[5] ) ) ; 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir //Create encryption template 130*cdf0e10cSrcweir Reference< XInterface > tplElement = 131*cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; 132*cdf0e10cSrcweir OSL_ENSURE( tplElement.is() , 133*cdf0e10cSrcweir "Encryptor - " 134*cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ; 137*cdf0e10cSrcweir OSL_ENSURE( xTplElement.is() , 138*cdf0e10cSrcweir "Encryptor - " 139*cdf0e10cSrcweir "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ; 142*cdf0e10cSrcweir OSL_ENSURE( xTplEleTunnel.is() , 143*cdf0e10cSrcweir "Encryptor - " 144*cdf0e10cSrcweir "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; 147*cdf0e10cSrcweir OSL_ENSURE( pTplElement != NULL , 148*cdf0e10cSrcweir "Encryptor - " 149*cdf0e10cSrcweir "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir pTplElement->setNativeElement( tplNode ) ; 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir //Create encryption target element 154*cdf0e10cSrcweir Reference< XInterface > tarElement = 155*cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; 156*cdf0e10cSrcweir OSL_ENSURE( tarElement.is() , 157*cdf0e10cSrcweir "Encryptor - " 158*cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ; 161*cdf0e10cSrcweir OSL_ENSURE( xTarElement.is() , 162*cdf0e10cSrcweir "Encryptor - " 163*cdf0e10cSrcweir "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ; 166*cdf0e10cSrcweir OSL_ENSURE( xTarEleTunnel.is() , 167*cdf0e10cSrcweir "Encryptor - " 168*cdf0e10cSrcweir "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir XMLElementWrapper_XmlSecImpl* pTarElement = ( XMLElementWrapper_XmlSecImpl* )xTarEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; 171*cdf0e10cSrcweir OSL_ENSURE( pTarElement != NULL , 172*cdf0e10cSrcweir "Encryptor - " 173*cdf0e10cSrcweir "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir pTarElement->setNativeElement( tarNode ) ; 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir //Build XML Encryption template 179*cdf0e10cSrcweir Reference< XInterface > enctpl = 180*cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext ) ; 181*cdf0e10cSrcweir OSL_ENSURE( enctpl.is() , 182*cdf0e10cSrcweir "Encryptor - " 183*cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ; 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ; 186*cdf0e10cSrcweir OSL_ENSURE( xTemplate.is() , 187*cdf0e10cSrcweir "Encryptor - " 188*cdf0e10cSrcweir "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ; 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir //Import the encryption template 191*cdf0e10cSrcweir xTemplate->setTemplate( xTplElement ) ; 192*cdf0e10cSrcweir xTemplate->setTarget( xTarElement ) ; 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir //Create security environment 195*cdf0e10cSrcweir //Build Security Environment 196*cdf0e10cSrcweir Reference< XInterface > xsecenv = 197*cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ; 198*cdf0e10cSrcweir OSL_ENSURE( xsecenv.is() , 199*cdf0e10cSrcweir "Encryptor - " 200*cdf0e10cSrcweir "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; 203*cdf0e10cSrcweir OSL_ENSURE( xSecEnv.is() , 204*cdf0e10cSrcweir "Encryptor - " 205*cdf0e10cSrcweir "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir //Setup key slot and certDb 208*cdf0e10cSrcweir Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; 209*cdf0e10cSrcweir OSL_ENSURE( xEnvTunnel.is() , 210*cdf0e10cSrcweir "Encryptor - " 211*cdf0e10cSrcweir "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; 214*cdf0e10cSrcweir OSL_ENSURE( pSecEnv != NULL , 215*cdf0e10cSrcweir "Encryptor - " 216*cdf0e10cSrcweir "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir //Setup key slot and certDb 219*cdf0e10cSrcweir if( n_hStoreHandle != NULL ) { 220*cdf0e10cSrcweir pSecEnv->setCryptoSlot( n_hStoreHandle ) ; 221*cdf0e10cSrcweir pSecEnv->setCertDb( n_hStoreHandle ) ; 222*cdf0e10cSrcweir } else { 223*cdf0e10cSrcweir pSecEnv->enableDefaultCrypt( sal_True ) ; 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir pSecEnv->adoptSymKey( symKey ) ; 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir //Build XML Security Context 230*cdf0e10cSrcweir Reference< XInterface > xmlsecctx = 231*cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl"), xContext ) ; 232*cdf0e10cSrcweir OSL_ENSURE( xmlsecctx.is() , 233*cdf0e10cSrcweir "Encryptor - " 234*cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; 237*cdf0e10cSrcweir OSL_ENSURE( xSecCtx.is() , 238*cdf0e10cSrcweir "Encryptor - " 239*cdf0e10cSrcweir "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir xSecCtx->addSecurityEnvironment( xSecEnv ) ; 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir //Get encrypter 244*cdf0e10cSrcweir Reference< XInterface > xmlencrypter = 245*cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_MSCryptImpl"), xContext ) ; 246*cdf0e10cSrcweir OSL_ENSURE( xmlencrypter.is() , 247*cdf0e10cSrcweir "Encryptor - " 248*cdf0e10cSrcweir "Cannot get service instance of \"xsec.XMLEncryption\"" ) ; 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ; 251*cdf0e10cSrcweir OSL_ENSURE( xEncrypter.is() , 252*cdf0e10cSrcweir "Encryptor - " 253*cdf0e10cSrcweir "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ; 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir //perform encryption 256*cdf0e10cSrcweir xTemplate = xEncrypter->encrypt( xTemplate , xSecEnv ) ; 257*cdf0e10cSrcweir OSL_ENSURE( xTemplate.is() , 258*cdf0e10cSrcweir "Encryptor - " 259*cdf0e10cSrcweir "Cannot encrypt the xml document" ) ; 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir com::sun::star::xml::crypto::SecurityOperationStatus m_nStatus = xTemplate->getStatus(); 263*cdf0e10cSrcweir if (m_nStatus == SecurityOperationStatus_OPERATION_SUCCEEDED) 264*cdf0e10cSrcweir { 265*cdf0e10cSrcweir fprintf( stdout, "Operation succeeds.\n") ; 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir else 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir fprintf( stdout, "Operation fails.\n") ; 270*cdf0e10cSrcweir } 271*cdf0e10cSrcweir } catch( Exception& e ) { 272*cdf0e10cSrcweir fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; 273*cdf0e10cSrcweir goto done ; 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir dstFile = fopen( argv[2], "w" ) ; 277*cdf0e10cSrcweir if( dstFile == NULL ) { 278*cdf0e10cSrcweir fprintf( stderr , "### Can not open file %s\n", argv[2] ) ; 279*cdf0e10cSrcweir goto done ; 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir //Save result 283*cdf0e10cSrcweir xmlDocDump( dstFile, doc ) ; 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir done: 286*cdf0e10cSrcweir if( dstFile != NULL ) 287*cdf0e10cSrcweir fclose( dstFile ) ; 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir if( symKey != NULL ) { 290*cdf0e10cSrcweir CryptDestroyKey( symKey ) ; 291*cdf0e10cSrcweir } 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir if( hCryptProv != NULL ) { 294*cdf0e10cSrcweir CryptReleaseContext( hCryptProv, 0 ) ; 295*cdf0e10cSrcweir } 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir if( n_hStoreHandle != NULL ) 298*cdf0e10cSrcweir CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir /* Shutdown libxslt/libxml */ 301*cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT 302*cdf0e10cSrcweir xsltCleanupGlobals(); 303*cdf0e10cSrcweir #endif /* XMLSEC_NO_XSLT */ 304*cdf0e10cSrcweir xmlCleanupParser(); 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir return 0; 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir 309