@@ -394,13 +394,13 @@ bool MessageCrypto::encrypt(const std::set<std::string>& encKeys, const CryptoKe
394394 return true ;
395395}
396396
397- bool MessageCrypto::decryptDataKey (const proto::EncryptionKeys & encKeys, const CryptoKeyReader& keyReader) {
398- const auto & keyName = encKeys.key () ;
399- const auto & encryptedDataKey = encKeys.value () ;
400- const auto & encKeyMeta = encKeys.metadata () ;
397+ bool MessageCrypto::decryptDataKey (const EncryptionKey & encKeys, const CryptoKeyReader& keyReader) {
398+ const auto & keyName = encKeys.key ;
399+ const auto & encryptedDataKey = encKeys.value ;
400+ const auto & encKeyMeta = encKeys.metadata ;
401401 StringMap keyMeta;
402402 for (auto iter = encKeyMeta.begin (); iter != encKeyMeta.end (); iter++) {
403- keyMeta[iter->key () ] = iter->value () ;
403+ keyMeta[iter->first ] = iter->second ;
404404 }
405405
406406 // Read the private key info using callback
@@ -451,11 +451,10 @@ bool MessageCrypto::decryptDataKey(const proto::EncryptionKeys& encKeys, const C
451451 return true ;
452452}
453453
454- bool MessageCrypto::decryptData (const std::string& dataKeySecret, const proto::MessageMetadata& msgMetadata ,
454+ bool MessageCrypto::decryptData (const std::string& dataKeySecret, const EncryptionContext& context ,
455455 SharedBuffer& payload, SharedBuffer& decryptedPayload) {
456456 // unpack iv and encrypted data
457- msgMetadata.encryption_param ().copy (reinterpret_cast <char *>(iv_.get ()),
458- msgMetadata.encryption_param ().size ());
457+ context.param ().copy (reinterpret_cast <char *>(iv_.get ()), context.param ().size ());
459458
460459 EVP_CIPHER_CTX* cipherCtx = NULL ;
461460 decryptedPayload = SharedBuffer::allocate (payload.readableBytes () + EVP_MAX_BLOCK_LENGTH + tagLen_);
@@ -518,15 +517,14 @@ bool MessageCrypto::decryptData(const std::string& dataKeySecret, const proto::M
518517 return true ;
519518}
520519
521- bool MessageCrypto::getKeyAndDecryptData (const proto::MessageMetadata& msgMetadata , SharedBuffer& payload,
520+ bool MessageCrypto::getKeyAndDecryptData (const EncryptionContext& context , SharedBuffer& payload,
522521 SharedBuffer& decryptedPayload) {
523522 SharedBuffer decryptedData;
524523 bool dataDecrypted = false ;
525524
526- for (auto iter = msgMetadata.encryption_keys ().begin (); iter != msgMetadata.encryption_keys ().end ();
527- iter++) {
528- const std::string& keyName = iter->key ();
529- const std::string& encDataKey = iter->value ();
525+ for (auto && kv : context.keys ()) {
526+ const std::string& keyName = kv.key ;
527+ const std::string& encDataKey = kv.value ;
530528 unsigned char keyDigest[EVP_MAX_MD_SIZE];
531529 unsigned int digestLen = 0 ;
532530 getDigest (keyName, encDataKey.c_str (), encDataKey.size (), keyDigest, digestLen);
@@ -539,7 +537,7 @@ bool MessageCrypto::getKeyAndDecryptData(const proto::MessageMetadata& msgMetada
539537 // retruns a different key, decryption fails. At this point, we would
540538 // call decryptDataKey to refresh the cache and come here again to decrypt.
541539 auto dataKeyEntry = dataKeyCacheIter->second ;
542- if (decryptData (dataKeyEntry.first , msgMetadata , payload, decryptedPayload)) {
540+ if (decryptData (dataKeyEntry.first , context , payload, decryptedPayload)) {
543541 dataDecrypted = true ;
544542 break ;
545543 }
@@ -552,17 +550,16 @@ bool MessageCrypto::getKeyAndDecryptData(const proto::MessageMetadata& msgMetada
552550 return dataDecrypted;
553551}
554552
555- bool MessageCrypto::decrypt (const proto::MessageMetadata& msgMetadata , SharedBuffer& payload,
553+ bool MessageCrypto::decrypt (const EncryptionContext& context , SharedBuffer& payload,
556554 const CryptoKeyReaderPtr& keyReader, SharedBuffer& decryptedPayload) {
557555 // Attempt to decrypt using the existing key
558- if (getKeyAndDecryptData (msgMetadata , payload, decryptedPayload)) {
556+ if (getKeyAndDecryptData (context , payload, decryptedPayload)) {
559557 return true ;
560558 }
561559
562560 // Either first time, or decryption failed. Attempt to regenerate data key
563561 bool isDataKeyDecrypted = false ;
564- for (int index = 0 ; index < msgMetadata.encryption_keys_size (); index++) {
565- const proto::EncryptionKeys& encKeys = msgMetadata.encryption_keys (index);
562+ for (auto && encKeys : context.keys ()) {
566563 if (decryptDataKey (encKeys, *keyReader)) {
567564 isDataKeyDecrypted = true ;
568565 break ;
@@ -574,7 +571,7 @@ bool MessageCrypto::decrypt(const proto::MessageMetadata& msgMetadata, SharedBuf
574571 return false ;
575572 }
576573
577- return getKeyAndDecryptData (msgMetadata , payload, decryptedPayload);
574+ return getKeyAndDecryptData (context , payload, decryptedPayload);
578575}
579576
580577} /* namespace pulsar */
0 commit comments