From c54850b23a8240a41755af171a6d3f990ee69f84 Mon Sep 17 00:00:00 2001 From: Thorsten Behrens Date: Thu, 24 Aug 2017 17:40:09 +0200 Subject: gpg4libre/comphelper: add storage helper for GPG encryption data Change-Id: Idba9ad7a821cb33070cf5e5a0f79ae55db99b276 Reviewed-on: https://gerrit.libreoffice.org/41504 Reviewed-by: Katarina Behrens Tested-by: Katarina Behrens --- comphelper/source/misc/storagehelper.cxx | 51 ++++++++++++++++++++++++++++++++ include/comphelper/storagehelper.hxx | 3 ++ sfx2/source/dialog/filedlghelper.cxx | 7 +---- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/comphelper/source/misc/storagehelper.cxx b/comphelper/source/misc/storagehelper.cxx index ed55fe219e52..19e427c1fc87 100644 --- a/comphelper/source/misc/storagehelper.cxx +++ b/comphelper/source/misc/storagehelper.cxx @@ -34,10 +34,14 @@ #include #include #include +#include +#include #include #include +#include +#include #include #include @@ -403,6 +407,53 @@ uno::Sequence< beans::NamedValue > OStorageHelper::CreatePackageEncryptionData( return aEncryptionData; } +uno::Sequence< beans::NamedValue > OStorageHelper::CreateGpgPackageEncryptionData() +{ + // generate session key + // -------------------- + + // Get a random number generator and seed it with current timestamp + TimeValue aTime; + osl_getSystemTime( &aTime ); + rtlRandomPool aRandomPool = rtl_random_createPool(); + rtl_random_addBytes(aRandomPool, &aTime, 8); + + // get 16 random chars out of it + uno::Sequence < sal_Int8 > aVector(16); + rtl_random_getBytes( aRandomPool, aVector.getArray(), aVector.getLength() ); + + rtl_random_destroyPool(aRandomPool); + + uno::Sequence< beans::NamedValue > aContainer(2); + uno::Sequence< beans::NamedValue > aGpgEncryptionData(3); + uno::Sequence< beans::NamedValue > aEncryptionData(1); + + // TODO fire certificate chooser dialog + uno::Reference< security::XDocumentDigitalSignatures > xSigner( + security::DocumentDigitalSignatures::createWithVersion( + comphelper::getProcessComponentContext(), "1.2" ) ); + + // The use may provide a description while choosing a certificate. + OUString aDescription; + uno::Reference< security::XCertificate > xSignCertificate= + xSigner->chooseCertificate(aDescription); + + uno::Sequence < sal_Int8 > aKeyID; + if (xSignCertificate.is()) + { + aKeyID = xSignCertificate->getSHA1Thumbprint(); + } + + aGpgEncryptionData[0].Name = "KeyId"; + aGpgEncryptionData[0].Value <<= aKeyID; + + aContainer[0].Name = "GpgInfos"; + aContainer[0].Value <<= aGpgEncryptionData; + aContainer[1].Name = "EncryptionKey"; + aContainer[1].Value <<= aEncryptionData; + + return aContainer; +} bool OStorageHelper::IsValidZipEntryFileName( const OUString& aName, bool bSlashAllowed ) { diff --git a/include/comphelper/storagehelper.hxx b/include/comphelper/storagehelper.hxx index 84c958fb1f23..1f5e22cb14e3 100644 --- a/include/comphelper/storagehelper.hxx +++ b/include/comphelper/storagehelper.hxx @@ -172,6 +172,9 @@ public: CreatePackageEncryptionData( const OUString& aPassword ); + static css::uno::Sequence< css::beans::NamedValue > + CreateGpgPackageEncryptionData(); + static bool IsValidZipEntryFileName( const OUString& aName, bool bSlashAllowed ); static bool IsValidZipEntryFileName( const sal_Unicode *pChar, sal_Int32 nLength, bool bSlashAllowed ); diff --git a/sfx2/source/dialog/filedlghelper.cxx b/sfx2/source/dialog/filedlghelper.cxx index d6a5489f85ce..a4a6847deeea 100644 --- a/sfx2/source/dialog/filedlghelper.cxx +++ b/sfx2/source/dialog/filedlghelper.cxx @@ -1522,12 +1522,7 @@ ErrCode FileDialogHelper_Impl::execute( std::vector& rpURLList, if ( ( aValue >>= bGpg ) && bGpg ) { // ask for a key - OUString aDocName(rpURLList[0]); - // ErrCode errCode = RequestKey(pCurrentFilter, aDocName, rpSet); - //if (errCode != ERRCODE_NONE) - rpSet->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( ::comphelper::OStorageHelper::CreatePackageEncryptionData( aDocName ) ) ) ); - - return ERRCODE_IO_NOTSUPPORTED; //errCode; + rpSet->Put( SfxUnoAnyItem( SID_ENCRYPTIONDATA, uno::makeAny( ::comphelper::OStorageHelper::CreateGpgPackageEncryptionData() ) ) ); } } catch( const IllegalArgumentException& ){} -- cgit