merge the formfield patch from ooo-build
[ooovba.git] / xmlsecurity / tools / standalone / csfit / verifier.cxx
blob32895b4519e8208e262d7402013247dfd39c18d1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: verifier.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <stdio.h>
35 #include "helper.hxx"
37 #include "libxml/tree.h"
38 #include "libxml/parser.h"
39 #ifndef XMLSEC_NO_XSLT
40 #include "libxslt/xslt.h"
41 #endif
43 #include "securityenvironment_nssimpl.hxx"
44 #include "xmlelementwrapper_xmlsecimpl.hxx"
46 #include "nspr.h"
47 #include "prtypes.h"
49 #include "pk11func.h"
50 #include "cert.h"
51 #include "cryptohi.h"
52 #include "certdb.h"
53 #include "nss.h"
55 #include "xmlsec/strings.h"
56 #include "xmlsec/xmltree.h"
58 #include <rtl/ustring.hxx>
59 #include <cppuhelper/servicefactory.hxx>
61 #include <com/sun/star/lang/XComponent.hpp>
62 #include <com/sun/star/beans/PropertyValue.hpp>
63 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
64 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
65 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
66 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
67 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
68 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
70 using namespace ::rtl ;
71 using namespace ::cppu ;
72 using namespace ::com::sun::star::uno ;
73 using namespace ::com::sun::star::io ;
74 using namespace ::com::sun::star::ucb ;
75 using namespace ::com::sun::star::beans ;
76 using namespace ::com::sun::star::document ;
77 using namespace ::com::sun::star::lang ;
78 using namespace ::com::sun::star::registry ;
79 using namespace ::com::sun::star::xml::wrapper ;
80 using namespace ::com::sun::star::xml::crypto ;
83 int SAL_CALL main( int argc, char **argv )
85 CERTCertDBHandle* certHandle = NULL ;
86 PK11SlotInfo* slot = NULL ;
87 xmlDocPtr doc = NULL ;
88 xmlNodePtr tplNode ;
89 xmlNodePtr tarNode ;
90 xmlAttrPtr idAttr ;
91 xmlChar* idValue ;
92 xmlAttrPtr uriAttr ;
93 xmlChar* uriValue ;
94 OUString* uri = NULL ;
95 Reference< XUriBinding > xUriBinding ;
97 if( argc != 4 ) {
98 fprintf( stderr, "Usage: %s < CertDir > <file_url> <rdb file>\n" , argv[0] ) ;
99 return 1 ;
102 //Init libxml and libxslt libraries
103 xmlInitParser();
104 LIBXML_TEST_VERSION
105 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
106 xmlSubstituteEntitiesDefault(1);
108 #ifndef XMLSEC_NO_XSLT
109 xmlIndentTreeOutput = 1;
110 #endif // XMLSEC_NO_XSLT
113 //Initialize NSPR and NSS
114 PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
115 PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
116 if( NSS_Init( argv[1] ) != SECSuccess ) {
117 fprintf( stderr , "### cannot intialize NSS!\n" ) ;
118 return 1 ;
121 certHandle = CERT_GetDefaultCertDB() ;
122 slot = PK11_GetInternalKeySlot() ;
124 //Load XML document
125 doc = xmlParseFile( argv[2] ) ;
126 if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
127 fprintf( stderr , "### Cannot load template xml document!\n" ) ;
128 goto done ;
131 //Find the signature template
132 tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ;
133 if( tplNode == NULL ) {
134 fprintf( stderr , "### Cannot find the signature template!\n" ) ;
135 goto done ;
138 //Find the element with ID attribute
139 tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ;
140 if( tarNode == NULL ) {
141 tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ;
144 //Find the "id" attrbute in the element
145 if( tarNode != NULL ) {
146 if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) {
147 //NULL
148 } else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) {
149 //NULL
150 } else {
151 idAttr = NULL ;
155 //Add ID to DOM
156 if( idAttr != NULL ) {
157 idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ;
158 if( idValue == NULL ) {
159 fprintf( stderr , "### the ID value is NULL!\n" ) ;
160 goto done ;
163 if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) {
164 fprintf( stderr , "### Can not add the ID value!\n" ) ;
165 goto done ;
169 //Reference handler
170 //Find the signature reference
171 tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ;
172 if( tarNode == NULL ) {
173 fprintf( stderr , "### Cannot find the signature reference!\n" ) ;
174 goto done ;
177 //Find the "URI" attrbute in the reference
178 uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ;
179 if( tarNode == NULL ) {
180 fprintf( stderr , "### Cannot find URI of the reference!\n" ) ;
181 goto done ;
184 //Get the "URI" attrbute value
185 uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ;
186 if( uriValue == NULL ) {
187 fprintf( stderr , "### the URI value is NULL!\n" ) ;
188 goto done ;
191 if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) {
192 fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ;
193 // uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ;
194 uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ;
197 if( uri != NULL ) {
198 fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
199 Reference< XInputStream > xStream = createStreamFromFile( *uri ) ;
200 if( !xStream.is() ) {
201 fprintf( stderr , "### Can not get the URI stream!\n" ) ;
202 goto done ;
205 xUriBinding = new OUriBinding( *uri, xStream ) ;
209 try {
210 Reference< XMultiComponentFactory > xManager = NULL ;
211 Reference< XComponentContext > xContext = NULL ;
213 xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[3] ) ) ;
215 //Create signature template
216 Reference< XInterface > element =
217 xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
218 OSL_ENSURE( element.is() ,
219 "Verifier - "
220 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
222 Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ;
223 OSL_ENSURE( xElement.is() ,
224 "Verifier - "
225 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
227 Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ;
228 OSL_ENSURE( xEleTunnel.is() ,
229 "Verifier - "
230 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ;
232 XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
233 OSL_ENSURE( pElement != NULL ,
234 "Verifier - "
235 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
237 //Set signature template
238 pElement->setNativeElement( tplNode ) ;
240 //Build XML Signature template
241 Reference< XInterface > signtpl =
242 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLSignatureTemplate"), xContext ) ;
243 OSL_ENSURE( signtpl.is() ,
244 "Verifier - "
245 "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
247 Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ;
248 OSL_ENSURE( xTemplate.is() ,
249 "Verifier - "
250 "Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ;
252 //Import the signature template
253 xTemplate->setTemplate( xElement ) ;
255 //Import the URI/Stream binding
256 if( xUriBinding.is() )
257 xTemplate->setBinding( xUriBinding ) ;
259 //Create security environment
260 //Build Security Environment
261 Reference< XInterface > xsecenv =
262 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext ) ;
263 OSL_ENSURE( xsecenv.is() ,
264 "Verifier - "
265 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
267 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
268 OSL_ENSURE( xSecEnv.is() ,
269 "Verifier - "
270 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
272 //Setup key slot and certDb
273 Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
274 OSL_ENSURE( xElement.is() ,
275 "Verifier - "
276 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
278 SecurityEnvironment_NssImpl* pSecEnv = ( SecurityEnvironment_NssImpl* )xEnvTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
279 OSL_ENSURE( pSecEnv != NULL ,
280 "Verifier - "
281 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
283 pSecEnv->setCryptoSlot( slot ) ;
284 pSecEnv->setCertDb( certHandle ) ;
286 //Build XML Security Context
287 Reference< XInterface > xmlsecctx =
288 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext ) ;
289 OSL_ENSURE( xsecenv.is() ,
290 "Verifier - "
291 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
293 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
294 OSL_ENSURE( xSecCtx.is() ,
295 "Verifier - "
296 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
298 xSecCtx->setSecurityEnvironment( xSecEnv ) ;
300 //Generate XML signature
301 Reference< XInterface > xmlsigner =
302 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl"), xContext ) ;
303 OSL_ENSURE( xmlsigner.is() ,
304 "Verifier - "
305 "Cannot get service instance of \"xsec.XMLSignature\"" ) ;
307 Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ;
308 OSL_ENSURE( xSigner.is() ,
309 "Verifier - "
310 "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
313 //perform validation
314 sal_Bool valid = xSigner->validate( xTemplate , xSecCtx ) ;
315 if( !valid ) {
316 printf( "Signature is INVALID!\n" ) ;
317 } else {
318 printf( "Signature is VALID!\n" ) ;
320 } catch( Exception& e ) {
321 fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
322 goto done ;
325 done :
326 if( doc != NULL )
327 xmlFreeDoc( doc ) ;
329 if( slot != NULL )
330 PK11_FreeSlot( slot ) ;
332 PK11_LogoutAll() ;
333 NSS_Shutdown() ;
335 /* Shutdown libxslt/libxml */
336 #ifndef XMLSEC_NO_XSLT
337 xsltCleanupGlobals();
338 #endif /* XMLSEC_NO_XSLT */
339 xmlCleanupParser();
341 return 0 ;