cryptsql.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. # -*- coding: utf-8 -*-
  2. """
  3. # Crypto SQL
  4. # Copyright (c) 2011-2023 Michael Büsch <m@bues.ch>
  5. # Licensed under the GNU/GPL version 2 or later.
  6. """
  7. import functools
  8. import hashlib
  9. import math
  10. import os
  11. import re
  12. import secrets
  13. import sqlite3 as sql
  14. import zlib
  15. from libpwman.aes import AES
  16. from libpwman.argon2 import Argon2
  17. from libpwman.fileobj import FileObj, FileObjCollection, FileObjError
  18. __all__ = [
  19. "CSQLError",
  20. "CryptSQL",
  21. ]
  22. CSQL_HEADER = b"CryptSQL v1"
  23. def decodeInt(buf, error, minValue=None, maxValue=None):
  24. """Decode bytes into a int as decimal representation.
  25. buf: Bytes buffer.
  26. error: Error message string, in case of conversion failure.
  27. minValue: The smallest allowed integer value.
  28. maxValue: The biggest allowed integer value.
  29. """
  30. try:
  31. value = int(buf.decode("UTF-8"), 10)
  32. if minValue is not None and value < minValue:
  33. raise ValueError
  34. if maxValue is not None and value > maxValue:
  35. raise ValueError
  36. return value
  37. except (ValueError, UnicodeError) as e:
  38. raise CSQLError("%s: %s" % (error, buf.decode("UTF-8", "ignore")))
  39. def decodeChoices(buf, error, choices):
  40. """Decode bytes into one of the possible choices strings.
  41. buf: Bytes buffer.
  42. error: Error message string, in case of conversion failure.
  43. choices: An iterable of possible strings.
  44. """
  45. try:
  46. string = buf.decode("UTF-8")
  47. if string not in choices:
  48. raise ValueError
  49. return string
  50. except (ValueError, UnicodeError) as e:
  51. raise CSQLError("%s: %s" % (error, buf.decode("UTF-8", "ignore")))
  52. class CSQLError(Exception):
  53. """CryptSQL exception.
  54. """
  55. class CryptSQLCursor:
  56. """Encrypted SQL database cursor.
  57. """
  58. def __init__(self, db):
  59. self.__db = db
  60. self.__c = db.cursor()
  61. def sqlExec(self, code, params=[]):
  62. """Execute one SQL statement.
  63. """
  64. try:
  65. self.__c.execute(code, params)
  66. return self
  67. except (sql.Error, sql.DatabaseError) as e:
  68. raise CSQLError("Database error: " + str(e))
  69. def sqlExecScript(self, code):
  70. """Execute multiple SQL statements.
  71. Warning: This implicitly commits pending transactions before executing.
  72. """
  73. try:
  74. self.__c.executescript(code)
  75. return self
  76. except (sql.Error, sql.DatabaseError) as e:
  77. raise CSQLError("Database error: " + str(e))
  78. def fetchOne(self):
  79. """Fetches the next row of a query result set.
  80. Returns a list of query results or None.
  81. See sqlite3.Cursor.fetchone for more details.
  82. """
  83. try:
  84. return self.__c.fetchone()
  85. except (sql.Error, sql.DatabaseError) as e:
  86. raise CSQLError("Database error: " + str(e))
  87. def fetchAll(self):
  88. """Fetches all rows of a query result.
  89. Returns a list of lists of query results or an empty list.
  90. See sqlite3.Cursor.fetchall for more details.
  91. """
  92. try:
  93. return self.__c.fetchall()
  94. except (sql.Error, sql.DatabaseError) as e:
  95. raise CSQLError("Database error: " + str(e))
  96. def lastRowID(self):
  97. """Get the rowid of the last modified row.
  98. Returns an int or None.
  99. See sqlite3.Cursor.lastrowid for more details.
  100. """
  101. try:
  102. return self.__c.lastrowid
  103. except (sql.Error, sql.DatabaseError) as e:
  104. raise CSQLError("Database error: " + str(e))
  105. class CryptSQL:
  106. """Encrypted SQL database.
  107. """
  108. # Argon2 KDF parameters.
  109. KDF_SALT_NBYTES = 19
  110. KDF_THREADS = 7
  111. KDF_MEM_BASE = 1024 * 24
  112. KDF_MEM_CHUNK = 4 * KDF_THREADS
  113. DEFAULT_KDF_MEM = int(math.ceil(KDF_MEM_BASE / KDF_MEM_CHUNK)) * KDF_MEM_CHUNK
  114. DEFAULT_KDF_ITER = 163
  115. KDF_MEMLIMIT = DEFAULT_KDF_MEM
  116. KDF_ITERLIMIT_A = lambda kdfMem: int(math.ceil(2500000 / kdfMem))
  117. KDF_ITERLIMIT_B = 2
  118. def __init__(self, readOnly=True):
  119. """readOnly: If True, no commit is possible.
  120. """
  121. self.__readOnly = readOnly
  122. self.__db = None
  123. self.__filename = None
  124. self.__passphrase = None
  125. self.__key = None
  126. def getPassphrase(self):
  127. """Get the current passphrase string for encryption and decryption.
  128. """
  129. try:
  130. return self.__passphrase.decode("UTF-8")
  131. except UnicodeError as e:
  132. raise CSQLError("Cannot UTF-8-decode passphrase.")
  133. def setPassphrase(self, passphrase):
  134. """Set a new passphrase string for encryption and decryption.
  135. """
  136. assert isinstance(passphrase, str),\
  137. "CryptSQL: Passphrase is not 'str'."
  138. try:
  139. self.__key = None
  140. self.__passphrase = passphrase.encode("UTF-8")
  141. except UnicodeError as e:
  142. raise CSQLError("Cannot UTF-8-encode passphrase.")
  143. def getKey(self):
  144. """Get the raw key. May be None, if there is none, yet.
  145. Do not use this. getPassphrase probably is what you want.
  146. """
  147. return self.__key
  148. def setKey(self, key):
  149. """Set the raw key.
  150. Do not use this. setPassphrase probably is what you want.
  151. """
  152. self.__key = key
  153. def getFilename(self):
  154. """Get the file path of the currently open database.
  155. May return None, if no database file is opened.
  156. """
  157. return self.__filename
  158. def __parseFile(self, filename):
  159. """Read all data from 'filename' and decrypt it into memory.
  160. """
  161. try:
  162. fc = FileObjCollection.parseFile(filename)
  163. if fc is None:
  164. return
  165. # Get the file fields.
  166. head = fc.get(
  167. name=b"HEAD",
  168. error="Missing file header object",
  169. )
  170. if head != CSQL_HEADER:
  171. raise CSQLError("Invalid file header")
  172. cipher = fc.get(
  173. name=b"CIPHER",
  174. error="Missing CIPHER header object",
  175. )
  176. cipherMode = fc.get(
  177. name=b"CIPHER_MODE",
  178. error="Missing CIPHER_MODE header object",
  179. )
  180. cipherIV = fc.get(
  181. name=b"CIPHER_IV",
  182. error="Missing CIPHER_IV header object",
  183. )
  184. keyLen = fc.get(
  185. name=b"KEY_LEN",
  186. error="Missing KEY_LEN header object",
  187. )
  188. kdfMethod = fc.get(
  189. name=b"KDF_METHOD",
  190. error="Missing KDF_METHOD header object",
  191. )
  192. kdfSalt = fc.get(
  193. name=b"KDF_SALT",
  194. error="Missing KDF_SALT header object",
  195. )
  196. kdfIter = fc.get(
  197. name=b"KDF_ITER",
  198. error="Missing KDF_ITER header object",
  199. )
  200. if kdfMethod == b"PBKDF2":
  201. kdfHash = fc.get(
  202. name=b"KDF_HASH",
  203. error="Missing KDF_HASH header object",
  204. )
  205. kdfMac = fc.get(
  206. name=b"KDF_MAC",
  207. error="Missing KDF_MAC header object",
  208. )
  209. elif kdfMethod == b"ARGON2":
  210. kdfType = fc.get(
  211. name=b"KDF_TYPE",
  212. error="Missing KDF_TYPE header object",
  213. )
  214. kdfVer = fc.get(
  215. name=b"KDF_VER",
  216. error="Missing KDF_VER header object",
  217. )
  218. kdfPar = fc.get(
  219. name=b"KDF_PAR",
  220. error="Missing KDF_PAR header object",
  221. )
  222. kdfMem = fc.get(
  223. name=b"KDF_MEM",
  224. error="Missing KDF_MEM header object",
  225. )
  226. compress = fc.get(
  227. name=b"COMPRESS",
  228. default=b"NONE",
  229. )
  230. paddingMethod = fc.get(
  231. name=b"PADDING",
  232. default=b"PWMAN",
  233. )
  234. payload = fc.get(
  235. name=b"PAYLOAD",
  236. error="Missing PAYLOAD object",
  237. )
  238. # Check payload.
  239. if len(payload) < 1:
  240. raise CSQLError("Invalid PAYLOAD length: %d" % (
  241. len(payload)))
  242. # Check the padding method.
  243. paddingMethod = decodeChoices(
  244. buf=paddingMethod,
  245. choices=("PWMAN", "PKCS7"),
  246. error="Unknown padding method header",
  247. )
  248. # Check the cipher.
  249. cipher = decodeChoices(
  250. buf=cipher,
  251. choices=("AES",),
  252. error="Unknown CIPHER header value",
  253. )
  254. cipherMode = decodeChoices(
  255. buf=cipherMode,
  256. choices=("CBC",),
  257. error="Unknown CIPHER_MODE header value",
  258. )
  259. cipherBlockSize = AES.BLOCK_SIZE
  260. # Check the cipher IV.
  261. if len(cipherIV) != cipherBlockSize:
  262. raise CSQLError("Invalid CIPHER_IV header length: %d" % (
  263. len(cipherIV)))
  264. # Check the cipher key length.
  265. keyLen = decodeChoices(
  266. buf=keyLen,
  267. choices=("256",),
  268. error="Unknown KEY_LEN header value",
  269. )
  270. keyLen = int(keyLen) // 8
  271. # Check the key derivation function salt.
  272. if len(kdfSalt) < 16:
  273. raise CSQLError("Invalid KDF_SALT header length: %d" % (
  274. len(kdfSalt)))
  275. # Check the key derivation function iterations.
  276. kdfIter = decodeInt(
  277. buf=kdfIter,
  278. minValue=1,
  279. maxValue=((1 << 32) - 1),
  280. error="Invalid KDF_ITER header value",
  281. )
  282. # Check the key derivation function.
  283. kdfMethod = decodeChoices(
  284. buf=kdfMethod,
  285. choices=("PBKDF2", "ARGON2"),
  286. error="Unknown KDF_METHOD header value",
  287. )
  288. if kdfMethod == "PBKDF2":
  289. kdfHash = decodeChoices(
  290. buf=kdfHash,
  291. choices=("SHA256", "SHA512", "SHA3-512"),
  292. error="Unknown KDF_HASH header value",
  293. )
  294. kdfMac = decodeChoices(
  295. buf=kdfMac,
  296. choices=("HMAC",),
  297. error="Unknown KDF_MAC header value",
  298. )
  299. kdf = lambda: hashlib.pbkdf2_hmac(
  300. hash_name=kdfHash,
  301. password=self.__passphrase,
  302. salt=kdfSalt,
  303. iterations=kdfIter,
  304. dklen=keyLen,
  305. )
  306. elif kdfMethod == "ARGON2":
  307. kdfType = decodeChoices(
  308. buf=kdfType,
  309. choices=("ID",),
  310. error="Unknown KDF_TYPE header value",
  311. )
  312. kdfVer = decodeChoices(
  313. buf=kdfVer,
  314. choices=(str(0x13), ),
  315. error="Unknown KDF_VER header value",
  316. )
  317. kdfPar = decodeInt(
  318. buf=kdfPar,
  319. minValue=1,
  320. maxValue=((1 << 24) - 1),
  321. error="Invalid KDF_PAR header value",
  322. )
  323. kdfMem = decodeInt(
  324. buf=kdfMem,
  325. minValue=(8 * kdfPar),
  326. maxValue=((1 << 32) - 1),
  327. error="Invalid KDF_MEM header value",
  328. )
  329. kdf = lambda: Argon2.get().argon2id_v1p3(
  330. passphrase=self.__passphrase,
  331. salt=kdfSalt,
  332. timeCost=kdfIter,
  333. memCost=kdfMem,
  334. parallel=kdfPar,
  335. keyLen=keyLen,
  336. )
  337. else:
  338. assert False
  339. # Check the compression method.
  340. compress = decodeChoices(
  341. buf=compress,
  342. choices=("NONE", "ZLIB"),
  343. error="Unknown COMPRESS header value",
  344. )
  345. try:
  346. # Generate the key.
  347. key = kdf() if self.__key is None else self.__key
  348. # Decrypt the payload.
  349. payload = AES.get().decrypt(
  350. key=key,
  351. iv=cipherIV,
  352. data=payload,
  353. legacyPadding=(paddingMethod == "PWMAN"))
  354. # Decompress the payload (legacy).
  355. if compress == "ZLIB":
  356. payload = zlib.decompress(payload)
  357. # Import the SQL database.
  358. self.importSqlScript(payload.decode("UTF-8"))
  359. # Store the raw key.
  360. self.__key = key
  361. except Exception as e:
  362. raise CSQLError("Failed to decrypt database. "
  363. "Wrong passphrase?")
  364. except FileObjError as e:
  365. raise CSQLError("Database file error: %s" % str(e))
  366. def isOpen(self):
  367. """Returns True, if a database file is opened.
  368. """
  369. return self.__db is not None
  370. def open(self, filename):
  371. """Open a database file and decrypt its contents into memory.
  372. filename: The database file path.
  373. """
  374. if self.isOpen():
  375. raise CSQLError("A database is already open")
  376. self.__db = sql.connect(":memory:")
  377. self.__db.text_factory = str
  378. self.setRegexpFlags()
  379. self.sqlCreateFunction("regexp", 2, self._sqlRegexpMatch)
  380. try:
  381. self.__parseFile(filename)
  382. except CSQLError as e:
  383. self.close()
  384. raise e
  385. self.__filename = filename
  386. def close(self):
  387. """Close the currently opened database.
  388. This does not commit. All uncommitted changes are lost.
  389. """
  390. self.__db = None
  391. self.__filename = None
  392. self.__passphrase = None
  393. def __random(self, nrBytes):
  394. """Return cryptographically secure random bytes.
  395. nrBytes: The number of bytes to return.
  396. """
  397. if nrBytes <= 0:
  398. raise CSQLError("__random(): Invalid number of random bytes.")
  399. data = secrets.token_bytes(nrBytes)
  400. if len(data) != nrBytes:
  401. raise CSQLError("__random(): Sanity check failed (length).")
  402. if functools.reduce(lambda a, b: a | b, data) == 0:
  403. raise CSQLError("__random(): Sanity check failed (zero).")
  404. if functools.reduce(lambda a, b: a & b, data) == 0xFF:
  405. raise CSQLError("__random(): Sanity check failed (ones).")
  406. return data
  407. def dropUncommitted(self):
  408. """Drop all changes that are not committed, yet.
  409. """
  410. self.__db.rollback()
  411. def commit(self):
  412. """Write all changes to the encrypted database file.
  413. """
  414. cls = self.__class__
  415. if self.__readOnly:
  416. raise CSQLError("The database is read-only. "
  417. "Cannot commit changes.")
  418. if not self.__db or not self.__filename:
  419. raise CSQLError("Database is not open")
  420. self.sqlVacuum()
  421. # Dump the database
  422. payload = self.sqlPlainDump()
  423. # Get the KDF parameters.
  424. kdfSalt = self.__random(cls.KDF_SALT_NBYTES)
  425. kdfMem = cls.DEFAULT_KDF_MEM
  426. kdfMemUser = os.getenv("PWMAN_ARGON2MEM", "").lower().strip()
  427. if kdfMemUser:
  428. try:
  429. kdfMem = int(kdfMemUser, 10)
  430. except ValueError:
  431. raise CSQLError("The value of the environment variable "
  432. "PWMAN_ARGON2MEM is invalid.")
  433. kdfMem = max(kdfMem, cls.KDF_MEMLIMIT)
  434. kdfIter = cls.DEFAULT_KDF_ITER
  435. kdfIterUser = os.getenv("PWMAN_ARGON2TIME", "").lower().strip()
  436. if kdfIterUser:
  437. try:
  438. kdfIter = int(kdfIterUser, 10)
  439. except ValueError:
  440. raise CSQLError("The value of the environment variable "
  441. "PWMAN_ARGON2TIME is invalid.")
  442. kdfIter = max(kdfIter, cls.KDF_ITERLIMIT_A(kdfMem))
  443. kdfIter = max(kdfIter, cls.KDF_ITERLIMIT_B)
  444. kdfPar = cls.KDF_THREADS
  445. keyLen = 256 // 8
  446. try:
  447. # Generate the key.
  448. key = Argon2.get().argon2id_v1p3(
  449. passphrase=self.__passphrase,
  450. salt=kdfSalt,
  451. timeCost=kdfIter,
  452. memCost=kdfMem,
  453. parallel=kdfPar,
  454. keyLen=keyLen,
  455. )
  456. # Encrypt payload
  457. cipherIV = self.__random(AES.BLOCK_SIZE)
  458. payload = AES.get().encrypt(key=key, iv=cipherIV, data=payload)
  459. except Exception as e:
  460. raise CSQLError("Failed to encrypt: %s" % str(e))
  461. try:
  462. # Assemble file objects
  463. fc = FileObjCollection((
  464. FileObj(b"HEAD", CSQL_HEADER),
  465. FileObj(b"CIPHER", b"AES"),
  466. FileObj(b"CIPHER_MODE", b"CBC"),
  467. FileObj(b"CIPHER_IV", cipherIV),
  468. FileObj(b"KEY_LEN", str(keyLen * 8).encode("UTF-8")),
  469. FileObj(b"KDF_METHOD", b"ARGON2"),
  470. FileObj(b"KDF_TYPE", b"ID"),
  471. FileObj(b"KDF_VER", str(0x13).encode("UTF-8")),
  472. FileObj(b"KDF_SALT", kdfSalt),
  473. FileObj(b"KDF_ITER", str(kdfIter).encode("UTF-8")),
  474. FileObj(b"KDF_MEM", str(kdfMem).encode("UTF-8")),
  475. FileObj(b"KDF_PAR", str(kdfPar).encode("UTF-8")),
  476. FileObj(b"PADDING", b"PKCS7"),
  477. FileObj(b"PAYLOAD", payload),
  478. ))
  479. # Write to the file
  480. self.__key = None
  481. fc.writeFile(self.__filename)
  482. self.__key = key
  483. except FileObjError as e:
  484. raise CSQLError("File error: %s" % str(e))
  485. def setRegexpFlags(self, search=True, ignoreCase=True, multiLine=True, dotAll=True):
  486. """Change the behavior of the REGEXP operator.
  487. """
  488. if search:
  489. self._regexpMatch = re.search
  490. else:
  491. self._regexpMatch = re.match
  492. self._regexpFlags = 0
  493. if ignoreCase:
  494. self._regexpFlags |= re.IGNORECASE
  495. if multiLine:
  496. self._regexpFlags |= re.MULTILINE
  497. if dotAll:
  498. self._regexpFlags |= re.DOTALL
  499. def _sqlRegexpMatch(self, pattern, string):
  500. """Default implementation of the REGEXP operator.
  501. """
  502. return 0 if self._regexpMatch(pattern,
  503. string,
  504. self._regexpFlags) is None else 1
  505. def sqlVacuum(self):
  506. """Run the SQL VACUUM statement.
  507. This also commits all changes to the SQL database,
  508. but not to the database file.
  509. """
  510. self.__db.commit()
  511. self.sqlExec("VACUUM;")
  512. self.__db.commit()
  513. def sqlExec(self, code, params=[]):
  514. """Execute one SQL statement.
  515. """
  516. return CryptSQLCursor(self.__db).sqlExec(code, params)
  517. def sqlExecScript(self, code):
  518. """Execute multiple SQL statements.
  519. Warning: This implicitly commits pending transactions before executing.
  520. """
  521. return CryptSQLCursor(self.__db).sqlExecScript(code)
  522. def sqlCreateFunction(self, name, nrParams, func):
  523. """Create an SQL function.
  524. See sqlite3.Connection.create_function for more details.
  525. """
  526. self.__db.create_function(name, nrParams, func)
  527. def sqlIsEmpty(self):
  528. """Returns True, if the database does not contain any tables.
  529. """
  530. c = self.sqlExec("ANALYZE;")
  531. tbl = c.sqlExec("SELECT tbl FROM sqlite_stat1;").fetchOne()
  532. return not bool(tbl)
  533. def sqlPlainDump(self):
  534. """Get a plain text dump of the database.
  535. Returns bytes.
  536. """
  537. return ("\n".join(self.__db.iterdump())).encode("UTF-8")
  538. def importSqlScript(self, script, clear=True):
  539. """Imports a plain text dump into the database.
  540. script: The script string to import.
  541. clear: If True, drop all tables from the database before importing.
  542. """
  543. if clear:
  544. self.dropAllTables()
  545. self.sqlExecScript(script)
  546. def dropAllTables(self):
  547. """Drop all tables from the database.
  548. """
  549. c = self.sqlExec("SELECT name FROM sqlite_master "
  550. "WHERE type='table';")
  551. for table in c.fetchAll():
  552. table = table[0]
  553. if table != "sqlite_sequence":
  554. self.sqlExec("DROP TABLE %s" % table)