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