diff options
author | Malte Timmermann <mt@openoffice.org> | 2004-07-12 12:15:31 +0000 |
---|---|---|
committer | Malte Timmermann <mt@openoffice.org> | 2004-07-12 12:15:31 +0000 |
commit | 9f34fde7a8750b856570b5e855d2ecb226bf0c23 (patch) | |
tree | 994a2faf203d884d79fe9e1f1e055f041e5703d0 /xmlsecurity/tools/standalone | |
parent | 8c7c9465b6a35c829294a829cfc3152e5dc9ce6e (diff) |
libxmlsec
Diffstat (limited to 'xmlsecurity/tools/standalone')
18 files changed, 4723 insertions, 0 deletions
diff --git a/xmlsecurity/tools/standalone/csfit/certmngr.cxx b/xmlsecurity/tools/standalone/csfit/certmngr.cxx new file mode 100644 index 000000000000..bacf989e3ac1 --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/certmngr.cxx @@ -0,0 +1,201 @@ +/************************************************************************* + * + * $RCSfile: certmngr.cxx,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_NssImpl.hxx" + +#include "nspr.h" +#include "prtypes.h" + +#include "pk11func.h" +#include "cert.h" +#include "cryptohi.h" +#include "certdb.h" +#include "nss.h" + +#include "xmlsec/strings.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::security ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + +extern OUString bigIntegerToNumericString( Sequence< sal_Int8 > serial ) ; + +int SAL_CALL main( int argc, char **argv ) +{ + CERTCertDBHandle* certHandle ; + PK11SlotInfo* slot = NULL ; + + if( argc != 3 ) { + fprintf( stderr, "Usage: %s < CertDir > <rdb file>\n\n" , argv[0] ) ; + return 1 ; + } + + for( ; getchar() != 'q' ; ) { + + //Initialize NSPR and NSS + PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ; + PK11_SetPasswordFunc( PriPK11PasswordFunc ) ; + if( NSS_Init( argv[1] ) != SECSuccess ) { + fprintf( stderr , "### cannot intialize NSS!\n" ) ; + goto done ; + } + + certHandle = CERT_GetDefaultCertDB() ; + slot = PK11_GetInternalKeySlot() ; + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[2] ) ) ; + OSL_ENSURE( xManager.is() , + "ServicesManager - " + "Cannot get service manager" ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Signer - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Signer - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xEnvTunnel.is() , + "Signer - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Signer - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + pSecEnv->setCryptoSlot( slot ) ; + pSecEnv->setCertDb( certHandle ) ; + + //Get personal certificate + Sequence < Reference< XCertificate > > xPersonalCerts = pSecEnv->getPersonalCertificates() ; + Sequence < Reference< XCertificate > > xCertPath ; + for( int i = 0; i < xPersonalCerts.getLength(); i ++ ) { + //Print the certificate infomation. + fprintf( stdout, "\nPersonal Certificate Info\n" ) ; + fprintf( stdout, "\tCertificate Issuer[%s]\n", OUStringToOString( xPersonalCerts[i]->getIssuerName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + fprintf( stdout, "\tCertificate Serial Number[%s]\n", OUStringToOString( bigIntegerToNumericString( xPersonalCerts[i]->getSerialNumber() ), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + fprintf( stdout, "\tCertificate Subject[%s]\n", OUStringToOString( xPersonalCerts[i]->getSubjectName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + + //build the certificate path + xCertPath = pSecEnv->buildCertificatePath( xPersonalCerts[i] ) ; + //Print the certificate path. + fprintf( stdout, "\tCertificate Path\n" ) ; + for( int j = 0; j < xCertPath.getLength(); j ++ ) { + fprintf( stdout, "\t\tCertificate Authority Subject[%s]\n", OUStringToOString( xCertPath[j]->getSubjectName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + } + + //Get the certificate + Sequence < sal_Int8 > serial = xPersonalCerts[i]->getSerialNumber() ; + Reference< XCertificate > xcert = pSecEnv->getCertificate( xPersonalCerts[i]->getIssuerName(), xPersonalCerts[i]->getSerialNumber() ) ; + if( !xcert.is() ) { + fprintf( stdout, "The personal certificate is not in the certificate database\n" ) ; + } + } + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + +done: + if( slot != NULL ) + PK11_FreeSlot( slot ) ; + + PK11_LogoutAll() ; + NSS_Shutdown() ; + + } + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/csfit/decrypter.cxx b/xmlsecurity/tools/standalone/csfit/decrypter.cxx new file mode 100644 index 000000000000..fb49fa9aa79f --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/decrypter.cxx @@ -0,0 +1,303 @@ +/************************************************************************* + * + * $RCSfile: decrypter.cxx,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + + +#include "SecurityEnvironment_NssImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "nspr.h" +#include "prtypes.h" + +#include "pk11func.h" +#include "cert.h" +#include "cryptohi.h" +#include "certdb.h" +#include "nss.h" + +#include "xmlsec/strings.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> +#include <cppuhelper/bootstrap.hxx> +#include <cppuhelper/servicefactory.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryption.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + + +int SAL_CALL main( int argc, char **argv ) +{ + CERTCertDBHandle* certHandle = NULL ; + PK11SlotInfo* slot = NULL ; + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + FILE* dstFile = NULL ; + + + if( argc != 5 ) { + fprintf( stderr, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv[0] ) ; + return 1 ; + } + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + + //Initialize NSPR and NSS + PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ; + PK11_SetPasswordFunc( PriPK11PasswordFunc ) ; + if( NSS_Init( argv[1] ) != SECSuccess ) { + fprintf( stderr , "### cannot intialize NSS!\n" ) ; + goto done ; + } + + certHandle = CERT_GetDefaultCertDB() ; + slot = PK11_GetInternalKeySlot() ; + + //Load XML document + doc = xmlParseFile( argv[2] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the encryption template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the encryption template!\n" ) ; + goto done ; + } + + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ; + + //Create encryption template + Reference< XInterface > tplElement = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext ) ; + OSL_ENSURE( tplElement.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplElement.is() , + "Decryptor - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplEleTunnel.is() , + "Decryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; + + XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pTplElement != NULL , + "Decryptor - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + pTplElement->setNativeElement( tplNode ) ; + + //Build XML Encryption template + Reference< XInterface > enctpl = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryptionTemplate"), xContext ) ; + OSL_ENSURE( enctpl.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ; + + Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Decryptor - " + "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ; + + //Import the encryption template + xTemplate->setTemplate( xTplElement ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.SecurityEnvironment"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Decryptor - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xEnvTunnel.is() , + "Decryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Decryptor - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + pSecEnv->setCryptoSlot( slot ) ; + pSecEnv->setCertDb( certHandle ) ; + + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLSecurityContext"), xContext ) ; + OSL_ENSURE( xmlsecctx.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Decryptor - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + + //Get encrypter + Reference< XInterface > xmlencrypter = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext ) ; + OSL_ENSURE( xmlencrypter.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLEncryption\"" ) ; + + Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ; + OSL_ENSURE( xEncrypter.is() , + "Decryptor - " + "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ; + + + //Perform decryption + Reference< XXMLElementWrapper> xDecrRes = xEncrypter->decrypt( xTemplate , xSecCtx ) ; + OSL_ENSURE( xDecrRes.is() , + "Decryptor - " + "Cannot decrypt the xml document" ) ; + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + + dstFile = fopen( argv[3], "w" ) ; + if( dstFile == NULL ) { + fprintf( stderr , "### Can not open file %s\n", argv[3] ) ; + goto done ; + } + + //Save result + xmlDocDump( dstFile, doc ) ; + +done: + if( dstFile != NULL ) + fclose( dstFile ) ; + + if( slot != NULL ) + PK11_FreeSlot( slot ) ; + + PK11_LogoutAll() ; + NSS_Shutdown() ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/csfit/encrypter.cxx b/xmlsecurity/tools/standalone/csfit/encrypter.cxx new file mode 100644 index 000000000000..fe73d3052a4f --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/encrypter.cxx @@ -0,0 +1,340 @@ +/************************************************************************* + * + * $RCSfile: encrypter.cxx,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_NssImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "nspr.h" +#include "prtypes.h" + +#include "pk11func.h" +#include "cert.h" +#include "cryptohi.h" +#include "certdb.h" +#include "nss.h" + +#include "xmlsec/strings.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> +#include <cppuhelper/servicefactory.hxx> + +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryption.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + +int SAL_CALL main( int argc, char **argv ) +{ + CERTCertDBHandle* certHandle ; + PK11SlotInfo* slot = NULL ; + PK11SymKey* symKey = NULL ; + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + FILE* dstFile = NULL ; + + if( argc != 7 ) { + 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] ) ; + return 1 ; + } + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + //Initialize NSPR and NSS + PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ; + PK11_SetPasswordFunc( PriPK11PasswordFunc ) ; + if( NSS_Init( argv[1] ) != SECSuccess ) { + fprintf( stderr , "### cannot intialize NSS!\n" ) ; + goto done ; + } + + certHandle = CERT_GetDefaultCertDB() ; + slot = PK11_GetInternalKeySlot() ; + + symKey = PK11_KeyGen( slot , CKM_DES3_CBC, NULL, 128, NULL ) ; + if( symKey == NULL ) { + fprintf( stderr , "### cannot create symmetric key!\n" ) ; + goto done ; + } + + //Load XML document + doc = xmlParseFile( argv[2] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the encryption template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the encryption template!\n" ) ; + goto done ; + } + + //Find the encryption template + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( const unsigned char*)argv[4], ( const unsigned char*)argv[5] ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find the encryption target!\n" ) ; + goto done ; + } + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[6] ) ) ; + + //Create encryption template + Reference< XInterface > tplElement = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext ) ; + OSL_ENSURE( tplElement.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplElement.is() , + "Encryptor - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplEleTunnel.is() , + "Encryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; + + XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pTplElement != NULL , + "Encryptor - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + pTplElement->setNativeElement( tplNode ) ; + + //Create encryption target element + Reference< XInterface > tarElement = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext ) ; + OSL_ENSURE( tarElement.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ; + OSL_ENSURE( xTarElement.is() , + "Encryptor - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ; + OSL_ENSURE( xTarEleTunnel.is() , + "Encryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; + + XMLElementWrapper_XmlSecImpl* pTarElement = ( XMLElementWrapper_XmlSecImpl* )xTarEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pTarElement != NULL , + "Encryptor - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + pTarElement->setNativeElement( tarNode ) ; + + + //Build XML Encryption template + Reference< XInterface > enctpl = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryptionTemplate"), xContext ) ; + OSL_ENSURE( enctpl.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ; + + Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Encryptor - " + "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ; + + //Import the encryption template + xTemplate->setTemplate( xTplElement ) ; + xTemplate->setTarget( xTarElement ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.SecurityEnvironment"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Encryptor - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xEnvTunnel.is() , + "Encryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Encryptor - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + pSecEnv->setCryptoSlot( slot ) ; + pSecEnv->setCertDb( certHandle ) ; + pSecEnv->adoptSymKey( symKey ) ; + + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLSecurityContext"), xContext ) ; + OSL_ENSURE( xmlsecctx.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Encryptor - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + //Get encrypter + Reference< XInterface > xmlencrypter = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext ) ; + OSL_ENSURE( xmlencrypter.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLEncryption\"" ) ; + + Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ; + OSL_ENSURE( xEncrypter.is() , + "Encryptor - " + "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ; + + //perform encryption + xTemplate = xEncrypter->encrypt( xTemplate , xSecCtx ) ; + OSL_ENSURE( xTemplate.is() , + "Encryptor - " + "Cannot encrypt the xml document" ) ; + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + + dstFile = fopen( argv[3], "w" ) ; + if( dstFile == NULL ) { + fprintf( stderr , "### Can not open file %s\n", argv[3] ) ; + goto done ; + } + + //Save result + xmlDocDump( dstFile, doc ) ; + +done: + if( dstFile != NULL ) + fclose( dstFile ) ; + + if( symKey != NULL ) { + PK11_FreeSymKey( symKey ) ; + } + + if( slot != NULL ) + PK11_FreeSlot( slot ) ; + + PK11_LogoutAll() ; + NSS_Shutdown() ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/csfit/helper.cxx b/xmlsecurity/tools/standalone/csfit/helper.cxx new file mode 100644 index 000000000000..f0b566c8a6bd --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/helper.cxx @@ -0,0 +1,297 @@ +/************************************************************************* + * + * $RCSfile: helper.cxx,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include "helper.hxx" +#include "osl/diagnose.h" +#include "rtl/ustring.h" +/*#include "libxml/xmlstring.h" +*/ + +/*- + * Helper : create a input stream from a file + */ +Reference< XInputStream > createStreamFromFile( const OUString sFile ) +{ + const sal_Char* pcFile ; + OString aString ; + + aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ; + pcFile = aString.getStr() ; + if( pcFile != NULL ) { + FILE *f = fopen( pcFile , "rb" ); + Reference< XInputStream > r; + + if( f ) { + fseek( f , 0 , SEEK_END ); + int nLength = ftell( f ); + fseek( f , 0 , SEEK_SET ); + + Sequence<sal_Int8> seqIn(nLength); + fread( seqIn.getArray() , nLength , 1 , f ); + + r = Reference< XInputStream > ( new OInputStream( seqIn ) ); + fclose( f ); + } + return r; + } else { + return NULL ; + } + + return NULL ; +} + +/*- + * Helper : set a output stream to a file + */ +Reference< XOutputStream > createStreamToFile( const OUString sFile ) +{ + const sal_Char* pcFile ; + OString aString ; + + aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ; + pcFile = aString.getStr() ; + if( pcFile != NULL ) + return Reference< XOutputStream >( new OOutputStream( pcFile ) ) ; + else + return NULL ; +} + +/*- + * Helper : get service manager and context + */ +Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl ) throw( RuntimeException , Exception ) +{ + Reference< XMultiComponentFactory > xLocalServiceManager = NULL ; + Reference< XComponentContext > xLocalComponentContext = NULL ; + Reference< XMultiComponentFactory > xUsedServiceManager = NULL ; + Reference< XComponentContext > xUsedComponentContext = NULL ; + + OSL_ENSURE( !sUnoUrl.equalsAscii( "" ) , + "serviceManager - " + "No uno URI specified" ) ; + + OSL_ENSURE( !sRdbUrl.equalsAscii( "" ) , + "serviceManager - " + "No rdb URI specified" ) ; + + if( sUnoUrl.equalsAscii( "local" ) ) { + Reference< XSimpleRegistry > xSimpleRegistry = createSimpleRegistry(); + OSL_ENSURE( xSimpleRegistry.is() , + "serviceManager - " + "Cannot create simple registry" ) ; + + //xSimpleRegistry->open(OUString::createFromAscii("xmlsecurity.rdb"), sal_False, sal_False); + xSimpleRegistry->open(sRdbUrl, sal_True, sal_False); + OSL_ENSURE( xSimpleRegistry->isValid() , + "serviceManager - " + "Cannot open xml security registry rdb" ) ; + + xLocalComponentContext = bootstrap_InitialComponentContext( xSimpleRegistry ) ; + OSL_ENSURE( xLocalComponentContext.is() , + "serviceManager - " + "Cannot create intial component context" ) ; + + xLocalServiceManager = xLocalComponentContext->getServiceManager() ; + OSL_ENSURE( xLocalServiceManager.is() , + "serviceManager - " + "Cannot create intial service manager" ) ; + + /*- + * Because of the exception rasied from + * ucbhelper/source/provider/provconf.cxx, lin 323 + * I do not use the content broker at present + ******************************************************************** + //init ucb + if( ::ucb::ContentBroker::get() == NULL ) { + Reference< lang::XMultiServiceFactory > xSvmg( xLocalServiceManager , UNO_QUERY ) ; + OSL_ENSURE( xLocalServiceManager.is() , + "serviceManager - " + "Cannot get multi-service factory" ) ; + + Sequence< Any > args( 2 ) ; + args[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL ) ; + args[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE ) ; + if( ! ::ucb::ContentBroker::initialize( xSvmg , args ) ) { + throw RuntimeException( OUString::createFromAscii( "Cannot inlitialize ContentBroker" ) , Reference< XInterface >() , Any() ) ; + } + } + ********************************************************************/ + + xUsedComponentContext = xLocalComponentContext ; + xUsedServiceManager = xLocalServiceManager ; + } else { + Reference< XComponentContext > xLocalComponentContext = defaultBootstrap_InitialComponentContext() ; + OSL_ENSURE( xLocalComponentContext.is() , + "serviceManager - " + "Cannot create intial component context" ) ; + + Reference< XMultiComponentFactory > xLocalServiceManager = xLocalComponentContext->getServiceManager(); + OSL_ENSURE( xLocalServiceManager.is() , + "serviceManager - " + "Cannot create intial service manager" ) ; + + Reference< XInterface > urlResolver = + xLocalServiceManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver") , xLocalComponentContext ) ; + OSL_ENSURE( urlResolver.is() , + "serviceManager - " + "Cannot get service instance of \"bridge.UnoUrlResolver\"" ) ; + + Reference< XUnoUrlResolver > xUnoUrlResolver( urlResolver , UNO_QUERY ) ; + OSL_ENSURE( xUnoUrlResolver.is() , + "serviceManager - " + "Cannot get interface of \"XUnoUrlResolver\" from service \"bridge.UnoUrlResolver\"" ) ; + + Reference< XInterface > initialObject = xUnoUrlResolver->resolve( sUnoUrl ) ; + OSL_ENSURE( initialObject.is() , + "serviceManager - " + "Cannot resolve uno url" ) ; + + /*- + * Method 1: with Naming Service + ******************************************************************** + Reference< XNamingService > xNamingService( initialObject , UNO_QUERY ) ; + OSL_ENSURE( xNamingService.is() , + "serviceManager - " + "Cannot get interface of \"XNamingService\" from URL resolver" ) ; + + Reference< XInterface > serviceManager = + xNamingService->getRegisteredObject( OUString::createFromAscii( "StarOffice.ServiceManager" ) ) ; + OSL_ENSURE( serviceManager.is() , + "serviceManager - " + "Cannot get service instance of \"StarOffice.ServiceManager\"" ) ; + + xUsedServiceManager = Reference< XMultiComponentFactory >( serviceManager , UNO_QUERY ); + OSL_ENSURE( xUsedServiceManager.is() , + "serviceManager - " + "Cannot get interface of \"XMultiComponentFactory\" from service \"StarOffice.ServiceManager\"" ) ; + + Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ; + OSL_ENSURE( xPropSet.is() , + "serviceManager - " + "Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ; + + xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ; + OSL_ENSURE( xUsedComponentContext.is() , + "serviceManager - " + "Cannot create remote component context" ) ; + + ********************************************************************/ + + /*- + * Method 2: with Componnent context + ******************************************************************** + Reference< XPropertySet > xPropSet( initialObject , UNO_QUERY ) ; + OSL_ENSURE( xPropSet.is() , + "serviceManager - " + "Cannot get interface of \"XPropertySet\" from URL resolver" ) ; + + xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ; + OSL_ENSURE( xUsedComponentContext.is() , + "serviceManager - " + "Cannot create remote component context" ) ; + + xUsedServiceManager = xUsedComponentContext->getServiceManager(); + OSL_ENSURE( xUsedServiceManager.is() , + "serviceManager - " + "Cannot create remote service manager" ) ; + ********************************************************************/ + + /*- + * Method 3: with Service Manager + ********************************************************************/ + xUsedServiceManager = Reference< XMultiComponentFactory >( initialObject , UNO_QUERY ); + OSL_ENSURE( xUsedServiceManager.is() , + "serviceManager - " + "Cannot create remote service manager" ) ; + + Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ; + OSL_ENSURE( xPropSet.is() , + "serviceManager - " + "Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ; + + xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ; + OSL_ENSURE( xUsedComponentContext.is() , + "serviceManager - " + "Cannot create remote component context" ) ; + /********************************************************************/ + } + + xContext = xUsedComponentContext ; + return xUsedServiceManager ; +} + +char* PriPK11PasswordFunc( + PK11SlotInfo *slot , + PRBool retry , + void* arg +) { + char* passwd = NULL ; + + if( retry != PR_TRUE ) { + passwd = ( char* )PORT_Alloc( 20 ) ; + printf( "Input Password:\n" ) ; + scanf( "%s" , passwd ) ; + printf( "The passwod is [%s]\n" , passwd ) ; + } + + return passwd ; +} + diff --git a/xmlsecurity/tools/standalone/csfit/helper.hxx b/xmlsecurity/tools/standalone/csfit/helper.hxx new file mode 100644 index 000000000000..a8e63251dfbd --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/helper.hxx @@ -0,0 +1,275 @@ +/************************************************************************* + * + * $RCSfile: helper.hxx,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include <stdio.h> +/*#include <libxml/xmlstring.h> +*/ + +#include <rtl/ustring.hxx> +#include <pk11func.h> + +#include <cppuhelper/bootstrap.hxx> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/servicefactory.hxx> +#include <ucbhelper/contentbroker.hxx> +#include <ucbhelper/configurationkeys.hxx> + +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/bridge/XUnoUrlResolver.hpp> +#include <com/sun/star/registry/XImplementationRegistration.hpp> +#include <com/sun/star/document/XFilter.hpp> +#include <com/sun/star/document/XExporter.hpp> +#include <com/sun/star/document/XImporter.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/ucb/XSimpleFileAccess.hpp> + +#include <com/sun/star/xml/crypto/XUriBinding.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> + +#include <com/sun/star/xml/sax/XParser.hpp> +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> +#include <com/sun/star/xml/sax/InputSource.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/uno/XNamingService.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::bridge ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::task ; +using namespace ::com::sun::star::xml ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::sax ; + + +/** + * Helper: Implementation of XInputStream + */ +class OInputStream : public WeakImplHelper1 < XInputStream > +{ + public: + OInputStream( const Sequence< sal_Int8 >&seq ) : m_seq( seq ), nPos( 0 ) {} + + virtual sal_Int32 SAL_CALL readBytes( + Sequence< sal_Int8 >& aData , + sal_Int32 nBytesToRead + ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) + { + nBytesToRead = ( nBytesToRead > m_seq.getLength() - nPos ) ? + m_seq.getLength() - nPos : + nBytesToRead ; + aData = Sequence< sal_Int8 > ( &( m_seq.getConstArray()[nPos] ), nBytesToRead ) ; + nPos += nBytesToRead ; + return nBytesToRead ; + } + + virtual sal_Int32 SAL_CALL readSomeBytes( + ::com::sun::star::uno::Sequence< sal_Int8 >& aData , + sal_Int32 nMaxBytesToRead + ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) + { + return readBytes( aData, nMaxBytesToRead ) ; + } + + virtual void SAL_CALL skipBytes( + sal_Int32 nBytesToSkip + ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) + { + // not implemented + } + + virtual sal_Int32 SAL_CALL available( + void + ) throw( NotConnectedException, IOException, RuntimeException ) + { + return m_seq.getLength() - nPos ; + } + + virtual void SAL_CALL closeInput( + void + ) throw( NotConnectedException, IOException, RuntimeException ) + { + // not needed + } + + private: + sal_Int32 nPos; + Sequence< sal_Int8> m_seq; +} ; + +/** + * Helper : create a input stream from a file + */ +Reference< XInputStream > createStreamFromFile( const OUString sFile ) ; + +/** + * Helper: Implementation of XOutputStream + */ +class OOutputStream : public WeakImplHelper1 < XOutputStream > +{ + public: + OOutputStream( const char *pcFile ) { + strcpy( m_pcFile , pcFile ) ; + m_f = 0 ; + } + + virtual void SAL_CALL writeBytes( + const Sequence< sal_Int8 >& aData + ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) { + if( !m_f ) { + m_f = fopen( m_pcFile , "w" ) ; + } + + fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f ) ; + } + + virtual void SAL_CALL flush( + void + ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) { + fflush( m_f ) ; + } + + virtual void SAL_CALL closeOutput( + void + ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) { + fclose( m_f ) ; + m_f = 0 ; + } + + private: + char m_pcFile[256]; + FILE *m_f; +} ; + +/** + * Helper: Implementation of XUriBinding + */ +class OUriBinding : public WeakImplHelper1 < ::com::sun::star::xml::crypto::XUriBinding > +{ + public: + OUriBinding() { + //Do nothing + } + + OUriBinding( + ::rtl::OUString& aUri, + ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& aInputStream ) { + m_vUris.push_back( aUri ) ; + m_vStreams.push_back( aInputStream ) ; + } + + virtual void SAL_CALL setUriBinding( + const ::rtl::OUString& aUri , + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aInputStream + ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) { + m_vUris.push_back( aUri ) ; + m_vStreams.push_back( aInputStream ) ; + } + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getUriBinding( const ::rtl::OUString& uri ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) { + ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream ; + + int size = m_vUris.size() ; + for( int i = 0 ; i<size ; ++i ) { + if( uri == m_vUris[i] ) { + xInputStream = m_vStreams[i]; + break; + } + } + + return xInputStream; + } + + private: + std::vector< ::rtl::OUString > m_vUris ; + std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > > m_vStreams ; +} ; + +/** + * Helper : set a output stream to a file + */ +Reference< XOutputStream > createStreamToFile( const OUString sFile ) ; + +/** + * Helper : get service manager and context + */ +Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl ) throw( RuntimeException , Exception ) ; + +/** + * Helper : Get password function for PKCS11 slot + */ +char* PriPK11PasswordFunc( PK11SlotInfo *slot , PRBool retry , void* arg ) ; + + diff --git a/xmlsecurity/tools/standalone/csfit/makefile.mk b/xmlsecurity/tools/standalone/csfit/makefile.mk new file mode 100644 index 000000000000..450233169461 --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/makefile.mk @@ -0,0 +1,187 @@ +#************************************************************************* +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.1.1.1 $ +# +# last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ +# +# The Contents of this file are made available subject to the terms of +# either of the following licenses +# +# - GNU Lesser General Public License Version 2.1 +# - Sun Industry Standards Source License Version 1.1 +# +# Sun Microsystems Inc., October, 2000 +# +# GNU Lesser General Public License Version 2.1 +# ============================================= +# Copyright 2000 by Sun Microsystems, Inc. +# 901 San Antonio Road, Palo Alto, CA 94303, USA +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1, as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307 USA +# +# +# Sun Industry Standards Source License Version 1.1 +# ================================================= +# The contents of this file are subject to the Sun Industry Standards +# Source License Version 1.1 (the "License"); You may not use this file +# except in compliance with the License. You may obtain a copy of the +# License at http://www.openoffice.org/license.html. +# +# Software provided under this License is provided on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. +# See the License for the specific provisions governing your rights and +# obligations concerning the Software. +# +# The Initial Developer of the Original Code is: Sun Microsystems, Inc. +# +# Copyright: 2000 by Sun Microsystems, Inc. +# +# All Rights Reserved. +# +# Contributor(s): _______________________________________ +# +# +# +#************************************************************************* + +PRJ=..$/..$/.. + +PRJNAME=xmlsecurity +TARGET=xmlsecurity-cxsfit +ENABLE_EXCEPTIONS=TRUE +NO_BSYMBOLIC=TRUE +TARGETTYPE=CUI +LIBTARGET=NO + +# --- Settings ----------------------------------------------------- + +.INCLUDE : svpre.mk +.INCLUDE : settings.mk +.INCLUDE : sv.mk +.INCLUDE : $(PRJ)$/util$/target.pmk + + +.IF "$(CRYPTO_ENGINE)" == "nss" + +CDEFS += -DXMLSEC_CRYPTO_NSS -DXMLSEC_NO_XSLT +SOLARINC += \ + -I$(MOZ_INC) \ + -I$(NSPR_INC) \ + -I$(NSS_INC) \ + -I$(PRJ)$/source$/xmlsec \ + -I$(PRJ)$/source$/xmlsec$/nss + +# --- Files -------------------------------------------------------- + +SHARE_LIBS = \ + $(CPPULIB) \ + $(CPPUHELPERLIB) \ + $(SALLIB) + +.IF "$(GUI)"=="WNT" +SHARE_LIBS+= "ixml2.lib" "nss3.lib" "nspr4.lib" "xmlsec.lib" "xmlsec-nss.lib" "xsec_xmlsec.lib" +.ELSE +SHARE_LIBS+= "-lxml2" "-lnss3" "-lnspr4" "-lxmlsec" "-lxmlsec-nss" "-lxsec_xmlsec" +.ENDIF + +SHARE_OBJS = \ + $(OBJ)$/helper.obj + +# +# The 1st application +# + +APP2TARGET= signer +APP2OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/signer.obj + +.IF "$(OS)" == "LINUX" +APP2STDLIBS+= -lstdc++ +.ENDIF + +APP2STDLIBS+= \ + $(SHARE_LIBS) + +# +# The 2nd application +# +APP3TARGET= encrypter +APP3OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/encrypter.obj + +.IF "$(OS)" == "LINUX" +APP3STDLIBS+= -lstdc++ +.ENDIF + +APP3STDLIBS+= \ + $(SHARE_LIBS) + +# +# The 3rd application +# +APP4TARGET= verifier +APP4OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/verifier.obj + +.IF "$(OS)" == "LINUX" +APP4STDLIBS+= -lstdc++ +.ENDIF + +APP4STDLIBS+= \ + $(SHARE_LIBS) + +# +# The 4th application +# +APP5TARGET= decrypter +APP5OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/decrypter.obj + +.IF "$(OS)" == "LINUX" +APP5STDLIBS+= -lstdc++ +.ENDIF + +APP5STDLIBS+= \ + $(SHARE_LIBS) + +# +# The 5th application +# +APP6TARGET= certmngr +APP6OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/certmngr.obj + +.IF "$(OS)" == "LINUX" +APP6STDLIBS+= -lstdc++ +.ENDIF + +APP6STDLIBS+= \ + $(SHARE_LIBS) + +.ENDIF + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/xmlsecurity/tools/standalone/csfit/signer.cxx b/xmlsecurity/tools/standalone/csfit/signer.cxx new file mode 100644 index 000000000000..b29acea17d15 --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/signer.cxx @@ -0,0 +1,388 @@ +/************************************************************************* + * + * $RCSfile: signer.cxx,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_NssImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "nspr.h" +#include "prtypes.h" + +#include "pk11func.h" +#include "cert.h" +#include "cryptohi.h" +#include "certdb.h" +#include "nss.h" + +#include "xmlsec/strings.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLSignature.hpp> +#include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + + +int SAL_CALL main( int argc, char **argv ) +{ + CERTCertDBHandle* certHandle ; + PK11SlotInfo* slot = NULL ; + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + xmlAttrPtr idAttr ; + xmlChar* idValue ; + xmlAttrPtr uriAttr ; + xmlChar* uriValue ; + OUString* uri = NULL ; + Reference< XUriBinding > xUriBinding ; + FILE* dstFile = NULL ; + + if( argc != 5 ) { + fprintf( stderr, "Usage: %s < CertDir > <file_url of template> <file_url of result> <rdb file>\n\n" , argv[0] ) ; + return 1 ; + } + + for( int hhh = 0 ; hhh < 2 ; hhh ++ ) { + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + + //Initialize NSPR and NSS + PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ; + PK11_SetPasswordFunc( PriPK11PasswordFunc ) ; + if( NSS_Init( argv[1] ) != SECSuccess ) { + fprintf( stderr , "### cannot intialize NSS!\n" ) ; + goto done ; + } + + certHandle = CERT_GetDefaultCertDB() ; + slot = PK11_GetInternalKeySlot() ; + + //Load XML document + doc = xmlParseFile( argv[2] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the signature template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the signature template!\n" ) ; + goto done ; + } + + //Find the element with ID attribute + //Here we only try to find the "document" node. + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ; + if( tarNode == NULL ) { + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ; + } + + //Find the "id" attrbute in the element + if( tarNode != NULL ) { + if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) { + //NULL + } else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) { + //NULL + } else { + idAttr = NULL ; + } + } + + //Add ID to DOM + if( idAttr != NULL ) { + idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ; + if( idValue == NULL ) { + fprintf( stderr , "### the ID value is NULL!\n" ) ; + goto done ; + } + + if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) { + fprintf( stderr , "### Can not add the ID value!\n" ) ; + goto done ; + } + } + + //Reference handler + //Find the signature reference + tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find the signature reference!\n" ) ; + goto done ; + } + + //Find the "URI" attrbute in the reference + uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find URI of the reference!\n" ) ; + goto done ; + } + + //Get the "URI" attrbute value + uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ; + if( uriValue == NULL ) { + fprintf( stderr , "### the URI value is NULL!\n" ) ; + goto done ; + } + + if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) { + fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ; + // uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ; + uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ; + } + + if( uri != NULL ) { + fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + Reference< XInputStream > xStream = createStreamFromFile( *uri ) ; + if( !xStream.is() ) { + fprintf( stderr , "### Can not get the URI stream!\n" ) ; + goto done ; + } + + xUriBinding = new OUriBinding( *uri, xStream ) ; + } + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ; + OSL_ENSURE( xManager.is() , + "ServicesManager - " + "Cannot get service manager" ) ; + + //Create signature template + Reference< XInterface > element = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; + OSL_ENSURE( element.is() , + "Signer - " + "Cannot get service instance of \"wrapper.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Signer - " + "Cannot get interface of \"XXMLElement\" from service \"xsec.XMLElement\"" ) ; + + Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ; + OSL_ENSURE( xEleTunnel.is() , + "Signer - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ; + + XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pElement != NULL , + "Signer - " + "Cannot get implementation of \"xsec.XMLElement\"" ) ; + + //Set signature template + pElement->setNativeElement( tplNode ) ; + + //Build XML Signature template + Reference< XInterface > signtpl = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSignatureTemplate" ) , xContext ) ; + OSL_ENSURE( signtpl.is() , + "Signer - " + "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ; + + Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Signer - " + "Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ; + + //Import the signature template + xTemplate->setTemplate( xElement ) ; + + //Import the URI/Stream binding + if( xUriBinding.is() ) + xTemplate->setBinding( xUriBinding ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Signer - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Signer - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Signer - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Signer - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + pSecEnv->setCryptoSlot( slot ) ; + pSecEnv->setCertDb( certHandle ) ; + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Signer - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Signer - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + //Generate XML signature + Reference< XInterface > xmlsigner = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl"), xContext ) ; + OSL_ENSURE( xmlsigner.is() , + "Signer - " + "Cannot get service instance of \"xsec.XMLSignature\"" ) ; + + Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ; + OSL_ENSURE( xSigner.is() , + "Signer - " + "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ; + + //perform signature + xTemplate = xSigner->generate( xTemplate , xSecCtx ) ; + OSL_ENSURE( xTemplate.is() , + "Signer - " + "Cannot generate the xml signature" ) ; + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + + dstFile = fopen( argv[3], "w" ) ; + if( dstFile == NULL ) { + fprintf( stderr , "### Can not open file %s\n", argv[3] ) ; + goto done ; + } + + //Save result + xmlDocDump( dstFile, doc ) ; + +done: + if( uri != NULL ) + delete uri ; + + if( dstFile != NULL ) + fclose( dstFile ) ; + + if( doc != NULL ) + xmlFreeDoc( doc ) ; + + if( slot != NULL ) + PK11_FreeSlot( slot ) ; + + PK11_LogoutAll() ; + NSS_Shutdown() ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + } + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/csfit/util/makefile.mk b/xmlsecurity/tools/standalone/csfit/util/makefile.mk new file mode 100644 index 000000000000..ee39211b9f12 --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/util/makefile.mk @@ -0,0 +1,216 @@ +#************************************************************************* +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.1.1.1 $ +# +# last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ +# +# The Contents of this file are made available subject to the terms of +# either of the following licenses +# +# - GNU Lesser General Public License Version 2.1 +# - Sun Industry Standards Source License Version 1.1 +# +# Sun Microsystems Inc., October, 2000 +# +# GNU Lesser General Public License Version 2.1 +# ============================================= +# Copyright 2000 by Sun Microsystems, Inc. +# 901 San Antonio Road, Palo Alto, CA 94303, USA +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1, as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307 USA +# +# +# Sun Industry Standards Source License Version 1.1 +# ================================================= +# The contents of this file are subject to the Sun Industry Standards +# Source License Version 1.1 (the "License"); You may not use this file +# except in compliance with the License. You may obtain a copy of the +# License at http://www.openoffice.org/license.html. +# +# Software provided under this License is provided on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. +# See the License for the specific provisions governing your rights and +# obligations concerning the Software. +# +# The Initial Developer of the Original Code is: Sun Microsystems, Inc. +# +# Copyright: 2000 by Sun Microsystems, Inc. +# +# All Rights Reserved. +# +# Contributor(s): _______________________________________ +# +# +# +#************************************************************************* +PRJ=..$/..$/..$/.. + +PRJNAME=xmlsecurity +TARGET=xmlsecurity-secfit-boot +TARGETTYPE=GUI + +VERSION=$(UPD) +GEN_HID=TRUE +APP2NOSAL=TRUE + +# --- Settings ----------------------------------------------------------- +USE_JAVAVER=TRUE + +.INCLUDE : settings.mk + +VERINFONAME=verinfo + +.INCLUDE : target.mk +.INCLUDE : $(PRJ)$/util$/target.pmk + +# ------------------------------------------------------------------------- +# --- Targets ------------------------------------------------------------- + +ALLTAR : $(BIN)$/boot_services.rdb + +REGISTERLIBS= \ + i18npool.uno$(DLLPOST) \ + $(DLLPRE)tk$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ + $(DLLPRE)mcnttype$(DLLPOST) + +# $(DLLPRE)i18n$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ +# $(DLLPRE)i18npool$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ +# $(DLLPRE)tk$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ +# $(DLLPRE)mcnttype$(DLLPOST) + +# Additional libraries +DLLPRE_NO= +REGISTERLIBS+= \ + $(DLLPRE_NO)shlibloader.uno$(DLLPOST) \ + $(DLLPRE_NO)dynamicloader.uno$(DLLPOST) \ + $(DLLPRE_NO)namingservice.uno$(DLLPOST) \ + $(DLLPRE_NO)servicemgr.uno$(DLLPOST) \ + $(DLLPRE)xsec_fw$(DLLPOST) \ + $(DLLPRE)xsec_xmlsec$(DLLPOST) + +# $(DLLPRE)implreg.uno$(DLLPOST) \ +# $(DLLPRE)nestedreg.uno$(DLLPOST) \ +# $(DLLPRE)simplereg.uno$(DLLPOST) \ + + +.IF "$(GUI)" == "UNX" +MY_DLLDIR=$(SOLARLIBDIR) +REGISTERLIBS+= \ + $(DLLPRE)dtransX11$(UPD)$(DLLPOSTFIX)$(DLLPOST) + +.ELSE # "$(GUI)" == "UNX" +.IF "$(GUI)"=="WNT" + +MY_DLLDIR=$(SOLARBINDIR) +REGISTERLIBS+= \ + $(DLLPRE)sysdtrans$(DLLPOST) \ + $(DLLPRE)ftransl$(DLLPOST) \ + $(DLLPRE)dnd$(DLLPOST) + +.ELSE # "$(GUI)"=="WNT" + + @echo "**********************************************************" + @echo "*** unknown platform: don't know which librarys to use ***" + @echo "**********************************************************" + force_dmake_to_error +.ENDIF # "$(GUI)"=="WNT" +.ENDIF # "$(GUI)" == "UNX" + +.IF "$(SOLAR_JAVA)" != "" +.IF "$(JAVANUMVER)" >= "000100040000" +# native libraries, which are only necessary, when java shall run within setup +REGISTERLIBS_JAVA= \ + javavm.uno$(DLLPOST) \ + javaloader.uno$(DLLPOST) + +# add here java components, which shall run with setup +REGISTERJARS=\ + java_uno_accessbridge.jar +# xsec_jxsec.jar + +# jar-files, which regcomp needs so that it can use java +REGCOMP_JARS=unoil.jar java_uno.jar ridl.jar sandbox.jar jurt.jar juh.jar + +# CLASSPATH, which regcomp needs to be run +# $(SOLARLIBDIR) needs to be included in case Java components are registered, +# because java_uno.jar needs to find the native java_uno shared library: +MY_CLASSPATH_TMP=$(foreach,i,$(REGCOMP_JARS) $(SOLARBINDIR)$/$i)$(PATH_SEPERATOR)$(SOLARLIBDIR)$(PATH_SEPERATOR)$(XCLASSPATH) +REGCOMP_CLASSPATH=$(strip $(subst,!,$(PATH_SEPERATOR) $(MY_CLASSPATH_TMP:s/ /!/))) + +.IF "$(GUI)"!="WNT" +DOLLAR_SIGN=\$$ +MY_JAVA_COMPPATH=file://$(SOLARBINDIR) +.IF "$(OS)"=="MACOSX" +REGCOMP_ENV=\ + setenv CLASSPATH $(REGCOMP_CLASSPATH) && \ + setenv DYLD_LIBRARY_PATH $(DYLD_LIBRARY_PATH):$(JDKLIB) +.ELSE +REGCOMP_ENV=\ + setenv CLASSPATH $(REGCOMP_CLASSPATH) && \ + setenv LD_LIBRARY_PATH $(LD_LIBRARY_PATH):$(JDKLIB) +.ENDIF +.ELSE # "$(GUI)" != "WNT" +.IF "$(USE_SHELL)" != "4nt" +DOLLAR_SIGN=\$$ +REGCOMP_ENV=\ + setenv CLASSPATH "$(strip $(subst,\,/ $(shell guw.pl echo $(REGCOMP_CLASSPATH))))" && \ + setenv PATH "$(PATH):$(JREPATH)" +MY_JAVA_COMPPATH=$(strip $(subst,\,/ file:///$(shell guw.pl echo $(SOLARBINDIR)))) +.ELSE # "$(USE_SHELL)" != "4nt" +DOLLAR_SIGN=$$ +REGCOMP_ENV=\ + set CLASSPATH=$(REGCOMP_CLASSPATH) && \ + set PATH=$(PATH);$(JREPATH) +MY_JAVA_COMPPATH=$(strip $(subst,\,/ file:///$(SOLARBINDIR))) +.ENDIF # "$(USE_SHELL)" != "4nt" +.ENDIF # "$(GUI)"!="WNT" +.ENDIF # "$(JAVANUMVER)" >= "000100040000" +.ENDIF # "$(SOLAR_JAVA)" != "" + +$(BIN)$/boot_services.rdb: \ + makefile.mk \ + $(foreach,i,$(REGISTERLIBS) $(MY_DLLDIR)$/$(i)) \ + $(foreach,i,$(REGISTERLIBS_JAVA) $(MY_DLLDIR)$/$(i)) + -rm -f $@ $(BIN)$/regcomp.rdb $(BIN)$/boot_services.tmp +# register the native libraries + $(REGCOMP) -register -r $(BIN)$/boot_services.tmp -c "$(strip $(REGISTERLIBS))" +.IF "$(SOLAR_JAVA)" != "" +.IF "$(OS)"!="MACOSX" +.IF "$(JAVANUMVER)" >= "000100040000" + $(REGCOMP) -register -r $(BIN)$/boot_services.tmp -c "$(strip $(REGISTERLIBS_JAVA))" +# prepare a registry which regcomp can work on (types+java services) + $(REGCOMP) -register -r $(BIN)$/regcomp.rdb -c "$(strip $(REGISTERLIBS_JAVA))" + $(REGMERGE) $(BIN)$/regcomp.rdb / $(SOLARBINDIR)/udkapi.rdb +# now do the registration + +$(REGCOMP_ENV) && \ + $(REGCOMP) -br $(BIN)$/regcomp.rdb \ + -register \ + -r $(BIN)$/boot_services.tmp \ + -l com.sun.star.loader.Java2 \ + -env:UNO_JAVA_COMPPATH=$(MY_JAVA_COMPPATH) \ + $(foreach,i,$(REGISTERJARS) -c vnd.sun.star.expand:$(DOLLAR_SIGN)UNO_JAVA_COMPPATH/$(i)) +# $(foreach,i,$(REGISTERJARS) -c vnd.sun.star.expand:$(DOLLAR_SIGN)UNO_JAVA_COMPPATH/$(i)) -c $(MY_JAVA_COMPPATH)$/xsec_jxsec.jar + +.ENDIF # "$(JAVANUMVER)" >= "000100040000" +.ENDIF # "$(OS)" != "MACOSX" +.ENDIF # "$(SOLAR_JAVA)" != "" +# to have a transaction, move it now to the final name + $(REGMERGE) $(BIN)$/boot_services.tmp / $(SOLARBINDIR)/types.rdb + mv $(BIN)$/boot_services.tmp $@ + diff --git a/xmlsecurity/tools/standalone/csfit/verifier.cxx b/xmlsecurity/tools/standalone/csfit/verifier.cxx new file mode 100644 index 000000000000..2de2a424b9ca --- /dev/null +++ b/xmlsecurity/tools/standalone/csfit/verifier.cxx @@ -0,0 +1,371 @@ +/************************************************************************* + * + * $RCSfile: verifier.cxx,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_NssImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "nspr.h" +#include "prtypes.h" + +#include "pk11func.h" +#include "cert.h" +#include "cryptohi.h" +#include "certdb.h" +#include "nss.h" + +#include "xmlsec/strings.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> +#include <cppuhelper/servicefactory.hxx> + +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLSignature.hpp> +#include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + + +int SAL_CALL main( int argc, char **argv ) +{ + CERTCertDBHandle* certHandle = NULL ; + PK11SlotInfo* slot = NULL ; + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + xmlAttrPtr idAttr ; + xmlChar* idValue ; + xmlAttrPtr uriAttr ; + xmlChar* uriValue ; + OUString* uri = NULL ; + Reference< XUriBinding > xUriBinding ; + + if( argc != 4 ) { + fprintf( stderr, "Usage: %s < CertDir > <file_url> <rdb file>\n" , argv[0] ) ; + return 1 ; + } + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + + //Initialize NSPR and NSS + PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ; + PK11_SetPasswordFunc( PriPK11PasswordFunc ) ; + if( NSS_Init( argv[1] ) != SECSuccess ) { + fprintf( stderr , "### cannot intialize NSS!\n" ) ; + return 1 ; + } + + certHandle = CERT_GetDefaultCertDB() ; + slot = PK11_GetInternalKeySlot() ; + + //Load XML document + doc = xmlParseFile( argv[2] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the signature template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the signature template!\n" ) ; + goto done ; + } + + //Find the element with ID attribute + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ; + if( tarNode == NULL ) { + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ; + } + + //Find the "id" attrbute in the element + if( tarNode != NULL ) { + if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) { + //NULL + } else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) { + //NULL + } else { + idAttr = NULL ; + } + } + + //Add ID to DOM + if( idAttr != NULL ) { + idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ; + if( idValue == NULL ) { + fprintf( stderr , "### the ID value is NULL!\n" ) ; + goto done ; + } + + if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) { + fprintf( stderr , "### Can not add the ID value!\n" ) ; + goto done ; + } + } + + //Reference handler + //Find the signature reference + tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find the signature reference!\n" ) ; + goto done ; + } + + //Find the "URI" attrbute in the reference + uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find URI of the reference!\n" ) ; + goto done ; + } + + //Get the "URI" attrbute value + uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ; + if( uriValue == NULL ) { + fprintf( stderr , "### the URI value is NULL!\n" ) ; + goto done ; + } + + if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) { + fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ; + // uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ; + uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ; + } + + if( uri != NULL ) { + fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + Reference< XInputStream > xStream = createStreamFromFile( *uri ) ; + if( !xStream.is() ) { + fprintf( stderr , "### Can not get the URI stream!\n" ) ; + goto done ; + } + + xUriBinding = new OUriBinding( *uri, xStream ) ; + } + + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[3] ) ) ; + + //Create signature template + Reference< XInterface > element = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; + OSL_ENSURE( element.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Verifier - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ; + OSL_ENSURE( xEleTunnel.is() , + "Verifier - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ; + + XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pElement != NULL , + "Verifier - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + //Set signature template + pElement->setNativeElement( tplNode ) ; + + //Build XML Signature template + Reference< XInterface > signtpl = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLSignatureTemplate"), xContext ) ; + OSL_ENSURE( signtpl.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ; + + Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Verifier - " + "Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ; + + //Import the signature template + xTemplate->setTemplate( xElement ) ; + + //Import the URI/Stream binding + if( xUriBinding.is() ) + xTemplate->setBinding( xUriBinding ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Verifier - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Verifier - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Verifier - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Verifier - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + pSecEnv->setCryptoSlot( slot ) ; + pSecEnv->setCertDb( certHandle ) ; + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Verifier - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + //Generate XML signature + Reference< XInterface > xmlsigner = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl"), xContext ) ; + OSL_ENSURE( xmlsigner.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLSignature\"" ) ; + + Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ; + OSL_ENSURE( xSigner.is() , + "Verifier - " + "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ; + + + //perform validation + sal_Bool valid = xSigner->validate( xTemplate , xSecCtx ) ; + if( !valid ) { + printf( "Signature is INVALID!\n" ) ; + } else { + printf( "Signature is VALID!\n" ) ; + } + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + +done : + if( doc != NULL ) + xmlFreeDoc( doc ) ; + + if( slot != NULL ) + PK11_FreeSlot( slot ) ; + + PK11_LogoutAll() ; + NSS_Shutdown() ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + return 0 ; +} + diff --git a/xmlsecurity/tools/standalone/mscsfit/certmngr.cxx b/xmlsecurity/tools/standalone/mscsfit/certmngr.cxx new file mode 100644 index 000000000000..8f234a0346f9 --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/certmngr.cxx @@ -0,0 +1,140 @@ +/** -- C++ Source File -- **/ +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_MSCryptImpl.hxx" + +#include "xmlsec/strings.h" +#include "xmlsec/xmltree.h" +#include "xmlsec/mscrypto/app.h" + +#include <rtl/ustring.hxx> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::security ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + +extern OUString bigIntegerToNumericString( Sequence< sal_Int8 > serial ) ; + +int SAL_CALL main( int argc, char **argv ) +{ + const char* n_pCertStore ; + HCERTSTORE n_hStoreHandle ; + + if( argc != 3 && argc != 2 ) { + fprintf( stderr, "Usage: %s <rdb file>\n" , argv[0] ) ; + fprintf( stderr, "Or: \t%s <rdb file> < Cert Store Name >\n\n" , argv[0] ) ; + return 1 ; + } + + //Initialize the crypto engine + if( argc == 3 ) { + n_pCertStore = argv[2] ; + n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ; + if( n_hStoreHandle == NULL ) { + fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ; + return 1 ; + } + } else { + n_pCertStore = NULL ; + n_hStoreHandle = NULL ; + } + //xmlSecMSCryptoAppInit( n_pCertStore ) ; + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[1] ) ) ; + OSL_ENSURE( xManager.is() , + "ServicesManager - " + "Cannot get service manager" ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Signer - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Signer - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xEnvTunnel.is() , + "Signer - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Signer - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + if( n_hStoreHandle != NULL ) { + pSecEnv->setCryptoSlot( n_hStoreHandle ) ; + pSecEnv->setCertDb( n_hStoreHandle ) ; + } else { + pSecEnv->enableDefaultCrypt( sal_True ) ; + } + + //Get personal certificate + Sequence < Reference< XCertificate > > xPersonalCerts = pSecEnv->getPersonalCertificates() ; + OSL_ENSURE( xPersonalCerts.hasElements() , + "getPersonalCertificates - " + "No personal certificates found\n" ) ; + + Sequence < Reference< XCertificate > > xCertPath ; + for( int i = 0; i < xPersonalCerts.getLength(); i ++ ) { + //Print the certificate infomation. + fprintf( stdout, "\nPersonal Certificate Info\n" ) ; + fprintf( stdout, "\tCertificate Issuer[%s]\n", OUStringToOString( xPersonalCerts[i]->getIssuerName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + fprintf( stdout, "\tCertificate Serial Number[%s]\n", OUStringToOString( bigIntegerToNumericString( xPersonalCerts[i]->getSerialNumber() ), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + fprintf( stdout, "\tCertificate Subject[%s]\n", OUStringToOString( xPersonalCerts[i]->getSubjectName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + + //build the certificate path + xCertPath = pSecEnv->buildCertificatePath( xPersonalCerts[i] ) ; + //Print the certificate path. + fprintf( stdout, "\tCertificate Path\n" ) ; + for( int j = 0; j < xCertPath.getLength(); j ++ ) { + fprintf( stdout, "\t\tCertificate Authority Subject[%s]\n", OUStringToOString( xCertPath[j]->getSubjectName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + } + + //Get the certificate + Sequence < sal_Int8 > serial = xPersonalCerts[i]->getSerialNumber() ; + Reference< XCertificate > xcert = pSecEnv->getCertificate( xPersonalCerts[i]->getIssuerName(), xPersonalCerts[i]->getSerialNumber() ) ; + if( !xcert.is() ) { + fprintf( stdout, "The personal certificate is not in the certificate database\n" ) ; + } + } + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + +done: + if( n_hStoreHandle != NULL ) + CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; + + //xmlSecMSCryptoAppShutdown() ; + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/mscsfit/decrypter.cxx b/xmlsecurity/tools/standalone/mscsfit/decrypter.cxx new file mode 100644 index 000000000000..17a758f0cf42 --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/decrypter.cxx @@ -0,0 +1,243 @@ +/** -- C++ Source File -- **/ +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + + +#include "SecurityEnvironment_XmlSecImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "nspr.h" +#include "prtypes.h" + +#include "pk11func.h" +#include "cert.h" +#include "cryptohi.h" +#include "certdb.h" +#include "nss.h" + +#include "xmlsec/strings.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> +#include <cppuhelper/bootstrap.hxx> +#include <cppuhelper/servicefactory.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryption.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + + +int SAL_CALL main( int argc, char **argv ) +{ + CERTCertDBHandle* certHandle = NULL ; + PK11SlotInfo* slot = NULL ; + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + FILE* dstFile = NULL ; + + + if( argc != 5 ) { + fprintf( stderr, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv[0] ) ; + return 1 ; + } + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + + //Initialize NSPR and NSS + PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ; + PK11_SetPasswordFunc( PriPK11PasswordFunc ) ; + if( NSS_Init( argv[1] ) != SECSuccess ) { + fprintf( stderr , "### cannot intialize NSS!\n" ) ; + goto done ; + } + + certHandle = CERT_GetDefaultCertDB() ; + slot = PK11_GetInternalKeySlot() ; + + //Load XML document + doc = xmlParseFile( argv[2] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the encryption template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the encryption template!\n" ) ; + goto done ; + } + + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ; + + //Create encryption template + Reference< XInterface > tplElement = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext ) ; + OSL_ENSURE( tplElement.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplElement.is() , + "Decryptor - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplEleTunnel.is() , + "Decryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; + + XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pTplElement != NULL , + "Decryptor - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + pTplElement->setNativeElement( tplNode ) ; + + //Build XML Encryption template + Reference< XInterface > enctpl = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryptionTemplate"), xContext ) ; + OSL_ENSURE( enctpl.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ; + + Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Decryptor - " + "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ; + + //Import the encryption template + xTemplate->setTemplate( xTplElement ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.SecurityEnvironment"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Decryptor - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xEnvTunnel.is() , + "Decryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_XmlSecImpl* pSecEnv = ( SecurityEnvironment_XmlSecImpl* )xEnvTunnel->getSomething( SecurityEnvironment_XmlSecImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Decryptor - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + pSecEnv->setCryptoSlot( slot ) ; + pSecEnv->setCertDb( certHandle ) ; + + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLSecurityContext"), xContext ) ; + OSL_ENSURE( xmlsecctx.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Decryptor - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + + //Get encrypter + Reference< XInterface > xmlencrypter = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext ) ; + OSL_ENSURE( xmlencrypter.is() , + "Decryptor - " + "Cannot get service instance of \"xsec.XMLEncryption\"" ) ; + + Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ; + OSL_ENSURE( xEncrypter.is() , + "Decryptor - " + "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ; + + + //Perform decryption + Reference< XXMLElementWrapper> xDecrRes = xEncrypter->decrypt( xTemplate , xSecCtx ) ; + OSL_ENSURE( xDecrRes.is() , + "Decryptor - " + "Cannot decrypt the xml document" ) ; + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + + dstFile = fopen( argv[3], "w" ) ; + if( dstFile == NULL ) { + fprintf( stderr , "### Can not open file %s\n", argv[3] ) ; + goto done ; + } + + //Save result + xmlDocDump( dstFile, doc ) ; + +done: + if( dstFile != NULL ) + fclose( dstFile ) ; + + if( slot != NULL ) + PK11_FreeSlot( slot ) ; + + PK11_LogoutAll() ; + NSS_Shutdown() ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/mscsfit/encrypter.cxx b/xmlsecurity/tools/standalone/mscsfit/encrypter.cxx new file mode 100644 index 000000000000..e0f88626ba65 --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/encrypter.cxx @@ -0,0 +1,294 @@ +/** -- C++ Source File -- **/ +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_MSCryptImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "xmlsec/strings.h" +#include "xmlsec/mscrypto/app.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> +#include <cppuhelper/servicefactory.hxx> + +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryption.hpp> +#include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + +int SAL_CALL main( int argc, char **argv ) +{ + const char* n_pCertStore ; + HCERTSTORE n_hStoreHandle ; + + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + FILE* dstFile = NULL ; + + HCRYPTPROV hCryptProv = NULL ; + HCRYPTKEY symKey = NULL ; + + if( argc != 6 && argc != 7 ) { + fprintf( stderr, "Usage: %s <file_url of template> <file_url of result> <target element name> <target element namespace> <rdb file>\n\n" , argv[0] ) ; + 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] ) ; + return 1 ; + } + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + //Initialize the crypto engine + if( argc == 7 ) { + n_pCertStore = argv[6] ; + n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ; + if( n_hStoreHandle == NULL ) { + fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ; + return 1 ; + } + } else { + n_pCertStore = NULL ; + n_hStoreHandle = NULL ; + } + xmlSecMSCryptoAppInit( n_pCertStore ) ; + + //Create encryption key. + //CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ; + //CryptAcquireContext( &hCryptProv , "MyTempKeyContainer" , NULL , PROV_RSA_FULL , CRYPT_DELETEKEYSET ) ; + + if( !CryptAcquireContext( &hCryptProv , NULL , NULL , PROV_RSA_FULL , CRYPT_VERIFYCONTEXT ) ) { + fprintf( stderr, "### cannot get crypto provider context!\n" ); + goto done ; + } + + if( !CryptGenKey( hCryptProv, CALG_RC4, 0x00800000 | CRYPT_EXPORTABLE, &symKey ) ) { + fprintf( stderr , "### cannot create symmetric key!\n" ) ; + goto done ; + } + + //Load XML document + doc = xmlParseFile( argv[1] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the encryption template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the encryption template!\n" ) ; + goto done ; + } + + //Find the encryption template + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( const unsigned char*)argv[3], ( const unsigned char*)argv[4] ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find the encryption target!\n" ) ; + goto done ; + } + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[5] ) ) ; + + //Create encryption template + Reference< XInterface > tplElement = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; + OSL_ENSURE( tplElement.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplElement.is() , + "Encryptor - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ; + OSL_ENSURE( xTplEleTunnel.is() , + "Encryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; + + XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pTplElement != NULL , + "Encryptor - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + pTplElement->setNativeElement( tplNode ) ; + + //Create encryption target element + Reference< XInterface > tarElement = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; + OSL_ENSURE( tarElement.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xTarElement( tarElement , UNO_QUERY ) ; + OSL_ENSURE( xTarElement.is() , + "Encryptor - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xTarEleTunnel( xTarElement , UNO_QUERY ) ; + OSL_ENSURE( xTarEleTunnel.is() , + "Encryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ; + + XMLElementWrapper_XmlSecImpl* pTarElement = ( XMLElementWrapper_XmlSecImpl* )xTarEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pTarElement != NULL , + "Encryptor - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + pTarElement->setNativeElement( tarNode ) ; + + + //Build XML Encryption template + Reference< XInterface > enctpl = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext ) ; + OSL_ENSURE( enctpl.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ; + + Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Encryptor - " + "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ; + + //Import the encryption template + xTemplate->setTemplate( xTplElement ) ; + xTemplate->setTarget( xTarElement ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Encryptor - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xEnvTunnel.is() , + "Encryptor - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Encryptor - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + if( n_hStoreHandle != NULL ) { + pSecEnv->setCryptoSlot( n_hStoreHandle ) ; + pSecEnv->setCertDb( n_hStoreHandle ) ; + } else { + pSecEnv->enableDefaultCrypt( sal_True ) ; + } + + pSecEnv->adoptSymKey( symKey ) ; + + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xmlsecctx.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Encryptor - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + //Get encrypter + Reference< XInterface > xmlencrypter = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xmlencrypter.is() , + "Encryptor - " + "Cannot get service instance of \"xsec.XMLEncryption\"" ) ; + + Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ; + OSL_ENSURE( xEncrypter.is() , + "Encryptor - " + "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ; + + //perform encryption + xTemplate = xEncrypter->encrypt( xTemplate , xSecCtx ) ; + OSL_ENSURE( xTemplate.is() , + "Encryptor - " + "Cannot encrypt the xml document" ) ; + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + + dstFile = fopen( argv[2], "w" ) ; + if( dstFile == NULL ) { + fprintf( stderr , "### Can not open file %s\n", argv[2] ) ; + goto done ; + } + + //Save result + xmlDocDump( dstFile, doc ) ; + +done: + if( dstFile != NULL ) + fclose( dstFile ) ; + + if( symKey != NULL ) { + CryptDestroyKey( symKey ) ; + } + + if( hCryptProv != NULL ) { + CryptReleaseContext( hCryptProv, 0 ) ; + } + + if( n_hStoreHandle != NULL ) + CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/mscsfit/helper.cxx b/xmlsecurity/tools/standalone/mscsfit/helper.cxx new file mode 100644 index 000000000000..de11d89405bb --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/helper.cxx @@ -0,0 +1,217 @@ +#include "helper.hxx" +#include "osl/diagnose.h" +#include "rtl/ustring.h" + +/*- + * Helper : create a input stream from a file + */ +Reference< XInputStream > createStreamFromFile( const OUString sFile ) +{ + const sal_Char* pcFile ; + OString aString ; + + aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ; + pcFile = aString.getStr() ; + if( pcFile != NULL ) { + FILE *f = fopen( pcFile , "rb" ); + Reference< XInputStream > r; + + if( f ) { + fseek( f , 0 , SEEK_END ); + int nLength = ftell( f ); + fseek( f , 0 , SEEK_SET ); + + Sequence<sal_Int8> seqIn(nLength); + fread( seqIn.getArray() , nLength , 1 , f ); + + r = Reference< XInputStream > ( new OInputStream( seqIn ) ); + fclose( f ); + } + return r; + } else { + return NULL ; + } + + return NULL ; +} + +/*- + * Helper : set a output stream to a file + */ +Reference< XOutputStream > createStreamToFile( const OUString sFile ) +{ + const sal_Char* pcFile ; + OString aString ; + + aString = OUStringToOString( sFile , RTL_TEXTENCODING_ASCII_US ) ; + pcFile = aString.getStr() ; + if( pcFile != NULL ) + return Reference< XOutputStream >( new OOutputStream( pcFile ) ) ; + else + return NULL ; +} + +/*- + * Helper : get service manager and context + */ +Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl ) throw( RuntimeException , Exception ) +{ + Reference< XMultiComponentFactory > xLocalServiceManager = NULL ; + Reference< XComponentContext > xLocalComponentContext = NULL ; + Reference< XMultiComponentFactory > xUsedServiceManager = NULL ; + Reference< XComponentContext > xUsedComponentContext = NULL ; + + OSL_ENSURE( !sUnoUrl.equalsAscii( "" ) , + "serviceManager - " + "No uno URI specified" ) ; + + OSL_ENSURE( !sRdbUrl.equalsAscii( "" ) , + "serviceManager - " + "No rdb URI specified" ) ; + + if( sUnoUrl.equalsAscii( "local" ) ) { + Reference< XSimpleRegistry > xSimpleRegistry = createSimpleRegistry(); + OSL_ENSURE( xSimpleRegistry.is() , + "serviceManager - " + "Cannot create simple registry" ) ; + + //xSimpleRegistry->open(OUString::createFromAscii("xmlsecurity.rdb"), sal_False, sal_False); + xSimpleRegistry->open(sRdbUrl, sal_True, sal_False); + OSL_ENSURE( xSimpleRegistry->isValid() , + "serviceManager - " + "Cannot open xml security registry rdb" ) ; + + xLocalComponentContext = bootstrap_InitialComponentContext( xSimpleRegistry ) ; + OSL_ENSURE( xLocalComponentContext.is() , + "serviceManager - " + "Cannot create intial component context" ) ; + + xLocalServiceManager = xLocalComponentContext->getServiceManager() ; + OSL_ENSURE( xLocalServiceManager.is() , + "serviceManager - " + "Cannot create intial service manager" ) ; + + /*- + * Because of the exception rasied from + * ucbhelper/source/provider/provconf.cxx, lin 323 + * I do not use the content broker at present + ******************************************************************** + //init ucb + if( ::ucb::ContentBroker::get() == NULL ) { + Reference< lang::XMultiServiceFactory > xSvmg( xLocalServiceManager , UNO_QUERY ) ; + OSL_ENSURE( xLocalServiceManager.is() , + "serviceManager - " + "Cannot get multi-service factory" ) ; + + Sequence< Any > args( 2 ) ; + args[ 0 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL ) ; + args[ 1 ] <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE ) ; + if( ! ::ucb::ContentBroker::initialize( xSvmg , args ) ) { + throw RuntimeException( OUString::createFromAscii( "Cannot inlitialize ContentBroker" ) , Reference< XInterface >() , Any() ) ; + } + } + ********************************************************************/ + + xUsedComponentContext = xLocalComponentContext ; + xUsedServiceManager = xLocalServiceManager ; + } else { + Reference< XComponentContext > xLocalComponentContext = defaultBootstrap_InitialComponentContext() ; + OSL_ENSURE( xLocalComponentContext.is() , + "serviceManager - " + "Cannot create intial component context" ) ; + + Reference< XMultiComponentFactory > xLocalServiceManager = xLocalComponentContext->getServiceManager(); + OSL_ENSURE( xLocalServiceManager.is() , + "serviceManager - " + "Cannot create intial service manager" ) ; + + Reference< XInterface > urlResolver = + xLocalServiceManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.bridge.UnoUrlResolver") , xLocalComponentContext ) ; + OSL_ENSURE( urlResolver.is() , + "serviceManager - " + "Cannot get service instance of \"bridge.UnoUrlResolver\"" ) ; + + Reference< XUnoUrlResolver > xUnoUrlResolver( urlResolver , UNO_QUERY ) ; + OSL_ENSURE( xUnoUrlResolver.is() , + "serviceManager - " + "Cannot get interface of \"XUnoUrlResolver\" from service \"bridge.UnoUrlResolver\"" ) ; + + Reference< XInterface > initialObject = xUnoUrlResolver->resolve( sUnoUrl ) ; + OSL_ENSURE( initialObject.is() , + "serviceManager - " + "Cannot resolve uno url" ) ; + + /*- + * Method 1: with Naming Service + ******************************************************************** + Reference< XNamingService > xNamingService( initialObject , UNO_QUERY ) ; + OSL_ENSURE( xNamingService.is() , + "serviceManager - " + "Cannot get interface of \"XNamingService\" from URL resolver" ) ; + + Reference< XInterface > serviceManager = + xNamingService->getRegisteredObject( OUString::createFromAscii( "StarOffice.ServiceManager" ) ) ; + OSL_ENSURE( serviceManager.is() , + "serviceManager - " + "Cannot get service instance of \"StarOffice.ServiceManager\"" ) ; + + xUsedServiceManager = Reference< XMultiComponentFactory >( serviceManager , UNO_QUERY ); + OSL_ENSURE( xUsedServiceManager.is() , + "serviceManager - " + "Cannot get interface of \"XMultiComponentFactory\" from service \"StarOffice.ServiceManager\"" ) ; + + Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ; + OSL_ENSURE( xPropSet.is() , + "serviceManager - " + "Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ; + + xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ; + OSL_ENSURE( xUsedComponentContext.is() , + "serviceManager - " + "Cannot create remote component context" ) ; + + ********************************************************************/ + + /*- + * Method 2: with Componnent context + ******************************************************************** + Reference< XPropertySet > xPropSet( initialObject , UNO_QUERY ) ; + OSL_ENSURE( xPropSet.is() , + "serviceManager - " + "Cannot get interface of \"XPropertySet\" from URL resolver" ) ; + + xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ; + OSL_ENSURE( xUsedComponentContext.is() , + "serviceManager - " + "Cannot create remote component context" ) ; + + xUsedServiceManager = xUsedComponentContext->getServiceManager(); + OSL_ENSURE( xUsedServiceManager.is() , + "serviceManager - " + "Cannot create remote service manager" ) ; + ********************************************************************/ + + /*- + * Method 3: with Service Manager + ********************************************************************/ + xUsedServiceManager = Reference< XMultiComponentFactory >( initialObject , UNO_QUERY ); + OSL_ENSURE( xUsedServiceManager.is() , + "serviceManager - " + "Cannot create remote service manager" ) ; + + Reference< XPropertySet > xPropSet( xUsedServiceManager , UNO_QUERY ) ; + OSL_ENSURE( xPropSet.is() , + "serviceManager - " + "Cannot get interface of \"XPropertySet\" from service \"StarOffice.ServiceManager\"" ) ; + + xPropSet->getPropertyValue( OUString::createFromAscii( "DefaultContext" ) ) >>= xUsedComponentContext ; + OSL_ENSURE( xUsedComponentContext.is() , + "serviceManager - " + "Cannot create remote component context" ) ; + /********************************************************************/ + } + + xContext = xUsedComponentContext ; + return xUsedServiceManager ; +} + diff --git a/xmlsecurity/tools/standalone/mscsfit/helper.hxx b/xmlsecurity/tools/standalone/mscsfit/helper.hxx new file mode 100644 index 000000000000..0f95ef50c4ab --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/helper.hxx @@ -0,0 +1,204 @@ +#include <stdio.h> +#include <rtl/ustring.hxx> + +#include <cppuhelper/bootstrap.hxx> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/servicefactory.hxx> +#include <ucbhelper/contentbroker.hxx> +#include <ucbhelper/configurationkeys.hxx> + +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/bridge/XUnoUrlResolver.hpp> +#include <com/sun/star/registry/XImplementationRegistration.hpp> +#include <com/sun/star/document/XFilter.hpp> +#include <com/sun/star/document/XExporter.hpp> +#include <com/sun/star/document/XImporter.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/io/XActiveDataSource.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/ucb/XSimpleFileAccess.hpp> + +#include <com/sun/star/xml/crypto/XUriBinding.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> + +#include <com/sun/star/xml/sax/XParser.hpp> +#include <com/sun/star/xml/sax/XDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> +#include <com/sun/star/xml/sax/InputSource.hpp> +#include <com/sun/star/io/XInputStream.hpp> +#include <com/sun/star/io/XOutputStream.hpp> +#include <com/sun/star/uno/XNamingService.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::bridge ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::task ; +using namespace ::com::sun::star::xml ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::sax ; + + +/** + * Helper: Implementation of XInputStream + */ +class OInputStream : public WeakImplHelper1 < XInputStream > +{ + public: + OInputStream( const Sequence< sal_Int8 >&seq ) : m_seq( seq ), nPos( 0 ) {} + + virtual sal_Int32 SAL_CALL readBytes( + Sequence< sal_Int8 >& aData , + sal_Int32 nBytesToRead + ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) + { + nBytesToRead = ( nBytesToRead > m_seq.getLength() - nPos ) ? + m_seq.getLength() - nPos : + nBytesToRead ; + aData = Sequence< sal_Int8 > ( &( m_seq.getConstArray()[nPos] ), nBytesToRead ) ; + nPos += nBytesToRead ; + return nBytesToRead ; + } + + virtual sal_Int32 SAL_CALL readSomeBytes( + ::com::sun::star::uno::Sequence< sal_Int8 >& aData , + sal_Int32 nMaxBytesToRead + ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) + { + return readBytes( aData, nMaxBytesToRead ) ; + } + + virtual void SAL_CALL skipBytes( + sal_Int32 nBytesToSkip + ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException ) + { + // not implemented + } + + virtual sal_Int32 SAL_CALL available( + void + ) throw( NotConnectedException, IOException, RuntimeException ) + { + return m_seq.getLength() - nPos ; + } + + virtual void SAL_CALL closeInput( + void + ) throw( NotConnectedException, IOException, RuntimeException ) + { + // not needed + } + + private: + sal_Int32 nPos; + Sequence< sal_Int8> m_seq; +} ; + +/** + * Helper : create a input stream from a file + */ +Reference< XInputStream > createStreamFromFile( const OUString sFile ) ; + +/** + * Helper: Implementation of XOutputStream + */ +class OOutputStream : public WeakImplHelper1 < XOutputStream > +{ + public: + OOutputStream( const char *pcFile ) { + strcpy( m_pcFile , pcFile ) ; + m_f = 0 ; + } + + virtual void SAL_CALL writeBytes( + const Sequence< sal_Int8 >& aData + ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) { + if( !m_f ) { + m_f = fopen( m_pcFile , "w" ) ; + } + + fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f ) ; + } + + virtual void SAL_CALL flush( + void + ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) { + fflush( m_f ) ; + } + + virtual void SAL_CALL closeOutput( + void + ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) { + fclose( m_f ) ; + m_f = 0 ; + } + + private: + char m_pcFile[256]; + FILE *m_f; +} ; + +/** + * Helper: Implementation of XUriBinding + */ +class OUriBinding : public WeakImplHelper1 < ::com::sun::star::xml::crypto::XUriBinding > +{ + public: + OUriBinding() { + //Do nothing + } + + OUriBinding( + ::rtl::OUString& aUri, + ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& aInputStream ) { + m_vUris.push_back( aUri ) ; + m_vStreams.push_back( aInputStream ) ; + } + + virtual void SAL_CALL setUriBinding( + const ::rtl::OUString& aUri , + const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aInputStream + ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) { + m_vUris.push_back( aUri ) ; + m_vStreams.push_back( aInputStream ) ; + } + + virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getUriBinding( const ::rtl::OUString& uri ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) { + ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream ; + + int size = m_vUris.size() ; + for( int i = 0 ; i<size ; ++i ) { + if( uri == m_vUris[i] ) { + xInputStream = m_vStreams[i]; + break; + } + } + + return xInputStream; + } + + private: + std::vector< ::rtl::OUString > m_vUris ; + std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > > m_vStreams ; +} ; + +/** + * Helper : set a output stream to a file + */ +Reference< XOutputStream > createStreamToFile( const OUString sFile ) ; + +/** + * Helper : get service manager and context + */ +Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl ) throw( RuntimeException , Exception ) ; + diff --git a/xmlsecurity/tools/standalone/mscsfit/makefile.mk b/xmlsecurity/tools/standalone/mscsfit/makefile.mk new file mode 100644 index 000000000000..8c28a824f68c --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/makefile.mk @@ -0,0 +1,184 @@ +#************************************************************************* +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.1.1.1 $ +# +# last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ +# +# The Contents of this file are made available subject to the terms of +# either of the following licenses +# +# - GNU Lesser General Public License Version 2.1 +# - Sun Industry Standards Source License Version 1.1 +# +# Sun Microsystems Inc., October, 2000 +# +# GNU Lesser General Public License Version 2.1 +# ============================================= +# Copyright 2000 by Sun Microsystems, Inc. +# 901 San Antonio Road, Palo Alto, CA 94303, USA +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1, as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307 USA +# +# +# Sun Industry Standards Source License Version 1.1 +# ================================================= +# The contents of this file are subject to the Sun Industry Standards +# Source License Version 1.1 (the "License"); You may not use this file +# except in compliance with the License. You may obtain a copy of the +# License at http://www.openoffice.org/license.html. +# +# Software provided under this License is provided on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. +# See the License for the specific provisions governing your rights and +# obligations concerning the Software. +# +# The Initial Developer of the Original Code is: Sun Microsystems, Inc. +# +# Copyright: 2000 by Sun Microsystems, Inc. +# +# All Rights Reserved. +# +# Contributor(s): _______________________________________ +# +# +# +#************************************************************************* + +PRJ=..$/..$/.. + +PRJNAME=xmlsecurity +TARGET=xmlsecurity-mscxsfit +ENABLE_EXCEPTIONS=TRUE +NO_BSYMBOLIC=TRUE +TARGETTYPE=CUI +LIBTARGET=NO + + +# --- Settings ----------------------------------------------------- + +.INCLUDE : svpre.mk +.INCLUDE : settings.mk +.INCLUDE : sv.mk +.INCLUDE : $(PRJ)$/util$/target.pmk + +.IF "$(CRYPTO_ENGINE)" == "mscrypto" + +CDEFS += -DXMLSEC_CRYPTO_MSCRYPTO -DXMLSEC_NO_XSLT +SOLARINC += \ + -I$(PRJ)$/source$/xmlsec \ + -I$(PRJ)$/source$/xmlsec$/mscrypt + +# --- Files -------------------------------------------------------- + +SHARE_LIBS = \ + $(CPPULIB) \ + $(CPPUHELPERLIB) \ + $(SALLIB) + +.IF "$(GUI)"=="WNT" +SHARE_LIBS+= "ixml2.lib" "crypt32.lib" "advapi32.lib" "xmlsec.lib" "xmlsec-mscrypto.lib" "xsec_xmlsec.lib" +.ELSE +SHARE_LIBS+= "-lxml2" "-lnss3" "-lnspr4" "-lxmlsec" "-lxmlsec-nss" "-lxsec_xmlsec" +.ENDIF + +SHARE_OBJS = \ + $(OBJ)$/helper.obj + +# +# The 1st application +# + +APP2TARGET= signer +APP2OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/signer.obj + +.IF "$(OS)" == "LINUX" +APP2STDLIBS+= -lstdc++ +.ENDIF + +APP2STDLIBS+= \ + $(SHARE_LIBS) + +# +# The 2nd application +# +APP3TARGET= encrypter +APP3OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/encrypter.obj + +.IF "$(OS)" == "LINUX" +APP3STDLIBS+= -lstdc++ +.ENDIF + +APP3STDLIBS+= \ + $(SHARE_LIBS) + +# +# The 3rd application +# +APP4TARGET= verifier +APP4OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/verifier.obj + +.IF "$(OS)" == "LINUX" +APP4STDLIBS+= -lstdc++ +.ENDIF + +APP4STDLIBS+= \ + $(SHARE_LIBS) + +## +## The 4th application +## +#APP5TARGET= decrypter +#APP5OBJS= \ +# $(SHARE_OBJS) \ +# $(OBJ)$/decrypter.obj +# +#.IF "$(OS)" == "LINUX" +#APP5STDLIBS+= -lstdc++ +#.ENDIF +# +#APP5STDLIBS+= \ +# $(SHARE_LIBS) + +# +# The 5th application +# +APP6TARGET= certmngr +APP6OBJS= \ + $(SHARE_OBJS) \ + $(OBJ)$/certmngr.obj + +.IF "$(OS)" == "LINUX" +APP6STDLIBS+= -lstdc++ +.ENDIF + +APP6STDLIBS+= \ + $(SHARE_LIBS) + +.ENDIF + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/xmlsecurity/tools/standalone/mscsfit/signer.cxx b/xmlsecurity/tools/standalone/mscsfit/signer.cxx new file mode 100644 index 000000000000..d47912b8e83c --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/signer.cxx @@ -0,0 +1,328 @@ +/** -- C++ Source File -- **/ +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_MSCryptImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "xmlsec/strings.h" +#include "xmlsec/mscrypto/app.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> + +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLSignature.hpp> +#include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + + +int SAL_CALL main( int argc, char **argv ) +{ + const char* n_pCertStore ; + HCERTSTORE n_hStoreHandle ; + + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + xmlAttrPtr idAttr ; + xmlChar* idValue ; + xmlAttrPtr uriAttr ; + xmlChar* uriValue ; + OUString* uri = NULL ; + Reference< XUriBinding > xUriBinding ; + FILE* dstFile = NULL ; + + if( argc !=4 && argc != 5 ) { + fprintf( stderr, "Usage: %s <file_url of template> <file_url of result> <rdb file>\n" , argv[0] ) ; + fprintf( stderr, "Or: \t%s <file_url of template> <file_url of result> <rdb file> < Cert Store Name >\n\n" , argv[0] ) ; + return 1 ; + } + + for( int hhh = 0 ; hhh < 4 ; hhh ++ ) { + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + //Initialize the crypto engine + if( argc == 5 ) { + n_pCertStore = argv[4] ; + n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ; + if( n_hStoreHandle == NULL ) { + fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ; + return 1 ; + } + } else { + n_pCertStore = NULL ; + n_hStoreHandle = NULL ; + } + xmlSecMSCryptoAppInit( n_pCertStore ) ; + + //Load XML document + doc = xmlParseFile( argv[1] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the signature template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the signature template!\n" ) ; + goto done ; + } + + //Find the element with ID attribute + //Here we only try to find the "document" node. + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ; + if( tarNode == NULL ) { + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ; + } + + //Find the "id" attrbute in the element + if( tarNode != NULL ) { + if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) { + //NULL + } else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) { + //NULL + } else { + idAttr = NULL ; + } + } + + //Add ID to DOM + if( idAttr != NULL ) { + idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ; + if( idValue == NULL ) { + fprintf( stderr , "### the ID value is NULL!\n" ) ; + goto done ; + } + + if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) { + fprintf( stderr , "### Can not add the ID value!\n" ) ; + goto done ; + } + } + + //Reference handler + //Find the signature reference + tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find the signature reference!\n" ) ; + goto done ; + } + + //Find the "URI" attrbute in the reference + uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find URI of the reference!\n" ) ; + goto done ; + } + + //Get the "URI" attrbute value + uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ; + if( uriValue == NULL ) { + fprintf( stderr , "### the URI value is NULL!\n" ) ; + goto done ; + } + + if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) { + fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ; + // uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ; + uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ; + } + + if( uri != NULL ) { + fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + Reference< XInputStream > xStream = createStreamFromFile( *uri ) ; + if( !xStream.is() ) { + fprintf( stderr , "### Can not get the URI stream!\n" ) ; + goto done ; + } + + xUriBinding = new OUriBinding( *uri, xStream ) ; + } + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[3] ) ) ; + OSL_ENSURE( xManager.is() , + "ServicesManager - " + "Cannot get service manager" ) ; + + //Create signature template + Reference< XInterface > element = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; + OSL_ENSURE( element.is() , + "Signer - " + "Cannot get service instance of \"wrapper.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Signer - " + "Cannot get interface of \"XXMLElement\" from service \"xsec.XMLElement\"" ) ; + + Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ; + OSL_ENSURE( xEleTunnel.is() , + "Signer - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ; + + XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pElement != NULL , + "Signer - " + "Cannot get implementation of \"xsec.XMLElement\"" ) ; + + //Set signature template + pElement->setNativeElement( tplNode ) ; + + //Build XML Signature template + Reference< XInterface > signtpl = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSignatureTemplate" ) , xContext ) ; + OSL_ENSURE( signtpl.is() , + "Signer - " + "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ; + + Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Signer - " + "Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ; + + //Import the signature template + xTemplate->setTemplate( xElement ) ; + + //Import the URI/Stream binding + if( xUriBinding.is() ) + xTemplate->setBinding( xUriBinding ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Signer - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Signer - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Signer - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Signer - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + if( n_hStoreHandle != NULL ) { + pSecEnv->setCryptoSlot( n_hStoreHandle ) ; + pSecEnv->setCertDb( n_hStoreHandle ) ; + } else { + pSecEnv->enableDefaultCrypt( sal_True ) ; + } + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Signer - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Signer - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + //Generate XML signature + Reference< XInterface > xmlsigner = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xmlsigner.is() , + "Signer - " + "Cannot get service instance of \"xsec.XMLSignature\"" ) ; + + Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ; + OSL_ENSURE( xSigner.is() , + "Signer - " + "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ; + + //perform signature + xTemplate = xSigner->generate( xTemplate , xSecCtx ) ; + OSL_ENSURE( xTemplate.is() , + "Signer - " + "Cannot generate the xml signature" ) ; + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + + dstFile = fopen( argv[2], "w" ) ; + if( dstFile == NULL ) { + fprintf( stderr , "### Can not open file %s\n", argv[2] ) ; + goto done ; + } + + //Save result + xmlDocDump( dstFile, doc ) ; + +done: + if( uri != NULL ) + delete uri ; + + if( dstFile != NULL ) + fclose( dstFile ) ; + + if( doc != NULL ) + xmlFreeDoc( doc ) ; + + if( n_hStoreHandle != NULL ) + CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; + + xmlSecMSCryptoAppShutdown() ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + } + + return 0; +} + diff --git a/xmlsecurity/tools/standalone/mscsfit/util/makefile.mk b/xmlsecurity/tools/standalone/mscsfit/util/makefile.mk new file mode 100644 index 000000000000..a6ecf612ea56 --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/util/makefile.mk @@ -0,0 +1,216 @@ +#************************************************************************* +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.1.1.1 $ +# +# last change: $Author: mt $ $Date: 2004-07-12 13:15:25 $ +# +# The Contents of this file are made available subject to the terms of +# either of the following licenses +# +# - GNU Lesser General Public License Version 2.1 +# - Sun Industry Standards Source License Version 1.1 +# +# Sun Microsystems Inc., October, 2000 +# +# GNU Lesser General Public License Version 2.1 +# ============================================= +# Copyright 2000 by Sun Microsystems, Inc. +# 901 San Antonio Road, Palo Alto, CA 94303, USA +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1, as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307 USA +# +# +# Sun Industry Standards Source License Version 1.1 +# ================================================= +# The contents of this file are subject to the Sun Industry Standards +# Source License Version 1.1 (the "License"); You may not use this file +# except in compliance with the License. You may obtain a copy of the +# License at http://www.openoffice.org/license.html. +# +# Software provided under this License is provided on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. +# See the License for the specific provisions governing your rights and +# obligations concerning the Software. +# +# The Initial Developer of the Original Code is: Sun Microsystems, Inc. +# +# Copyright: 2000 by Sun Microsystems, Inc. +# +# All Rights Reserved. +# +# Contributor(s): _______________________________________ +# +# +# +#************************************************************************* +PRJ=..$/..$/..$/.. + +PRJNAME=xmlsecurity +TARGET=xmlsecurity-mscsfit-boot +TARGETTYPE=GUI + +VERSION=$(UPD) +GEN_HID=TRUE +APP2NOSAL=TRUE + +# --- Settings ----------------------------------------------------------- +USE_JAVAVER=TRUE + +.INCLUDE : settings.mk +.INCLUDE : $(PRJ)$/util$/target.pmk + +VERINFONAME=verinfo + +.INCLUDE : target.mk + +# ------------------------------------------------------------------------- +# --- Targets ------------------------------------------------------------- + +ALLTAR : $(BIN)$/boot_services.rdb + +REGISTERLIBS= \ + i18npool.uno$(DLLPOST) \ + $(DLLPRE)tk$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ + $(DLLPRE)mcnttype$(DLLPOST) + +# $(DLLPRE)i18n$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ +# $(DLLPRE)i18npool$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ +# $(DLLPRE)tk$(UPD)$(DLLPOSTFIX)$(DLLPOST) \ +# $(DLLPRE)mcnttype$(DLLPOST) + +# Additional libraries +DLLPRE_NO= +REGISTERLIBS+= \ + $(DLLPRE_NO)shlibloader.uno$(DLLPOST) \ + $(DLLPRE_NO)dynamicloader.uno$(DLLPOST) \ + $(DLLPRE_NO)namingservice.uno$(DLLPOST) \ + $(DLLPRE_NO)servicemgr.uno$(DLLPOST) \ + $(DLLPRE)xsec_fw$(DLLPOST) \ + $(DLLPRE)xsec_xmlsec$(DLLPOST) + +# $(DLLPRE)implreg.uno$(DLLPOST) \ +# $(DLLPRE)nestedreg.uno$(DLLPOST) \ +# $(DLLPRE)simplereg.uno$(DLLPOST) \ + + +.IF "$(GUI)" == "UNX" +MY_DLLDIR=$(SOLARLIBDIR) +REGISTERLIBS+= \ + $(DLLPRE)dtransX11$(UPD)$(DLLPOSTFIX)$(DLLPOST) + +.ELSE # "$(GUI)" == "UNX" +.IF "$(GUI)"=="WNT" + +MY_DLLDIR=$(SOLARBINDIR) +REGISTERLIBS+= \ + $(DLLPRE)sysdtrans$(DLLPOST) \ + $(DLLPRE)ftransl$(DLLPOST) \ + $(DLLPRE)dnd$(DLLPOST) + +.ELSE # "$(GUI)"=="WNT" + + @echo "**********************************************************" + @echo "*** unknown platform: don't know which librarys to use ***" + @echo "**********************************************************" + force_dmake_to_error +.ENDIF # "$(GUI)"=="WNT" +.ENDIF # "$(GUI)" == "UNX" + +.IF "$(SOLAR_JAVA)" != "" +.IF "$(JAVANUMVER)" >= "000100040000" +# native libraries, which are only necessary, when java shall run within setup +REGISTERLIBS_JAVA= \ + javavm.uno$(DLLPOST) \ + javaloader.uno$(DLLPOST) + +# add here java components, which shall run with setup +REGISTERJARS=\ + java_uno_accessbridge.jar +# xsec_jxsec.jar + +# jar-files, which regcomp needs so that it can use java +REGCOMP_JARS=unoil.jar java_uno.jar ridl.jar sandbox.jar jurt.jar juh.jar + +# CLASSPATH, which regcomp needs to be run +# $(SOLARLIBDIR) needs to be included in case Java components are registered, +# because java_uno.jar needs to find the native java_uno shared library: +MY_CLASSPATH_TMP=$(foreach,i,$(REGCOMP_JARS) $(SOLARBINDIR)$/$i)$(PATH_SEPERATOR)$(SOLARLIBDIR)$(PATH_SEPERATOR)$(XCLASSPATH) +REGCOMP_CLASSPATH=$(strip $(subst,!,$(PATH_SEPERATOR) $(MY_CLASSPATH_TMP:s/ /!/))) + +.IF "$(GUI)"!="WNT" +DOLLAR_SIGN=\$$ +MY_JAVA_COMPPATH=file://$(SOLARBINDIR) +.IF "$(OS)"=="MACOSX" +REGCOMP_ENV=\ + setenv CLASSPATH $(REGCOMP_CLASSPATH) && \ + setenv DYLD_LIBRARY_PATH $(DYLD_LIBRARY_PATH):$(JDKLIB) +.ELSE +REGCOMP_ENV=\ + setenv CLASSPATH $(REGCOMP_CLASSPATH) && \ + setenv LD_LIBRARY_PATH $(LD_LIBRARY_PATH):$(JDKLIB) +.ENDIF +.ELSE # "$(GUI)" != "WNT" +.IF "$(USE_SHELL)" != "4nt" +DOLLAR_SIGN=\$$ +REGCOMP_ENV=\ + setenv CLASSPATH "$(strip $(subst,\,/ $(shell guw.pl echo $(REGCOMP_CLASSPATH))))" && \ + setenv PATH "$(PATH):$(JREPATH)" +MY_JAVA_COMPPATH=$(strip $(subst,\,/ file:///$(shell guw.pl echo $(SOLARBINDIR)))) +.ELSE # "$(USE_SHELL)" != "4nt" +DOLLAR_SIGN=$$ +REGCOMP_ENV=\ + set CLASSPATH=$(REGCOMP_CLASSPATH) && \ + set PATH=$(PATH);$(JREPATH) +MY_JAVA_COMPPATH=$(strip $(subst,\,/ file:///$(SOLARBINDIR))) +.ENDIF # "$(USE_SHELL)" != "4nt" +.ENDIF # "$(GUI)"!="WNT" +.ENDIF # "$(JAVANUMVER)" >= "000100040000" +.ENDIF # "$(SOLAR_JAVA)" != "" + +$(BIN)$/boot_services.rdb: \ + makefile.mk \ + $(foreach,i,$(REGISTERLIBS) $(MY_DLLDIR)$/$(i)) \ + $(foreach,i,$(REGISTERLIBS_JAVA) $(MY_DLLDIR)$/$(i)) + -rm -f $@ $(BIN)$/regcomp.rdb $(BIN)$/boot_services.tmp +# register the native libraries + $(REGCOMP) -register -r $(BIN)$/boot_services.tmp -c "$(strip $(REGISTERLIBS))" +.IF "$(SOLAR_JAVA)" != "" +.IF "$(OS)"!="MACOSX" +.IF "$(JAVANUMVER)" >= "000100040000" + $(REGCOMP) -register -r $(BIN)$/boot_services.tmp -c "$(strip $(REGISTERLIBS_JAVA))" +# prepare a registry which regcomp can work on (types+java services) + $(REGCOMP) -register -r $(BIN)$/regcomp.rdb -c "$(strip $(REGISTERLIBS_JAVA))" + $(REGMERGE) $(BIN)$/regcomp.rdb / $(SOLARBINDIR)/udkapi.rdb +# now do the registration + +$(REGCOMP_ENV) && \ + $(REGCOMP) -br $(BIN)$/regcomp.rdb \ + -register \ + -r $(BIN)$/boot_services.tmp \ + -l com.sun.star.loader.Java2 \ + -env:UNO_JAVA_COMPPATH=$(MY_JAVA_COMPPATH) \ + $(foreach,i,$(REGISTERJARS) -c vnd.sun.star.expand:$(DOLLAR_SIGN)UNO_JAVA_COMPPATH/$(i)) +# $(foreach,i,$(REGISTERJARS) -c vnd.sun.star.expand:$(DOLLAR_SIGN)UNO_JAVA_COMPPATH/$(i)) -c $(MY_JAVA_COMPPATH)$/xsec_jxsec.jar + +.ENDIF # "$(JAVANUMVER)" >= "000100040000" +.ENDIF # "$(OS)" != "MACOSX" +.ENDIF # "$(SOLAR_JAVA)" != "" +# to have a transaction, move it now to the final name + $(REGMERGE) $(BIN)$/boot_services.tmp / $(SOLARBINDIR)/types.rdb + mv $(BIN)$/boot_services.tmp $@ + diff --git a/xmlsecurity/tools/standalone/mscsfit/verifier.cxx b/xmlsecurity/tools/standalone/mscsfit/verifier.cxx new file mode 100644 index 000000000000..af2c5ec82e06 --- /dev/null +++ b/xmlsecurity/tools/standalone/mscsfit/verifier.cxx @@ -0,0 +1,319 @@ +/** -- C++ Source File -- **/ +#include <stdio.h> +#include "helper.hxx" + +#include "libxml/tree.h" +#include "libxml/parser.h" +#ifndef XMLSEC_NO_XSLT +#include "libxslt/xslt.h" +#endif + +#include "SecurityEnvironment_MSCryptImpl.hxx" +#include "XMLElementWrapper_XmlSecImpl.hxx" + +#include "xmlsec/strings.h" +#include "xmlsec/mscrypto/app.h" +#include "xmlsec/xmltree.h" + +#include <rtl/ustring.hxx> +#include <cppuhelper/servicefactory.hxx> + +#include <com/sun/star/lang/XComponent.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> +#include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp> +#include <com/sun/star/xml/crypto/XXMLSignature.hpp> +#include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp> +#include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp> +#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp> + +using namespace ::rtl ; +using namespace ::cppu ; +using namespace ::com::sun::star::uno ; +using namespace ::com::sun::star::io ; +using namespace ::com::sun::star::ucb ; +using namespace ::com::sun::star::beans ; +using namespace ::com::sun::star::document ; +using namespace ::com::sun::star::lang ; +using namespace ::com::sun::star::registry ; +using namespace ::com::sun::star::xml::wrapper ; +using namespace ::com::sun::star::xml::crypto ; + + +int SAL_CALL main( int argc, char **argv ) +{ + const char* n_pCertStore ; + HCERTSTORE n_hStoreHandle ; + + xmlDocPtr doc = NULL ; + xmlNodePtr tplNode ; + xmlNodePtr tarNode ; + xmlAttrPtr idAttr ; + xmlChar* idValue ; + xmlAttrPtr uriAttr ; + xmlChar* uriValue ; + OUString* uri = NULL ; + Reference< XUriBinding > xUriBinding ; + FILE* dstFile = NULL ; + + if( argc !=3 && argc != 4 ) { + fprintf( stderr, "Usage: %s <file_url> <rdb file>\n" , argv[0] ) ; + fprintf( stderr, "Or: \t%s <file_url> <rdb file> < Cert Store Name >\n\n" , argv[0] ) ; + return 1 ; + } + + for( int hhh = 0 ; hhh < 1 ; hhh ++ ) { + + //Init libxml and libxslt libraries + xmlInitParser(); + LIBXML_TEST_VERSION + xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS; + xmlSubstituteEntitiesDefault(1); + + #ifndef XMLSEC_NO_XSLT + xmlIndentTreeOutput = 1; + #endif // XMLSEC_NO_XSLT + + //Initialize the crypto engine + if( argc == 4 ) { + n_pCertStore = argv[3] ; + n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ; + if( n_hStoreHandle == NULL ) { + fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ; + return 1 ; + } + } else { + n_pCertStore = NULL ; + n_hStoreHandle = NULL ; + } + xmlSecMSCryptoAppInit( n_pCertStore ) ; + + //Load XML document + doc = xmlParseFile( argv[1] ) ; + if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) { + fprintf( stderr , "### Cannot load template xml document!\n" ) ; + goto done ; + } + + //Find the signature template + tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ; + if( tplNode == NULL ) { + fprintf( stderr , "### Cannot find the signature template!\n" ) ; + goto done ; + } + + //Find the element with ID attribute + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ; + if( tarNode == NULL ) { + tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ; + } + + //Find the "id" attrbute in the element + if( tarNode != NULL ) { + if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) { + //NULL + } else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) { + //NULL + } else { + idAttr = NULL ; + } + } + + //Add ID to DOM + if( idAttr != NULL ) { + idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ; + if( idValue == NULL ) { + fprintf( stderr , "### the ID value is NULL!\n" ) ; + goto done ; + } + + if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) { + fprintf( stderr , "### Can not add the ID value!\n" ) ; + goto done ; + } + } + + //Reference handler + //Find the signature reference + tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find the signature reference!\n" ) ; + goto done ; + } + + //Find the "URI" attrbute in the reference + uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ; + if( tarNode == NULL ) { + fprintf( stderr , "### Cannot find URI of the reference!\n" ) ; + goto done ; + } + + //Get the "URI" attrbute value + uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ; + if( uriValue == NULL ) { + fprintf( stderr , "### the URI value is NULL!\n" ) ; + goto done ; + } + + if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) { + fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ; + // uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ; + uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ; + } + + if( uri != NULL ) { + fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + Reference< XInputStream > xStream = createStreamFromFile( *uri ) ; + if( !xStream.is() ) { + fprintf( stderr , "### Can not get the URI stream!\n" ) ; + goto done ; + } + + xUriBinding = new OUriBinding( *uri, xStream ) ; + } + + + try { + Reference< XMultiComponentFactory > xManager = NULL ; + Reference< XComponentContext > xContext = NULL ; + + xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[2] ) ) ; + + //Create signature template + Reference< XInterface > element = + xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ; + OSL_ENSURE( element.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ; + + Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Verifier - " + "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ; + + Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ; + OSL_ENSURE( xEleTunnel.is() , + "Verifier - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ; + + XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ; + OSL_ENSURE( pElement != NULL , + "Verifier - " + "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ; + + //Set signature template + pElement->setNativeElement( tplNode ) ; + + //Build XML Signature template + Reference< XInterface > signtpl = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLSignatureTemplate"), xContext ) ; + OSL_ENSURE( signtpl.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ; + + Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ; + OSL_ENSURE( xTemplate.is() , + "Verifier - " + "Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ; + + //Import the signature template + xTemplate->setTemplate( xElement ) ; + + //Import the URI/Stream binding + if( xUriBinding.is() ) + xTemplate->setBinding( xUriBinding ) ; + + //Create security environment + //Build Security Environment + Reference< XInterface > xsecenv = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Verifier - " + "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; + + Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xSecEnv.is() , + "Verifier - " + "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; + OSL_ENSURE( xElement.is() , + "Verifier - " + "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; + + SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; + OSL_ENSURE( pSecEnv != NULL , + "Verifier - " + "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; + + //Setup key slot and certDb + if( n_hStoreHandle != NULL ) { + pSecEnv->setCryptoSlot( n_hStoreHandle ) ; + pSecEnv->setCertDb( n_hStoreHandle ) ; + } else { + pSecEnv->enableDefaultCrypt( sal_True ) ; + } + + //Build XML Security Context + Reference< XInterface > xmlsecctx = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xsecenv.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ; + + Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ; + OSL_ENSURE( xSecCtx.is() , + "Verifier - " + "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ; + + xSecCtx->setSecurityEnvironment( xSecEnv ) ; + + //Generate XML signature + Reference< XInterface > xmlsigner = + xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_MSCryptImpl"), xContext ) ; + OSL_ENSURE( xmlsigner.is() , + "Verifier - " + "Cannot get service instance of \"xsec.XMLSignature\"" ) ; + + Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ; + OSL_ENSURE( xSigner.is() , + "Verifier - " + "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ; + + + //perform validation + sal_Bool valid = xSigner->validate( xTemplate , xSecCtx ) ; + if( !valid ) { + printf( "Signature is INVALID!\n" ) ; + } else { + printf( "Signature is VALID!\n" ) ; + } + } catch( Exception& e ) { + fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; + goto done ; + } + +done : + if( uri != NULL ) + delete uri ; + + if( doc != NULL ) + xmlFreeDoc( doc ) ; + + if( n_hStoreHandle != NULL ) + CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; + + xmlSecMSCryptoAppShutdown() ; + + /* Shutdown libxslt/libxml */ + #ifndef XMLSEC_NO_XSLT + xsltCleanupGlobals(); + #endif /* XMLSEC_NO_XSLT */ + xmlCleanupParser(); + + } + + return 0 ; +} + |