123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125(*
* Copyright (c) 2017 Christiano F. Haesbaert <haesbaert@haesbaert.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)openMirage_crypto_pktypepriv=|Rsa_privofRsa.priv|Ed25519_privofMirage_crypto_ec.Ed25519.privtypepub=|Rsa_pubofRsa.pub|Ed25519_pubofMirage_crypto_ec.Ed25519.publetpub_of_priv=function|Rsa_privpriv->Rsa_pub(Rsa.pub_of_privpriv)|Ed25519_privpriv->Ed25519_pub(Mirage_crypto_ec.Ed25519.pub_of_privpriv)letsexp_of_pubp=matchpwith|Rsa_pubp->Sexplib.Sexp.(List[Atom"rsa";Rsa.sexp_of_pubp])|Ed25519_pubp->letdata=Cstruct_sexp.sexp_of_t(Mirage_crypto_ec.Ed25519.pub_to_cstructp)inSexplib.Sexp.(List[Atom"ed25519";data])letpub_of_sexp_=failwith"Hostkey.pub_of_sexp: TODO"letsshname=function|Rsa_pub_->"ssh-rsa"|Ed25519_pub_->"ssh-ed25519"letcomptible_algpa=matchpwith|Rsa_pub_->beginmatchawith|"ssh-rsa"|"rsa-sha2-256"|"rsa-sha2-512"->true|_->falseend|Ed25519_pub_->beginmatchawith|"ssh-ed25519"->true|_->falseendtypealg=|Rsa_sha1|Rsa_sha256|Rsa_sha512|Ed25519lethash=function|Rsa_sha1->`SHA1|Rsa_sha256->`SHA256|Rsa_sha512->`SHA512|Ed25519->`SHA512letalg_of_string=function|"ssh-rsa"->OkRsa_sha1|"rsa-sha2-256"->OkRsa_sha256|"rsa-sha2-512"->OkRsa_sha512|"ssh-ed25519"->OkEd25519|s->Error("Unknown public key algorithm "^s)letalg_to_string=function|Rsa_sha1->"ssh-rsa"|Rsa_sha256->"rsa-sha2-256"|Rsa_sha512->"rsa-sha2-512"|Ed25519->"ssh-ed25519"letalg_of_sexp=function|Sexplib.Sexp.Atoms->beginmatchalg_of_stringswith|Okalg->alg|Errormsg->failwithmsgend|_->failwith"expected sexp atom for public key algorithm"letsexp_of_algt=Sexplib.Sexp.Atom(alg_to_stringt)letpreferred_algs=[Ed25519;Rsa_sha256;Rsa_sha512;Rsa_sha1]letalgs_of_typ=function|`Ed25519->[Ed25519]|`Rsa->[Rsa_sha256;Rsa_sha512;Rsa_sha1]letpriv_to_typ=function|Rsa_priv_->`Rsa|Ed25519_priv_->`Ed25519letalg_matchestypalg=List.memalg(algs_of_typtyp)letsignature_equal=Cstruct.equalletsignalgprivblob=matchprivwith|Rsa_privpriv->lethash=hashalginRsa.PKCS1.sign~hash~key:priv(`Messageblob)|Ed25519_privpriv->Mirage_crypto_ec.Ed25519.sign~key:privblobletverifyalgpub~unsigned~signed=matchpubwith|Rsa_pubkey->lethashph=h=hashalginRsa.PKCS1.verify~hashp~key~signature:signed(`Messageunsigned)|Ed25519_pubkey->Mirage_crypto_ec.Ed25519.verify~keysigned~msg:unsigned