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