| 624 | |
| 625 | #ifdef _WIN32 |
| 626 | bool |
| 627 | create_dir_iv(CryptContext *con, LPCWSTR path) |
| 628 | { |
| 629 | |
| 630 | if (con && !con->GetConfig()->DirIV()) |
| 631 | return true; |
| 632 | |
| 633 | DWORD error = 0; |
| 634 | HANDLE hfile = INVALID_HANDLE_VALUE; |
| 635 | |
| 636 | try { |
| 637 | |
| 638 | unsigned char diriv[DIR_IV_LEN]; |
| 639 | |
| 640 | if (!get_random_bytes(con, diriv, DIR_IV_LEN)) |
| 641 | throw ((int)(GetLastError() ? GetLastError() : ERROR_OUTOFMEMORY)); |
| 642 | |
| 643 | wstring path_str = path; |
| 644 | |
| 645 | LPCWSTR encpath = &path_str[0]; |
| 646 | |
| 647 | if (!encpath) |
| 648 | throw((int)ERROR_OUTOFMEMORY); |
| 649 | |
| 650 | if (path_str[path_str.size() - 1] != '\\') { |
| 651 | path_str.push_back('\\'); |
| 652 | } |
| 653 | |
| 654 | path_str.append(DIR_IV_NAME); |
| 655 | |
| 656 | hfile = CreateFile(&path_str[0], GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, 0, NULL); |
| 657 | |
| 658 | if (hfile == INVALID_HANDLE_VALUE) |
| 659 | throw((int)ERROR_ACCESS_DENIED); |
| 660 | |
| 661 | DWORD nWritten = 0; |
| 662 | if (!WriteFile(hfile, diriv, DIR_IV_LEN, &nWritten, NULL)) { |
| 663 | throw((int)GetLastError()); |
| 664 | } |
| 665 | |
| 666 | if (nWritten != DIR_IV_LEN) { |
| 667 | throw((int)ERROR_OUTOFMEMORY); |
| 668 | } |
| 669 | |
| 670 | FILETIME LastWriteTime; |
| 671 | |
| 672 | if (!GetFileTime(hfile, NULL, NULL, &LastWriteTime)) { |
| 673 | throw((int)GetLastError()); |
| 674 | } |
| 675 | |
| 676 | CloseHandle(hfile); |
| 677 | hfile = INVALID_HANDLE_VALUE; |
| 678 | |
| 679 | // assume somebody will want to use it soon |
| 680 | if (con) |
| 681 | con->m_dir_iv_cache.store(path, diriv, LastWriteTime); |
| 682 | |
| 683 | DWORD attr = GetFileAttributesW(&path_str[0]); |
no test coverage detected