codecext.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #ifndef SQLITE_OMIT_DISKIO
  2. #ifdef SQLITE_HAS_CODEC
  3. #include "codec.h"
  4. void sqlite3_activate_see(const char *info)
  5. {
  6. }
  7. /*
  8. // Free the encryption data structure associated with a pager instance.
  9. // (called from the modified code in pager.c)
  10. */
  11. void sqlite3CodecFree(void *pCodecArg)
  12. {
  13. if (pCodecArg)
  14. {
  15. CodecTerm(pCodecArg);
  16. sqlite3_free(pCodecArg);
  17. }
  18. }
  19. void sqlite3CodecSizeChange(void *pArg, int pageSize, int reservedSize)
  20. {
  21. }
  22. /*
  23. // Encrypt/Decrypt functionality, called by pager.c
  24. */
  25. void* sqlite3Codec(void* pCodecArg, void* data, Pgno nPageNum, int nMode)
  26. {
  27. Codec* codec = NULL;
  28. int pageSize;
  29. if (pCodecArg == NULL)
  30. {
  31. return data;
  32. }
  33. codec = (Codec*) pCodecArg;
  34. if (!CodecIsEncrypted(codec))
  35. {
  36. return data;
  37. }
  38. pageSize = sqlite3BtreeGetPageSize(CodecGetBtree(codec));
  39. switch(nMode)
  40. {
  41. case 0: /* Undo a "case 7" journal file encryption */
  42. case 2: /* Reload a page */
  43. case 3: /* Load a page */
  44. if (CodecHasReadKey(codec))
  45. {
  46. CodecDecrypt(codec, nPageNum, (unsigned char*) data, pageSize);
  47. }
  48. break;
  49. case 6: /* Encrypt a page for the main database file */
  50. if (CodecHasWriteKey(codec))
  51. {
  52. unsigned char* pageBuffer = CodecGetPageBuffer(codec);
  53. memcpy(pageBuffer, data, pageSize);
  54. data = pageBuffer;
  55. CodecEncrypt(codec, nPageNum, (unsigned char*) data, pageSize, 1);
  56. }
  57. break;
  58. case 7: /* Encrypt a page for the journal file */
  59. /* Under normal circumstances, the readkey is the same as the writekey. However,
  60. when the database is being rekeyed, the readkey is not the same as the writekey.
  61. The rollback journal must be written using the original key for the
  62. database file because it is, by nature, a rollback journal.
  63. Therefore, for case 7, when the rollback is being written, always encrypt using
  64. the database's readkey, which is guaranteed to be the same key that was used to
  65. read the original data.
  66. */
  67. if (CodecHasReadKey(codec))
  68. {
  69. unsigned char* pageBuffer = CodecGetPageBuffer(codec);
  70. memcpy(pageBuffer, data, pageSize);
  71. data = pageBuffer;
  72. CodecEncrypt(codec, nPageNum, (unsigned char*) data, pageSize, 0);
  73. }
  74. break;
  75. }
  76. return data;
  77. }
  78. void* mySqlite3PagerGetCodec(
  79. Pager *pPager
  80. );
  81. void mySqlite3PagerSetCodec(
  82. Pager *pPager,
  83. void *(*xCodec)(void*,void*,Pgno,int),
  84. void (*xCodecSizeChng)(void*,int,int),
  85. void (*xCodecFree)(void*),
  86. void *pCodec
  87. );
  88. int sqlite3CodecAttach(sqlite3* db, int nDb, const void* zKey, int nKey)
  89. {
  90. /* Attach a key to a database. */
  91. Codec* codec = (Codec*) sqlite3_malloc(sizeof(Codec));
  92. CodecInit(codec);
  93. sqlite3_mutex_enter(db->mutex);
  94. /* No key specified, could mean either use the main db's encryption or no encryption */
  95. if (zKey == NULL || nKey <= 0)
  96. {
  97. /* No key specified */
  98. if (nDb != 0 && nKey > 0)
  99. {
  100. Codec* mainCodec = (Codec*) mySqlite3PagerGetCodec(sqlite3BtreePager(db->aDb[0].pBt));
  101. /* Attached database, therefore use the key of main database, if main database is encrypted */
  102. if (mainCodec != NULL && CodecIsEncrypted(mainCodec))
  103. {
  104. CodecCopy(codec, mainCodec);
  105. CodecSetBtree(codec, db->aDb[nDb].pBt);
  106. #if (SQLITE_VERSION_NUMBER >= 3006016)
  107. mySqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, sqlite3CodecSizeChange, sqlite3CodecFree, codec);
  108. #else
  109. #if (SQLITE_VERSION_NUMBER >= 3003014)
  110. sqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec);
  111. #else
  112. sqlite3pager_set_codec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec);
  113. #endif
  114. db->aDb[nDb].pAux = codec;
  115. db->aDb[nDb].xFreeAux = sqlite3CodecFree;
  116. #endif
  117. }
  118. else
  119. {
  120. CodecSetIsEncrypted(codec, 0);
  121. sqlite3_free(codec);
  122. }
  123. }
  124. }
  125. else
  126. {
  127. /* Key specified, setup encryption key for database */
  128. CodecSetIsEncrypted(codec, 1);
  129. CodecSetHasReadKey(codec, 1);
  130. CodecSetHasWriteKey(codec, 1);
  131. CodecGenerateReadKey(codec, (char*) zKey, nKey);
  132. CodecCopyKey(codec, 1);
  133. CodecSetBtree(codec, db->aDb[nDb].pBt);
  134. #if (SQLITE_VERSION_NUMBER >= 3006016)
  135. mySqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, sqlite3CodecSizeChange, sqlite3CodecFree, codec);
  136. #else
  137. #if (SQLITE_VERSION_NUMBER >= 3003014)
  138. sqlite3PagerSetCodec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec);
  139. #else
  140. sqlite3pager_set_codec(sqlite3BtreePager(db->aDb[nDb].pBt), sqlite3Codec, codec);
  141. #endif
  142. db->aDb[nDb].pAux = codec;
  143. db->aDb[nDb].xFreeAux = sqlite3CodecFree;
  144. #endif
  145. }
  146. sqlite3_mutex_leave(db->mutex);
  147. return SQLITE_OK;
  148. }
  149. void sqlite3CodecGetKey(sqlite3* db, int nDb, void** zKey, int* nKey)
  150. {
  151. /*
  152. // The unencrypted password is not stored for security reasons
  153. // therefore always return NULL
  154. // If the main database is encrypted a key length of 1 is returned.
  155. // In that case an attached database will get the same encryption key
  156. // as the main database if no key was explicitly given for the attached database.
  157. */
  158. Codec* mainCodec = (Codec*) mySqlite3PagerGetCodec(sqlite3BtreePager(db->aDb[0].pBt));
  159. int keylen = (mainCodec != NULL && CodecIsEncrypted(mainCodec)) ? 1 : 0;
  160. *zKey = NULL;
  161. *nKey = keylen;
  162. }
  163. static int dbFindIndex(sqlite3* db, const char* zDb)
  164. {
  165. int dbIndex = 0;
  166. if (zDb != NULL)
  167. {
  168. int found = 0;
  169. int index;
  170. for (index = 0; found == 0 && index < db->nDb; ++index)
  171. {
  172. struct Db* pDb = &db->aDb[index];
  173. if (strcmp(pDb->zName, zDb) == 0)
  174. {
  175. found = 1;
  176. dbIndex = index;
  177. }
  178. }
  179. if (found == 0) dbIndex = 0;
  180. }
  181. return dbIndex;
  182. }
  183. int sqlite3_key(sqlite3 *db, const void *zKey, int nKey)
  184. {
  185. /* The key is only set for the main database, not the temp database */
  186. return sqlite3_key_v2(db, "main", zKey, nKey);
  187. }
  188. int sqlite3_key_v2(sqlite3 *db, const char *zDbName, const void *zKey, int nKey)
  189. {
  190. /* The key is only set for the main database, not the temp database */
  191. int dbIndex = dbFindIndex(db, zDbName);
  192. return sqlite3CodecAttach(db, dbIndex, zKey, nKey);
  193. }
  194. int sqlite3_rekey_v2(sqlite3 *db, const char *zDbName, const void *zKey, int nKey)
  195. {
  196. /* Changes the encryption key for an existing database. */
  197. int dbIndex = dbFindIndex(db, zDbName);
  198. int rc = SQLITE_ERROR;
  199. Btree* pbt = db->aDb[dbIndex].pBt;
  200. Pager* pPager = sqlite3BtreePager(pbt);
  201. Codec* codec = (Codec*) mySqlite3PagerGetCodec(pPager);
  202. if ((zKey == NULL || nKey == 0) && (codec == NULL || !CodecIsEncrypted(codec)))
  203. {
  204. /*
  205. // Database not encrypted and key not specified
  206. // therefore do nothing
  207. */
  208. return SQLITE_OK;
  209. }
  210. if (codec == NULL || !CodecIsEncrypted(codec))
  211. {
  212. /*
  213. // Database not encrypted, but key specified
  214. // therefore encrypt database
  215. */
  216. if (codec == NULL)
  217. {
  218. codec = (Codec*) sqlite3_malloc(sizeof(Codec));
  219. CodecInit(codec);
  220. }
  221. CodecSetIsEncrypted(codec, 1);
  222. CodecSetHasReadKey(codec, 0); /* Original database is not encrypted */
  223. CodecSetHasWriteKey(codec, 1);
  224. CodecGenerateWriteKey(codec, (char*) zKey, nKey);
  225. CodecSetBtree(codec, pbt);
  226. #if (SQLITE_VERSION_NUMBER >= 3006016)
  227. mySqlite3PagerSetCodec(pPager, sqlite3Codec, sqlite3CodecSizeChange, sqlite3CodecFree, codec);
  228. #else
  229. #if (SQLITE_VERSION_NUMBER >= 3003014)
  230. sqlite3PagerSetCodec(pPager, sqlite3Codec, codec);
  231. #else
  232. sqlite3pager_set_codec(pPager, sqlite3Codec, codec);
  233. #endif
  234. db->aDb[dbIndex].pAux = codec;
  235. db->aDb[dbIndex].xFreeAux = sqlite3CodecFree;
  236. #endif
  237. }
  238. else if (zKey == NULL || nKey == 0)
  239. {
  240. /*
  241. // Database encrypted, but key not specified
  242. // therefore decrypt database
  243. // Keep read key, drop write key
  244. */
  245. CodecSetHasWriteKey(codec, 0);
  246. }
  247. else
  248. {
  249. /*
  250. // Database encrypted and key specified
  251. // therefore re-encrypt database with new key
  252. // Keep read key, change write key to new key
  253. */
  254. CodecGenerateWriteKey(codec, (char*) zKey, nKey);
  255. CodecSetHasWriteKey(codec, 1);
  256. }
  257. sqlite3_mutex_enter(db->mutex);
  258. /* Start transaction */
  259. rc = sqlite3BtreeBeginTrans(pbt, 1);
  260. if (!rc)
  261. {
  262. int pageSize = sqlite3BtreeGetPageSize(pbt);
  263. Pgno nSkip = WX_PAGER_MJ_PGNO(pageSize);
  264. #if (SQLITE_VERSION_NUMBER >= 3003014)
  265. DbPage *pPage;
  266. #else
  267. void *pPage;
  268. #endif
  269. Pgno n;
  270. /* Rewrite all pages using the new encryption key (if specified) */
  271. #if (SQLITE_VERSION_NUMBER >= 3007001)
  272. Pgno nPage;
  273. int nPageCount = -1;
  274. sqlite3PagerPagecount(pPager, &nPageCount);
  275. nPage = nPageCount;
  276. #elif (SQLITE_VERSION_NUMBER >= 3006000)
  277. int nPageCount = -1;
  278. int rc = sqlite3PagerPagecount(pPager, &nPageCount);
  279. Pgno nPage = (Pgno) nPageCount;
  280. #elif (SQLITE_VERSION_NUMBER >= 3003014)
  281. Pgno nPage = sqlite3PagerPagecount(pPager);
  282. #else
  283. Pgno nPage = sqlite3pager_pagecount(pPager);
  284. #endif
  285. for (n = 1; rc == SQLITE_OK && n <= nPage; n++)
  286. {
  287. if (n == nSkip) continue;
  288. #if (SQLITE_VERSION_NUMBER >= 3010000)
  289. rc = sqlite3PagerGet(pPager, n, &pPage, 0);
  290. #elif (SQLITE_VERSION_NUMBER >= 3003014)
  291. rc = sqlite3PagerGet(pPager, n, &pPage);
  292. #else
  293. rc = sqlite3pager_get(pPager, n, &pPage);
  294. #endif
  295. if (!rc)
  296. {
  297. #if (SQLITE_VERSION_NUMBER >= 3003014)
  298. rc = sqlite3PagerWrite(pPage);
  299. sqlite3PagerUnref(pPage);
  300. #else
  301. rc = sqlite3pager_write(pPage);
  302. sqlite3pager_unref(pPage);
  303. #endif
  304. }
  305. }
  306. }
  307. if (rc == SQLITE_OK)
  308. {
  309. /* Commit transaction if all pages could be rewritten */
  310. rc = sqlite3BtreeCommit(pbt);
  311. }
  312. if (rc != SQLITE_OK)
  313. {
  314. /* Rollback in case of error */
  315. #if (SQLITE_VERSION_NUMBER >= 3008007)
  316. /* Unfortunately this change was introduced in version 3.8.7.2 which cannot be detected using the SQLITE_VERSION_NUMBER */
  317. /* That is, compilation will fail for version 3.8.7 or 3.8.7.1 ==> Please change manually ... or upgrade to 3.8.7.2 or higher */
  318. sqlite3BtreeRollback(pbt, SQLITE_OK, 0);
  319. #elif (SQLITE_VERSION_NUMBER >= 3007011)
  320. sqlite3BtreeRollback(pbt, SQLITE_OK);
  321. #else
  322. sqlite3BtreeRollback(pbt);
  323. #endif
  324. }
  325. sqlite3_mutex_leave(db->mutex);
  326. if (rc == SQLITE_OK)
  327. {
  328. /* Set read key equal to write key if necessary */
  329. if (CodecHasWriteKey(codec))
  330. {
  331. CodecCopyKey(codec, 0);
  332. CodecSetHasReadKey(codec, 1);
  333. }
  334. else
  335. {
  336. CodecSetIsEncrypted(codec, 0);
  337. }
  338. }
  339. else
  340. {
  341. /* Restore write key if necessary */
  342. if (CodecHasReadKey(codec))
  343. {
  344. CodecCopyKey(codec, 1);
  345. }
  346. else
  347. {
  348. CodecSetIsEncrypted(codec, 0);
  349. }
  350. }
  351. if (!CodecIsEncrypted(codec))
  352. {
  353. /* Remove codec for unencrypted database */
  354. #if (SQLITE_VERSION_NUMBER >= 3006016)
  355. mySqlite3PagerSetCodec(pPager, NULL, NULL, NULL, NULL);
  356. #else
  357. #if (SQLITE_VERSION_NUMBER >= 3003014)
  358. sqlite3PagerSetCodec(pPager, NULL, NULL);
  359. #else
  360. sqlite3pager_set_codec(pPager, NULL, NULL);
  361. #endif
  362. db->aDb[dbIndex].pAux = NULL;
  363. db->aDb[dbIndex].xFreeAux = NULL;
  364. sqlite3CodecFree(codec);
  365. #endif
  366. }
  367. return rc;
  368. }
  369. int sqlite3_rekey(sqlite3 *db, const void *zKey, int nKey)
  370. {
  371. return sqlite3_rekey_v2(db, "main", zKey, nKey);
  372. }
  373. #endif /* SQLITE_HAS_CODEC */
  374. #endif /* SQLITE_OMIT_DISKIO */