1*cdf0e10cSrcweir /** -- C++ Source File -- **/ 2*cdf0e10cSrcweir 3*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 4*cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx" 5*cdf0e10cSrcweir #include <stdio.h> 6*cdf0e10cSrcweir #include "helper.hxx" 7*cdf0e10cSrcweir 8*cdf0e10cSrcweir #include "libxml/tree.h" 9*cdf0e10cSrcweir #include "libxml/parser.h" 10*cdf0e10cSrcweir #ifndef XMLSEC_NO_XSLT 11*cdf0e10cSrcweir #include "libxslt/xslt.h" 12*cdf0e10cSrcweir #endif 13*cdf0e10cSrcweir 14*cdf0e10cSrcweir #include "securityenvironment_mscryptimpl.hxx" 15*cdf0e10cSrcweir 16*cdf0e10cSrcweir #include <xmlsecurity/biginteger.hxx> 17*cdf0e10cSrcweir 18*cdf0e10cSrcweir #include "xmlsec/strings.h" 19*cdf0e10cSrcweir #include "xmlsec/xmltree.h" 20*cdf0e10cSrcweir #include "xmlsec/mscrypto/app.h" 21*cdf0e10cSrcweir 22*cdf0e10cSrcweir #include <rtl/ustring.hxx> 23*cdf0e10cSrcweir 24*cdf0e10cSrcweir using namespace ::rtl ; 25*cdf0e10cSrcweir using namespace ::cppu ; 26*cdf0e10cSrcweir using namespace ::com::sun::star::uno ; 27*cdf0e10cSrcweir using namespace ::com::sun::star::io ; 28*cdf0e10cSrcweir using namespace ::com::sun::star::ucb ; 29*cdf0e10cSrcweir using namespace ::com::sun::star::beans ; 30*cdf0e10cSrcweir using namespace ::com::sun::star::document ; 31*cdf0e10cSrcweir using namespace ::com::sun::star::lang ; 32*cdf0e10cSrcweir using namespace ::com::sun::star::security ; 33*cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ; 34*cdf0e10cSrcweir using namespace ::com::sun::star::xml::crypto ; 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv ) 37*cdf0e10cSrcweir { 38*cdf0e10cSrcweir const char* n_pCertStore ; 39*cdf0e10cSrcweir HCERTSTORE n_hStoreHandle ; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir if( argc != 3 && argc != 2 ) { 42*cdf0e10cSrcweir fprintf( stderr, "Usage: %s <rdb file>\n" , argv[0] ) ; 43*cdf0e10cSrcweir fprintf( stderr, "Or: \t%s <rdb file> < Cert Store Name >\n\n" , argv[0] ) ; 44*cdf0e10cSrcweir return 1 ; 45*cdf0e10cSrcweir } 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir //Initialize the crypto engine 48*cdf0e10cSrcweir if( argc == 3 ) { 49*cdf0e10cSrcweir n_pCertStore = argv[2] ; 50*cdf0e10cSrcweir n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ; 51*cdf0e10cSrcweir if( n_hStoreHandle == NULL ) { 52*cdf0e10cSrcweir fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ; 53*cdf0e10cSrcweir return 1 ; 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir } else { 56*cdf0e10cSrcweir n_pCertStore = NULL ; 57*cdf0e10cSrcweir n_hStoreHandle = NULL ; 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir //xmlSecMSCryptoAppInit( n_pCertStore ) ; 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir try { 62*cdf0e10cSrcweir Reference< XMultiComponentFactory > xManager = NULL ; 63*cdf0e10cSrcweir Reference< XComponentContext > xContext = NULL ; 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[1] ) ) ; 66*cdf0e10cSrcweir OSL_ENSURE( xManager.is() , 67*cdf0e10cSrcweir "ServicesManager - " 68*cdf0e10cSrcweir "Cannot get service manager" ) ; 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir //Create security environment 71*cdf0e10cSrcweir //Build Security Environment 72*cdf0e10cSrcweir Reference< XInterface > xsecenv = 73*cdf0e10cSrcweir xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ; 74*cdf0e10cSrcweir OSL_ENSURE( xsecenv.is() , 75*cdf0e10cSrcweir "Signer - " 76*cdf0e10cSrcweir "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ; 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ; 79*cdf0e10cSrcweir OSL_ENSURE( xSecEnv.is() , 80*cdf0e10cSrcweir "Signer - " 81*cdf0e10cSrcweir "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ; 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ; 84*cdf0e10cSrcweir OSL_ENSURE( xEnvTunnel.is() , 85*cdf0e10cSrcweir "Signer - " 86*cdf0e10cSrcweir "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ; 89*cdf0e10cSrcweir OSL_ENSURE( pSecEnv != NULL , 90*cdf0e10cSrcweir "Signer - " 91*cdf0e10cSrcweir "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ; 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir //Setup key slot and certDb 94*cdf0e10cSrcweir if( n_hStoreHandle != NULL ) { 95*cdf0e10cSrcweir pSecEnv->setCryptoSlot( n_hStoreHandle ) ; 96*cdf0e10cSrcweir pSecEnv->setCertDb( n_hStoreHandle ) ; 97*cdf0e10cSrcweir } else { 98*cdf0e10cSrcweir pSecEnv->enableDefaultCrypt( sal_True ) ; 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir //Get personal certificate 102*cdf0e10cSrcweir Sequence < Reference< XCertificate > > xPersonalCerts = pSecEnv->getPersonalCertificates() ; 103*cdf0e10cSrcweir OSL_ENSURE( xPersonalCerts.hasElements() , 104*cdf0e10cSrcweir "getPersonalCertificates - " 105*cdf0e10cSrcweir "No personal certificates found\n" ) ; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir Sequence < Reference< XCertificate > > xCertPath ; 108*cdf0e10cSrcweir for( int i = 0; i < xPersonalCerts.getLength(); i ++ ) { 109*cdf0e10cSrcweir //Print the certificate infomation. 110*cdf0e10cSrcweir fprintf( stdout, "\nPersonal Certificate Info\n" ) ; 111*cdf0e10cSrcweir fprintf( stdout, "\tCertificate Issuer[%s]\n", OUStringToOString( xPersonalCerts[i]->getIssuerName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; 112*cdf0e10cSrcweir fprintf( stdout, "\tCertificate Serial Number[%s]\n", OUStringToOString( bigIntegerToNumericString( xPersonalCerts[i]->getSerialNumber() ), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; 113*cdf0e10cSrcweir fprintf( stdout, "\tCertificate Subject[%s]\n", OUStringToOString( xPersonalCerts[i]->getSubjectName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir //build the certificate path 116*cdf0e10cSrcweir xCertPath = pSecEnv->buildCertificatePath( xPersonalCerts[i] ) ; 117*cdf0e10cSrcweir //Print the certificate path. 118*cdf0e10cSrcweir fprintf( stdout, "\tCertificate Path\n" ) ; 119*cdf0e10cSrcweir for( int j = 0; j < xCertPath.getLength(); j ++ ) { 120*cdf0e10cSrcweir fprintf( stdout, "\t\tCertificate Authority Subject[%s]\n", OUStringToOString( xCertPath[j]->getSubjectName(), RTL_TEXTENCODING_ASCII_US ).getStr() ) ; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir //Get the certificate 124*cdf0e10cSrcweir Sequence < sal_Int8 > serial = xPersonalCerts[i]->getSerialNumber() ; 125*cdf0e10cSrcweir Reference< XCertificate > xcert = pSecEnv->getCertificate( xPersonalCerts[i]->getIssuerName(), xPersonalCerts[i]->getSerialNumber() ) ; 126*cdf0e10cSrcweir if( !xcert.is() ) { 127*cdf0e10cSrcweir fprintf( stdout, "The personal certificate is not in the certificate database\n" ) ; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir //Get the certificate characters 131*cdf0e10cSrcweir sal_Int32 chars = pSecEnv->getCertificateCharacters( xPersonalCerts[i] ) ; 132*cdf0e10cSrcweir fprintf( stdout, "The certificate characters are %d\n", chars ) ; 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir //Get the certificate status 135*cdf0e10cSrcweir sal_Int32 validity = pSecEnv->verifyCertificate( xPersonalCerts[i] ) ; 136*cdf0e10cSrcweir fprintf( stdout, "The certificate validities are %d\n", validity ) ; 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir } catch( Exception& e ) { 140*cdf0e10cSrcweir fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ; 141*cdf0e10cSrcweir goto done ; 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir done: 145*cdf0e10cSrcweir if( n_hStoreHandle != NULL ) 146*cdf0e10cSrcweir CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir //xmlSecMSCryptoAppShutdown() ; 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir return 0; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153