public AuthChallenge(Socket conn) { int soFar = 0; byte[] buffer = new byte[MESSAGE_SIZE]; while (soFar < MESSAGE_SIZE) { //read the header and challenge phrase int rcvd = conn.Receive(buffer, (int)MESSAGE_SIZE - soFar, SocketFlags.None); if (rcvd == 0) { throw new AuthException("Disconnect during authentication"); } if (soFar == 0 && buffer[0] != PACKET_IDENTIFIER) { throw new AuthException("Invalid challenge packet header"); } //skip the first byte if (soFar == 0) { if (rcvd > 1) { _challenge.Append(buffer, 1, rcvd - 1); } } else { _challenge.Append(buffer, 0, rcvd); } soFar += rcvd; } }
/// <summary> /// Sets up the data in the header /// </summary> /// <param name="type"></param> /// <param name="assetUUID"></param> /// <param name="dataSize"></param> private void SetupHeader(RequestType type, string assetUUID, int dataSize) { _data = new AppendableByteArray(HEADER_SIZE + dataSize); _data.Append((byte)type); _data.Append(Util.UuidToAscii(assetUUID)); _data.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataSize))); //size }
/// <summary> /// Constructs a new authentication response /// </summary> /// <param name="challenge">The challenge from the server</param> /// <param name="password">The password for the server</param> public AuthResponse(AuthChallenge challenge, string password) { //convert the password to ascii ASCIIEncoding encoding = new ASCIIEncoding(); byte[] asciiPW = encoding.GetBytes(password); //get data from the challenge byte[] challengeBytes = challenge.Challenge; //add the two ranges together and compute the hash AppendableByteArray authString = new AppendableByteArray(asciiPW.Length + challengeBytes.Length); authString.Append(asciiPW); authString.Append(challengeBytes); SHA1 sha = new SHA1CryptoServiceProvider(); byte[] challengeHash = sha.ComputeHash(authString.data); //copy the results to the raw packet data _rawMessageData.Append(PACKET_IDENTIFIER); _rawMessageData.Append(encoding.GetBytes(Util.HashToHex(challengeHash))); }
/// <summary> /// Constructs a new authentication response /// </summary> /// <param name="challenge">The challenge from the server</param> /// <param name="password">The password for the server</param> public AuthResponse(AuthChallenge challenge, string password) { //convert the password to ascii ASCIIEncoding encoding = new ASCIIEncoding(); byte[] asciiPW = encoding.GetBytes(password); //get data from the challenge byte[] challengeBytes = challenge.Challenge; //add the two ranges together and compute the hash AppendableByteArray authString = new AppendableByteArray(asciiPW.Length + challengeBytes.Length); authString.Append(asciiPW); authString.Append(challengeBytes); SHA1 sha = new SHA1CryptoServiceProvider(); byte[] challengeHash = sha.ComputeHash(authString.data); //copy the results to the raw packet data _rawMessageData.Append(PACKET_IDENTIFIER); _rawMessageData.Append(encoding.GetBytes(Util.HashToHex(challengeHash))); }
public AuthStatus(Socket conn) { int soFar = 0; byte[] buffer = new byte[MESSAGE_SIZE]; while (soFar < MESSAGE_SIZE) { //read the header and challenge phrase int rcvd = conn.Receive(buffer, MESSAGE_SIZE, SocketFlags.None); if (rcvd == 0) { throw new AuthException("Disconnect during authentication"); } if (soFar == 0 && buffer[0] != PACKET_IDENTIFIER) { throw new AuthException("Invalid challenge packet header"); } _data.Append(buffer, 0, rcvd); soFar += rcvd; } }
public AppendableByteArray Serialize() { UTF8Encoding encoding = new UTF8Encoding(); byte[] nameBytes = encoding.GetBytes(_name); byte[] descBytes = encoding.GetBytes(_description); if (nameBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset name would be too long after encoding {0} {1}", _name, _uuid)); } if (descBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset description would be too long after encoding {0} {1}", _description, _uuid)); } //see the packet diagram to understand where the size calculation is coming from AppendableByteArray retArray = new AppendableByteArray(HEADER_SIZE + 1 + nameBytes.Length + 1 + descBytes.Length + 4 + _data.Length); retArray.Append(Util.UuidToAscii(_uuid)); retArray.Append((byte)_type); retArray.Append((byte)(_local ? 1 : 0)); retArray.Append((byte)(_temporary ? 1 : 0)); retArray.Append(Util.HTONL(_createTime)); retArray.Append((byte)nameBytes.Length); retArray.Append(nameBytes); retArray.Append((byte)descBytes.Length); retArray.Append(descBytes); retArray.Append(Util.HTONL(_data.Length)); retArray.Append(_data); return(retArray); }
public AppendableByteArray Serialize() { UTF8Encoding encoding = new UTF8Encoding(); byte[] nameBytes = encoding.GetBytes(_name); byte[] descBytes = encoding.GetBytes(_description); if (nameBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset name would be too long after encoding {0} {1}", _name, _uuid)); } if (descBytes.Length > 255) { throw new AssetProtocolError(String.Format("Serialized asset description would be too long after encoding {0} {1}", _description, _uuid)); } //see the packet diagram to understand where the size calculation is coming from AppendableByteArray retArray = new AppendableByteArray(HEADER_SIZE + 1 + nameBytes.Length + 1 + descBytes.Length + 4 + _data.Length); retArray.Append(Util.UuidToAscii(_uuid)); retArray.Append((byte)_type); retArray.Append((byte)(_local ? 1 : 0)); retArray.Append((byte)(_temporary ? 1 : 0)); retArray.Append(Util.HTONL(_createTime)); retArray.Append((byte)nameBytes.Length); retArray.Append(nameBytes); retArray.Append((byte)descBytes.Length); retArray.Append(descBytes); retArray.Append(Util.HTONL(_data.Length)); retArray.Append(_data); return retArray; }
/// <summary> /// CTOR /// </summary> /// <param name="type"></param> /// <param name="assetUUID"></param> /// <param name="data"></param> public ClientRequestMsg(RequestType type, string assetUUID, byte[] data) { this.SetupHeader(type, assetUUID, data.Length); _data.Append(data); }
/// <summary> /// Sets up the data in the header /// </summary> /// <param name="type"></param> /// <param name="assetUUID"></param> /// <param name="dataSize"></param> private void SetupHeader(RequestType type, string assetUUID, int dataSize) { _data = new AppendableByteArray(HEADER_SIZE + dataSize); _data.Append((byte)type); _data.Append(Util.UuidToAscii(assetUUID)); _data.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(dataSize))); //size }