evalSsl.ml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. open EvalHash
  2. open EvalValue
  3. open EvalEncode
  4. open EvalDecode
  5. open EvalExceptions
  6. open Mbedtls
  7. let as_x509_crt vthis = match vthis with
  8. | VInstance {ikind = IMbedtlsX509Crt i} -> i
  9. | _ -> unexpected_value vthis "X509Crt"
  10. let as_config vthis = match vthis with
  11. | VInstance {ikind = IMbedtlsConfig i} -> i
  12. | _ -> unexpected_value vthis "Config"
  13. let as_socket vthis = match vthis with
  14. | VInstance {ikind = ISocket sock} -> sock
  15. | _ -> unexpected_value vthis "NativeSocket"
  16. let as_ctr_drbg vthis = match vthis with
  17. | VInstance {ikind = IMbedtlsCtrDrbg i} -> i
  18. | _ -> unexpected_value vthis "CtrDrbg"
  19. let as_entropy vthis = match vthis with
  20. | VInstance {ikind = IMbedtlsEntropy i} -> i
  21. | _ -> unexpected_value vthis "Entropy"
  22. let as_pk_context vthis = match vthis with
  23. | VInstance {ikind = IMbedtlsPkContext i} -> i
  24. | _ -> unexpected_value vthis "PkContext"
  25. let as_ssl vthis = match vthis with
  26. | VInstance {ikind = IMbedtlsSsl ctx} -> ctx
  27. | _ -> unexpected_value vthis "Ssl"
  28. let init_constructors add =
  29. add key_mbedtls_Config
  30. (fun _ ->
  31. let cfg = mbedtls_ssl_config_init() in
  32. encode_instance key_mbedtls_Config ~kind:(IMbedtlsConfig cfg)
  33. );
  34. add key_mbedtls_CtrDrbg
  35. (fun _ ->
  36. let ctr = mbedtls_ctr_drbg_init() in
  37. encode_instance key_mbedtls_CtrDrbg ~kind:(IMbedtlsCtrDrbg ctr)
  38. );
  39. add key_mbedtls_Entropy
  40. (fun _ ->
  41. let entropy = mbedtls_entropy_init() in
  42. encode_instance key_mbedtls_Entropy ~kind:(IMbedtlsEntropy entropy)
  43. );
  44. add key_mbedtls_PkContext
  45. (fun _ ->
  46. let pk = mbedtls_pk_init() in
  47. encode_instance key_mbedtls_PkContext ~kind:(IMbedtlsPkContext pk)
  48. );
  49. add key_mbedtls_Ssl
  50. (fun _ ->
  51. let ssl = mbedtls_ssl_init() in
  52. encode_instance key_mbedtls_Ssl ~kind:(IMbedtlsSsl ssl)
  53. );
  54. add key_mbedtls_X509Crt
  55. (fun _ ->
  56. let cert = mbedtls_x509_crt_init() in
  57. encode_instance key_mbedtls_X509Crt ~kind:(IMbedtlsX509Crt cert)
  58. )
  59. let init_fields init_fields builtins =
  60. let socket_send socket bytes =
  61. Unix.send socket bytes 0 (Bytes.length bytes) []
  62. in
  63. let socket_receive socket bytes =
  64. Unix.recv socket bytes 0 (Bytes.length bytes) []
  65. in
  66. let native_cert this =
  67. as_x509_crt (EvalField.field this (hash "native"))
  68. in
  69. init_fields builtins (["sys";"ssl"],"Certificate") [] [
  70. "get_altNames",vifun0 (fun this ->
  71. let x509_crt = native_cert this in
  72. let a = hx_cert_get_alt_names x509_crt in
  73. VArray (EvalArray.create (Array.map encode_string a))
  74. );
  75. "get_notAfter",vifun0 (fun this ->
  76. let x509_crt = native_cert this in
  77. let f = hx_cert_get_notafter x509_crt in
  78. encode_instance key_Date ~kind:(IDate f)
  79. );
  80. "get_notBefore",vifun0 (fun this ->
  81. let x509_crt = native_cert this in
  82. let f = hx_cert_get_notbefore x509_crt in
  83. encode_instance key_Date ~kind:(IDate f)
  84. );
  85. "issuer",vifun1 (fun this field ->
  86. let x509_crt = native_cert this in
  87. match hx_cert_get_issuer x509_crt (decode_string field) with
  88. | Some s -> encode_string s
  89. | None -> vnull
  90. );
  91. "subject",vifun1 (fun this field ->
  92. let x509_crt = native_cert this in
  93. match hx_cert_get_subject x509_crt (decode_string field) with
  94. | Some s -> encode_string s
  95. | None -> vnull
  96. );
  97. ];
  98. init_fields builtins (["sys";"ssl"],"Mbedtls") [
  99. "loadDefaults",vfun1 (fun this ->
  100. vint (hx_cert_load_defaults (as_x509_crt this));
  101. );
  102. "setSocket",vfun2 (fun this socket ->
  103. let ctx = as_ssl this in
  104. let socket = as_socket socket in
  105. mbedtls_ssl_set_bio ctx socket socket_send socket_receive;
  106. vnull
  107. );
  108. ] [];
  109. init_fields builtins (["mbedtls"],"X509Crt") [] [
  110. "next",vifun0 (fun this ->
  111. match mbedtls_x509_next (as_x509_crt this) with
  112. | None -> vnull
  113. | Some cert -> encode_instance key_mbedtls_X509Crt ~kind:(IMbedtlsX509Crt cert)
  114. );
  115. "parse",vifun1 (fun this bytes ->
  116. vint (mbedtls_x509_crt_parse (as_x509_crt this) (decode_bytes bytes));
  117. );
  118. "parse_file",vifun1 (fun this path ->
  119. vint (mbedtls_x509_crt_parse_file (as_x509_crt this) (decode_string path));
  120. );
  121. "parse_path",vifun1 (fun this path ->
  122. vint (mbedtls_x509_crt_parse_path (as_x509_crt this) (decode_string path));
  123. );
  124. ];
  125. init_fields builtins (["mbedtls"],"Config") [] [
  126. "authmode",vifun1 (fun this authmode ->
  127. mbedtls_ssl_config_authmode (as_config this) (decode_int authmode);
  128. vnull;
  129. );
  130. "ca_chain",vifun1 (fun this ca_chain ->
  131. mbedtls_ssl_conf_ca_chain (as_config this) (as_x509_crt ca_chain);
  132. vnull;
  133. );
  134. "defaults",vifun3 (fun this endpoint transport preset ->
  135. vint (mbedtls_ssl_config_defaults (as_config this) (decode_int endpoint) (decode_int transport) (decode_int preset));
  136. );
  137. "rng",vifun1(fun this p_rng ->
  138. mbedtls_ssl_config_rng (as_config this) (as_ctr_drbg p_rng);
  139. vnull
  140. )
  141. ];
  142. init_fields builtins (["mbedtls"],"CtrDrbg") [] [
  143. "random",vifun2 (fun this output output_len ->
  144. vint (mbedtls_ctr_drbg_random (as_ctr_drbg this) (decode_bytes output) (decode_int output_len));
  145. );
  146. "seed",vifun2(fun this entropy custom ->
  147. vint (mbedtls_ctr_drbg_seed (as_ctr_drbg this) (as_entropy entropy) (match custom with VString s -> Some s.sstring | _ -> None))
  148. )
  149. ];
  150. init_fields builtins (["mbedtls"],"Error") [
  151. "strerror",vfun1 (fun code -> encode_string (mbedtls_strerror (decode_int code)));
  152. ] [];
  153. init_fields builtins (["mbedtls"],"PkContext") [] [
  154. "parse_key",vifun2 (fun this key password ->
  155. vint (mbedtls_pk_parse_key (as_pk_context this) (decode_bytes key) (match password with VNull -> None | _ -> Some (decode_string password)));
  156. );
  157. "parse_keyfile",vifun2 (fun this path password ->
  158. vint (mbedtls_pk_parse_keyfile (as_pk_context this) (decode_string path) (match password with VNull -> None | _ -> Some (decode_string password)));
  159. );
  160. "parse_public_key",vifun1 (fun this key ->
  161. vint (mbedtls_pk_parse_public_key (as_pk_context this) (decode_bytes key));
  162. );
  163. "parse_public_keyfile",vifun1 (fun this path ->
  164. vint (mbedtls_pk_parse_public_keyfile (as_pk_context this) (decode_string path));
  165. );
  166. ];
  167. init_fields builtins (["mbedtls"],"Ssl") [] [
  168. "get_peer_cert",vifun0 (fun this ->
  169. match mbedtls_ssl_get_peer_cert (as_ssl this) with
  170. | None -> vnull
  171. | Some cert -> encode_instance key_mbedtls_X509Crt ~kind:(IMbedtlsX509Crt cert)
  172. );
  173. "handshake",vifun0 (fun this ->
  174. vint (mbedtls_ssl_handshake (as_ssl this));
  175. );
  176. "read",vifun3(fun this buf pos len ->
  177. vint (mbedtls_ssl_read (as_ssl this) (decode_bytes buf) (decode_int pos) (decode_int len);)
  178. );
  179. "set_hostname",vifun1 (fun this hostname ->
  180. vint (mbedtls_ssl_set_hostname (as_ssl this) (decode_string hostname));
  181. );
  182. "setup",vifun1 (fun this conf ->
  183. vint (mbedtls_ssl_setup (as_ssl this) (as_config conf))
  184. );
  185. "write",vifun3(fun this buf pos len ->
  186. vint (mbedtls_ssl_write (as_ssl this) (decode_bytes buf) (decode_int pos) (decode_int len);)
  187. );
  188. ];
  189. let statics a = List.map (fun (s,i) -> s,vint i) (Array.to_list a) in
  190. init_fields builtins (["mbedtls"],"SslAuthmode") (statics (hx_get_ssl_authmode_flags())) [];
  191. init_fields builtins (["mbedtls"],"SslEndpoint") (statics (hx_get_ssl_endpoint_flags())) [];
  192. init_fields builtins (["mbedtls"],"SslPreset") (statics (hx_get_ssl_preset_flags())) [];
  193. init_fields builtins (["mbedtls"],"SslTransport") (statics (hx_get_ssl_transport_flags())) [];