📄 warwin32registry.cpp
字号:
// Remap the name to <keyname>.cr // That way we can recognize encrypted values. cr_path << valueName << _T(".cr"); valueName = cr_path.GetPath(); mCrypto.Encrypt(encr_value, newValue); new_value = (CONST BYTE *)(encr_value.c_str()); bytes = (encr_value.size() +1) * sizeof(TCHAR); } else#endif // WAR_CRYPTO { new_value = (CONST BYTE *)(newValue.c_str()); bytes = (newValue.size() +1) * sizeof(TCHAR); } if ((status = ::RegSetValueEx(mNodeKey, valueName, 0, REG_SZ, new_value, bytes)) != ERROR_SUCCESS) { WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, status), NULL); }}void WarWin32Registry::SetValue(war_registrypath_ccstr_t valueName, DWORD newValue) const throw(WarException){ LONG status = 0; if ((status = ::RegSetValueEx(mNodeKey, valueName, 0, REG_DWORD, (CONST BYTE *)(&newValue), sizeof(DWORD))) != ERROR_SUCCESS) { WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, status), NULL); }}void WarWin32Registry::DeleteKey(war_registrypath_ccstr_t keyName) { if (keyName == NULL) { WarWin32Registry my_key; Reset(); my_key.Open(mRootKey, mPath.GetPathname().GetPath(), KEY_WRITE); my_key.DeleteKey(mPath.GetFilename().GetPath()); return; } LONG status = ::SHDeleteKey(mNodeKey, keyName); if (status != ERROR_SUCCESS) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, status), NULL);}void WarWin32Registry::DeleteValue(war_registrypath_ccstr_t valueName) const{ LONG status = ::RegDeleteValue(mNodeKey, valueName); if (status != ERROR_SUCCESS) { switch(status) { case ERROR_FILE_NOT_FOUND: WarThrow(WarError(WAR_ERR_OBJECT_NOT_FOUND, status), NULL); default: WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, status), NULL); } }}void WarWin32Registry::DeleteAllValues() throw(WarException){ keyname_list_t values; EnumValue(values); for(keyname_list_t::const_iterator P = values.begin() ; P != values.end() ; ++P) { DeleteValue(P->c_str()); }}void WarWin32Registry::EnumKey(keyname_list_t& resultList){ int indedx = 0; LONG status; TCHAR name_buffer[DEFAULT_NAME_BUFFER_LEN]; DWORD name_len = DEFAULT_NAME_BUFFER_LEN, class_len = 0; while((status = ::RegEnumKeyEx(mNodeKey, indedx++, name_buffer, &name_len, NULL, NULL, &class_len, NULL)) == ERROR_SUCCESS) { resultList.push_back(war_regstr_t(name_buffer)); name_len = DEFAULT_NAME_BUFFER_LEN; class_len = 0; } if (ERROR_NO_MORE_ITEMS != status) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, status), NULL);}void WarWin32Registry::EnumValue(keyname_list_t& resultList){ int indedx = 0; LONG status; TCHAR name_buffer[DEFAULT_NAME_BUFFER_LEN]; DWORD name_len = DEFAULT_NAME_BUFFER_LEN; while((status = ::RegEnumValue(mNodeKey, indedx++, name_buffer, &name_len, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS) { resultList.push_back(war_regstr_t(name_buffer)); name_len = DEFAULT_NAME_BUFFER_LEN; } if (ERROR_NO_MORE_ITEMS != status) WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, status), NULL);}void WarWin32Registry::Flush(){}void WarWin32Registry::Rename(war_registrypath_ccstr_t newName) throw(WarException){ if (!newName || !*newName) WarThrow(WarError(WAR_ERR_INVALID_ARGUMENT), NULL); war_registrypath_t new_path = mPath.GetPathname().GetPath(); new_path << WAR_SYSSLASH << newName; try { WarWin32Registry test_key; test_key.Open(mRootKey, new_path, KEY_READ); WarThrow(WarError(WAR_ERR_NAME_EXIST), NULL); } catch(WarException) { } WarWin32Registry new_key, father_key; father_key.Open(mRootKey, mPath.GetPathname().GetPath(), KEY_ALL_ACCESS); new_key.Create(mRootKey, new_path); // Requires the IESDK to be installed. // If you get linking-problems, make sure that the // SDK library path (C:\Program Files\Microsoft SDK\lib) // have higher priority than the VC98 library path. // This is a bug in the install-program for the SDK. DWORD status = SHCopyKey(father_key.mNodeKey, mPath.GetFilename().GetPath(), new_key.mNodeKey, NULL); if (status != ERROR_SUCCESS) { new_key.DeleteKey(); WarThrow(WarError(WAR_ERR_SYSTEM_ERROR, status), NULL); } DeleteKey(); // Steal the new properties from new_key mNodeKey = new_key.mNodeKey; new_key.mNodeKey = NULL; mRootKey = new_key.mRootKey; new_key.mRootKey = NULL; mPath = new_key.mPath;}#if WAR_CRYPTOvoid WarWin32Registry::LoadPamFile() throw(WarException){ WarLog err_log(WARLOG_ERROR, "WarWin32Registry::LoadPamFile()"); war_syspath_t key_file = GetStrValue( WAR_WINNT_REG_PKEYFILE, NULL, true).c_str(); FILE *fp = _tfopen(key_file.GetPath(), _T("rt")); if (NULL == fp) { WarError sys_err(WAR_ERR_SYSTEM_ERROR, errno); err_log << "Failed to open pam file " << key_file.GetPath() << sys_err << war_endl; WarThrow(sys_err, NULL); } EVP_PKEY *key = PEM_read_PrivateKey( fp, NULL, NULL, NULL); fclose(fp); if (NULL == key) { WarOpenSslError sys_err; err_log << "PEM_read_PrivateKey() failed when reading pam file " << key_file.GetPath() << sys_err << war_endl; WarThrow(sys_err, "PEM_read_PrivateKey()"); } char *strkey = BN_bn2hex(key->pkey.dh->priv_key); WarCryptText key_encr(strkey); WarCollector<TCHAR> data_detrypt_key(WarCollector<TCHAR>::SM_ERASE); key_encr.Decrypt(data_detrypt_key.mString, GetStrValue(WAR_WINNT_REG_CRYPTKEY, NULL, true)); mCrypto.SetPassword(WarCollector<char>(data_detrypt_key).GetValue().c_str()); memset(strkey, 0, strlen(strkey)); OPENSSL_free(strkey); EVP_PKEY_free(key);}#endif WAR_CRYPTO//============================= ACCESS ===================================//============================= INQUIRY ===================================const WarWin32Registry::open_t WarWin32Registry::GetRef(war_registrypath_ccstr_t SubKeyName) const{ war_registrypath_t my_path = mPath; if (SubKeyName) my_path << WAR_SYSSLASH << SubKeyName; return open_t(*this, my_path);}bool WarWin32Registry::HaveValue(war_registrypath_ccstr_t valueName, bool evalCrypt)const{ DWORD dw_type = 0, dw_len = 0; LONG status = 0;#if WAR_CRYPTO if (evalCrypt) { war_registrypath_t cr_path; cr_path << valueName << _T(".cr"); if ((status = ::RegQueryValueEx(mNodeKey, cr_path.GetPath(), 0, &dw_type, NULL, &dw_len)) == ERROR_SUCCESS) { return true; } }#endif // WAR_CRYPTO if ((status = ::RegQueryValueEx(mNodeKey, valueName, 0, &dw_type, NULL, &dw_len)) != ERROR_SUCCESS) { if(ERROR_FILE_NOT_FOUND == status) return false; } return true;}bool WarWin32Registry::HaveKey(war_registrypath_ccstr_t keyName)const{ try { WarWin32Registry tmp_key; tmp_key.Open(GetRef(keyName)); } catch(WarException) { return false; } return true;}/////////////////////////////// PROTECTED ////////////////////////////////////////////////////////////////// PRIVATE ///////////////////////////////////void WarWin32Registry::Reset(){ if (IsOpen()) ::CloseHandle(mNodeKey); mNodeKey = NULL;}#if WAR_CRYPTObool WarWin32Registry::GetEncryptedStrValue( war_registrypath_ccstr_t valueName, war_regstr_t& return_val) const throw(WarException){ war_registrypath_t cr_path; cr_path << valueName << _T(".cr"); war_regstr_t hex_encoded_value; try { hex_encoded_value = GetStrValue(cr_path.GetPath(), NULL, true); } catch(WarException& ex) { if (ex.SystemError() == ERROR_FILE_NOT_FOUND) return false; // Not encrypted throw ex; } mCrypto.Decrypt(return_val, hex_encoded_value); return true;}#endif // WAR_CRYPTO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -