From 3ce79c593efe3fa4fb56b35de963842e75b4e062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Tro=C3=9Fbach?= Date: Tue, 17 Nov 2026 08:28:54 +0100 Subject: [PATCH 1/2] Refuse raw:hex: keys with non-hex characters, avoid over-read sqlite3mcExtractRawKey accepted a value after "raw:hex:" if only its length matched; non-hex characters were converted as 0, so for example "raw:hex:" followed by 64 times "z" gave an all-zero key without an error. Check isHex as well. The test for the secondary prefixes read 4 bytes even if fewer than 4 followed "raw:", up to 3 bytes past the end of a key passed without a terminating zero (for example "raw:b" with length 5). --- src/codec_algos.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/codec_algos.c b/src/codec_algos.c index 50156c0..f416fff 100644 --- a/src/codec_algos.c +++ b/src/codec_algos.c @@ -262,8 +262,8 @@ sqlite3mcExtractRawKey(const char* password, int passwordLength, */ int nRaw = passwordLength - 4; const unsigned char* zRaw = (const unsigned char*) password + 4; - int hasBinPrefix = !memcmp(zRaw, "bin:", 4); - int hasHexPrefix = !memcmp(zRaw, "hex:", 4); + int hasBinPrefix = nRaw >= 4 && !memcmp(zRaw, "bin:", 4); + int hasHexPrefix = nRaw >= 4 && !memcmp(zRaw, "hex:", 4); if (hasBinPrefix || hasHexPrefix) { zRaw += 4; @@ -279,7 +279,7 @@ sqlite3mcExtractRawKey(const char* password, int passwordLength, hasBinPrefix = 1; } int isValid = (hasBinPrefix && (nRaw == keyLength || nRaw == (keyLength + saltLength))) || - (hasHexPrefix && (nRaw == 2 * keyLength || nRaw == 2 * (keyLength + saltLength))); + (hasHexPrefix && isHex && (nRaw == 2 * keyLength || nRaw == 2 * (keyLength + saltLength))); if (isValid) { bypass = 1; From 975e49749dd5a644e2a19017a06df72ad8d909e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Tro=C3=9Fbach?= Date: Tue, 17 Nov 2026 08:28:54 +0100 Subject: [PATCH 2/2] Test the parsing of raw keys in cryptotest Valid values with raw:hex:, raw:bin: and raw:, with and without salt, a 128-bit hex key that has the length of key plus salt in binary form, and invalid values (wrong length, non-hex characters after raw:hex:, too short), each passed in a buffer of its exact length. --- test/cryptotest.c | 78 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/test/cryptotest.c b/test/cryptotest.c index 800a372..ff73aca 100644 --- a/test/cryptotest.c +++ b/test/cryptotest.c @@ -1,8 +1,8 @@ /* ** Tests for the cryptographic primitives: checks AES-CBC (NIST SP 800-38A), ** ChaCha20 and Poly1305 (RFC 8439) for all implementations available on the -** target, compares the implementations on pseudo-random input, and checks -** the Poly1305 tag comparison. +** target, compares the implementations on pseudo-random input, checks +** the Poly1305 tag comparison and the parsing of raw keys. */ #include "sqlite3mc.c" @@ -701,12 +701,86 @@ static void testTagCompare(void) #endif /* HAVE_SELECTABLE_IMPLEMENTATIONS */ +/* +** Raw keys: "raw:hex:", "raw:bin:" and "raw:" followed by the key (and +** optionally the salt). A value of the wrong length or with non-hex +** characters after "raw:hex:" must be refused, not used as a passphrase. +** Every key is passed in a buffer of its exact length (no terminating +** zero), so that a build with AddressSanitizer catches reads past its end. +*/ +static int checkRawKey(const char* text, int length, int keyLength, int expectBypass, int expectRc, + const unsigned char* expectKey, const unsigned char* expectSalt) +{ + unsigned char key[32], salt[16]; + char* buffer = (char*) malloc(length > 0 ? length : 1); + int rc, bypass, ok; + memcpy(buffer, text, length); + memset(key, 0xee, sizeof(key)); + memset(salt, 0xee, sizeof(salt)); + bypass = sqlite3mcExtractRawKey(buffer, length, 0, keyLength, 16, key, salt, &rc); + free(buffer); + ok = (bypass == expectBypass && rc == expectRc); + if (ok && expectKey != NULL) ok = (memcmp(key, expectKey, keyLength) == 0); + if (ok && expectSalt != NULL) ok = (memcmp(salt, expectSalt, 16) == 0); + if (!ok) printf(" raw key %.*s%s (%d bytes): bypass=%d rc=%d\n", length > 24 ? 24 : length, text, + length > 24 ? "..." : "", length, bypass, rc); + return ok ? 0 : 1; +} + +static void testRawKey(void) +{ + static const char hex64[] = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"; + static const char hex32salt[] = "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"; + unsigned char key[32], salt[16], binKey[32]; + char text[200]; + int failures = 0, checks = 0, j; + + for (j = 0; j < 32; j++) key[j] = (unsigned char) j; + for (j = 0; j < 16; j++) salt[j] = (unsigned char) (0xa0 + j); + for (j = 0; j < 32; j++) binKey[j] = (unsigned char) ('G' + (j % 20)); /* not hex characters */ + + /* Valid: 256-bit key, with and without salt */ + sqlite3_snprintf(sizeof(text), text, "raw:hex:%s", hex64); + failures += checkRawKey(text, 72, 32, 1, SQLITE_OK, key, NULL); checks++; + sqlite3_snprintf(sizeof(text), text, "raw:hex:%s%s", hex64, hex32salt); + failures += checkRawKey(text, 104, 32, 1, SQLITE_OK, key, salt); checks++; + sqlite3_snprintf(sizeof(text), text, "raw:%s", hex64); + failures += checkRawKey(text, 68, 32, 1, SQLITE_OK, key, NULL); checks++; + memcpy(text, "raw:bin:", 8); memcpy(text + 8, key, 32); + failures += checkRawKey(text, 40, 32, 1, SQLITE_OK, key, NULL); checks++; + memcpy(text + 40, salt, 16); + failures += checkRawKey(text, 56, 32, 1, SQLITE_OK, key, salt); checks++; + memcpy(text, "raw:", 4); memcpy(text + 4, binKey, 32); + failures += checkRawKey(text, 36, 32, 1, SQLITE_OK, binKey, NULL); checks++; + + /* Valid: 128-bit key; 32 hex digits are a hex key, not 16 bytes of key and 16 of salt */ + sqlite3_snprintf(sizeof(text), text, "raw:%.32s", hex64); + failures += checkRawKey(text, 36, 16, 1, SQLITE_OK, key, NULL); checks++; + + /* Invalid: wrong length, non-hex characters after raw:hex:, too short */ + failures += checkRawKey("raw:0123456789", 14, 32, 0, SQLITE_ERROR, NULL, NULL); checks++; + sqlite3_snprintf(sizeof(text), text, "raw:hex:%.63s", hex64); + failures += checkRawKey(text, 71, 32, 0, SQLITE_ERROR, NULL, NULL); checks++; + memcpy(text, "raw:hex:", 8); memset(text + 8, 'z', 64); + failures += checkRawKey(text, 72, 32, 0, SQLITE_ERROR, NULL, NULL); checks++; + memcpy(text, "raw:bin:", 8); memcpy(text + 8, key, 32); text[40] = 'x'; + failures += checkRawKey(text, 41, 32, 0, SQLITE_ERROR, NULL, NULL); checks++; + failures += checkRawKey("raw:b", 5, 32, 0, SQLITE_ERROR, NULL, NULL); checks++; + failures += checkRawKey("raw:he", 6, 32, 0, SQLITE_ERROR, NULL, NULL); checks++; + + /* Not a raw key: passphrase */ + failures += checkRawKey("secret", 6, 32, 0, SQLITE_OK, NULL, NULL); checks++; + + reportResult("Raw key", "parsing", failures, checks); +} + int main(void) { printf("%s, SQLite %s\n", sqlite3mc_version(), sqlite3_libversion()); testAesCbc(); testChaCha20(); testPoly1305(); + testRawKey(); #ifdef HAVE_SELECTABLE_IMPLEMENTATIONS testTagCompare(); #endif