Compare commits

..

No commits in common. "main" and "v0.1.0" have entirely different histories.
main ... v0.1.0

10 changed files with 62 additions and 89 deletions

View file

@ -1,5 +1,5 @@
(lock (lock
(locked (locked
(package (name (bjosodium)) (version "0.2.0") (package (name (bjosodium)) (version "0.1.0")
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium"))) (source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))
(commit "2be75d9b22691b44d571ef4fa8516390ca9a47a4d64a3d92b39d6f9878627b13")))) (commit "911f4bcc4676ca6393f8eb43286fdddd69cf6b5665aca0770ceaf869fb73767b"))))

View file

@ -1,12 +1,12 @@
(package (package
(name (bjomarks)) (name (bjomarks))
(version "0.2.1") (version "0.1.0")
(description "Bookmarks in an encrypted store: one file for each device, in any folder that a file syncer or WebDAV shares.") (description "Bookmarks in an encrypted store: one file for each device, in any folder that a file syncer or WebDAV shares.")
(license "MPL-2.0") (license "MPL-2.0")
(depends (depends
(package (name (bjosodium)) (package (name (bjosodium))
(version (version-at-least "0.2.0")) (version (version-at-least "0.1.0"))
(source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium"))))) (source (git (url "https://rikspucko.koketteriet.se/bjoli/bjosodium")))))
;; A library: there is no src/main.bjo. ;; A library: there is no src/main.bjo.

View file

@ -169,7 +169,7 @@
(when (>= i 0) (when (>= i 0)
(set! out i (get hex-digits (cast int (bitwise-and rest 15L)))) (set! out i (get hex-digits (cast int (bitwise-and rest 15L))))
(go (- i 1) (shift-right rest 4)))) (go (- i 1) (shift-right rest 4))))
(bytes->string/lossy out)) (bytes->string out))
;; The value of the lower-case hex digits of `bytes` from `start` to `end`. ;; The value of the lower-case hex digits of `bytes` from `start` to `end`.
;; `None` for any other byte. ;; `None` for any other byte.

View file

@ -5,8 +5,8 @@
;; bjomarks/src/keys.bjo — the module (bjomarks keys): `keys.bin`, the file ;; bjomarks/src/keys.bjo — the module (bjomarks keys): `keys.bin`, the file
;; that holds the key of a vault, encrypted with keys made from passwords. ;; that holds the key of a vault, encrypted with keys made from passwords.
;; ;;
;; - A vault has one data key (DEK): a random key of 32 bytes that encrypts ;; - A vault has one data key (DEK): 32 random bytes that encrypt every
;; every device's file. ;; device's file.
;; - `keys.bin` holds the DEK wrapped (encrypted) in one or more slots. A ;; - `keys.bin` holds the DEK wrapped (encrypted) in one or more slots. A
;; password slot wraps it with a key (KEK) that Argon2id makes from the ;; password slot wraps it with a key (KEK) that Argon2id makes from the
;; password and the slot's salt and cost. ;; password and the slot's salt and cost.
@ -65,7 +65,6 @@
(import (std prelude)) (import (std prelude))
(import (std uuid)) (import (std uuid))
(import (bjosodium core)) (import (bjosodium core))
(import (bjosodium raw))
(import (bjomarks binary)) (import (bjomarks binary))
(: string) (: string)
@ -84,8 +83,8 @@
;; each with its key id, the latest first. ;; each with its key id, the latest first.
(type (: Unlocked (Record (: vault-id Uuid) (type (: Unlocked (Record (: vault-id Uuid)
(: key-id int) (: key-id int)
(: dek AeadKey) (: dek (Array byte))
(: earlier (Vec (Tuple int AeadKey)))))) (: earlier (Vec (Tuple int (Array byte)))))))
;; The key ids of a keys file, which it shows without a password: the ;; The key ids of a keys file, which it shows without a password: the
;; current one, and the earlier ones, the latest first. ;; current one, and the earlier ones, the latest first.
@ -132,33 +131,14 @@
(: int) (: int)
(def max-earlier-keys 200) (def max-earlier-keys 200)
;; The Argon2id cost of `cost`. Raises `ArgumentException` when it is out of
;; libsodium's range.
(: (-> KeyCost Argon2idCost))
(defun (argon2id-cost cost) (make-argon2id-cost (.iterations cost) (.memory-kib cost)))
;; `key`, encrypted with `kek` and `nonce`. The copy of its bytes is wiped.
(: (-> AeadKey AeadNonce AeadKey (Array byte) (Array byte)))
(defun (wrap-key kek nonce key aad)
(def plain (aead-key->bytes key))
(try (aead-encrypt kek nonce plain #:aad aad) #:finally (wipe! plain)))
;; The key of the decrypted bytes `plain`, which are `dek-length` long. The
;; bytes are wiped.
(: (-> (Array byte) AeadKey))
(defun (take-key plain)
(def key (bytes->aead-key plain))
(wipe! plain)
key)
(: (-> Cipher int)) (: (-> Cipher int))
(defun (slot-length cipher) #fl(+ slot-head-length (cipher-nonce-length cipher) dek-length (cipher-tag-length cipher))) (defun (slot-length cipher) #fl(+ slot-head-length (cipher-nonce-length cipher) dek-length (cipher-tag-length cipher)))
;; The DEK, wrapped for `password` in a new slot, as the whole of a keys ;; The DEK, wrapped for `password` in a new slot, as the whole of a keys
;; file with that one password slot, and a slot for each of the `#:earlier` ;; file with that one password slot, and a slot for each of the `#:earlier`
;; keys, wrapped with the DEK. ;; keys, wrapped with the DEK.
(: (-> Uuid int AeadKey string (#:cost KeyCost) (#:cipher Cipher) (: (-> Uuid int (Array byte) string (#:cost KeyCost) (#:cipher Cipher)
(#:earlier (Vec (Tuple int AeadKey))) (#:earlier (Vec (Tuple int (Array byte))))
(Array byte))) (Array byte)))
(defun (wrap-keys vault-id key-id dek password #:cost default-key-cost #:cipher (preferred-cipher) (defun (wrap-keys vault-id key-id dek password #:cost default-key-cost #:cipher (preferred-cipher)
#:earlier []) #:earlier [])
@ -179,10 +159,10 @@
(put-u32! file (+ at 10) (.memory-kib cost)) (put-u32! file (+ at 10) (.memory-kib cost))
(put-bytes! file (+ at 14) salt) (put-bytes! file (+ at 14) salt)
(put-u8! file (+ at 30) (cipher->code cipher)) (put-u8! file (+ at 30) (cipher->code cipher))
(put-bytes! file (+ at slot-head-length) (aead-nonce->bytes nonce)) (put-bytes! file (+ at slot-head-length) nonce)
(def aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length))) (def aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length)))
(def kek (argon2id-derive-key password salt (argon2id-cost cost))) (def kek (argon2id-derive-key password salt #:iterations (.iterations cost) #:memory-kib (.memory-kib cost)))
(def wrapped (try (wrap-key kek nonce dek aad) #:finally (wipe-aead-key! kek))) (def wrapped (try (aead-encrypt cipher kek nonce dek #:aad aad) #:finally (wipe! kek)))
(put-bytes! file (+ at slot-head-length nonce-length) wrapped) (put-bytes! file (+ at slot-head-length nonce-length) wrapped)
(loop (:for (Tuple i old) (enumerate kept)) (loop (:for (Tuple i old) (enumerate kept))
(:do (def (Tuple old-id old-dek) old) (:do (def (Tuple old-id old-dek) old)
@ -193,10 +173,10 @@
(put-u8! file (+ there 5) 0) (put-u8! file (+ there 5) 0)
(put-u32! file (+ there 6) key-id) (put-u32! file (+ there 6) key-id)
(put-u8! file (+ there 30) (cipher->code cipher)) (put-u8! file (+ there 30) (cipher->code cipher))
(put-bytes! file (+ there slot-head-length) (aead-nonce->bytes old-nonce)) (put-bytes! file (+ there slot-head-length) old-nonce)
(def old-aad (bytes-concat (get-bytes file 0 22) (get-bytes file there slot-head-length))) (def old-aad (bytes-concat (get-bytes file 0 22) (get-bytes file there slot-head-length)))
(put-bytes! file (+ there slot-head-length nonce-length) (put-bytes! file (+ there slot-head-length nonce-length)
(wrap-key dek old-nonce old-dek old-aad)))) (aead-encrypt cipher dek old-nonce old-dek #:aad old-aad))))
file) file)
;; A new vault: a new vault id and a new DEK, with key id 1, and its keys ;; A new vault: a new vault id and a new DEK, with key id 1, and its keys
@ -204,7 +184,7 @@
(: (-> string (#:cost KeyCost) (#:cipher Cipher) (Tuple (Array byte) Unlocked))) (: (-> string (#:cost KeyCost) (#:cipher Cipher) (Tuple (Array byte) Unlocked)))
(defun (make-keys password #:cost default-key-cost #:cipher (preferred-cipher)) (defun (make-keys password #:cost default-key-cost #:cipher (preferred-cipher))
(def vault-id (uuid-v4)) (def vault-id (uuid-v4))
(def dek (make-aead-key)) (def dek (make-aead-key XChaCha20Poly1305))
(Tuple (wrap-keys vault-id 1 dek password #:cost cost #:cipher cipher) (Tuple (wrap-keys vault-id 1 dek password #:cost cost #:cipher cipher)
(Unlocked (vault-id vault-id) (key-id 1) (dek dek) (earlier [])))) (Unlocked (vault-id vault-id) (key-id 1) (dek dek) (earlier []))))
@ -212,7 +192,7 @@
;; a new key at once do not give theirs the same id. ;; a new key at once do not give theirs the same id.
(: (-> (Vec int) int)) (: (-> (Vec int) int))
(defun (new-key-id known) (defun (new-key-id known)
(def id (get-u32 (random-bytes 4) 0)) (def id (get-u32 (make-aead-key XChaCha20Poly1305) 0))
(if (or (<= id 1) (vec-contains? known id)) (new-key-id known) id)) (if (or (<= id 1) (vec-contains? known id)) (new-key-id known) id))
;; A new DEK for the vault of `u`, for a new password: its keys file, and ;; A new DEK for the vault of `u`, for a new password: its keys file, and
@ -222,7 +202,7 @@
(defun (new-keys u password #:cost default-key-cost #:cipher (preferred-cipher)) (defun (new-keys u password #:cost default-key-cost #:cipher (preferred-cipher))
(def earlier (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u))) (def earlier (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u)))
(def key-id (new-key-id (vec-map fst earlier))) (def key-id (new-key-id (vec-map fst earlier)))
(def dek (make-aead-key)) (def dek (make-aead-key XChaCha20Poly1305))
(def kept (vec-take earlier max-earlier-keys)) (def kept (vec-take earlier max-earlier-keys))
(Tuple (wrap-keys (.vault-id u) key-id dek password #:cost cost #:cipher cipher #:earlier kept) (Tuple (wrap-keys (.vault-id u) key-id dek password #:cost cost #:cipher cipher #:earlier kept)
(Unlocked (vault-id (.vault-id u)) (key-id key-id) (dek dek) (earlier kept)))) (Unlocked (vault-id (.vault-id u)) (key-id key-id) (dek dek) (earlier kept))))
@ -249,7 +229,7 @@
(: salt (Array byte)) (: salt (Array byte))
(: cipher Cipher) (: cipher Cipher)
(: aad (Array byte)) (: aad (Array byte))
(: nonce AeadNonce) (: nonce (Array byte))
(: wrapped (Array byte))))) (: wrapped (Array byte)))))
;; The slot at `at`, and where the next one starts, or why it is not a slot. ;; The slot at `at`, and where the next one starts, or why it is not a slot.
@ -271,7 +251,7 @@
(salt (get-bytes file (+ at 14) 16)) (salt (get-bytes file (+ at 14) 16))
(cipher cipher) (cipher cipher)
(aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length))) (aad (bytes-concat (get-bytes file 0 22) (get-bytes file at slot-head-length)))
(nonce (bytes->aead-nonce cipher (get-bytes file (+ at slot-head-length) nonce-length))) (nonce (get-bytes file (+ at slot-head-length) nonce-length))
(wrapped (get-bytes file (+ at slot-head-length nonce-length) (wrapped (get-bytes file (+ at slot-head-length nonce-length)
(+ dek-length (cipher-tag-length cipher))))) (+ dek-length (cipher-tag-length cipher)))))
(+ at length)))))) (+ at length))))))
@ -295,13 +275,13 @@
(Err (BadKeys (str "a slot asks for " (int->string (.memory-kib cost)) " KiB")))) (Err (BadKeys (str "a slot asks for " (int->string (.memory-kib cost)) " KiB"))))
(else (Ok cost)))) (else (Ok cost))))
;; The bytes of the DEK of a password slot, or `None` when the password does ;; The DEK of a password slot, or `None` when the password does not open it.
;; not open it.
(: (-> Slot string KeyCost (Option (Array byte)))) (: (-> Slot string KeyCost (Option (Array byte))))
(defun (open-slot slot password cost) (defun (open-slot slot password cost)
(def kek (argon2id-derive-key password (.salt slot) (argon2id-cost cost))) (def kek (argon2id-derive-key password (.salt slot) #:iterations (.iterations cost)
(try (aead-decrypt kek (.nonce slot) (.wrapped slot) #:aad (.aad slot)) #:memory-kib (.memory-kib cost)))
#:finally (wipe-aead-key! kek))) (try (aead-decrypt (.cipher slot) kek (.nonce slot) (.wrapped slot) #:aad (.aad slot))
#:finally (wipe! kek)))
;; The vault id, the key id and the DEK, from the first password slot that ;; The vault id, the key id and the DEK, from the first password slot that
;; `password` opens. ;; `password` opens.
@ -315,16 +295,16 @@
(def (Ok slots) (read-slots file count) :propagate) (def (Ok slots) (read-slots file count) :propagate)
(def passwords (vec-filter (fun (s) (and (= (.kind s) 1) (= (.kdf s) 1))) slots)) (def passwords (vec-filter (fun (s) (and (= (.kind s) 1) (= (.kdf s) 1))) slots))
;; The earlier keys that the DEK `key-id` wraps, or why one does not open. ;; The earlier keys that the DEK `key-id` wraps, or why one does not open.
(: (-> int AeadKey (Result KeysError (Vec (Tuple int AeadKey))))) (: (-> int (Array byte) (Result KeysError (Vec (Tuple int (Array byte))))))
(defun (earlier-of key-id dek) (defun (earlier-of key-id dek)
(let go ((i 0) (out [])) (let go ((i 0) (out []))
(if (= i (vec-length slots)) (if (= i (vec-length slots))
(Ok out) (Ok out)
(let ((s (vec-ref slots i))) (let ((s (vec-ref slots i)))
(if (and (= (.kind s) 2) (= (get-u32 (.aad s) 28) key-id)) (if (and (= (.kind s) 2) (= (get-u32 (.aad s) 28) key-id))
(match (aead-decrypt dek (.nonce s) (.wrapped s) #:aad (.aad s)) (match (aead-decrypt (.cipher s) dek (.nonce s) (.wrapped s) #:aad (.aad s))
((Some old) #:when (= (array-length old) dek-length) ((Some old) #:when (= (array-length old) dek-length)
(go (+ i 1) (vec-add out (Tuple (.slot-key-id s) (take-key old))))) (go (+ i 1) (vec-add out (Tuple (.slot-key-id s) old))))
(_ (Err (BadKeys (str "the earlier key " (int->string (.slot-key-id s)) " does not open"))))) (_ (Err (BadKeys (str "the earlier key " (int->string (.slot-key-id s)) " does not open")))))
(go (+ i 1) out)))))) (go (+ i 1) out))))))
(cond (cond
@ -340,13 +320,12 @@
((Err why) (Err why)) ((Err why) (Err why))
((Ok cost) ((Ok cost)
(match (open-slot slot password cost) (match (open-slot slot password cost)
((Some plain) ((Some dek)
(if (= (array-length plain) dek-length) (if (= (array-length dek) dek-length)
(let ((dek (take-key plain))) (match (earlier-of (.slot-key-id slot) dek)
(match (earlier-of (.slot-key-id slot) dek) ((Ok earlier)
((Ok earlier) (Ok (Unlocked (vault-id vault-id) (key-id (.slot-key-id slot)) (dek dek) (earlier earlier))))
(Ok (Unlocked (vault-id vault-id) (key-id (.slot-key-id slot)) (dek dek) (earlier earlier)))) ((Err why) (Err why)))
((Err why) (Err why))))
(Err (BadKeys "the key in a slot has the wrong length")))) (Err (BadKeys "the key in a slot has the wrong length"))))
(None (go (+ i 1)))))))))))) (None (go (+ i 1))))))))))))

View file

@ -72,7 +72,6 @@
(import (std uuid)) (import (std uuid))
(import (text json)) (import (text json))
(import (bjosodium core)) (import (bjosodium core))
(import (bjosodium raw))
(import (bjomarks binary)) (import (bjomarks binary))
(import (bjomarks bookmark)) (import (bjomarks bookmark))
(import (bjomarks keys)) (import (bjomarks keys))
@ -269,12 +268,11 @@
(else (Err (str "unknown compression " (int->string compression)))))))) (else (Err (str "unknown compression " (int->string compression))))))))
;; A device file with a new random nonce. ;; A device file with a new random nonce.
(: (-> FileHead AeadKey DeviceDoc (Array byte))) (: (-> FileHead (Array byte) DeviceDoc (Array byte)))
(defun (encode-device-file head dek doc) (defun (encode-device-file head dek doc)
(def cipher (.head-cipher head)) (def cipher (.head-cipher head))
(def nonce (make-aead-nonce cipher)) (def nonce (make-aead-nonce cipher))
(def nonce-bytes (aead-nonce->bytes nonce)) (def header (: (make-array (+ head-length (array-length nonce))) (Array byte)))
(def header (: (make-array (+ head-length (array-length nonce-bytes))) (Array byte)))
(put-bytes! header 0 device-magic) (put-bytes! header 0 device-magic)
(put-u16! header 4 device-format) (put-u16! header 4 device-format)
(put-bytes! header 6 (uuid->bytes (.head-vault head))) (put-bytes! header 6 (uuid->bytes (.head-vault head)))
@ -282,14 +280,14 @@
(put-u64! header 38 (.head-revision head)) (put-u64! header 38 (.head-revision head))
(put-u32! header 46 (.head-key-id head)) (put-u32! header 46 (.head-key-id head))
(put-u8! header 50 (cipher->code cipher)) (put-u8! header 50 (cipher->code cipher))
(put-bytes! header head-length nonce-bytes) (put-bytes! header head-length nonce)
(def contents (pack (string->bytes (device-doc->string doc)))) (def contents (pack (string->bytes (device-doc->string doc))))
(def sealed (aead-encrypt dek nonce contents #:aad header)) (def sealed (aead-encrypt cipher dek nonce contents #:aad header))
(wipe! contents) (wipe! contents)
(bytes-concat header sealed)) (bytes-concat header sealed))
;; The DEK of the key id `id`, of those `known`. ;; The DEK of the key id `id`, of those `known`.
(: (-> (Vec (Tuple int AeadKey)) int (Option AeadKey))) (: (-> (Vec (Tuple int (Array byte))) int (Option (Array byte))))
(defun (key-of known id) (defun (key-of known id)
(let go ((i 0)) (let go ((i 0))
(cond ((= i (vec-length known)) None) (cond ((= i (vec-length known)) None)
@ -300,7 +298,7 @@
;; - The file must be of the vault `vault-id`, and encrypted with one of the ;; - The file must be of the vault `vault-id`, and encrypted with one of the
;; DEKs `known`, each with its key id. ;; DEKs `known`, each with its key id.
;; - The document must name the device that the header names. ;; - The document must name the device that the header names.
(: (-> (Array byte) Uuid (Vec (Tuple int AeadKey)) (Result string (Tuple FileHead DeviceDoc)))) (: (-> (Array byte) Uuid (Vec (Tuple int (Array byte))) (Result string (Tuple FileHead DeviceDoc))))
(defun (decode-device-file file vault-id known) (defun (decode-device-file file vault-id known)
(def n (array-length file)) (def n (array-length file))
(def version (if (>= n 6) (get-u16 file 4) 0)) (def version (if (>= n 6) (get-u16 file 4) 0))
@ -324,13 +322,12 @@
(:def nonce-length (cipher-nonce-length cipher)) (:def nonce-length (cipher-nonce-length cipher))
((< n (+ head-length nonce-length (cipher-tag-length cipher))) (Err "too short for a device file")) ((< n (+ head-length nonce-length (cipher-tag-length cipher))) (Err "too short for a device file"))
(:def header (get-bytes file 0 (+ head-length nonce-length))) (:def header (get-bytes file 0 (+ head-length nonce-length)))
(:def nonce (bytes->aead-nonce cipher (get-bytes file head-length nonce-length))) (:def nonce (get-bytes file head-length nonce-length))
(:def sealed (get-bytes file (+ head-length nonce-length) (- n (+ head-length nonce-length)))) (:def sealed (get-bytes file (+ head-length nonce-length) (- n (+ head-length nonce-length))))
(:def (Some contents) (aead-decrypt dek nonce sealed #:aad header) (:def (Some contents) (aead-decrypt cipher dek nonce sealed #:aad header)
:leave-with (Err "it does not decrypt: it was changed, or it was encrypted with another key")) :leave-with (Err "it does not decrypt: it was changed, or it was encrypted with another key"))
(:def (Ok json) (unpack contents) :propagate) (:def (Ok json) (unpack contents) :propagate)
(:def (Ok text) (bytes->string json) :leave-with (Err "the document in it is not UTF-8")) (:def (Ok doc) (parse-device-doc (bytes->string json)) :propagate)
(:def (Ok doc) (parse-device-doc text) :propagate)
((not (= (.device doc) device)) ((not (= (.device doc) device))
(Err (str "the header names the device " (uuid->string device) ", and the document names " (Err (str "the header names the device " (uuid->string device) ", and the document names "
(uuid->string (.device doc))))) (uuid->string (.device doc)))))
@ -418,8 +415,8 @@
(: id Uuid) (: id Uuid)
(: device Uuid) (: device Uuid)
(: key-id int) (: key-id int)
(: dek AeadKey) (: dek (Array byte))
(: known (Vec (Tuple int AeadKey))) (: known (Vec (Tuple int (Array byte))))
(: cipher Cipher) (: cipher Cipher)
(: entries (Map string Entry)) (: entries (Map string Entry))
(: seen (Map string long)) (: seen (Map string long))
@ -452,7 +449,7 @@
((BadKeys why) (BadFile (str keys-file-name ": " why))))) ((BadKeys why) (BadFile (str keys-file-name ": " why)))))
;; The keys that `u` gives: the current one first, then the earlier ones. ;; The keys that `u` gives: the current one first, then the earlier ones.
(: (-> Unlocked (Vec (Tuple int AeadKey)))) (: (-> Unlocked (Vec (Tuple int (Array byte)))))
(defun (keys-of u) (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u))) (defun (keys-of u) (vec-append [(Tuple (.key-id u) (.dek u))] (.earlier u)))
(: (-> Storage Unlocked string Uuid Vault)) (: (-> Storage Unlocked string Uuid Vault))
@ -577,7 +574,7 @@
(None (Err e)))))))) (None (Err e))))))))
;; `u` with the keys of `more` that it does not have, as earlier keys. ;; `u` with the keys of `more` that it does not have, as earlier keys.
(: (-> Unlocked (Vec (Tuple int AeadKey)) Unlocked)) (: (-> Unlocked (Vec (Tuple int (Array byte))) Unlocked))
(defun (with-keys u more) (defun (with-keys u more)
(def ids (vec-map fst (keys-of u))) (def ids (vec-map fst (keys-of u)))
(record-set u (earlier (vec-append (.earlier u) (vec-filter (fun (k) (not (vec-contains? ids (fst k)))) more))))) (record-set u (earlier (vec-append (.earlier u) (vec-filter (fun (k) (not (vec-contains? ids (fst k)))) more)))))
@ -702,7 +699,7 @@
;; Sets the DEKs to zeros. Do not use the vault after this. ;; Sets the DEKs to zeros. Do not use the vault after this.
(: (-> Vault void)) (: (-> Vault void))
(defun (vault-close v) (defun (vault-close v)
(loop (:for k (.known v)) (:do (wipe-aead-key! (snd k))))) (loop (:for k (.known v)) (:do (wipe! (snd k)))))
;; --------------------------------------------------------------------------- ;; ---------------------------------------------------------------------------
;; Changes ;; Changes

View file

@ -87,7 +87,7 @@
(let go ((i start)) (let go ((i start))
(if (and (< i end) (name-byte? (cast int (array-ref bytes i)))) (if (and (< i end) (name-byte? (cast int (array-ref bytes i))))
(go (+ i 1)) (go (+ i 1))
(bytes->string/lossy (ro-span->array (array->ro-span bytes #:from start #:to i)))))) (bytes->string (ro-span->array (array->ro-span bytes #:from start #:to i))))))
;; The charset that a `Content-Type` value names, if it names one. ;; The charset that a `Content-Type` value names, if it names one.
(: (-> string (Option string))) (: (-> string (Option string)))
@ -118,9 +118,9 @@
(def encoding (match charset (def encoding (match charset
((Some name) (match (encoding-named name) ((Some e) e) (None utf-8))) ((Some name) (match (encoding-named name) ((Some e) e) (None utf-8)))
(None utf-8))) (None utf-8)))
(match (bytes->string slice #:encoding encoding) (match (try (bytes->string slice #:encoding encoding) #:catch (System.Exception))
((Ok text) text) ((Ok text) text)
((Err _) (bytes->string/lossy slice #:encoding windows-1252)))) ((Err _) (bytes->string slice #:encoding windows-1252))))
;; The title of the page in the first `n` bytes of `bytes`, as ;; The title of the page in the first `n` bytes of `bytes`, as
;; `clean-title` makes it. `None` when there is no whole `<title>` element, ;; `clean-title` makes it. `None` when there is no whole `<title>` element,

View file

@ -28,9 +28,6 @@
(: (-> (Array byte) (Array byte))) (: (-> (Array byte) (Array byte)))
(defun (copy-of a) (get-bytes a 0 (array-length a))) (defun (copy-of a) (get-bytes a 0 (array-length a)))
(: (-> AeadKey AeadKey bool))
(defun (same-key? a b) (bytes=? (aead-key->bytes a) (aead-key->bytes b)))
;; A copy of `a` with bit 0 of byte `at` changed. ;; A copy of `a` with bit 0 of byte `at` changed.
(: (-> (Array byte) int (Array byte))) (: (-> (Array byte) int (Array byte)))
(defun (flipped a at) (defun (flipped a at)
@ -55,8 +52,8 @@
(def (Ok opened) (unlock-keys file "hunter2") :leave-with (check-fail "it did not open")) (def (Ok opened) (unlock-keys file "hunter2") :leave-with (check-fail "it did not open"))
(check (= (.vault-id opened) (.vault-id made))) (check (= (.vault-id opened) (.vault-id made)))
(check (= (.key-id opened) 1)) (check (= (.key-id opened) 1))
(check (same-key? (.dek opened) (.dek made))) (check (bytes=? (.dek opened) (.dek made)))
(check (= (array-length (aead-key->bytes (.dek made))) 32)) (check (= (array-length (.dek made)) 32))
(check (= (keys-vault-id file) (Ok (.vault-id made))))) (check (= (keys-vault-id file) (Ok (.vault-id made)))))
(deftest "each cipher wraps the key" (deftest "each cipher wraps the key"
@ -78,14 +75,14 @@
(def (Tuple _f1 a) (make-keys "pw" #:cost cheap)) (def (Tuple _f1 a) (make-keys "pw" #:cost cheap))
(def (Tuple _f2 b) (make-keys "pw" #:cost cheap)) (def (Tuple _f2 b) (make-keys "pw" #:cost cheap))
(check (not (= (.vault-id a) (.vault-id b)))) (check (not (= (.vault-id a) (.vault-id b))))
(check (not (same-key? (.dek a) (.dek b))))) (check (not (bytes=? (.dek a) (.dek b)))))
(deftest "a new password keeps the vault and its key" (deftest "a new password keeps the vault and its key"
(def (Tuple old made) (make-keys "old password" #:cost cheap)) (def (Tuple old made) (make-keys "old password" #:cost cheap))
(def new (wrap-keys (.vault-id made) (.key-id made) (.dek made) "new password" #:cost cheap)) (def new (wrap-keys (.vault-id made) (.key-id made) (.dek made) "new password" #:cost cheap))
(check (= (outcome (unlock-keys new "old password")) "WrongPassword")) (check (= (outcome (unlock-keys new "old password")) "WrongPassword"))
(def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open")) (def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open"))
(check (same-key? (.dek opened) (.dek made))) (check (bytes=? (.dek opened) (.dek made)))
(check (= (.vault-id opened) (.vault-id made))) (check (= (.vault-id opened) (.vault-id made)))
(check (= (outcome (unlock-keys old "old password")) "Ok"))) (check (= (outcome (unlock-keys old "old password")) "Ok")))
@ -138,13 +135,13 @@
(def (Tuple new rotated) (new-keys made "new password" #:cost cheap)) (def (Tuple new rotated) (new-keys made "new password" #:cost cheap))
(check (= (.vault-id rotated) (.vault-id made))) (check (= (.vault-id rotated) (.vault-id made)))
(check (not (= (.key-id rotated) (.key-id made)))) (check (not (= (.key-id rotated) (.key-id made))))
(check (not (same-key? (.dek rotated) (.dek made)))) (check (not (bytes=? (.dek rotated) (.dek made))))
;; The new file opens with the new password, and gives the earlier key. ;; The new file opens with the new password, and gives the earlier key.
(def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open")) (def (Ok opened) (unlock-keys new "new password") :leave-with (check-fail "it did not open"))
(check (= (.key-id opened) (.key-id rotated))) (check (= (.key-id opened) (.key-id rotated)))
(check (same-key? (.dek opened) (.dek rotated))) (check (bytes=? (.dek opened) (.dek rotated)))
(check (= (vec-map fst (.earlier opened)) [1])) (check (= (vec-map fst (.earlier opened)) [1]))
(check (same-key? (snd (vec-ref (.earlier opened) 0)) (.dek made))) (check (bytes=? (snd (vec-ref (.earlier opened) 0)) (.dek made)))
(check (= (outcome (unlock-keys new "old password")) "WrongPassword")) (check (= (outcome (unlock-keys new "old password")) "WrongPassword"))
;; The old file and the old password give the old key only. ;; The old file and the old password give the old key only.
(def (Ok was) (unlock-keys old "old password") :leave-with (check-fail "the old file did not open")) (def (Ok was) (unlock-keys old "old password") :leave-with (check-fail "the old file did not open"))

View file

@ -543,7 +543,7 @@
(def a0 (vault-save (fst (add-bookmark (create f (.one f)) "https://example.org/a" [])))) (def a0 (vault-save (fst (add-bookmark (create f (.one f)) "https://example.org/a" []))))
(def mine (own-keys f (.one f))) (def mine (own-keys f (.one f)))
;; The same vault id, another key, and another password. ;; The same vault id, another key, and another password.
(set-shared-keys! f (wrap-keys (vault-id a0) 7 (make-aead-key) "not yours" #:cost cheap)) (set-shared-keys! f (wrap-keys (vault-id a0) 7 (make-aead-key XChaCha20Poly1305) "not yours" #:cost cheap))
(def (Tuple a1 report) (sync (opened (reopen f (.one f) "hunter2")))) (def (Tuple a1 report) (sync (opened (reopen f (.one f) "hunter2"))))
(check (told? report "holds none of this device's keys")) (check (told? report "holds none of this device's keys"))
(check (= (urls a1) ["https://example.org/a"])) (check (= (urls a1) ["https://example.org/a"]))

View file

@ -134,7 +134,7 @@
(if (string-contains? seen "\r\n\r\n") (if (string-contains? seen "\r\n\r\n")
(path-of seen) (path-of seen)
(match (read-some in) (match (read-some in)
((Some bs) (go (str seen (bytes->string/lossy bs)))) ((Some bs) (go (str seen (bytes->string bs))))
(None (path-of seen)))))) (None (path-of seen))))))
;; Waits `ms`, or less when the server stops. ;; Waits `ms`, or less when the server stops.

View file

@ -63,7 +63,7 @@
(if (string-contains? seen "\r\n\r\n") (if (string-contains? seen "\r\n\r\n")
(match (string-split-once seen "\r\n") ((Some (Tuple l _)) l) (None seen)) (match (string-split-once seen "\r\n") ((Some (Tuple l _)) l) (None seen))
(match (read-some in) (match (read-some in)
((Some bs) (go (str seen (bytes->string/lossy bs)))) ((Some bs) (go (str seen (bytes->string bs))))
(None seen))))) (None seen)))))
;; One connection: one request, one answer. The wait ends early when the ;; One connection: one request, one answer. The wait ends early when the