1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/* -*- 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/.
*/
#include <comphelper/crypto/Crypto.hxx>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <sal/types.h>
#include <config_oox.h>
#include <nss.h>
#include <nspr.h>
#include <pk11pub.h>
namespace comphelper
{
namespace
{
#define MAX_WRAPPED_KEY_LEN 128
class CryptoImplementationNSS : public ICryptoImplementation
{
PK11SlotInfo* mSlot;
PK11Context* mContext;
SECItem* mSecParam;
PK11SymKey* mSymKey;
PK11Context* mWrapKeyContext;
PK11SymKey* mWrapKey;
public:
CryptoImplementationNSS()
: mSlot(nullptr)
, mContext(nullptr)
, mSecParam(nullptr)
, mSymKey(nullptr)
, mWrapKeyContext(nullptr)
, mWrapKey(nullptr)
{
// Initialize NSS, database functions are not needed
if (!NSS_IsInitialized())
{
auto const e = NSS_NoDB_Init(nullptr);
if (e != SECSuccess)
{
PRErrorCode error = PR_GetError();
const char* errorText = PR_ErrorToName(error);
throw css::uno::RuntimeException(
"NSS_NoDB_Init failed with "
+ OUString(errorText, strlen(errorText), RTL_TEXTENCODING_UTF8) + " ("
+ OUString::number(static_cast<int>(error)) + ")");
}
}
}
virtual ~CryptoImplementationNSS()
{
if (mContext)
PK11_DestroyContext(mContext, PR_TRUE);
if (mSecParam)
SECITEM_FreeItem(mSecParam, PR_TRUE);
if (mSymKey)
PK11_FreeSymKey(mSymKey);
if (mWrapKeyContext)
PK11_DestroyContext(mWrapKeyContext, PR_TRUE);
if (mWrapKey)
PK11_FreeSymKey(mWrapKey);
if (mSlot)
PK11_FreeSlot(mSlot);
}
PK11SymKey* ImportSymKey(CK_MECHANISM_TYPE mechanism, CK_ATTRIBUTE_TYPE operation, SECItem* key)
{
mSymKey = PK11_ImportSymKey(mSlot, mechanism, PK11_OriginUnwrap, operation, key, nullptr);
if (!mSymKey) //rhbz#1614419 maybe failed due to FIPS, use rhbz#1461450 style workaround
{
/*
* Without FIPS it would be possible to just use
* mSymKey = PK11_ImportSymKey( mSlot, mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr );
* with FIPS NSS Level 2 certification has to be "workarounded" (so it becomes Level 1) by using
* following method:
* 1. Generate wrap key
* 2. Encrypt authkey with wrap key
* 3. Unwrap encrypted authkey using wrap key
*/
/*
* Generate wrapping key
*/
CK_MECHANISM_TYPE wrap_mechanism = PK11_GetBestWrapMechanism(mSlot);
int wrap_key_len = PK11_GetBestKeyLength(mSlot, wrap_mechanism);
mWrapKey = PK11_KeyGen(mSlot, wrap_mechanism, nullptr, wrap_key_len, nullptr);
if (!mWrapKey)
throw css::uno::RuntimeException(u"PK11_KeyGen SymKey failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
/*
* Encrypt authkey with wrapping key
*/
/*
* Initialization of IV is not needed because PK11_GetBestWrapMechanism should return ECB mode
*/
SECItem tmp_sec_item = {};
mWrapKeyContext
= PK11_CreateContextBySymKey(wrap_mechanism, CKA_ENCRYPT, mWrapKey, &tmp_sec_item);
if (!mWrapKeyContext)
throw css::uno::RuntimeException(u"PK11_CreateContextBySymKey failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
unsigned char wrapped_key_data[MAX_WRAPPED_KEY_LEN];
int wrapped_key_len = sizeof(wrapped_key_data);
if (PK11_CipherOp(mWrapKeyContext, wrapped_key_data, &wrapped_key_len,
sizeof(wrapped_key_data), key->data, key->len)
!= SECSuccess)
{
throw css::uno::RuntimeException(u"PK11_CipherOp failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
}
if (PK11_Finalize(mWrapKeyContext) != SECSuccess)
throw css::uno::RuntimeException(u"PK11_Finalize failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
/*
* Finally unwrap sym key
*/
SECItem wrapped_key = {};
wrapped_key.data = wrapped_key_data;
wrapped_key.len = wrapped_key_len;
mSymKey = PK11_UnwrapSymKey(mWrapKey, wrap_mechanism, &tmp_sec_item, &wrapped_key,
mechanism, operation, key->len);
}
return mSymKey;
}
void setupEncryptContext(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv,
CryptoType type) override
{
setupCryptoContext(key, iv, type, CKA_ENCRYPT);
}
void setupDecryptContext(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv,
CryptoType type) override
{
setupCryptoContext(key, iv, type, CKA_DECRYPT);
}
void setupCryptoContext(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv,
CryptoType type, CK_ATTRIBUTE_TYPE operation)
{
CK_MECHANISM_TYPE mechanism = static_cast<CK_ULONG>(-1);
SECItem ivItem;
ivItem.type = siBuffer;
if (iv.empty())
ivItem.data = nullptr;
else
ivItem.data = iv.data();
ivItem.len = iv.size();
SECItem* pIvItem = nullptr;
switch (type)
{
case CryptoType::AES_128_ECB:
case CryptoType::AES_256_ECB:
mechanism = CKM_AES_ECB;
break;
case CryptoType::AES_128_CBC:
mechanism = CKM_AES_CBC;
pIvItem = &ivItem;
break;
case CryptoType::AES_256_CBC:
mechanism = CKM_AES_CBC;
pIvItem = &ivItem;
break;
default:
break;
}
mSlot = PK11_GetBestSlot(mechanism, nullptr);
if (!mSlot)
throw css::uno::RuntimeException(u"NSS Slot failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
SECItem keyItem;
keyItem.type = siBuffer;
keyItem.data = key.data();
keyItem.len = key.size();
mSymKey = ImportSymKey(mechanism, CKA_ENCRYPT, &keyItem);
if (!mSymKey)
throw css::uno::RuntimeException(u"NSS SymKey failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
mSecParam = PK11_ParamFromIV(mechanism, pIvItem);
mContext = PK11_CreateContextBySymKey(mechanism, operation, mSymKey, mSecParam);
}
void setupCryptoHashContext(std::vector<sal_uInt8>& rKey, CryptoHashType eType) override
{
CK_MECHANISM_TYPE aMechanism = static_cast<CK_ULONG>(-1);
switch (eType)
{
case CryptoHashType::SHA1:
aMechanism = CKM_SHA_1_HMAC;
break;
case CryptoHashType::SHA256:
aMechanism = CKM_SHA256_HMAC;
break;
case CryptoHashType::SHA384:
aMechanism = CKM_SHA384_HMAC;
break;
case CryptoHashType::SHA512:
aMechanism = CKM_SHA512_HMAC;
break;
}
mSlot = PK11_GetBestSlot(aMechanism, nullptr);
if (!mSlot)
throw css::uno::RuntimeException(u"NSS Slot failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
SECItem aKeyItem;
aKeyItem.data = rKey.data();
aKeyItem.len = rKey.size();
mSymKey = ImportSymKey(aMechanism, CKA_SIGN, &aKeyItem);
if (!mSymKey)
throw css::uno::RuntimeException(u"NSS SymKey failure"_ustr,
css::uno::Reference<css::uno::XInterface>());
SECItem param;
param.data = nullptr;
param.len = 0;
mContext = PK11_CreateContextBySymKey(aMechanism, CKA_SIGN, mSymKey, ¶m);
// Also call digest to begin
PK11_DigestBegin(mContext);
}
sal_uInt32 decryptUpdate(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
sal_uInt32 inputLength) override
{
if (!mContext)
return 0;
int outputLength = 0;
(void)PK11_CipherOp(mContext, output.data(), &outputLength, inputLength, input.data(),
inputLength);
return outputLength;
}
sal_uInt32 encryptUpdate(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
sal_uInt32 inputLength) override
{
if (!mContext)
return 0;
int outputLength = 0;
(void)PK11_CipherOp(mContext, output.data(), &outputLength, inputLength, input.data(),
inputLength);
return outputLength;
}
bool cryptoHashUpdate(std::vector<sal_uInt8>& rInput, sal_uInt32 nInputLength) override
{
return PK11_DigestOp(mContext, rInput.data(), nInputLength) == SECSuccess;
}
bool cryptoHashFinalize(std::vector<sal_uInt8>& rHash) override
{
unsigned int nSizeWritten = 0;
PK11_DigestFinal(mContext, rHash.data(), &nSizeWritten, rHash.size());
return nSizeWritten == rHash.size();
}
};
} // anonymous namespace
std::shared_ptr<ICryptoImplementation> ICryptoImplementation::createInstance()
{
return std::shared_ptr<ICryptoImplementation>(new CryptoImplementationNSS);
}
} // namespace comphelper
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|