merge the formfield patch from ooo-build
[ooovba.git] / xmlsecurity / tools / standalone / csfit / signer.cxx
blob24e1764cbfbe15d56f67227bccbe5c66926b7b9b
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: signer.cxx,v $
10 * $Revision: 1.6 $
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>
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 ;
83 PK11SlotInfo* slot ;
84 xmlDocPtr doc ;
85 xmlNodePtr tplNode ;
86 xmlNodePtr tarNode ;
87 xmlAttrPtr idAttr ;
88 xmlChar* idValue ;
89 xmlAttrPtr uriAttr ;
90 xmlChar* uriValue ;
91 OUString* uri ;
92 Reference< XUriBinding > xUriBinding ;
93 FILE* dstFile ;
95 if( argc != 5 ) {
96 fprintf( stderr, "Usage: %s < CertDir > <file_url of template> <file_url of result> <rdb file>\n\n" , argv[0] ) ;
97 return 1 ;
100 for( int hhh = 0 ; hhh < 10 ; hhh ++ ) {
101 slot = NULL ;
102 doc = NULL ;
103 uri = NULL ;
104 dstFile = NULL ;
107 //Init libxml and libxslt libraries
108 xmlInitParser();
109 LIBXML_TEST_VERSION
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" ) ;
123 goto done ;
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" ) ;
133 goto done ;
137 //Load XML document
138 doc = xmlParseFile( argv[2] ) ;
139 if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
140 fprintf( stderr , "### Cannot load template xml document!\n" ) ;
141 goto done ;
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" ) ;
148 goto done ;
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 ) {
161 //NULL
162 } else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) {
163 //NULL
164 } else {
165 idAttr = NULL ;
169 //Add ID to DOM
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" ) ;
174 goto done ;
177 if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) {
178 fprintf( stderr , "### Can not add the ID value!\n" ) ;
179 goto done ;
183 //Reference handler
184 //Find the signature reference
185 tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ;
186 if( tarNode == NULL ) {
187 fprintf( stderr , "### Cannot find the signature reference!\n" ) ;
188 goto done ;
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" ) ;
195 goto done ;
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" ) ;
202 goto done ;
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 ) ;
211 if( uri != NULL ) {
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" ) ;
216 goto done ;
219 xUriBinding = new OUriBinding( *uri, xStream ) ;
222 try {
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() ,
228 "ServicesManager - "
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() ,
235 "Signer - "
236 "Cannot get service instance of \"wrapper.XMLElementWrapper\"" ) ;
238 Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ;
239 OSL_ENSURE( xElement.is() ,
240 "Signer - "
241 "Cannot get interface of \"XXMLElement\" from service \"xsec.XMLElement\"" ) ;
243 Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ;
244 OSL_ENSURE( xEleTunnel.is() ,
245 "Signer - "
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 ,
250 "Signer - "
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() ,
260 "Signer - "
261 "Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
263 Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ;
264 OSL_ENSURE( xTemplate.is() ,
265 "Signer - "
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() ,
280 "Signer - "
281 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
283 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
284 OSL_ENSURE( xSecEnv.is() ,
285 "Signer - "
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() ,
291 "Signer - "
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 ,
296 "Signer - "
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() ,
306 "Signer - "
307 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
309 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
310 OSL_ENSURE( xSecCtx.is() ,
311 "Signer - "
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() ,
320 "Signer - "
321 "Cannot get service instance of \"xsec.XMLSignature\"" ) ;
323 Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ;
324 OSL_ENSURE( xSigner.is() ,
325 "Signer - "
326 "Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
328 //perform signature
329 xTemplate = xSigner->generate( xTemplate , xSecCtx ) ;
330 OSL_ENSURE( xTemplate.is() ,
331 "Signer - "
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() ) ;
335 goto done ;
338 dstFile = fopen( argv[3], "w" ) ;
339 if( dstFile == NULL ) {
340 fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
341 goto done ;
344 //Save result
345 xmlDocDump( dstFile, doc ) ;
347 done:
348 if( uri != NULL )
349 delete uri ;
351 if( dstFile != NULL )
352 fclose( dstFile ) ;
354 if( doc != NULL )
355 xmlFreeDoc( doc ) ;
357 if( slot != NULL )
358 PK11_FreeSlot( slot ) ;
360 PK11_LogoutAll() ;
361 NSS_Shutdown() ;
363 /* Shutdown libxslt/libxml */
364 #ifndef XMLSEC_NO_XSLT
365 xsltCleanupGlobals();
366 #endif /* XMLSEC_NO_XSLT */
367 xmlCleanupParser();
371 return 0;