1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 /** -- C++ Source File -- **/
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmlsecurity.hxx"
26 #include <stdio.h>
27 #include "helper.hxx"
28 
29 #include "libxml/tree.h"
30 #include "libxml/parser.h"
31 #ifndef XMLSEC_NO_XSLT
32 #include "libxslt/xslt.h"
33 #endif
34 
35 #include "securityenvironment_mscryptimpl.hxx"
36 #include "xmlelementwrapper_xmlsecimpl.hxx"
37 
38 #include "xmlsec/strings.h"
39 #include "xmlsec/mscrypto/app.h"
40 #include "xmlsec/xmltree.h"
41 
42 #include <rtl/ustring.hxx>
43 #include <cppuhelper/servicefactory.hxx>
44 
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/beans/PropertyValue.hpp>
47 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
48 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
49 #include <com/sun/star/xml/crypto/XXMLSignature.hpp>
50 #include <com/sun/star/xml/crypto/XXMLSignatureTemplate.hpp>
51 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
52 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
53 
54 using namespace ::rtl ;
55 using namespace ::cppu ;
56 using namespace ::com::sun::star::uno ;
57 using namespace ::com::sun::star::io ;
58 using namespace ::com::sun::star::ucb ;
59 using namespace ::com::sun::star::beans ;
60 using namespace ::com::sun::star::document ;
61 using namespace ::com::sun::star::lang ;
62 using namespace ::com::sun::star::registry ;
63 using namespace ::com::sun::star::xml::wrapper ;
64 using namespace ::com::sun::star::xml::crypto ;
65 
66 
main(int argc,char ** argv)67 int SAL_CALL main( int argc, char **argv )
68 {
69 	const char* 		n_pCertStore ;
70 	HCERTSTORE			n_hStoreHandle ;
71 
72 	xmlDocPtr			doc = NULL ;
73 	xmlNodePtr			tplNode ;
74 	xmlNodePtr			tarNode ;
75 	xmlAttrPtr			idAttr ;
76 	xmlChar*			idValue ;
77 	xmlAttrPtr			uriAttr ;
78 	xmlChar*			uriValue ;
79 	OUString*			uri = NULL ;
80 	Reference< XUriBinding >	xUriBinding ;
81 	FILE*				dstFile = NULL ;
82 
83 	if( argc !=3 && argc != 4 ) {
84 		fprintf( stderr, "Usage: %s <file_url> <rdb file>\n" , argv[0] ) ;
85 		fprintf( stderr, "Or: \t%s <file_url> <rdb file> < Cert Store Name >\n\n" , argv[0] ) ;
86 		return 1 ;
87 	}
88 
89 	for( int hhh = 0 ; hhh < 1 ; hhh ++ ) {
90 
91 	//Init libxml and libxslt libraries
92 	xmlInitParser();
93 	LIBXML_TEST_VERSION
94 	xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
95 	xmlSubstituteEntitiesDefault(1);
96 
97 	#ifndef XMLSEC_NO_XSLT
98 	xmlIndentTreeOutput = 1;
99 	#endif // XMLSEC_NO_XSLT
100 
101 	//Initialize the crypto engine
102 	if( argc == 4 ) {
103 		n_pCertStore = argv[3] ;
104 		n_hStoreHandle = CertOpenSystemStore( NULL, n_pCertStore ) ;
105 		if( n_hStoreHandle == NULL ) {
106 			fprintf( stderr, "Can not open the system cert store %s\n", n_pCertStore ) ;
107 			return 1 ;
108 		}
109 	} else {
110 		n_pCertStore = NULL ;
111 		n_hStoreHandle = NULL ;
112 	}
113 	xmlSecMSCryptoAppInit( n_pCertStore ) ;
114 
115 	//Load XML document
116 	doc = xmlParseFile( argv[1] ) ;
117 	if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
118 		fprintf( stderr , "### Cannot load template xml document!\n" ) ;
119 		goto done ;
120 	}
121 
122 	//Find the signature template
123 	tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeSignature, xmlSecDSigNs ) ;
124 	if( tplNode == NULL ) {
125 		fprintf( stderr , "### Cannot find the signature template!\n" ) ;
126 		goto done ;
127 	}
128 
129 	//Find the element with ID attribute
130 	tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", ( xmlChar* )"http://openoffice.org/2000/office" ) ;
131 	if( tarNode == NULL ) {
132 		tarNode = xmlSecFindNode( xmlDocGetRootElement( doc ), ( xmlChar* )"document", NULL ) ;
133 	}
134 
135 	//Find the "id" attrbute in the element
136 	if( tarNode != NULL ) {
137 		if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"id" ) ) != NULL ) {
138 			//NULL
139 		} else if( ( idAttr = xmlHasProp( tarNode, ( xmlChar* )"Id" ) ) != NULL ) {
140 			//NULL
141 		} else {
142 			idAttr = NULL ;
143 		}
144 	}
145 
146 	//Add ID to DOM
147 	if( idAttr != NULL ) {
148 		idValue = xmlNodeListGetString( tarNode->doc, idAttr->children, 1 ) ;
149 		if( idValue == NULL ) {
150 			fprintf( stderr , "### the ID value is NULL!\n" ) ;
151 			goto done ;
152 		}
153 
154 		if( xmlAddID( NULL, doc, idValue, idAttr ) == NULL ) {
155 			fprintf( stderr , "### Can not add the ID value!\n" ) ;
156 			goto done ;
157 		}
158 	}
159 
160 	//Reference handler
161 	//Find the signature reference
162 	tarNode = xmlSecFindNode( tplNode, xmlSecNodeReference, xmlSecDSigNs ) ;
163 	if( tarNode == NULL ) {
164 		fprintf( stderr , "### Cannot find the signature reference!\n" ) ;
165 		goto done ;
166 	}
167 
168 	//Find the "URI" attrbute in the reference
169 	uriAttr = xmlHasProp( tarNode, ( xmlChar* )"URI" ) ;
170 	if( tarNode == NULL ) {
171 		fprintf( stderr , "### Cannot find URI of the reference!\n" ) ;
172 		goto done ;
173 	}
174 
175 	//Get the "URI" attrbute value
176 	uriValue = xmlNodeListGetString( tarNode->doc, uriAttr->children, 1 ) ;
177 	if( uriValue == NULL ) {
178 		fprintf( stderr , "### the URI value is NULL!\n" ) ;
179 		goto done ;
180 	}
181 
182 	if( strchr( ( const char* )uriValue, '/' ) != NULL && strchr( ( const char* )uriValue, '#' ) == NULL ) {
183 		fprintf( stdout , "### Find a stream URI [%s]\n", uriValue ) ;
184 	//	uri = new ::rtl::OUString( ( const sal_Unicode* )uriValue ) ;
185 		uri = new ::rtl::OUString( ( const sal_Char* )uriValue, xmlStrlen( uriValue ), RTL_TEXTENCODING_ASCII_US ) ;
186 	}
187 
188 	if( uri != NULL ) {
189 		fprintf( stdout , "### Find the URI [%s]\n", OUStringToOString( *uri , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
190 		Reference< XInputStream > xStream = createStreamFromFile( *uri ) ;
191 		if( !xStream.is() ) {
192 			fprintf( stderr , "### Can not get the URI stream!\n" ) ;
193 			goto done ;
194 		}
195 
196 		xUriBinding = new OUriBinding( *uri, xStream ) ;
197 	}
198 
199 
200 	try {
201 		Reference< XMultiComponentFactory > xManager = NULL ;
202 		Reference< XComponentContext > xContext = NULL ;
203 
204 		xManager = serviceManager( xContext , OUString::createFromAscii( "local" ),  OUString::createFromAscii( argv[2] ) ) ;
205 
206 		//Create signature template
207 		Reference< XInterface > element =
208 			xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext ) ;
209 		OSL_ENSURE( element.is() ,
210 			"Verifier - "
211 			"Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
212 
213 		Reference< XXMLElementWrapper > xElement( element , UNO_QUERY ) ;
214 		OSL_ENSURE( xElement.is() ,
215 			"Verifier - "
216 			"Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
217 
218 		Reference< XUnoTunnel > xEleTunnel( xElement , UNO_QUERY ) ;
219 		OSL_ENSURE( xEleTunnel.is() ,
220 			"Verifier - "
221 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElement\"" ) ;
222 
223 		XMLElementWrapper_XmlSecImpl* pElement = ( XMLElementWrapper_XmlSecImpl* )xEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
224 		OSL_ENSURE( pElement != NULL ,
225 			"Verifier - "
226 			"Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
227 
228 		//Set signature template
229 		pElement->setNativeElement( tplNode ) ;
230 
231 		//Build XML Signature template
232 		Reference< XInterface > signtpl =
233 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLSignatureTemplate"), xContext ) ;
234 		OSL_ENSURE( signtpl.is() ,
235 			"Verifier - "
236 			"Cannot get service instance of \"xsec.XMLSignatureTemplate\"" ) ;
237 
238 		Reference< XXMLSignatureTemplate > xTemplate( signtpl , UNO_QUERY ) ;
239 		OSL_ENSURE( xTemplate.is() ,
240 			"Verifier - "
241 			"Cannot get interface of \"XXMLSignatureTemplate\" from service \"xsec.XMLSignatureTemplate\"" ) ;
242 
243 		//Import the signature template
244 		xTemplate->setTemplate( xElement ) ;
245 
246 		//Import the URI/Stream binding
247 		if( xUriBinding.is() )
248 			xTemplate->setBinding( xUriBinding ) ;
249 
250 		//Create security environment
251 		//Build Security Environment
252 		Reference< XInterface > xsecenv =
253 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_MSCryptImpl"), xContext ) ;
254 		OSL_ENSURE( xsecenv.is() ,
255 			"Verifier - "
256 			"Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
257 
258 		Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
259 		OSL_ENSURE( xSecEnv.is() ,
260 			"Verifier - "
261 			"Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
262 
263 		//Setup key slot and certDb
264 		Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
265 		OSL_ENSURE( xElement.is() ,
266 			"Verifier - "
267 			"Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
268 
269 		SecurityEnvironment_MSCryptImpl* pSecEnv = ( SecurityEnvironment_MSCryptImpl* )xEnvTunnel->getSomething( SecurityEnvironment_MSCryptImpl::getUnoTunnelId() ) ;
270 		OSL_ENSURE( pSecEnv != NULL ,
271 			"Verifier - "
272 			"Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
273 
274 		//Setup key slot and certDb
275 		if( n_hStoreHandle != NULL ) {
276 			pSecEnv->setCryptoSlot( n_hStoreHandle ) ;
277 			pSecEnv->setCertDb( n_hStoreHandle ) ;
278 		} else {
279 			pSecEnv->enableDefaultCrypt( sal_True ) ;
280 		}
281 
282 		//Build XML Security Context
283 		Reference< XInterface > xmlsecctx =
284 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_MSCryptImpl"), xContext ) ;
285 		OSL_ENSURE( xsecenv.is() ,
286 			"Verifier - "
287 			"Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
288 
289 		Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
290 		OSL_ENSURE( xSecCtx.is() ,
291 			"Verifier - "
292 			"Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
293 
294 		xSecCtx->addSecurityEnvironment( xSecEnv ) ;
295 
296 		//Generate XML signature
297 		Reference< XInterface > xmlsigner =
298 			xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_MSCryptImpl"), xContext ) ;
299 		OSL_ENSURE( xmlsigner.is() ,
300 			"Verifier - "
301 			"Cannot get service instance of \"xsec.XMLSignature\"" ) ;
302 
303 		Reference< XXMLSignature > xSigner( xmlsigner , UNO_QUERY ) ;
304 		OSL_ENSURE( xSigner.is() ,
305 			"Verifier - "
306 			"Cannot get interface of \"XXMLSignature\" from service \"xsec.XMLSignature\"" ) ;
307 
308 
309 		//perform validation
310 		xTemplate = xSigner->validate( xTemplate , xSecCtx ) ;
311 
312 		com::sun::star::xml::crypto::SecurityOperationStatus m_nStatus = xTemplate->getStatus();
313 
314 		if (m_nStatus == SecurityOperationStatus_OPERATION_SUCCEEDED)
315 		{
316 			fprintf( stdout, "Operation succeeds.\n") ;
317 		}
318 		else
319 		{
320 			fprintf( stdout, "Operation fails.\n") ;
321 		}
322 	} catch( Exception& e ) {
323 		fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
324 		goto done ;
325 	}
326 
327 done :
328 	if( uri != NULL )
329 		delete uri ;
330 
331 	if( doc != NULL )
332 		xmlFreeDoc( doc ) ;
333 
334 	if( n_hStoreHandle != NULL )
335 		CertCloseStore( n_hStoreHandle, CERT_CLOSE_STORE_FORCE_FLAG ) ;
336 
337 	xmlSecMSCryptoAppShutdown() ;
338 
339 	/* Shutdown libxslt/libxml */
340 	#ifndef XMLSEC_NO_XSLT
341 	xsltCleanupGlobals();
342 	#endif /* XMLSEC_NO_XSLT */
343 	xmlCleanupParser();
344 
345 	}
346 
347 	return 0 ;
348 }
349 
350