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