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
36 #include "securityenvironment_mscryptimpl.hxx"
37 #include "xmlelementwrapper_xmlsecimpl.hxx"
38
39 #include "nspr.h"
40 #include "prtypes.h"
41
42 #include "pk11func.h"
43 #include "cert.h"
44 #include "cryptohi.h"
45 #include "certdb.h"
46 #include "nss.h"
47
48 #include "xmlsec/strings.h"
49 #include "xmlsec/xmltree.h"
50
51 #include <rtl/ustring.hxx>
52 #include <cppuhelper/bootstrap.hxx>
53 #include <cppuhelper/servicefactory.hxx>
54
55 #include <com/sun/star/beans/PropertyValue.hpp>
56 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
57 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
58 #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
59 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
60 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
61 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
62
63
64 using namespace ::rtl ;
65 using namespace ::cppu ;
66 using namespace ::com::sun::star::uno ;
67 using namespace ::com::sun::star::io ;
68 using namespace ::com::sun::star::ucb ;
69 using namespace ::com::sun::star::beans ;
70 using namespace ::com::sun::star::document ;
71 using namespace ::com::sun::star::lang ;
72 using namespace ::com::sun::star::registry ;
73 using namespace ::com::sun::star::xml::wrapper ;
74 using namespace ::com::sun::star::xml::crypto ;
75
76
main(int argc,char ** argv)77 int SAL_CALL main( int argc, char **argv )
78 {
79 CERTCertDBHandle* certHandle = NULL ;
80 PK11SlotInfo* slot = NULL ;
81 xmlDocPtr doc = NULL ;
82 xmlNodePtr tplNode ;
83 xmlNodePtr tarNode ;
84 FILE* dstFile = NULL ;
85
86
87 if( argc != 5 ) {
88 fprintf( stderr, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv[0] ) ;
89 return 1 ;
90 }
91
92 //Init libxml and libxslt libraries
93 xmlInitParser();
94 LIBXML_TEST_VERSION
95 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
96 xmlSubstituteEntitiesDefault(1);
97
98 #ifndef XMLSEC_NO_XSLT
99 xmlIndentTreeOutput = 1;
100 #endif // XMLSEC_NO_XSLT
101
102
103 //Initialize NSPR and NSS
104 PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1 ) ;
105 PK11_SetPasswordFunc( PriPK11PasswordFunc ) ;
106 if( NSS_Init( argv[1] ) != SECSuccess ) {
107 fprintf( stderr , "### cannot initialize NSS!\n" ) ;
108 goto done ;
109 }
110
111 certHandle = CERT_GetDefaultCertDB() ;
112 slot = PK11_GetInternalKeySlot() ;
113
114 //Load XML document
115 doc = xmlParseFile( argv[2] ) ;
116 if( doc == NULL || xmlDocGetRootElement( doc ) == NULL ) {
117 fprintf( stderr , "### Cannot load template xml document!\n" ) ;
118 goto done ;
119 }
120
121 //Find the encryption template
122 tplNode = xmlSecFindNode( xmlDocGetRootElement( doc ), xmlSecNodeEncryptedData, xmlSecEncNs ) ;
123 if( tplNode == NULL ) {
124 fprintf( stderr , "### Cannot find the encryption template!\n" ) ;
125 goto done ;
126 }
127
128
129 try {
130 Reference< XMultiComponentFactory > xManager = NULL ;
131 Reference< XComponentContext > xContext = NULL ;
132
133 xManager = serviceManager( xContext , OUString::createFromAscii( "local" ), OUString::createFromAscii( argv[4] ) ) ;
134
135 //Create encryption template
136 Reference< XInterface > tplElement =
137 xManager->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.xsec.XMLElementWrapper" ) , xContext ) ;
138 OSL_ENSURE( tplElement.is() ,
139 "Decryptor - "
140 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
141
142 Reference< XXMLElementWrapper > xTplElement( tplElement , UNO_QUERY ) ;
143 OSL_ENSURE( xTplElement.is() ,
144 "Decryptor - "
145 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
146
147 Reference< XUnoTunnel > xTplEleTunnel( xTplElement , UNO_QUERY ) ;
148 OSL_ENSURE( xTplEleTunnel.is() ,
149 "Decryptor - "
150 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
151
152 XMLElementWrapper_XmlSecImpl* pTplElement = ( XMLElementWrapper_XmlSecImpl* )xTplEleTunnel->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
153 OSL_ENSURE( pTplElement != NULL ,
154 "Decryptor - "
155 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
156
157 pTplElement->setNativeElement( tplNode ) ;
158
159 //Build XML Encryption template
160 Reference< XInterface > enctpl =
161 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryptionTemplate"), xContext ) ;
162 OSL_ENSURE( enctpl.is() ,
163 "Decryptor - "
164 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
165
166 Reference< XXMLEncryptionTemplate > xTemplate( enctpl , UNO_QUERY ) ;
167 OSL_ENSURE( xTemplate.is() ,
168 "Decryptor - "
169 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
170
171 //Import the encryption template
172 xTemplate->setTemplate( xTplElement ) ;
173
174 //Create security environment
175 //Build Security Environment
176 Reference< XInterface > xsecenv =
177 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.SecurityEnvironment"), xContext ) ;
178 OSL_ENSURE( xsecenv.is() ,
179 "Decryptor - "
180 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
181
182 Reference< XSecurityEnvironment > xSecEnv( xsecenv , UNO_QUERY ) ;
183 OSL_ENSURE( xSecEnv.is() ,
184 "Decryptor - "
185 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
186
187 //Setup key slot and certDb
188 Reference< XUnoTunnel > xEnvTunnel( xsecenv , UNO_QUERY ) ;
189 OSL_ENSURE( xEnvTunnel.is() ,
190 "Decryptor - "
191 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
192
193 SecurityEnvironment_XmlSecImpl* pSecEnv = ( SecurityEnvironment_XmlSecImpl* )xEnvTunnel->getSomething( SecurityEnvironment_XmlSecImpl::getUnoTunnelId() ) ;
194 OSL_ENSURE( pSecEnv != NULL ,
195 "Decryptor - "
196 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
197
198 pSecEnv->setCryptoSlot( slot ) ;
199 pSecEnv->setCertDb( certHandle ) ;
200
201
202 //Build XML Security Context
203 Reference< XInterface > xmlsecctx =
204 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLSecurityContext"), xContext ) ;
205 OSL_ENSURE( xmlsecctx.is() ,
206 "Decryptor - "
207 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
208
209 Reference< XXMLSecurityContext > xSecCtx( xmlsecctx , UNO_QUERY ) ;
210 OSL_ENSURE( xSecCtx.is() ,
211 "Decryptor - "
212 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
213
214 xSecCtx->setSecurityEnvironment( xSecEnv ) ;
215
216
217 //Get encrypter
218 Reference< XInterface > xmlencrypter =
219 xManager->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.xsec.XMLEncryption"), xContext ) ;
220 OSL_ENSURE( xmlencrypter.is() ,
221 "Decryptor - "
222 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
223
224 Reference< XXMLEncryption > xEncrypter( xmlencrypter , UNO_QUERY ) ;
225 OSL_ENSURE( xEncrypter.is() ,
226 "Decryptor - "
227 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
228
229
230 //Perform decryption
231 Reference< XXMLElementWrapper> xDecrRes = xEncrypter->decrypt( xTemplate , xSecCtx ) ;
232 OSL_ENSURE( xDecrRes.is() ,
233 "Decryptor - "
234 "Cannot decrypt the xml document" ) ;
235 } catch( Exception& e ) {
236 fprintf( stderr , "Error Message: %s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
237 goto done ;
238 }
239
240 dstFile = fopen( argv[3], "w" ) ;
241 if( dstFile == NULL ) {
242 fprintf( stderr , "### Can not open file %s\n", argv[3] ) ;
243 goto done ;
244 }
245
246 //Save result
247 xmlDocDump( dstFile, doc ) ;
248
249 done:
250 if( dstFile != NULL )
251 fclose( dstFile ) ;
252
253 if( slot != NULL )
254 PK11_FreeSlot( slot ) ;
255
256 PK11_LogoutAll() ;
257 NSS_Shutdown() ;
258
259 /* Shutdown libxslt/libxml */
260 #ifndef XMLSEC_NO_XSLT
261 xsltCleanupGlobals();
262 #endif /* XMLSEC_NO_XSLT */
263 xmlCleanupParser();
264
265 return 0;
266 }
267
268