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