From 4817bd316e113d1cb4573491293f10b1705e298c Mon Sep 17 00:00:00 2001 From: Wilmer Ricciotti Date: Mon, 12 Dec 2011 15:27:04 +0000 Subject: [PATCH] Matitaweb: secure SHA-256 encryption for passwords. Includes an utility for converting the user db to the new format. --- matitaB/configure.ac | 1 + matitaB/matita/Makefile | 4 ++ matitaB/matita/matitaAuthentication.ml | 49 +++++++++++++++++++++---- matitaB/matita/matitaAuthentication.mli | 7 +++- matitaB/matita/matitaweb.js | 6 ++- 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/matitaB/configure.ac b/matitaB/configure.ac index 39822d14e..b3a90abc6 100644 --- a/matitaB/configure.ac +++ b/matitaB/configure.ac @@ -97,6 +97,7 @@ $FINDLIB_CREQUIRES \ netcgi2 \ nethttpd \ uuidm \ +cryptokit \ " for r in $FINDLIB_LIBSREQUIRES $FINDLIB_REQUIRES $FINDLIB_WREQUIRES do diff --git a/matitaB/matita/Makefile b/matitaB/matita/Makefile index 7339eaf41..461eb0fb2 100644 --- a/matitaB/matita/Makefile +++ b/matitaB/matita/Makefile @@ -225,6 +225,10 @@ matitadaemon.opt: matitadaemon.ml $(WLIBX_DEPS) $(WCMXS) $(MAINCMXS) $(H)echo " OCAMLOPT $<" $(H)$(OCAMLOPT) $(WPKGS) -linkpkg -o $@ $(WCMXS) $(MAINCMXS) matitadaemon.ml +webdb_convert: webdb_convert.ml $(WLIB_DEPS) $(WCMOS) $(MAINCMOS) + $(H)echo " OCAMLC $<" + $(H)$(OCAMLC) $(WPKGS) -linkpkg -o $@ $(WCMOS) $(MAINCMOS) webdb_convert.ml + rottener: rottener.ml $(CLIB_DEPS) $(CCMOS) $(MAINCMOS) $(H)echo " OCAMLC $<" $(H)$(OCAMLC) $(CPKGS) -package lablgtk2 -linkpkg -o $@ $(CCMOS) $(MAINCMOS) rottener.ml diff --git a/matitaB/matita/matitaAuthentication.ml b/matitaB/matita/matitaAuthentication.ml index 7ae548058..a3704ee4e 100644 --- a/matitaB/matita/matitaAuthentication.ml +++ b/matitaB/matita/matitaAuthentication.ml @@ -27,8 +27,8 @@ type session_id = Uuidm.t type matita_file = MatitaFilesystem.matita_flag * string -(* user table: user id, (password, optional session id) *) -type user = string * (string * session_id option) +(* user table: user id, (salt, encrypted password, optional session id) *) +type user = string * (string * string * session_id option) let user_tbl = (ref [] : user list ref) @@ -38,6 +38,7 @@ type session = session_id * (string * MatitaEngine.status * MatitaEngine.status let session_tbl = (ref [] : session list ref) exception UsernameCollision of string +exception InvalidPassword let lookup_user uid = List.assoc uid !user_tbl @@ -48,10 +49,10 @@ let get_users () = List.map fst !user_tbl let create_session uid = let status = new MatitaEngine.status (Some uid) "cic:/matita" in let history = [status] in - let pw,sid = List.assoc uid !user_tbl in + let salt,pw,sid = List.assoc uid !user_tbl in let clean_utbl = List.remove_assoc uid !user_tbl in let new_session = Uuidm.create `V4 in - user_tbl := (uid,(pw,Some new_session))::clean_utbl; + user_tbl := (uid,(salt,pw,Some new_session))::clean_utbl; let clean_stbl = match sid with | Some sid' -> List.remove_assoc sid' !session_tbl @@ -83,8 +84,8 @@ let set_history sid hist = let logout_user sid = let uid,st,hist = List.assoc sid !session_tbl in - let pw,_ = List.assoc uid !user_tbl in - user_tbl := (uid,(pw,None))::List.remove_assoc uid !user_tbl; + let salt,pw,_ = List.assoc uid !user_tbl in + user_tbl := (uid,(salt,pw,None))::List.remove_assoc uid !user_tbl; session_tbl := List.remove_assoc sid !session_tbl ;; @@ -101,7 +102,7 @@ let config_path () = ;; let serialize () = - let clean_utbl = List.map (fun (uid,(pw,_)) -> uid,(pw,None)) !user_tbl in + let clean_utbl = List.map (fun (uid,(salt,pw,_)) -> uid,(salt,pw,None)) !user_tbl in let utbl_ch = open_out (config_path () ^ "/usertable.dump") in Marshal.to_channel utbl_ch clean_utbl []; close_out utbl_ch; @@ -157,11 +158,43 @@ let add_user uid pw = raise (UsernameCollision uid) with Not_found -> let ft = MatitaFilesystem.checkout uid in - user_tbl := (uid,(pw,None))::!user_tbl; + (* use a 8 byte salt *) + let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in + let sha256 = Cryptokit.Hash.sha256 () in + sha256#add_string (salt ^ pw); + let crypto_pw = sha256#result in + user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl; write_ft uid ft; serialize () ;; +let add_user_no_checkout uid pw = + try + let _ = lookup_user uid in + raise (UsernameCollision uid) + with Not_found -> + (* use a 8 byte salt *) + let salt = Cryptokit.Random.string Cryptokit.Random.secure_rng 8 in + let sha256 = Cryptokit.Hash.sha256 () in + sha256#add_string (salt ^ pw); + let crypto_pw = sha256#result in + sha256#wipe; + user_tbl := (uid,(salt,crypto_pw,None))::!user_tbl; + serialize () +;; + +let check_pw uid pw = + try + let salt,crypto_pw,_ = lookup_user uid in + let sha256 = Cryptokit.Hash.sha256 () in + sha256#add_string (salt ^ pw); + let computed_pw = sha256#result in + sha256#wipe; + if crypto_pw <> computed_pw + then (prerr_endline ("password " ^ pw ^ " incorrect"); raise InvalidPassword) + with Not_found _ -> raise InvalidPassword +;; + let reset () = user_tbl := []; session_tbl := []; diff --git a/matitaB/matita/matitaAuthentication.mli b/matitaB/matita/matitaAuthentication.mli index ccb0af73f..d2b1f5910 100644 --- a/matitaB/matita/matitaAuthentication.mli +++ b/matitaB/matita/matitaAuthentication.mli @@ -26,8 +26,9 @@ type session_id = Uuidm.t exception UsernameCollision of string +exception InvalidPassword -val lookup_user : string -> (string * session_id option) +val lookup_user : string -> (string * string * session_id option) val user_of_session : session_id -> string @@ -62,4 +63,8 @@ val set_file_flag : val add_user : string -> string -> unit +val add_user_no_checkout : string -> string -> unit + +val check_pw : string -> string -> unit + val reset : unit -> unit diff --git a/matitaB/matita/matitaweb.js b/matitaB/matita/matitaweb.js index 343d539e6..6e6b905db 100644 --- a/matitaB/matita/matitaweb.js +++ b/matitaB/matita/matitaweb.js @@ -708,8 +708,10 @@ function get_choice_opts(i) { var desc; if (is_defined(title)) { desc = title; - } else { + } else if (is_defined(href)) { desc = href; + } else { + desc = "Preliminary error"; } res[j] = document.createElement("input"); @@ -815,7 +817,7 @@ function show_failures() { var choicedesc = choice.getAttribute("desc"); var title = "

Disambiguation failed

"; - var subtitle = "

Errors at node " + choiceno + " = " + choicedesc + "

"; + var subtitle = "

Errors at node " + cpno + " = " + choicedesc + "

"; disambcell.innerHTML = title + subtitle; var failures = get_failure_opts(cpno,choiceno); -- 2.39.2