11# frozen_string_literal: true
22
3- # Net::IMAP authenticator for the "` DIGEST-MD5`" SASL mechanism type, specified
3+ # Net::IMAP authenticator for the + DIGEST-MD5+ SASL mechanism type, specified
44# in RFC-2831[https://tools.ietf.org/html/rfc2831]. See Net::IMAP#authenticate.
55#
66# == Deprecated
99# RFC-6331[https://tools.ietf.org/html/rfc6331] and should not be relied on for
1010# security. It is included for compatibility with existing servers.
1111class Net ::IMAP ::SASL ::DigestMD5Authenticator
12+ DataFormatError = Net ::IMAP ::DataFormatError
13+ ResponseParseError = Net ::IMAP ::ResponseParseError
14+ private_constant :DataFormatError , :ResponseParseError
15+
1216 STAGE_ONE = :stage_one
1317 STAGE_TWO = :stage_two
1418 STAGE_DONE = :stage_done
@@ -42,6 +46,60 @@ class Net::IMAP::SASL::DigestMD5Authenticator
4246 #
4347 attr_reader :authzid
4448
49+ # A namespace or collection of identities which contains +username+.
50+ #
51+ # Used by DIGEST-MD5, GSS-API, and NTLM. This is often a domain name that
52+ # contains the name of the host performing the authentication.
53+ #
54+ # <em>Defaults to the last realm in the server-provided list of
55+ # realms.</em>
56+ attr_reader :realm
57+
58+ # Fully qualified canonical DNS host name for the requested service.
59+ #
60+ # <em>Defaults to #realm.</em>
61+ attr_reader :host
62+
63+ # The service protocol, a
64+ # {registered GSSAPI service name}[https://www.iana.org/assignments/gssapi-service-names/gssapi-service-names.xhtml],
65+ # e.g. "imap", "ldap", or "xmpp".
66+ #
67+ # For Net::IMAP, the default is "imap" and should not be overridden. This
68+ # must be set appropriately to use authenticators in other protocols.
69+ #
70+ # If an IANA-registered name isn't available, GSS-API
71+ # (RFC-2743[https://tools.ietf.org/html/rfc2743]) allows the generic name
72+ # "host".
73+ attr_reader :service
74+
75+ # The generic server name when the server is replicated.
76+ #
77+ # Not used by other \SASL mechanisms. +service_name+ will be ignored when it
78+ # is +nil+ or identical to +host+.
79+ #
80+ # From RFC-2831[https://tools.ietf.org/html/rfc2831]:
81+ # >>>
82+ # The service is considered to be replicated if the client's
83+ # service-location process involves resolution using standard DNS lookup
84+ # operations, and if these operations involve DNS records (such as SRV, or
85+ # MX) which resolve one DNS name into a set of other DNS names. In this
86+ # case, the initial name used by the client is the "serv-name", and the
87+ # final name is the "host" component.
88+ attr_reader :service_name
89+
90+ # Parameters sent by the server are stored in this hash.
91+ attr_reader :sparams
92+
93+ # The charset sent by the server. "UTF-8" (case insensitive) is the only
94+ # allowed value. +nil+ should be interpreted as ISO 8859-1.
95+ attr_reader :charset
96+
97+ # nonce sent by the server
98+ attr_reader :nonce
99+
100+ # qop-options sent by the server
101+ attr_reader :qop
102+
45103 # :call-seq:
46104 # new(username, password, authzid = nil, **options) -> authenticator
47105 # new(username:, password:, authzid: nil, **options) -> authenticator
@@ -64,90 +122,81 @@ class Net::IMAP::SASL::DigestMD5Authenticator
64122 # When +authzid+ is not set, the server should derive the authorization
65123 # identity from the authentication identity.
66124 #
125+ # * _optional_ #realm — A namespace for the #username, e.g. a domain.
126+ # <em>Defaults to the last realm in the server-provided realms list.</em>
127+ # * _optional_ #host — FQDN for requested service.
128+ # <em>Defaults to</em> #realm.
129+ # * _optional_ #service_name — The generic host name when the server is
130+ # replicated.
131+ # * _optional_ #service — the registered service protocol. E.g. "imap",
132+ # "smtp", "ldap", "xmpp".
133+ # <em>For Net::IMAP, this defaults to "imap".</em>
134+ #
67135 # * _optional_ +warn_deprecation+ — Set to +false+ to silence the warning.
68136 #
69137 # Any other keyword arguments are silently ignored.
70138 def initialize ( user = nil , pass = nil , authz = nil ,
71139 username : nil , password : nil , authzid : nil ,
72140 authcid : nil , secret : nil ,
141+ realm : nil , service : "imap" , host : nil , service_name : nil ,
73142 warn_deprecation : true , **)
74- username = authcid || username || user or
75- raise ArgumentError , "missing username (authcid)"
76- password ||= secret || pass or raise ArgumentError , "missing password"
77- authzid ||= authz
78143 if warn_deprecation
79- warn "WARNING: DIGEST-MD5 SASL mechanism was deprecated by RFC6331."
80- # TODO: recommend SCRAM instead.
144+ warn "WARNING: DIGEST-MD5 SASL mechanism was deprecated by RFC-6331."
81145 end
146+
82147 require "digest/md5"
148+ require "securerandom"
83149 require "strscan"
84- @username , @password , @authzid = username , password , authzid
150+
151+ [ authcid , username , user ] . compact . count == 1 or
152+ raise ArgumentError , "conflicting values for username"
153+ [ authzid , authz ] . compact . count <= 1 or
154+ raise ArgumentError , "conflicting values for authzid"
155+ [ password , secret , pass ] . compact . count == 1 or
156+ raise ArgumentError , "conflicting values for password"
157+
158+ @username = authcid || username || user
159+ @password = password || secret || pass
160+ @authzid = authzid || authz
161+ @realm = realm
162+ @host = host
163+ @service = service
164+ @service_name = service_name
165+
166+ @username or raise ArgumentError , "missing username (authcid)"
167+ @password or raise ArgumentError , "missing password"
168+
85169 @nc , @stage = { } , STAGE_ONE
86170 end
87171
172+ # From RFC-2831[https://tools.ietf.org/html/rfc2831]:
173+ # >>>
174+ # Indicates the principal name of the service with which the client wishes
175+ # to connect, formed from the serv-type, host, and serv-name. For
176+ # example, the FTP service on "ftp.example.com" would have a "digest-uri"
177+ # value of "ftp/ftp.example.com"; the SMTP server from the example above
178+ # would have a "digest-uri" value of "smtp/mail3.example.com/example.com".
179+ def digest_uri
180+ if service_name && service_name != host
181+ "#{ service } /#{ host } /#{ service_name } "
182+ else
183+ "#{ service } /#{ host } "
184+ end
185+ end
186+
88187 def initial_response? ; false end
89188
90189 # Responds to server challenge in two stages.
91190 def process ( challenge )
92191 case @stage
93192 when STAGE_ONE
94193 @stage = STAGE_TWO
95- sparams = { }
96- c = StringScanner . new ( challenge )
97- while c . scan ( /(?:\s *,)?\s *(\w +)=("(?:[^\\ "]|\\ .)*"|[^,]+)\s */ )
98- k , v = c [ 1 ] , c [ 2 ]
99- if v =~ /^"(.*)"$/
100- v = $1
101- if v =~ /,/
102- v = v . split ( ',' )
103- end
104- end
105- sparams [ k ] = v
106- end
107-
108- raise Net ::IMAP ::DataFormatError , "Bad Challenge: '#{ challenge } '" unless c . eos? and sparams [ 'qop' ]
109- raise Net ::IMAP ::Error , "Server does not support auth (qop = #{ sparams [ 'qop' ] . join ( ',' ) } )" unless sparams [ 'qop' ] . include? ( "auth" )
110-
111- response = {
112- :nonce => sparams [ 'nonce' ] ,
113- :username => @username ,
114- :realm => sparams [ 'realm' ] ,
115- :cnonce => Digest ::MD5 . hexdigest ( "%.15f:%.15f:%d" % [ Time . now . to_f , rand , Process . pid . to_s ] ) ,
116- :'digest-uri' => 'imap/' + sparams [ 'realm' ] ,
117- :qop => 'auth' ,
118- :maxbuf => 65535 ,
119- :nc => "%08d" % nc ( sparams [ 'nonce' ] ) ,
120- :charset => sparams [ 'charset' ] ,
121- }
122-
123- response [ :authzid ] = @authzid unless @authzid . nil?
124-
125- # now, the real thing
126- a0 = Digest ::MD5 . digest ( [ response . values_at ( :username , :realm ) , @password ] . join ( ':' ) )
127-
128- a1 = [ a0 , response . values_at ( :nonce , :cnonce ) ] . join ( ':' )
129- a1 << ':' + response [ :authzid ] unless response [ :authzid ] . nil?
130-
131- a2 = "AUTHENTICATE:" + response [ :'digest-uri' ]
132- a2 << ":00000000000000000000000000000000" if response [ :qop ] and response [ :qop ] =~ /^auth-(?:conf|int)$/
133-
134- response [ :response ] = Digest ::MD5 . hexdigest (
135- [
136- Digest ::MD5 . hexdigest ( a1 ) ,
137- response . values_at ( :nonce , :nc , :cnonce , :qop ) ,
138- Digest ::MD5 . hexdigest ( a2 )
139- ] . join ( ':' )
140- )
141-
142- return response . keys . map { |key | qdval ( key . to_s , response [ key ] ) } . join ( ',' )
194+ process_stage_one ( challenge )
195+ stage_one_response
143196 when STAGE_TWO
144197 @stage = STAGE_DONE
145- # if at the second stage, return an empty string
146- if challenge =~ /rspauth=/
147- return ''
148- else
149- raise ResponseParseError , challenge
150- end
198+ process_stage_two ( challenge )
199+ "" # if at the second stage, return an empty string
151200 else
152201 raise ResponseParseError , challenge
153202 end
@@ -157,23 +206,158 @@ def done?; @stage == STAGE_DONE end
157206
158207 private
159208
209+ def process_stage_one ( challenge )
210+ @sparams = parse_challenge ( challenge )
211+ @qop = sparams . key? ( "qop" ) ? [ "auth" ] : sparams [ "qop" ] . flatten
212+
213+ guard_stage_one ( challenge )
214+
215+ @nonce = sparams [ "nonce" ] . first
216+ @charset = sparams [ "charset" ] . first
217+
218+ @realm ||= sparams [ "realm" ] . last
219+ @host ||= realm
220+ end
221+
222+ def guard_stage_one ( challenge )
223+ if !qop . include? ( "auth" )
224+ raise DataFormatError , "Server does not support auth (qop = %p)" % [
225+ sparams [ "qop" ]
226+ ]
227+ elsif ( emptykey = REQUIRED . find { sparams [ _1 ] . empty? } )
228+ raise DataFormatError , "Server didn't send %p (%p)" % [ emptykey , challenge ]
229+ elsif ( multikey = NO_MULTIPLES . find { sparams [ _1 ] . length > 1 } )
230+ raise DataFormatError , "Server sent multiple %p (%p)" % [ multikey , challenge ]
231+ end
232+ end
233+
234+ def stage_one_response
235+ response = {
236+ nonce : nonce ,
237+ username : username ,
238+ realm : realm ,
239+ cnonce : SecureRandom . base64 ( 32 ) ,
240+ "digest-uri" : digest_uri ,
241+ qop : "auth" ,
242+ maxbuf : 65535 ,
243+ nc : "%08d" % nc ( nonce ) ,
244+ charset : charset ,
245+ }
246+
247+ response [ :authzid ] = authzid unless authzid . nil?
248+ response [ :response ] = compute_digest ( response )
249+
250+ format_response ( response )
251+ end
252+
253+ def process_stage_two ( challenge )
254+ raise ResponseParseError , challenge unless challenge =~ /rspauth=/
255+ end
256+
160257 def nc ( nonce )
161- if @nc . has_key? nonce
162- @nc [ nonce ] = @nc [ nonce ] + 1
163- else
164- @nc [ nonce ] = 1
258+ @nc [ nonce ] = @nc . key? ( nonce ) ? @nc [ nonce ] + 1 : 1
259+ @nc [ nonce ]
260+ end
261+
262+ def compute_digest ( response )
263+ a1 = compute_a1 ( response )
264+ a2 = compute_a2 ( response )
265+ Digest ::MD5 . hexdigest (
266+ [
267+ Digest ::MD5 . hexdigest ( a1 ) ,
268+ response . values_at ( :nonce , :nc , :cnonce , :qop ) ,
269+ Digest ::MD5 . hexdigest ( a2 )
270+ ] . join ( ":" )
271+ )
272+ end
273+
274+ def compute_a0 ( response )
275+ Digest ::MD5 . digest (
276+ [ response . values_at ( :username , :realm ) , password ] . join ( ":" )
277+ )
278+ end
279+
280+ def compute_a1 ( response )
281+ a0 = compute_a0 ( response )
282+ a1 = [ a0 , response . values_at ( :nonce , :cnonce ) ] . join ( ":" )
283+ a1 << ":#{ response [ :authzid ] } " unless response [ :authzid ] . nil?
284+ a1
285+ end
286+
287+ def compute_a2 ( response )
288+ a2 = "AUTHENTICATE:#{ response [ :"digest-uri" ] } "
289+ if response [ :qop ] and response [ :qop ] =~ /^auth-(?:conf|int)$/
290+ a2 << ":00000000000000000000000000000000"
291+ end
292+ a2
293+ end
294+
295+ # Directives which must not have multiples. The RFC states:
296+ # >>>
297+ # This directive may appear at most once; if multiple instances are present,
298+ # the client should abort the authentication exchange.
299+ NO_MULTIPLES = %w[ nonce stale maxbuf charset algorithm ] . freeze
300+
301+ # Required directives which must occur exactly once. The RFC states: >>>
302+ # This directive is required and MUST appear exactly once; if not present,
303+ # or if multiple instances are present, the client should abort the
304+ # authentication exchange.
305+ REQUIRED = %w[ nonce algorithm ] . freeze
306+
307+ # Directives which are composed of one or more comma delimited tokens
308+ QUOTED_LISTABLE = %w[ qop cipher ] . freeze
309+
310+ private_constant :NO_MULTIPLES , :REQUIRED , :QUOTED_LISTABLE
311+
312+ LWS = /[\r \n \t ]*/n # less strict than RFC, more strict than '\s'
313+ TOKEN = /[^\x00 -\x20 \x7f ()<>@,;:\\ "\/ \[ \] ?={}]+/n
314+ QUOTED_STR = /"(?: [\t \x20 -\x7e &&[^"]] | \\ [\x00 -\x7f ] )*"/nx
315+ LIST_DELIM = /(?:#{ LWS } , )+ #{ LWS } /nx
316+ AUTH_PARAM = /
317+ (#{ TOKEN } ) #{ LWS } = #{ LWS } (#{ QUOTED_STR } | #{ TOKEN } ) #{ LIST_DELIM } ?
318+ /nx
319+
320+ private_constant :LWS , :TOKEN , :QUOTED_STR , :LIST_DELIM , :AUTH_PARAM
321+
322+ def parse_challenge ( challenge )
323+ sparams = Hash . new { |h , k | h [ k ] = [ ] }
324+ c = StringScanner . new ( challenge )
325+ c . skip LIST_DELIM
326+ while c . scan AUTH_PARAM
327+ k , v = c [ 1 ] , c [ 2 ]
328+ k = k . downcase
329+ if v =~ /\A "(.*)"\z /mn
330+ v = $1. gsub ( /\\ (.)/mn , '\1' )
331+ v = split_quoted_list ( v , challenge ) if QUOTED_LISTABLE . include? k
332+ end
333+ sparams [ k ] << v
334+ end
335+ c . eos? or raise DataFormatError , "Bad Challenge: %p" % [ challenge ]
336+ sparams . any? or raise DataFormatError , "Bad Challenge: %p" % [ challenge ]
337+ sparams
338+ end
339+
340+ def split_quoted_list ( value , challenge )
341+ value . split ( LIST_DELIM ) . reject ( &:empty? ) . tap do
342+ _1 . any? or raise DataFormatError , "Bad Challenge: %p" % [ challenge ]
165343 end
166- return @nc [ nonce ]
344+ end
345+
346+ def format_response ( response )
347+ response
348+ . keys
349+ . map { |key | qdval ( key . to_s , response [ key ] ) }
350+ . join ( "," )
167351 end
168352
169353 # some responses need quoting
170- def qdval ( k , v )
171- return if k . nil? or v . nil?
172- if %w" username authzid realm nonce cnonce digest-uri qop " . include? k
173- v = v . gsub ( /([\\ "])/ , "\\ \1 " )
174- return '%s="%s"' % [ k , v ]
354+ def qdval ( key , val )
355+ return if key . nil? or val . nil?
356+ if %w[ username authzid realm nonce cnonce digest-uri qop ] . include? key
357+ val = val . gsub ( /([\\ "])/n , "\\ \1 " )
358+ '%s="%s"' % [ key , val ]
175359 else
176- return ' %s=%s' % [ k , v ]
360+ " %s=%s" % [ key , val ]
177361 end
178362 end
179363
0 commit comments