sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

How to convert Telegram Public key PEM format to this API format? #833

Closed nimix3 closed 5 years ago

nimix3 commented 5 years ago

Hello all,

anyone can help to convert from telegram public key to the format of this API (BigInteger)

Telegram provide PEM format (OpenSSL RSA Public key format) in the developer part like: -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvmpxVY7ld/8DAjz6F6q0 5shjg8/4p6047bn6/m8yPy1RBsvIyvuDuGnP/RzPEhzXQ9UJ5Ynmh2XJZgHoE9xb nfxL5BXHplJhMtADXKM9bWB11PU1Eioc3+AXBB8QiNFBn2XI5UkO5hPhbb9mJpjA 9Uhw8EdfqJP8QetVsI/xrCEbwEXe0xvifRLJbY08/Gp66KpQvy7g8w7VB8wlgePe xW3pT13Ap6vuC+mQuJPyiHvSxjEKHgqePji9NP3tJUFQjcECqcm0yV7/2d0t/pbC m+ZH1sadZspQCEPPrtbkQBlvHb4OLiIWPGHKSMeRFvp3IWcmdJqXahxLCUS1Eh6M AQIDAQAB -----END PUBLIC KEY-----

but in the API in the file TLSharp.Core\MTProto\Crypto\RSA.cs [65] it is like this: private static readonly Dictionary<string, RSAServerKey> serverKeys = new Dictionary<string, RSAServerKey>() { { "216be86c022bb4c3", new RSAServerKey("216be86c022bb4c3", new BigInteger("00C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F", 16), new BigInteger("010001", 16)) } };

Does anyone know about how to convert from the first one to the second one (this API format)?

nimix3 commented 5 years ago

It seems ASN1 format is the RSAServerKey, You can convert PEM or OpenSSL format to ASN1 format using this website https://lapo.it/asn1js but the question is the first number "216be86c022bb4c3" and whats this?

Ok, It seems the fingerprint of the given key but how create the fingerprint? Does anyone have any idea?

nimix3 commented 5 years ago

As described here https://core.telegram.org/mtproto/auth_key#dh-exchange-initiation The whole sentence from documentation is "server_public_key_fingerprints is a list of public RSA key fingerprints (64 lower-order bits of SHA1 (server_public_key); the public key is represented as a bare type rsa_public_key n:string e:string = RSAPublicKey, where, as usual, n and е are numbers in big endian format serialized as strings of bytes, following which SHA1 is computed) received by the server." So you need to TL-serialize n and e and calculate sha1 from that serialization.

knocte commented 5 years ago

What is TL-serialize n?

nimix3 commented 5 years ago

for correct fingerprint, we need to serialize strings as stated in https://core.telegram.org/mtproto/serialize#base-types and then sha1 as described above, Unfortunately, I do not get success yet!

nimix3 commented 5 years ago

I add some new server public keys in Pull Request, Bye