Skip to content

Commit d637b86

Browse files
Copilotmgallien
authored andcommitted
Address code review feedback: toLatin1, bounds check, const_cast comments, counter check
1 parent 7c9445b commit d637b86

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/cmd/e2ee_verify_metadata.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ static bool aes128GcmDecrypt(const QByteArray &key,
154154
break;
155155
}
156156

157-
// Set expected GCM authentication tag before calling Final
157+
// Set expected GCM authentication tag before calling Final.
158+
// EVP_CIPHER_CTX_ctrl takes a void* but does not modify the tag data when
159+
// EVP_CTRL_GCM_SET_TAG is used for decryption; the cast is required by the API.
158160
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, kGcmTagSize,
159161
const_cast<unsigned char *>(tag))) {
160162
printVerbose(QStringLiteral("AES-GCM: failed to set GCM tag"));
@@ -193,6 +195,8 @@ static QByteArray gunzip(const QByteArray &compressed)
193195
return {};
194196
}
195197

198+
// z_stream.next_in is Bytef* (non-const) in the zlib API even though inflate
199+
// does not modify the input; the cast is required to satisfy the API signature.
196200
stream.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(compressed.constData()));
197201
stream.avail_in = static_cast<uInt>(compressed.size());
198202

@@ -235,13 +239,14 @@ static QByteArray decryptAndDecompressMetadata(const QJsonDocument &innerDoc,
235239
const auto metaObj = innerDoc.object().value(QStringLiteral("metadata")).toObject();
236240

237241
const QByteArray ciphertextField =
238-
metaObj.value(QStringLiteral("ciphertext")).toString().toLocal8Bit();
242+
metaObj.value(QStringLiteral("ciphertext")).toString().toLatin1();
239243
const QByteArray nonce =
240-
QByteArray::fromBase64(metaObj.value(QStringLiteral("nonce")).toString().toLocal8Bit());
244+
QByteArray::fromBase64(metaObj.value(QStringLiteral("nonce")).toString().toLatin1());
241245

242246
// Strip the legacy "|iv" suffix – the canonical nonce comes from the
243247
// separate "nonce" field.
244-
const QByteArray ciphertextBase64 = ciphertextField.split('|').at(0);
248+
const QList<QByteArray> ciphertextParts = ciphertextField.split('|');
249+
const QByteArray ciphertextBase64 = ciphertextParts.value(0);
245250
const QByteArray ciphertextWithTag = QByteArray::fromBase64(ciphertextBase64);
246251

247252
printVerbose(QStringLiteral("Ciphertext+tag size: %1 bytes").arg(ciphertextWithTag.size()));
@@ -555,9 +560,11 @@ static bool validateDecryptedJson(const QJsonDocument &doc, bool isNested)
555560
bool ok = true;
556561
const QJsonObject obj = doc.object();
557562

558-
// counter – must be a non-negative JSON number
563+
// counter – must be a non-negative JSON number.
564+
// isDouble() guards against missing/non-numeric values; toInteger() then
565+
// checks the numeric value is non-negative.
559566
if (!obj.value(QStringLiteral("counter")).isDouble()
560-
|| obj.value(QStringLiteral("counter")).toInteger(-1) < 0) {
567+
|| obj.value(QStringLiteral("counter")).toInteger() < 0) {
561568
printFail(QStringLiteral("Decrypted JSON: 'counter' is missing or not a non-negative integer"));
562569
ok = false;
563570
} else {
@@ -660,7 +667,7 @@ static bool verifyKeyChecksum(const QJsonDocument &decryptedDoc,
660667
const QJsonArray kcArr =
661668
decryptedDoc.object().value(QStringLiteral("keyChecksums")).toArray();
662669
for (const QJsonValue &kcVal : kcArr) {
663-
const QByteArray kcStr = kcVal.toVariant().toString().toUtf8();
670+
const QByteArray kcStr = kcVal.toString().toUtf8();
664671
printVerbose(QStringLiteral(" Stored checksum: %1").arg(QString::fromLatin1(kcStr)));
665672
if (kcStr == expectedHex) {
666673
return true;

0 commit comments

Comments
 (0)