package krb

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file auth_context.ml

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
open! Core
open Async

type t

module Ap_req = struct
  type t = Bigstring.Stable.V1.t [@@deriving bin_io]
end

module Ap_rep = struct
  type t = Bigstring.Stable.V1.t [@@deriving bin_io]
end

module Krb_cred = struct
  type t = Bigstring.Stable.V1.t [@@deriving bin_io]
end

module Raw = struct
  external init : Context.t -> t Krb_result.t = "caml_krb5_auth_con_init"
  external free : Context.t -> t -> unit = "caml_krb5_auth_con_free"

  external setuseruserkey
    :  Context.t
    -> t
    -> Keyblock.t
    -> unit Krb_result.t
    = "caml_krb5_auth_con_setuseruserkey"

  external setflags
    :  Context.t
    -> t
    -> Krb_flags.Auth_context.t list
    -> unit
    = "caml_krb5_auth_con_setflags"

  external setaddrs_compat
    :  Context.t
    -> t
    -> local_port:int
    -> remote_port:int
    -> unit Krb_result.t
    = "caml_krb5_auth_con_setaddrs_compat"

  external setaddrs
    :  Context.t
    -> t
    -> local_port:int
    -> remote_port:int
    -> local_addr:int32
    -> remote_addr:int32
    -> unit Krb_result.t
    = "caml_krb5_auth_con_setaddrs_bytecode" "caml_krb5_auth_con_setaddrs"

  external make_priv
    :  Context.t
    -> t
    -> Bigsubstring.t
    -> Bigstring.t Krb_result.t
    = "caml_krb5_mk_priv"

  external read_priv
    :  Context.t
    -> t
    -> Bigsubstring.t
    -> Bigstring.t Krb_result.t
    = "caml_krb5_rd_priv"

  external make_safe
    :  Context.t
    -> t
    -> Bigsubstring.t
    -> Bigstring.t Krb_result.t
    = "caml_krb5_mk_safe"

  external read_safe
    :  Context.t
    -> t
    -> Bigsubstring.t
    -> Bigstring.t Krb_result.t
    = "caml_krb5_rd_safe"

  external make_ap_req
    :  Context.t
    -> t
    -> Krb_flags.Ap_req.t list
    -> service:string
    -> hostname:string
    -> Cred_cache.Raw.t
    -> Bigstring.t Krb_result.t
    = "caml_krb5_mk_req_bytecode" "caml_krb5_mk_req_native"

  external make_ap_req_extended
    :  Context.t
    -> t
    -> Krb_flags.Ap_req.t list
    -> Credentials.Raw.t
    -> Bigstring.t Krb_result.t
    = "caml_krb5_mk_req_extended"

  external read_ap_req_client
    :  Context.t
    -> t
    -> ap_req:Ap_req.t
    -> Principal.Raw.t
    -> Keytab.Raw.t option
    -> Principal.Raw.t Krb_result.t
    = "caml_krb5_rd_req"

  external make_ap_rep : Context.t -> t -> Ap_rep.t Krb_result.t = "caml_krb5_mk_rep"

  external read_ap_rep
    :  Context.t
    -> t
    -> Ap_rep.t
    -> unit Krb_result.t
    = "caml_krb5_rd_rep"

  external make_krb_cred
    :  Context.t
    -> t
    -> client:Principal.Raw.t
    -> Cred_cache.Raw.t
    -> forwardable:bool
    -> Krb_cred.t Krb_result.t
    = "caml_krb5_fwd_tgt_cred"

  external cc_store_krb_cred
    :  Context.t
    -> t
    -> Cred_cache.Raw.t
    -> Krb_cred.t
    -> unit Krb_result.t
    = "caml_krb5_cc_store_krb_cred"
end

type 'a with_inets =
  local_inet:Socket.Address.Inet.t -> remote_inet:Socket.Address.Inet.t -> 'a

let create () =
  let info = Krb_info.create "[krb5_auth_con_init]" in
  Context_sequencer.enqueue_job_with_info ~info ~f:Raw.init
  >>|? fun t ->
  Context_sequencer.add_finalizer t ~f:Raw.free;
  t
;;

let set_flags raw_auth flags =
  Context_sequencer.enqueue_job_exn ~f:(fun c -> Raw.setflags c raw_auth flags)
;;

let set_addrs_compat raw_auth ~local_port ~remote_port =
  let tag_arguments = lazy [%message (local_port : int) (remote_port : int)] in
  let info = Krb_info.create ~tag_arguments "[krb5_auth_con_setaddrs_compat]" in
  Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
    Raw.setaddrs_compat c raw_auth ~local_port ~remote_port)
;;

let set_addrs raw_auth ~local_port ~remote_port ~local_addr ~remote_addr =
  let tag_arguments =
    lazy
      [%message
        (local_port : int)
          (remote_port : int)
          (local_addr : Unix.Inet_addr.t)
          (remote_addr : Unix.Inet_addr.t)]
  in
  let info = Krb_info.create ~tag_arguments "[krb5_auth_con_setaddrs]" in
  Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
    Raw.setaddrs
      c
      raw_auth
      ~local_port
      ~remote_port
      ~local_addr:(Unix.Inet_addr.inet4_addr_to_int32_exn local_addr)
      ~remote_addr:(Unix.Inet_addr.inet4_addr_to_int32_exn remote_addr))
;;

let set_user_to_user_key raw_auth keyblock =
  let info = Krb_info.create "[krb5_auth_con_setuseruserkey]" in
  Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
    Raw.setuseruserkey c raw_auth keyblock)
;;

let common_init ?user_to_user_key ?handle_addrs () =
  create ()
  >>=? fun t ->
  set_flags t [ KRB5_AUTH_CONTEXT_DO_SEQUENCE ]
  >>= fun () ->
  Option.value_map handle_addrs ~default:Deferred.Or_error.ok_unit ~f:(fun f -> f t)
  >>=? fun () ->
  (match user_to_user_key with
   | None -> Deferred.Or_error.ok_unit
   | Some keyblock -> set_user_to_user_key t keyblock)
  >>|? fun () -> t
;;

let handle_inet_addrs_compat ~local_inet ~remote_inet t =
  let `Inet (_, local_port), `Inet (_, remote_port) = local_inet, remote_inet in
  set_addrs_compat t ~local_port ~remote_port
;;

let handle_inet_addrs ~local_inet ~remote_inet t =
  let `Inet (local_addr, local_port), `Inet (remote_addr, remote_port) =
    local_inet, remote_inet
  in
  set_addrs t ~local_port ~remote_port ~local_addr ~remote_addr
;;

module Client_common = struct
  let mk_req_extended auth_context ?(flags = []) credentials =
    let tag_arguments = lazy [%message (credentials : Credentials.t)] in
    let info = Krb_info.create ~tag_arguments "[krb5_mk_req_extended]" in
    Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
      Raw.make_ap_req_extended c auth_context flags (Credentials.to_raw credentials))
  ;;

  let init ?handle_addrs flags credentials =
    common_init ?handle_addrs ()
    >>=? fun auth_context ->
    mk_req_extended auth_context ~flags credentials
    >>|? fun ap_req -> auth_context, ap_req
  ;;
end

module Service_common = struct
  let read_ap_req_client auth_context ap_req principal keytab =
    let tag_arguments =
      lazy [%message (principal : Principal.t) (keytab : Keytab.t option)]
    in
    let info = Krb_info.create ~tag_arguments "[krb5_rd_req]" in
    Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
      Raw.read_ap_req_client
        c
        auth_context
        ~ap_req
        (Principal.to_raw principal)
        (Option.map keytab ~f:Keytab.to_raw))
    >>=? fun raw_principal ->
    Context_sequencer.add_finalizer raw_principal ~f:Principal.Raw.free;
    Principal.of_raw raw_principal
  ;;

  let init ?handle_addrs principal authentication_key ~ap_req =
    let keytab, user_to_user_key =
      match authentication_key with
      | `Keytab keytab -> Some keytab, None
      | `User_to_user keyblock -> None, Some keyblock
    in
    common_init ?handle_addrs ?user_to_user_key ()
    >>=? fun auth_context ->
    read_ap_req_client auth_context ap_req principal keytab
    >>|? fun client -> auth_context, client
  ;;
end

module V0 = struct
  module Client = struct
    type 'a with_init_args =
      Cred_cache.t -> Krb_flags.Ap_req.t list -> service:string -> hostname:string -> 'a

    let[@warning "-16"] mk_req auth_context ccache ?(flags = []) ~service ~hostname =
      let tag_arguments =
        lazy [%message (ccache : Cred_cache.t) (service : string) (hostname : string)]
      in
      let info = Krb_info.create ~tag_arguments "[krb5_mk_req]" in
      Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
        Raw.make_ap_req c auth_context flags ~service ~hostname (Cred_cache.to_raw ccache))
    ;;

    let init_aux ?handle_addrs ccache flags ~service ~hostname =
      common_init ?handle_addrs ()
      >>=? fun auth_context ->
      mk_req auth_context ccache ~flags ~service ~hostname
      >>|? fun ap_req -> auth_context, ap_req
    ;;

    let init ccache flags ~service ~hostname ~local_inet ~remote_inet =
      let handle_addrs = handle_inet_addrs_compat ~local_inet ~remote_inet in
      init_aux ~handle_addrs ccache flags ~service ~hostname
    ;;

    let init_without_addrs ccache flags ~service ~hostname =
      init_aux ccache flags ~service ~hostname
      >>|? fun (auth_context, ap_req) ->
      ignore (auth_context : t);
      ap_req
    ;;
  end

  module Service = struct
    type 'a with_init_args =
      Principal.t
      -> [ `Keytab of Keytab.t | `User_to_user of Keyblock.t ]
      -> ap_req:Ap_req.t
      -> 'a

    let init principal authentication_key ~ap_req ~local_inet ~remote_inet =
      let handle_addrs = handle_inet_addrs_compat ~local_inet ~remote_inet in
      Service_common.init ~handle_addrs principal authentication_key ~ap_req
    ;;
  end
end

(* V1 uses [mk_req_extended], so it takes in credentials rather than a service name, host
   name, and credential cache.*)
module V1 = struct
  module Client = struct
    type 'a with_init_args = Krb_flags.Ap_req.t list -> Credentials.t -> 'a

    let init flags credentials ~local_inet ~remote_inet =
      let handle_addrs = handle_inet_addrs_compat ~local_inet ~remote_inet in
      Client_common.init ~handle_addrs flags credentials
    ;;
  end

  module Service = V0.Service
end

(* V0 and V1 used a broken [set_addrs] function (it didn't correctly
   set the addresses). It doesn't actually matter that it is wrong, because the only check
   is for consistency across what the server and client set. In V2, we start to use
   [set_addrs] correctly, to be compatible with krb [genaddrs]. *)
module Client = struct
  type 'a with_init_args = 'a V1.Client.with_init_args

  let init flags credentials ~local_inet ~remote_inet =
    let handle_addrs t = handle_inet_addrs t ~local_inet ~remote_inet in
    Client_common.init ~handle_addrs flags credentials
  ;;

  let init_without_addrs flags credentials =
    Client_common.init flags credentials
    >>|? fun (auth_context, ap_req) ->
    ignore (auth_context : t);
    ap_req
  ;;

  let read_and_verify_ap_rep auth_context ~ap_rep =
    let info = Krb_info.create "[krb5_rd_rep]" in
    Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
      Raw.read_ap_rep c auth_context ap_rep)
  ;;

  let make_krb_cred t ~forwardable ~client ccache =
    let tag_arguments = lazy [%message (client : Principal.t) (ccache : Cred_cache.t)] in
    let tag_error = function
      (* KRB5KDC_ERR_BADOPTION - KDC can't fulfill requested option *)
      | -1765328371l -> return [%message "Make sure your tgt is forwardable."]
      | _ -> return Sexp.unit
    in
    let info = Krb_info.create ~tag_arguments ~tag_error "[krb5_fwd_tgt_creds]" in
    Context_sequencer.enqueue_job_with_info ~info ~f:(fun ctx ->
      let client = Principal.to_raw client in
      let ccache = Cred_cache.to_raw ccache in
      Raw.make_krb_cred ctx t ~client ccache ~forwardable)
  ;;
end

module Service = struct
  type 'a with_init_args = 'a V1.Service.with_init_args

  let init principal authentication_key ~ap_req ~local_inet ~remote_inet =
    let handle_addrs t = handle_inet_addrs t ~local_inet ~remote_inet in
    Service_common.init ~handle_addrs principal authentication_key ~ap_req
  ;;

  let init_without_addrs principal authentication_key ~ap_req =
    Service_common.init principal authentication_key ~ap_req
    >>|? fun (auth_context, client_principal) ->
    ignore (auth_context : t);
    client_principal
  ;;

  let make_ap_rep auth_context =
    let info = Krb_info.create "[krb5_mk_rep]" in
    Context_sequencer.enqueue_job_with_info ~info ~f:(fun c ->
      Raw.make_ap_rep c auth_context)
  ;;

  let read_krb_cred_into_cred_cache auth_context krb_cred ccache =
    let tag_arguments = lazy [%message (ccache : Cred_cache.t)] in
    let info = Krb_info.create ~tag_arguments "[krb5_cc_store_krb_cred]" in
    Context_sequencer.enqueue_job_with_info ~info ~f:(fun ctx ->
      let ccache = Cred_cache.to_raw ccache in
      Raw.cc_store_krb_cred ctx auth_context ccache krb_cred)
  ;;
end

module Safe = struct
  let encode auth_context data =
    let info = Krb_info.create "[krb5_mk_safe]" in
    Context_sequencer.enqueue_blocking_if_below_encryption_size_threshold
      ~data_size:(Bigsubstring.length data)
      ~info
      ~f:(fun c -> Raw.make_safe c auth_context data)
  ;;

  let decode auth_context data =
    let info = Krb_info.create "[krb5_rd_safe]" in
    Context_sequencer.enqueue_blocking_if_below_encryption_size_threshold
      ~data_size:(Bigsubstring.length data)
      ~info
      ~f:(fun c -> Raw.read_safe c auth_context data)
  ;;
end

module Priv = struct
  let encode auth_context data =
    let info = Krb_info.create "[krb5_mk_priv]" in
    Context_sequencer.enqueue_blocking_if_below_encryption_size_threshold
      ~data_size:(Bigsubstring.length data)
      ~info
      ~f:(fun c -> Raw.make_priv c auth_context data)
  ;;

  let decode auth_context data =
    let info = Krb_info.create "[krb5_rd_priv]" in
    Context_sequencer.enqueue_blocking_if_below_encryption_size_threshold
      ~data_size:(Bigsubstring.length data)
      ~info
      ~f:(fun c -> Raw.read_priv c auth_context data)
  ;;
end
OCaml

Innovation. Community. Security.