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: signer.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>
60 #include <com/sun/star/beans/PropertyValue.hpp>
61 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
62 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
63 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
64 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
65 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
66 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
68 using namespace ::rtl
;
69 using namespace ::cppu
;
70 using namespace ::com::sun::star::uno
;
71 using namespace ::com::sun::star::io
;
72 using namespace ::com::sun::star::ucb
;
73 using namespace ::com::sun::star::beans
;
74 using namespace ::com::sun::star::document
;
75 using namespace ::com::sun::star::lang
;
76 using namespace ::com::sun::star::xml::wrapper
;
77 using namespace ::com::sun::star::xml::crypto
;
80 int SAL_CALL
main( int argc
, char **argv
)
82 CERTCertDBHandle
* certHandle
;
92 Reference
< XUriBinding
> xUriBinding
;
96 fprintf( stderr
, "Usage: %s < CertDir > <file_url of template> <file_url of result> <rdb file>\n\n" , argv
[0] ) ;
100 for( int hhh
= 0 ; hhh
< 10 ; hhh
++ ) {
107 //Init libxml and libxslt libraries
110 xmlLoadExtDtdDefaultValue
= XML_DETECT_IDS
| XML_COMPLETE_ATTRS
;
111 xmlSubstituteEntitiesDefault(1);
113 #ifndef XMLSEC_NO_XSLT
114 xmlIndentTreeOutput
= 1;
115 #endif // XMLSEC_NO_XSLT
118 //Initialize NSPR and NSS
119 PR_Init( PR_SYSTEM_THREAD
, PR_PRIORITY_NORMAL
, 1 ) ;
120 PK11_SetPasswordFunc( PriPK11PasswordFunc
) ;
121 if( NSS_Init( argv
[1] ) != SECSuccess
) {
122 fprintf( stderr
, "### cannot intialize NSS!\n" ) ;
126 certHandle
= CERT_GetDefaultCertDB() ;
127 slot
= PK11_GetInternalKeySlot() ;
129 if( PK11_NeedLogin( slot
) ) {
130 SECStatus nRet
= PK11_Authenticate( slot
, PR_TRUE
, NULL
);
131 if( nRet
!= SECSuccess
) {
132 fprintf( stderr
, "### cannot authehticate the crypto token!\n" ) ;
138 doc
= xmlParseFile( argv
[2] ) ;
139 if( doc
== NULL
|| xmlDocGetRootElement( doc
) == NULL
) {
140 fprintf( stderr
, "### Cannot load template xml document!\n" ) ;
144 //Find the signature template
145 tplNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), xmlSecNodeSignature
, xmlSecDSigNs
) ;
146 if( tplNode
== NULL
) {
147 fprintf( stderr
, "### Cannot find the signature template!\n" ) ;
151 //Find the element with ID attribute
152 //Here we only try to find the "document" node.
153 tarNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), ( xmlChar
* )"document", ( xmlChar
* )"http://openoffice.org/2000/office" ) ;
154 if( tarNode
== NULL
) {
155 tarNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), ( xmlChar
* )"document", NULL
) ;
158 //Find the "id" attrbute in the element
159 if( tarNode
!= NULL
) {
160 if( ( idAttr
= xmlHasProp( tarNode
, ( xmlChar
* )"id" ) ) != NULL
) {
162 } else if( ( idAttr
= xmlHasProp( tarNode
, ( xmlChar
* )"Id" ) ) != NULL
) {
170 if( idAttr
!= NULL
) {
171 idValue
= xmlNodeListGetString( tarNode
->doc
, idAttr
->children
, 1 ) ;
172 if( idValue
== NULL
) {
173 fprintf( stderr
, "### the ID value is NULL!\n" ) ;
177 if( xmlAddID( NULL
, doc
, idValue
, idAttr
) == NULL
) {
178 fprintf( stderr
, "### Can not add the ID value!\n" ) ;
184 //Find the signature reference
185 tarNode
= xmlSecFindNode( tplNode
, xmlSecNodeReference
, xmlSecDSigNs
) ;
186 if( tarNode
== NULL
) {
187 fprintf( stderr
, "### Cannot find the signature reference!\n" ) ;
191 //Find the "URI" attrbute in the reference
192 uriAttr
= xmlHasProp( tarNode
, ( xmlChar
* )"URI" ) ;
193 if( tarNode
== NULL
) {
194 fprintf( stderr
, "### Cannot find URI of the reference!\n" ) ;
198 //Get the "URI" attrbute value
199 uriValue
= xmlNodeListGetString( tarNode
->doc
, uriAttr
->children
, 1 ) ;
200 if( uriValue
== NULL
) {
201 fprintf( stderr
, "### the URI value is NULL!\n" ) ;
205 if( strchr( ( const char* )uriValue
, '/' ) != NULL
&& strchr( ( const char* )uriValue
, '#' ) == NULL
) {
206 fprintf( stdout
, "### Find a stream URI [%s]\n", uriValue
) ;
207 // uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ;
208 uri
= new ::rtl::OUString( ( const sal_Char
* )uriValue
, xmlStrlen( uriValue
), RTL_TEXTENCODING_ASCII_US
) ;
212 fprintf( stdout
, "### Find the URI [%s]\n", OUStringToOString( *uri
, RTL_TEXTENCODING_ASCII_US
).getStr() ) ;
213 Reference
< XInputStream
> xStream
= createStreamFromFile( *uri
) ;
214 if( !xStream
.is() ) {
215 fprintf( stderr
, "### Can not get the URI stream!\n" ) ;
219 xUriBinding
= new OUriBinding( *uri
, xStream
) ;
223 Reference
< XMultiComponentFactory
> xManager
= NULL
;
224 Reference
< XComponentContext
> xContext
= NULL
;
226 xManager
= serviceManager( xContext
, OUString::createFromAscii( "local" ), OUString::createFromAscii( argv
[4] ) ) ;
227 OSL_ENSURE( xManager
.is() ,
229 "Cannot get service manager" ) ;
231 //Create signature template
232 Reference
< XInterface
> element
=
233 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext
) ;
234 OSL_ENSURE( element
.is() ,
236 "Cannot get service instance of \"wrapper.XMLElementWrapper\"" ) ;
238 Reference
< XXMLElementWrapper
> xElement( element
, UNO_QUERY
) ;
239 OSL_ENSURE( xElement
.is() ,
241 "Cannot get interface of \"XXMLElement\" from service \"xsec.XMLElement\"" ) ;
243 Reference
< XUnoTunnel
> xEleTunnel( xElement
, UNO_QUERY
) ;
244 OSL_ENSURE( xEleTunnel
.is() ,
246 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ;
248 XMLElementWrapper_XmlSecImpl
* pElement
= ( XMLElementWrapper_XmlSecImpl
* )xEleTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
249 OSL_ENSURE( pElement
!= NULL
,
251 "Cannot get implementation of \"xsec.XMLElement\"" ) ;
253 //Set signature template
254 pElement
->setNativeElement( tplNode
) ;
256 //Build XML Signature template
257 Reference
< XInterface
> signtpl
=
258 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSignatureTemplate" ) , xContext
) ;
259 OSL_ENSURE( signtpl
.is() ,
261 "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
263 Reference
< XXMLSignatureTemplate
> xTemplate( signtpl
, UNO_QUERY
) ;
264 OSL_ENSURE( xTemplate
.is() ,
266 "Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ;
268 //Import the signature template
269 xTemplate
->setTemplate( xElement
) ;
271 //Import the URI/Stream binding
272 if( xUriBinding
.is() )
273 xTemplate
->setBinding( xUriBinding
) ;
275 //Create security environment
276 //Build Security Environment
277 Reference
< XInterface
> xsecenv
=
278 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext
) ;
279 OSL_ENSURE( xsecenv
.is() ,
281 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
283 Reference
< XSecurityEnvironment
> xSecEnv( xsecenv
, UNO_QUERY
) ;
284 OSL_ENSURE( xSecEnv
.is() ,
286 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
288 //Setup key slot and certDb
289 Reference
< XUnoTunnel
> xEnvTunnel( xsecenv
, UNO_QUERY
) ;
290 OSL_ENSURE( xElement
.is() ,
292 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
294 SecurityEnvironment_NssImpl
* pSecEnv
= ( SecurityEnvironment_NssImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
295 OSL_ENSURE( pSecEnv
!= NULL
,
297 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
299 pSecEnv
->setCryptoSlot( slot
) ;
300 pSecEnv
->setCertDb( certHandle
) ;
302 //Build XML Security Context
303 Reference
< XInterface
> xmlsecctx
=
304 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext
) ;
305 OSL_ENSURE( xsecenv
.is() ,
307 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
309 Reference
< XXMLSecurityContext
> xSecCtx( xmlsecctx
, UNO_QUERY
) ;
310 OSL_ENSURE( xSecCtx
.is() ,
312 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
314 xSecCtx
->setSecurityEnvironment( xSecEnv
) ;
316 //Generate XML signature
317 Reference
< XInterface
> xmlsigner
=
318 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl"), xContext
) ;
319 OSL_ENSURE( xmlsigner
.is() ,
321 "Cannot get service instance of \"xsec.XMLSignature\"" ) ;
323 Reference
< XXMLSignature
> xSigner( xmlsigner
, UNO_QUERY
) ;
324 OSL_ENSURE( xSigner
.is() ,
326 "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
329 xTemplate
= xSigner
->generate( xTemplate
, xSecCtx
) ;
330 OSL_ENSURE( xTemplate
.is() ,
332 "Cannot generate the xml signature" ) ;
333 } catch( Exception
& e
) {
334 fprintf( stderr
, "Error Message: %s\n" , OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() ) ;
338 dstFile
= fopen( argv
[3], "w" ) ;
339 if( dstFile
== NULL
) {
340 fprintf( stderr
, "### Can not open file %s\n", argv
[3] ) ;
345 xmlDocDump( dstFile
, doc
) ;
351 if( dstFile
!= NULL
)
358 PK11_FreeSlot( slot
) ;
363 /* Shutdown libxslt/libxml */
364 #ifndef XMLSEC_NO_XSLT
365 xsltCleanupGlobals();
366 #endif /* XMLSEC_NO_XSLT */