Source

oni / sasl / sasl_digest.ml

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
(*---------------------------------------------------------------------------*
  $Change$
  Copyright (c) 2004-2010, James H. Woodyatt
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  
    Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    
    Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  OF THE POSSIBILITY OF SUCH DAMAGE. 
 *---------------------------------------------------------------------------*)

(**)
module J = Cf_journal
let jout = J.stdout
(**)

open Sasl_error

type cipher_t =
    | Cipher_des
    | Cipher_3des
    | Cipher_rc4
    | Cipher_rc4_40
    | Cipher_rc4_56
    | Cipher_extend of string

type qop_t =
    | QOP_auth
    | QOP_auth_int
    | QOP_auth_conf of cipher_t
    | QOP_extend of string

type uri_t = {
    uri_serv_type: string;
    uri_host: string;
    uri_serv_name: string option;
}

type challenge_data_t = {
    c_realm: string list;
    c_charset: string option;
    c_qoplist: qop_t list;
    c_maxbuf: int;
}

type response_data_t = {
    r_realm: string;
    r_secret: string;
    r_username: string;
    r_authzid: string option;
    r_uri: uri_t option;
    r_qop: qop_t;
    r_maxbuf: int;
}

type challenge_t = [ `Q_digest of challenge_data_t ]
type response_t = [ `R_digest of response_data_t ]

let rec emit_list_aux_ f pp = function
    | [] ->
        ()
    | hd :: tl ->
        Format.pp_print_char pp ',';
        f pp hd;
        emit_list_aux_ f pp tl

let emit_list_ f pp = function
    | [] ->
        ()
    | hd :: tl ->
        f pp hd;
        emit_list_aux_ f pp tl
    
let qop_to_string_ =
    let
        auth_ = "auth" and
        auth_int_ = "auth-int" and
        auth_conf_ = "auth-conf"
    in
    function
    | QOP_auth -> auth_
    | QOP_auth_int -> auth_int_
    | QOP_auth_conf _ -> auth_conf_
    | QOP_extend str -> str
    
let cipher_to_string_ =
    let
        des_ = "des" and
        threedes_ = "3des" and
        rc4_ = "rc4" and
        rc4_40_ = "rc4-40" and
        rc4_56_ = "rc4-56"
    in
    function
    | Cipher_des -> des_
    | Cipher_3des -> threedes_
    | Cipher_rc4 -> rc4_
    | Cipher_rc4_40 -> rc4_40_
    | Cipher_rc4_56 -> rc4_56_
    | Cipher_extend s -> s

module P = struct
    open Cf_llscan.Op
    
    let equal = Mime_lex.parse_optfws >>= fun () -> ?.'='
    let comma = Mime_lex.parse_optfws >>= fun () -> ?.','
    
    let directive =
        Mime_lex.parse_atom >>= fun tag ->
        equal >>= fun _ ->
        Mime_lex.parse_word >>= fun str ->
        Cf_llscan.ret (String.lowercase tag, str)
    
    let message =
        Cf_llscan.req begin
            directive >>= fun hd ->
            ?* (comma >>= fun _ -> directive) >>= fun tl ->
            Cf_llscan.fin >>= fun () ->
            Cf_llscan.ret (hd :: tl)
        end
    
    let tokenlist =
        Cf_llscan.req begin
            Mime_lex.parse_atom >>= fun hd ->
            ?* (comma >>= fun _ -> Mime_lex.parse_atom) >>= fun tl ->
            Cf_llscan.fin >>= fun () ->
            Cf_llscan.ret (hd :: tl)
        end
        
    let parse_error_ () = raise (X X_mechanism_error)
    
    let partition_on_directive_ name s =
        List.partition (fun (n, _) -> n = name) s
    
    let optional_one_ name s =
        let a, b = partition_on_directive_ name s in
        match a with
        | [] -> None, b
        | (_, a) :: [] -> Some a, b
        | _ -> parse_error_ ()
    
    let require_nonzero_ name s =
        let a, b = partition_on_directive_ name s in
        match a with
        | [] -> parse_error_ ()
        | _ -> let a = List.map (fun (_, r) -> r) a in a, b
    
    let require_one_ name s =
        let a, b = partition_on_directive_ name s in
        match a with
        | (_, a) :: [] -> a, b
        | _ -> parse_error_ ()
end

module M1 = struct
    open Cf_llscan.Op
    
    type t = {
        nonce: string;
        realm: string list;
        qoplist: qop_t list;
        maxbuf: int;
        stale: bool;
        utf8: bool;
    }
    
    let default_ = {
        nonce = "";
        realm = [];
        qoplist = [];
        maxbuf = 65536;
        stale = false;
        utf8 = false;
    }
    
    let rec emit_qoplist_ pp acc = function
        | [] ->
            emit_list_ Mime_lex.emit_token pp (List.rev acc)
        | hd :: tl ->
            let str = qop_to_string_ hd in
            let acc = if List.mem str acc then acc else str :: acc in
            emit_qoplist_ pp acc tl
    
    let rec emit_ciphers_ pp acc = function
        | [] ->
            emit_list_ Mime_lex.emit_token pp (List.rev acc)
        | hd :: tl ->
            match hd with
            | QOP_auth
            | QOP_auth_int
            | QOP_extend _ ->
                emit_ciphers_ pp acc tl
            | QOP_auth_conf c ->
                let str = cipher_to_string_ c in
                let acc = if List.mem str acc then acc else str :: acc in
                emit_ciphers_ pp acc tl
    
    let ciphers_exist_ =
        List.exists (function QOP_auth_conf _ -> true | _ -> false)
    
    let emit m =
        let qop_options =
            match m.qoplist with
            | [] ->
                ""
            | x ->
                let b = Buffer.create 40 in
                let pp = Mime_lex.formatter b in
                emit_qoplist_ pp [] m.qoplist;
                Format.pp_print_flush pp ();
                Buffer.contents b
        and cipher_opt =
            if ciphers_exist_ m.qoplist then begin
                let b = Buffer.create 40 in
                let pp = Mime_lex.formatter b in
                emit_ciphers_ pp [] m.qoplist;
                Format.pp_print_flush pp ();
                Buffer.contents b
            end
            else
                ""
        in
        let b = Buffer.create 256 in
        let pp = Mime_lex.formatter b in
        Format.pp_print_string pp "algorithm=md5-sess,nonce=";
        Mime_lex.emit_quoted_string pp m.nonce;
        if m.utf8 then Format.pp_print_string pp ",charset=utf8";
        if m.stale then Format.pp_print_string pp ",stale=true";
        emit_list_ Mime_lex.emit_quoted_string pp m.realm;
        if qop_options <> "" then begin
            Format.pp_print_string pp ",qop-options=";
            Mime_lex.emit_quoted_string pp qop_options
        end;
        if cipher_opt <> "" then begin
            if m.maxbuf <> default_.maxbuf then
                Format.fprintf pp ",maxbuf=%u" m.maxbuf;
            Format.pp_print_string pp ",cipher-opts=";
            Mime_lex.emit_quoted_string pp cipher_opt
        end
    
    let rec parse_cipher_opt_aux_ acc = function
        | [] ->
            List.rev acc
        | hd :: tl ->
            let hd =
                match String.lowercase hd with
                | "des" -> Cipher_des
                | "3des" -> Cipher_3des
                | "rc4" -> Cipher_rc4
                | "rc4-40" -> Cipher_rc4_40
                | "rc4-56" -> Cipher_rc4_56
                | x -> Cipher_extend x
            in
            parse_cipher_opt_aux_ (hd :: acc) tl
    
    let parse_cipher_opt_ cipher_opt =
        match P.tokenlist (Cf_seq.of_string cipher_opt) with
        | Some (x, _) -> parse_cipher_opt_aux_ [] x
        | None -> []
    
    let rec parse_qop_options_aux_ acc cipher_opt = function
        | [] ->
            List.rev acc
        | hd :: tl as all ->
            match String.lowercase hd with
            | "auth" ->
                parse_qop_options_aux_ (QOP_auth :: acc) cipher_opt tl
            | "auth-int" ->
                parse_qop_options_aux_ (QOP_auth_int :: acc) cipher_opt tl
            | "auth-conf" ->
                begin
                    match cipher_opt with
                    | [] ->
                        parse_qop_options_aux_ acc [] tl
                    | cHd :: cTl ->
                        let qop = QOP_auth_conf cHd in
                        parse_qop_options_aux_ (qop :: acc) cTl all
                end
            | x ->
                let qop = QOP_extend x in
                parse_qop_options_aux_ (qop :: acc) cipher_opt tl
    
    let parse_qop_options_ qop_options cipher_opt =
        match P.tokenlist (Cf_seq.of_string qop_options) with
        | None ->
            raise (X X_mechanism_error)
        | Some (qop_options, _) ->
            let cipher_opt =
                let f = function "auth-conf" -> true | _ -> false in
                if List.exists f qop_options then
                    match cipher_opt with
                    | (_, v) :: [] -> parse_cipher_opt_ v
                    | _ -> raise (X X_mechanism_error)
                else
                    []
            in
            parse_qop_options_aux_ [] cipher_opt qop_options
    
    let parse m =
        match P.message (Cf_message.to_seq m) with
        | None ->
            raise (X X_mechanism_error)
        | Some (s, _) ->
            let nonce, s = P.require_one_ "nonce" s in
            let qop_options, s = P.require_one_ "qop-options" s in
            let cipher_opt, s = P.partition_on_directive_ "cipher-opt" s in
            let realm, s = P.require_nonzero_ "realm" s in
            let utf8, s = P.optional_one_ "charset" s in
            let utf8 =
                match utf8 with
                | None -> false
                | Some str when String.lowercase str = "utf8" -> true
                | _ -> P.parse_error_ ()
            in
            let stale, s = P.optional_one_ "stale" s in
            let stale =
                match stale with
                | None -> false
                | Some str when String.lowercase str = "true" -> true
                | _ -> P.parse_error_ ()
            in
            let maxbuf, _ = P.optional_one_ "maxbuf" s in
            let maxbuf =
                match maxbuf with
                | None ->
                    default_.maxbuf
                | Some str ->
                    try int_of_string str with _ -> P.parse_error_ ()
            in
            let qoplist = parse_qop_options_ qop_options cipher_opt in
            {
                nonce = nonce;
                realm = realm;
                qoplist = qoplist;
                maxbuf = maxbuf;
                stale = stale;
                utf8 = utf8;
            }
end

module M2 = struct
    open Cf_llscan.Op
    
    type t = {
        username: string;
        nonce: string;
        cnonce: string;
        response: string;
        realm: string option;
        authzid: string option;
        nc: int32 option;
        uri: (string * string * string option) option;
        maxbuf: int;
        utf8: bool;
        cipher: cipher_t option;
    }
    
    let default_ = {
        username = "";
        nonce = "";
        cnonce = "";
        response = "";
        realm = None;
        authzid = None;
        nc = None;
        uri = None;
        maxbuf = 65536;
        utf8 = false;
        cipher = None;
    }
end

module type Hash_T = sig
    val name: string
    val create: unit -> Cryptokit.hash
end

module type T = sig
    module H: Hash_T
    
    class ['u] client:
        ('u, [> challenge_t ], [> response_t ]) Sasl_supplicant.request_t
            #Iom_gadget.tx ->
        ['u] Sasl_mechanism.client
    
    class ['u] service:
        ('u, [> response_t ]) Sasl_authenticator.request_t #Iom_gadget.tx ->
        ['u] Sasl_mechanism.service
end

module Create(H: Hash_T) : (T with module H = H) = struct
    module H = H
    
    module G = Iom_gadget
    module Mech = Sasl_mechanism
    module Auth = Sasl_authenticator
    module Supp = Sasl_supplicant
    module Sec = Sasl_security
    
    let name_ = "DIGEST-" ^ String.uppercase H.name
    
    let properties_ = {
        Sec.p_flags = Sec.Flags.nil;
        Sec.p_ssf = 0;
    }
    
    class ['u] client reqTx =
        let reqTx = (reqTx :> ('u, 'c, 'r) Supp.request_t G.tx) in
        let unimplemented_ = false in
        object
            inherit ['u] Mech.client_core ~name:name_ ~sec:properties_
            constraint 'c = [> challenge_t ]
            constraint 'r = [> response_t ]
            
            method core ctrlRx ctrlTx =
                let ctrlRx = (ctrlRx :> Mech.client_tx_t G.rx) in
                let ctrlTx = (ctrlTx :> 'u Mech.client_rx_t G.tx) in
                jout#fail "Sasl_digest.client#core: incomplete!"
                Cf_cmonad.nil 
        end
    
    class ['u] service authTxTx =
        let authTxTx = (authTxTx :> ('u, 'r) Auth.request_t G.tx) in
        let unimplemented_ = false in
        object(self)
            inherit ['u] Mech.service_core ~name:name_ ~sec:properties_
            constraint 'r = [> response_t ]
            
            method core ctrlRx ctrlTx =
                let ctrlRx = (ctrlRx :> Mech.service_tx_t G.rx) in
                let ctrlTx = (ctrlTx :> 'u Mech.service_rx_t G.tx) in
                jout#fail "Sasl_digest.service#core: incomplete!"
                Cf_cmonad.nil 
        end
end

module H_md5: Hash_T = struct
    let name = "MD5"
    let create = Cryptokit.Hash.md5
end

module H_sha1: Hash_T = struct
    let name = "SHA1"
    let create = Cryptokit.Hash.sha1
end

module MD5 = Create(H_md5)
module SHA1 = Create(H_sha1)

(*--- $File$ ---*)