jstedfast/MailKit · error · ArgumentNullException

value

Error message

value

What it means

Guard in the HMACMD5.Key property setter: assigning a null key is invalid because the HMAC algorithm requires key material; the setter rejects null with ArgumentNullException, clears any previous key otherwise, and re-initializes the hash.

Solutions

  1. Ensure the key byte[] is non-null before assignment
  2. Guard the credential/key derivation so it cannot yield null
  3. Catch ArgumentNullException and surface a missing-credentials error

Example fix

// before
hmac.Key = sessionKey; // may be null
// after
if (sessionKey != null)
	hmac.Key = sessionKey;
Defensive patterns

Strategy: validation

Validate before calling

if (key == null) throw new ArgumentException("HMAC key must not be null");

Type guard

bool HasKey(byte[] k) => k != null && k.Length > 0;

Try / catch

try { hmac.Key = key; } catch (ArgumentNullException) { /* missing credentials */ }

Prevention

When it happens

Trigger: Assigning null to hmac.Key, typically when the key came from an unset config value or a failed credential lookup.

Common situations: NTLM code paths where the session key derivation silently produced null (e.g. missing NTLM credentials).

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/f23db8a4b12472d6. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Security/Ntlm/HMACMD5.cs:65

		{
			Dispose (false);
		}

		public byte[] Hash
		{
			get {
				if (hashValue == null)
					throw new InvalidOperationException ("No hash value computed.");

				return hashValue;
			}
		}

		public byte[] Key {
			get { return key; }
			set {
				if (value == null)
					throw new ArgumentNullException (nameof (value));

				if (key != null)
					Array.Clear (key, 0, key.Length);

				key = value;
				Initialize ();
			}
		}

		void HashCore (byte[] block, int offset, int size)
		{
			hash.BlockUpdate (block, offset, size);
		}

		byte[] HashFinal ()
		{
			var value = new byte[hash.GetMacSize ()];

View on GitHub (pinned to 9d3859a785)