Skip to content

Commit dd39b12

Browse files
committed
✨ SASL SCRAM-SHA-*: Add mechanisms [🚧 more tests, credit]
Also, don't forget to credit the PR on net-sasl for getting this started!
1 parent f22fe34 commit dd39b12

File tree

7 files changed

+538
-1
lines changed

7 files changed

+538
-1
lines changed

lib/net/imap/sasl.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class IMAP
3333
# +PLAIN+:: See PlainAuthenticator.
3434
# Login using clear-text username and password.
3535
#
36+
# +SCRAM-SHA-1+, +SCRAM-SHA-256+::
37+
# See ScramAuthenticator.
38+
# Login by username and password. The password is not sent
39+
# to the server but is used in a salted challenge/response
40+
# exchange. One of the benefits over +PLAIN+ is that the
41+
# server cannot impersonate the user to other servers.
42+
# +SCRAM-SHA-1+ and +SCRAM-SHA-256+ are supported, but any
43+
# algorithm supported by OpenSSL::Digest can easily be
44+
# added.
45+
#
3646
# +OAUTHBEARER+:: See OAuthBearerAuthenticator.
3747
# Login using an OAUTH2 Bearer token. This is the
3848
# standard mechanism for using OAuth2 with \SASL, but it
@@ -77,10 +87,15 @@ module SASL
7787
autoload :Authenticator, "#{sasl_dir}/authenticator"
7888
autoload :Authenticators, "#{sasl_dir}/authenticators"
7989
autoload :GS2Header, "#{sasl_dir}/gs2_header"
90+
autoload :ScramAlgorithm, "#{sasl_dir}/scram_algorithm"
91+
autoload :ScramAuthenticator, "#{sasl_dir}/scram_authenticator"
92+
8093
autoload :AnonymousAuthenticator, "#{sasl_dir}/anonymous_authenticator"
8194
autoload :ExternalAuthenticator, "#{sasl_dir}/external_authenticator"
8295
autoload :OAuthBearerAuthenticator, "#{sasl_dir}/oauthbearer_authenticator"
8396
autoload :PlainAuthenticator, "#{sasl_dir}/plain_authenticator"
97+
autoload :ScramSHA1Authenticator, "#{sasl_dir}/scram_sha1_authenticator"
98+
autoload :ScramSHA256Authenticator, "#{sasl_dir}/scram_sha256_authenticator"
8499
autoload :XOAuth2Authenticator, "#{sasl_dir}/xoauth2_authenticator"
85100

86101
autoload :CramMD5Authenticator, "#{sasl_dir}/cram_md5_authenticator"
@@ -94,6 +109,8 @@ def self.authenticators
94109
registry.add_authenticator "External"
95110
registry.add_authenticator "OAuthBearer"
96111
registry.add_authenticator "Plain"
112+
registry.add_authenticator "Scram-SHA-1"
113+
registry.add_authenticator "Scram-SHA-256"
97114
registry.add_authenticator "XOAuth2"
98115
registry.add_authenticator "Login" # deprecated
99116
registry.add_authenticator "Cram-MD5" # deprecated

lib/net/imap/sasl/authenticator.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ module SASL
5959
# * +scram_sha1_salted_passwords+, +scram_sha256_salted_password+ ---
6060
# Salted password(s) (with salt and iteration count) for the +SCRAM-*+
6161
# mechanism family. <tt>[salt, iterations, pbkdf2_hmac]</tt> tuple.
62-
# <em>(not implemented yet...)</em>
6362
# * +passcode+ --- passcode for SecurID 2FA <em>(not implemented)</em>
6463
# * +pin+ --- Personal Identification number, e.g. for SecurID 2FA
6564
# <em>(not implemented)</em>

lib/net/imap/sasl/scram_algorithm.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
module Net
4+
class IMAP
5+
module SASL
6+
7+
# For method descriptions, see {RFC5802
8+
# §2}[https://www.rfc-editor.org/rfc/rfc5802#section-2] and {RFC5802
9+
# §3}[https://www.rfc-editor.org/rfc/rfc5802#section-3].
10+
#
11+
# Expects:
12+
# * #Hi, #H, and #HMAC use:
13+
# * +#digest+ --- an OpenSSL::Digest.
14+
# * #salted_password uses:
15+
# * +#salt+ and +#iterations+ --- the server's values for this user
16+
# * +#password+
17+
# * #auth_message is built from:
18+
# * +#client_first_message_bare+ --- contains +#cnonce+
19+
# * +#server_first_message+ --- contains +#snonce+
20+
# * +#client_final_message_no_proof+ --- contains +#snonce+
21+
module ScramAlgorithm
22+
def Normalize(str) SASL.saslprep(str) end
23+
24+
def Hi(str, salt, iterations)
25+
length = digest.digest_length
26+
OpenSSL::KDF.pbkdf2_hmac(
27+
str,
28+
salt: salt,
29+
iterations: iterations,
30+
length: length,
31+
hash: digest,
32+
)
33+
end
34+
35+
def H(str) digest.digest str end
36+
37+
def HMAC(key, data) OpenSSL::HMAC.digest(digest, key, data) end
38+
39+
def XOR(str1, str2)
40+
str1.unpack("C*")
41+
.zip(str2.unpack("C*"))
42+
.map {|a, b| a ^ b }
43+
.pack("C*")
44+
end
45+
46+
def auth_message
47+
[
48+
client_first_message_bare,
49+
server_first_message,
50+
client_final_message_no_proof,
51+
]
52+
.join(",")
53+
end
54+
55+
def salted_password
56+
Hi(Normalize(password), salt, iterations)
57+
end
58+
59+
def client_key; HMAC(salted_password, "Client Key") end
60+
def server_key; HMAC(salted_password, "Server Key") end
61+
def stored_key; H(client_key) end
62+
def client_signature; HMAC(stored_key, auth_message) end
63+
def server_signature; HMAC(server_key, auth_message) end
64+
def client_proof; XOR(client_key, client_signature) end
65+
end
66+
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)