Function: sasl-scram--client-final-message

sasl-scram--client-final-message is a byte-compiled function defined in sasl-scram-rfc.el.gz.

Signature

(sasl-scram--client-final-message HASH-FUN BLOCK-LENGTH HASH-LENGTH CLIENT STEP)

Source Code

;; Defined in /usr/src/emacs/lisp/net/sasl-scram-rfc.el.gz
(defun sasl-scram--client-final-message (hash-fun block-length hash-length client step)
  (unless (string-match
	   "^r=\\([^,]+\\),s=\\([^,]+\\),i=\\([0-9]+\\)\\(?:$\\|,\\)"
	   (sasl-step-data step))
    (sasl-error "Unexpected server response"))
  (let* ((hmac-fun (lambda (text key)
		     (decode-hex-string
		      (rfc2104-hash hash-fun block-length hash-length key text))))
	 (step-data (sasl-step-data step))
	 (nonce (match-string 1 step-data))
	 (salt-base64 (match-string 2 step-data))
	 (iteration-count (string-to-number (match-string 3 step-data)))

	 (c-nonce (sasl-client-property client 'c-nonce))
	 ;; no channel binding, no authorization id
         (cbind-input (funcall sasl-scram-gs2-header-function client)))
    (unless (string-prefix-p c-nonce nonce)
      (sasl-error "Invalid nonce from server"))
    (let* ((client-final-message-without-proof
            (concat "c=" (base64-encode-string cbind-input t) ","
		    "r=" nonce))
	   (password
	    ;; TODO: either apply saslprep or disallow non-ASCII characters
	    (sasl-read-passphrase
	     (format "%s passphrase for %s: "
		     (sasl-mechanism-name (sasl-client-mechanism client))
		     (sasl-client-name client))))
	   (salt (base64-decode-string salt-base64))
           (string-xor (lambda (a b)
                         (apply #'unibyte-string (cl-mapcar #'logxor a b))))
	   (salted-password
	    ;; Hi(str, salt, i):
	    (let ((digest (concat salt (string 0 0 0 1)))
		  (xored nil))
	      (dotimes (_i iteration-count xored)
		(setq digest (funcall hmac-fun digest password))
		(setq xored (if (null xored)
				digest
                              (funcall string-xor xored digest))))))
	   (client-key
	    (funcall hmac-fun "Client Key" salted-password))
	   (stored-key (decode-hex-string (funcall hash-fun client-key)))
	   (auth-message
	    (concat
	     (sasl-scram--client-first-message-bare client) ","
	     step-data ","
	     client-final-message-without-proof))
	   (client-signature (funcall hmac-fun (encode-coding-string auth-message 'utf-8) stored-key))
	   (client-proof (funcall string-xor client-key client-signature))
	   (client-final-message
	    (concat client-final-message-without-proof ","
                    "p=" (base64-encode-string client-proof t))))
      (sasl-client-set-property client 'auth-message auth-message)
      (sasl-client-set-property client 'salted-password salted-password)
      client-final-message)))