2017-05-12 00:26:06 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sal/config.h>
|
|
|
|
#include <rtl/uuid.h>
|
|
|
|
#include "xmlsignature_gpgimpl.hxx"
|
|
|
|
|
|
|
|
#include <gpgme.h>
|
|
|
|
#include <context.h>
|
|
|
|
#include <key.h>
|
|
|
|
#include <data.h>
|
|
|
|
#include <signingresult.h>
|
|
|
|
|
|
|
|
#include "xmlsec/xmldocumentwrapper_xmlsecimpl.hxx"
|
|
|
|
#include "xmlsec/xmlelementwrapper_xmlsecimpl.hxx"
|
|
|
|
#include "xmlsec/xmlstreamio.hxx"
|
|
|
|
#include "xmlsec/errorcallback.hxx"
|
|
|
|
|
|
|
|
#include "SecurityEnvironment.hxx"
|
|
|
|
#include "xmlsec-wrapper.h"
|
|
|
|
|
|
|
|
using namespace ::com::sun::star::uno ;
|
|
|
|
using namespace ::com::sun::star::lang ;
|
|
|
|
using ::com::sun::star::lang::XMultiServiceFactory ;
|
|
|
|
using ::com::sun::star::lang::XSingleServiceFactory ;
|
|
|
|
|
|
|
|
using ::com::sun::star::xml::wrapper::XXMLElementWrapper ;
|
|
|
|
using ::com::sun::star::xml::wrapper::XXMLDocumentWrapper ;
|
|
|
|
using ::com::sun::star::xml::crypto::XSecurityEnvironment ;
|
|
|
|
using ::com::sun::star::xml::crypto::XXMLSignature ;
|
|
|
|
using ::com::sun::star::xml::crypto::XXMLSignatureTemplate ;
|
|
|
|
using ::com::sun::star::xml::crypto::XXMLSecurityContext ;
|
|
|
|
using ::com::sun::star::xml::crypto::XUriBinding ;
|
|
|
|
|
|
|
|
XMLSignature_GpgImpl::XMLSignature_GpgImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLSignature_GpgImpl::~XMLSignature_GpgImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXMLSignature */
|
|
|
|
Reference< XXMLSignatureTemplate >
|
|
|
|
SAL_CALL XMLSignature_GpgImpl::generate(
|
|
|
|
const Reference< XXMLSignatureTemplate >& aTemplate ,
|
|
|
|
const Reference< XSecurityEnvironment >& aEnvironment
|
|
|
|
)
|
|
|
|
{
|
|
|
|
xmlSecDSigCtxPtr pDsigCtx = nullptr ;
|
|
|
|
xmlNodePtr pNode = nullptr ;
|
|
|
|
|
|
|
|
if( !aTemplate.is() )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
|
|
|
|
if( !aEnvironment.is() )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
|
|
|
|
//Get the xml node
|
|
|
|
Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
|
|
|
|
if( !xElement.is() ) {
|
|
|
|
throw RuntimeException() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLElementWrapper_XmlSecImpl* pElement =
|
|
|
|
dynamic_cast<XMLElementWrapper_XmlSecImpl*>(xElement.get());
|
|
|
|
if( pElement == nullptr ) {
|
|
|
|
throw RuntimeException() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
pNode = pElement->getNativeElement() ;
|
|
|
|
|
|
|
|
//Get the stream/URI binding
|
|
|
|
Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
|
|
|
|
if( xUriBinding.is() ) {
|
|
|
|
//Register the stream input callbacks into libxml2
|
|
|
|
if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
}
|
|
|
|
|
2017-05-21 14:28:57 +02:00
|
|
|
#if 0
|
2017-05-12 00:26:06 +02:00
|
|
|
//Get Keys Manager
|
|
|
|
SecurityEnvironmentGpg* pSecEnv =
|
|
|
|
dynamic_cast<SecurityEnvironmentGpg*>(aEnvironment.get());
|
|
|
|
if( pSecEnv == nullptr )
|
|
|
|
throw RuntimeException() ;
|
2017-05-21 14:28:57 +02:00
|
|
|
#endif
|
2017-05-12 00:26:06 +02:00
|
|
|
|
2017-05-21 14:28:57 +02:00
|
|
|
// TODO pSecEnv is still from nss, roll our own impl there
|
2017-05-12 00:26:06 +02:00
|
|
|
// TODO figure out key from pSecEnv!
|
|
|
|
// unclear how/where that is transported in nss impl...
|
|
|
|
setErrorRecorder();
|
|
|
|
|
|
|
|
//Create Signature context
|
|
|
|
pDsigCtx = xmlSecDSigCtxCreate( nullptr ) ;
|
|
|
|
if( pDsigCtx == nullptr )
|
|
|
|
{
|
|
|
|
clearErrorRecorder();
|
|
|
|
return aTemplate;
|
|
|
|
}
|
|
|
|
|
2017-05-23 23:08:02 +02:00
|
|
|
// set intended operation to sign - several asserts inside libxmlsec
|
|
|
|
// wanting that for digest / transforms
|
|
|
|
pDsigCtx->operation = xmlSecTransformOperationSign;
|
|
|
|
|
2017-05-12 00:26:06 +02:00
|
|
|
// Calculate digest for all references
|
|
|
|
xmlNodePtr cur = xmlSecGetNextElementNode(pNode->children);
|
2017-05-18 03:48:09 +02:00
|
|
|
if( cur != nullptr )
|
2017-05-12 00:26:06 +02:00
|
|
|
cur = xmlSecGetNextElementNode(cur->children);
|
2017-05-18 03:48:09 +02:00
|
|
|
while( cur != nullptr )
|
2017-05-12 00:26:06 +02:00
|
|
|
{
|
|
|
|
// some of those children I suppose should be reference elements
|
|
|
|
if( xmlSecCheckNodeName(cur, xmlSecNodeReference, xmlSecDSigNs) )
|
|
|
|
{
|
|
|
|
xmlSecDSigReferenceCtxPtr pDsigRefCtx =
|
|
|
|
xmlSecDSigReferenceCtxCreate(pDsigCtx,
|
|
|
|
xmlSecDSigReferenceOriginSignedInfo);
|
2017-05-18 03:48:09 +02:00
|
|
|
if(pDsigRefCtx == nullptr)
|
2017-05-12 00:26:06 +02:00
|
|
|
throw RuntimeException();
|
|
|
|
|
|
|
|
// add this one to the list
|
|
|
|
if( xmlSecPtrListAdd(&(pDsigCtx->signedInfoReferences),
|
|
|
|
pDsigRefCtx) < 0 )
|
|
|
|
{
|
|
|
|
// TODO resource handling
|
|
|
|
xmlSecDSigReferenceCtxDestroy(pDsigRefCtx);
|
|
|
|
throw RuntimeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( xmlSecDSigReferenceCtxProcessNode(pDsigRefCtx, cur) < 0 )
|
|
|
|
throw RuntimeException();
|
|
|
|
|
|
|
|
// final check - all good?
|
|
|
|
if(pDsigRefCtx->status != xmlSecDSigStatusSucceeded)
|
|
|
|
{
|
|
|
|
pDsigCtx->status = xmlSecDSigStatusInvalid;
|
|
|
|
return aTemplate; // TODO - harder error?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = xmlSecGetNextElementNode(cur->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get me a digestible buffer from the signature template!
|
|
|
|
// -------------------------------------------------------
|
|
|
|
|
2017-05-23 23:08:02 +02:00
|
|
|
// run the transformations over SignedInfo element (first child of
|
|
|
|
// pNode)
|
2017-05-18 03:48:09 +02:00
|
|
|
xmlSecNodeSetPtr nodeset = nullptr;
|
2017-05-23 23:08:02 +02:00
|
|
|
cur = xmlSecGetNextElementNode(pNode->children);
|
|
|
|
// TODO assert that...
|
|
|
|
nodeset = xmlSecNodeSetGetChildren(pNode->doc, cur, 1, 0);
|
2017-05-18 03:48:09 +02:00
|
|
|
if(nodeset == nullptr)
|
2017-05-12 00:26:06 +02:00
|
|
|
throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
|
|
|
|
|
|
|
|
if( xmlSecTransformCtxXmlExecute(&(pDsigCtx->transformCtx), nodeset) < 0 )
|
|
|
|
throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
|
|
|
|
|
|
|
|
//Sign the template via gpgme
|
|
|
|
GpgME::initializeLibrary();
|
|
|
|
if( GpgME::checkEngine(GpgME::OpenPGP) )
|
|
|
|
throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
|
|
|
|
|
|
|
|
GpgME::Context* ctx = GpgME::Context::createForProtocol(GpgME::OpenPGP);
|
|
|
|
if( ctx == nullptr )
|
|
|
|
throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
|
|
|
|
|
|
|
|
ctx->setKeyListMode(GPGME_KEYLIST_MODE_LOCAL);
|
|
|
|
GpgME::Error err;
|
|
|
|
if( ctx->addSigningKey(ctx->key("0x909BE2575CEDBEA3", err, true)) )
|
|
|
|
throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
|
|
|
|
|
|
|
|
// good, ctx is setup now, let's sign the lot
|
|
|
|
GpgME::Data data_in(
|
|
|
|
reinterpret_cast<char*>(xmlSecBufferGetData(pDsigCtx->transformCtx.result)),
|
|
|
|
xmlSecBufferGetSize(pDsigCtx->transformCtx.result), false);
|
|
|
|
GpgME::Data data_out;
|
|
|
|
|
2017-05-23 23:08:02 +02:00
|
|
|
SAL_INFO("xmlsecurity.xmlsec.gpg", "Generating signature for: " << xmlSecBufferGetData(pDsigCtx->transformCtx.result));
|
|
|
|
|
2017-05-12 00:26:06 +02:00
|
|
|
GpgME::SigningResult sign_res=ctx->sign(data_in, data_out,
|
2017-05-23 23:08:02 +02:00
|
|
|
GpgME::Detached);
|
2017-05-12 00:26:06 +02:00
|
|
|
// TODO: needs some error handling
|
|
|
|
data_out.seek(0,SEEK_SET);
|
|
|
|
int len=0, curr=0; char buf;
|
|
|
|
while( (curr=data_out.read(&buf, 1)) )
|
|
|
|
len += curr;
|
|
|
|
|
|
|
|
// write signed data to xml
|
|
|
|
std::vector<unsigned char> buf2(len);
|
|
|
|
data_out.seek(0,SEEK_SET);
|
|
|
|
if( data_out.read(&buf2[0], len) != len )
|
|
|
|
throw RuntimeException("The GpgME library failed to initialize for the OpenPGP protocol.");
|
|
|
|
|
|
|
|
// walk xml tree to sign value node - go to children, first is
|
|
|
|
// SignedInfo, 2nd is signaturevalue
|
|
|
|
cur = xmlSecGetNextElementNode(pNode->children);
|
|
|
|
cur = xmlSecGetNextElementNode(cur->next);
|
|
|
|
|
2017-05-23 23:08:02 +02:00
|
|
|
// TODO some assert would be good...
|
2017-05-12 00:26:06 +02:00
|
|
|
xmlNodeSetContentLen(cur, &buf2[0], len);
|
|
|
|
|
2017-05-23 23:08:02 +02:00
|
|
|
aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
|
|
|
|
|
2017-05-12 00:26:06 +02:00
|
|
|
// done
|
|
|
|
xmlSecDSigCtxDestroy( pDsigCtx ) ;
|
|
|
|
|
|
|
|
//Unregistered the stream/URI binding
|
|
|
|
if( xUriBinding.is() )
|
|
|
|
xmlUnregisterStreamInputCallbacks() ;
|
|
|
|
|
|
|
|
clearErrorRecorder();
|
|
|
|
return aTemplate ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXMLSignature */
|
|
|
|
Reference< XXMLSignatureTemplate >
|
|
|
|
SAL_CALL XMLSignature_GpgImpl::validate(
|
|
|
|
const Reference< XXMLSignatureTemplate >& aTemplate ,
|
|
|
|
const Reference< XXMLSecurityContext >& aSecurityCtx
|
|
|
|
) {
|
|
|
|
xmlSecKeysMngrPtr pMngr = nullptr ;
|
|
|
|
xmlSecDSigCtxPtr pDsigCtx = nullptr ;
|
|
|
|
xmlNodePtr pNode = nullptr ;
|
|
|
|
//sal_Bool valid ;
|
|
|
|
(void)pMngr; (void)pDsigCtx; (void)pNode;
|
|
|
|
|
|
|
|
if( !aTemplate.is() )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
|
|
|
|
if( !aSecurityCtx.is() )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
|
|
|
|
//Get the xml node
|
|
|
|
Reference< XXMLElementWrapper > xElement = aTemplate->getTemplate() ;
|
|
|
|
if( !xElement.is() )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
|
|
|
|
XMLElementWrapper_XmlSecImpl* pElement =
|
|
|
|
dynamic_cast<XMLElementWrapper_XmlSecImpl*>(xElement.get());
|
|
|
|
if( pElement == nullptr )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
|
|
|
|
pNode = pElement->getNativeElement() ;
|
|
|
|
|
|
|
|
//Get the stream/URI binding
|
|
|
|
Reference< XUriBinding > xUriBinding = aTemplate->getBinding() ;
|
|
|
|
if( xUriBinding.is() ) {
|
|
|
|
//Register the stream input callbacks into libxml2
|
|
|
|
if( xmlRegisterStreamInputCallbacks( xUriBinding ) < 0 )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
setErrorRecorder();
|
|
|
|
|
|
|
|
sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
|
|
|
|
sal_Int32 i;
|
|
|
|
|
|
|
|
for (i=0; i<nSecurityEnvironment; ++i)
|
|
|
|
{
|
|
|
|
Reference< XSecurityEnvironment > aEnvironment = aSecurityCtx->getSecurityEnvironmentByIndex(i);
|
|
|
|
|
|
|
|
//Get Keys Manager
|
|
|
|
Reference< XUnoTunnel > xSecTunnel( aEnvironment , UNO_QUERY_THROW ) ;
|
|
|
|
#if 0
|
|
|
|
SecurityEnvironment_NssImpl* pSecEnv =
|
|
|
|
reinterpret_cast<SecurityEnvironment_NssImpl*>(
|
|
|
|
sal::static_int_cast<sal_uIntPtr>(
|
|
|
|
xSecTunnel->getSomething( SecurityEnvironment_NssImpl::getUnoTunnelId() )));
|
|
|
|
if( pSecEnv == nullptr )
|
|
|
|
throw RuntimeException() ;
|
|
|
|
|
|
|
|
pMngr = pSecEnv->createKeysManager();
|
|
|
|
if( !pMngr ) {
|
|
|
|
throw RuntimeException() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create Signature context
|
|
|
|
pDsigCtx = xmlSecDSigCtxCreate( pMngr ) ;
|
|
|
|
if( pDsigCtx == nullptr )
|
|
|
|
{
|
|
|
|
SecurityEnvironment_NssImpl::destroyKeysManager( pMngr );
|
|
|
|
//throw XMLSignatureException() ;
|
|
|
|
clearErrorRecorder();
|
|
|
|
return aTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Verify signature
|
|
|
|
int rs = xmlSecDSigCtxVerify( pDsigCtx , pNode );
|
|
|
|
|
|
|
|
// Also verify manifest: this is empty for ODF, but contains everything (except signature metadata) for OOXML.
|
|
|
|
xmlSecSize nReferenceCount = xmlSecPtrListGetSize(&pDsigCtx->manifestReferences);
|
|
|
|
// Require that all manifest references are also good.
|
|
|
|
xmlSecSize nReferenceGood = 0;
|
|
|
|
for (xmlSecSize nReference = 0; nReference < nReferenceCount; ++nReference)
|
|
|
|
{
|
|
|
|
xmlSecDSigReferenceCtxPtr pReference = static_cast<xmlSecDSigReferenceCtxPtr>(xmlSecPtrListGetItem(&pDsigCtx->manifestReferences, nReference));
|
|
|
|
if (pReference)
|
|
|
|
{
|
|
|
|
if (pReference->status == xmlSecDSigStatusSucceeded)
|
|
|
|
++nReferenceGood;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rs == 0 && pDsigCtx->status == xmlSecDSigStatusSucceeded && nReferenceCount == nReferenceGood)
|
|
|
|
{
|
|
|
|
aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED);
|
|
|
|
xmlSecDSigCtxDestroy( pDsigCtx ) ;
|
|
|
|
SecurityEnvironment_NssImpl::destroyKeysManager( pMngr );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aTemplate->setStatus(css::xml::crypto::SecurityOperationStatus_UNKNOWN);
|
|
|
|
}
|
|
|
|
xmlSecDSigCtxDestroy( pDsigCtx ) ;
|
|
|
|
SecurityEnvironment_NssImpl::destroyKeysManager( pMngr );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Unregistered the stream/URI binding
|
|
|
|
if( xUriBinding.is() )
|
|
|
|
xmlUnregisterStreamInputCallbacks() ;
|
|
|
|
|
|
|
|
//return valid ;
|
|
|
|
clearErrorRecorder();
|
|
|
|
return aTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XServiceInfo */
|
|
|
|
OUString SAL_CALL XMLSignature_GpgImpl::getImplementationName() {
|
|
|
|
return impl_getImplementationName() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XServiceInfo */
|
|
|
|
sal_Bool SAL_CALL XMLSignature_GpgImpl::supportsService( const OUString& serviceName) {
|
|
|
|
Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
|
|
|
|
const OUString* pArray = seqServiceNames.getConstArray() ;
|
|
|
|
for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
|
|
|
|
if( *( pArray + i ) == serviceName )
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XServiceInfo */
|
|
|
|
Sequence< OUString > SAL_CALL XMLSignature_GpgImpl::getSupportedServiceNames() {
|
|
|
|
return impl_getSupportedServiceNames() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Helper for XServiceInfo
|
|
|
|
Sequence< OUString > XMLSignature_GpgImpl::impl_getSupportedServiceNames() {
|
|
|
|
::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ) ;
|
|
|
|
Sequence<OUString> seqServiceNames { "com.sun.star.xml.crypto.XMLSignature" };
|
|
|
|
return seqServiceNames ;
|
|
|
|
}
|
|
|
|
|
|
|
|
OUString XMLSignature_GpgImpl::impl_getImplementationName() {
|
|
|
|
return OUString("com.sun.star.xml.security.bridge.xmlsec.XMLSignature_GpgImpl") ;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Helper for registry
|
|
|
|
Reference< XInterface > SAL_CALL XMLSignature_GpgImpl::impl_createInstance( const Reference< XMultiServiceFactory >& ) {
|
|
|
|
return Reference< XInterface >( *new XMLSignature_GpgImpl ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reference< XSingleServiceFactory > XMLSignature_GpgImpl::impl_createFactory( const Reference< XMultiServiceFactory >& aServiceManager ) {
|
|
|
|
return ::cppu::createSingleFactory( aServiceManager , impl_getImplementationName() , impl_createInstance , impl_getSupportedServiceNames() ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|