github.com/StackExchange/blackbox/v2@v2.0.1-0.20220331193400-d84e904973ab/pkg/crypters/gnupg/keychain.go (about) 1 package gnupg 2 3 /* 4 5 # How does Blackbox manage key rings? 6 7 Blackbox uses the user's .gnupg directory for most actions, such as decrypting data. 8 Decrypting requires the user's private key, which is stored by the user in their 9 home directory (and up to them to store safely). 10 Black box does not store the user's private key in the repo. 11 12 When encrypting data, black needs the public key of all the admins, not just the users. 13 To assure that the user's `.gnupg` has all these public keys, prior to 14 encrypting data the public keys are imported from .blackbox, which stores 15 a keychain that stores the public (not private!) keys of all the admins. 16 17 FYI: v1 does this import before decrypting, because I didn't know any better. 18 19 # Binary compatibility: 20 21 When writing v1, we didn't realize that the pubkey.gpg file is a binary format 22 that is not intended to be portable. In fact, it is intentionally not portable. 23 This means that all admins must use the exact same version of GnuPG 24 or the files (pubring.gpg or pubring.kbx) may get corrupted. 25 26 In v2, we store the public keys in the portable ascii format 27 in a file called `.blackbox/public-keys-db.asc`. 28 It will also update the binary files if they exist. 29 If `.blackbox/public-keys-db.asc` doesn't exist, it will be created. 30 31 Eventually we will stop updating the binary files. 32 33 # Importing public keys to the user 34 35 How to import the public keys to the user's GPG system: 36 37 If pubkeyring-ascii.txt exists: 38 gpg --import pubkeyring-ascii.asc 39 Else if pubring.kbx 40 gpg --import pubring.kbx 41 Else if pubring.gpg 42 gpg --import pubring.gpg 43 44 This is what v1 does: 45 #if gpg2 is installed next to gpg like on ubuntu 16 46 if [[ "$GPG" != "gpg2" ]]; then 47 $GPG --export --no-default-keyring --keyring "$(get_pubring_path)" >"$keyringasc" 48 $GPG --import "$keyringasc" 2>&1 | egrep -v 'not changed$' >&2 49 Else 50 $GPG --keyring "$(get_pubring_path)" --export | $GPG --import 51 fi 52 53 # How to add a key to the keyring? 54 55 Old, binary format: 56 # Get the key they want to add: 57 FOO is a user-specified directory, otherwise $HOME/.gnupg: 58 $GPG --homedir="FOO" --export -a "$KEYNAME" >TEMPFILE 59 # Import into the binary files: 60 KEYRINGDIR is .blackbox 61 $GPG --no-permission-warning --homedir="$KEYRINGDIR" --import TEMPFILE 62 # Git add any of these files if they exist: 63 pubring.gpg pubring.kbx trustdb.gpg blackbox-admins.txt 64 # Tell the user to git commit them. 65 66 New, ascii format: 67 # Get the key to be added. Write to a TEMPFILE 68 FOO is a user-specified directory, otherwise $HOME/.gnupg: 69 $GPG --homedir="FOO" --export -a "$KEYNAME" >TEMPFILE 70 # Make a tempdir called TEMPDIR 71 # Import the pubkeyring-ascii.txt to TEMPDIR's keyring. (Skip if file not found) 72 # Import the temp1 data to TEMPDIR 73 # Export the TEMPDIR to create a new .blackbox/pubkeyring-ascii.txt 74 PATH_TO_BINARY is the path to .blackbox/pubring.gpg; if that's not found then pubring.kbx 75 $GPG --keyring PATH_TO_BINARY --export -a --output .blackbox/pubkeyring-ascii.txt 76 # Git add .blackbox/pubkeyring-ascii.txt and .blackbox/blackbox-admins.txt 77 # Tell the user to git commit them. 78 # Delete TEMPDIR 79 80 # How to remove a key from the keyring? 81 82 Old, binary format: 83 # Remove key from the binary file 84 $GPG --no-permission-warning --homedir="$KEYRINGDIR" --batch --yes --delete-key "$KEYNAME" || true 85 # Git add any of these files if they exist: 86 pubring.gpg pubring.kbx trustdb.gpg blackbox-admins.txt 87 # Tell the user to git commit them. 88 89 New, ascii format: 90 # Make a tempdir called TEMPDIR 91 # Import the pubkeyring-ascii.txt to TEMPDIR's keyring. (Skip if file not found) 92 # Remove key from the ring file 93 $GPG --no-permission-warning --homedir="$KEYRINGDIR" --batch --yes --delete-key "$KEYNAME" || true 94 # Export the TEMPDIR to create a new .blackbox/pubkeyring-ascii.txt 95 PATH_TO_BINARY is the path to .blackbox/pubring.gpg; if that's not found then pubring.kbx 96 $GPG --keyring PATH_TO_BINARY --export -a --output .blackbox/pubkeyring-ascii.txt 97 # Git add .blackbox/pubkeyring-ascii.txt and .blackbox/blackbox-admins.txt 98 # Update the .blackbox copy of pubring.gpg, pubring.kbx, or trustdb.gpg (if they exist) 99 # with copies from TEMPDIR (if they exist). Git add any files that are updated. 100 # Tell the user to git commit them. 101 # Delete TEMPDIR 102 103 */ 104 105 //func prepareUserKeychain() error { 106 // return nil 107 //}