text
stringlengths 12
786k
|
---|
let ( ) = match Bool . get ( ) with | false -> ( ) ; ; [ %% expect { | ^^^^^ ' a -> Bool . t } ] | |
let ( ) = match List . get ( ) with | [ ] -> ( ) ; ; [ %% expect { | ^^ ' a -> ' b List . t } ] | |
let ( ) = match List . get ( ) with | _ :: _ -> ( ) ; ; [ %% expect { | ^^^^^^ ' a -> ' b List . t } ] | |
let ( ) = match Unit . get ( ) with | ( ) -> ( ) ; ; [ %% expect { | ^^ ' a -> Unit . t } ] | |
let ( ) = Constr . put A ; ; [ %% expect { | ^ unit -> Constr . t } ] | |
let ( ) = Record . put { a = 0 ; b = 0 ; c = 0 } ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^ unit -> Record . t } ] | |
let ( ) = Bool . put true ; ; [ %% expect { | ^^^^ unit -> Bool . t } ] | |
let ( ) = Bool . put false ; ; [ %% expect { | ^^^^^ unit -> Bool . t } ] | |
let ( ) = List . put [ ] ; ; [ %% expect { | ^^ unit -> int List . t } ] | |
let ( ) = List . put ( 1 :: 2 ) ; ; [ %% expect { | ^^^^^^^^ unit -> int List . t } ] | |
let ( ) = Unit . put ( ) ; ; [ %% expect { | ^^ unit -> Unit . t } ] | |
let ( ) = ignore ( ( Record . get ( ) ) . a ) ; ; [ %% expect { | ^^^^^^^^^^^^^^^ } ] | |
let ( ) = ( Record . get ( ) ) . a <- 5 ; ; [ %% expect { | ^^^^^^^^^^^^^^^ } ] | |
let ( ) = ignore { ( Record . get ( ) ) with a = 5 } ; ; [ %% expect { | ^^^^^^^^^^^^^^^ } ] | |
let foo x = Record . put { x with a = 5 } ; ; [ %% expect { | ^^^^^^^^^^^^^^^^ unit -> Record . t } ] | |
let section = Lwt_log . Section . make " wscat " |
let client uri = let open Websocket in Resolver_lwt . resolve_uri ~ uri Resolver_lwt_unix . system >>= fun endp -> let ctx = Lazy . force Conduit_lwt_unix . default_ctx in Conduit_lwt_unix . endp_to_client ~ ctx endp >>= fun client -> connect ~ ctx client uri >>= fun conn -> let close_sent = ref false in let rec react ( ) = Websocket_lwt_unix . read conn >>= function | { Frame . opcode = Ping ; _ } -> write conn ( Frame . create ~ opcode : Pong ( ) ) >>= react | { opcode = Close ; content ; _ } -> ( if ! close_sent then Lwt . return_unit else if String . length content >= 2 then write conn ( Frame . create ~ opcode : Close ~ content ( : String . sub content 0 2 ) ( ) ) else write conn ( Frame . close 1000 ) ) >>= fun ( ) -> Websocket_lwt_unix . close_transport conn | { opcode = Pong ; _ } -> react ( ) | { opcode = Text ; content ; _ } | { opcode = Binary ; content ; _ } -> Lwt_io . printf " > % s \ n > " %! content >>= react | _ -> Websocket_lwt_unix . close_transport conn in let rec pushf ( ) = Lwt_io . ( read_line_opt stdin ) >>= function | None -> Lwt_log . debug ~ section " Got EOF . Sending a close frame . " >>= fun ( ) -> write conn ( Frame . create ~ opcode : Close ( ) ) >>= fun ( ) -> close_sent := true ; pushf ( ) | Some content -> write conn ( Frame . create ~ content ( ) ) >>= pushf in pushf ( ) <?> react ( ) |
let rec react client client_id = let open Websocket in Connected_client . recv client >>= fun fr -> Lwt_log . debug_f ~ section " Client % d : % S " client_id Frame . ( show fr ) >>= fun ( ) -> match fr . opcode with | Frame . Opcode . Ping -> Connected_client . send client Frame . ( create ~ opcode : Opcode . Pong ~ content : fr . content ( ) ) >>= fun ( ) -> react client client_id | Close -> if String . length fr . content >= 2 then let content = String . sub fr . content 0 2 in Connected_client . send client Frame . ( create ~ opcode : Opcode . Close ~ content ( ) ) else Connected_client . send client @@ Frame . close 1000 | Pong -> react client client_id | Text | Binary -> Connected_client . send client fr >>= fun ( ) -> react client client_id | _ -> Connected_client . send client Frame . ( close 1002 ) |
let server uri = let id = ref ( - 1 ) in let echo_fun client = incr id ; let id = ! id in Lwt_log . info_f ~ section " Connection from client id % d " id >>= fun ( ) -> Lwt . catch ( fun ( ) -> react client id ) ( fun exn -> Lwt_log . error_f ~ section ~ exn " Client % d error " id >>= fun ( ) -> Lwt . fail exn ) in Resolver_lwt . resolve_uri ~ uri Resolver_lwt_unix . system >>= fun endp -> let open Conduit_lwt_unix in let endp_str = endp |> Conduit . sexp_of_endp |> Sexplib . Sexp . to_string_hum in Lwt_log . info_f ~ section " endp = % s " endp_str >>= fun ( ) -> let ctx = Lazy . force default_ctx in endp_to_server ~ ctx endp >>= fun server -> let server_str = server |> sexp_of_server |> Sexplib . Sexp . to_string_hum in Lwt_log . info_f ~ section " server = % s " server_str >>= fun ( ) -> establish_server ~ ctx ~ mode : server echo_fun |
let main is_server uri = if ! is_server then ( ignore @@ server uri ; fst @@ Lwt . wait ( ) ) else client uri |
let apply_loglevel = function | 2 -> Lwt_log . ( add_rule " " * Info ) | 3 -> Lwt_log . ( add_rule " " * Debug ) | _ -> ( ) |
let ( ) = let uri = ref " " in let server = ref false in let speclist = Arg . align [ ( " - s " , Arg . Set server , " Run as server " ) ; ( " - loglevel " , Arg . Int apply_loglevel , " 1 - 3 Set loglevel " ) ] in let anon_fun s = uri := s in let usage_msg = " Usage : " ^ Sys . argv . ( 0 ) ^ " < options > uri \ nOptions are " : in Arg . parse speclist anon_fun usage_msg ; Lwt_main . run ( main server ( Uri . of_string ! uri ) ) |
module LwtWsIteratee = Wslib . Websockets . Wsprotocol ( Lwt ) |
let start handler = Logs_lwt . info ( fun m -> m " Starting wsproxy " ) >>= fun ( ) -> let fd_sock = Lwt_unix . stdin in let ( ) = Lwt_unix . listen fd_sock 5 in let rec loop ( ) = let ensure_close = function | [ ] -> Lwt . return_unit | fds -> Logs_lwt . warn ( fun m -> m " Closing % d excess fds " ( List . length fds ) ) >>= fun ( ) -> List . iter ( fun fd -> try Unix . close fd with _ -> ( ) ) fds ; Lwt . return_unit in Lwt . catch ( fun ( ) -> Lwt_unix . accept fd_sock >>= fun ( fd_sock ' , _ ) -> let ( _ : unit Lwt . t ) = let buffer = Bytes . make 16384 ' \ 000 ' in with_fd fd_sock ' ~ callback ( : fun fd -> let io_vectors = Lwt_unix . IO_vectors . create ( ) in Lwt_unix . IO_vectors . append_bytes io_vectors buffer 0 16384 ; Lwt_unix . Versioned . recv_msg_2 ~ socket : fd ~ io_vectors ) >>= fun ( len , newfds ) -> match newfds with | [ ] -> Logs_lwt . warn ( fun m -> m " No fd to start a connection : not proxying " ) | ufd :: ufds -> ensure_close ufds >>= fun ( ) -> with_fd ( Lwt_unix . of_unix_file_descr ufd ) ~ callback ( : fun fd -> Logs_lwt . debug ( fun m -> m " About to start connection " ) >>= fun ( ) -> Lwt_unix . setsockopt fd Lwt_unix . SO_KEEPALIVE true ; let msg = Bytes . ( to_string @@ sub buffer 0 len ) in handler fd msg ) in loop ( ) ) ( fun e -> Logs_lwt . err ( fun m -> m " Caught exception : % s " ( Printexc . to_string e ) ) >>= fun ( ) -> Lwt . return_unit ) >>= fun ( ) -> loop ( ) in with_fd fd_sock ~ callback ( : fun _ -> loop ( ) ) |
let proxy ( fd : Lwt_unix . file_descr ) addr protocol = let open LwtWsIteratee in let open Lwt_support in ( match protocol with | " hixie76 " -> Logs_lwt . debug ( fun m -> m " Old - style ( hixie76 ) protocol " ) >>= fun ( ) -> Lwt . return ( wsframe_old , wsunframe_old ) | " hybi10 " -> Logs_lwt . debug ( fun m -> m " New - style ( hybi10 ) protocol " ) >>= fun ( ) -> Lwt . return ( wsframe , wsunframe ) | _ -> Logs_lwt . warn ( fun m -> m " Unknown protocol , fallback to hybi10 " ) >>= fun ( ) -> Lwt . return ( wsframe , wsunframe ) ) >>= fun ( frame , unframe ) -> with_open_connection_fd addr ~ callback ( : fun localfd -> let session_id = Uuidm . v ` V4 |> Uuidm . to_string in Logs_lwt . debug ( fun m -> m " Starting proxy session % s " session_id ) >>= fun ( ) -> let thread1 = lwt_fd_enumerator localfd ( frame ( writer ( really_write fd ) " thread1 " ) ) >>= fun _ -> Lwt . return_unit in let thread2 = lwt_fd_enumerator fd ( unframe ( writer ( really_write localfd ) " thread2 " ) ) >>= fun _ -> Lwt . return_unit in Lwt . choose [ thread1 ; thread2 ] >>= fun ( ) -> Logs_lwt . debug ( fun m -> m " Closing proxy session % s " session_id ) ) |
module RX = struct let socket = Re . Str . regexp " ^/ var / run / xen / vnc [ - 0 - 9 ] " +$ let port = Re . Str . regexp " [ ^ 0 - 9 ] " +$ end |
let handler sock msg = Logs_lwt . debug ( fun m -> m " Got msg : ' % s ' " msg ) >>= fun ( ) -> match Re . Str . ( split @@ regexp " [ ] " ) : msg with | ( [ protocol ; _ ; path ] | [ protocol ; path ] ) when Re . Str . string_match RX . socket path 0 -> let addr = Unix . ADDR_UNIX path in proxy sock addr protocol | ( [ protocol ; _ ; sport ] | [ protocol ; sport ] ) when Re . Str . string_match RX . port sport 0 -> let localhost = Unix . inet_addr_loopback in let addr = Unix . ADDR_INET ( localhost , int_of_string sport ) in proxy sock addr protocol | _ -> Logs_lwt . warn ( fun m -> m " The message ' % s ' is malformed : not proxying " msg ) |
let lwt_reporter ( ) = let buf_fmt ~ like = let b = Buffer . create 512 in ( Fmt . with_buffer ~ like b , fun ( ) -> let m = Buffer . contents b in Buffer . reset b ; m ) in let app , app_flush = buf_fmt ~ like : Fmt . stdout in let dst , dst_flush = buf_fmt ~ like : Fmt . stderr in let reporter = Logs_fmt . reporter ~ app ~ dst ( ) in let report src level ~ over k msgf = let k ( ) = let write ( ) = match level with | Logs . App -> Lwt_io . write Lwt_io . stdout ( app_flush ( ) ) | _ -> Lwt_io . write Lwt_io . stderr ( dst_flush ( ) ) in let unblock ( ) = over ( ) ; Lwt . return_unit in Lwt . finalize write unblock |> Lwt . ignore_result ; k ( ) in reporter . Logs . report src level ~ over ( : fun ( ) -> ( ) ) k msgf in { Logs . report } |
let _ = Logs . set_reporter ( lwt_reporter ( ) ) ; Logs . set_level ~ all : true ( Some Logs . Info ) ; Lwt_main . run ( start handler ) |
let src = Logs . Src . create " fastws . async . wstest " |
module Log = ( val Logs . src_log src : Logs . LOG ) |
module Log_async = ( val Logs_async . src_log src : Logs_async . LOG ) |
let url_of_test url prefix i = let open Uri in with_query ' ( with_path url " runCase " ) [ ( " casetuple " , prefix ^ " . " ^ string_of_int i ) ; ( " agent " , " fastws " ) ] |
let run_test url section j = Async_uri . with_connection url ( fun { r ; w ; _ } -> Fastws_async . with_connection ( url_of_test url section j ) r w Fn . id Fn . id ( fun r w -> Pipe . iter r ~ f ( : fun ( fr : Frame . t ) -> Pipe . write w fr ) ) ) |
let main url section tests = Random . self_init ( ) ; Deferred . Or_error . List . iter ( List . concat tests ) ~ f ( : fun x -> Fastws_async . Raw . to_or_error ( run_test url section x ) ) |
let url_cmd = Command . Arg_type . create Uri . of_string |
let pp_header ppf ( l , _ ) = let now = Time_ns . now ( ) in Format . fprintf ppf " % a [ % a ] " Time_ns . pp now Logs . pp_level l |
let range_of_string s = match String . rsplit2 s ~ on ' ' :- with | None -> [ Int . of_string s ] | Some ( a , b ) -> let a = Int . of_string a in let b = Int . of_string b in List . range ~ start ` : inclusive ~ stop ` : inclusive a b |
let range = let open Command . Arg_type in comma_separated ( map Export . string ~ f : range_of_string ) |
let ( ) = Command . async_or_error ~ summary " : Autobahn test client " ( let open Command . Let_syntax in [ % map_open let ( ) = Logs_async_reporter . set_level_via_param [ ] and url = anon ( " url " %: url_cmd ) and section = anon ( " section " %: string ) and tests = anon ( " tests " %: range ) in fun ( ) -> Logs . set_reporter ( Logs_async_reporter . reporter ( ) ) ; main url section tests ] ) |> Command_unix . run |
module type S = sig type ' a t val create : unit -> ' a t val push : ' a t -> ' a -> unit val pop : ' a t -> ' a val steal : ' a t -> ' a end |
module CArray = struct type ' a t = ' a array let rec log2 n = if n <= 1 then 0 else 1 + ( log2 ( n asr 1 ) ) let create sz v = assert ( 0 < sz && sz = Int . shift_left 1 ( log2 sz ) ) ; assert ( Int . logand sz ( sz - 1 ) == 0 ) ; Array . make sz v let size t = Array . length t [ @@ inline ] let mask t = size t - 1 [ @@ inline ] let index i t = Int . logand i ( mask t ) [ @@ inline ] let get t i = Array . unsafe_get t ( index i t ) [ @@ inline ] let put t i v = Array . unsafe_set t ( index i t ) v [ @@ inline ] let transfer src dst top num = ArrayExtra . blit_circularly src ( index top src ) dst ( index top dst ) num [ @@ inline ] let grow t top bottom = let sz = size t in assert ( bottom - top = sz ) ; let dst = create ( 2 * sz ) ( Obj . magic ( ) ) in transfer t dst top sz ; dst let shrink t top bottom = let sz = size t in assert ( bottom - top <= sz / 2 ) ; let dst = create ( sz / 2 ) ( Obj . magic ( ) ) in transfer t dst top ( bottom - top ) ; dst end |
module M : S = struct let min_size = 32 let shrink_const = 3 type ' a t = { top : int Atomic . t ; bottom : int Atomic . t ; tab : ' a ref CArray . t Atomic . t ; mutable next_shrink : int ; } let create ( ) = { top = Atomic . make 1 ; bottom = Atomic . make 1 ; tab = Atomic . make ( CArray . create min_size ( Obj . magic ( ) ) ) ; next_shrink = 0 } let set_next_shrink q = let sz = CArray . size ( Atomic . get q . tab ) in if sz <= min_size then q . next_shrink <- 0 else q . next_shrink <- sz / shrink_const let grow q t b = Atomic . set q . tab ( CArray . grow ( Atomic . get q . tab ) t b ) ; set_next_shrink q let size q = let b = Atomic . get q . bottom in let t = Atomic . get q . top in b - t let push q v = let v ' = ref v in let b = Atomic . get q . bottom in let t = Atomic . get q . top in let a = Atomic . get q . tab in let size = b - t in let a = if size = CArray . size a then ( grow q t b ; Atomic . get q . tab ) else a in CArray . put a b v ' ; Atomic . set q . bottom ( b + 1 ) let release ptr = let res = ! ptr in ptr := Obj . magic ( ) ; res [ @@ inline ] let pop q = if size q = 0 then raise Exit else begin let b = ( Atomic . get q . bottom ) - 1 in Atomic . set q . bottom b ; let t = Atomic . get q . top in let a = Atomic . get q . tab in let size = b - t in if size < 0 then begin Atomic . set q . bottom ( b + 1 ) ; raise Exit end else let out = CArray . get a b in if b = t then begin if ( Atomic . compare_and_set q . top t ( t + 1 ) ) then ( Atomic . set q . bottom ( b + 1 ) ; release out ) else ( Atomic . set q . bottom ( b + 1 ) ; raise Exit ) end else begin if q . next_shrink > size then begin Atomic . set q . tab ( CArray . shrink a t b ) ; set_next_shrink q end ; release out end end let rec steal q = let t = Atomic . get q . top in let b = Atomic . get q . bottom in let size = b - t in if size <= 0 then raise Exit else let a = Atomic . get q . tab in let out = CArray . get a t in if Atomic . compare_and_set q . top t ( t + 1 ) then release out else begin Domain . cpu_relax ( ) ; steal q end end |
type codepoint = | Point of int | Malformed |
type ' a folder = ' a -> int -> codepoint -> ' a |
let needed_bytes c = if 0x00 <= c && c <= 0x7F then 1 else if 0xC2 <= c && c <= 0xDF then 2 else if 0xE0 <= c && c <= 0xEF then 3 else if 0xF0 <= c && c <= 0xF4 then 4 else 0 |
let unsafe_char s i = Char . code ( Bytes . unsafe_get s i ) |
let codepoint s i = function | 1 -> unsafe_char s i | 2 -> let b0 = unsafe_char s i in let b1 = unsafe_char s ( i + 1 ) in ( ( b0 land 0x1F ) lsl 6 ) lor ( b1 land 0x3F ) | 3 -> let b0 = unsafe_char s ( i ) in let b1 = unsafe_char s ( i + 1 ) in let b2 = unsafe_char s ( i + 2 ) in ( ( b0 land 0x0F ) lsl 12 ) lor ( ( b1 land 0x3F ) lsl 6 ) lor ( b2 land 0x3F ) | 4 -> let b0 = unsafe_char s ( i ) in let b1 = unsafe_char s ( i + 1 ) in let b2 = unsafe_char s ( i + 2 ) in let b3 = unsafe_char s ( i + 3 ) in ( ( b0 land 0x07 ) lsl 18 ) lor ( ( b1 land 0x3F ) lsl 12 ) lor ( ( b2 land 0x3F ) lsl 6 ) lor ( b3 land 0x3F ) | _ -> assert false |
let fold_wtf_8 ( ? pos = 0 ) ? len f acc s = let rec loop acc f s i l = if i = l then acc else let need = needed_bytes ( unsafe_char s i ) in if need = 0 then ( loop [ @ tailcall ] ) ( f acc i Malformed ) f s ( i + 1 ) l else let rem = l - i in if rem < need then f acc i Malformed else ( loop [ @ tailcall ] ) ( f acc i ( Point ( codepoint s i need ) ) ) f s ( i + need ) l in let len = match len with | None -> String . length s - pos | Some l -> l in loop acc f ( Bytes . unsafe_of_string s ) pos len |
let add_wtf_8 buf code = let [ @ inline ] w byte = Buffer . add_char buf ( Char . unsafe_chr byte ) in if code >= 0x10000 then begin w ( 0xf0 lor ( code lsr 18 ) ) ; w ( 0x80 lor ( ( code lsr 12 ) land 0x3F ) ) ; w ( 0x80 lor ( ( code lsr 6 ) land 0x3F ) ) ; w ( 0x80 lor ( code land 0x3F ) ) end else if code >= 0x800 then begin w ( 0xe0 lor ( code lsr 12 ) ) ; w ( 0x80 lor ( ( code lsr 6 ) land 0x3F ) ) ; w ( 0x80 lor ( code land 0x3F ) ) end else if code >= 0x80 then begin w ( 0xc0 lor ( code lsr 6 ) ) ; w ( 0x80 lor ( code land 0x3F ) ) end else w code |
type json = Yojson . Safe . t [ @@ deriving of_yojson ] |
type hex = string [ @@ deriving eq ] |
let pp_hex fmt s = let ( ` Hex h ) = Hex . of_string s in Format . pp_print_string fmt h |
let hex_of_yojson json = let padded s = if String . length s mod 2 = 0 then s else " 0 " ^ s in match [ % of_yojson : string ] json with | Ok s -> Ok ( Hex . to_string ( ` Hex ( padded s ) ) ) | Error _ as e -> e |
type test_result = Valid | Acceptable | Invalid [ @@ deriving show ] |
let test_result_of_yojson = function | ` String " valid " -> Ok Valid | ` String " acceptable " -> Ok Acceptable | ` String " invalid " -> Ok Invalid | _ -> Error " test_result " |
type ecdh_test = { tcId : int ; comment : string ; curve : json option ; [ @ yojson . default None ] public : hex ; private_ : hex ; [ @ yojson . key " private " ] shared : hex ; result : test_result ; flags : string list ; } |
let has_ignored_flag test ~ ignored_flags = List . exists ( fun ignored_flag -> List . mem ignored_flag test . flags ) ignored_flags |
type ecdh_test_group = { curve : string ; tests : ecdh_test list ; encoding : json option ; [ @ yojson . default None ] type_ : json option ; [ @ yojson . default None ] [ @ yojson . key " type " ] } |
type ecdsa_key = { curve : string ; keySize : int ; type_ : json ; [ @ yojson . key " type " ] uncompressed : hex ; wx : hex ; wy : hex ; } |
type dsa_test = { tcId : int ; comment : string ; msg : hex ; sig_ : hex ; [ @ yojson . key " sig " ] result : test_result ; flags : string list ; } |
type ecdsa_test_group = { key : ecdsa_key ; keyDer : string ; keyPem : string ; sha : string ; tests : dsa_test list ; type_ : json option ; [ @ yojson . default None ] [ @ yojson . key " type " ] } |
type eddsa_key = { curve : string ; keySize : int ; pk : hex ; sk : hex ; type_ : json ; [ @ yojson . key " type " ] } |
type eddsa_test_group = { jwk : json ; key : eddsa_key ; keyDer : string ; keyPem : string ; type_ : json ; [ @ yojson . key " type " ] tests : dsa_test list ; } |
type test_file = { algorithm : json ; generatorVersion : json ; header : json ; notes : json ; numberOfTests : json ; schema : json ; testGroups : json list ; } |
let get_json = function Ok x -> x | Error s -> failwith s |
let load_file_exn path = Yojson . Safe . from_file path |> [ % of_yojson : test_file ] |> get_json |
let ecdh_test_group_exn json = [ % of_yojson : ecdh_test_group ] json |> get_json |
let ecdsa_test_group_exn json = [ % of_yojson : ecdsa_test_group ] json |> get_json |
let eddsa_test_group_exn json = [ % of_yojson : eddsa_test_group ] json |> get_json |
type tBSCertificate = { version : [ ` V1 | ` V2 | ` V3 ] ; serial : Z . t ; signature : OID . t ; issuer : ( OID . t * string ) list list ; validity : Ptime . t * Ptime . t ; subject : ( OID . t * string ) list list ; pk_info : OID . t * Cstruct . t ; issuer_id : Cstruct . t option ; subject_id : Cstruct . t option ; extensions : ( OID . t * bool * Cstruct . t ) list option } |
type certificate = { tbs_cert : tBSCertificate ; signature_algo : OID . t ; signature : Cstruct . t } |
let def x = function None -> x | Some y -> y |
let def ' x = fun y -> if y = x then None else Some y |
let extensions = let extension = map ( fun ( oid , b , v ) -> ( oid , def false b , v ) ) ( fun ( oid , b , v ) -> ( oid , def ' false b , v ) ) @@ sequence3 ( required ~ label " : id " oid ) ( optional ~ label " : critical " bool ) ( required ~ label " : value " octet_string ) in sequence_of extension |
let directory_name = map ( function | ` C1 s -> s | ` C2 s -> s | ` C3 s -> s | ` C4 s -> s | ` C5 s -> s | ` C6 s -> s ) ( function s -> ` C1 s ) @@ choice6 printable_string utf8_string teletex_string universal_string bmp_string ia5_string |
let name = let attribute_tv = sequence2 ( required ~ label " : attr type " oid ) ( required ~ label " : attr value " directory_name ) in let rd_name = set_of attribute_tv in let rdn_sequence = sequence_of rd_name in rdn_sequence |
let algorithmIdentifier = map ( fun ( oid , _ ) -> oid ) ( fun oid -> ( oid , None ) ) @@ sequence2 ( required ~ label " : algorithm " oid ) ( optional ~ label " : params " null ) |
let version = map ( function 2 -> ` V2 | 3 -> ` V3 | _ -> ` V1 ) ( function ` V2 -> 2 | ` V3 -> 3 | _ -> 1 ) int |
let time = map ( function ` C1 t -> t | ` C2 t -> t ) ( fun t -> ` C2 t ) ( choice2 utc_time generalized_time ) |
let validity = sequence2 ( required ~ label " : not before " time ) ( required ~ label " : not after " time ) |
let subjectPublicKeyInfo = sequence2 ( required ~ label " : algorithm " algorithmIdentifier ) ( required ~ label " : subjectPK " bit_string_cs ) |
let tBSCertificate = let f = fun ( a , ( b , ( c , ( d , ( e , ( f , ( g , ( h , ( i , j ) ) ) ) ) ) ) ) ) -> { version = def ` V1 a ; serial = b ; signature = c ; issuer = d ; validity = e ; subject = f ; pk_info = g ; issuer_id = h ; subject_id = i ; extensions = j } and g = fun { version = a ; serial = b ; signature = c ; issuer = d ; validity = e ; subject = f ; pk_info = g ; issuer_id = h ; subject_id = i ; extensions = j } -> ( def ' ` V1 a , ( b , ( c , ( d , ( e , ( f , ( g , ( h , ( i , j ) ) ) ) ) ) ) ) ) in map f g @@ sequence @@ ( optional ~ label " : version " @@ explicit 0 version ) @ ( required ~ label " : serialNumber " @@ certificateSerialNumber ) @ ( required ~ label " : signature " @@ algorithmIdentifier ) @ ( required ~ label " : issuer " @@ name ) @ ( required ~ label " : validity " @@ validity ) @ ( required ~ label " : subject " @@ name ) @ ( required ~ label " : subjectPKInfo " @@ subjectPublicKeyInfo ) @ ( optional ~ label " : issuerUID " @@ implicit 1 uniqueIdentifier ) @ ( optional ~ label " : subjectUID " @@ implicit 2 uniqueIdentifier ) -@ ( optional ~ label " : extensions " @@ explicit 3 extensions ) |
let certificate = let f ( a , b , c ) = { tbs_cert = a ; signature_algo = b ; signature = c } and g { tbs_cert = a ; signature_algo = b ; signature = c } = ( a , b , c ) in map f g @@ sequence3 ( required ~ label " : tbsCertificate " tBSCertificate ) ( required ~ label " : signatureAlgorithm " algorithmIdentifier ) ( required ~ label " : signatureValue " bit_string_cs ) |
let cert_ber , cert_der = ( codec ber certificate , codec der certificate ) |
let examples = [ " 3082062030820408a003020102 0203020acd300d06092a864886 f70d0101050500305431143012 060355040a130b434163657274 20496e632e311e301c06035504 0b1315687474703a2f2f777777 2e4341636572742e6f7267311c 301a0603550403131343416365 727420436c617373203320526f 6f74301e170d31333039303431 35343333395a170d3135303930 343135343333395a3069310b30 09060355040613024445311030 0e0603550408130748616d6275 72673110300e06035504071307 48616d627572673121301f0603 55040a13184368616f7320436f 6d707574657220436c75622065 2e562e31133011060355040313 0a7777772e6363632e64653082 0222300d06092a864886f70d01 010105000382020f003082020a 0282020100b867422b9bec4548 8639b5e86d7b8848beec017328 59f6e96a2e63846237169fa933 61a5294bb9bffb936f64195ba0 c75a81d6d900207c447baa466d b5aa2ddb758207388b1f74499e c070bbc09b5076c2c8d7296362 0416d7e6aa47abac84aee26b17 596853a90cb27c5a57f066f6ed 3b29d878e9a1e7e3199d953fe0 bd364b24af59e9ff87c4ded1f3 6598747680d95ccfb52090e7cf 517cf450edd8364259837019bc c336eaafebb4f5fed770981c05 88fa44de4d425f78bc12bcfab8 cade0a8a2d09d83401490dc143 bd813c6054c5b009e98d382544 f948a9fe77403e134274cea8ba 851e5037b00a3a075226676aea acc9064e5987c1b96534af2e91 96442462a40a02b66aabe22a9d b1b6af1babec2c5556fc53725e 1659f4a810a7a4b1b2deca5bef 1cd986f10a0f3945768f708f5d cd0ce3974f9ea0c27961b14304 28de8fefabd4f21658c2d02d60 037360cef1b9939ae33be90671 4094a1cc4c0d3edac2426dfdc3 5af18e79ea94fec4e014755809 05313ff6e22482f9b0eb3f30fb f0d9df5954e3bb44095a559f39 ef054ed3452e17aa2fc877c683 2cde17dc56f5306f125f839307 d2f01d706efacd691bd131ff89 f5ec89d7502cb5c7dfc1d15530 c32e7df56eddfe341d264c8291 75957f729f4175b44af54da20d 60bdadde11ce0c0a970713b9e1 9886432a9471a188c58ca5fb45 5b1e821b57d900a34b99020301 0001a381e53081e2300c060355 1d130101ff04023000300e0603 551d0f0101ff0404030203a830 340603551d25042d302b06082b 0601050507030206082b060105 0507030106096086480186f842 0401060a2b0601040182370a03 03303306082b06010505070101 04273025302306082b06010505 0730018617687474703a2f2f6f 6373702e6361636572742e6f72 672f30380603551d1f0431302f 302da02ba0298627687474703a 2f2f63726c2e6361636572742e 6f72672f636c617373332d7265 766f6b652e63726c301d060355 1d1104163014820a7777772e63 63632e646582066363632e6465 300d06092a864886f70d010105 0500038202010085e6700b89e3 7917899556187be487bed8e5ce 563eaaf527c01e8808cf3dbd8a 24b8a7a83d075a460e4e97df44 261f3a9c3986d5903bda265a31 e152b7fef5cb951605770417d8 c53d6b238592bc166523ea43c4 2689bfc02e36e66320999d2807 13117e7209f03668c2d84f610b 0c1e0d53281fa03bbe46b5c378 e3a4f7ecf46b4e2414be366f71 5881b37f9a0e9262e48dd8fc4b 18bbfb710418f9564ffa692419 ab9d2a029c5895a48f46ad7120 3cdfbf47739308b1b6336fe579 4bf1f5bdecc141ddc22e9052af 87427dbebb2e3b16372d771f4a cdeec993d1e7081437ae67f9d3 f8ebfab96d15571337323e16be 879c65b34278314e75e54222cc 089bdbb3fe3dc7ef793c7e994d acb58712cc97504d6fc2740758 3f1853f9bd635c2d792fa63fc0 0a20e4083a57106b3ed90d39a4 a9d4e3680fab5bc0917afc11bc 85114d189d893a82184f5832c9 72af0d67911c6c5917f77e1c54 a3fe7cb8d1b675e9d327711f9d 21eb321652ea11397525e74fa3 3b6cb24480f180a0ae3a7e9897 d73b9834bda98d4cdcf226bc77 2f94c9603db78aed0eb23b4cf0 d80a9b35953b09e9b9235f42ab b4ee46e35400405a403b4bedf6 4c0a884400e83c314eaba4f031 1dae70b25e33d0475661b8acee 57425f8d39858fa9bafb5443fa 030e303bee0843be66e2f46957 68d5a7f5114de83c8c7e0b543b d905b092eec7229685bf4ffe " ; " 308204763082035ea003020102 020843b5eedccc2793ee300d06 092a864886f70d010105050030 49310b30090603550406130255 5331133011060355040a130a47 6f6f676c6520496e6331253023 0603550403131c476f6f676c65 20496e7465726e657420417574 686f72697479204732301e170d 3134303132393134303533375a 170d3134303532393030303030 305a3068310b30090603550406 13025553311330110603550408 0c0a43616c69666f726e696131 16301406035504070c0d4d6f75 6e7461696e2056696577311330 11060355040a0c0a476f6f676c 6520496e633117301506035504 030c0e7777772e676f6f676c65 2e636f6d30820122300d06092a 864886f70d0101010500038201 0f003082010a0282010100a478 79a679863bb8c311c4a835e0d3 f1f3316d0ff566508d9be05750 6200fc02e4627c0f9faafc6270 4922ed37754ab678ce57670236 c04be7c2d1e4238bc7e8253a2c ae45e0420bf976cd3ef2553776 8a155e8a9e99e24a52287323f8 7eedc7f5dbceffec46cc23945a 0c150f4c79991de0ed937f1751 8b01ad2f779c80aae150d4031c b604ab06492da5f7046f9787e1 7430e682e4397110ca9ffa6a75 812a02ac455448da9b08dc5164 81b1696a4a7dfb7c8f6cfcc643 0b37ccc33e8085e14cad134bd2 8276637715741c620d576a8c64 be006e6a214cff02cbc734bdc9 12c6b9e4e4ab305b9b08f0b360 330054b2b38aa657e46db97347 bfaa1d1b48ae3f0203010001a3 8201413082013d301d0603551d 250416301406082b0601050507 030106082b0601050507030230 190603551d1104123010820e77 77772e676f6f676c652e636f6d 306806082b0601050507010104 5c305a302b06082b0601050507 3002861f687474703a2f2f706b 692e676f6f676c652e636f6d2f 47494147322e637274302b0608 2b06010505073001861f687474 703a2f2f636c69656e7473312e 676f6f676c652e636f6d2f6f63 7370301d0603551d0e04160414 7520ead1f9b9b734d5e9e4358a aee864c6732ba4300c0603551d 130101ff04023000301f060355 1d230418301680144add06161b bcf668b576f581b6bb621aba5a 812f30170603551d200410300e 300c060a2b06010401d6790205 0130300603551d1f0429302730 25a023a021861f687474703a2f 2f706b692e676f6f676c652e63 6f6d2f47494147322e63726c30 0d06092a864886f70d01010505 0003820101003a8fda0f284e64 fc55f9b1b2d8e29ef1b2796d9d d1c3375a32ce66fcf9c9a47ba5 bf7851ec63483ecd4794056df3 6f410c06735758d4c207569521 c4467bc1940c30270334973100 5e062b0d6faf649f6ba7b52ed1 6e52fcdfef07efced1b0b797b9 c6a1af7902a1ceb5a137a62341 c4238dce0ed548b851033490c4 d70aac1e475979c9cd4b6f4867 24a92b6b24af7ac7eea5246cfd 659336c5bec9c5532a770094b8 89bf7ee313ebeb91907d48bff2 f828495bcecb9637ad3fd4dc2b 48f6d3e80d26536064e5eb82c3 c496bc744198993287823c891e 66cacdeb35dcdfc1375f17525b d39e311a89f417bc98fdca9a9c 3075053e392ac08d474b26f589 1b61 " ; " 30820263308201cc020900cb6c 4e844b58a1d4300d06092a8648 86f70d01010505003076310b30 09060355040613024155311330 1106035504080c0a536f6d652d 53746174653121301f06035504 0a0c18496e7465726e65742057 69646769747320507479204c74 643115301306035504030c0c59 4f5552204e414d452121213118 301606092a864886f70d010901 16096d65406261722e6465301e 170d3134303231373232303834 355a170d313530323137323230 3834355a3076310b3009060355 04061302415531133011060355 04080c0a536f6d652d53746174 653121301f060355040a0c1849 6e7465726e6574205769646769 747320507479204c7464311530 1306035504030c0c594f555220 4e414d45212121311830160609 2a864886f70d01090116096d65 406261722e646530819f300d06 092a864886f70d010101050003 818d0030818902818100b64048 dee6bc21943da2ab5eb6f8d837 007f417c0fe33492c3aa2f553e 4d5e31434689c26f2be68e00d2 88b0e3abf6fe118845d9498985 12f192cbe49fd5b0831f01cb2d 274db3a638f5befb3ce81ab6b5 59393444044fedd6ca154f76bf bd525608bb550a39bbd2ed12e6 d71f9f84ba21aa5e2180150267 1aab049af8640da10203010001 300d06092a864886f70d010105 0500038181008a38669a48969d c947296d442d7f032082d2db21 e5374cdd6ef6e7cc1da0fde511 ed3c5252f0a673dc689fdc5fca cc1b85dfe22b7bef2adb56b537 32e9811063794d6e239f8fa267 215ba7a4d3dce505e799ec5c38 cd1c16ee75e0d5a46b8f4c8e82 650561539a84305df19a5a241b e555f870834e094d41cf9f74b3 342e8345 " ; ] |
let time ( ) = None |
let with_loaded_file file ~ f = let fullpath = " . / testcertificates " / ^ file ^ " . pem " in let fd = Unix . ( openfile fullpath [ O_RDONLY ] 0 ) in let buf = Unix_cstruct . of_fd fd in try let r = f buf in Unix . close fd ; match r with | Ok data -> data | Error ( ` Msg m ) -> Alcotest . failf " decoding error in % s : % s " fullpath m with e -> Unix . close fd ; Alcotest . failf " exception in % s : % s " fullpath ( Printexc . to_string e ) |
let priv = match with_loaded_file " private / cakey " ~ f : Private_key . decode_pem with | ` RSA x -> x | _ -> assert false |
let cert name = with_loaded_file name ~ f : Certificate . decode_pem |
let host name = Domain_name . host_exn ( Domain_name . of_string_exn name ) |
let invalid_cas = [ " cacert - basicconstraint - ca - false " ; " cacert - unknown - critical - extension " ; " cacert - keyusage - crlsign " ; " cacert - ext - usage - timestamping " ] |
let cert_public_is_pub cert = let pub = Mirage_crypto_pk . Rsa . pub_of_priv priv in ( match Certificate . public_key cert with | ` RSA pub ' when pub = pub ' -> ( ) | _ -> Alcotest . fail " public / private key doesn ' t match " ) |
let test_invalid_ca name ( ) = let c = cert name in cert_public_is_pub c ; Alcotest . ( check int " CA list is empty " 0 ( List . length ( Validation . valid_cas [ c ] ) ) ) |
let invalid_ca_tests = List . mapi ( fun i args -> " invalid CA " ^ string_of_int i , ` Quick , test_invalid_ca args ) invalid_cas |
let cacert = cert " cacert " |
let cacert_pathlen0 = cert " cacert - pathlen - 0 " |
let cacert_ext = cert " cacert - unknown - extension " |
let cacert_ext_ku = cert " cacert - ext - usage " |
let cacert_v1 = cert " cacert - v1 " |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.