| 369 | } |
| 370 | |
| 371 | void CryptoManager::lockAndReadHeader(thread_db* tdbb, unsigned flags) |
| 372 | { |
| 373 | if (flags & CRYPT_HDR_INIT) |
| 374 | { |
| 375 | ThreadStatusGuard guard(tdbb); |
| 376 | if (LCK_lock(tdbb, stateLock, CRYPT_INIT, LCK_NO_WAIT)) |
| 377 | { |
| 378 | LCK_write_data(tdbb, stateLock, 1); |
| 379 | if (!LCK_convert(tdbb, stateLock, CRYPT_NORMAL, LCK_NO_WAIT)) |
| 380 | { |
| 381 | fb_assert(tdbb->tdbb_status_vector->getState() & IStatus::STATE_ERRORS); |
| 382 | ERR_punt(); |
| 383 | } |
| 384 | } |
| 385 | else if (!LCK_lock(tdbb, stateLock, CRYPT_NORMAL, LCK_WAIT)) |
| 386 | { |
| 387 | fb_assert(false); |
| 388 | } |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | ThreadStatusGuard guard(tdbb); |
| 393 | if (!LCK_convert(tdbb, stateLock, CRYPT_NORMAL, |
| 394 | (flags & CRYPT_HDR_NOWAIT) ? LCK_NO_WAIT : LCK_WAIT)) |
| 395 | { |
| 396 | // Failed to take state lock - switch to slow IO mode |
| 397 | slowIO = LCK_read_data(tdbb, stateLock); |
| 398 | fb_assert(slowIO); |
| 399 | } |
| 400 | else |
| 401 | slowIO = 0; |
| 402 | } |
| 403 | |
| 404 | PhysHdr hdr(tdbb); |
| 405 | crypt = hdr->hdr_flags & Ods::hdr_encrypted; |
| 406 | process = hdr->hdr_flags & Ods::hdr_crypt_process; |
| 407 | |
| 408 | if (flags & CRYPT_RELOAD_PLUGIN && cryptPlugin) |
| 409 | { |
| 410 | PluginManagerInterfacePtr()->releasePlugin(cryptPlugin); |
| 411 | cryptPlugin = NULL; |
| 412 | } |
| 413 | |
| 414 | // tdbb w/o attachment comes when database is shutting down in the end of detachDatabase() |
| 415 | // the only needed here page is header, i.e. we can live w/o cryptPlugin |
| 416 | if ((crypt || process) && !cryptPlugin && tdbb->getAttachment()) |
| 417 | { |
| 418 | ClumpletWriter hc(ClumpletWriter::UnTagged, hdr->hdr_page_size); |
| 419 | hdr.getClumplets(hc); |
| 420 | if (hc.find(Ods::HDR_crypt_key)) |
| 421 | hc.getString(keyName); |
| 422 | else |
| 423 | keyName = ""; |
| 424 | |
| 425 | loadPlugin(tdbb, hdr->hdr_crypt_plugin); |
| 426 | pluginName = hdr->hdr_crypt_plugin; |
| 427 | string valid; |
| 428 | calcValidation(valid, cryptPlugin); |
nothing calls this directly
no test coverage detected