| 1 |
#include <windows.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <tchar.h> |
| 4 |
#include "Tombo.h" |
| 5 |
#include "PasswordManager.h" |
| 6 |
#include "PasswordDialog.h" |
| 7 |
#include "CryptManager.h" |
| 8 |
#include "Property.h" |
| 9 |
#include "resource.h" |
| 10 |
#include "Message.h" |
| 11 |
|
| 12 |
#define FINGERPRINT_VERSION 1 |
| 13 |
#define FINGERPRINT_NUM_ENCRYPT 64 |
| 14 |
|
| 15 |
////////////////////////////////////////// |
| 16 |
// global vars |
| 17 |
////////////////////////////////////////// |
| 18 |
|
| 19 |
PasswordManager *g_pPassManager = NULL; |
| 20 |
|
| 21 |
////////////////////////////////////////// |
| 22 |
// デストラクタ |
| 23 |
////////////////////////////////////////// |
| 24 |
|
| 25 |
PasswordManager::~PasswordManager() |
| 26 |
{ |
| 27 |
ForgetPassword(); |
| 28 |
} |
| 29 |
|
| 30 |
////////////////////////////////////////// |
| 31 |
// 初期化 |
| 32 |
////////////////////////////////////////// |
| 33 |
|
| 34 |
BOOL PasswordManager::Init(HWND hWnd, HINSTANCE hInst) |
| 35 |
{ |
| 36 |
hParent = hWnd; |
| 37 |
hInstance = hInst; |
| 38 |
UpdateAccess(); |
| 39 |
return TRUE; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
////////////////////////////////////////// |
| 44 |
// パスワードの取得 |
| 45 |
////////////////////////////////////////// |
| 46 |
|
| 47 |
const char *PasswordManager::Password(BOOL *pCancel, BOOL bEncrypt) |
| 48 |
{ |
| 49 |
if (pPassword) return pPassword; |
| 50 |
|
| 51 |
PasswordDialog dlg; |
| 52 |
|
| 53 |
BOOL bPrev = bDisableHotKey; |
| 54 |
bDisableHotKey = TRUE; |
| 55 |
DWORD nResult = dlg.Popup(hInstance, hParent, bEncrypt); |
| 56 |
bDisableHotKey = bPrev; |
| 57 |
if ( nResult == IDOK) { |
| 58 |
if (dlg.Password() == NULL) { |
| 59 |
TomboMessageBox(hParent, MSG_CANT_GET_PASS, |
| 60 |
TEXT("ERROR"), MB_ICONSTOP | MB_OK); |
| 61 |
return NULL; |
| 62 |
} else { |
| 63 |
// タイマーのセット |
| 64 |
|
| 65 |
if (!SetTimer(hParent, ID_PASSWORDTIMER, 60*1000, NULL)) { |
| 66 |
TomboMessageBox(hParent, MSG_TIMER_SET_FAILED, TEXT("ERROR"), MB_ICONSTOP | MB_OK); |
| 67 |
return NULL; |
| 68 |
} |
| 69 |
|
| 70 |
// パスワードの保持 |
| 71 |
pPassword = new char[strlen(dlg.Password()) + 1]; |
| 72 |
strcpy(pPassword, dlg.Password()); |
| 73 |
return pPassword; |
| 74 |
} |
| 75 |
} else { |
| 76 |
*pCancel = TRUE; |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
////////////////////////////////////////// |
| 83 |
// パスワードの無効化 |
| 84 |
////////////////////////////////////////// |
| 85 |
|
| 86 |
void PasswordManager::ForgetPassword() |
| 87 |
{ |
| 88 |
if (pPassword) { |
| 89 |
char *p = pPassword; |
| 90 |
while (*p) { |
| 91 |
*p++ = '\0'; |
| 92 |
} |
| 93 |
delete [] pPassword; |
| 94 |
pPassword = NULL; |
| 95 |
KillTimer(hParent, ID_PASSWORDTIMER); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
////////////////////////////////////////// |
| 100 |
// FingerPrintの計算 |
| 101 |
////////////////////////////////////////// |
| 102 |
// GetFingerPrint, SetFingerPrintの下請け |
| 103 |
|
| 104 |
|
| 105 |
static BOOL GetFingerPrintFromSeed(LPBYTE pResult, const LPBYTE pSeed, const char *pPass) |
| 106 |
{ |
| 107 |
CryptManager cMgr; |
| 108 |
if (!cMgr.Init(pPass)) return FALSE; |
| 109 |
|
| 110 |
BYTE workbuf1[16], workbuf2[16]; |
| 111 |
DWORD i, count; |
| 112 |
|
| 113 |
for (i = 0; i < 16; i++) workbuf1[i] = pSeed[i]; |
| 114 |
|
| 115 |
for (count = 0; count < FINGERPRINT_NUM_ENCRYPT; count++) { |
| 116 |
// 暗号化 |
| 117 |
if (!cMgr.Encrypt(workbuf1, 16)) return FALSE; |
| 118 |
|
| 119 |
// MD5によるハッシュ化 |
| 120 |
getMD5Sum(workbuf2, workbuf1, 16); |
| 121 |
|
| 122 |
for (i = 0; i < 16; i++) workbuf1[i] = workbuf2[i]; |
| 123 |
} |
| 124 |
for (i = 0; i < 16; i++) pResult[i] = workbuf1[i]; |
| 125 |
|
| 126 |
return TRUE; |
| 127 |
} |
| 128 |
|
| 129 |
////////////////////////////////////////// |
| 130 |
// フィンガープリントの取得 |
| 131 |
////////////////////////////////////////// |
| 132 |
|
| 133 |
BOOL GetFingerPrint(LPBYTE pFp, const char *pPassword) |
| 134 |
{ |
| 135 |
DWORD i; |
| 136 |
|
| 137 |
// FingerPrintのseed取得のための乱数系列初期化 |
| 138 |
SYSTEMTIME st; |
| 139 |
GetSystemTime(&st); |
| 140 |
FILETIME ft; |
| 141 |
SystemTimeToFileTime(&st, &ft); |
| 142 |
srand(ft.dwLowDateTime); |
| 143 |
|
| 144 |
// FingerPrintのseedの取得 |
| 145 |
for (i = 0; i < 16; i++) { |
| 146 |
pFp[i] = (BYTE)(rand() & 0xFF); |
| 147 |
} |
| 148 |
|
| 149 |
// FingerPrintの取得 |
| 150 |
pFp[0] = FINGERPRINT_VERSION; |
| 151 |
BOOL bResult = GetFingerPrintFromSeed(pFp + 17, pFp + 1, pPassword); |
| 152 |
|
| 153 |
return bResult; |
| 154 |
} |
| 155 |
|
| 156 |
////////////////////////////////////////// |
| 157 |
// フィンガープリントのチェック |
| 158 |
////////////////////////////////////////// |
| 159 |
|
| 160 |
BOOL CheckFingerPrint(LPBYTE pFp, const char *pPassword) |
| 161 |
{ |
| 162 |
BYTE result[16]; |
| 163 |
|
| 164 |
if (pFp[0] != FINGERPRINT_VERSION) { |
| 165 |
SetLastError(ERROR_INVALID_DATA); |
| 166 |
return FALSE; |
| 167 |
} |
| 168 |
|
| 169 |
if (!GetFingerPrintFromSeed(result, pFp + 1, pPassword)) return FALSE; |
| 170 |
for (DWORD i = 0; i < 16; i++) { |
| 171 |
if (result[i] != pFp[17 + i]) return FALSE; |
| 172 |
} |
| 173 |
return TRUE; |
| 174 |
} |
| 175 |
|
| 176 |
////////////////////////////////////////// |
| 177 |
// アクセスの続行 |
| 178 |
////////////////////////////////////////// |
| 179 |
void PasswordManager::UpdateAccess() |
| 180 |
{ |
| 181 |
SYSTEMTIME st; |
| 182 |
GetSystemTime(&st); |
| 183 |
SystemTimeToFileTime(&st, &ftLastAccess); |
| 184 |
} |
| 185 |
|
| 186 |
void PasswordManager::ForgetPasswordIfNotAccessed() |
| 187 |
{ |
| 188 |
if (pPassword == NULL) return; |
| 189 |
|
| 190 |
SYSTEMTIME st; |
| 191 |
FILETIME ftNow; |
| 192 |
GetSystemTime(&st); |
| 193 |
SystemTimeToFileTime(&st, &ftNow); |
| 194 |
|
| 195 |
UINT64 utLastAccess = ((UINT64)ftLastAccess.dwHighDateTime << 32) | (UINT64)ftLastAccess.dwLowDateTime; |
| 196 |
UINT64 utNow = ((UINT64)ftNow.dwHighDateTime << 32) | (UINT64)ftNow.dwLowDateTime; |
| 197 |
|
| 198 |
UINT64 utDiff = (utNow - utLastAccess) / 10000000; |
| 199 |
utDiff /= 60; |
| 200 |
|
| 201 |
if (utDiff > g_Property.GetPassTimeout()) { |
| 202 |
SendMessage(hParent, WM_TIMER, 0, 0); |
| 203 |
} |
| 204 |
} |