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