GNU TeXmacsUser manualReferenceAboutEnglish
GnuPG encryption of documents

All encryption in TeXmacs is delegated to the external program gpg. The Scheme code in

progs/security/gpg/

builds the command lines, sends the data and the passphrases to the program, and stores the results in documents. Unless stated otherwise, file names below are relative to

src/TeXmacs/progs/

.

1.Enabling encryption

Encryption is an experimental feature. supports-gpg? (

security/gpg/gpg-base.scm

) is true only if

The menus only show the encryption entries when encryption is enabled: the Insert\(\rightarrow\)Fold\(\rightarrow\)Encrypt submenu (gpg-menu,

dynamic/fold-menu.scm

) and the Encryption submenu of Document (document-encryption-menu,

generic/document-menu.scm

). The key manager and the preferences are in

security/gpg/gpg-widgets.scm

.

2.Keys and the key directory

TeXmacs does not use the user's ordinary GnuPG keyring. Each TeXmacs user (in the sense of the user database, see the database) has its own key directory

$TEXMACS_HOME_PATH/users/default-user/gnupg

(gpg-homedir), which is created on demand and, except on Windows, made inaccessible to other users (chmod og-rwx). Every command is run with --homedir pointing to this directory, or to another directory given as an optional last argument (the wallet has its own, see the wallet).

gpg-base.scm offers the usual operations on this keyring:

Creation

gpg-gen-key creates an RSA key of 4096 bits from a name, an e-mail address, an optional comment and a passphrase, by sending a parameter file to gpg --gen-key.

Listing

gpg-public-keys and gpg-secret-keys parse the output of --with-colons; each key is a list of rows (pub/sec, fpr, uid, …). gpg-get-key-fingerprint, gpg-get-key-user-id, gpg-public-key-fingerprints, gpg-secret-key-fingerprints and the gpg-search-…-by-fingerprint functions query these lists.

Import, export, deletion

gpg-import-public-keys, gpg-import-secret-keys, gpg-export-public-keys, gpg-export-secret-keys, gpg-delete-public-key, gpg-delete-secret-and-public-key.

Default identity

The preference gpg default key fingerprint (gpg-get-default-key-fingerprint, gpg-set-default-key-fingerprint in

security/gpg/gpg-widgets.scm

), which is also stored as the user information gpg-key-fingerprint. It is proposed as a recipient when a region is encrypted.

Collected public keys

Public keys found in documents (see below) can be stored in collected-public-keys.scm in the key directory, a list of entries (fingerprint user-id armored-key) (gpg-collected-public-keys, gpg-add-collected-public-keys, gpg-import-public-key-from-collected, …).

3.Running gpg

All calls go through evaluate-system, the glue for evaluate_system (

src/src/System/Misc/sys_utils.cpp

). On Unix it starts the program with posix_spawn, without a shell (unix_system,

src/src/Plugins/Unix/unix_sys_utils.cpp

), writes given strings to given file descriptors of the child and collects given output descriptors. An input whose descriptor is \(- 1\) gets a fresh pipe, and the string $$i in the arguments is replaced by its descriptor number. TeXmacs uses this to send passphrases with --passphrase-fd $$1: passphrases never appear on the command line. The data to encrypt or decrypt are sent on standard input, and the result is read from standard output. The common options are

gpg --homedir dir --batch --no-tty --no-use-agent …

(gpg-executable-default). On failure, gpg-error reports the command line and the error output with report-system-error.

The two kinds of encryption are:

Public key encryption

gpg-encrypt data fingerprints runs --encrypt with one -r per recipient, --trust-model always and --armor. gpg-decrypt data passphrase runs --decrypt with the passphrase of the secret key.

Passphrase encryption

gpg-passphrase-encrypt data passphrase runs --symmetric with the cipher given by the preference gpg cipher algorithm (AES256 by default, or AES192); gpg-passphrase-decrypt uses the same command as gpg-decrypt.

gpg-decryptable? runs a decryption and only reports whether it succeeded; gpg-correct-passphrase? encrypts a test string for a key and checks whether the passphrase decrypts it. Results are armored ASCII strings, so they can be stored in documents. gpg-encrypt-save-object and gpg-load-decrypt-object store a Scheme object encrypted in a file; the wallet uses them.

4.Encrypted regions

A document may contain encrypted regions. The markup is defined in

src/TeXmacs/packages/standard/std-security.ts

, which is part of std:

gpg-decrypted, gpg-decrypted-block

A region in clear, followed by the fingerprints of its recipients.

gpg-encrypted, gpg-encrypted-block

The armored result of public key encryption, followed by the fingerprints.

gpg-passphrase-decrypted, gpg-passphrase-decrypted-block

A region in clear, to be encrypted with a passphrase.

gpg-passphrase-encrypted, gpg-passphrase-encrypted-block

The armored result of passphrase encryption.

The packages gpg-info-level-none, -short and -detailed (

packages/customize/encryption/

) set gpg-info-level, which determines how much information on the recipients is shown around decrypted blocks.

The operations are in

security/gpg/gpg-edit.scm

:

Insertion

tm-gpg-dialogue-insert-decrypted and the block variant ask for the recipients (gpg-widget-select-public-key-fingerprints) and wrap the subtree at the cursor; tm-gpg-insert-passphrase-decrypted and its block variant insert the tag with make.

Encryption

tm-gpg-encrypt serializes the body with serialize-texmacs, encrypts it for the recipients and replaces the tag by its encrypted counterpart; tm-gpg-dialogue-passphrase-encrypt first asks for a new passphrase (twice). Both then autosave the buffer.

Decryption

tm-gpg-dialogue-decrypt keeps the recipients whose secret key is in the keyring and tries them one after the other (gpg-try-decrypt): the passphrase of each key is taken from the wallet if possible, and asked for otherwise; a passphrase that works is stored in the wallet if the wallet is on persistently. The decrypted string is parsed back with parse-texmacs-snippet. tm-gpg-dialogue-passphrase-decrypt asks for the passphrase of a passphrase encrypted region.

Toggling

alternate-toggle and the focus menus and icons of

security/gpg/gpg-menu.scm

switch between the two forms; Recipients changes the recipients of a decrypted region. Structured insertion and removal of children are disabled for all these tags.

So that the recipients of a document can encrypt for each other, the public keys of all recipients are stored in the document itself, in the attachment gpg: a table from fingerprints to pairs (user-id armored-key) (gpg-set-ahash-table-attachment, gpg-get-ahash-table-attachment). tm-gpg-get-key-user-id uses it to display user identifiers, and the public keys can be imported into the keyring or into the collected keys from there.

5.Encrypted documents

A whole document can be encrypted with a passphrase (Document\(\rightarrow\)Encryption\(\rightarrow\)Passphrase encryption, tm-gpg-dialogue-passphrase-buffer-set-encryption). This stores the passphrase for the buffer, sets the initial environment variable encryption to gpg-passphrase and saves the buffer.

Passphrases of buffers.
They are kept in memory in the table gpg-buffer-passphrase-table of
gpg-edit.scm

, under the concrete file name of the buffer and under the name of its autosave file (suffix ~), and also in the wallet if it is on (gpg-set-buffer-passphrase). save-buffer-as-main is overloaded for encrypted buffers to copy the passphrase to the new name.

Saving.
export_tree (
src/src/Texmacs/Data/new_buffer.cpp

) checks whether a document in TeXmacs format has an initial variable encryption; if so, it replaces the document by the result of the Scheme function tree-export-encrypted before writing it. This function serializes the document, encrypts it with the passphrase registered for the target file name, and returns a new document with style generic whose body is a single gpg-passphrase-encrypted-buffer holding the armored data. The same hook encrypts autosave files, since they are written by buffer-export.

Loading.
The file then loads like any other document; load-buffer-open (
texmacs/texmacs/tm-files.scm

) notices the gpg-passphrase-encrypted-buffer tag and calls tm-gpg-dialogue-passphrase-decrypt-buffer. If encryption is not enabled, a dialog explains how to enable it. Otherwise the passphrase is taken from the wallet, or asked for; the decrypted document replaces the buffer contents (buffer-set) and the passphrase is registered for later saves.

Disabling.
tm-gpg-passphrase-buffer-unset-encryption removes the variable, forgets the passphrase and saves the buffer in clear.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".