sqliterk_crypto.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * WCDB available.
  4. *
  5. * Copyright (C) 2017 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "sqliterk_crypto.h"
  21. #include "SQLiteRepairKit.h"
  22. #include "sqliterk_os.h"
  23. #include "sqliterk_pager.h"
  24. #ifdef WCDB_BUILTIN_SQLCIPHER
  25. #include <sqlcipher/sqlite3.h>
  26. #else //WCDB_BUILTIN_SQLCIPHER
  27. #include <sqlite3.h>
  28. #endif //WCDB_BUILTIN_SQLCIPHER
  29. #include <string.h>
  30. // Declarations by SQLCipher.
  31. #define CIPHER_DECRYPT 0
  32. #define CIPHER_ENCRYPT 1
  33. #define CIPHER_READ_CTX 0
  34. #define CIPHER_WRITE_CTX 1
  35. #define CIPHER_READWRITE_CTX 2
  36. /* Extensions defined in crypto_impl.c */
  37. typedef struct codec_ctx codec_ctx;
  38. /* Activation and initialization */
  39. void sqlcipher_activate(void);
  40. void sqlcipher_deactivate(void);
  41. int sqlcipher_codec_ctx_init(
  42. codec_ctx **, void *, void *, void *, const void *, int);
  43. void sqlcipher_codec_ctx_free(codec_ctx **);
  44. int sqlcipher_codec_key_derive(codec_ctx *);
  45. int sqlcipher_codec_key_copy(codec_ctx *, int);
  46. /* Page cipher implementation */
  47. int sqlcipher_page_cipher(
  48. codec_ctx *, int, int, int, int, unsigned char *, unsigned char *);
  49. /* Context setters & getters */
  50. //void sqlcipher_codec_ctx_set_error(codec_ctx *, int);
  51. int sqlcipher_codec_ctx_set_pass(codec_ctx *, const void *, int, int);
  52. void sqlcipher_codec_get_keyspec(codec_ctx *, void **zKey, int *nKey);
  53. int sqlcipher_codec_ctx_set_pagesize(codec_ctx *, int);
  54. int sqlcipher_codec_ctx_get_pagesize(codec_ctx *);
  55. int sqlcipher_codec_ctx_get_reservesize(codec_ctx *);
  56. void sqlcipher_set_default_pagesize(int page_size);
  57. int sqlcipher_get_default_pagesize(void);
  58. void sqlcipher_set_default_kdf_iter(int iter);
  59. int sqlcipher_get_default_kdf_iter(void);
  60. int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int);
  61. int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int);
  62. void *sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx);
  63. int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int, int);
  64. int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *, int);
  65. int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *, int);
  66. const char *sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx);
  67. void *sqlcipher_codec_ctx_get_data(codec_ctx *);
  68. //void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
  69. void sqlcipher_set_default_use_hmac(int use);
  70. int sqlcipher_get_default_use_hmac(void);
  71. void sqlcipher_set_hmac_salt_mask(unsigned char mask);
  72. unsigned char sqlcipher_get_hmac_salt_mask(void);
  73. int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use);
  74. int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx);
  75. int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
  76. int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
  77. int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx,
  78. unsigned int flag,
  79. int for_ctx);
  80. const char *sqlcipher_codec_get_cipher_provider(codec_ctx *ctx);
  81. //int sqlcipher_codec_ctx_migrate(codec_ctx *ctx);
  82. int sqlcipher_codec_add_random(codec_ctx *ctx, const char *data, int random_sz);
  83. int sqlcipher_cipher_profile(sqlite3 *db, const char *destination);
  84. //static void sqlcipher_profile_callback(void *file, const char *sql, sqlite3_uint64 run_time);
  85. //static int sqlcipher_codec_get_store_pass(codec_ctx *ctx);
  86. //static void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey);
  87. //static void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value);
  88. int sqlcipher_codec_fips_status(codec_ctx *ctx);
  89. const char *sqlcipher_codec_get_provider_version(codec_ctx *ctx);
  90. // sqlite3_file redirector
  91. typedef struct {
  92. const struct sqlite3_io_methods *pMethods;
  93. sqliterk_file *fd;
  94. const unsigned char *kdf_salt;
  95. } sqlite3_file_rkredir;
  96. int sqliterkRead(sqlite3_file *fd, void *data, int iAmt, sqlite3_int64 iOfst)
  97. {
  98. sqlite3_file_rkredir *rkos = (sqlite3_file_rkredir *) fd;
  99. if (rkos->kdf_salt) {
  100. memcpy(data, rkos->kdf_salt, (iAmt > 16) ? 16 : iAmt);
  101. return SQLITE_OK;
  102. } else {
  103. sqliterk_file *f = rkos->fd;
  104. size_t size = iAmt;
  105. return sqliterkOSRead(f, (off_t) iOfst, data, &size);
  106. }
  107. }
  108. int sqliterkCryptoSetCipher(sqliterk_pager *pager,
  109. sqliterk_file *fd,
  110. const sqliterk_cipher_conf *conf)
  111. {
  112. codec_ctx *codec = NULL;
  113. int rc;
  114. if (conf) {
  115. // Check arguments.
  116. if (!conf->key || conf->key_len <= 0)
  117. return SQLITERK_MISUSE;
  118. // SQLite library must be initialized before calling sqlcipher_activate(),
  119. // or it will cause a deadlock.
  120. sqlite3_initialize();
  121. sqlcipher_activate();
  122. // XXX: fake BTree structure passed to sqlcipher_codec_ctx_init.
  123. // Member of such structure is assigned but never used by repair kit.
  124. int fake_db[8];
  125. sqlite3_file_rkredir file;
  126. struct sqlite3_io_methods methods = {0};
  127. methods.xRead = sqliterkRead;
  128. file.pMethods = &methods;
  129. file.fd = fd;
  130. file.kdf_salt = conf->kdf_salt;
  131. // Initialize codec context.
  132. rc = sqlcipher_codec_ctx_init(&codec, fake_db, NULL, &file, conf->key,
  133. conf->key_len);
  134. if (rc != SQLITE_OK)
  135. goto bail_sqlite_errstr;
  136. // Set cipher.
  137. if (conf->cipher_name) {
  138. rc = sqlcipher_codec_ctx_set_cipher(codec, conf->cipher_name,
  139. CIPHER_READWRITE_CTX);
  140. if (rc != SQLITE_OK)
  141. goto bail_sqlite_errstr;
  142. }
  143. // Set page size.
  144. if (conf->page_size > 0) {
  145. rc = sqlcipher_codec_ctx_set_pagesize(codec, conf->page_size);
  146. if (rc != SQLITE_OK)
  147. goto bail_sqlite_errstr;
  148. }
  149. // Set HMAC usage.
  150. if (conf->use_hmac >= 0) {
  151. rc = sqlcipher_codec_ctx_set_use_hmac(codec, conf->use_hmac);
  152. if (rc != SQLITE_OK)
  153. goto bail_sqlite_errstr;
  154. }
  155. // Set KDF Iteration.
  156. if (conf->kdf_iter > 0) {
  157. rc = sqlcipher_codec_ctx_set_kdf_iter(codec, conf->kdf_iter,
  158. CIPHER_READWRITE_CTX);
  159. if (rc != SQLITE_OK)
  160. goto bail;
  161. }
  162. // Update pager page size.
  163. int page_sz = sqlcipher_codec_ctx_get_pagesize(codec);
  164. int reserve_sz = sqlcipher_codec_ctx_get_reservesize(codec);
  165. pager->pagesize = page_sz;
  166. pager->reservedBytes = reserve_sz;
  167. }
  168. if (pager->codec) {
  169. sqlcipher_codec_ctx_free(&pager->codec);
  170. sqlcipher_deactivate();
  171. }
  172. pager->codec = codec;
  173. return SQLITERK_OK;
  174. bail_sqlite_errstr:
  175. sqliterkOSError(SQLITERK_CANTOPEN,
  176. "Failed to initialize cipher context: %s",
  177. sqlite3_errstr(rc));
  178. rc = SQLITERK_CANTOPEN;
  179. bail:
  180. if (codec)
  181. sqlcipher_codec_ctx_free(&codec);
  182. sqlcipher_deactivate();
  183. return rc;
  184. }
  185. void sqliterkCryptoFreeCodec(sqliterk_pager *pager)
  186. {
  187. if (!pager->codec)
  188. return;
  189. sqlcipher_codec_ctx_free(&pager->codec);
  190. sqlcipher_deactivate();
  191. }
  192. int sqliterkCryptoDecode(sqliterk_codec *codec, int pgno, void *data)
  193. {
  194. int rc;
  195. int offset = 0;
  196. unsigned char *pdata = (unsigned char *) data;
  197. int page_sz = sqlcipher_codec_ctx_get_pagesize(codec);
  198. unsigned char *buffer =
  199. (unsigned char *) sqlcipher_codec_ctx_get_data(codec);
  200. rc = sqlcipher_codec_key_derive(codec);
  201. if (rc != SQLITE_OK)
  202. return rc;
  203. if (pgno == 1) {
  204. offset = 16; // FILE_HEADER_SZ
  205. memcpy(buffer, "SQLite format 3", 16);
  206. }
  207. rc = sqlcipher_page_cipher(codec, CIPHER_READ_CTX, pgno, CIPHER_DECRYPT,
  208. page_sz - offset, pdata + offset,
  209. buffer + offset);
  210. if (rc != SQLITE_OK)
  211. goto bail;
  212. memcpy(pdata, buffer, page_sz);
  213. return SQLITERK_OK;
  214. bail:
  215. sqliterkOSError(SQLITERK_DAMAGED, "Failed to decode page %d: %s", pgno,
  216. sqlite3_errstr(rc));
  217. return rc;
  218. }