basic setup of card internal

This commit is contained in:
2024-08-12 10:19:36 +07:00
parent de3b17c7de
commit b78526df9b
11 changed files with 238 additions and 5 deletions

27
smartcard/CardState.ml Normal file
View File

@ -0,0 +1,27 @@
(**
ICAO 9303 communication is like a state machine,
we can make a state machine initialized and for
each subsequent commands, modifying the state.
So actually we can make an OCAML state machine.
*)
type t = {
(* a MF may not exist *)
masterfile : Filesystem.t option;
(* a list of every files in the card *)
allfiles : Filesystem.t list;
(* encryption scheme *)
sm : SecureMessage.t option;
(* currently selected file *)
current_file : Filesystem.t option;
}
let process command _state =
let cm = Command.parse command in
match cm.ins with
| 0x44 -> print_endline "ACTIVATE FILE"
| 0xE2 -> print_endline "APPEND RECORD"
| 0x82 -> print_endline "EXTERNAL / MUTUAL AUTHENTICATE"
| 0x86 -> print_endline "GENERAL AUTHENTICATE 0x86"
| 0x87 -> print_endline "GENERAL AUTHENTICATE 0x87"
| _ -> print_endline "NOT IMPLEMENTED"

13
smartcard/Command.ml Normal file
View File

@ -0,0 +1,13 @@
type t = {
cla : int;
ins : int;
p1 : int;
p2 : int;
}
let parse command = {
cla = Bytes.get command 0 |> Char.code;
ins = Bytes.get command 1 |> Char.code;
p1 = Bytes.get command 2 |> Char.code;
p2 = Bytes.get command 3 |> Char.code;
}

39
smartcard/Filesystem.ml Normal file
View File

@ -0,0 +1,39 @@
type t
= DelicatedFile of {
fid : Bytes.t;
aid : Bytes.t option;
files : t list;
}
| ElementaryFile of {
fid : Bytes.t;
sid : Bytes.t;
data : Bytes.t;
}
let find_by_fid fid files =
let rec eachfile = fun file ->
match file with
| DelicatedFile df -> if Bytes.equal fid df.fid
then true
else List.exists eachfile df.files
| ElementaryFile ef -> Bytes.equal fid ef.fid
in
List.find_opt eachfile files
let find_by_aid aid files =
let rec eachfile = fun file ->
match file with
| ElementaryFile _ -> false
| DelicatedFile df -> match df.aid with
| Some id -> Bytes.equal aid id
| None -> List.exists eachfile df.files
in
List.find_opt eachfile files
let find_by_sid sid files =
let rec eachfile = fun file ->
match file with
| DelicatedFile df -> List.exists eachfile df.files
| ElementaryFile ef -> Bytes.equal sid ef.sid
in
List.find_opt eachfile files

View File

@ -0,0 +1,3 @@
type t
= TripleDES
| AES

2
smartcard/dune Normal file
View File

@ -0,0 +1,2 @@
(library
(name SmartCard))