1 /** -- C++ Source File -- **/
2 
3 // MARKER(update_precomp.py): autogen include statement, do not remove
4 #include "precompiled_xmlsecurity.hxx"
5 #include <stdio.h>
6 #include "helper.hxx"
7 
8 #include "libxml/tree.h"
9 #include "libxml/parser.h"
10 #ifndef XMLSEC_NO_XSLT
11 #include "libxslt/xslt.h"
12 #endif
13 
14 #include "securityenvironment_mscryptimpl.hxx"
15 
16 #include <xmlsecurity/biginteger.hxx>
17 
18 #include "xmlsec/strings.h"
19 #include "xmlsec/xmltree.h"
20 #include "xmlsec/mscrypto/app.h"
21 
22 #include <rtl/ustring.hxx>
23 
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 ;
35 
36 int SAL_CALL main( int argc, char **argv )
37 {
38 	const char* n_pCertStore ;
39 	HCERTSTORE n_hStoreHandle ;
40 
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 ;
45 	}
46 
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 ;
54 		}
55 	} else {
56 		n_pCertStore = NULL ;
57 		n_hStoreHandle = NULL ;
58 	}
59 	//xmlSecMSCryptoAppInit( n_pCertStore ) ;
60 
61 	try {
62 		Reference< XMultiComponentFactory > xManager = NULL ;
63 		Reference< XComponentContext > xContext = NULL ;
64 
65 		xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[1] ) ) ;
66 		OSL_ENSURE( xManager.is() ,
67 			"ServicesManager - "
68 			"Cannot get service manager" ) ;
69 
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\"" ) ;
77 
78 		Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
79 		OSL_ENSURE( xSecEnv.is() ,
80 			"Signer - "
81 			"Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
82 
83 		Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
84 		OSL_ENSURE( xEnvTunnel.is() ,
85 			"Signer - "
86 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
87 
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\"" ) ;
92 
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 ) ;
99 		}
100 
101 		//Get personal certificate
102 		Sequence < Reference< XCertificate > > xPersonalCerts = pSecEnv->getPersonalCertificates() ;
103 		OSL_ENSURE( xPersonalCerts.hasElements() ,
104 			"getPersonalCertificates - "
105 			"No personal certificates found\n" ) ;
106 
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() ) ;
114 
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() ) ;
121 			}
122 
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" ) ;
128 			}
129 
130 			//Get the certificate characters
131 			sal_Int32 chars = pSecEnv->getCertificateCharacters( xPersonalCerts[i] ) ;
132 			fprintf( stdout, "The certificate characters are %d\n", chars ) ;
133 
134 			//Get the certificate status
135 			sal_Int32 validity = pSecEnv->verifyCertificate( xPersonalCerts[i] ) ;
136 			fprintf( stdout, "The certificate validities are %d\n", validity ) ;
137 
138 		}
139 	} catch( Exception& e ) {
140 		fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
141 		goto done ;
142 	}
143 
144 done:
145 	if( n_hStoreHandle != NULL )
146 		CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
147 
148 	//xmlSecMSCryptoAppShutdown() ;
149 
150 	return 0;
151 }
152 
153