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