1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
34 #include "libxml/tree.h"
35 #include "libxml/parser.h"
36 #ifndef XMLSEC_NO_XSLT
37 #include "libxslt/xslt.h"
41 #include "securityenvironment_nssimpl.hxx"
42 #include "xmlelementwrapper_xmlsecimpl.hxx"
53 #include "xmlsec/strings.h"
54 #include "xmlsec/xmltree.h"
56 #include <rtl/ustring.hxx>
57 #include <cppuhelper/bootstrap.hxx>
58 #include <cppuhelper/servicefactory.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/XXMLEncryption.hpp>
64 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
65 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
66 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
69 using namespace ::rtl
;
70 using namespace ::cppu
;
71 using namespace ::com::sun::star::uno
;
72 using namespace ::com::sun::star::io
;
73 using namespace ::com::sun::star::ucb
;
74 using namespace ::com::sun::star::beans
;
75 using namespace ::com::sun::star::document
;
76 using namespace ::com::sun::star::lang
;
77 using namespace ::com::sun::star::registry
;
78 using namespace ::com::sun::star::xml::wrapper
;
79 using namespace ::com::sun::star::xml::crypto
;
82 int SAL_CALL
main( int argc
, char **argv
)
84 CERTCertDBHandle
* certHandle
= NULL
;
85 PK11SlotInfo
* slot
= NULL
;
86 xmlDocPtr doc
= NULL
;
89 FILE* dstFile
= NULL
;
93 fprintf( stderr
, "Usage: %s < CertDir > <input file_url> <output file_url> <rdb file>\n\n" , argv
[0] ) ;
97 //Init libxml and libxslt libraries
100 xmlLoadExtDtdDefaultValue
= XML_DETECT_IDS
| XML_COMPLETE_ATTRS
;
101 xmlSubstituteEntitiesDefault(1);
103 #ifndef XMLSEC_NO_XSLT
104 xmlIndentTreeOutput
= 1;
105 #endif // XMLSEC_NO_XSLT
108 //Initialize NSPR and NSS
109 PR_Init( PR_SYSTEM_THREAD
, PR_PRIORITY_NORMAL
, 1 ) ;
110 PK11_SetPasswordFunc( PriPK11PasswordFunc
) ;
111 if( NSS_Init( argv
[1] ) != SECSuccess
) {
112 fprintf( stderr
, "### cannot intialize NSS!\n" ) ;
116 certHandle
= CERT_GetDefaultCertDB() ;
117 slot
= PK11_GetInternalKeySlot() ;
120 doc
= xmlParseFile( argv
[2] ) ;
121 if( doc
== NULL
|| xmlDocGetRootElement( doc
) == NULL
) {
122 fprintf( stderr
, "### Cannot load template xml document!\n" ) ;
126 //Find the encryption template
127 tplNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), xmlSecNodeEncryptedData
, xmlSecEncNs
) ;
128 if( tplNode
== NULL
) {
129 fprintf( stderr
, "### Cannot find the encryption template!\n" ) ;
135 Reference
< XMultiComponentFactory
> xManager
= NULL
;
136 Reference
< XComponentContext
> xContext
= NULL
;
138 xManager
= serviceManager( xContext
, OUString::createFromAscii( "local" ), OUString::createFromAscii( argv
[4] ) ) ;
140 //Create encryption template
141 Reference
< XInterface
> tplElement
=
142 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext
) ;
143 OSL_ENSURE( tplElement
.is() ,
145 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
147 Reference
< XXMLElementWrapper
> xTplElement( tplElement
, UNO_QUERY
) ;
148 OSL_ENSURE( xTplElement
.is() ,
150 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
152 Reference
< XUnoTunnel
> xTplEleTunnel( xTplElement
, UNO_QUERY
) ;
153 OSL_ENSURE( xTplEleTunnel
.is() ,
155 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
157 XMLElementWrapper_XmlSecImpl
* pTplElement
= ( XMLElementWrapper_XmlSecImpl
* )xTplEleTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
158 OSL_ENSURE( pTplElement
!= NULL
,
160 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
162 pTplElement
->setNativeElement( tplNode
) ;
164 //Build XML Encryption template
165 Reference
< XInterface
> enctpl
=
166 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext
) ;
167 OSL_ENSURE( enctpl
.is() ,
169 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
171 Reference
< XXMLEncryptionTemplate
> xTemplate( enctpl
, UNO_QUERY
) ;
172 OSL_ENSURE( xTemplate
.is() ,
174 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
176 //Import the encryption template
177 xTemplate
->setTemplate( xTplElement
) ;
179 //Create security environment
180 //Build Security Environment
181 Reference
< XInterface
> xsecenv
=
182 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext
) ;
183 OSL_ENSURE( xsecenv
.is() ,
185 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
187 Reference
< XSecurityEnvironment
> xSecEnv( xsecenv
, UNO_QUERY
) ;
188 OSL_ENSURE( xSecEnv
.is() ,
190 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
192 //Setup key slot and certDb
193 Reference
< XUnoTunnel
> xEnvTunnel( xsecenv
, UNO_QUERY
) ;
194 OSL_ENSURE( xEnvTunnel
.is() ,
196 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
198 SecurityEnvironment_NssImpl
* pSecEnv
= ( SecurityEnvironment_NssImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
199 OSL_ENSURE( pSecEnv
!= NULL
,
201 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
203 pSecEnv
->setCryptoSlot( slot
) ;
204 pSecEnv
->setCertDb( certHandle
) ;
207 //Build XML Security Context
208 Reference
< XInterface
> xmlsecctx
=
209 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext
) ;
210 OSL_ENSURE( xmlsecctx
.is() ,
212 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
214 Reference
< XXMLSecurityContext
> xSecCtx( xmlsecctx
, UNO_QUERY
) ;
215 OSL_ENSURE( xSecCtx
.is() ,
217 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
219 xSecCtx
->setSecurityEnvironment( xSecEnv
) ;
223 Reference
< XInterface
> xmlencrypter
=
224 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl"), xContext
) ;
225 OSL_ENSURE( xmlencrypter
.is() ,
227 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
229 Reference
< XXMLEncryption
> xEncrypter( xmlencrypter
, UNO_QUERY
) ;
230 OSL_ENSURE( xEncrypter
.is() ,
232 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
236 Reference
< XXMLElementWrapper
> xDecrRes
= xEncrypter
->decrypt( xTemplate
, xSecCtx
) ;
237 OSL_ENSURE( xDecrRes
.is() ,
239 "Cannot decrypt the xml document" ) ;
240 } catch( Exception
& e
) {
241 fprintf( stderr
, "Error Message: %s\n" , OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() ) ;
245 dstFile
= fopen( argv
[3], "w" ) ;
246 if( dstFile
== NULL
) {
247 fprintf( stderr
, "### Can not open file %s\n", argv
[3] ) ;
252 xmlDocDump( dstFile
, doc
) ;
255 if( dstFile
!= NULL
)
259 PK11_FreeSlot( slot
) ;
264 /* Shutdown libxslt/libxml */
265 #ifndef XMLSEC_NO_XSLT
266 xsltCleanupGlobals();
267 #endif /* XMLSEC_NO_XSLT */