File: GPG.md

package info (click to toggle)
leiningen-clojure 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,312 kB
  • sloc: xml: 881; sh: 771; lisp: 46; makefile: 34; java: 16
file content (328 lines) | stat: -rw-r--r-- 12,815 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Using GPG](#using-gpg)
  - [What is it?](#what-is-it)
  - [Installing GPG](#installing-gpg)
    - [Linux](#linux)
        - [Debian based distributions](#debian-based-distributions)
      - [Fedora based distributions](#fedora-based-distributions)
    - [Mac](#mac)
    - [Windows](#windows)
  - [Creating a keypair](#creating-a-keypair)
  - [Listing keys](#listing-keys)
  - [Publishing your public key](#publishing-your-public-key)
  - [How Leiningen uses GPG](#how-leiningen-uses-gpg)
    - [Signing a file](#signing-a-file)
    - [Overriding the gpg defaults](#overriding-the-gpg-defaults)
  - [Troubleshooting](#troubleshooting)
    - [Debian based distributions](#debian-based-distributions-1)
      - [gpg: can't query passphrase in batch mode](#gpg-cant-query-passphrase-in-batch-mode)
    - [Mac OSX](#mac-osx)
      - [Unable to get GPG installed via Homebrew and OSX Keychain to work](#unable-to-get-gpg-installed-via-homebrew-and-osx-keychain-to-work)
      - [GPG doesn't ask for a passphrase](#gpg-doesnt-ask-for-a-passphrase)
    - [GPG prompts for passphrase but does not work with Leiningen](#gpg-prompts-for-passphrase-but-does-not-work-with-leiningen)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Using GPG

This is an introduction to setting up and using
[GPG](https://www.gnupg.org/) keys with
[Leiningen](https://leiningen.org) to sign artifacts for publication to
[Clojars](https://clojars.org/) and to encrypt repository credentials.

There are two versions of GPG available: v1.x and v2.x. For our
purposes, they are functionally equivalent. Package managers generally
install v2.x as `gpg2`, and v1.x as `gpg`, except for Homebrew which
installs v2.x as `gpg`, and v1.x as `gpg1`. By default, Leiningen
expects the GPG command to be `gpg`. You're welcome to use any version
you like, but this primer will only cover installing v1.x (except under
macOS), and has only been tested under v1.x.

## What is it?

[GPG](https://www.gnupg.org/) (or Gnu Privacy Guard) is a set of tools
for cryptographic key creation/management and encryption/signing of
data. If you are unfamiliar with the concepts of public key
cryptography, this
[Wikipedia entry](https://en.wikipedia.org/wiki/Public-key_cryptography)
serves as a good introduction.

An important concept to understand in public key cryptography is that
you are really dealing with two keys (a *keypair*): one public, the
other private (or secret). The public key can be freely shared, and is
used by yourself and others to encrypt data that only you, as the
holder of the private key, can decrypt. It can also be used to verify
the signature of a file, confirming that the file was signed by the
holder of the private key, and the contents of the file have not been
altered since it was signed. **You should guard your private key
and passphrase closely, and share them with no one.**

## Installing GPG

### Linux

##### Debian based distributions

Apt uses GPG v1, so it should already be installed. If not:

    apt-get install gnupg
    
#### Fedora based distributions

Fedora and friends may have GPG v2 installed by default, but GPG v1 is
available via:
    
    yum install gnupg
    
### Mac

There are several options here, depending on which package manager you
have installed (if any):

1. via [homebrew](https://brew.sh/): `brew install gnupg2`
2. via [macports](https://www.macports.org/): `port install gnupg2`
3. via a [binary installer](https://www.gpgtools.org/installer/index.html) 
   (this installs gpg2 as gpg)

### Windows

[GPG4Win](https://gpg4win.org/) provides a binary installer that
provides some possibly useful GUI tools in addition to providing the
`gpg` command.

## Creating a keypair

Create a keypair with:

    gpg --gen-key

This will prompt you for details about the keypair to be generated,
and store the resulting keypair in `~/.gnupg/`.

The default key type (RSA and RSA) is fine for our purposes, as is the
default keysize (2048). We recommend a validity period of 2 years. 

You'll be prompted for a passphrase to protect your private key - it's
important to use a strong one to help protect the integrity of your key.

## Listing keys

GPG stores keys in a keystore located in `~/.gnupg/`. This keystore
holds your keypair(s), along with any other public keys you have used.
    
To list all of the public keys in your keystore:

    gpg --list-keys
    
This will include any public keys you have used, including keys from
others (if you've never used GPG before and just created your first
keypair, you should just see your own key).

The output of the `--list-keys` option will include the id of your
public key in the 'pub' line in the key listing (you'll need that id 
for other commands described here):

    $ gpg --list-keys

                ↓↓↓↓↓↓↓↓
    pub   2048R/2ADFB13E 2013-03-16 [expires: 2014-03-16]
    uid                  Bob Bobson <bob@bobsons.net>
    sub   2048R/8D2344D0 2013-03-16 [expires: 2014-03-16]

The `--fingerprint` option will act just like `--list-keys`, but will
include the fingerprint of each certificate in the output. You can
filter the output of the `--fingerprint` option by providing a key id
or any substring from the uid (this trick also works for the
`--list-keys` option):

    $ gpg --fingerprint 2ADFB13E

    pub   2048R/2ADFB13E 2013-03-16 [expires: 2014-03-16]
          Key fingerprint = 3367 5FD0 D67B 3218 5815  51A3 97D4 06D0 2ADF B13E
    uid                  Bob Bobson <bob@bobsons.net>
    sub   2048R/8D2344D0 2013-03-16 [expires: 2014-03-16]

## Publishing your public key

To make it easier for others that need your public key to find it,
you can publish it to a key server with:

    gpg --send-keys 2ADFB13E # use your id instead

This pushes a copy of your public key to one of a cluster of free key
servers, and the key is propagated to all of the other servers in the
cluster in short order.

If your keypair is compromised, you can publish a revocation
certificate to the key server to let others know that your key can no
longer be trusted for any future signing or encryption. It's a good
idea to generate a revocation certificate whenever you create a new
keypair, and store it in a safe place. As long as you have that
revocation certificate, you can revoke a keypair even if you no longer
have the private key. You can generate a revocation certificate with:

    $ gpg --output 2ADFB13E-revoke.asc --gen-revoke 2ADFB13E

Be sure to protect your revocation certificate carefully - anyone who
gains access to it can use it to revoke your keypair. The GPG
maintainers recommend printing it out and storing the hardcopy in a
safe place.

To revoke your certificate **when the time comes (not now!)**, do the
following:

    $ gpg --import 2ADFB13E-revoke.asc  # ONLY WHEN YOU NEED TO REVOKE
    $ gpg --send-keys 2ADFB13E          # ONLY WHEN YOU NEED TO REVOKE

## How Leiningen uses GPG

Leiningen uses GPG for three things: decrypting credential files,
signing release artifacts, and signing tags. We'll focus on artifact
signing here; for information on credentials encryption/decryption,
see the
[deploy guide](https://github.com/technomancy/leiningen/blob/stable/doc/DEPLOY.md). Once
you are configured to sign releases, signing tags should be
straightforward.

On some systems you will be prompted for your GPG passphrase when it
is needed if you haven't entered it. If yours does not, you can
install [Keychain](https://github.com/funtoo/keychain), which provides
this functionality portably. 

### Signing a file

When you deploy a non-SNAPSHOT artifact via the `deploy`
task, Leiningen will attempt to create GPG signatures of the jar and
pom files. It does so by shelling out to `gpg` and using your default
private key to sign each artifact. This will create a signature file
for each artifact named by appending `.asc` to the artifact name.

Both signatures are then uploaded along with the artifacts. If you're
deploying to Clojars, you'll want to provide it with your *public* key
(see below) in order that the signatures can be verified.

To disable signing of releases, set `:sign-releases` to false in the
`:repositories` entry you are targeting. If you do this, everyone who
is depending on your library will not be able to confirm that the copy
they get has not been tampered with, so this is not recommended.

### Overriding the gpg defaults

By default, Leiningen will try to call GPG as `gpg`, which assumes
that `gpg` is in your path, and your GPG binary is actually called
`gpg`. If either of those are false, you can override the command
Leiningen uses for GPG by setting the `LEIN_GPG` environment variable.

GPG by default will select the first private key it finds (which will
be the first key listed by `gpg --list-secret-keys`). If you have
multiple keys and want to sign with one other than first, or want to
use specific keys for a particular release repository, you can specify
which key to use either globally, per-project, or
per-deploy-repository. All three places use the same configuration
syntax, it's all about where you put it. You can specify the key by id
or by the uid.

To set a key globally, add it to your user profile in
`~/.lein/profiles.clj`:

    {:user {...
            :signing {:gpg-key "2ADFB13E"}}} ;; using the key id

To set a key for a particular project, add it to the project
definition:

    (defproject ham-biscuit "0.1.0"
       ...
       :signing {:gpg-key "bob@bobsons.net"} ;; using the key uid
       ...)
    
To set a key for a particular deploy repository, add it to the
repository specification in your project definition:

    (defproject ham-biscuit "0.1.0"
       ...
       :deploy-repositories 
         [["releases" {:url "https://blueant.com/archiva/internal/releases"
                       :signing {:gpg-key "2ADFB13E"}}]
         ["snapshots" "https://blueant.com/archiva/internal/snapshots"]]
       ...)

## Troubleshooting

### Debian based distributions

#### gpg: can't query passphrase in batch mode

	Could not decrypt credentials from /home/xxx/.lein/credentials.clj.gpg
	gpg: can't query passphrase in batch mode
	gpg: decryption failed: secret key not available


This error happens with gpg 1.4.12. Make sure that you have `use-agent` option explicitly enabled in `~/.gnupg/gpg.conf`. See gpg option list.

You can test whether this solution works with

	gpg --quiet --batch --decrypt ~/.lein/credentials.clj.gpg

If the system asked your passphrase then problem solved.

### Mac OSX

#### Unable to get GPG installed via Homebrew and OSX Keychain to work

Installing GPG from here instead: https://www.gpgtools.org/installer/index.html

#### GPG doesn't ask for a passphrase

Make sure that you have "use-agent" option explicitly enabled in ~/.gnupg/gpg.conf. See gpg option list.

You can test the config with

    gpg --quiet --batch --decrypt ~/.lein/credentials.clj.gpg
    
Leiningen should pick it up automatically when the command above works correctly.

#### gpg: decryption failed: secret key not available

When you run

    gpg --quiet --batch --decrypt ~/.lein/credentials.clj.gpg

you get the error message

    gpg: decryption failed: secret key not available

try running it without `--quiet`

    gpg  --use-agent --decrypt ~/.lein/credentials.clj.gpg

If you get

    gpg: encrypted with RSA key, ID DEAD8F70
    gpg: decryption failed: secret key not available

run `gpg -k` and check that `DEAD8F70` is in the known keys list. If it isn't, `~/.lein/credentials.clj.gpg` may have been encrypted with a different key.

### GPG prompts for passphrase but does not work with Leiningen

    gpg --quiet --batch --decrypt ~/.lein/credentials.clj.gpg

It's hanging for a while after executing lein repl and then prints out these messages:

    $ lein repl
    Could not decrypt credentials from /Users/xxx/.lein/credentials.clj.gpg
    pinentry-curses: no LC_CTYPE known - assuming UTF-8
    pinentry-curses: no LC_CTYPE known - assuming UTF-8
    pinentry-curses: no LC_CTYPE known - assuming UTF-8
    pinentry-curses: no LC_CTYPE known - assuming UTF-8
    pinentry-curses: no LC_CTYPE known - assuming UTF-8
    gpg-agent[1009]: command get_passphrase failed: Invalid IPC response
    gpg: problem with the agent: Invalid IPC response
    gpg: decryption failed: No secret key
    
Leiningen can't present enter-passphrase page of gpg. It's an issue with Leiningen or gpg2 or gpg-agent or pinentry.

Use the GUI version of gpg, GPG Suite. Now when executing 'lein repl', it prompts for passphrase on the GUI instead.