| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <com/sun/star/lang/IllegalArgumentException.hpp> |
| 21 |
#include <com/sun/star/mozilla/XMozillaBootstrap.hpp> |
| 22 |
#include <com/sun/star/xml/crypto/DigestID.hpp> |
| 23 |
#include <com/sun/star/xml/crypto/CipherID.hpp> |
| 24 |
#include <com/sun/star/lang/XMultiComponentFactory.hpp> |
| 25 |
|
| 26 |
#include <cppuhelper/supportsservice.hxx> |
| 27 |
#include <officecfg/Office/Common.hxx> |
| 28 |
#include <sal/types.h> |
| 29 |
#include <rtl/bootstrap.hxx> |
| 30 |
#include <rtl/string.hxx> |
| 31 |
#include <o3tl/environment.hxx> |
| 32 |
#include <osl/file.hxx> |
| 33 |
#include <osl/process.h> |
| 34 |
#include <osl/thread.h> |
| 35 |
#include <sal/log.hxx> |
| 36 |
#include <comphelper/diagnose_ex.hxx> |
| 37 |
#include <unotools/tempfile.hxx> |
| 38 |
#include <comphelper/singletonref.hxx> |
| 39 |
#include <comphelper/sequence.hxx> |
| 40 |
|
| 41 |
#include <nss/nssinitializer.hxx> |
| 42 |
|
| 43 |
#include "digestcontext.hxx" |
| 44 |
#include "ciphercontext.hxx" |
| 45 |
#include <memory> |
| 46 |
#include <utility> |
| 47 |
#include <vector> |
| 48 |
|
| 49 |
#include <nss.h> |
| 50 |
#include <pk11pub.h> |
| 51 |
#include <secmod.h> |
| 52 |
#include <prerror.h> |
| 53 |
#include <prinit.h> |
| 54 |
|
| 55 |
namespace cssu = css::uno; |
| 56 |
namespace cssl = css::lang; |
| 57 |
|
| 58 |
using namespace com::sun::star; |
| 59 |
|
| 60 |
#define ROOT_CERTS "Root Certs for OpenOffice.org" |
| 61 |
|
| 62 |
extern "C" { |
| 63 |
|
| 64 |
static void nsscrypto_finalize(); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
namespace |
| 69 |
{ |
| 70 |
|
| 71 |
class InitNSSPrivate |
| 72 |
{ |
| 73 |
private: |
| 74 |
std::optional<utl::TempFileNamed> m_oTempFileDatabaseDirectory; |
| 75 |
|
| 76 |
public: |
| 77 |
OUString getTempDatabasePath() |
| 78 |
{ |
| 79 |
if (!m_oTempFileDatabaseDirectory) |
| 80 |
{ |
| 81 |
m_oTempFileDatabaseDirectory.emplace(nullptr, true); |
| 82 |
m_oTempFileDatabaseDirectory->EnableKillingFile(); |
| 83 |
} |
| 84 |
return m_oTempFileDatabaseDirectory->GetFileName(); |
| 85 |
} |
| 86 |
|
| 87 |
void reset() |
| 88 |
{ |
| 89 |
if (m_oTempFileDatabaseDirectory) |
| 90 |
{ |
| 91 |
m_oTempFileDatabaseDirectory.reset(); |
| 92 |
} |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
comphelper::SingletonRef<InitNSSPrivate>* getInitNSSPrivate() |
| 97 |
{ |
| 98 |
static comphelper::SingletonRef<InitNSSPrivate> aInitNSSPrivate; |
| 99 |
return &aInitNSSPrivate; |
| 100 |
} |
| 101 |
|
| 102 |
bool nsscrypto_initialize( const css::uno::Reference< css::uno::XComponentContext > &rxContext, bool & out_nss_init ); |
| 103 |
|
| 104 |
#ifdef XMLSEC_CRYPTO_NSS |
| 105 |
|
| 106 |
void deleteRootsModule() |
| 107 |
{ |
| 108 |
SECMODModule *RootsModule = nullptr; |
| 109 |
SECMODModuleList *list = SECMOD_GetDefaultModuleList(); |
| 110 |
SECMODListLock *lock = SECMOD_GetDefaultModuleListLock(); |
| 111 |
SECMOD_GetReadLock(lock); |
| 112 |
|
| 113 |
while (!RootsModule && list) |
| 114 |
{ |
| 115 |
SECMODModule *module = list->module; |
| 116 |
|
| 117 |
for (int i=0; i < module->slotCount; i++) |
| 118 |
{ |
| 119 |
PK11SlotInfo *slot = module->slots[i]; |
| 120 |
if (PK11_IsPresent(slot)) |
| 121 |
{ |
| 122 |
if (PK11_HasRootCerts(slot)) |
| 123 |
{ |
| 124 |
SAL_INFO("xmlsecurity.xmlsec", "The root certificates module \"" << module->commonName << "\" is already loaded: " << module->dllName); |
| 125 |
|
| 126 |
RootsModule = SECMOD_ReferenceModule(module); |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
list = list->next; |
| 132 |
} |
| 133 |
SECMOD_ReleaseReadLock(lock); |
| 134 |
|
| 135 |
if (!RootsModule) |
| 136 |
return; |
| 137 |
|
| 138 |
PRInt32 modType; |
| 139 |
if (SECSuccess == SECMOD_DeleteModule(RootsModule->commonName, &modType)) |
| 140 |
{ |
| 141 |
SAL_INFO("xmlsecurity.xmlsec", "Deleted module \"" << RootsModule->commonName << "\"."); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
SAL_INFO("xmlsecurity.xmlsec", "Failed to delete \"" << RootsModule->commonName << "\": " << RootsModule->dllName); |
| 146 |
} |
| 147 |
SECMOD_DestroyModule(RootsModule); |
| 148 |
RootsModule = nullptr; |
| 149 |
} |
| 150 |
|
| 151 |
#endif |
| 152 |
|
| 153 |
bool lcl_pathExists(const OUString& sPath) |
| 154 |
{ |
| 155 |
if (sPath.isEmpty()) |
| 156 |
return false; |
| 157 |
|
| 158 |
::osl::DirectoryItem aPathItem; |
| 159 |
OUString sURL; |
| 160 |
osl::FileBase::getFileURLFromSystemPath(sPath, sURL); |
| 161 |
if (::osl::FileBase::E_None == ::osl::DirectoryItem::get(sURL, aPathItem)) |
| 162 |
{ |
| 163 |
::osl::FileStatus aStatus = osl_FileStatus_Mask_Validate; |
| 164 |
if (::osl::FileBase::E_None == aPathItem.getFileStatus(aStatus)) |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
} // namespace |
| 172 |
|
| 173 |
const OUString & ONSSInitializer::getMozillaCurrentProfile(const css::uno::Reference< css::uno::XComponentContext > &rxContext, bool bSetActive) |
| 174 |
{ |
| 175 |
if (m_bIsNSSinitialized) |
| 176 |
return m_sNSSPath; |
| 177 |
if (bSetActive) |
| 178 |
m_bIsNSSinitialized = true; |
| 179 |
|
| 180 |
// first, try to get the profile from "MOZILLA_CERTIFICATE_FOLDER" |
| 181 |
if (OUString pEnv = o3tl::getEnvironment(u"MOZILLA_CERTIFICATE_FOLDER"_ustr); !pEnv.isEmpty()) |
| 182 |
{ |
| 183 |
SAL_INFO( |
| 184 |
"xmlsecurity.xmlsec", |
| 185 |
"Using Mozilla profile from MOZILLA_CERTIFICATE_FOLDER=" << pEnv); |
| 186 |
m_sNSSPath = pEnv; |
| 187 |
} |
| 188 |
|
| 189 |
// second, try to get saved user-preference |
| 190 |
if (m_sNSSPath.isEmpty()) |
| 191 |
{ |
| 192 |
try |
| 193 |
{ |
| 194 |
OUString sUserSetCertPath = |
| 195 |
officecfg::Office::Common::Security::Scripting::CertDir::get().value_or(OUString()); |
| 196 |
|
| 197 |
if (lcl_pathExists(sUserSetCertPath)) |
| 198 |
{ |
| 199 |
SAL_INFO( |
| 200 |
"xmlsecurity.xmlsec", |
| 201 |
"Using Mozilla profile from /org.openoffice.Office.Common/" |
| 202 |
"Security/Scripting/CertDir: " << sUserSetCertPath); |
| 203 |
m_sNSSPath = sUserSetCertPath; |
| 204 |
} |
| 205 |
} |
| 206 |
catch (const uno::Exception &) |
| 207 |
{ |
| 208 |
TOOLS_WARN_EXCEPTION("xmlsecurity.xmlsec", "getMozillaCurrentProfile:"); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// third, dig around to see if there's one default available |
| 213 |
mozilla::MozillaProductType productTypes[3] = { |
| 214 |
mozilla::MozillaProductType_Thunderbird, |
| 215 |
mozilla::MozillaProductType_Firefox, |
| 216 |
mozilla::MozillaProductType_Mozilla }; |
| 217 |
|
| 218 |
uno::Reference<uno::XInterface> xInstance = rxContext->getServiceManager()->createInstanceWithContext(u"com.sun.star.mozilla.MozillaBootstrap"_ustr, rxContext); |
| 219 |
OSL_ENSURE( xInstance.is(), "failed to create instance" ); |
| 220 |
|
| 221 |
uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap(xInstance,uno::UNO_QUERY); |
| 222 |
OSL_ENSURE( xMozillaBootstrap.is(), "failed to create instance" ); |
| 223 |
|
| 224 |
if (xMozillaBootstrap.is()) |
| 225 |
{ |
| 226 |
for (auto const productTypeIter : productTypes) |
| 227 |
{ |
| 228 |
OUString profile = xMozillaBootstrap->getDefaultProfile(productTypeIter); |
| 229 |
|
| 230 |
if (!profile.isEmpty()) |
| 231 |
{ |
| 232 |
OUString sProfilePath = xMozillaBootstrap->getProfilePath(productTypeIter, profile); |
| 233 |
if (m_sNSSPath.isEmpty()) |
| 234 |
{ |
| 235 |
SAL_INFO("xmlsecurity.xmlsec", "Using Mozilla profile " << sProfilePath); |
| 236 |
m_sNSSPath = sProfilePath; |
| 237 |
} |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
SAL_INFO_IF(m_sNSSPath.isEmpty(), "xmlsecurity.xmlsec", "No Mozilla profile found"); |
| 244 |
return m_sNSSPath; |
| 245 |
} |
| 246 |
|
| 247 |
css::uno::Sequence<css::xml::crypto::NSSProfile> SAL_CALL ONSSInitializer::getNSSProfiles() |
| 248 |
{ |
| 249 |
ONSSInitializer::getMozillaCurrentProfile(m_xContext); |
| 250 |
|
| 251 |
std::vector<xml::crypto::NSSProfile> aProfileList; |
| 252 |
aProfileList.reserve(10); |
| 253 |
|
| 254 |
mozilla::MozillaProductType productTypes[3] = { |
| 255 |
mozilla::MozillaProductType_Thunderbird, |
| 256 |
mozilla::MozillaProductType_Firefox, |
| 257 |
mozilla::MozillaProductType_Mozilla }; |
| 258 |
|
| 259 |
uno::Reference<uno::XInterface> xInstance = m_xContext->getServiceManager()->createInstanceWithContext(u"com.sun.star.mozilla.MozillaBootstrap"_ustr, m_xContext); |
| 260 |
OSL_ENSURE(xInstance.is(), "failed to create instance" ); |
| 261 |
|
| 262 |
uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap(xInstance,uno::UNO_QUERY); |
| 263 |
|
| 264 |
if (xMozillaBootstrap.is()) |
| 265 |
{ |
| 266 |
for (auto const productTypeIter : productTypes) |
| 267 |
{ |
| 268 |
uno::Sequence<OUString> aProductProfileList; |
| 269 |
xMozillaBootstrap->getProfileList(productTypeIter, aProductProfileList); |
| 270 |
for (const auto& sProfile : aProductProfileList) |
| 271 |
aProfileList.push_back({sProfile, xMozillaBootstrap->getProfilePath(productTypeIter, sProfile), productTypeIter}); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
OUString sUserSelect; |
| 276 |
try |
| 277 |
{ |
| 278 |
sUserSelect = officecfg::Office::Common::Security::Scripting::CertDir::get().value_or(OUString());; |
| 279 |
if (!lcl_pathExists(sUserSelect)) |
| 280 |
sUserSelect = OUString(); |
| 281 |
} |
| 282 |
catch (const uno::Exception &) |
| 283 |
{ |
| 284 |
TOOLS_WARN_EXCEPTION("xmlsecurity.xmlsec", "getMozillaCurrentProfile:"); |
| 285 |
} |
| 286 |
aProfileList.push_back({u"MANUAL"_ustr, sUserSelect, mozilla::MozillaProductType_Default}); |
| 287 |
|
| 288 |
OUString pEnv = o3tl::getEnvironment(u"MOZILLA_CERTIFICATE_FOLDER"_ustr); |
| 289 |
aProfileList.push_back({u"MOZILLA_CERTIFICATE_FOLDER"_ustr, |
| 290 |
pEnv, |
| 291 |
mozilla::MozillaProductType_Default}); |
| 292 |
|
| 293 |
return comphelper::containerToSequence(aProfileList); |
| 294 |
} |
| 295 |
|
| 296 |
bool ONSSInitializer::m_bIsNSSinitialized = false; |
| 297 |
OUString ONSSInitializer::m_sNSSPath; |
| 298 |
|
| 299 |
OUString SAL_CALL ONSSInitializer::getNSSPath() |
| 300 |
{ |
| 301 |
ONSSInitializer::getMozillaCurrentProfile(m_xContext); |
| 302 |
return m_sNSSPath; |
| 303 |
}; |
| 304 |
|
| 305 |
sal_Bool SAL_CALL ONSSInitializer::getIsNSSinitialized() |
| 306 |
{ |
| 307 |
return m_bIsNSSinitialized; |
| 308 |
} |
| 309 |
|
| 310 |
ONSSInitializer::ONSSInitializer(css::uno::Reference< css::uno::XComponentContext > xContext) |
| 311 |
: m_xContext(std::move(xContext)) |
| 312 |
{ |
| 313 |
} |
| 314 |
|
| 315 |
ONSSInitializer::ONSSInitializer() |
| 316 |
{ |
| 317 |
} |
| 318 |
|
| 319 |
namespace |
| 320 |
{ |
| 321 |
|
| 322 |
//Older versions of Firefox (FF), for example FF2, and Thunderbird (TB) 2 write |
| 323 |
//the roots certificate module (libnssckbi.so), which they use, into the |
| 324 |
//profile. This module will then already be loaded during NSS_Init (and the |
| 325 |
//other init functions). This fails in two cases. First, FF3 was used to create |
| 326 |
//the profile, or possibly used that profile before, and second the profile was |
| 327 |
//used on a different platform. |
| 328 |
// |
| 329 |
//Then one needs to add the roots module oneself. This should be done with |
| 330 |
//SECMOD_LoadUserModule rather than SECMOD_AddNewModule. The latter would write |
| 331 |
//the location of the roots module to the profile, which makes FF2 and TB2 use |
| 332 |
//it instead of their own module. |
| 333 |
// |
| 334 |
//When using SYSTEM_NSS then the libnss3.so lib is typically found in /usr/lib. |
| 335 |
//This folder may, however, NOT contain the roots certificate module. That is, |
| 336 |
//just providing the library name in SECMOD_LoadUserModule or |
| 337 |
//SECMOD_AddNewModule will FAIL to load the mozilla unless the LD_LIBRARY_PATH |
| 338 |
//contains an FF or TB installation. |
| 339 |
//ATTENTION: DO NOT call this function directly instead use initNSS |
| 340 |
//return true - whole initialization was successful |
| 341 |
//param out_nss_init = true: at least the NSS initialization (NSS_InitReadWrite |
| 342 |
//was successful and therefore NSS_Shutdown should be called when terminating. |
| 343 |
bool nsscrypto_initialize(css::uno::Reference<css::uno::XComponentContext> const & rxContext, bool & out_nss_init) |
| 344 |
{ |
| 345 |
// this method must be called only once, no need for additional lock |
| 346 |
OString sCertDir; |
| 347 |
|
| 348 |
#ifdef XMLSEC_CRYPTO_NSS |
| 349 |
sCertDir = OUStringToOString(ONSSInitializer::getMozillaCurrentProfile(rxContext, true), osl_getThreadTextEncoding()); |
| 350 |
#else |
| 351 |
(void) rxContext; |
| 352 |
#endif |
| 353 |
SAL_INFO("xmlsecurity.xmlsec", "Using profile: " << sCertDir ); |
| 354 |
|
| 355 |
PR_Init( PR_USER_THREAD, PR_PRIORITY_NORMAL, 1 ) ; |
| 356 |
|
| 357 |
bool bSuccess = false; |
| 358 |
// there might be no profile |
| 359 |
if (!sCertDir.isEmpty()) |
| 360 |
{ |
| 361 |
if (sCertDir.indexOf(':') == -1) //might be env var with explicit prefix |
| 362 |
{ |
| 363 |
OUString sCertDirURL; |
| 364 |
osl::FileBase::getFileURLFromSystemPath( |
| 365 |
OStringToOUString(sCertDir, osl_getThreadTextEncoding()), |
| 366 |
sCertDirURL); |
| 367 |
osl::DirectoryItem item; |
| 368 |
if (osl::FileBase::E_NOENT != osl::DirectoryItem::get(sCertDirURL + "/cert8.db", item) && |
| 369 |
osl::FileBase::E_NOENT == osl::DirectoryItem::get(sCertDirURL + "/cert9.db", item)) |
| 370 |
{ |
| 371 |
SAL_INFO("xmlsecurity.xmlsec", "nsscrypto_initialize: trying to avoid profile migration"); |
| 372 |
sCertDir = "dbm:" + sCertDir; |
| 373 |
} |
| 374 |
} |
| 375 |
if (NSS_InitReadWrite(sCertDir.getStr()) != SECSuccess) |
| 376 |
{ |
| 377 |
SAL_INFO("xmlsecurity.xmlsec", "Initializing NSS with profile failed."); |
| 378 |
int errlen = PR_GetErrorTextLength(); |
| 379 |
if (errlen > 0) |
| 380 |
{ |
| 381 |
std::unique_ptr<char[]> const error(new char[errlen + 1]); |
| 382 |
PR_GetErrorText(error.get()); |
| 383 |
SAL_INFO("xmlsecurity.xmlsec", error.get()); |
| 384 |
} |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
bSuccess = true; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if (!bSuccess) // Try to create a database in temp dir |
| 393 |
{ |
| 394 |
SAL_INFO("xmlsecurity.xmlsec", "Initializing NSS with a temporary profile."); |
| 395 |
OUString rString = (*getInitNSSPrivate())->getTempDatabasePath(); |
| 396 |
|
| 397 |
if (NSS_InitReadWrite(rString.toUtf8().getStr()) != SECSuccess) |
| 398 |
{ |
| 399 |
SAL_INFO("xmlsecurity.xmlsec", "Initializing NSS with a temporary profile."); |
| 400 |
int errlen = PR_GetErrorTextLength(); |
| 401 |
if(errlen > 0) |
| 402 |
{ |
| 403 |
std::unique_ptr<char[]> const error(new char[errlen + 1]); |
| 404 |
PR_GetErrorText(error.get()); |
| 405 |
SAL_INFO("xmlsecurity.xmlsec", error.get()); |
| 406 |
} |
| 407 |
return false; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
// Initialize and set empty password if needed |
| 412 |
// note: it's possible that the first NSS_InitReadWrite() succeeds by |
| 413 |
// creating a new DB; in this case it may also be necessary to call |
| 414 |
// PK11_InitPin() |
| 415 |
PK11SlotInfo* pSlot = PK11_GetInternalKeySlot(); |
| 416 |
if (pSlot) |
| 417 |
{ |
| 418 |
if (PK11_NeedUserInit(pSlot)) |
| 419 |
PK11_InitPin(pSlot, nullptr, nullptr); |
| 420 |
PK11_FreeSlot(pSlot); |
| 421 |
} |
| 422 |
|
| 423 |
out_nss_init = true; |
| 424 |
|
| 425 |
#ifdef XMLSEC_CRYPTO_NSS |
| 426 |
bool return_value = true; |
| 427 |
|
| 428 |
#if defined SYSTEM_NSS || defined IOS // The statically linked nss on iOS acts as a "system" nss in this regards |
| 429 |
if (!SECMOD_HasRootCerts()) |
| 430 |
#endif |
| 431 |
{ |
| 432 |
deleteRootsModule(); |
| 433 |
|
| 434 |
#ifdef IOS // Use statically linked NSS |
| 435 |
OUString rootModulePath("NSSCKBI"); |
| 436 |
|
| 437 |
if (true) |
| 438 |
#else |
| 439 |
#if defined SYSTEM_NSS || defined ANDROID |
| 440 |
OUString rootModule(u"libnssckbi" SAL_DLLEXTENSION ""_ustr); |
| 441 |
#else |
| 442 |
OUString rootModule("${LO_LIB_DIR}/libnssckbi" SAL_DLLEXTENSION); |
| 443 |
#endif |
| 444 |
::rtl::Bootstrap::expandMacros(rootModule); |
| 445 |
|
| 446 |
OUString rootModulePath; |
| 447 |
if (::osl::File::E_None == ::osl::File::getSystemPathFromFileURL(rootModule, rootModulePath)) |
| 448 |
#endif |
| 449 |
{ |
| 450 |
OString ospath = OUStringToOString(rootModulePath, osl_getThreadTextEncoding()); |
| 451 |
OString aStr = "name=\"" ROOT_CERTS "\" library=\"" + ospath + "\""; |
| 452 |
|
| 453 |
SECMODModule * RootsModule = |
| 454 |
SECMOD_LoadUserModule( |
| 455 |
const_cast<char*>(aStr.getStr()), |
| 456 |
nullptr, // no parent |
| 457 |
PR_FALSE); // do not recurse |
| 458 |
|
| 459 |
if (RootsModule) |
| 460 |
{ |
| 461 |
|
| 462 |
bool found = RootsModule->loaded; |
| 463 |
|
| 464 |
SECMOD_DestroyModule(RootsModule); |
| 465 |
RootsModule = nullptr; |
| 466 |
if (found) |
| 467 |
SAL_INFO("xmlsecurity.xmlsec", "Added new root certificate module " ROOT_CERTS " contained in " << ospath); |
| 468 |
else |
| 469 |
{ |
| 470 |
SAL_INFO("xmlsecurity.xmlsec", "FAILED to load the new root certificate module " ROOT_CERTS "contained in " << ospath); |
| 471 |
return_value = false; |
| 472 |
} |
| 473 |
} |
| 474 |
else |
| 475 |
{ |
| 476 |
SAL_INFO("xmlsecurity.xmlsec", "FAILED to add new root certificate module " ROOT_CERTS " contained in " << ospath); |
| 477 |
return_value = false; |
| 478 |
|
| 479 |
} |
| 480 |
} |
| 481 |
else |
| 482 |
{ |
| 483 |
SAL_INFO("xmlsecurity.xmlsec", "Adding new root certificate module failed."); |
| 484 |
return_value = false; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
return return_value; |
| 489 |
#else |
| 490 |
return true; |
| 491 |
#endif |
| 492 |
} |
| 493 |
|
| 494 |
} // namespace |
| 495 |
|
| 496 |
// must be extern "C" because we pass the function pointer to atexit |
| 497 |
extern "C" void nsscrypto_finalize() |
| 498 |
{ |
| 499 |
SECMODModule *RootsModule = SECMOD_FindModule(ROOT_CERTS); |
| 500 |
|
| 501 |
if (RootsModule) |
| 502 |
{ |
| 503 |
|
| 504 |
if (SECSuccess == SECMOD_UnloadUserModule(RootsModule)) |
| 505 |
{ |
| 506 |
SAL_INFO("xmlsecurity.xmlsec", "Unloaded module \"" ROOT_CERTS "\"."); |
| 507 |
} |
| 508 |
else |
| 509 |
{ |
| 510 |
SAL_INFO("xmlsecurity.xmlsec", "Failed unloading module \"" ROOT_CERTS "\"."); |
| 511 |
} |
| 512 |
SECMOD_DestroyModule(RootsModule); |
| 513 |
} |
| 514 |
else |
| 515 |
{ |
| 516 |
SAL_INFO("xmlsecurity.xmlsec", "Unloading module \"" ROOT_CERTS "\" failed because it was not found."); |
| 517 |
} |
| 518 |
PK11_LogoutAll(); |
| 519 |
(void)NSS_Shutdown(); |
| 520 |
|
| 521 |
(*getInitNSSPrivate())->reset(); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
ONSSInitializer::~ONSSInitializer() |
| 526 |
{ |
| 527 |
} |
| 528 |
|
| 529 |
bool ONSSInitializer::initNSS( const css::uno::Reference< css::uno::XComponentContext > &rxContext ) |
| 530 |
{ |
| 531 |
static bool gbInitialized = [&rxContext]() |
| 532 |
{ |
| 533 |
bool bNSSInit = false; |
| 534 |
bool bInitialized = nsscrypto_initialize( rxContext, bNSSInit ); |
| 535 |
if (bNSSInit) |
| 536 |
atexit(nsscrypto_finalize); |
| 537 |
return bInitialized; |
| 538 |
}(); |
| 539 |
return gbInitialized; |
| 540 |
} |
| 541 |
|
| 542 |
css::uno::Reference< css::xml::crypto::XDigestContext > SAL_CALL ONSSInitializer::getDigestContext( ::sal_Int32 nDigestID, const css::uno::Sequence< css::beans::NamedValue >& aParams ) |
| 543 |
{ |
| 544 |
SECOidTag nNSSDigestID = SEC_OID_UNKNOWN; |
| 545 |
sal_Int32 nDigestLength = 0; |
| 546 |
bool b1KData = false; |
| 547 |
if ( nDigestID == css::xml::crypto::DigestID::SHA256 |
| 548 |
|| nDigestID == css::xml::crypto::DigestID::SHA256_1K ) |
| 549 |
{ |
| 550 |
nNSSDigestID = SEC_OID_SHA256; |
| 551 |
nDigestLength = 32; |
| 552 |
b1KData = ( nDigestID == css::xml::crypto::DigestID::SHA256_1K ); |
| 553 |
} |
| 554 |
else if ( nDigestID == css::xml::crypto::DigestID::SHA1 |
| 555 |
|| nDigestID == css::xml::crypto::DigestID::SHA1_1K ) |
| 556 |
{ |
| 557 |
nNSSDigestID = SEC_OID_SHA1; |
| 558 |
nDigestLength = 20; |
| 559 |
b1KData = ( nDigestID == css::xml::crypto::DigestID::SHA1_1K ); |
| 560 |
} |
| 561 |
else if ( nDigestID == css::xml::crypto::DigestID::SHA512 |
| 562 |
|| nDigestID == css::xml::crypto::DigestID::SHA512_1K ) |
| 563 |
{ |
| 564 |
nNSSDigestID = SEC_OID_SHA512; |
| 565 |
nDigestLength = 64; |
| 566 |
b1KData = ( nDigestID == css::xml::crypto::DigestID::SHA512_1K ); |
| 567 |
} |
| 568 |
else |
| 569 |
throw css::lang::IllegalArgumentException(u"Unexpected digest requested."_ustr, css::uno::Reference< css::uno::XInterface >(), 1 ); |
| 570 |
|
| 571 |
if ( aParams.hasElements() ) |
| 572 |
throw css::lang::IllegalArgumentException(u"Unexpected arguments provided for digest creation."_ustr, css::uno::Reference< css::uno::XInterface >(), 2 ); |
| 573 |
|
| 574 |
if( !initNSS( m_xContext ) ) |
| 575 |
return nullptr; |
| 576 |
|
| 577 |
PK11Context* pContext = PK11_CreateDigestContext( nNSSDigestID ); |
| 578 |
if ( !pContext || PK11_DigestBegin( pContext ) != SECSuccess ) |
| 579 |
return nullptr; |
| 580 |
|
| 581 |
return new ODigestContext( pContext, nDigestLength, b1KData ); |
| 582 |
} |
| 583 |
|
| 584 |
css::uno::Reference< css::xml::crypto::XCipherContext > SAL_CALL ONSSInitializer::getCipherContext( ::sal_Int32 nCipherID, const css::uno::Sequence< ::sal_Int8 >& aKey, const css::uno::Sequence< ::sal_Int8 >& aInitializationVector, sal_Bool bEncryption, const css::uno::Sequence< css::beans::NamedValue >& aParams ) |
| 585 |
{ |
| 586 |
CK_MECHANISM_TYPE nNSSCipherID = 0; |
| 587 |
bool bW3CPadding = false; |
| 588 |
switch (nCipherID) |
| 589 |
{ |
| 590 |
case css::xml::crypto::CipherID::AES_CBC_W3C_PADDING: |
| 591 |
nNSSCipherID = CKM_AES_CBC; |
| 592 |
bW3CPadding = true; |
| 593 |
break; |
| 594 |
case css::xml::crypto::CipherID::AES_GCM_W3C: |
| 595 |
nNSSCipherID = CKM_AES_GCM; |
| 596 |
break; |
| 597 |
default: |
| 598 |
throw css::lang::IllegalArgumentException(u"Unexpected cipher requested."_ustr, css::uno::Reference< css::uno::XInterface >(), 1); |
| 599 |
} |
| 600 |
|
| 601 |
if ( aKey.getLength() != 16 && aKey.getLength() != 24 && aKey.getLength() != 32 ) |
| 602 |
throw css::lang::IllegalArgumentException(u"Unexpected key length."_ustr, css::uno::Reference< css::uno::XInterface >(), 2 ); |
| 603 |
|
| 604 |
if ( aParams.hasElements() ) |
| 605 |
throw css::lang::IllegalArgumentException(u"Unexpected arguments provided for cipher creation."_ustr, css::uno::Reference< css::uno::XInterface >(), 5 ); |
| 606 |
|
| 607 |
css::uno::Reference< css::xml::crypto::XCipherContext > xResult; |
| 608 |
if( initNSS( m_xContext ) ) |
| 609 |
{ |
| 610 |
if ( aInitializationVector.getLength() != PK11_GetIVLength( nNSSCipherID ) ) |
| 611 |
throw css::lang::IllegalArgumentException(u"Unexpected length of initialization vector."_ustr, css::uno::Reference< css::uno::XInterface >(), 3 ); |
| 612 |
|
| 613 |
xResult = OCipherContext::Create( nNSSCipherID, aKey, aInitializationVector, bEncryption, bW3CPadding ); |
| 614 |
assert(xResult.is()); |
| 615 |
} |
| 616 |
|
| 617 |
return xResult; |
| 618 |
} |
| 619 |
|
| 620 |
/* XServiceInfo */ |
| 621 |
OUString SAL_CALL ONSSInitializer::getImplementationName() |
| 622 |
{ |
| 623 |
return u"com.sun.star.xml.crypto.NSSInitializer"_ustr; |
| 624 |
} |
| 625 |
|
| 626 |
sal_Bool SAL_CALL ONSSInitializer::supportsService( const OUString& rServiceName ) |
| 627 |
{ |
| 628 |
return cppu::supportsService(this, rServiceName); |
| 629 |
} |
| 630 |
|
| 631 |
cssu::Sequence< OUString > SAL_CALL ONSSInitializer::getSupportedServiceNames( ) |
| 632 |
{ |
| 633 |
return { NSS_SERVICE_NAME }; |
| 634 |
} |
| 635 |
|
| 636 |
#ifndef XMLSEC_CRYPTO_NSS |
| 637 |
extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* |
| 638 |
com_sun_star_xml_crypto_NSSInitializer_get_implementation( |
| 639 |
uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/) |
| 640 |
{ |
| 641 |
return cppu::acquire(new ONSSInitializer(pCtx)); |
| 642 |
} |
| 643 |
#endif |
| 644 |
|
| 645 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |