535 lines
25 KiB
Org Mode
535 lines
25 KiB
Org Mode
|
|
* bjomarks — bookmarks in an encrypted store
|
||
|
|
|
||
|
|
Bookmarks that sync between devices through a folder that any file syncer or
|
||
|
|
WebDAV share carries. The folder holds only encrypted files: the storage
|
||
|
|
provider sees how many devices there are and roughly how large the vault is,
|
||
|
|
and nothing else. Encryption and password hashing are [[file:../bjosodium/Readme.org][bjosodium]]'s.
|
||
|
|
|
||
|
|
#+BEGIN_SRC sh
|
||
|
|
export BJOMARKS_PASSWORD="correct horse"
|
||
|
|
bjo build examples/marks.bjo
|
||
|
|
M="dotnet examples/marks.exe"
|
||
|
|
$M ~/Sync/bookmarks ~/.local/share/bjomarks create
|
||
|
|
$M ~/Sync/bookmarks ~/.local/share/bjomarks add https://example.org/ rust async
|
||
|
|
# on the second device, once the syncer has copied the folder:
|
||
|
|
$M ~/Sync/bookmarks ~/.local/share/bjomarks join
|
||
|
|
$M ~/Sync/bookmarks ~/.local/share/bjomarks list
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
| Module | What it has |
|
||
|
|
|-----------------------+----------------------------------------------------------------|
|
||
|
|
| ~(bjomarks bookmark)~ | bookmarks, stamps, entries, the merge, and their JSON |
|
||
|
|
| ~(bjomarks keys)~ | ~keys.bin~: the vault's key, wrapped for a password |
|
||
|
|
| ~(bjomarks store)~ | device files, storage, and the vault that syncs |
|
||
|
|
| ~(bjomarks title)~ | the title of a web page, fetched from its address |
|
||
|
|
| ~(bjomarks wayback)~ | the latest snapshot of a page in the Wayback Machine |
|
||
|
|
| ~(bjomarks import)~ | Firefox's JSON backup, read, and its bookmarks put in a vault |
|
||
|
|
| ~(bjomarks draft)~ | the dialog of a bookmark as it is typed |
|
||
|
|
| ~(bjomarks search)~ | the search of the list: words, typos, tags, domains and days |
|
||
|
|
| ~(bjomarks duplicates)~ | one bookmark for a page that two devices bookmarked |
|
||
|
|
| ~(bjomarks binary)~ | big-endian integers and the cipher byte, for the binary files |
|
||
|
|
|
||
|
|
** Using it
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(depends
|
||
|
|
(package (name (bjomarks))
|
||
|
|
(version (version-at-least "0.1.0"))
|
||
|
|
(source (path (dir "../bjomarks")))))
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(import (std prelude))
|
||
|
|
(import (bjomarks bookmark))
|
||
|
|
(import (bjomarks store))
|
||
|
|
|
||
|
|
;; Adds a bookmark, changes its tags, and syncs.
|
||
|
|
(: (-> Storage string string (Result string int)))
|
||
|
|
(defun (add-one storage local password)
|
||
|
|
(match (open-vault storage local password)
|
||
|
|
((Err WrongPassword) (Err "The password is wrong."))
|
||
|
|
((Err _) (Err "The vault does not open."))
|
||
|
|
((Ok opened)
|
||
|
|
(def (Tuple v report) (vault-sync opened)) ; merge the other devices
|
||
|
|
(def (Ok (Tuple v2 b)) (vault-add v "https://example.org/" #:tags ["rust"]) :propagate)
|
||
|
|
(def v3 (vault-put v2 (bookmark-with-tags b ["rust" "web"])))
|
||
|
|
(def (Tuple v4 _) (vault-sync v3)) ; write this device's file
|
||
|
|
(vault-close v4)
|
||
|
|
(Ok (vec-length (.problems report))))))
|
||
|
|
|
||
|
|
(add-one (folder-storage "/home/me/Sync/bookmarks") "/home/me/.local/share/bjomarks" password)
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
| ~(create-vault storage local pw #:cost)~ | a new vault, with this device as its first |
|
||
|
|
| ~(join-vault storage local pw)~ | this device as a new device of a vault; synced |
|
||
|
|
| ~(open-vault storage local pw)~ | the vault of this device, from its own files |
|
||
|
|
| ~(vault-sync v)~ | merges the other devices, writes this device's file if it changed; with a ~SyncReport~ |
|
||
|
|
| ~(vault-save v)~ | writes this device's file, if there are changes |
|
||
|
|
| ~(vault-merge current done)~ | ~current~, with what a sync or save that ran in the background on an earlier copy of it did; it has changes to write when ~current~ changed meanwhile |
|
||
|
|
| ~(vault-bookmarks v)~ ~(vault-find v id)~ | the bookmarks, the newest first |
|
||
|
|
| ~(vault-add v url #:title #:tags #:archive #:added)~ | a new bookmark, or ~Err~ for an address that is not an absolute URL |
|
||
|
|
| ~(vault-put v b)~ | a changed bookmark |
|
||
|
|
| ~(vault-delete v id)~ | a deleted one |
|
||
|
|
| ~(vault-restore v entry)~ | the entry as it was before a delete, at a new stamp: an undo |
|
||
|
|
| ~(vault-change-password v pw #:cost)~ | ~(Ok v)~ with a new key and keys.bin; or ~(Err why)~ |
|
||
|
|
| ~(vault-close v)~ | sets the key to zeros |
|
||
|
|
|
||
|
|
A vault is a value: a change returns a new vault, and nothing is written
|
||
|
|
until ~vault-save~ or ~vault-sync~. ~open-vault~, ~create-vault~ and
|
||
|
|
~join-vault~ return a ~StoreError~: ~WrongPassword~, ~NoVault~,
|
||
|
|
~VaultExists~, ~OtherVault~ or ~BadFile~, each with a text that says where.
|
||
|
|
A file of another device that cannot be read does not stop a sync: it is
|
||
|
|
skipped, and the report's ~problems~ name it. What the file system raises is
|
||
|
|
raised.
|
||
|
|
|
||
|
|
*In a bjoroutine*, the file operations of the store do not block the thread,
|
||
|
|
and Argon2id runs on a thread of its own. ~Storage~ is a union, not a record
|
||
|
|
of functions, for that reason: a function in a record field cannot be of
|
||
|
|
either colour.
|
||
|
|
|
||
|
|
*A folder without a path*, such as a folder of Android's Storage Access
|
||
|
|
Framework, is a storage of three functions:
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(function-storage "Documents/bookmarks"
|
||
|
|
(fun (name) ...) ; (Some bytes), or None: no such file
|
||
|
|
(fun (name bytes) ...) ; writes it, and the folders on the way
|
||
|
|
(fun (dir) ...)) ; the names of the files in dir
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
- A name is relative, with ~/~ between its parts, as for a folder.
|
||
|
|
- In a bjoroutine the functions block the thread. Call the vault's
|
||
|
|
functions in ~blocking~.
|
||
|
|
- A write need not replace the file in one step. A device that reads half
|
||
|
|
of a device file skips it, names it in the report's ~problems~, and reads
|
||
|
|
it at the next sync.
|
||
|
|
- bjomarks-android makes one from a folder of bjogui's ~(bjogui storage)~.
|
||
|
|
|
||
|
|
** A bookmark
|
||
|
|
|
||
|
|
A bookmark has an id, an address, an optional title, the instant it was
|
||
|
|
added, a set of tags, and an optional link to a copy in a web archive:
|
||
|
|
|
||
|
|
#+BEGIN_SRC json
|
||
|
|
{"id": "0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f",
|
||
|
|
"stamp": "019a722fb300-0002-3f9c0d2a-1111-4222-8333-444455556666",
|
||
|
|
"url": "https://example.org/",
|
||
|
|
"title": "Example Domain",
|
||
|
|
"added": "2026-11-11T09:12:00Z",
|
||
|
|
"tags": ["async", "rust"],
|
||
|
|
"archive": "https://web.archive.org/web/20261111091200/https://example.org/"}
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
| Field | Rules |
|
||
|
|
|-----------+---------------------------------------------------------------------------|
|
||
|
|
| ~id~ | a uuid; it does not change |
|
||
|
|
| ~stamp~ | when the entry last changed, and on which device: see /Merging/ |
|
||
|
|
| ~url~ | an absolute URL by RFC 3986: it has a scheme |
|
||
|
|
| ~title~ | one line of at most 300 characters, or ~null~, or absent. Read cleaned: white space made one space, the ends trimmed; empty is no title |
|
||
|
|
| ~added~ | ISO 8601 with an offset; read as UTC |
|
||
|
|
| ~tags~ | strings that are not empty; read as a set, written sorted. Can be absent |
|
||
|
|
| ~archive~ | an absolute URL, or ~null~, or absent |
|
||
|
|
| ~deleted~ | ~true~ for a tombstone, which has only ~id~ and ~stamp~ |
|
||
|
|
|
||
|
|
*Fields from a newer version are kept.* A field that this version does not
|
||
|
|
know, such as a ~note~ that a later version adds, is kept in the entry's
|
||
|
|
~extra~, in the order it was read, and written back after the known fields.
|
||
|
|
~vault-put~ keeps the extra fields of the entry it replaces. So an older
|
||
|
|
version that changes the tags does not remove the note. A version from
|
||
|
|
before titles keeps a ~title~ in the same way.
|
||
|
|
|
||
|
|
~parse-device-doc~ and ~json->entry~ return ~(Err why)~ with where:
|
||
|
|
~records[3]: tags[1]: expected a string~.
|
||
|
|
|
||
|
|
** Importing from Firefox
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(import (bjomarks import))
|
||
|
|
|
||
|
|
(match (read-firefox-backup (file-read-text "bookmarks-2026-11-11.json"))
|
||
|
|
((Ok backup)
|
||
|
|
(def (Tuple v2 report) (vault-import v (.bookmarks backup)))
|
||
|
|
;; (.added report), (.already report); (.not-web backup), (.invalid backup)
|
||
|
|
v2)
|
||
|
|
((Err why) ...))
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
~read-firefox-backup~ reads the JSON backup that Firefox writes (Library,
|
||
|
|
Import and Backup, Backup…), not the HTML export: for that it returns an
|
||
|
|
~Err~ that says which file to use.
|
||
|
|
- Only http and https addresses are read; the others are counted in
|
||
|
|
~not-web~. An http address that is not a URL by RFC 3986 is named, with
|
||
|
|
the reason, in ~invalid~: a bookmark must be one.
|
||
|
|
- Each folder of the user's that holds a bookmark is a tag of it, at any
|
||
|
|
depth; Firefox's four top folders are not. Firefox's tags are tags too.
|
||
|
|
~tag-name~ makes a name a tag: lower case, ~-~ for each run of white
|
||
|
|
space and commas.
|
||
|
|
- An address bookmarked more than once is one bookmark, with the tags of
|
||
|
|
all its copies, the earliest date and the first title; ~copies~ counts
|
||
|
|
the others.
|
||
|
|
- Dates are Firefox's ~dateAdded~, in microseconds.
|
||
|
|
|
||
|
|
~vault-import~ adds the bookmarks whose address the vault does not have,
|
||
|
|
and changes none that it has. So a backup can be imported again, and only
|
||
|
|
what is new comes in. The vault is saved as after any change.
|
||
|
|
~(import-summary name backup report)~ returns the line that the apps show:
|
||
|
|
"Imported 1 bookmark from bookmarks.json. 1 was here already."
|
||
|
|
|
||
|
|
** Fetching a title
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(import (bjomarks title))
|
||
|
|
|
||
|
|
(fetch-title "https://example.org/") ; (Ok "Example Domain")
|
||
|
|
(fetch-title "https://example.org/" #:within 5) ; a deadline of 5 seconds, not 10
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
~fetch-title~ is a ~defbjo~: call it in a bjoroutine. It returns the text
|
||
|
|
of the page's ~<title>~ element, cleaned as the ~title~ field is, or
|
||
|
|
~(Err why)~:
|
||
|
|
|
||
|
|
- for an address that is not http or https; nothing is fetched,
|
||
|
|
- for a request that fails, or a status that is not 2xx,
|
||
|
|
- for a type that is not HTML (no type is taken as HTML),
|
||
|
|
- when there is no title in the first 512 KiB,
|
||
|
|
- when the fetch does not end in the deadline: 10 seconds, or ~#:within~.
|
||
|
|
The fetch is then cancelled.
|
||
|
|
|
||
|
|
Redirects are followed. Reading stops at the end of the title. The title is
|
||
|
|
decoded by the charset of the ~Content-Type~ header, else of a ~<meta>~
|
||
|
|
element in the first 4 KiB, else as UTF-8; bytes that do not decode are read
|
||
|
|
as Windows-1252. Character references such as ~&~ are decoded.
|
||
|
|
~page-title~ does the same for bytes got another way.
|
||
|
|
|
||
|
|
*A fetch tells the site.* The site sees the request: this machine's address,
|
||
|
|
the time, and the User-Agent ~Mozilla/5.0 (compatible; bjomarks/0.1;
|
||
|
|
+title)~. The storage provider sees nothing of it. Only ~<title>~ is read,
|
||
|
|
not ~og:title~ or other metadata.
|
||
|
|
|
||
|
|
** Looking up an archive link
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(import (bjomarks wayback))
|
||
|
|
|
||
|
|
(fetch-archive "https://example.com/")
|
||
|
|
;; (Ok "https://web.archive.org/web/20261118035140/https://example.com/")
|
||
|
|
(fetch-archive "https://example.com/" #:within 5) ; a deadline of 5 seconds, not 20
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
~fetch-archive~ is a ~defbjo~: call it in a bjoroutine. It asks the
|
||
|
|
availability API of the Wayback Machine (~wayback-api~,
|
||
|
|
~https://archive.org/wayback/available~) for the page, and returns the
|
||
|
|
address of its latest snapshot, made https. It returns ~(Err why)~:
|
||
|
|
|
||
|
|
- for an address that is not http or https; nothing is asked,
|
||
|
|
- for a request that fails, or a status that is not 2xx,
|
||
|
|
- for an answer that is not JSON,
|
||
|
|
- when the Wayback Machine has no snapshot of the page,
|
||
|
|
- when the lookup does not end in the deadline: 20 seconds, or ~#:within~.
|
||
|
|
The API is often slow. The lookup is then cancelled.
|
||
|
|
|
||
|
|
~#:api~ gives another address for the API, as the tests do. ~snapshot-in~
|
||
|
|
reads the snapshot from an answer got another way.
|
||
|
|
|
||
|
|
*A lookup tells the Internet Archive.* It sees the address of the page,
|
||
|
|
this machine's address and the User-Agent ~Mozilla/5.0 (compatible;
|
||
|
|
bjomarks/0.1; +archive)~. The page itself is not fetched, and no snapshot
|
||
|
|
is made.
|
||
|
|
|
||
|
|
** The dialog
|
||
|
|
|
||
|
|
(bjomarks draft) has what the apps' dialog of a bookmark does, so that the
|
||
|
|
desktop and Android do the same. The apps only show it.
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(import (bjomarks draft))
|
||
|
|
|
||
|
|
(def d (with-address v empty-draft "https://example.org/")) ; loads its bookmark, if one has it
|
||
|
|
(match (apply-draft v d)
|
||
|
|
((Ok (Tuple v2 bookmark)) v2)
|
||
|
|
((Err why) ...)) ; not an absolute URL
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
- A ~Draft~ is a bookmark being made (~editing~ is ~None~) or changed, with
|
||
|
|
its fields as typed.
|
||
|
|
- ~with-address~: in the dialog of a new bookmark, an address that a
|
||
|
|
bookmark has loads that bookmark. Another address brings back what was
|
||
|
|
typed.
|
||
|
|
- A fetch of the title or of the archive link has a number:
|
||
|
|
~start-title-fetch~ starts it, and ~with-title-fetched~ puts its result in
|
||
|
|
the draft only when the draft still waits for that number. The same for
|
||
|
|
~start-archive-fetch~ and ~with-archive-fetched~.
|
||
|
|
|
||
|
|
** The search
|
||
|
|
|
||
|
|
(bjomarks search) has the search of the list, for both apps.
|
||
|
|
|
||
|
|
#+BEGIN_SRC scheme
|
||
|
|
(import (bjomarks search))
|
||
|
|
|
||
|
|
(search-bookmarks v "rust -t:docs d:github.com a:2024:") ; the bookmarks that the search shows
|
||
|
|
(query-problem (parse-query "a:2012-13")) ; "Not a date: 2012-13"
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
| Term | A bookmark matches it when |
|
||
|
|
|-------------------+---------------------------------------------------------------------|
|
||
|
|
| ~rust~ | the word is in the title, the address or a tag, maybe with a typo |
|
||
|
|
| ~t:rust~ | it has the tag "rust", the whole tag (~tag:rust~ is the same) |
|
||
|
|
| ~d:github~ | its domain has "github" in it |
|
||
|
|
| ~d:github.com~ | its domain is github.com or one under it, such as gist.github.com |
|
||
|
|
| ~a:2012-06~ | it was added in June 2012 (a year, a month or a day) |
|
||
|
|
| ~a:2011:2012-06~ | it was added from 1 January 2011 to 30 June 2012 (~a:2011:~, ~a::2012~) |
|
||
|
|
| ~"two words"~ | it has exactly that text |
|
||
|
|
| ~-~ before a term | it does not match the term |
|
||
|
|
|
||
|
|
- A word matches a whole word, the start of a word, a part of one, or a
|
||
|
|
word with typos: one in a word of 4 to 7 letters, two in a longer one. A
|
||
|
|
swap of two letters side by side is one typo. The other terms are exact.
|
||
|
|
- Capitals and accents do not matter, but å, ä, ö, æ and ø are letters of
|
||
|
|
their own.
|
||
|
|
- With words, the best matches come first: a whole word before a part, and
|
||
|
|
the title before the tags and the address. Otherwise the newest first.
|
||
|
|
- The days are in the clock's time zone. A date that is not one matches
|
||
|
|
nothing.
|
||
|
|
- What the search looks at is made once for each vault, and the last result
|
||
|
|
is kept, so the apps may search on each redraw.
|
||
|
|
|
||
|
|
** Duplicates
|
||
|
|
|
||
|
|
(bjomarks duplicates): two devices that bookmark a page before they sync
|
||
|
|
make two bookmarks of it. ~vault-join-duplicates~, after a sync, makes them
|
||
|
|
one, with the tags of both, the earliest time added and an archive link if
|
||
|
|
either has one. Bookmarks with different titles stay two, with the joined
|
||
|
|
tags, and are returned as a ~Duplicate~; ~vault-choose-title~ joins them
|
||
|
|
with the title chosen. Each device joins them the same way, so devices that
|
||
|
|
do it before they sync agree.
|
||
|
|
|
||
|
|
** Merging
|
||
|
|
|
||
|
|
Each device's file holds every entry it knows. A device reads the files of
|
||
|
|
the others and merges by id: *the entry with the later stamp wins*. A
|
||
|
|
deleted bookmark keeps its entry as a tombstone, so a delete reaches the
|
||
|
|
other devices and an older copy cannot bring the bookmark back. A change
|
||
|
|
after the delete does bring it back.
|
||
|
|
|
||
|
|
A stamp is a hybrid logical clock: the wall-clock time in milliseconds, a
|
||
|
|
counter, and the device. A new stamp is later than every stamp the device has
|
||
|
|
made or merged, also when its wall clock is behind the others'. Its text is
|
||
|
|
~<12 hex digits of ms>-<4 hex digits of counter>-<device uuid>~, so the order
|
||
|
|
of the texts is the order of the stamps.
|
||
|
|
|
||
|
|
Two different entries with the same stamp come only from a fault. Then the
|
||
|
|
entry with the greater JSON text wins, so all devices still choose the same
|
||
|
|
one. The tests check with generated entries that the merge gives the same
|
||
|
|
result in any order and grouping, that merging twice changes nothing, and
|
||
|
|
that each id keeps its latest stamp.
|
||
|
|
|
||
|
|
A bookmark is merged as a whole: when two devices change the same bookmark
|
||
|
|
before they sync, the later change wins and the other is lost, also when one
|
||
|
|
changed the tags and the other the archive link.
|
||
|
|
|
||
|
|
** The files
|
||
|
|
|
||
|
|
*** In the shared storage
|
||
|
|
|
||
|
|
#+BEGIN_SRC text
|
||
|
|
keys.bin the vault's key, wrapped for each password
|
||
|
|
devices/<device uuid>.bin one file for each device; only that device writes it
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
*** In this device's folder (not shared)
|
||
|
|
|
||
|
|
#+BEGIN_SRC text
|
||
|
|
device.json {"format": 1, "vault": "<uuid>", "device": "<uuid>"}
|
||
|
|
state.bin the same bytes as this device's file in the storage
|
||
|
|
keys.bin a copy of the storage's, so that the vault opens without it
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
~(device-folder root shared storage)~ gives a folder in ~root/vaults~ for
|
||
|
|
each shared folder, named by a hash of ~shared~, so that a device can have
|
||
|
|
the vaults of several shared folders. The files of a device folder of the
|
||
|
|
older layout, ~root~ itself, move to the folder of the shared folder whose
|
||
|
|
~keys.bin~ is of their vault.
|
||
|
|
|
||
|
|
~open-vault~ reads ~state.bin~ and this device's file in the storage, and
|
||
|
|
uses the one with the higher revision; it writes the other again at the next
|
||
|
|
save. A storage that lost this device's file, or ~keys.bin~, gets it again
|
||
|
|
at the next sync.
|
||
|
|
|
||
|
|
The local copy of ~keys.bin~ is replaced only by one that this device has
|
||
|
|
the current key of, after it wrote its file with a key that the new one
|
||
|
|
holds. A ~keys.bin~ that an attacker put in the storage, with keys of their
|
||
|
|
own, therefore never replaces it: the device opens from its copy, and the
|
||
|
|
sync names the problem. See /A new password/.
|
||
|
|
|
||
|
|
*Do not copy this device's folder to another machine.* Two devices with one
|
||
|
|
device id write the same file, and the syncer keeps one of them.
|
||
|
|
|
||
|
|
*** keys.bin, version 1
|
||
|
|
|
||
|
|
Integers are big-endian.
|
||
|
|
|
||
|
|
#+BEGIN_SRC text
|
||
|
|
offset size field
|
||
|
|
0 4 "BMKV"
|
||
|
|
4 2 version: 1
|
||
|
|
6 16 vault id
|
||
|
|
22 1 number of slots, from 1
|
||
|
|
23 the slots, one after the other
|
||
|
|
|
||
|
|
A password slot:
|
||
|
|
0 1 kind: 1 = password
|
||
|
|
1 4 key id: the data key that the slot wraps
|
||
|
|
5 1 kdf: 1 = Argon2id (version 1.3, one lane)
|
||
|
|
6 4 iterations
|
||
|
|
10 4 memory, in KiB
|
||
|
|
14 16 salt
|
||
|
|
30 1 cipher: 1 = AEGIS-256, 2 = XChaCha20-Poly1305
|
||
|
|
31 n nonce: 32 bytes, or 24
|
||
|
|
31+n 32+t the data key, encrypted, and the tag: 32 bytes, or 16
|
||
|
|
|
||
|
|
A slot of an earlier key, the same length:
|
||
|
|
0 1 kind: 2 = an earlier key
|
||
|
|
1 4 key id: the earlier data key that the slot holds
|
||
|
|
5 1 kdf: 0 = none
|
||
|
|
6 4 the key id of the data key that wraps it: the current one
|
||
|
|
10 20 zeros
|
||
|
|
30 1 cipher
|
||
|
|
31 n nonce
|
||
|
|
31+n 32+t the earlier data key, encrypted with the current one, and the tag
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
- The data key (DEK) is 32 random bytes. It encrypts every device file.
|
||
|
|
- A password slot wraps the DEK with a key that Argon2id makes from the
|
||
|
|
password, the salt and the cost. The associated data is bytes 0 to 21 of
|
||
|
|
the file and bytes 0 to 30 of the slot, so a change to the vault id, the
|
||
|
|
cost or the salt makes the slot fail.
|
||
|
|
- The password slots come first, and wrap the same DEK: the current one.
|
||
|
|
The earlier keys follow, the latest first, at most 200. A version that
|
||
|
|
knows only password slots skips them.
|
||
|
|
- A reader tries at most 4 password slots, and refuses a cost above 100
|
||
|
|
iterations or 1 GiB, because a file from an attacker could otherwise stop
|
||
|
|
the device.
|
||
|
|
- The default cost is 3 iterations over 64 MiB, about 75 ms on a desktop CPU
|
||
|
|
of 2020. Measure on the slowest device, and give ~#:cost~ more if it allows.
|
||
|
|
|
||
|
|
*** A new password
|
||
|
|
|
||
|
|
~vault-change-password~ makes a new DEK, with a random key id, and a new
|
||
|
|
~keys.bin~: a password slot for the new DEK, and the earlier DEKs wrapped
|
||
|
|
with it. The device then writes its file with the new DEK. An old copy of
|
||
|
|
~keys.bin~ (in a syncer's versions, a backup or a trash folder) and the old
|
||
|
|
password then open only what was written before the change.
|
||
|
|
|
||
|
|
- The other devices, while they are open, read on with the keys they have,
|
||
|
|
and write with their key, which the new ~keys.bin~ holds as an earlier
|
||
|
|
one, so that all of them read it. Their sync tells them that the password
|
||
|
|
changed; the old password no longer opens the vault, also from their copy
|
||
|
|
of ~keys.bin~.
|
||
|
|
- A device that does not have every key of the storage's ~keys.bin~ cannot
|
||
|
|
change the password: it would leave a key out.
|
||
|
|
- Two devices that change it at the same time: the syncer keeps one
|
||
|
|
~keys.bin~. The other device writes its file with a key that the kept one
|
||
|
|
holds, so that the others read it, and opens with its own password from
|
||
|
|
its copy of ~keys.bin~ until it opens with the other one. With the same
|
||
|
|
password, both files open, and their keys are joined.
|
||
|
|
|
||
|
|
*** A device file, version 1
|
||
|
|
|
||
|
|
#+BEGIN_SRC text
|
||
|
|
offset size field
|
||
|
|
0 4 "BMK1"
|
||
|
|
4 2 version: 1
|
||
|
|
6 16 vault id
|
||
|
|
22 16 device id: the same as in the file's name
|
||
|
|
38 8 revision: 1 for the first file, then 1 more each time
|
||
|
|
46 4 key id: the DEK that encrypts the file
|
||
|
|
50 1 cipher: 1 = AEGIS-256, 2 = XChaCha20-Poly1305
|
||
|
|
51 n nonce: 32 bytes, or 24; new for each file
|
||
|
|
51+n the contents, encrypted, then the tag
|
||
|
|
|
||
|
|
The contents:
|
||
|
|
0 1 compression: 0 = none, 1 = Brotli
|
||
|
|
1 4 length of the stored document
|
||
|
|
5 4 length of the document
|
||
|
|
9 the stored document, then bytes of 0
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
- The associated data is the header, bytes 0 to 50+n. A file whose header
|
||
|
|
was changed does not decrypt.
|
||
|
|
- The contents are padded to a multiple of 16 KiB, so that the size of a
|
||
|
|
file shows only roughly how many bookmarks there are.
|
||
|
|
- The document is UTF-8 JSON:
|
||
|
|
#+BEGIN_SRC json
|
||
|
|
{"format": 1, "device": "<uuid>", "seen": {"<uuid>": 41}, "records": [<entry>, ...]}
|
||
|
|
#+END_SRC
|
||
|
|
~records~ are in the order of their ids, so the same entries give the same
|
||
|
|
text. ~seen~ is the highest revision of each other device's file that this
|
||
|
|
device has merged.
|
||
|
|
- Each device writes with the cipher it prefers (see bjosodium's
|
||
|
|
~preferred-cipher~), and reads both.
|
||
|
|
|
||
|
|
*** What a sync checks
|
||
|
|
|
||
|
|
A file of another device is skipped, and reported, when:
|
||
|
|
|
||
|
|
- its name is not ~<uuid>.bin~ (a syncer's conflicted copy, for example);
|
||
|
|
- it is not a device file, or of a newer version;
|
||
|
|
- it is of another vault, or encrypted with a key this device does not have;
|
||
|
|
- it does not decrypt: it was changed, or written with another key;
|
||
|
|
- its header or its document names another device than its file name.
|
||
|
|
|
||
|
|
A file whose revision is older than one this device merged before is merged,
|
||
|
|
and reported: the storage gave back an older copy. It cannot undo anything,
|
||
|
|
since its entries lose to the newer ones.
|
||
|
|
|
||
|
|
** What the storage provider sees
|
||
|
|
|
||
|
|
- The number of devices, from the number of files, and their random ids.
|
||
|
|
- The size of each file, in steps of 16 KiB.
|
||
|
|
- When each file changes.
|
||
|
|
- The vault id and each file's revision, in the headers.
|
||
|
|
|
||
|
|
Not the addresses, the titles, the tags, the archive links, the times or
|
||
|
|
the exact number of bookmarks.
|
||
|
|
|
||
|
|
** Changes from the plan
|
||
|
|
|
||
|
|
The format we agreed on, with these changes:
|
||
|
|
|
||
|
|
- The cipher is AEGIS-256 or XChaCha20-Poly1305, not AES-256-GCM. So the
|
||
|
|
header has a cipher byte, and the nonce is 32 or 24 bytes, not 12.
|
||
|
|
- The contents have a header of their own: the compression, and the two
|
||
|
|
lengths that Brotli needs and that padding hides.
|
||
|
|
- ~seen~ is in the document, as planned, so ~device.json~ holds only the
|
||
|
|
two ids. The local folder holds a copy of ~keys.bin~ too.
|
||
|
|
- The field for the instant is ~added~, and the archive link is ~archive~.
|
||
|
|
There is no folder, order or note yet. A later version can add them, and
|
||
|
|
this one keeps them (see /Fields from a newer version are kept/).
|
||
|
|
|
||
|
|
** Not done
|
||
|
|
|
||
|
|
- WebDAV: a second case of ~Storage~.
|
||
|
|
- A second secret for the password slot (a key in a QR code, to pair a
|
||
|
|
device), and a slot for a recovery code. The slot kinds leave room for both.
|
||
|
|
- Removing old tombstones.
|
||
|
|
- A way to change the password in the two apps.
|
||
|
|
- Merging field by field, so that changes to different fields of one
|
||
|
|
bookmark both win.
|
||
|
|
|
||
|
|
** Try it
|
||
|
|
|
||
|
|
#+BEGIN_SRC sh
|
||
|
|
bjo run tests/bookmark.bjo # stamps, JSON, and the merge's properties
|
||
|
|
bjo run tests/keys.bjo # keys.bin, and changes to it that must fail
|
||
|
|
bjo run tests/store.bjo # two and three devices in folders of the temp directory
|
||
|
|
bjo run tests/title.bjo # titles in bytes, and fetched from a server on the loopback
|
||
|
|
bjo run tests/import.bjo # a Firefox backup, read, and imported into a vault
|
||
|
|
bjo run tests/draft.bjo # the dialog of a bookmark
|
||
|
|
bjo run tests/search.bjo # the search: parsing, typos, tags, domains and days
|
||
|
|
bjo run tests/duplicates.bjo # a page bookmarked on two devices before they synced
|
||
|
|
#+END_SRC
|
||
|
|
|
||
|
|
All eight run on Linux x64. Nothing is run on Windows, macOS, Android or iOS
|
||
|
|
yet.
|