2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2020-05-17 16:07:16 +02:00
|
|
|
#include "td/mtproto/RSA.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/mtproto/mtproto_api.h"
|
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/bn.h>
|
2020-12-22 21:26:15 +01:00
|
|
|
#include <openssl/opensslv.h>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <openssl/pem.h>
|
2020-12-22 21:26:15 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x30000000L
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <openssl/rsa.h>
|
2020-12-22 21:26:15 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
RSA::RSA(BigNum n, BigNum e) : n_(std::move(n)), e_(std::move(e)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
RSA RSA::clone() const {
|
|
|
|
return RSA(n_.clone(), e_.clone());
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:27:43 +01:00
|
|
|
Result<RSA> RSA::from_pem_public_key(Slice pem) {
|
2018-12-31 20:04:05 +01:00
|
|
|
init_crypto();
|
|
|
|
|
|
|
|
auto *bio =
|
|
|
|
BIO_new_mem_buf(const_cast<void *>(static_cast<const void *>(pem.ubegin())), narrow_cast<int32>(pem.size()));
|
|
|
|
if (bio == nullptr) {
|
|
|
|
return Status::Error("Cannot create BIO");
|
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
|
|
|
BIO_free(bio);
|
|
|
|
};
|
|
|
|
|
2020-12-22 21:26:15 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
|
|
|
|
EVP_PKEY *rsa = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
|
|
|
|
#else
|
2018-10-27 01:51:27 +02:00
|
|
|
auto rsa = PEM_read_bio_RSAPublicKey(bio, nullptr, nullptr, nullptr);
|
2020-12-22 21:26:15 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
if (rsa == nullptr) {
|
2020-12-22 21:26:15 +01:00
|
|
|
return Status::Error("Error while reading RSA public key");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
SCOPE_EXIT {
|
2020-12-22 21:26:15 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
|
|
|
|
EVP_PKEY_free(rsa);
|
|
|
|
#else
|
2018-12-31 20:04:05 +01:00
|
|
|
RSA_free(rsa);
|
2020-12-22 21:26:15 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2020-12-22 21:26:15 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
|
|
|
|
if (!EVP_PKEY_is_a(rsa, "RSA")) {
|
|
|
|
return Status::Error("Key is not an RSA key");
|
|
|
|
}
|
|
|
|
if (EVP_PKEY_size(rsa) != 256) {
|
|
|
|
return Status::Error("EVP_PKEY_size != 256");
|
|
|
|
}
|
|
|
|
#else
|
2018-12-31 20:04:05 +01:00
|
|
|
if (RSA_size(rsa) != 256) {
|
|
|
|
return Status::Error("RSA_size != 256");
|
|
|
|
}
|
2020-12-22 21:26:15 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-12-22 21:26:15 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
|
|
|
|
BIGNUM *n_num = nullptr;
|
|
|
|
BIGNUM *e_num = nullptr;
|
|
|
|
|
|
|
|
int res = EVP_PKEY_get_bn_param(rsa, "n", &n_num);
|
|
|
|
CHECK(res == 1 && n_num != nullptr);
|
|
|
|
res = EVP_PKEY_get_bn_param(rsa, "e", &e_num);
|
|
|
|
CHECK(res == 1 && e_num != nullptr);
|
|
|
|
|
|
|
|
auto n = static_cast<void *>(n_num);
|
|
|
|
auto e = static_cast<void *>(e_num);
|
|
|
|
#else
|
2018-12-31 20:04:05 +01:00
|
|
|
const BIGNUM *n_num;
|
|
|
|
const BIGNUM *e_num;
|
2020-12-22 21:26:15 +01:00
|
|
|
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
|
|
|
|
RSA_get0_key(rsa, &n_num, &e_num, nullptr);
|
|
|
|
#else
|
2018-12-31 20:04:05 +01:00
|
|
|
n_num = rsa->n;
|
|
|
|
e_num = rsa->e;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto n = static_cast<void *>(BN_dup(n_num));
|
|
|
|
auto e = static_cast<void *>(BN_dup(e_num));
|
|
|
|
if (n == nullptr || e == nullptr) {
|
|
|
|
return Status::Error("Cannot dup BIGNUM");
|
|
|
|
}
|
2020-12-22 21:26:15 +01:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return RSA(BigNum::from_raw(n), BigNum::from_raw(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 RSA::get_fingerprint() const {
|
|
|
|
// string objects are necessary, because mtproto_api::rsa_public_key contains Slice inside
|
|
|
|
string n_str = n_.to_binary();
|
|
|
|
string e_str = e_.to_binary();
|
2019-11-21 15:53:39 +01:00
|
|
|
mtproto_api::rsa_public_key public_key(n_str, e_str);
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t size = tl_calc_length(public_key);
|
|
|
|
std::vector<unsigned char> tmp(size);
|
|
|
|
size = tl_store_unsafe(public_key, tmp.data());
|
|
|
|
CHECK(size == tmp.size());
|
|
|
|
unsigned char key_sha1[20];
|
|
|
|
sha1(Slice(tmp.data(), tmp.size()), key_sha1);
|
|
|
|
return as<int64>(key_sha1 + 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t RSA::size() const {
|
2020-03-10 16:27:43 +01:00
|
|
|
// Checked in RSA::from_pem_public_key step
|
2018-12-31 20:04:05 +01:00
|
|
|
return 256;
|
|
|
|
}
|
|
|
|
|
2020-02-13 01:49:59 +01:00
|
|
|
size_t RSA::encrypt(unsigned char *from, size_t from_len, size_t max_from_len, unsigned char *to, size_t to_len) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(from_len > 0 && from_len <= 2550);
|
|
|
|
size_t pad = (25500 - from_len - 32) % 255 + 32;
|
|
|
|
size_t chunks = (from_len + pad) / 255;
|
|
|
|
int bits = n_.get_num_bits();
|
|
|
|
CHECK(bits >= 2041 && bits <= 2048);
|
|
|
|
CHECK(chunks * 255 == from_len + pad);
|
2020-02-13 01:49:59 +01:00
|
|
|
CHECK(from_len + pad <= max_from_len);
|
|
|
|
CHECK(chunks * 256 <= to_len);
|
2018-12-31 20:04:05 +01:00
|
|
|
Random::secure_bytes(from + from_len, pad);
|
|
|
|
|
2020-12-22 20:28:04 +01:00
|
|
|
size_t result = chunks * 256;
|
2018-12-31 20:04:05 +01:00
|
|
|
BigNumContext ctx;
|
|
|
|
BigNum y;
|
|
|
|
while (chunks-- > 0) {
|
|
|
|
BigNum x = BigNum::from_binary(Slice(from, 255));
|
|
|
|
BigNum::mod_exp(y, x, e_, n_, ctx);
|
2019-07-27 00:27:22 +02:00
|
|
|
MutableSlice(to, 256).copy_from(y.to_binary(256));
|
2018-12-31 20:04:05 +01:00
|
|
|
to += 256;
|
|
|
|
}
|
2020-12-22 20:28:04 +01:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 16:27:43 +01:00
|
|
|
void RSA::decrypt_signature(Slice from, MutableSlice to) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(from.size() == 256);
|
|
|
|
BigNumContext ctx;
|
|
|
|
BigNum x = BigNum::from_binary(from);
|
|
|
|
BigNum y;
|
|
|
|
BigNum::mod_exp(y, x, e_, n_, ctx);
|
2019-07-27 00:27:22 +02:00
|
|
|
to.copy_from(y.to_binary(256));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|