cryptsql.py 17 KB

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