using System;
namespace S22.Sasl.Mechanisms.Srp {
///
/// Represents the initial client-response sent to the server to initiate
/// the authentication exchange.
///
internal class ClientMessage1 {
///
/// The username to authenticate with.
///
/// SRP specification imposes a limit of 65535 bytes
/// on this field.
public string Username {
get;
set;
}
///
/// The authorization identity to authenticate with.
///
/// SRP specification imposes a limit of 65535 bytes
/// on this field.
public string AuthId {
get;
set;
}
///
/// The session identifier of a previous session whose parameters the
/// client wishes to re-use.
///
/// SRP specification imposes a limit of 65535 bytes
/// on this field. If the client wishes to initialize a new session,
/// this parameter must be set to the empty string.
public string SessionId {
get;
set;
}
///
/// The client's nonce used in deriving a new shared context key from
/// the shared context key of the previous session.
///
/// SRP specification imposes a limit of 255 bytes on this
/// field. If not needed, it must be set to an empty byte array.
public byte[] ClientNonce {
get;
set;
}
///
/// Creates a new instance of the ClientMessage1 class using the specified
/// username.
///
/// The username to authenticate with.
/// The authorization id to authenticate with.
/// Thrown if the username parameter
/// is null.
public ClientMessage1(string username, string authId = null) {
username.ThrowIfNull("username");
Username = username;
AuthId = authId ?? String.Empty;
SessionId = String.Empty;
ClientNonce = new byte[0];
}
///
/// Serializes this instance of the ClientMessage1 class into a sequence of
/// bytes according to the requirements of the SRP specification.
///
/// A sequence of bytes representing this instance of the
/// ClientMessage1 class.
/// Thrown if the cummultative length
/// of the serialized data fields exceeds the maximum number of bytes
/// allowed as per SRP specification.
/// SRP specification imposes a limit of 2,147,483,643 bytes on
/// the serialized data.
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();
}
}
}