Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: warning on decode lenght #3188

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/security/rsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,25 @@ uint16_t RSA::decodeLength(char*&pos) const {
if (length & 0x80) {
uint8_t numLengthBytes = length & 0x7F;
if (numLengthBytes > 4) {
g_logger().error("[RSA::loadPEM] - Invalid 'length'");
g_logger().error("[RSA::decodeLength] - Invalid 'length'");
return 0;
}
// Copy 'numLengthBytes' bytes from 'pos' into 'buffer', starting at the correct position
std::ranges::copy_n(pos, numLengthBytes, buffer.begin() + (4 - numLengthBytes));
// Adjust the copy destination to ensure it doesn't overflow
auto destIt = buffer.begin() + (4 - numLengthBytes);
if (destIt < buffer.begin() || destIt + numLengthBytes > buffer.end()) {
g_logger().error("[RSA::decodeLength] - Invalid copy range");
return 0;
}
// Copy 'numLengthBytes' bytes from 'pos' into 'buffer'
std::copy_n(pos, numLengthBytes, destIt);
pos += numLengthBytes;
// Reconstruct 'length' from 'buffer' (big-endian)
uint32_t tempLength = 0;
for (size_t i = 0; i < numLengthBytes; ++i) {
tempLength = (tempLength << 8) | buffer[4 - numLengthBytes + i];
}
if (tempLength > UINT16_MAX) {
g_logger().error("[RSA::loadPEM] - Length too large");
g_logger().error("[RSA::decodeLength] - Length too large");
return 0;
}
length = static_cast<uint16_t>(tempLength);
Expand Down
Loading