Skip to content

Commit a311916

Browse files
nevanssingpolyma
andcommitted
🔒 Add SASL SCRAM-SHA-* mechanisms
Based on the implementation by @singpolyma at nevans/net-sasl#5 Co-authored-by: Stephen Paul Weber <[email protected]>
1 parent 8d49eeb commit a311916

File tree

8 files changed

+557
-0
lines changed

8 files changed

+557
-0
lines changed

lib/net/imap.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,17 @@ def starttls(options = {}, verify = true)
10261026
#
10271027
# Login using clear-text username and password.
10281028
#
1029+
# +SCRAM-SHA-1+::
1030+
# +SCRAM-SHA-256+::
1031+
# See ScramAuthenticator[Net::IMAP::SASL::ScramAuthenticator].
1032+
#
1033+
# Login by username and password. The password is not sent to the
1034+
# server but is used in a salted challenge/response exchange.
1035+
# +SCRAM-SHA-1+ and +SCRAM-SHA-256+ are directly supported by
1036+
# Net::IMAP::SASL. New authenticators can easily be added for any other
1037+
# <tt>SCRAM-*</tt> mechanism if the digest algorithm is supported by
1038+
# OpenSSL::Digest.
1039+
#
10291040
# +XOAUTH2+::
10301041
# See XOAuth2Authenticator[Net::IMAP::SASL::XOAuth2Authenticator].
10311042
#

lib/net/imap/sasl.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ class IMAP
5151
#
5252
# Login using clear-text username and password.
5353
#
54+
# +SCRAM-SHA-1+::
55+
# +SCRAM-SHA-256+::
56+
# See ScramAuthenticator[Net::IMAP::SASL::ScramAuthenticator].
57+
#
58+
# Login by username and password. The password is not sent to the
59+
# server but is used in a salted challenge/response exchange.
60+
# +SCRAM-SHA-1+ and +SCRAM-SHA-256+ are directly supported by
61+
# Net::IMAP::SASL. New authenticators can easily be added for any other
62+
# <tt>SCRAM-*</tt> mechanism if the digest algorithm is supported by
63+
# OpenSSL::Digest.
64+
#
5465
# +XOAUTH2+::
5566
# See XOAuth2Authenticator[Net::IMAP::SASL::XOAuth2Authenticator].
5667
#
@@ -89,10 +100,15 @@ module SASL
89100
sasl_dir = File.expand_path("sasl", __dir__)
90101
autoload :Authenticators, "#{sasl_dir}/authenticators"
91102
autoload :GS2Header, "#{sasl_dir}/gs2_header"
103+
autoload :ScramAlgorithm, "#{sasl_dir}/scram_algorithm"
104+
autoload :ScramAuthenticator, "#{sasl_dir}/scram_authenticator"
105+
92106
autoload :AnonymousAuthenticator, "#{sasl_dir}/anonymous_authenticator"
93107
autoload :ExternalAuthenticator, "#{sasl_dir}/external_authenticator"
94108
autoload :OAuthBearerAuthenticator, "#{sasl_dir}/oauthbearer_authenticator"
95109
autoload :PlainAuthenticator, "#{sasl_dir}/plain_authenticator"
110+
autoload :ScramSHA1Authenticator, "#{sasl_dir}/scram_sha1_authenticator"
111+
autoload :ScramSHA256Authenticator, "#{sasl_dir}/scram_sha256_authenticator"
96112
autoload :XOAuth2Authenticator, "#{sasl_dir}/xoauth2_authenticator"
97113

98114
autoload :CramMD5Authenticator, "#{sasl_dir}/cram_md5_authenticator"

lib/net/imap/sasl/authenticators.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def initialize(use_defaults: false)
3737
add_authenticator "External"
3838
add_authenticator "OAuthBearer"
3939
add_authenticator "Plain"
40+
add_authenticator "Scram-SHA-1"
41+
add_authenticator "Scram-SHA-256"
4042
add_authenticator "XOAuth2"
4143
add_authenticator "Login" # deprecated
4244
add_authenticator "Cram-MD5" # deprecated

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)