Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file core.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501(** Core type definitions *)openPacketopenCiphersuitelet(let*)=Result.bindletguardpe=ifpthenOk()elseErroreletsplit_str?(start=0)stroff=String.substrstartoff,String.substr(start+off)(String.lengthstr-off-start)letmap_reader_errorr=Result.map_error(fune->`Fatale)rtypetls13=[`TLS_1_3]letpp_tls13ppf`TLS_1_3=Fmt.stringppf"TLS 1.3"typetls_before_13=[|`TLS_1_0|`TLS_1_1|`TLS_1_2]letpp_tls_before_13ppf=function|`TLS_1_0->Fmt.stringppf"TLS 1.0"|`TLS_1_1->Fmt.stringppf"TLS 1.1"|`TLS_1_2->Fmt.stringppf"TLS 1.2"typetls_version=[tls13|tls_before_13]letpp_tls_versionppf=function|#tls13asv->pp_tls13ppfv|#tls_before_13asv->pp_tls_before_13ppfvletpair_of_tls_version=function|`TLS_1_0->(3,1)|`TLS_1_1->(3,2)|`TLS_1_2->(3,3)|`TLS_1_3->(3,4)letcompare_tls_versionab=matcha,bwith|`TLS_1_0,`TLS_1_0->0|`TLS_1_0,_->-1|_,`TLS_1_0->1|`TLS_1_1,`TLS_1_1->0|`TLS_1_1,_->-1|_,`TLS_1_1->1|`TLS_1_2,`TLS_1_2->0|`TLS_1_2,_->-1|_,`TLS_1_2->1|`TLS_1_3,`TLS_1_3->0letnext=function|`TLS_1_0->Some`TLS_1_1|`TLS_1_1->Some`TLS_1_2|`TLS_1_2->Some`TLS_1_3|`TLS_1_3->Noneletall_versions(min,max)=letrecgencurr=ifcompare_tls_versionmaxcurr>=0thenmatchnextcurrwith|None->[curr]|Somec->curr::gencelse[]inList.rev(genmin)lettls_version_of_pair=function|(3,1)->Some`TLS_1_0|(3,2)->Some`TLS_1_1|(3,3)->Some`TLS_1_2|(3,4)->Some`TLS_1_3|_->Nonetypetls_any_version=[|tls_version|`SSL_3|`TLS_1_Xofint]letpp_tls_any_versionppf=function|#tls_versionasv->pp_tls_versionppfv|`SSL_3->Fmt.stringppf"SSL3"|`TLS_1_Xx->Fmt.pfppf"TLS1.%u"xletany_version_to_version=function|#tls_versionasv->Somev|_->Noneletversion_eqab=matchawith|#tls_versionasx->compare_tls_versionxb=0|_->falseletversion_geab=matchawith|#tls_versionasx->compare_tls_versionxb>=0|`SSL_3->false|`TLS_1_X_->truelettls_any_version_of_pairx=matchtls_version_of_pairxwith|Somev->Somev|None->matchxwith|(3,0)->Some`SSL_3|(3,x)->Some(`TLS_1_Xx)|_->Noneletpair_of_tls_any_version=function|#tls_versionasx->pair_of_tls_versionx|`SSL_3->(3,0)|`TLS_1_Xm->(3,m)letmax_protocol_version(_,hi)=hiletmin_protocol_version(lo,_)=lotypetls_hdr={content_type:content_type;version:tls_any_version;}letpp_tls_hdrppf{content_type;version}=Fmt.pfppf"content type: %a version: %a"pp_content_typecontent_typepp_tls_any_versionversionmoduleSessionID=structtypet=stringletcompare=String.comparelethasht=Hashtbl.hashtletequal=String.equalendmodulePreSharedKeyID=structtypet=stringletcompare=String.comparelethasht=Hashtbl.hashtletequal=String.equalendtypepsk_identity=(string*int32)*stringletbinders_lenpsks=letbinder_len(_,binder)=String.lengthbinder+1(* binder len *)in2(* binder len *)+List.fold_left(+)0(List.mapbinder_lenpsks)typegroup=[|`FFDHE2048|`FFDHE3072|`FFDHE4096|`FFDHE6144|`FFDHE8192|`X25519|`P256|`P384|`P521]letpp_groupppf=function|`FFDHE2048->Fmt.stringppf"FFDHE2048"|`FFDHE3072->Fmt.stringppf"FFDHE3072"|`FFDHE4096->Fmt.stringppf"FFDHE4096"|`FFDHE6144->Fmt.stringppf"FFDHE6144"|`FFDHE8192->Fmt.stringppf"FFDHE8192"|`X25519->Fmt.stringppf"X25519"|`P256->Fmt.stringppf"P256"|`P384->Fmt.stringppf"P384"|`P521->Fmt.stringppf"P521"letnamed_group_to_group=function|FFDHE2048->Some`FFDHE2048|FFDHE3072->Some`FFDHE3072|FFDHE4096->Some`FFDHE4096|FFDHE6144->Some`FFDHE6144|FFDHE8192->Some`FFDHE8192|X25519->Some`X25519|SECP256R1->Some`P256|SECP384R1->Some`P384|SECP521R1->Some`P521|_->Noneletgroup_to_named_group=function|`FFDHE2048->FFDHE2048|`FFDHE3072->FFDHE3072|`FFDHE4096->FFDHE4096|`FFDHE6144->FFDHE6144|`FFDHE8192->FFDHE8192|`X25519->X25519|`P256->SECP256R1|`P384->SECP384R1|`P521->SECP521R1letgroup_to_impl=function|`FFDHE2048->`Finite_fieldMirage_crypto_pk.Dh.Group.ffdhe2048|`FFDHE3072->`Finite_fieldMirage_crypto_pk.Dh.Group.ffdhe3072|`FFDHE4096->`Finite_fieldMirage_crypto_pk.Dh.Group.ffdhe4096|`FFDHE6144->`Finite_fieldMirage_crypto_pk.Dh.Group.ffdhe6144|`FFDHE8192->`Finite_fieldMirage_crypto_pk.Dh.Group.ffdhe8192|`X25519->`X25519|`P256->`P256|`P384->`P384|`P521->`P521typesignature_algorithm=[|`RSA_PKCS1_MD5|`RSA_PKCS1_SHA1|`RSA_PKCS1_SHA224|`RSA_PKCS1_SHA256|`RSA_PKCS1_SHA384|`RSA_PKCS1_SHA512|`ECDSA_SECP256R1_SHA1|`ECDSA_SECP256R1_SHA256|`ECDSA_SECP384R1_SHA384|`ECDSA_SECP521R1_SHA512|`RSA_PSS_RSAENC_SHA256|`RSA_PSS_RSAENC_SHA384|`RSA_PSS_RSAENC_SHA512|`ED25519(* | `ED448
| `RSA_PSS_PSS_SHA256
| `RSA_PSS_PSS_SHA384
| `RSA_PSS_PSS_SHA512 *)]lethash_of_signature_algorithm=function|`RSA_PKCS1_MD5->`MD5|`RSA_PKCS1_SHA1->`SHA1|`RSA_PKCS1_SHA224->`SHA224|`RSA_PKCS1_SHA256->`SHA256|`RSA_PKCS1_SHA384->`SHA384|`RSA_PKCS1_SHA512->`SHA512|`RSA_PSS_RSAENC_SHA256->`SHA256|`RSA_PSS_RSAENC_SHA384->`SHA384|`RSA_PSS_RSAENC_SHA512->`SHA512|`ECDSA_SECP256R1_SHA1->`SHA1|`ECDSA_SECP256R1_SHA256->`SHA256|`ECDSA_SECP384R1_SHA384->`SHA384|`ECDSA_SECP521R1_SHA512->`SHA512|`ED25519->`SHA512letsignature_scheme_of_signature_algorithm=function|`RSA_PKCS1_MD5->`RSA_PKCS1|`RSA_PKCS1_SHA1->`RSA_PKCS1|`RSA_PKCS1_SHA224->`RSA_PKCS1|`RSA_PKCS1_SHA256->`RSA_PKCS1|`RSA_PKCS1_SHA384->`RSA_PKCS1|`RSA_PKCS1_SHA512->`RSA_PKCS1|`RSA_PSS_RSAENC_SHA256->`RSA_PSS|`RSA_PSS_RSAENC_SHA384->`RSA_PSS|`RSA_PSS_RSAENC_SHA512->`RSA_PSS|`ECDSA_SECP256R1_SHA1->`ECDSA|`ECDSA_SECP256R1_SHA256->`ECDSA|`ECDSA_SECP384R1_SHA384->`ECDSA|`ECDSA_SECP521R1_SHA512->`ECDSA|`ED25519->`ED25519letpp_signature_algorithmppfsa=leth=hash_of_signature_algorithmsaandss=signature_scheme_of_signature_algorithmsainletpp_signature_schemeppf=function|`RSA_PKCS1->Fmt.stringppf"RSA-PKCS1"|`RSA_PSS->Fmt.stringppf"RSA-PSS"|`ECDSA->Fmt.stringppf"ECDSA"|`ED25519->Fmt.stringppf"ED25519"inmatchsswith|`ED25519->Fmt.pfppf"%a"pp_signature_schemess|`ECDSA->letgroup_to_string=function|`ECDSA_SECP256R1_SHA1->"SECP256R1"|`ECDSA_SECP256R1_SHA256->"SECP256R1"|`ECDSA_SECP384R1_SHA384->"SECP384R1"|`ECDSA_SECP521R1_SHA512->"SECP521R1"|_->assertfalseinFmt.pfppf"%a %s %a"pp_signature_schemess(group_to_stringsa)pp_hashh|_->Fmt.pfppf"%a %a"pp_signature_schemesspp_hashhletrsa_sigalg=function|`RSA_PSS_RSAENC_SHA256|`RSA_PSS_RSAENC_SHA384|`RSA_PSS_RSAENC_SHA512|`RSA_PKCS1_SHA256|`RSA_PKCS1_SHA384|`RSA_PKCS1_SHA512|`RSA_PKCS1_SHA224|`RSA_PKCS1_SHA1|`RSA_PKCS1_MD5->true|`ECDSA_SECP256R1_SHA1|`ECDSA_SECP256R1_SHA256|`ECDSA_SECP384R1_SHA384|`ECDSA_SECP521R1_SHA512|`ED25519->falselettls13_sigalg=function|`RSA_PSS_RSAENC_SHA256|`RSA_PSS_RSAENC_SHA384|`RSA_PSS_RSAENC_SHA512|`ECDSA_SECP256R1_SHA256|`ECDSA_SECP384R1_SHA384|`ECDSA_SECP521R1_SHA512|`ED25519->true|`RSA_PKCS1_SHA256|`RSA_PKCS1_SHA384|`RSA_PKCS1_SHA512|`RSA_PKCS1_SHA224|`RSA_PKCS1_SHA1|`RSA_PKCS1_MD5|`ECDSA_SECP256R1_SHA1->falseletpk_matches_sapksa=matchpk,sawith|`RSA_,_->rsa_sigalgsa|`ED25519_,`ED25519|`P256_,(`ECDSA_SECP256R1_SHA1|`ECDSA_SECP256R1_SHA256)|`P384_,`ECDSA_SECP384R1_SHA384|`P521_,`ECDSA_SECP521R1_SHA512->true|_->falsetypeclient_extension=[|`Hostnameof[`host]Domain_name.t|`MaxFragmentLengthofmax_fragment_length|`SupportedGroupsofPacket.named_grouplist|`SecureRenegotiationofstring|`Paddingofint|`SignatureAlgorithmsofsignature_algorithmlist|`ExtendedMasterSecret|`ALPNofstringlist|`KeyShareof(Packet.named_group*string)list|`EarlyDataIndication|`PreSharedKeysofpsk_identitylist|`SupportedVersionsoftls_any_versionlist|`PostHandshakeAuthentication|`Cookieofstring|`PskKeyExchangeModesofpsk_key_exchange_modelist|`ECPointFormats|`UnknownExtensionof(int*string)]typeserver13_extension=[|`KeyShareof(group*string)|`PreSharedKeyofint|`SelectedVersionoftls_version(* only used internally in writer!! *)]typeserver_extension=[server13_extension|`Hostname|`MaxFragmentLengthofmax_fragment_length|`SecureRenegotiationofstring|`ExtendedMasterSecret|`ALPNofstring|`ECPointFormats|`UnknownExtensionof(int*string)]typeencrypted_extension=[|`Hostname|`MaxFragmentLengthofmax_fragment_length|`SupportedGroupsofgrouplist|`ALPNofstring|`EarlyDataIndication|`UnknownExtensionof(int*string)]typehello_retry_extension=[|`SelectedGroupofgroup(* only used internally in writer!! *)|`Cookieofstring|`SelectedVersionoftls_version(* only used internally in writer!! *)|`UnknownExtensionof(int*string)]typeclient_hello={client_version:tls_any_version;client_random:string;sessionid:SessionID.toption;ciphersuites:any_ciphersuitelist;extensions:client_extensionlist}typeserver_hello={server_version:tls_version;server_random:string;sessionid:SessionID.toption;ciphersuite:ciphersuite;extensions:server_extensionlist}typedh_parameters={dh_p:string;dh_g:string;dh_Ys:string;}typehello_retry={retry_version:tls_version;ciphersuite:ciphersuite13;sessionid:SessionID.toption;selected_group:group;extensions:hello_retry_extensionlist}typesession_ticket_extension=[|`EarlyDataIndicationofint32|`UnknownExtensionofint*string]typesession_ticket={lifetime:int32;age_add:int32;nonce:string;ticket:string;extensions:session_ticket_extensionlist}typecertificate_request_extension=[(* | `StatusRequest *)|`SignatureAlgorithmsofsignature_algorithmlist(* | `SignedCertificateTimestamp *)|`CertificateAuthoritiesofX509.Distinguished_name.tlist(* | `OidFilters *)(* | `SignatureAlgorithmsCert *)|`UnknownExtensionof(int*string)]typetls_handshake=|HelloRequest|HelloRetryRequestofhello_retry|EncryptedExtensionsofencrypted_extensionlist|ServerHelloDone|ClientHelloofclient_hello|ServerHelloofserver_hello|Certificateofstring|ServerKeyExchangeofstring|CertificateRequestofstring|ClientKeyExchangeofstring|CertificateVerifyofstring|Finishedofstring|SessionTicketofsession_ticket|KeyUpdateofkey_update_request_type|EndOfEarlyDataletpp_handshakeppf=function|HelloRequest->Fmt.stringppf"HelloRequest"|HelloRetryRequest_->Fmt.stringppf"HelloRetryRequest"|EncryptedExtensions_->Fmt.stringppf"EncryptedExtensions"|ServerHelloDone->Fmt.stringppf"ServerHelloDone"|ClientHello_->Fmt.stringppf"ClientHello"|ServerHello_->Fmt.stringppf"ServerHello"|Certificate_->Fmt.stringppf"Certificate"|ServerKeyExchange_->Fmt.stringppf"ServerKeyExchange"|CertificateRequest_->Fmt.stringppf"CertificateRequest"|ClientKeyExchange_->Fmt.stringppf"ClientKeyExchange"|CertificateVerify_->Fmt.stringppf"CertificateVerify"|Finished_->Fmt.stringppf"Finished"|SessionTicket_->Fmt.stringppf"SessionTicket"|KeyUpdate_->Fmt.stringppf"KeyUpdate"|EndOfEarlyData->Fmt.stringppf"EndOfEarlyData"letsrc=Logs.Src.create"tls.tracing"~doc:"TLS tracing"moduleTracing=structinclude(valLogs.src_logsrc:Logs.LOG)letcs~tagbuf=debug(funm->m"%s@.%a"tag(Ohex.pp_hexdump())buf)leths~taghs=debug(funm->m"%s %a"tagpp_handshakehs)endtypetls_alert=alert_level*alert_type(** the master secret of a TLS connection *)typemaster_secret=stringtypepsk13={identifier:string;obfuscation:int32;secret:string;lifetime:int32;early_data:int32;issued_at:Ptime.t;(* origin : [ `Resumption | `External ] (* using different labels for binder_key *) *)}typeepoch_state=[`ZeroRTT|`Established](** information about an open session *)typeepoch_data={side:[`Client|`Server];state:epoch_state;protocol_version:tls_version;ciphersuite:Ciphersuite.ciphersuite;peer_random:string;peer_certificate_chain:X509.Certificate.tlist;peer_certificate:X509.Certificate.toption;peer_name:[`host]Domain_name.toption;trust_anchor:X509.Certificate.toption;received_certificates:X509.Certificate.tlist;own_random:string;own_certificate:X509.Certificate.tlist;own_private_key:X509.Private_key.toption;own_name:[`host]Domain_name.toption;master_secret:master_secret;exporter_master_secret:master_secret;session_id:SessionID.t;extended_ms:bool;alpn_protocol:stringoption;tls_unique:stringoption;}letsupports_key_usage?(not_present=false)usagecert=matchX509.Extension.(findKey_usage(X509.Certificate.extensionscert))with|None->not_present|Some(_,kus)->List.memusagekusletsupports_extended_key_usage?(not_present=false)usagecert=matchX509.Extension.(findExt_key_usage(X509.Certificate.extensionscert))with|None->not_present|Some(_,kus)->List.memusagekus