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