Make ‘text-quoting-style’ a plain defvar
[emacs.git] / lisp / net / sasl-scram-rfc.el
blob18d7a6bfa18a4845128950b1b4d2c04d0efa9048
1 ;;; sasl-scram-rfc.el --- SCRAM-SHA-1 module for the SASL client framework -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Magnus Henoch <magnus.henoch@gmail.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; This program is implemented from RFC 5802. It implements the
25 ;; SCRAM-SHA-1 SASL mechanism.
27 ;; RFC 5802 foresees "hash agility", i.e. new mechanisms based on the
28 ;; same protocol but using a different hash function. Likewise, this
29 ;; module attempts to separate generic and specific functions, which
30 ;; should make it easy to implement any future SCRAM-* SASL mechanism.
31 ;; It should be as simple as copying the SCRAM-SHA-1 section below and
32 ;; replacing all SHA-1 references.
34 ;; This module does not yet implement the variants with channel
35 ;; binding, i.e. SCRAM-*-PLUS. That would require cooperation from
36 ;; the TLS library.
38 ;;; Code:
40 (require 'cl-lib)
41 (require 'sasl)
42 (require 'hex-util)
43 (require 'rfc2104)
45 ;;; Generic for SCRAM-*
47 (defun sasl-scram-client-first-message (client _step)
48 (let ((c-nonce (sasl-unique-id)))
49 (sasl-client-set-property client 'c-nonce c-nonce))
50 (concat
51 ;; n = client doesn't support channel binding
52 "n,"
53 ;; TODO: where would we get authorization id from?
54 ","
55 (sasl-scram--client-first-message-bare client)))
57 (defun sasl-scram--client-first-message-bare (client)
58 (let ((c-nonce (sasl-client-property client 'c-nonce)))
59 (concat
60 ;; TODO: saslprep username or disallow non-ASCII characters
61 "n=" (sasl-client-name client) ","
62 "r=" c-nonce)))
64 (defun sasl-scram--client-final-message (hash-fun block-length hash-length client step)
65 (unless (string-match
66 "^r=\\([^,]+\\),s=\\([^,]+\\),i=\\([0-9]+\\)\\(?:$\\|,\\)"
67 (sasl-step-data step))
68 (sasl-error "Unexpected server response"))
69 (let* ((hmac-fun (lambda (text key)
70 (decode-hex-string
71 (rfc2104-hash hash-fun block-length hash-length key text))))
72 (step-data (sasl-step-data step))
73 (nonce (match-string 1 step-data))
74 (salt-base64 (match-string 2 step-data))
75 (iteration-count (string-to-number (match-string 3 step-data)))
77 (c-nonce (sasl-client-property client 'c-nonce))
78 ;; no channel binding, no authorization id
79 (cbind-input "n,,"))
80 (unless (string-prefix-p c-nonce nonce)
81 (sasl-error "Invalid nonce from server"))
82 (let* ((client-final-message-without-proof
83 (concat "c=" (base64-encode-string cbind-input) ","
84 "r=" nonce))
85 (password
86 ;; TODO: either apply saslprep or disallow non-ASCII characters
87 (sasl-read-passphrase
88 (format "%s passphrase for %s: "
89 (sasl-mechanism-name (sasl-client-mechanism client))
90 (sasl-client-name client))))
91 (salt (base64-decode-string salt-base64))
92 (salted-password
93 ;; Hi(str, salt, i):
94 (let ((digest (concat salt (string 0 0 0 1)))
95 (xored nil))
96 (dotimes (_i iteration-count xored)
97 (setq digest (funcall hmac-fun digest password))
98 (setq xored (if (null xored)
99 digest
100 (cl-map 'string 'logxor xored digest))))))
101 (client-key
102 (funcall hmac-fun "Client Key" salted-password))
103 (stored-key (decode-hex-string (funcall hash-fun client-key)))
104 (auth-message
105 (concat
106 (sasl-scram--client-first-message-bare client) ","
107 step-data ","
108 client-final-message-without-proof))
109 (client-signature (funcall hmac-fun (encode-coding-string auth-message 'utf-8) stored-key))
110 (client-proof (cl-map 'string 'logxor client-key client-signature))
111 (client-final-message
112 (concat client-final-message-without-proof ","
113 "p=" (base64-encode-string client-proof))))
114 (sasl-client-set-property client 'auth-message auth-message)
115 (sasl-client-set-property client 'salted-password salted-password)
116 client-final-message)))
118 (defun sasl-scram--authenticate-server (hash-fun block-length hash-length client step)
119 (cond
120 ((string-match "^e=\\([^,]+\\)" (sasl-step-data step))
121 (sasl-error (format "Server error: %s" (match-string 1 (sasl-step-data step)))))
122 ((string-match "^v=\\([^,]+\\)" (sasl-step-data step))
123 (let* ((hmac-fun (lambda (text key)
124 (decode-hex-string
125 (rfc2104-hash hash-fun block-length hash-length key text))))
126 (verifier (base64-decode-string (match-string 1 (sasl-step-data step))))
127 (auth-message (sasl-client-property client 'auth-message))
128 (salted-password (sasl-client-property client 'salted-password))
129 (server-key (funcall hmac-fun "Server Key" salted-password))
130 (expected-server-signature
131 (funcall hmac-fun (encode-coding-string auth-message 'utf-8) server-key)))
132 (unless (string= expected-server-signature verifier)
133 (sasl-error "Server not authenticated"))))
135 (sasl-error "Invalid response from server"))))
137 ;;; SCRAM-SHA-1
139 (defconst sasl-scram-sha-1-steps
140 '(sasl-scram-client-first-message
141 sasl-scram-sha-1-client-final-message
142 sasl-scram-sha-1-authenticate-server))
144 (defun sasl-scram-sha-1-client-final-message (client step)
145 (sasl-scram--client-final-message
146 ;; HMAC-SHA1 uses block length 64 and hash length 20; see RFC 2104.
147 'sha1 64 20 client step))
149 (defun sasl-scram-sha-1-authenticate-server (client step)
150 (sasl-scram--authenticate-server
151 'sha1 64 20 client step))
153 ;; This needs to be at the end, because of how `sasl-make-mechanism'
154 ;; handles step function names.
155 (put 'sasl-scram-sha-1 'sasl-mechanism
156 (sasl-make-mechanism "SCRAM-SHA-1" sasl-scram-sha-1-steps))
158 (put 'sasl-scram-rfc 'sasl-mechanism (get 'sasl-scram-sha-1 'sasl-mechanism))
160 (provide 'sasl-scram-sha-1)
162 (provide 'sasl-scram-rfc)
163 ;;; sasl-scram-rfc.el ends here