1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: verifier.cxx,v $
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"
37 #include "libxml/tree.h"
38 #include "libxml/parser.h"
39 #ifndef XMLSEC_NO_XSLT
40 #include "libxslt/xslt.h"
43 #include "securityenvironment_nssimpl.hxx"
44 #include "xmlelementwrapper_xmlsecimpl.hxx"
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
;
94 OUString
* uri
= NULL
;
95 Reference
< XUriBinding
> xUriBinding
;
98 fprintf( stderr
, "Usage: %s < CertDir > <file_url> <rdb file>\n" , argv
[0] ) ;
102 //Init libxml and libxslt libraries
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" ) ;
121 certHandle
= CERT_GetDefaultCertDB() ;
122 slot
= PK11_GetInternalKeySlot() ;
125 doc
= xmlParseFile( argv
[2] ) ;
126 if( doc
== NULL
|| xmlDocGetRootElement( doc
) == NULL
) {
127 fprintf( stderr
, "### Cannot load template xml document!\n" ) ;
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" ) ;
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
) {
148 } else if( ( idAttr
= xmlHasProp( tarNode
, ( xmlChar
* )"Id" ) ) != NULL
) {
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" ) ;
163 if( xmlAddID( NULL
, doc
, idValue
, idAttr
) == NULL
) {
164 fprintf( stderr
, "### Can not add the ID value!\n" ) ;
170 //Find the signature reference
171 tarNode
= xmlSecFindNode( tplNode
, xmlSecNodeReference
, xmlSecDSigNs
) ;
172 if( tarNode
== NULL
) {
173 fprintf( stderr
, "### Cannot find the signature reference!\n" ) ;
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" ) ;
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" ) ;
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
) ;
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" ) ;
205 xUriBinding
= new OUriBinding( *uri
, xStream
) ;
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() ,
220 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
222 Reference
< XXMLElementWrapper
> xElement( element
, UNO_QUERY
) ;
223 OSL_ENSURE( xElement
.is() ,
225 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
227 Reference
< XUnoTunnel
> xEleTunnel( xElement
, UNO_QUERY
) ;
228 OSL_ENSURE( xEleTunnel
.is() ,
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
,
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() ,
245 "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
247 Reference
< XXMLSignatureTemplate
> xTemplate( signtpl
, UNO_QUERY
) ;
248 OSL_ENSURE( xTemplate
.is() ,
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() ,
265 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
267 Reference
< XSecurityEnvironment
> xSecEnv( xsecenv
, UNO_QUERY
) ;
268 OSL_ENSURE( xSecEnv
.is() ,
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() ,
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
,
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() ,
291 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
293 Reference
< XXMLSecurityContext
> xSecCtx( xmlsecctx
, UNO_QUERY
) ;
294 OSL_ENSURE( xSecCtx
.is() ,
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() ,
305 "Cannot get service instance of \"xsec.XMLSignature\"" ) ;
307 Reference
< XXMLSignature
> xSigner( xmlsigner
, UNO_QUERY
) ;
308 OSL_ENSURE( xSigner
.is() ,
310 "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
314 sal_Bool valid
= xSigner
->validate( xTemplate
, xSecCtx
) ;
316 printf( "Signature is INVALID!\n" ) ;
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() ) ;
330 PK11_FreeSlot( slot
) ;
335 /* Shutdown libxslt/libxml */
336 #ifndef XMLSEC_NO_XSLT
337 xsltCleanupGlobals();
338 #endif /* XMLSEC_NO_XSLT */