19 #include <packager/media/base/rsa_key.h>
24 #include <absl/log/check.h>
25 #include <absl/log/log.h>
26 #include <mbedtls/error.h>
27 #include <mbedtls/md.h>
31 const size_t kPssSaltLength = 20u;
33 std::string mbedtls_strerr(
int rv) {
35 std::string output(mbedtls_high_level_strerr(rv));
40 const char* low_level_error = mbedtls_low_level_strerr(rv);
41 if (low_level_error) {
43 output += low_level_error;
49 std::string sha1(
const std::string& message) {
50 const mbedtls_md_info_t* md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
53 std::string hash(mbedtls_md_get_size(md_info), 0);
55 mbedtls_md(md_info,
reinterpret_cast<const uint8_t*
>(message.data()),
56 message.size(),
reinterpret_cast<uint8_t*
>(hash.data())));
66 RsaPrivateKey::RsaPrivateKey() {
67 mbedtls_pk_init(&pk_context_);
68 mbedtls_entropy_init(&entropy_context_);
69 mbedtls_ctr_drbg_init(&prng_context_);
72 RsaPrivateKey::~RsaPrivateKey() {
73 mbedtls_pk_free(&pk_context_);
74 mbedtls_entropy_free(&entropy_context_);
75 mbedtls_ctr_drbg_free(&prng_context_);
80 if (!key->Deserialize(serialized_key)) {
86 bool RsaPrivateKey::Deserialize(
const std::string& serialized_key) {
87 const mbedtls_pk_info_t* pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
90 CHECK_EQ(mbedtls_ctr_drbg_seed(&prng_context_, mbedtls_entropy_func,
91 &entropy_context_, NULL,
95 int rv = mbedtls_pk_parse_key(
96 &pk_context_,
reinterpret_cast<const uint8_t*
>(serialized_key.data()),
97 serialized_key.size(),
99 0, mbedtls_ctr_drbg_random, &prng_context_);
101 LOG(ERROR) <<
"RSA private key failed to load: " << mbedtls_strerr(rv);
106 mbedtls_rsa_context* rsa_context = mbedtls_pk_rsa(pk_context_);
107 rv = mbedtls_rsa_set_padding(rsa_context, MBEDTLS_RSA_PKCS_V21,
110 LOG(ERROR) <<
"RSA private key failed to set padding: "
111 << mbedtls_strerr(rv);
119 std::string* decrypted_message) {
120 DCHECK(decrypted_message);
122 mbedtls_rsa_context* rsa_context = mbedtls_pk_rsa(pk_context_);
124 size_t rsa_size = mbedtls_rsa_get_len(rsa_context);
125 if (encrypted_message.size() != rsa_size) {
126 LOG(ERROR) <<
"Encrypted RSA message has the wrong size (expected "
127 << rsa_size <<
", actual " << encrypted_message.size() <<
").";
130 decrypted_message->resize(encrypted_message.size());
132 size_t decrypted_size = 0;
133 int rv = mbedtls_rsa_rsaes_oaep_decrypt(
134 rsa_context, mbedtls_ctr_drbg_random, &prng_context_,
137 reinterpret_cast<const uint8_t*
>(encrypted_message.data()),
138 reinterpret_cast<uint8_t*
>(decrypted_message->data()),
139 decrypted_message->size());
142 LOG(ERROR) <<
"RSA private decrypt failure: " << mbedtls_strerr(rv);
145 decrypted_message->resize(decrypted_size);
150 std::string* signature) {
152 if (message.empty()) {
153 LOG(ERROR) <<
"Message to be signed is empty.";
157 mbedtls_rsa_context* rsa_context = mbedtls_pk_rsa(pk_context_);
159 size_t rsa_size = mbedtls_rsa_get_len(rsa_context);
160 signature->resize(rsa_size);
162 std::string hash = sha1(message);
163 int rv = mbedtls_rsa_rsassa_pss_sign_ext(
164 rsa_context, mbedtls_ctr_drbg_random, &prng_context_, MBEDTLS_MD_SHA1,
165 static_cast<unsigned int>(hash.size()),
166 reinterpret_cast<const uint8_t*
>(hash.data()), kPssSaltLength,
167 reinterpret_cast<uint8_t*
>(signature->data()));
170 LOG(ERROR) <<
"RSA sign failure: " << mbedtls_strerr(rv);
176 RsaPublicKey::RsaPublicKey() {
177 mbedtls_pk_init(&pk_context_);
178 mbedtls_entropy_init(&entropy_context_);
179 mbedtls_ctr_drbg_init(&prng_context_);
182 RsaPublicKey::~RsaPublicKey() {
183 mbedtls_pk_free(&pk_context_);
184 mbedtls_entropy_free(&entropy_context_);
185 mbedtls_ctr_drbg_free(&prng_context_);
190 if (!key->Deserialize(serialized_key)) {
193 return key.release();
196 bool RsaPublicKey::Deserialize(
const std::string& serialized_key) {
197 const mbedtls_pk_info_t* pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
200 CHECK_EQ(mbedtls_ctr_drbg_seed(&prng_context_, mbedtls_entropy_func,
201 &entropy_context_, NULL,
205 int rv = mbedtls_pk_parse_public_key(
206 &pk_context_,
reinterpret_cast<const uint8_t*
>(serialized_key.data()),
207 serialized_key.size());
209 LOG(ERROR) <<
"RSA public key failed to load: " << mbedtls_strerr(rv);
214 mbedtls_rsa_context* rsa_context = mbedtls_pk_rsa(pk_context_);
215 rv = mbedtls_rsa_set_padding(rsa_context, MBEDTLS_RSA_PKCS_V21,
218 LOG(ERROR) <<
"RSA public key failed to set padding: "
219 << mbedtls_strerr(rv);
227 std::string* encrypted_message) {
228 DCHECK(encrypted_message);
229 if (clear_message.empty()) {
230 LOG(ERROR) <<
"Message to be encrypted is empty.";
234 mbedtls_rsa_context* rsa_context = mbedtls_pk_rsa(pk_context_);
236 size_t rsa_size = mbedtls_rsa_get_len(rsa_context);
237 encrypted_message->resize(rsa_size);
239 int rv = mbedtls_rsa_rsaes_oaep_encrypt(
240 rsa_context, mbedtls_ctr_drbg_random, &prng_context_,
242 0, clear_message.size(),
243 reinterpret_cast<const uint8_t*
>(clear_message.data()),
244 reinterpret_cast<uint8_t*
>(encrypted_message->data()));
247 LOG(ERROR) <<
"RSA public encrypt failure: " << mbedtls_strerr(rv);
254 const std::string& signature) {
255 if (message.empty()) {
256 LOG(ERROR) <<
"Signed message is empty.";
260 mbedtls_rsa_context* rsa_context = mbedtls_pk_rsa(pk_context_);
262 size_t rsa_size = mbedtls_rsa_get_len(rsa_context);
263 if (signature.size() != rsa_size) {
264 LOG(ERROR) <<
"Message signature is of the wrong size (expected "
265 << rsa_size <<
", actual " << signature.size() <<
").";
270 std::string hash = sha1(message);
271 int rv = mbedtls_rsa_rsassa_pss_verify_ext(
272 rsa_context, MBEDTLS_MD_SHA1,
static_cast<unsigned int>(hash.size()),
273 reinterpret_cast<const uint8_t*
>(hash.data()), MBEDTLS_MD_SHA1,
274 kPssSaltLength,
reinterpret_cast<const uint8_t*
>(signature.data()));
277 LOG(ERROR) <<
"RSA signature verification failed: " << mbedtls_strerr(rv);
All the methods that are virtual are virtual for mocking.