Skip to content

Commit

Permalink
Merge pull request #43 from mirage/fix-base64
Browse files Browse the repository at this point in the history
Be more strict about padding
  • Loading branch information
dinosaure authored Mar 10, 2020
2 parents ca1db40 + d49eddd commit 6227924
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/base64.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ let unsafe_set_be_uint16 =
can raise and avoid appearance of unknown exceptions like an ex-nihilo
magic rabbit (or magic money?). *)
exception Out_of_bounds
exception Too_much_input

let get_uint8 t off =
if off < 0 || off >= String.length t then raise Out_of_bounds ;
Expand Down Expand Up @@ -153,6 +154,7 @@ let decode_sub ?(pad = true) { dmap; _ } ?(off = 0) ?len input =
let n = (len // 4) * 4 in
let n' = (n // 4) * 3 in
let res = Bytes.create n' in
let invalid_pad_overflow = pad in

let get_uint8_or_padding =
if pad then (fun t i -> if i >= len then raise Out_of_bounds ; get_uint8 t (off + i) )
Expand Down Expand Up @@ -233,18 +235,26 @@ let decode_sub ?(pad = true) { dmap; _ } ?(off = 0) ?len input =
if i + 4 = n
(* end of input in anyway *)
then match pad with
| 0 -> 0
| 4 -> 3
| 0 ->
0
| 4 ->
(* assert (invalid_pad_overflow = false) ; *)
3
(* [get_uint8] lies and if we get [4], that mean we got one or more (at
most 4) padding character. In this situation, because we round length
of [res] (see [n // 4]), we need to delete 3 bytes. *)
| pad -> pad
| pad ->
pad
else match pad with
| 0 -> dec (j + 3) (i + 4)
| 4 -> only_padding 3 (i + 4)
| 4 ->
(* assert (invalid_pad_overflow = false) ; *)
only_padding 3 (i + 4)
(* Same situation than above but we should get only more padding
characters then. *)
| pad -> only_padding pad (i + 4) end in
| pad ->
if invalid_pad_overflow = true then raise Too_much_input ;
only_padding pad (i + 4) end in

match dec 0 0 with
| 0 -> Ok (Bytes.unsafe_to_string res, 0, n')
Expand All @@ -254,6 +264,8 @@ let decode_sub ?(pad = true) { dmap; _ } ?(off = 0) ?len input =
| exception Not_found ->
(* appear when one character of [input] ∉ [alphabet] and this character <> '=' *)
error_msgf "Malformed input"
| exception Too_much_input ->
error_msgf "Too much input"

let decode ?pad ?(alphabet = default_alphabet) ?off ?len input =
match decode_sub ?pad alphabet ?off ?len input with
Expand Down
33 changes: 31 additions & 2 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ let cfcs_tests = [
2, 0, "", "BB";
]

let nocrypto_tests =
[ "\x00\x5a\x6d\x39\x76", None
; "\x5a\x6d\x39\x76", Some "\x66\x6f\x6f"
; "\x5a\x6d\x39\x76\x76", None
; "\x5a\x6d\x39\x76\x76\x76", None
; "\x5a\x6d\x39\x76\x76\x76\x76", None
; "\x5a\x6d\x39\x76\x00", None
; "\x5a\x6d\x39\x76\x62\x77\x3d\x3d", Some "\x66\x6f\x6f\x6f"
; "\x5a\x6d\x39\x76\x62\x77\x3d\x3d\x00", None
; "\x5a\x6d\x39\x76\x62\x77\x3d\x3d\x00\x01", None
; "\x5a\x6d\x39\x76\x62\x77\x3d\x3d\x00\x01\x02", None
; "\x5a\x6d\x39\x76\x62\x77\x3d\x3d\x00\x01\x02\x03", None
; "\x5a\x6d\x39\x76\x62\x32\x38\x3d", Some "\x66\x6f\x6f\x6f\x6f"
; "\x5a\x6d\x39\x76\x62\x32\x39\x76", Some "\x66\x6f\x6f\x6f\x6f\x6f"
; "YWE=", Some "aa"
; "YWE==", None
; "YWE===", None
; "YWE=====", None
; "YWE======", None ]

let alphabet_size () =
List.iter (fun (name,alphabet) ->
Alcotest.(check int) (sprintf "Alphabet size %s = 64" name)
Expand Down Expand Up @@ -118,7 +138,15 @@ let test_php () =
let test_cfcs () =
List.iter (fun (off, len, c,r) ->
Alcotest.(check string) (sprintf "decode %s" r) c (Base64.decode_exn ~pad:false ~off ~len r);
) cfcs_tests
) cfcs_tests

let test_nocrypto () =
List.iter (fun (input, res) ->
let res' = match Base64.decode ~pad:true input with
| Ok v -> Some v
| Error _ -> None in
Alcotest.(check (option string)) (sprintf "decode %S" input) res' res ;
) nocrypto_tests

exception Malformed
exception Wrong_padding
Expand Down Expand Up @@ -217,7 +245,8 @@ let test_codec = [ "RFC4648 test vectors", `Quick, test_rfc4648
; "RFC3548 test vectors", `Quick, test_rfc3548
; "Hannes test vectors", `Quick, test_hannes
; "Cfcs test vectors", `Quick, test_cfcs
; "PHP test vectors", `Quick, test_php ]
; "PHP test vectors", `Quick, test_php
; "Nocrypto test vectors", `Quick, test_nocrypto ]

let () =
Alcotest.run "Base64" [
Expand Down

0 comments on commit 6227924

Please sign in to comment.