kbrown / rpms / libreoffice

Forked from rpms/libreoffice 2 years ago
Clone

Blame SOURCES/0001-rhbz-1614419-use-workaround-for-PK11_ImportSymKey-fa.patch

91334d
From 3fe2dd8094fcec76a1310d3ab33c1cc27c85c356 Mon Sep 17 00:00:00 2001
91334d
From: rpmbuild <rpmbuild@fedoraproject.org>
91334d
Date: Fri, 10 Aug 2018 09:45:56 +0100
91334d
Subject: [PATCH] rhbz#1614419 use workaround for PK11_ImportSymKey failure
91334d
 under FIPS
91334d
91334d
---
91334d
 include/oox/crypto/CryptTools.hxx |  3 ++
91334d
 oox/source/crypto/CryptTools.cxx  | 83 +++++++++++++++++++++++++++++++++++----
91334d
 2 files changed, 79 insertions(+), 7 deletions(-)
91334d
91334d
diff --git a/include/oox/crypto/CryptTools.hxx b/include/oox/crypto/CryptTools.hxx
91334d
index 84e4c48..07605da 100644
91334d
--- a/include/oox/crypto/CryptTools.hxx
91334d
+++ b/include/oox/crypto/CryptTools.hxx
91334d
@@ -56,9 +56,12 @@ protected:
91334d
     EVP_CIPHER_CTX mContext;
91334d
 #endif
91334d
 #if USE_TLS_NSS
91334d
+    PK11SlotInfo* mSlot;
91334d
     PK11Context* mContext;
91334d
     SECItem*     mSecParam;
91334d
     PK11SymKey*  mSymKey;
91334d
+    PK11Context* mWrapKeyContext;
91334d
+    PK11SymKey*  mWrapKey;
91334d
 #endif
91334d
 
91334d
 #if USE_TLS_OPENSSL
91334d
diff --git a/oox/source/crypto/CryptTools.cxx b/oox/source/crypto/CryptTools.cxx
91334d
index 5ecf7b3..ee91925 100644
91334d
--- a/oox/source/crypto/CryptTools.cxx
91334d
+++ b/oox/source/crypto/CryptTools.cxx
91334d
@@ -19,9 +19,12 @@ using namespace std;
91334d
 
91334d
 Crypto::Crypto()
91334d
 #if USE_TLS_NSS
91334d
-    : mContext(nullptr)
91334d
+    : mSlot(nullptr)
91334d
+    , mContext(nullptr)
91334d
     , mSecParam(nullptr)
91334d
     , mSymKey(nullptr)
91334d
+    , mWrapKeyContext(nullptr)
91334d
+    , mWrapKey(nullptr)
91334d
 #endif
91334d
 {
91334d
 #if USE_TLS_NSS
91334d
@@ -38,10 +41,16 @@ Crypto::~Crypto()
91334d
 #if USE_TLS_NSS
91334d
     if (mContext)
91334d
         PK11_DestroyContext(mContext, PR_TRUE);
91334d
-    if (mSymKey)
91334d
-        PK11_FreeSymKey(mSymKey);
91334d
     if (mSecParam)
91334d
         SECITEM_FreeItem(mSecParam, PR_TRUE);
91334d
+    if (mSymKey)
91334d
+        PK11_FreeSymKey(mSymKey);
91334d
+    if (mWrapKeyContext)
91334d
+        PK11_DestroyContext(mWrapKeyContext, PR_TRUE);
91334d
+    if (mWrapKey)
91334d
+        PK11_FreeSymKey(mWrapKey);
91334d
+    if (mSlot)
91334d
+        PK11_FreeSlot(mSlot);
91334d
 #endif
91334d
 }
91334d
 
91334d
@@ -59,11 +68,14 @@ const EVP_CIPHER* Crypto::getCipher(CryptoType type)
91334d
         default:
91334d
             break;
91334d
     }
91334d
-    return NULL;
91334d
+    return nullptr;
91334d
 }
91334d
 #endif
91334d
 
91334d
 #if USE_TLS_NSS
91334d
+
91334d
+#define MAX_WRAPPED_KEY_LEN 128
91334d
+
91334d
 void Crypto::setupContext(vector<sal_uInt8>& key, vector<sal_uInt8>& iv, CryptoType type, CK_ATTRIBUTE_TYPE operation)
91334d
 {
91334d
     CK_MECHANISM_TYPE mechanism = static_cast<CK_ULONG>(-1);
91334d
@@ -95,9 +107,9 @@ void Crypto::setupContext(vector<sal_uInt8>& key, vector<sal_uInt8>& iv, CryptoT
91334d
             break;
91334d
     }
91334d
 
91334d
-    PK11SlotInfo* pSlot( PK11_GetBestSlot( mechanism, nullptr ) );
91334d
+    mSlot = PK11_GetBestSlot(mechanism, nullptr);
91334d
 
91334d
-    if (!pSlot)
91334d
+    if (!mSlot)
91334d
         throw css::uno::RuntimeException("NSS Slot failure", css::uno::Reference<css::uno::XInterface>());
91334d
 
91334d
     SECItem keyItem;
91334d
@@ -105,7 +117,64 @@ void Crypto::setupContext(vector<sal_uInt8>& key, vector<sal_uInt8>& iv, CryptoT
91334d
     keyItem.data = &key[0];
91334d
     keyItem.len  = key.size();
91334d
 
91334d
-    mSymKey = PK11_ImportSymKey( pSlot, mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr );
91334d
+    mSymKey = PK11_ImportSymKey( mSlot, mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr );
91334d
+    if (!mSymKey) //rhbz#1614419 maybe failed due to FIPS, use rhbz#1461450 style workaround
91334d
+    {
91334d
+        /*
91334d
+         * Without FIPS it would be possible to just use
91334d
+         *  mSymKey = PK11_ImportSymKey( mSlot, mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr );
91334d
+         * with FIPS NSS Level 2 certification has to be "workarounded" (so it becomes Level 1) by using
91334d
+         * following method:
91334d
+         * 1. Generate wrap key
91334d
+         * 2. Encrypt authkey with wrap key
91334d
+         * 3. Unwrap encrypted authkey using wrap key
91334d
+         */
91334d
+
91334d
+        /*
91334d
+         * Generate wrapping key
91334d
+         */
91334d
+        CK_MECHANISM_TYPE wrap_mechanism = PK11_GetBestWrapMechanism(mSlot);
91334d
+        int wrap_key_len = PK11_GetBestKeyLength(mSlot, wrap_mechanism);
91334d
+        mWrapKey = PK11_KeyGen(mSlot, wrap_mechanism, nullptr, wrap_key_len, nullptr);
91334d
+        if (!mWrapKey)
91334d
+            throw css::uno::RuntimeException("PK11_KeyGen SymKey failure", css::uno::Reference<css::uno::XInterface>());
91334d
+
91334d
+        /*
91334d
+         * Encrypt authkey with wrapping key
91334d
+         */
91334d
+
91334d
+        /*
91334d
+         * Initialization of IV is not needed because PK11_GetBestWrapMechanism should return ECB mode
91334d
+         */
91334d
+        SECItem tmp_sec_item;
91334d
+        memset(&tmp_sec_item, 0, sizeof(tmp_sec_item));
91334d
+        mWrapKeyContext = PK11_CreateContextBySymKey(wrap_mechanism, CKA_ENCRYPT, mWrapKey, &tmp_sec_item);
91334d
+        if (!mWrapKeyContext)
91334d
+            throw css::uno::RuntimeException("PK11_CreateContextBySymKey failure", css::uno::Reference<css::uno::XInterface>());
91334d
+
91334d
+        unsigned char wrapped_key_data[MAX_WRAPPED_KEY_LEN];
91334d
+        int wrapped_key_len = sizeof(wrapped_key_data);
91334d
+
91334d
+        if (PK11_CipherOp(mWrapKeyContext, wrapped_key_data, &wrapped_key_len,
91334d
+            sizeof(wrapped_key_data), keyItem.data, keyItem.len) != SECSuccess)
91334d
+        {
91334d
+            throw css::uno::RuntimeException("PK11_CipherOp failure", css::uno::Reference<css::uno::XInterface>());
91334d
+        }
91334d
+
91334d
+        if (PK11_Finalize(mWrapKeyContext) != SECSuccess)
91334d
+            throw css::uno::RuntimeException("PK11_Finalize failure", css::uno::Reference<css::uno::XInterface>());
91334d
+
91334d
+        /*
91334d
+         * Finally unwrap sym key
91334d
+         */
91334d
+        SECItem wrapped_key;
91334d
+        memset(&tmp_sec_item, 0, sizeof(tmp_sec_item));
91334d
+        wrapped_key.data = wrapped_key_data;
91334d
+        wrapped_key.len = wrapped_key_len;
91334d
+
91334d
+        mSymKey = PK11_UnwrapSymKey(mWrapKey, wrap_mechanism, &tmp_sec_item, &wrapped_key,
91334d
+            mechanism, CKA_ENCRYPT, keyItem.len);
91334d
+    }
91334d
     if (!mSymKey)
91334d
         throw css::uno::RuntimeException("NSS SymKey failure", css::uno::Reference<css::uno::XInterface>());
91334d
     mSecParam = PK11_ParamFromIV( mechanism, pIvItem );
91334d
-- 
91334d
1.8.3.1
91334d