Skip to content

Commit

Permalink
use Cstruct.length instead of deprecated Cstruct.len, require cstruct…
Browse files Browse the repository at this point in the history
… 6.0.0
  • Loading branch information
hannesm committed Aug 4, 2021
1 parent 6b1e199 commit e62cd5a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion otr.opam
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bug-reports: "https://github.com/hannesm/ocaml-otr/issues"
depends: [
"ocaml" {>= "4.07.0"}
"dune"
"cstruct" {>= "1.9.0"}
"cstruct" {>= "6.0.0"}
"sexplib0"
"mirage-crypto"
"mirage-crypto-pk"
Expand Down
4 changes: 2 additions & 2 deletions src/otr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module Engine = struct
| None -> Ok (state, None, [])
| Some data ->
let rec process_data state data out warn =
match Cstruct.len data with
match Cstruct.length data with
| 0 -> (state, out, warn)
| _ -> match Otr_parser.parse_tlv data with
| Ok (typ, buf, rest) ->
Expand Down Expand Up @@ -99,7 +99,7 @@ module Engine = struct
if ctr' <= keyblock.recv_ctr then
Ok (dh_keys, symm, None, [`Warning "ignoring message with invalid counter"])
else
let stop = Cstruct.len bytes - Cstruct.len reveal - 4 - 20 in
let stop = Cstruct.length bytes - Cstruct.length reveal - 4 - 20 in
guard (stop >= 0) "invalid data" >>= fun () ->
let mac' = Otr_crypto.sha1mac ~key:keyblock.recv_mac (Cstruct.sub bytes 0 stop) in
guard (Cstruct.equal mac mac') "invalid mac" >>| fun () ->
Expand Down
6 changes: 3 additions & 3 deletions src/otr_ake.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ let check_reveal_send_sig ctx (dh_secret, gy) dh_commit buf =
let check_sig ctx (ssid, c', m1', m2') (dh_secret, gx) gy signature =
(* decrypt signature, verify it and macs *)
safe_parse Otr_parser.decode_data signature >>= fun (enc_data, mac) ->
guard (Cstruct.len mac = 20) (Unknown "mac has wrong length") >>= fun () ->
guard (Cstruct.length mac = 20) (Unknown "mac has wrong length") >>= fun () ->
let mymac = Otr_crypto.mac160 ~key:m2' enc_data in
guard (Cstruct.equal mac mymac) (Unknown "mac do not match") >>= fun () ->
let dec = Otr_crypto.crypt ~key:c' ~ctr:0L enc_data in
Expand All @@ -162,8 +162,8 @@ let check_sig ctx (ssid, c', m1', m2') (dh_secret, gx) gy signature =
({ ctx with state }, format_ssid ssid high)

let handle_commit_await_key ctx dh_c h version instances buf =
guard (Cstruct.len buf >= 32) (Unknown "underflow") >>= fun () ->
let their_hash = Cstruct.sub buf (Cstruct.len buf - 32) 32 in
guard (Cstruct.length buf >= 32) (Unknown "underflow") >>= fun () ->
let their_hash = Cstruct.sub buf (Cstruct.length buf - 32) 32 in
if Otr_crypto.mpi_gt h their_hash then
Ok (ctx, Some dh_c)
else
Expand Down
4 changes: 2 additions & 2 deletions src/otr_builder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let encode_int data =
buf

let encode_data data =
encode_int (Int32.of_int (Cstruct.len data)) <+> data
encode_int (Int32.of_int (Cstruct.length data)) <+> data

let dh_commit version instances dhshared hashed =
let header = header version instances Otr_packet.DH_COMMIT in
Expand Down Expand Up @@ -89,7 +89,7 @@ let tlv ?data ?predata typ =
| Some x -> x
in
let data = pred <+> data in
Cstruct.BE.set_uint16 buf 2 (Cstruct.len data) ;
Cstruct.BE.set_uint16 buf 2 (Cstruct.length data) ;
buf <+> data
| None ->
Cstruct.BE.set_uint16 buf 2 0 ;
Expand Down
34 changes: 17 additions & 17 deletions src/otr_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ let classify_input bytes =

(* real OTR data parsing *)
let decode_data buf =
guard (len buf >= 4) Underflow >>= fun () ->
guard (length buf >= 4) Underflow >>= fun () ->
let size = BE.get_uint32 buf 0 in
let intsize = Int32.to_int size in
guard (len buf >= 4 + intsize) Underflow >>| fun () ->
guard (length buf >= 4 + intsize) Underflow >>| fun () ->
(sub buf 4 intsize, shift buf (4 + intsize))

let parse_gy data =
decode_data data >>= fun (gy, rst) ->
guard (len rst = 0) Underflow >>= fun () ->
guard (length rst = 0) Underflow >>= fun () ->
guard (get_uint8 gy 0 <> 0) LeadingZero >>| fun () ->
gy

Expand All @@ -170,7 +170,7 @@ let version_of_int = function
| _ -> Error (Unknown "version")

let parse_header bytes =
guard (len bytes >= 3) Underflow >>= fun () ->
guard (length bytes >= 3) Underflow >>= fun () ->
version_of_int (BE.get_uint16 bytes 0) >>= fun version ->
let typ = get_uint8 bytes 2 in
R.of_option
Expand All @@ -179,14 +179,14 @@ let parse_header bytes =
match version with
| `V2 -> Ok (version, typ, None, shift bytes 3)
| `V3 ->
guard (len bytes >= 11) Underflow >>| fun () ->
guard (length bytes >= 11) Underflow >>| fun () ->
let mine = BE.get_uint32 bytes 3
and thei = BE.get_uint32 bytes 7
in
(version, typ, Some (mine, thei), shift bytes 11)

let parse_signature_data buf =
guard (len buf >= 2) Underflow >>= fun () ->
guard (length buf >= 2) Underflow >>= fun () ->
let tag, buf = split buf 2 in
guard (BE.get_uint16 tag 0 = 0) (Unknown "key tag != 0") >>= fun () ->
decode_data buf >>= fun (p, buf) ->
Expand All @@ -199,7 +199,7 @@ let parse_signature_data buf =
guard (get_uint8 y 0 <> 0) LeadingZero >>= fun () ->
R.reword_error (function `Msg m -> Unknown m)
(Otr_crypto.OtrDsa.pub ~p ~q ~gg ~y) >>= fun key ->
guard (len buf = 44) (Unknown "signature lengh") >>| fun () ->
guard (length buf = 44) (Unknown "signature lengh") >>| fun () ->
let keyida = BE.get_uint32 buf 0 in
let buf = shift buf 4 in
let siga = split buf 20 in
Expand All @@ -208,30 +208,30 @@ let parse_signature_data buf =
let parse_reveal buf =
decode_data buf >>= fun (r, buf) ->
decode_data buf >>= fun (enc_data, mac) ->
guard (len mac = 20) (Unknown "wrong mac length") >>| fun () ->
guard (length mac = 20) (Unknown "wrong mac length") >>| fun () ->
(r, enc_data, mac)

let parse_dh_commit buf =
decode_data buf >>= fun (gxenc, buf) ->
decode_data buf >>= fun (hgx, buf) ->
guard ((len buf = 0) && (len hgx = 32)) (Unknown "bad dh_commit") >>| fun () ->
guard ((length buf = 0) && (length hgx = 32)) (Unknown "bad dh_commit") >>| fun () ->
(gxenc, hgx)

let parse_data_body buf =
guard (len buf >= 9) Underflow >>= fun () ->
guard (length buf >= 9) Underflow >>= fun () ->
let flags = get_uint8 buf 0
and s_keyid = BE.get_uint32 buf 1
and r_keyid = BE.get_uint32 buf 5
in
decode_data (shift buf 9) >>= fun (dh_y, buf) ->
guard (get_uint8 dh_y 0 <> 0) LeadingZero >>= fun () ->
guard (len buf >= 8) Underflow >>= fun () ->
guard (length buf >= 8) Underflow >>= fun () ->
let ctr = BE.get_uint64 buf 0 in
decode_data (shift buf 8) >>= fun (encdata, buf) ->
guard (len buf >= 20) Underflow >>= fun () ->
guard (length buf >= 20) Underflow >>= fun () ->
let mac = sub buf 0 20 in
decode_data (shift buf 20) >>= fun (reveal, buf) ->
guard (len buf = 0) Underflow >>| fun () ->
guard (length buf = 0) Underflow >>| fun () ->
let flags = if flags = 1 then true else false in
(flags, s_keyid, r_keyid, dh_y, ctr, encdata, mac, reveal)

Expand All @@ -242,22 +242,22 @@ let parse_data buf =
(version, instances, flags, s_keyid, r_keyid, dh_y, ctr, encdata, mac, reveal)

let parse_tlv buf =
guard (len buf >= 4) Underflow >>= fun () ->
guard (length buf >= 4) Underflow >>= fun () ->
let typ = BE.get_uint16 buf 0 in
let l = BE.get_uint16 buf 2 in
guard (len buf >= 4 + l) Underflow >>| fun () ->
guard (length buf >= 4 + l) Underflow >>| fun () ->
(int_to_tlv_type typ, sub buf 4 l, shift buf (4 + l))

let parse_datas buf n =
let rec p_data buf acc = function
| 0 when len buf = 0 -> Ok (List.rev acc)
| 0 when length buf = 0 -> Ok (List.rev acc)
| 0 -> Error Underflow
| n ->
decode_data buf >>= fun (x, buf) ->
guard (get_uint8 x 0 <> 0) LeadingZero >>= fun () ->
p_data buf (x :: acc) (pred n)
in
guard (len buf >= 4) Underflow >>= fun () ->
guard (length buf >= 4) Underflow >>= fun () ->
let cnt = BE.get_uint32 buf 0 in
if cnt = Int32.of_int n then
p_data (shift buf 4) [] n
Expand Down
2 changes: 1 addition & 1 deletion src/otr_ratchet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let check_keys dh_keys send recv gy =
match
Otr_crypto.check_gy gy,
dh_keys.their_keyid = Int32.succ send,
Cstruct.len dh_keys.previous_gy = 0
Cstruct.length dh_keys.previous_gy = 0
with
| true, _ , _ -> Some "invalid gy"
| _ , true, true -> Some "invalid previous gy"
Expand Down

0 comments on commit e62cd5a

Please sign in to comment.