/// <summary> /// Deserializes a new instance of the ServerMessage1 class from the /// specified buffer of bytes. /// </summary> /// <param name="buffer">The byte buffer to deserialize the ServerMessage1 /// instance from.</param> /// <returns>An instance of the ServerMessage1 class deserialized from the /// specified byte array.</returns> /// <exception cref="FormatException">Thrown if the byte buffer does not /// contain valid data.</exception> public static ServerMessage1 Deserialize(byte[] buffer) { using (var ms = new MemoryStream(buffer)) { using (var r = new BinaryReader(ms)) { uint bufferLength = r.ReadUInt32(true); // We don't support re-using previous sessions. byte reuse = r.ReadByte(); if (reuse != 0) { throw new FormatException("Unexpected re-use parameter value: " + reuse); } Mpi N = r.ReadMpi(); Mpi g = r.ReadMpi(); OctetSequence salt = r.ReadOs(); Mpi B = r.ReadMpi(); Utf8String L = r.ReadUtf8String(); return(new ServerMessage1() { Generator = g, PublicKey = B, Salt = salt.Value, SafePrimeModulus = N, Options = ParseOptions(L.Value), RawOptions = L.Value }); } } }
/// <summary> /// Serializes this instance of the ClientMessage1 class into a sequence of /// bytes according to the requirements of the SRP specification. /// </summary> /// <returns>A sequence of bytes representing this instance of the /// ClientMessage1 class.</returns> /// <exception cref="OverflowException">Thrown if the cummultative length /// of the serialized data fields exceeds the maximum number of bytes /// allowed as per SRP specification.</exception> /// <remarks>SRP specification imposes a limit of 2,147,483,643 bytes on /// the serialized data.</remarks> public byte[] Serialize() { byte[] username = new Utf8String(Username).Serialize(), authId = new Utf8String(AuthId).Serialize(), sessionId = new Utf8String(SessionId).Serialize(), nonce = new OctetSequence(ClientNonce).Serialize(); int length = username.Length + authId.Length + sessionId.Length + nonce.Length; return new ByteBuilder() .Append(length, true) .Append(username) .Append(authId) .Append(sessionId) .Append(nonce) .ToArray(); }
/// <summary> /// Serializes this instance of the ClientMessage2 class into a sequence of /// bytes according to the requirements of the SRP specification. /// </summary> /// <returns>A sequence of bytes representing this instance of the /// ClientMessage2 class.</returns> /// <exception cref="OverflowException">Thrown if the cummultative length /// of the serialized data fields exceeds the maximum number of bytes /// allowed as per SRP specification.</exception> /// <remarks>SRP specification imposes a limit of 2,147,483,643 bytes on /// the serialized data.</remarks> public byte[] Serialize() { byte[] publicKey = PublicKey.Serialize(), M1 = new OctetSequence(Proof).Serialize(), cIV = new OctetSequence(InitialVector).Serialize(), options = new Utf8String(BuildOptionsString()).Serialize(); int length = publicKey.Length + M1.Length + cIV.Length + options.Length; return new ByteBuilder() .Append(length, true) .Append(publicKey) .Append(M1) .Append(options) .Append(cIV) .ToArray(); }
/// <summary> /// Serializes this instance of the ClientMessage1 class into a sequence of /// bytes according to the requirements of the SRP specification. /// </summary> /// <returns>A sequence of bytes representing this instance of the /// ClientMessage1 class.</returns> /// <exception cref="OverflowException">Thrown if the cummultative length /// of the serialized data fields exceeds the maximum number of bytes /// allowed as per SRP specification.</exception> /// <remarks>SRP specification imposes a limit of 2,147,483,643 bytes on /// the serialized data.</remarks> public byte[] Serialize() { byte[] username = new Utf8String(Username).Serialize(), authId = new Utf8String(AuthId).Serialize(), sessionId = new Utf8String(SessionId).Serialize(), nonce = new OctetSequence(ClientNonce).Serialize(); int length = username.Length + authId.Length + sessionId.Length + nonce.Length; return(new ByteBuilder() .Append(length, true) .Append(username) .Append(authId) .Append(sessionId) .Append(nonce) .ToArray()); }
/// <summary> /// Serializes this instance of the ClientMessage2 class into a sequence of /// bytes according to the requirements of the SRP specification. /// </summary> /// <returns>A sequence of bytes representing this instance of the /// ClientMessage2 class.</returns> /// <exception cref="OverflowException">Thrown if the cummultative length /// of the serialized data fields exceeds the maximum number of bytes /// allowed as per SRP specification.</exception> /// <remarks>SRP specification imposes a limit of 2,147,483,643 bytes on /// the serialized data.</remarks> public byte[] Serialize() { byte[] publicKey = PublicKey.Serialize(), M1 = new OctetSequence(Proof).Serialize(), cIV = new OctetSequence(InitialVector).Serialize(), options = new Utf8String(BuildOptionsString()).Serialize(); int length = publicKey.Length + M1.Length + cIV.Length + options.Length; return(new ByteBuilder() .Append(length, true) .Append(publicKey) .Append(M1) .Append(options) .Append(cIV) .ToArray()); }
/// <summary> /// Deserializes a new instance of the ServerMessage2 class from the /// specified buffer of bytes. /// </summary> /// <param name="buffer">The byte buffer to deserialize the ServerMessage2 /// instance from.</param> /// <returns>An instance of the ServerMessage2 class deserialized from the /// specified byte array.</returns> /// <exception cref="FormatException">Thrown if the byte buffer does not /// contain valid data.</exception> public static ServerMessage2 Deserialize(byte[] buffer) { using (var ms = new MemoryStream(buffer)) { using (var r = new BinaryReader(ms)) { uint bufferLength = r.ReadUInt32(true); OctetSequence M2 = r.ReadOs(), sIV = r.ReadOs(); Utf8String sid = r.ReadUtf8String(); uint ttl = r.ReadUInt32(true); return(new ServerMessage2() { Proof = M2.Value, InitialVector = sIV.Value, SessionId = sid.Value, Ttl = ttl }); } } }