19 #include "packager/media/base/rsa_key.h"
21 #include <openssl/err.h>
22 #include <openssl/rsa.h>
23 #include <openssl/x509.h>
26 #include "packager/base/logging.h"
27 #include "packager/base/sha1.h"
28 #include "packager/base/stl_util.h"
32 const size_t kPssSaltLength = 20u;
35 RSA* DeserializeRsaKey(
const std::string& serialized_key,
36 bool deserialize_private_key) {
37 if (serialized_key.empty()) {
38 LOG(ERROR) <<
"Serialized RSA Key is empty.";
42 BIO* bio = BIO_new_mem_buf(const_cast<char*>(serialized_key.data()),
43 serialized_key.size());
45 LOG(ERROR) <<
"BIO_new_mem_buf returned NULL.";
48 RSA* rsa_key = deserialize_private_key ? d2i_RSAPrivateKey_bio(bio, NULL)
49 : d2i_RSAPublicKey_bio(bio, NULL);
54 RSA* DeserializeRsaPrivateKey(
const std::string& serialized_key) {
55 RSA* rsa_key = DeserializeRsaKey(serialized_key,
true);
57 LOG(ERROR) <<
"Private RSA key deserialization failure.";
60 if (RSA_check_key(rsa_key) != 1) {
61 LOG(ERROR) <<
"Invalid RSA Private key: " << ERR_error_string(
62 ERR_get_error(), NULL);
69 RSA* DeserializeRsaPublicKey(
const std::string& serialized_key) {
70 RSA* rsa_key = DeserializeRsaKey(serialized_key,
false);
72 LOG(ERROR) <<
"Private RSA key deserialization failure.";
75 if (RSA_size(rsa_key) <= 0) {
76 LOG(ERROR) <<
"Invalid RSA Public key: " << ERR_error_string(
77 ERR_get_error(), NULL);
86 namespace edash_packager {
89 RsaPrivateKey::RsaPrivateKey(RSA* rsa_key) : rsa_key_(rsa_key) {
92 RsaPrivateKey::~RsaPrivateKey() {
98 RSA* rsa_key = DeserializeRsaPrivateKey(serialized_key);
103 std::string* decrypted_message) {
104 DCHECK(decrypted_message);
106 size_t rsa_size = RSA_size(rsa_key_);
107 if (encrypted_message.size() != rsa_size) {
108 LOG(ERROR) <<
"Encrypted RSA message has the wrong size (expected "
109 << rsa_size <<
", actual " << encrypted_message.size() <<
").";
113 decrypted_message->resize(rsa_size);
114 int decrypted_size = RSA_private_decrypt(
116 reinterpret_cast<const uint8_t*>(encrypted_message.data()),
117 reinterpret_cast<uint8_t*>(string_as_array(decrypted_message)),
119 RSA_PKCS1_OAEP_PADDING);
121 if (decrypted_size == -1) {
122 LOG(ERROR) <<
"RSA private decrypt failure: " << ERR_error_string(
123 ERR_get_error(), NULL);
126 decrypted_message->resize(decrypted_size);
131 std::string* signature) {
133 if (message.empty()) {
134 LOG(ERROR) <<
"Message to be signed is empty.";
138 std::string message_digest = base::SHA1HashString(message);
141 size_t rsa_size = RSA_size(rsa_key_);
142 std::vector<uint8_t> padded_digest(rsa_size);
143 if (!RSA_padding_add_PKCS1_PSS_mgf1(
146 reinterpret_cast<uint8_t*>(string_as_array(&message_digest)),
150 LOG(ERROR) <<
"RSA padding failure: " << ERR_error_string(ERR_get_error(),
156 signature->resize(rsa_size);
157 int signature_size = RSA_private_encrypt(
158 padded_digest.size(),
160 reinterpret_cast<uint8_t*
>(string_as_array(signature)),
164 if (signature_size != static_cast<int>(rsa_size)) {
165 LOG(ERROR) <<
"RSA private encrypt failure: " << ERR_error_string(
166 ERR_get_error(), NULL);
172 RsaPublicKey::RsaPublicKey(RSA* rsa_key) : rsa_key_(rsa_key) {
175 RsaPublicKey::~RsaPublicKey() {
176 if (rsa_key_ != NULL)
181 RSA* rsa_key = DeserializeRsaPublicKey(serialized_key);
182 return rsa_key == NULL ? NULL :
new RsaPublicKey(rsa_key);
186 std::string* encrypted_message) {
187 DCHECK(encrypted_message);
188 if (clear_message.empty()) {
189 LOG(ERROR) <<
"Message to be encrypted is empty.";
193 size_t rsa_size = RSA_size(rsa_key_);
194 encrypted_message->resize(rsa_size);
195 int encrypted_size = RSA_public_encrypt(
196 clear_message.size(),
197 reinterpret_cast<const uint8_t*
>(clear_message.data()),
198 reinterpret_cast<uint8_t*>(string_as_array(encrypted_message)),
200 RSA_PKCS1_OAEP_PADDING);
202 if (encrypted_size != static_cast<int>(rsa_size)) {
203 LOG(ERROR) <<
"RSA public encrypt failure: " << ERR_error_string(
204 ERR_get_error(), NULL);
211 const std::string& signature) {
212 if (message.empty()) {
213 LOG(ERROR) <<
"Signed message is empty.";
217 size_t rsa_size = RSA_size(rsa_key_);
218 if (signature.size() != rsa_size) {
219 LOG(ERROR) <<
"Message signature is of the wrong size (expected "
220 << rsa_size <<
", actual " << signature.size() <<
").";
225 std::vector<uint8_t> padded_digest(signature.size());
227 RSA_public_decrypt(signature.size(),
228 reinterpret_cast<const uint8_t*
>(signature.data()),
233 if (decrypted_size != static_cast<int>(rsa_size)) {
234 LOG(ERROR) <<
"RSA public decrypt failure: " << ERR_error_string(
235 ERR_get_error(), NULL);
239 std::string message_digest = base::SHA1HashString(message);
242 return RSA_verify_PKCS1_PSS_mgf1(
244 reinterpret_cast<const uint8_t*>(message_digest.data()),
248 kPssSaltLength) != 0;