bool IsStealthAddress(const std::string& encodedAddress) { data_chunk raw; if (!DecodeBase58(encodedAddress, raw)) { //printf("IsStealthAddress DecodeBase58 falied.\n"); return false; }; if (!VerifyChecksum(raw)) { //printf("IsStealthAddress verify_checksum falied.\n"); return false; }; if (raw.size() < 1 + 1 + 33 + 1 + 33 + 1 + 1 + 4) { //printf("IsStealthAddress too few bytes provided.\n"); return false; }; uint8_t* p = &raw[0]; uint8_t version = *p++; if (version != stealth_version_byte) { //printf("IsStealthAddress version mismatch 0x%x != 0x%x.\n", version, stealth_version_byte); return false; }; return true; };
/** Decode a Bech32 string. */ std::pair<std::string, data> Decode(const std::string& str) { bool lower = false, upper = false; for (size_t i = 0; i < str.size(); ++i) { unsigned char c = str[i]; if (c < 33 || c > 126) return {}; if (c >= 'a' && c <= 'z') lower = true; if (c >= 'A' && c <= 'Z') upper = true; } if (lower && upper) return {}; size_t pos = str.rfind('1'); if (str.size() > 1023 || pos == str.npos || pos == 0 || pos + 7 > str.size()) { return {}; } data values(str.size() - 1 - pos); for (size_t i = 0; i < str.size() - 1 - pos; ++i) { unsigned char c = str[i + pos + 1]; int8_t rev = (c < 33 || c > 126) ? -1 : CHARSET_REV[c]; if (rev == -1) { return {}; } values[i] = rev; } std::string hrp; for (size_t i = 0; i < pos; ++i) { hrp += LowerCase(str[i]); } if (!VerifyChecksum(hrp, values)) { return {}; } return {hrp, data(values.begin(), values.end() - 6)}; }
UpdaterState CUpdater::ProcessFinishedDownload() { UpdaterState s = newversion; wxString const temp = GetTempFile(); if( temp.empty() ) { s = newversion; } else if( !VerifyChecksum( temp, version_information_.available_.size_, version_information_.available_.hash_ ) ) { wxLogNull log; wxRemoveFile(temp); s = newversion; } else { s = newversion_ready; wxString local_file = GetLocalFile( version_information_.available_, false ); wxLogNull log; if (local_file.empty() || !wxRenameFile( temp, local_file, false ) ) { s = newversion; wxRemoveFile( temp ); log_ += wxString::Format(_("Could not create local file %s\n"), local_file.c_str()); } else { local_file_ = local_file; log_ += wxString::Format(_("Local file is %s\n"), local_file.c_str()); } } return s; }
/** Check file signature (SHA 256 algorithm) * The function will return ERR if verification fails or if any errors occur, * * @param full_path Full path of file to be checked * * @param signature Base64 encoded checksum * * @return OK if the verification succeded, ERR if there was any error. */ OP_STATUS CheckFileSignature(const OpString &full_path, const OpString8& signature) { OpString resolved; OpString path; OP_MEMORY_VAR BOOL successful; RETURN_IF_ERROR(path.Append("file:")); RETURN_IF_ERROR(path.Append(full_path)); RETURN_IF_LEAVE(successful = g_url_api->ResolveUrlNameL(path, resolved)); if (!successful || resolved.Find("file://") != 0) return OpStatus::ERR; URL url = g_url_api->GetURL(resolved.CStr()); if (!url.QuickLoad(TRUE)) return OpStatus::ERR; // Verify hash if (VerifyChecksum(url, signature, SSL_SHA_256)) return OpStatus::OK; else return OpStatus::ERR; /* if (VerifySignedFile(url, signature, AUTOUPDATE_KEY, sizeof(AUTOUPDATE_KEY), SSL_SHA_256)) return OpStatus::OK; else return OpStatus::ERR; */ }
void CUpdater::RunIfNeeded() { build const b = AvailableBuild(); if( state_ == idle || state_ == failed || LongTimeSinceLastCheck() || (state_ == newversion && !b.url_.empty()) || (state_ == newversion_ready && !VerifyChecksum( DownloadedFile(), b.size_, b.hash_ ) ) ) { Run(); } }
bool CStealthAddress::SetEncoded(const std::string& encodedAddress) { data_chunk raw; if (!DecodeBase58(encodedAddress, raw)) { if (fDebug) printf("CStealthAddress::SetEncoded DecodeBase58 falied.\n"); return false; }; if (!VerifyChecksum(raw)) { if (fDebug) printf("CStealthAddress::SetEncoded verify_checksum falied.\n"); return false; }; if (raw.size() < 1 + 1 + 33 + 1 + 33 + 1 + 1 + 4) { if (fDebug) printf("CStealthAddress::SetEncoded() too few bytes provided.\n"); return false; }; uint8_t* p = &raw[0]; uint8_t version = *p++; if (version != stealth_version_byte) { printf("CStealthAddress::SetEncoded version mismatch 0x%x != 0x%x.\n", version, stealth_version_byte); return false; }; options = *p++; scan_pubkey.resize(33); memcpy(&scan_pubkey[0], p, 33); p += 33; //uint8_t spend_pubkeys = *p++; p++; spend_pubkey.resize(33); memcpy(&spend_pubkey[0], p, 33); return true; };
BOOL CDataIndex::Open(LPCTSTR DataFileName, BOOL UseStarts) { // construct index filename from data filename and read header, then verify checksum if (m_Valid) Close(); // create index for the given data file m_DataFileName = _T(DataFileName); // build index filename CFileSpec fs(DataFileName); fs.SetExt(".ldx"); // build cell starts filename CFileSpec ifs(DataFileName); ifs.SetExt(".ldi"); // open index file and read header m_FileHandle = fopen(fs.GetFullSpec(), "rb"); if (m_FileHandle) { if (ReadHeader()) { if (VerifyChecksum(DataFileName)) { m_Valid = TRUE; } else { fclose(m_FileHandle); m_Valid = FALSE; // check for old header checksum if (VerifyChecksum(DataFileName, TRUE)) { // update index and starts file headers UpdateChecksum(DataFileName); // this has to work since we have already opened the file m_FileHandle = fopen(fs.GetFullSpec(), "rb"); if (m_FileHandle) { ReadHeader(); m_Valid = TRUE; } } } } } // see if there is a starts file if (m_Valid && UseStarts) { FILE* f = fopen(ifs.GetFullSpec(), "rb"); if (f) { if (ReadHeader(f)) { if (VerifyChecksum(DataFileName)) { m_CellStarts = new long[m_Header.GridCellsAcross * m_Header.GridCellsUp]; if (m_CellStarts) { if (fread(m_CellStarts, sizeof(long), m_Header.GridCellsAcross * m_Header.GridCellsUp, f) == (unsigned int) (m_Header.GridCellsAcross * m_Header.GridCellsUp)) m_HaveStarts = TRUE; else delete [] m_CellStarts; } } } fclose(f); // re-read index file header...starts file header was read into m_Header ReadHeader(); } else { // could not read starts file...this is an error since UseStarts was TRUE if (m_FileHandle) fclose(m_FileHandle); m_Valid = FALSE; m_FileHandle = NULL; } } return(m_Valid); }
void CUpdateWizard::OnEngineEvent(wxEvent& event) { if (!m_pEngine) return; if (m_currentPage >= 3) { CNotification *pNotification = m_pEngine->GetNextNotification(); while (pNotification) { delete pNotification; pNotification = m_pEngine->GetNextNotification(); } return; } CNotification *pNotification = m_pEngine->GetNextNotification(); while (pNotification) { switch (pNotification->GetID()) { case nId_logmsg: { if (!m_loaded) break; CLogmsgNotification* pLogMsg = reinterpret_cast<CLogmsgNotification *>(pNotification); if (pLogMsg->msgType == Status || pLogMsg->msgType == Command) { if (!m_currentPage) { wxStaticText *pText = XRCCTRL(*this, "ID_CHECKINGTEXTPROGRESS", wxStaticText); wxString text = pLogMsg->msg; text.Replace(_T("&"), _T("&&")); WrapText(pText, text, m_pages[0]->GetClientSize().x); pText->SetLabel(text); m_pages[0]->GetSizer()->Layout(); wxGauge* pProgress = XRCCTRL(*this, "ID_CHECKINGPROGRESS", wxGauge); int value = pProgress->GetValue(); #ifdef __WXDEBUG__ wxASSERT(value < MAXCHECKPROGRESS); #endif if (value < MAXCHECKPROGRESS) pProgress->SetValue(value + 1); } else if (m_currentPage == 2) { wxStaticText *pText = XRCCTRL(*this, "ID_DOWNLOADPROGRESSTEXT", wxStaticText); wxString text = pLogMsg->msg; text.Replace(_T("&"), _T("&&")); WrapText(pText, text, m_pages[2]->GetClientSize().x); pText->SetLabel(text); m_pages[2]->GetSizer()->Layout(); } } wxString label; switch (pLogMsg->msgType) { case Error: label = _("Error:"); break; case Status: label = _("Status:"); break; case Command: label = _("Command:"); break; case Response: label = _("Response:"); break; default: break; } if (label != _T("")) m_update_log += label + _T(" ") + pLogMsg->msg + _T("\n"); } break; case nId_operation: { COperationNotification* pOpMsg = reinterpret_cast<COperationNotification*>(pNotification); if (pOpMsg->nReplyCode != FZ_REPLY_OK) { while (pNotification) { delete pNotification; pNotification = m_pEngine->GetNextNotification(); } FailedTransfer(); return; } if (!m_inTransfer) { if (m_loaded && !m_currentPage) { wxGauge* pProgress = XRCCTRL(*this, "ID_CHECKINGPROGRESS", wxGauge); pProgress->SetValue(pProgress->GetValue() + 1); } int res = SendTransferCommand(); if (res == FZ_REPLY_WOULDBLOCK) break; else if (res != FZ_REPLY_OK) { FailedTransfer(); break; } } if (!m_loaded || !m_currentPage) { m_pEngine->Command(CDisconnectCommand()); ParseData(); } else if (m_currentPage == 2) { if (!VerifyChecksum()) break; int pos = m_localFile.Find('.', true); wxASSERT(pos > 0); wxRenameFile(m_localFile, m_localFile.Left(pos)); m_localFile = m_localFile.Left(pos); wxStaticText* pText = XRCCTRL(*this, "ID_DOWNLOADCOMPLETE", wxStaticText); wxASSERT(pText); wxButton* pNext = wxDynamicCast(FindWindow(wxID_FORWARD), wxButton); pNext->Enable(); XRCCTRL(*this, "ID_DOWNLOADPROGRESS", wxGauge)->SetValue(100); #ifdef __WXMSW__ pText->SetLabel(_("The most recent version has been downloaded. Click on Finish to close FileZilla and to start the installation.")); #else pText->SetLabel(_("The most recent version has been downloaded. Please install it the same way you installed this version.")); #endif pText->Show(); RewrapPage(2); m_successfully_downloaded = true; } } break; case nId_data: { if (!m_inTransfer) break; wxASSERT(!m_currentPage); CDataNotification* pOpMsg = reinterpret_cast<CDataNotification*>(pNotification); int len; char* data = pOpMsg->Detach(len); if (m_data.Len() + len > 131072) { delete [] data; m_pEngine->Command(CCancelCommand()); FailedTransfer(); break; } for (int i = 0; i < len; i++) { if (!data[i] || (unsigned char)data[i] > 127) { delete [] data; data = 0; m_pEngine->Command(CCancelCommand()); FailedTransfer(); break; } } if (data) { m_data += wxString(data, wxConvUTF8, len); delete [] data; } break; } case nId_asyncrequest: { CAsyncRequestNotification* pData = reinterpret_cast<CAsyncRequestNotification *>(pNotification); if (pData->GetRequestID() == reqId_fileexists) { reinterpret_cast<CFileExistsNotification *>(pData)->overwriteAction = CFileExistsNotification::overwrite; } else if (pData->GetRequestID() == reqId_certificate) { CCertificateNotification* pCertNotification = (CCertificateNotification*)pData; pCertNotification->m_trusted = true; } m_pEngine->SetAsyncRequestReply(pData); } break; case nId_transferstatus: if (!m_loaded) break; if (m_currentPage == 2) { CTransferStatusNotification *pTransferStatusNotification = reinterpret_cast<CTransferStatusNotification *>(pNotification); const CTransferStatus *pStatus = pTransferStatusNotification->GetStatus(); SetTransferStatus(pStatus); } break; default: break; } delete pNotification; pNotification = m_pEngine->GetNextNotification(); } }
wxString CUpdater::GetLocalFile( build const& b, bool allow_existing ) { wxString const fn = GetFilename( b.url_ ); wxString const dl = GetDownloadDir().GetPath(); int i = 1; wxString f = dl + fn; while( CLocalFileSystem::GetFileType(f) != CLocalFileSystem::unknown && (!allow_existing || !VerifyChecksum(f, b.size_, b.hash_))) { if( ++i > 99 ) { return _T(""); } wxString ext; int pos; if( !fn.Right(8).CmpNoCase(_T(".tar.bz2")) ) { pos = fn.size() - 8; } else { pos = fn.Find('.', true); } if( pos == -1 ) { f = dl + fn + wxString::Format(_T(" (%d)"), i); } else { f = dl + fn.Left(pos) + wxString::Format(_T(" (%d)"), i) + fn.Mid(pos); } } return f; }
VOID BLWriteFlashNK(OAL_BLMENU_ITEM *pMenu) { DWORD i = 0; UCHAR ch; DWORD dwImageStart, dwImageLength, dwRecAddr, dwRecLen, dwRecChk; DWORD dwRecNum = 0; LPBYTE lpDest = NULL; OAL_KITL_TYPE bkType; wcscpy(g_bootCfg.filename, L"nk.bin"); if (BL_ERROR == BLSDCardDownload(g_bootCfg.filename)) { OALLog(TEXT("SD boot failed to open file\r\n")); goto CleanUp; } bkType = g_eboot.type; g_eboot.bootDeviceType = BOOT_SDCARD_TYPE; g_eboot.type = DOWNLOAD_TYPE_RAM; for (i=0; i < 7; i++) OEMReadData(1, &ch); if (!OEMReadData (sizeof (DWORD), (LPBYTE) &dwImageStart) || !OEMReadData (sizeof (DWORD), (LPBYTE) &dwImageLength)) { KITLOutputDebugString ("Unable to read image start/length\r\n"); return ; } while ( OEMReadData (sizeof (DWORD), (LPBYTE) &dwRecAddr) && OEMReadData (sizeof (DWORD), (LPBYTE) &dwRecLen) && OEMReadData (sizeof (DWORD), (LPBYTE) &dwRecChk) ) { KITLOutputDebugString(" <> Record [ %d ] dwRecAddr = 0x%x, dwRecLen = 0x%x\r\n", dwRecNum, dwRecAddr, dwRecLen); dwRecNum++; // last record of .bin file uses sentinel values for address and checksum. if (!dwRecAddr && !dwRecChk) { break; } // map the record address (FLASH data is cached, for example) lpDest = OEMMapMemAddr (dwImageStart, dwRecAddr); // read data block if (!OEMReadData (dwRecLen, lpDest)) { KITLOutputDebugString ("****** Data record %d corrupted, ABORT!!! ******\r\n", dwRecNum); return ; } if (!VerifyChecksum (dwRecLen, lpDest, dwRecChk)) { KITLOutputDebugString ("****** Checksum failure on record %d, ABORT!!! ******\r\n", dwRecNum); return ; } // Look for ROMHDR to compute ROM offset. NOTE: romimage guarantees that the record containing // the TOC signature and pointer will always come before the record that contains the ROMHDR contents. // if (dwRecLen == sizeof(ROMHDR) && (*(LPDWORD) OEMMapMemAddr(dwImageStart, dwImageStart+ROM_SIGNATURE_OFFSET) == ROM_SIGNATURE)) { DWORD dwTempOffset = (dwRecAddr - *(LPDWORD)OEMMapMemAddr(dwImageStart, dwImageStart+ ROM_SIGNATURE_OFFSET + sizeof(ULONG))); ROMHDR *pROMHdr = (ROMHDR *)lpDest; // Check to make sure this record really contains the ROMHDR. // if ((pROMHdr->physfirst == (dwImageStart - dwTempOffset)) && (pROMHdr->physlast == (dwImageStart - dwTempOffset + dwImageLength)) && (DWORD)(HIWORD(pROMHdr->dllfirst << 16) <= pROMHdr->dlllast) && (DWORD)(LOWORD(pROMHdr->dllfirst << 16) <= pROMHdr->dlllast)) { KITLOutputDebugString("rom_offset=0x%x.\r\n", dwTempOffset); } } } // while( records remaining ) g_eboot.bootDeviceType = bkType; //g_eboot.type = DOWNLOAD_TYPE_RAM; g_eboot.type = DOWNLOAD_TYPE_FLASHNAND; if (OEMWriteFlash(dwImageStart, dwImageLength)) OALLog(L"BLWriteFlashNK success..\n"); else OALLog(L"BLWriteFlashNK fail..\n"); //write boot device for nand flash if (BLWriteBootCfg(&g_bootCfg)) { OALLog(L" Current settings has been saved\r\n"); } else { OALLog(L"ERROR: Settings save failed!\r\n"); } CleanUp: return; }
// stealth addresses have the ticker suffix bool IsStealthAddress(const std::string& qualAddress) { std::string encodedAddress; #if USE_QUALIFIED_ADDRESSES int nColor; if (!SplitQualifiedAddress(qualAddress, encodedAddress, nColor, fDebug)) { if (fDebug) { printf("StealthAddress::IsStealthAddress: could not split address..\n"); } return false; } #else encodedAddress = qualAddress; #endif data_chunk raw; if (!DecodeBase58(encodedAddress, raw)) { if (fDebug) { printf("IsStealthAddress: DecodeBase58 falied.\n"); } return false; }; if (!VerifyChecksum(raw)) { if (fDebug) { printf("IsStealthAddress: verify_checksum falied.\n"); } return false; }; if (raw.size() < N_COLOR_BYTES + 1 + 1 + 33 + 1 + 33 + 1 + 1 + 4) { if (fDebug) { printf("IsStealthAddress: too few bytes provided.\n"); } return false; }; uint8_t* p = &raw[0]; int nColor = 0; for (int i = 0; i < N_COLOR_BYTES; ++i) { nColor += pow(256, (int) i) * ((int) *p); ++p; } if (!CheckColor(nColor)) { if (fDebug) { printf("IsStealthAddress: Invalid currency %d.\n", nColor); } } uint8_t version = *p++; if (version != stealth_version_byte) { if (fDebug) { printf("IsStealthAddress version mismatch 0x%x != 0x%x.\n", version, stealth_version_byte); } return false; }; return true; };
// color information is stored strting in the second byte so that // addresses for different colors have distinct starting sequences bool CStealthAddress::SetEncoded(const std::string& encodedAddress) { data_chunk raw; if (!DecodeBase58(encodedAddress, raw)) { if (fDebug) printf("CStealthAddress::SetEncoded DecodeBase58 falied.\n"); return false; }; if (!VerifyChecksum(raw)) { if (fDebug) printf("CStealthAddress::SetEncoded verify_checksum falied.\n"); return false; }; size_t nRawSize = raw.size(); // see CStealthAddress::Encoded() // Put color first so that stealth addresses of different currencies look different // N_COLOR_BYTES, 1-version, 1-options, 33-scan_pubkey, 1-#spend_pubkeys, // 33-spend_pubkey, 1-#sigs, 1-?, 4 checksum if (nRawSize < N_COLOR_BYTES + 1 + 1 + 33 + 1 + 33 + 1 + 1 + 4) { if (fDebug) printf("CStealthAddress::SetEncoded() too few bytes provided.\n"); return false; }; uint8_t* p = &raw[0]; // Stealth addresses store color as a simple index little bytes first. this->nColor = 0; for (int i = 0; i < N_COLOR_BYTES; ++i) { this->nColor += pow(256, (int) i) * ((int) *p); ++p; } if (!CheckColor(nColor)) { printf("CStealthAddress::SetEncoded(): Color %d is not valid (1...%d).\n", this->nColor, N_COLORS); } uint8_t version = *p++; if (version != stealth_version_byte) { printf("CStealthAddress::SetEncoded version mismatch 0x%x != 0x%x.\n", version, stealth_version_byte); return false; }; options = *p++; scan_pubkey.resize(33); memcpy(&scan_pubkey[0], p, 33); p += 33; //uint8_t spend_pubkeys = *p++; p++; spend_pubkey.resize(33); memcpy(&spend_pubkey[0], p, 33); return true; };