WONStatus PeerAuthClient::HandleChallenge1(ReadBuffer &theChallenge, ByteBufferPtr &challenge2) { unsigned short aSecretLenWithLen = theChallenge.ReadShort(); ByteBufferPtr aSecretBuf = mPeerData->GetPrivateKey().Decrypt(theChallenge.ReadBytes(aSecretLenWithLen),aSecretLenWithLen); if(aSecretBuf.get()==NULL) return WS_PeerAuthClient_Challenge1DecryptFailure; unsigned short aSecretLen = aSecretBuf->data()[0] | (aSecretBuf->data()[1]<<8); if(aSecretLen>aSecretBuf->length()-2) return WS_PeerAuthClient_Challenge1InvalidSecretLen; mSecretA.Create(8); if(!mSecretB.SetKey(aSecretBuf->data()+2, aSecretLen)) return WS_PeerAuthClient_Challenge1InvalidSecretKey; unsigned short aCertLen = theChallenge.ReadShort(); if(mUseAuth2) { mServerCertificate = new Auth2Certificate(theChallenge.ReadBytes(aCertLen),aCertLen); if(!mServerCertificate->IsValid()) return WS_PeerAuthClient_Challenge1CertificateUnpackFailure; } else { mServerCertificate = new AuthCertificate(theChallenge.ReadBytes(aCertLen),aCertLen); if(!mServerCertificate->IsValid()) return WS_PeerAuthClient_Challenge1CertificateUnpackFailure; } if(!mPeerData->Verify(mServerCertificate.get())) return WS_PeerAuthClient_Challenge1CertificateVerifyFailure; return GetChallenge2(challenge2); }
WONStatus PeerAuthServer::HandleRequest(ReadBuffer &theRequest, ByteBufferPtr &theChallenge) { unsigned char authMode = theRequest.ReadByte(); unsigned char encryptType = theRequest.ReadByte(); unsigned short encryptFlags = theRequest.ReadShort(); if(authMode!=1) return WS_PeerAuthServer_InvalidAuthMode; if(encryptType!=0 && encryptType!=1) return WS_PeerAuthServer_InvalidEncryptType; bool encrypted = encryptType==1; mAuthType = encrypted?AUTH_TYPE_PERSISTENT:AUTH_TYPE_PERSISTENT_NOCRYPT; unsigned short aLen = theRequest.ReadShort(); if(mUseAuth2) mClientCertificate = new Auth2Certificate(theRequest.ReadBytes(aLen),aLen); else mClientCertificate = new AuthCertificate(theRequest.ReadBytes(aLen),aLen); if(!mClientCertificate->IsValid()) return WS_PeerAuthServer_InvalidClientCertificate; else if(mClientCertificate->IsExpired(mPeerData->GetAuthDelta())) return WS_PeerAuthServer_ExpiredClientCertificate; else if(!mPeerData->Verify(mClientCertificate.get())) return WS_PeerAuthServer_FailedToVerifyClientCertificate; return GetChallenge1(theChallenge); }
bool Auth2Certificate::UnpackHook(ReadBuffer &theData) { if(!AuthBase::UnpackHook(theData)) return false; unsigned short aDataCount = theData.ReadShort(); for(int i=0; i<aDataCount; i++) { unsigned short aTypeId = theData.ReadShort(); unsigned short aDataLen = theData.ReadShort(); int aPos = theData.pos(); switch(aTypeId) { case 0: // standard login data { mUserId = theData.ReadLong(); unsigned short aKeyLen = theData.ReadShort(); if(!mPubKey.SetPublicKey(theData.ReadBytes(aKeyLen),aKeyLen)) return false; theData.ReadWString(mUserName); unsigned char aCommunityCount = theData.ReadByte(); for(int i=0; i<aCommunityCount; i++) mCommunityTrustMap[theData.ReadLong()] = theData.ReadShort(); } break; case 1: // user data { DWORD aCommunityId = theData.ReadLong(); mUserDataMap[aCommunityId] = theData.ReadBuf(2); } break; case 2: // nickname { wstring aKey, aVal; theData.ReadWString(aKey); theData.ReadWString(aVal); mNicknameMap[aKey] = aVal; } break; case 3: // KeyId data block { DWORD aCommunityId = theData.ReadLong(); DWORD aKeyId = theData.ReadLong(); mKeyIdMap[aCommunityId] = aKeyId; } break; } theData.ReadBytes(aDataLen - (theData.pos() - aPos)); } return true; }
void RoutingGetGroupListOp::ParseReplyExceptForStatus(ReadBuffer &theMsg) { mGroupMap.clear(); mFlags = theMsg.ReadShort(); unsigned short aNumGroups = theMsg.ReadShort(); for(int i=0; i<aNumGroups; i++) { unsigned short aLen = theMsg.ReadShort(); unsigned long aBeginPos = theMsg.pos(); RoutingGroupInfoPtr anInfo = GetNewGroupInfo(); anInfo->mId = theMsg.ReadShort(); if (HasGroupName()) theMsg.ReadWString(anInfo->mName); if (HasCaptainId()) anInfo->mCaptainId = theMsg.ReadShort(); if (HasMaxPlayers()) anInfo->mMaxPlayers = theMsg.ReadShort(); if (HasGroupFlags()) anInfo->mFlags = theMsg.ReadLong(); if (HasAsyncFlags()) anInfo->mAsyncFlags = theMsg.ReadLong(); if (HasObserverCount()) anInfo->mObserverCount = theMsg.ReadShort(); if (HasMemberCount()) anInfo->mMemberCount = theMsg.ReadShort(); if (HasMembers()) { for(int j=0; j<anInfo->mMemberCount; j++) { RoutingMemberInfoPtr aMemberInfo = GetNewMemberInfo(); aMemberInfo->mClientId = theMsg.ReadShort(); aMemberInfo->mFlags = theMsg.ReadByte(); anInfo->mMemberMap[aMemberInfo->mClientId] = aMemberInfo; } } // if (theMsg.HasMoreBytes()) // anInfo->mParentId = theMsg.ReadShort(); mGroupMap[anInfo->mId] = anInfo; theMsg.ReadBytes(aLen - (theMsg.pos() - aBeginPos)); } if(HasClientCount() && theMsg.HasMoreBytes()) mClientCount = theMsg.ReadShort(); }
bool AuthCertificate::UnpackHook(ReadBuffer &theData) { if(!AuthBase::UnpackHook(theData)) return false; mUserId = theData.ReadLong(); mCommunityId = theData.ReadLong(); mTrustLevel = theData.ReadShort(); unsigned short aKeyLen = theData.ReadShort(); if(!mPubKey.SetPublicKey(theData.ReadBytes(aKeyLen),aKeyLen)) return false; return true; }
WONStatus PeerAuthServer::HandleChallenge2(ReadBuffer &theChallenge, ByteBufferPtr &theComplete) { unsigned short anEncryptLen = theChallenge.ReadShort(); ByteBufferPtr aDecrypt = mPeerData->GetPrivateKey().Decrypt(theChallenge.ReadBytes(anEncryptLen),anEncryptLen); if(aDecrypt.get()==NULL) return WS_PeerAuthServer_FailedToDecryptWithPrivateKey; ReadBuffer aBuf(aDecrypt->data(),aDecrypt->length()); unsigned short aSecretBLen = aBuf.ReadShort(); if(aSecretBLen!=mSecretB.GetKeyLen() || memcmp(mSecretB.GetKey(),aBuf.ReadBytes(aSecretBLen),aSecretBLen)!=0) return WS_PeerAuthServer_InvalidSecretB; if(!mSecretA.SetKey(aBuf.data()+aBuf.pos(),aBuf.Available())) return WS_PeerAuthServer_InvalidSecretA; return GetComplete(WS_Success, theComplete); }
bool LobbyGame::ReadSummary(ReadBuffer &theMsg) { try { if(mGameType==LobbyGameType_Internet) mIPAddr.SetSixByte(theMsg.ReadBytes(6)); else { unsigned short aLanProductId = theMsg.ReadShort(); if(aLanProductId!=LobbyMisc::GetLanProductId()) return false; } mInProgress = theMsg.ReadBool(); if(mGameType!=LobbyGameType_Internet) { std::wstring aName; theMsg.ReadWString(aName); mName = aName; } mSkillLevel = (LobbySkillLevel)theMsg.ReadByte(); if(mSkillLevel<LobbySkillLevel_None || mSkillLevel>=LobbySkillLevel_Max) mSkillLevel = LobbySkillLevel_None; if(mGameType!=LobbyGameType_Internet) { unsigned char aProtectionFlags = theMsg.ReadByte(); mHasPassword = (aProtectionFlags & 0x01)?true:false; mInviteOnly = (aProtectionFlags & 0x02)?true:false; mAskToJoin = (aProtectionFlags & 0x04)?true:false; } mNumPlayers = theMsg.ReadShort(); mMaxPlayers = theMsg.ReadShort(); return ReadSummaryHook(theMsg); } catch(ReadBufferException&) { } return false; }
WONStatus RoutingGetMembersOfGroupOp::HandleReply(unsigned char theMsgType, ReadBuffer &theMsg) { if(theMsgType!=RoutingGetMembersOfGroupReply) return WS_RoutingOp_DontWantReply; WONStatus aStatus = (WONStatus)theMsg.ReadShort(); unsigned short aGroupId = theMsg.ReadShort(); if(aGroupId!=mGroupId) return WS_RoutingOp_DontWantReply; if(aStatus==WS_Success) { unsigned char aFlags = theMsg.ReadByte(); mHasClientNames = ((aFlags & RoutingGroupAsyncFlag_DistributeClientName) != 0); mHasClientFlags = ((aFlags & RoutingGroupAsyncFlag_DistributeClientFlags) != 0); mObserverCount = theMsg.ReadShort(); unsigned short aNumMembers = theMsg.ReadShort(); for(int j=0; j<aNumMembers; j++) { unsigned short aLen = theMsg.ReadShort(); unsigned long aBeginPos = theMsg.pos(); RoutingMemberInfoPtr aMemberInfo = GetNewMemberInfo(); aMemberInfo->mClientId = theMsg.ReadShort(); aMemberInfo->mFlags = theMsg.ReadByte(); mMemberMap[aMemberInfo->mClientId] = aMemberInfo; if(aFlags!=0) aMemberInfo->mClientInfo = GetNewClientInfo(); if(mHasClientNames) theMsg.ReadWString(aMemberInfo->mClientInfo->mName); if(mHasClientFlags) aMemberInfo->mClientInfo->mFlags = theMsg.ReadLong(); theMsg.ReadBytes(aLen - (theMsg.pos() - aBeginPos)); } } return aStatus; }
WONStatus PeerAuthClient::HandleComplete(ReadBuffer &theComplete) { short aStatus = theComplete.ReadShort(); if(aStatus<0) { unsigned short aNumErrors = theComplete.ReadShort(); for(int i=0; i<aNumErrors; i++) { string anError; theComplete.ReadString(anError); } return (WONStatus)aStatus; } unsigned short aLen = theComplete.ReadShort(); ByteBufferPtr aDecrypt = mPeerData->GetPrivateKey().Decrypt(theComplete.ReadBytes(aLen),aLen); if(aDecrypt.get()==NULL) return WS_PeerAuthClient_CompleteDecryptFailure; if(aDecrypt->length()<2) return WS_PeerAuthClient_CompleteInvalidSecretLen; aLen = (aDecrypt->data()[0] | (aDecrypt->data()[1]<<8)); if(aLen>aDecrypt->length()-2 || aLen!=mSecretA.GetKeyLen() || memcmp(mSecretA.GetKey(),aDecrypt->data()+2,aLen)!=0) return WS_PeerAuthClient_CompleteInvalidSecretKey; unsigned short aSessionId = 0; if(mAuthType==AUTH_TYPE_SESSION) aSessionId = theComplete.ReadShort(); if(mAuthType!=AUTH_TYPE_PERSISTENT_NOCRYPT) mSession = new AuthSession(mAuthType, aSessionId, mSecretB, mLengthFieldSize); return WS_Success; }