public byte CreateSignature(ClientCredits pTarget, byte[] pachOutput, byte nMaxSize, uint ChallengeIP, byte byChaIPKind, RSAPKCS1SignatureFormatter sigkey) { // sigkey param is used for debug only if (sigkey == null) { sigkey = signkey_; } // create a signature of the public key from pTarget byte nResult; if (!IsCryptoAvailable) { return(0); } try { byte[] abyBuffer = new byte[CreditStruct.MAXPUBKEYSIZE + 9]; uint keylen = pTarget.SecIDKeyLen; Array.Copy(pTarget.SecureIdent, abyBuffer, keylen); // 4 additional bytes random data send from this client uint challenge = pTarget.CryptRndChallengeFrom; Array.Copy(BitConverter.GetBytes(challenge), 0, abyBuffer, keylen, 4); ushort ChIpLen = 0; if (byChaIPKind != 0) { ChIpLen = 5; Array.Copy(BitConverter.GetBytes(ChallengeIP), 0, abyBuffer, keylen + 4, 4); abyBuffer[keylen + 4 + 4] = byChaIPKind; } byte[] tmpBuf = new byte[keylen + 4 + ChIpLen]; Array.Copy(abyBuffer, tmpBuf, keylen + 4 + ChIpLen); byte[] outBuf = sigkey.CreateSignature(tmpBuf); nResult = (byte)outBuf.Length; if (outBuf.Length > nMaxSize) { nResult = nMaxSize; } Array.Copy(outBuf, pachOutput, nResult); } catch (Exception ex) { MpdUtilities.DebugLogError(ex); nResult = 0; } return(nResult); }
protected void SaveList() { lastSaved_ = MpdUtilities.GetTickCount(); string name = System.IO.Path.Combine(MuleApplication.Instance.Preference.GetMuleDirectory(Mule.Preference.DefaultDirectoryEnum.EMULE_CONFIGDIR), CLIENTS_MET_FILENAME); try { using (FileStream file = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.None)) { int count = clients_.Count; SafeMemFile memfile = MpdObjectManager.CreateSafeMemFile(count * (16 + 5 * 4 + 1 * 2 + 1 + CreditStruct.MAXPUBKEYSIZE)); memfile.WriteUInt8((byte)VersionsEnum.CREDITFILE_VERSION); Dictionary <MapCKey, ClientCredits> .Enumerator pos = clients_.GetEnumerator(); count = 0; while (pos.MoveNext()) { ClientCredits cur_credit = pos.Current.Value; if (cur_credit.GetUploadedTotal() != 0 || cur_credit.GetDownloadedTotal() != 0) { WriteCreditStruct(memfile, cur_credit.DataStruct); count++; } } memfile.WriteUInt32((uint)count); file.Write(memfile.Buffer, 0, (int)memfile.Length); file.Flush(); file.Close(); memfile.Close(); } } catch (Exception error) { MpdUtilities.DebugLogError(error); } }
public byte CreateSignature(ClientCredits pTarget, byte[] pachOutput, byte nMaxSize, uint ChallengeIP, byte byChaIPKind) { return(CreateSignature(pTarget, pachOutput, nMaxSize, ChallengeIP, byChaIPKind, null)); }
protected void LoadList() { string strFileName = System.IO.Path.Combine(MuleApplication.Instance.Preference.GetMuleDirectory(Mule.Preference.DefaultDirectoryEnum.EMULE_CONFIGDIR), CLIENTS_MET_FILENAME); if (!System.IO.File.Exists(strFileName)) { return; } SafeBufferedFile file = null; try { file = MpdObjectManager.CreateSafeBufferedFile(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None); byte version = file.ReadUInt8(); if (version != (byte)VersionsEnum.CREDITFILE_VERSION && version != (byte)VersionsEnum.CREDITFILE_VERSION_29) { file.Close(); return; } // everything is ok, lets see if the backup exist... string strBakFileName = System.IO.Path.Combine(MuleApplication.Instance.Preference.GetMuleDirectory(Mule.Preference.DefaultDirectoryEnum.EMULE_CONFIGDIR), string.Format("{0}{1}", CLIENTS_MET_FILENAME, ".bak")); uint dwBakFileSize = 0; bool bCreateBackup = true; if (System.IO.File.Exists(strBakFileName)) { FileInfo fInfo = new FileInfo(strBakFileName); dwBakFileSize = (uint)fInfo.Length; if (dwBakFileSize > (uint)file.Length) { // the size of the backup was larger then the org. file, something is wrong here, don't overwrite old backup.. bCreateBackup = false; } } //else: the backup doesn't exist, create it if (bCreateBackup) { file.Close(); // close the file before copying System.IO.File.Copy(strFileName, strBakFileName, true); file = MpdObjectManager.CreateSafeBufferedFile(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None); file.Seek(1, SeekOrigin.Begin); //set filepointer behind file version byte } uint count = file.ReadUInt32(); uint dwExpired = MpdUtilities.Time() - 12960000; // today - 150 day uint cDeleted = 0; for (uint i = 0; i < count; i++) { CreditStruct newcstruct = new CreditStruct(); if (version == (byte)VersionsEnum.CREDITFILE_VERSION_29) { ReadCreditStruct29a(file, newcstruct); } else { ReadCreditStruct(file, newcstruct); } if (newcstruct.nLastSeen < dwExpired) { cDeleted++; continue; } ClientCredits newcredits = MuleApplication.Instance.CoreObjectManager.CreateClientCredits(newcstruct); clients_[new MapCKey(newcredits.Key)] = newcredits; } file.Close(); } catch (Exception error) { MpdUtilities.DebugLogError(error); file.Close(); } }
public bool VerifyIdent(ClientCredits pTarget, byte[] pachSignature, byte nInputSize, uint dwForIP, byte byChaIPKind) { if (!IsCryptoAvailable) { pTarget.IdentState = IdentStateEnum.IS_NOTAVAILABLE; return(false); } bool bResult; try { RSAPKCS1SignatureDeformatter pubkey = MpdObjectManager.CreateRSAPKCS1V15SHA1Verifier(pTarget.SecureIdent, pTarget.SecIDKeyLen); // 4 additional bytes random data send from this client +5 bytes v2 byte[] abyBuffer = new byte[CreditStruct.MAXPUBKEYSIZE + 9]; Array.Copy(publicKey_, abyBuffer, publicKeyLen_); uint challenge = pTarget.CryptRndChallengeFor; Array.Copy(BitConverter.GetBytes(challenge), 0, abyBuffer, publicKeyLen_, 4); // v2 security improvments (not supported by 29b, not used as default by 29c) byte nChIpSize = 0; if (byChaIPKind != 0) { nChIpSize = 5; uint ChallengeIP = 0; switch (byChaIPKind) { case CRYPT_CIP_LOCALCLIENT: ChallengeIP = dwForIP; break; case CRYPT_CIP_REMOTECLIENT: if (MuleApplication.Instance.ServerConnect.ClientID == 0 || MuleApplication.Instance.ServerConnect.IsLowID) { ChallengeIP = MuleApplication.Instance.ServerConnect.LocalIP; } else { ChallengeIP = MuleApplication.Instance.ServerConnect.ClientID; } break; case CRYPT_CIP_NONECLIENT: // maybe not supported in future versions ChallengeIP = 0; break; } Array.Copy(BitConverter.GetBytes(ChallengeIP), 0, abyBuffer, publicKeyLen_ + 4, 4); abyBuffer[publicKeyLen_ + 4 + 4] = byChaIPKind; } //v2 end byte[] hash = new byte[publicKeyLen_ + 4 + nChIpSize]; Array.Copy(abyBuffer, hash, publicKeyLen_ + 4 + nChIpSize); byte[] sign = new byte[nInputSize]; Array.Copy(pachSignature, sign, nInputSize); bResult = pubkey.VerifySignature(hash, sign); } catch (Exception ex) { MpdUtilities.DebugLogError(ex); bResult = false; } if (!bResult) { if (pTarget.IdentState == IdentStateEnum.IS_IDNEEDED) { pTarget.IdentState = IdentStateEnum.IS_IDFAILED; } } else { pTarget.Verified(dwForIP); } return(bResult); }
public void Stop() { try { CloseConnection(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } try { ServerConnect.Stop(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } try { ClientUDP.Close(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } try { ListenSocket.StopListening(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } try { LastCommonRouteFinder.StopFinder(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } try { UploadBandwidthThrottler.Stop(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } try { Preference.Save(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } try { ClientCredits.CleanUp(); } catch (Exception ex) { MpdUtilities.DebugLogError("MuleApplication Stop Fail", ex); } if (ShutDownMuleApplication != null) { ShutDownMuleApplication(this, new EventArgs()); } }