1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
34 #include "libxml/tree.h"
35 #include "libxml/parser.h"
36 #ifndef XMLSEC_NO_XSLT
37 #include "libxslt/xslt.h"
40 #include "securityenvironment_nssimpl.hxx"
41 #include "xmlelementwrapper_xmlsecimpl.hxx"
52 #include "xmlsec/strings.h"
53 #include "xmlsec/xmltree.h"
55 #include <rtl/ustring.hxx>
57 #include <com/sun/star/beans/PropertyValue.hpp>
58 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
59 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
60 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
61 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
62 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
63 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
65 using namespace ::rtl
;
66 using namespace ::cppu
;
67 using namespace ::com::sun::star::uno
;
68 using namespace ::com::sun::star::io
;
69 using namespace ::com::sun::star::ucb
;
70 using namespace ::com::sun::star::beans
;
71 using namespace ::com::sun::star::document
;
72 using namespace ::com::sun::star::lang
;
73 using namespace ::com::sun::star::xml::wrapper
;
74 using namespace ::com::sun::star::xml::crypto
;
77 int SAL_CALL
main( int argc
, char **argv
)
79 CERTCertDBHandle
* certHandle
;
89 Reference
< XUriBinding
> xUriBinding
;
93 fprintf( stderr
, "Usage: %s < CertDir > <file_url of template> <file_url of result> <rdb file>\n\n" , argv
[0] ) ;
97 for( int hhh
= 0 ; hhh
< 10 ; hhh
++ ) {
104 //Init libxml and libxslt libraries
107 xmlLoadExtDtdDefaultValue
= XML_DETECT_IDS
| XML_COMPLETE_ATTRS
;
108 xmlSubstituteEntitiesDefault(1);
110 #ifndef XMLSEC_NO_XSLT
111 xmlIndentTreeOutput
= 1;
112 #endif // XMLSEC_NO_XSLT
115 //Initialize NSPR and NSS
116 PR_Init( PR_SYSTEM_THREAD
, PR_PRIORITY_NORMAL
, 1 ) ;
117 PK11_SetPasswordFunc( PriPK11PasswordFunc
) ;
118 if( NSS_Init( argv
[1] ) != SECSuccess
) {
119 fprintf( stderr
, "### cannot intialize NSS!\n" ) ;
123 certHandle
= CERT_GetDefaultCertDB() ;
124 slot
= PK11_GetInternalKeySlot() ;
126 if( PK11_NeedLogin( slot
) ) {
127 SECStatus nRet
= PK11_Authenticate( slot
, PR_TRUE
, NULL
);
128 if( nRet
!= SECSuccess
) {
129 fprintf( stderr
, "### cannot authehticate the crypto token!\n" ) ;
135 doc
= xmlParseFile( argv
[2] ) ;
136 if( doc
== NULL
|| xmlDocGetRootElement( doc
) == NULL
) {
137 fprintf( stderr
, "### Cannot load template xml document!\n" ) ;
141 //Find the signature template
142 tplNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), xmlSecNodeSignature
, xmlSecDSigNs
) ;
143 if( tplNode
== NULL
) {
144 fprintf( stderr
, "### Cannot find the signature template!\n" ) ;
148 //Find the element with ID attribute
149 //Here we only try to find the "document" node.
150 tarNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), ( xmlChar
* )"document", ( xmlChar
* )"http://openoffice.org/2000/office" ) ;
151 if( tarNode
== NULL
) {
152 tarNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), ( xmlChar
* )"document", NULL
) ;
155 //Find the "id" attrbute in the element
156 if( tarNode
!= NULL
) {
157 if( ( idAttr
= xmlHasProp( tarNode
, ( xmlChar
* )"id" ) ) != NULL
) {
159 } else if( ( idAttr
= xmlHasProp( tarNode
, ( xmlChar
* )"Id" ) ) != NULL
) {
167 if( idAttr
!= NULL
) {
168 idValue
= xmlNodeListGetString( tarNode
->doc
, idAttr
->children
, 1 ) ;
169 if( idValue
== NULL
) {
170 fprintf( stderr
, "### the ID value is NULL!\n" ) ;
174 if( xmlAddID( NULL
, doc
, idValue
, idAttr
) == NULL
) {
175 fprintf( stderr
, "### Can not add the ID value!\n" ) ;
181 //Find the signature reference
182 tarNode
= xmlSecFindNode( tplNode
, xmlSecNodeReference
, xmlSecDSigNs
) ;
183 if( tarNode
== NULL
) {
184 fprintf( stderr
, "### Cannot find the signature reference!\n" ) ;
188 //Find the "URI" attrbute in the reference
189 uriAttr
= xmlHasProp( tarNode
, ( xmlChar
* )"URI" ) ;
190 if( tarNode
== NULL
) {
191 fprintf( stderr
, "### Cannot find URI of the reference!\n" ) ;
195 //Get the "URI" attrbute value
196 uriValue
= xmlNodeListGetString( tarNode
->doc
, uriAttr
->children
, 1 ) ;
197 if( uriValue
== NULL
) {
198 fprintf( stderr
, "### the URI value is NULL!\n" ) ;
202 if( strchr( ( const char* )uriValue
, '/' ) != NULL
&& strchr( ( const char* )uriValue
, '#' ) == NULL
) {
203 fprintf( stdout
, "### Find a stream URI [%s]\n", uriValue
) ;
204 // uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ;
205 uri
= new ::rtl::OUString( ( const sal_Char
* )uriValue
, xmlStrlen( uriValue
), RTL_TEXTENCODING_ASCII_US
) ;
209 fprintf( stdout
, "### Find the URI [%s]\n", OUStringToOString( *uri
, RTL_TEXTENCODING_ASCII_US
).getStr() ) ;
210 Reference
< XInputStream
> xStream
= createStreamFromFile( *uri
) ;
211 if( !xStream
.is() ) {
212 fprintf( stderr
, "### Can not get the URI stream!\n" ) ;
216 xUriBinding
= new OUriBinding( *uri
, xStream
) ;
220 Reference
< XMultiComponentFactory
> xManager
= NULL
;
221 Reference
< XComponentContext
> xContext
= NULL
;
223 xManager
= serviceManager( xContext
, OUString::createFromAscii( "local" ), OUString::createFromAscii( argv
[4] ) ) ;
224 OSL_ENSURE( xManager
.is() ,
226 "Cannot get service manager" ) ;
228 //Create signature template
229 Reference
< XInterface
> element
=
230 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext
) ;
231 OSL_ENSURE( element
.is() ,
233 "Cannot get service instance of \"wrapper.XMLElementWrapper\"" ) ;
235 Reference
< XXMLElementWrapper
> xElement( element
, UNO_QUERY
) ;
236 OSL_ENSURE( xElement
.is() ,
238 "Cannot get interface of \"XXMLElement\" from service \"xsec.XMLElement\"" ) ;
240 Reference
< XUnoTunnel
> xEleTunnel( xElement
, UNO_QUERY
) ;
241 OSL_ENSURE( xEleTunnel
.is() ,
243 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ;
245 XMLElementWrapper_XmlSecImpl
* pElement
= ( XMLElementWrapper_XmlSecImpl
* )xEleTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
246 OSL_ENSURE( pElement
!= NULL
,
248 "Cannot get implementation of \"xsec.XMLElement\"" ) ;
250 //Set signature template
251 pElement
->setNativeElement( tplNode
) ;
253 //Build XML Signature template
254 Reference
< XInterface
> signtpl
=
255 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.crypto.XMLSignatureTemplate" ) , xContext
) ;
256 OSL_ENSURE( signtpl
.is() ,
258 "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
260 Reference
< XXMLSignatureTemplate
> xTemplate( signtpl
, UNO_QUERY
) ;
261 OSL_ENSURE( xTemplate
.is() ,
263 "Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ;
265 //Import the signature template
266 xTemplate
->setTemplate( xElement
) ;
268 //Import the URI/Stream binding
269 if( xUriBinding
.is() )
270 xTemplate
->setBinding( xUriBinding
) ;
272 //Create security environment
273 //Build Security Environment
274 Reference
< XInterface
> xsecenv
=
275 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext
) ;
276 OSL_ENSURE( xsecenv
.is() ,
278 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
280 Reference
< XSecurityEnvironment
> xSecEnv( xsecenv
, UNO_QUERY
) ;
281 OSL_ENSURE( xSecEnv
.is() ,
283 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
285 //Setup key slot and certDb
286 Reference
< XUnoTunnel
> xEnvTunnel( xsecenv
, UNO_QUERY
) ;
287 OSL_ENSURE( xElement
.is() ,
289 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
291 SecurityEnvironment_NssImpl
* pSecEnv
= ( SecurityEnvironment_NssImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
292 OSL_ENSURE( pSecEnv
!= NULL
,
294 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
296 pSecEnv
->setCryptoSlot( slot
) ;
297 pSecEnv
->setCertDb( certHandle
) ;
299 //Build XML Security Context
300 Reference
< XInterface
> xmlsecctx
=
301 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext
) ;
302 OSL_ENSURE( xsecenv
.is() ,
304 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
306 Reference
< XXMLSecurityContext
> xSecCtx( xmlsecctx
, UNO_QUERY
) ;
307 OSL_ENSURE( xSecCtx
.is() ,
309 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
311 xSecCtx
->setSecurityEnvironment( xSecEnv
) ;
313 //Generate XML signature
314 Reference
< XInterface
> xmlsigner
=
315 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_NssImpl"), xContext
) ;
316 OSL_ENSURE( xmlsigner
.is() ,
318 "Cannot get service instance of \"xsec.XMLSignature\"" ) ;
320 Reference
< XXMLSignature
> xSigner( xmlsigner
, UNO_QUERY
) ;
321 OSL_ENSURE( xSigner
.is() ,
323 "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
326 xTemplate
= xSigner
->generate( xTemplate
, xSecCtx
) ;
327 OSL_ENSURE( xTemplate
.is() ,
329 "Cannot generate the xml signature" ) ;
330 } catch( Exception
& e
) {
331 fprintf( stderr
, "Error Message: %s\n" , OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() ) ;
335 dstFile
= fopen( argv
[3], "w" ) ;
336 if( dstFile
== NULL
) {
337 fprintf( stderr
, "### Can not open file %s\n", argv
[3] ) ;
342 xmlDocDump( dstFile
, doc
) ;
348 if( dstFile
!= NULL
)
355 PK11_FreeSlot( slot
) ;
360 /* Shutdown libxslt/libxml */
361 #ifndef XMLSEC_NO_XSLT
362 xsltCleanupGlobals();
363 #endif /* XMLSEC_NO_XSLT */