1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: encrypter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
37 #include "libxml/tree.h"
38 #include "libxml/parser.h"
39 #ifndef XMLSEC_NO_XSLT
40 #include "libxslt/xslt.h"
43 #include "securityenvironment_nssimpl.hxx"
44 #include "xmlelementwrapper_xmlsecimpl.hxx"
55 #include "xmlsec/strings.h"
56 #include "xmlsec/xmltree.h"
58 #include <rtl/ustring.hxx>
59 #include <cppuhelper/servicefactory.hxx>
61 #include <com/sun/star/lang/XComponent.hpp>
62 #include <com/sun/star/beans/PropertyValue.hpp>
63 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
64 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
65 #include <com/sun/star/xml/crypto/XXMLEncryption.hpp>
66 #include <com/sun/star/xml/crypto/XXMLEncryptionTemplate.hpp>
67 #include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
68 #include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
70 using namespace ::rtl
;
71 using namespace ::cppu
;
72 using namespace ::com::sun::star::uno
;
73 using namespace ::com::sun::star::io
;
74 using namespace ::com::sun::star::ucb
;
75 using namespace ::com::sun::star::beans
;
76 using namespace ::com::sun::star::document
;
77 using namespace ::com::sun::star::lang
;
78 using namespace ::com::sun::star::registry
;
79 using namespace ::com::sun::star::xml::wrapper
;
80 using namespace ::com::sun::star::xml::crypto
;
82 int SAL_CALL
main( int argc
, char **argv
)
84 CERTCertDBHandle
* certHandle
;
85 PK11SlotInfo
* slot
= NULL
;
86 PK11SymKey
* symKey
= NULL
;
87 xmlDocPtr doc
= NULL
;
90 FILE* dstFile
= NULL
;
93 fprintf( stderr
, "Usage: %s < CertDir > <file_url of template> <file_url of result> <target element name> <target element namespace> <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
107 //Initialize NSPR and NSS
108 PR_Init( PR_SYSTEM_THREAD
, PR_PRIORITY_NORMAL
, 1 ) ;
109 PK11_SetPasswordFunc( PriPK11PasswordFunc
) ;
110 if( NSS_Init( argv
[1] ) != SECSuccess
) {
111 fprintf( stderr
, "### cannot intialize NSS!\n" ) ;
115 certHandle
= CERT_GetDefaultCertDB() ;
116 slot
= PK11_GetInternalKeySlot() ;
118 symKey
= PK11_KeyGen( slot
, CKM_DES3_CBC
, NULL
, 128, NULL
) ;
119 if( symKey
== NULL
) {
120 fprintf( stderr
, "### cannot create symmetric key!\n" ) ;
125 doc
= xmlParseFile( argv
[2] ) ;
126 if( doc
== NULL
|| xmlDocGetRootElement( doc
) == NULL
) {
127 fprintf( stderr
, "### Cannot load template xml document!\n" ) ;
131 //Find the encryption template
132 tplNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), xmlSecNodeEncryptedData
, xmlSecEncNs
) ;
133 if( tplNode
== NULL
) {
134 fprintf( stderr
, "### Cannot find the encryption template!\n" ) ;
138 //Find the encryption template
139 tarNode
= xmlSecFindNode( xmlDocGetRootElement( doc
), ( const unsigned char*)argv
[4], ( const unsigned char*)argv
[5] ) ;
140 if( tarNode
== NULL
) {
141 fprintf( stderr
, "### Cannot find the encryption target!\n" ) ;
146 Reference
< XMultiComponentFactory
> xManager
= NULL
;
147 Reference
< XComponentContext
> xContext
= NULL
;
149 xManager
= serviceManager( xContext
, OUString::createFromAscii( "local" ), OUString::createFromAscii( argv
[6] ) ) ;
151 //Create encryption template
152 Reference
< XInterface
> tplElement
=
153 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext
) ;
154 OSL_ENSURE( tplElement
.is() ,
156 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
158 Reference
< XXMLElementWrapper
> xTplElement( tplElement
, UNO_QUERY
) ;
159 OSL_ENSURE( xTplElement
.is() ,
161 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
163 Reference
< XUnoTunnel
> xTplEleTunnel( xTplElement
, UNO_QUERY
) ;
164 OSL_ENSURE( xTplEleTunnel
.is() ,
166 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
168 XMLElementWrapper_XmlSecImpl
* pTplElement
= ( XMLElementWrapper_XmlSecImpl
* )xTplEleTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
169 OSL_ENSURE( pTplElement
!= NULL
,
171 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
173 pTplElement
->setNativeElement( tplNode
) ;
175 //Create encryption target element
176 Reference
< XInterface
> tarElement
=
177 xManager
->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.xml.security.bridge.xmlsec.XMLElementWrapper_XmlSecImpl" ) , xContext
) ;
178 OSL_ENSURE( tarElement
.is() ,
180 "Cannot get service instance of \"xsec.XMLElementWrapper\"" ) ;
182 Reference
< XXMLElementWrapper
> xTarElement( tarElement
, UNO_QUERY
) ;
183 OSL_ENSURE( xTarElement
.is() ,
185 "Cannot get interface of \"XXMLElementWrapper\" from service \"xsec.XMLElementWrapper\"" ) ;
187 Reference
< XUnoTunnel
> xTarEleTunnel( xTarElement
, UNO_QUERY
) ;
188 OSL_ENSURE( xTarEleTunnel
.is() ,
190 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.XMLElementWrapper\"" ) ;
192 XMLElementWrapper_XmlSecImpl
* pTarElement
= ( XMLElementWrapper_XmlSecImpl
* )xTarEleTunnel
->getSomething( XMLElementWrapper_XmlSecImpl::getUnoTunnelImplementationId() ) ;
193 OSL_ENSURE( pTarElement
!= NULL
,
195 "Cannot get implementation of \"xsec.XMLElementWrapper\"" ) ;
197 pTarElement
->setNativeElement( tarNode
) ;
200 //Build XML Encryption template
201 Reference
< XInterface
> enctpl
=
202 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.crypto.XMLEncryptionTemplate"), xContext
) ;
203 OSL_ENSURE( enctpl
.is() ,
205 "Cannot get service instance of \"xsec.XMLEncryptionTemplate\"" ) ;
207 Reference
< XXMLEncryptionTemplate
> xTemplate( enctpl
, UNO_QUERY
) ;
208 OSL_ENSURE( xTemplate
.is() ,
210 "Cannot get interface of \"XXMLEncryptionTemplate\" from service \"xsec.XMLEncryptionTemplate\"" ) ;
212 //Import the encryption template
213 xTemplate
->setTemplate( xTplElement
) ;
214 xTemplate
->setTarget( xTarElement
) ;
216 //Create security environment
217 //Build Security Environment
218 Reference
< XInterface
> xsecenv
=
219 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.SecurityEnvironment_NssImpl"), xContext
) ;
220 OSL_ENSURE( xsecenv
.is() ,
222 "Cannot get service instance of \"xsec.SecurityEnvironment\"" ) ;
224 Reference
< XSecurityEnvironment
> xSecEnv( xsecenv
, UNO_QUERY
) ;
225 OSL_ENSURE( xSecEnv
.is() ,
227 "Cannot get interface of \"XSecurityEnvironment\" from service \"xsec.SecurityEnvironment\"" ) ;
229 //Setup key slot and certDb
230 Reference
< XUnoTunnel
> xEnvTunnel( xsecenv
, UNO_QUERY
) ;
231 OSL_ENSURE( xEnvTunnel
.is() ,
233 "Cannot get interface of \"XUnoTunnel\" from service \"xsec.SecurityEnvironment\"" ) ;
235 SecurityEnvironment_NssImpl
* pSecEnv
= ( SecurityEnvironment_NssImpl
* )xEnvTunnel
->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() ) ;
236 OSL_ENSURE( pSecEnv
!= NULL
,
238 "Cannot get implementation of \"xsec.SecurityEnvironment\"" ) ;
240 pSecEnv
->setCryptoSlot( slot
) ;
241 pSecEnv
->setCertDb( certHandle
) ;
242 pSecEnv
->adoptSymKey( symKey
) ;
245 //Build XML Security Context
246 Reference
< XInterface
> xmlsecctx
=
247 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLSecurityContext_NssImpl"), xContext
) ;
248 OSL_ENSURE( xmlsecctx
.is() ,
250 "Cannot get service instance of \"xsec.XMLSecurityContext\"" ) ;
252 Reference
< XXMLSecurityContext
> xSecCtx( xmlsecctx
, UNO_QUERY
) ;
253 OSL_ENSURE( xSecCtx
.is() ,
255 "Cannot get interface of \"XXMLSecurityContext\" from service \"xsec.XMLSecurityContext\"" ) ;
257 xSecCtx
->setSecurityEnvironment( xSecEnv
) ;
260 Reference
< XInterface
> xmlencrypter
=
261 xManager
->createInstanceWithContext( OUString::createFromAscii("com.sun.star.xml.security.bridge.xmlsec.XMLEncryption_NssImpl"), xContext
) ;
262 OSL_ENSURE( xmlencrypter
.is() ,
264 "Cannot get service instance of \"xsec.XMLEncryption\"" ) ;
266 Reference
< XXMLEncryption
> xEncrypter( xmlencrypter
, UNO_QUERY
) ;
267 OSL_ENSURE( xEncrypter
.is() ,
269 "Cannot get interface of \"XXMLEncryption\" from service \"xsec.XMLEncryption\"" ) ;
272 xTemplate
= xEncrypter
->encrypt( xTemplate
, xSecCtx
) ;
273 OSL_ENSURE( xTemplate
.is() ,
275 "Cannot encrypt the xml document" ) ;
276 } catch( Exception
& e
) {
277 fprintf( stderr
, "Error Message: %s\n" , OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() ) ;
281 dstFile
= fopen( argv
[3], "w" ) ;
282 if( dstFile
== NULL
) {
283 fprintf( stderr
, "### Can not open file %s\n", argv
[3] ) ;
288 xmlDocDump( dstFile
, doc
) ;
291 if( dstFile
!= NULL
)
294 if( symKey
!= NULL
) {
295 PK11_FreeSymKey( symKey
) ;
299 PK11_FreeSlot( slot
) ;
304 /* Shutdown libxslt/libxml */
305 #ifndef XMLSEC_NO_XSLT
306 xsltCleanupGlobals();
307 #endif /* XMLSEC_NO_XSLT */