xref: /linux/Documentation/process/maintainer-pgp-guide.rst (revision 55a42f78ffd386e01a5404419f8c5ded7db70a21)
1.. _pgpguide:
2
3===========================
4Kernel Maintainer PGP guide
5===========================
6
7:Author: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
8
9This document is aimed at Linux kernel developers, and especially at
10subsystem maintainers. It contains a subset of information discussed in
11the more general "`Protecting Code Integrity`_" guide published by the
12Linux Foundation. Please read that document for more in-depth discussion
13on some of the topics mentioned in this guide.
14
15.. _`Protecting Code Integrity`: https://github.com/lfit/itpol/blob/master/protecting-code-integrity.md
16
17The role of PGP in Linux Kernel development
18===========================================
19
20PGP helps ensure the integrity of the code that is produced by the Linux
21kernel development community and, to a lesser degree, establish trusted
22communication channels between developers via PGP-signed email exchange.
23
24The Linux kernel source code is available in two main formats:
25
26- Distributed source repositories (git)
27- Periodic release snapshots (tarballs)
28
29Both git repositories and tarballs carry PGP signatures of the kernel
30developers who create official kernel releases. These signatures offer a
31cryptographic guarantee that downloadable versions made available via
32kernel.org or any other mirrors are identical to what these developers
33have on their workstations. To this end:
34
35- git repositories provide PGP signatures on all tags
36- tarballs provide detached PGP signatures with all downloads
37
38.. _devs_not_infra:
39
40Trusting the developers, not infrastructure
41-------------------------------------------
42
43Ever since the 2011 compromise of core kernel.org systems, the main
44operating principle of the Kernel Archives project has been to assume
45that any part of the infrastructure can be compromised at any time. For
46this reason, the administrators have taken deliberate steps to emphasize
47that trust must always be placed with developers and never with the code
48hosting infrastructure, regardless of how good the security practices
49for the latter may be.
50
51The above guiding principle is the reason why this guide is needed. We
52want to make sure that by placing trust into developers we do not merely
53shift the blame for potential future security incidents to someone else.
54The goal is to provide a set of guidelines developers can use to create
55a secure working environment and safeguard the PGP keys used to
56establish the integrity of the Linux kernel itself.
57
58.. _pgp_tools:
59
60PGP tools
61=========
62
63Use GnuPG 2.4 or later
64----------------------
65
66Your distro should already have GnuPG installed by default, you just
67need to verify that you are using a reasonably recent version of it.
68To check, run::
69
70    $ gpg --version | head -n1
71
72If you have version 2.4 or above, then you are good to go. If you have
73an earlier version, then you are using a release of GnuPG that is no
74longer maintained and some commands from this guide may not work.
75
76Configure gpg-agent options
77~~~~~~~~~~~~~~~~~~~~~~~~~~~
78
79The GnuPG agent is a helper tool that will start automatically whenever
80you use the ``gpg`` command and run in the background with the purpose
81of caching the private key passphrase. There are two options you should
82know in order to tweak when the passphrase should be expired from cache:
83
84- ``default-cache-ttl`` (seconds): If you use the same key again before
85  the time-to-live expires, the countdown will reset for another period.
86  The default is 600 (10 minutes).
87- ``max-cache-ttl`` (seconds): Regardless of how recently you've used
88  the key since initial passphrase entry, if the maximum time-to-live
89  countdown expires, you'll have to enter the passphrase again. The
90  default is 30 minutes.
91
92If you find either of these defaults too short (or too long), you can
93edit your ``~/.gnupg/gpg-agent.conf`` file to set your own values::
94
95    # set to 30 minutes for regular ttl, and 2 hours for max ttl
96    default-cache-ttl 1800
97    max-cache-ttl 7200
98
99.. note::
100
101    It is no longer necessary to start gpg-agent manually at the
102    beginning of your shell session. You may want to check your rc files
103    to remove anything you had in place for older versions of GnuPG, as
104    it may not be doing the right thing any more.
105
106.. _protect_your_key:
107
108Protect your PGP key
109====================
110
111This guide assumes that you already have a PGP key that you use for Linux
112kernel development purposes. If you do not yet have one, please see the
113"`Protecting Code Integrity`_" document mentioned earlier for guidance
114on how to create a new one.
115
116You should also make a new key if your current one is weaker than 2048
117bits (RSA).
118
119Understanding PGP Subkeys
120-------------------------
121
122A PGP key rarely consists of a single keypair -- usually it is a
123collection of independent subkeys that can be used for different
124purposes based on their capabilities, assigned at their creation time.
125PGP defines four capabilities that a key can have:
126
127- **[S]** keys can be used for signing
128- **[E]** keys can be used for encryption
129- **[A]** keys can be used for authentication
130- **[C]** keys can be used for certifying other keys
131
132The key with the **[C]** capability is often called the "master" key,
133but this terminology is misleading because it implies that the Certify
134key can be used in place of any of other subkey on the same chain (like
135a physical "master key" can be used to open locks made for other keys).
136Since this is not the case, this guide will refer to it as "the Certify
137key" to avoid any ambiguity.
138
139It is critical to fully understand the following:
140
1411. All subkeys are fully independent from each other. If you lose a
142   private subkey, it cannot be restored or recreated from any other
143   private key on your chain.
1442. With the exception of the Certify key, there can be multiple subkeys
145   with identical capabilities (e.g. you can have 2 valid encryption
146   subkeys, 3 valid signing subkeys, but only one valid certification
147   subkey). All subkeys are fully independent -- a message encrypted to
148   one **[E]** subkey cannot be decrypted with any other **[E]** subkey
149   you may also have.
1503. A single subkey may have multiple capabilities (e.g. your **[C]** key
151   can also be your **[S]** key).
152
153The key carrying the **[C]** (certify) capability is the only key that
154can be used to indicate relationship with other keys. Only the **[C]**
155key can be used to:
156
157- add or revoke other keys (subkeys) with S/E/A capabilities
158- add, change or revoke identities (uids) associated with the key
159- add or change the expiration date on itself or any subkey
160- sign other people's keys for web of trust purposes
161
162By default, GnuPG creates the following when generating new keys:
163
164- One subkey carrying both Certify and Sign capabilities (**[SC]**)
165- A separate subkey with the Encryption capability (**[E]**)
166
167If you used the default parameters when generating your key, then that
168is what you will have. You can verify by running ``gpg --list-secret-keys``,
169for example::
170
171    sec   ed25519 2022-12-20 [SC] [expires: 2024-12-19]
172          000000000000000000000000AAAABBBBCCCCDDDD
173    uid           [ultimate] Alice Dev <adev@kernel.org>
174    ssb   cv25519 2022-12-20 [E] [expires: 2024-12-19]
175
176The long line under the ``sec`` entry is your key fingerprint --
177whenever you see ``[fpr]`` in the examples below, that 40-character
178string is what it refers to.
179
180Ensure your passphrase is strong
181--------------------------------
182
183GnuPG uses passphrases to encrypt your private keys before storing them on
184disk. This way, even if your ``.gnupg`` directory is leaked or stolen in
185its entirety, the attackers cannot use your private keys without first
186obtaining the passphrase to decrypt them.
187
188It is absolutely essential that your private keys are protected by a
189strong passphrase. To set it or change it, use::
190
191    $ gpg --change-passphrase [fpr]
192
193Create a separate Signing subkey
194--------------------------------
195
196Our goal is to protect your Certify key by moving it to offline media,
197so if you only have a combined **[SC]** key, then you should create a
198separate signing subkey::
199
200    $ gpg --quick-addkey [fpr] ed25519 sign
201
202Back up your Certify key for disaster recovery
203----------------------------------------------
204
205The more signatures you have on your PGP key from other developers, the
206more reasons you have to create a backup version that lives on something
207other than digital media, for disaster recovery reasons.
208
209A good way to create a printable hardcopy of your private key is by
210using the ``paperkey`` software written for this very purpose. See ``man
211paperkey`` for more details on the output format and its benefits over
212other solutions. Paperkey should already be packaged for most
213distributions.
214
215Run the following command to create a hardcopy backup of your private
216key::
217
218    $ gpg --export-secret-key [fpr] | paperkey -o /tmp/key-backup.txt
219
220Print out that file, then take a pen and write your passphrase on the
221margin of the paper. **This is strongly recommended** because the key
222printout is still encrypted with that passphrase, and if you ever change
223it you will not remember what it used to be when you had created the
224backup -- *guaranteed*.
225
226Put the resulting printout and the hand-written passphrase into an envelope
227and store in a secure and well-protected place, preferably away from your
228home, such as your bank vault.
229
230.. note::
231
232    The key is still encrypted with your passphrase, so printing out
233    even to "cloud-integrated" modern printers should remain a
234    relatively safe operation.
235
236Back up your whole GnuPG directory
237----------------------------------
238
239.. warning::
240
241    **!!!Do not skip this step!!!**
242
243It is important to have a readily available backup of your PGP keys
244should you need to recover them. This is different from the
245disaster-level preparedness we did with ``paperkey``. You will also rely
246on these external copies whenever you need to use your Certify key --
247such as when making changes to your own key or signing other people's
248keys after conferences and summits.
249
250Start by getting an external media card (preferably two!) that you will
251use for backup purposes. You will need to create an encrypted partition
252on this device using LUKS -- refer to your distro's documentation on how
253to accomplish this.
254
255For the encryption passphrase, you can use the same one as on your
256PGP key.
257
258Once the encryption process is over, re-insert your device and make sure
259it gets properly mounted. Copy your entire ``.gnupg`` directory over to
260the encrypted storage::
261
262    $ cp -a ~/.gnupg /media/disk/foo/gnupg-backup
263
264You should now test to make sure everything still works::
265
266    $ gpg --homedir=/media/disk/foo/gnupg-backup --list-key [fpr]
267
268If you don't get any errors, then you should be good to go. Unmount the
269device, distinctly label it so you don't overwrite it by accident, and
270put in a safe place -- but not too far away, because you'll need to use
271it every now and again for things like editing identities, adding or
272revoking subkeys, or signing other people's keys.
273
274Remove the Certify key from your homedir
275----------------------------------------
276
277The files in our home directory are not as well protected as we like to
278think.  They can be leaked or stolen via many different means:
279
280- by accident when making quick homedir copies to set up a new workstation
281- by systems administrator negligence or malice
282- via poorly secured backups
283- via malware in desktop apps (browsers, pdf viewers, etc)
284- via coercion when crossing international borders
285
286Protecting your key with a good passphrase greatly helps reduce the risk
287of any of the above, but passphrases can be discovered via keyloggers,
288shoulder-surfing, or any number of other means. For this reason, the
289recommended setup is to remove your Certify key from your home directory
290and store it on offline storage.
291
292.. warning::
293
294    Please see the previous section and make sure you have backed up
295    your GnuPG directory in its entirety. What we are about to do will
296    render your key useless if you do not have a usable backup!
297
298First, identify the "keygrip" of your Certify key::
299
300    $ gpg --with-keygrip --list-key [fpr]
301
302The output will be something like this::
303
304    pub   ed25519 2022-12-20 [SC] [expires: 2022-12-19]
305          000000000000000000000000AAAABBBBCCCCDDDD
306          Keygrip = 1111000000000000000000000000000000000000
307    uid           [ultimate] Alice Dev <adev@kernel.org>
308    sub   cv25519 2022-12-20 [E] [expires: 2022-12-19]
309          Keygrip = 2222000000000000000000000000000000000000
310    sub   ed25519 2022-12-20 [S]
311          Keygrip = 3333000000000000000000000000000000000000
312
313Find the keygrip entry that is beneath the ``pub`` line (right under the
314Certify key fingerprint). This will correspond directly to a file in your
315``~/.gnupg`` directory::
316
317    $ cd ~/.gnupg/private-keys-v1.d
318    $ ls
319    1111000000000000000000000000000000000000.key
320    2222000000000000000000000000000000000000.key
321    3333000000000000000000000000000000000000.key
322
323It is sufficient to remove the .key file that corresponds to the Certify
324key keygrip::
325
326    $ cd ~/.gnupg/private-keys-v1.d
327    $ rm 1111000000000000000000000000000000000000.key
328
329Now, if you issue the ``--list-secret-keys`` command, it will show that
330the Certify key is missing (the ``#`` indicates it is not available)::
331
332    $ gpg --list-secret-keys
333    sec#  ed25519 2022-12-20 [SC] [expires: 2024-12-19]
334          000000000000000000000000AAAABBBBCCCCDDDD
335    uid           [ultimate] Alice Dev <adev@kernel.org>
336    ssb   cv25519 2022-12-20 [E] [expires: 2024-12-19]
337    ssb   ed25519 2022-12-20 [S]
338
339You should also remove any ``secring.gpg`` files in the ``~/.gnupg``
340directory, which may be left over from previous versions of GnuPG.
341
342If you don't have the "private-keys-v1.d" directory
343~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344
345If you do not have a ``~/.gnupg/private-keys-v1.d`` directory, then your
346secret keys are still stored in the legacy ``secring.gpg`` file used by
347GnuPG v1. Making any changes to your key, such as changing the
348passphrase or adding a subkey, should automatically convert the old
349``secring.gpg`` format to use ``private-keys-v1.d`` instead.
350
351Once you get that done, make sure to delete the obsolete ``secring.gpg``
352file, which still contains your private keys.
353
354.. _smartcards:
355
356Move the subkeys to a dedicated crypto device
357=============================================
358
359Even though the Certify key is now safe from being leaked or stolen, the
360subkeys are still in your home directory. Anyone who manages to get
361their hands on those will be able to decrypt your communication or fake
362your signatures (if they know the passphrase). Furthermore, each time a
363GnuPG operation is performed, the keys are loaded into system memory and
364can be stolen from there by sufficiently advanced malware (think
365Meltdown and Spectre).
366
367A good way to completely protect your keys is to move them to a
368specialized hardware device that is capable of smartcard operations.
369
370The benefits of smartcards
371--------------------------
372
373A smartcard contains a cryptographic chip that is capable of storing
374private keys and performing crypto operations directly on the card
375itself. Because the key contents never leave the smartcard, the
376operating system of the computer into which you plug in the hardware
377device is not able to retrieve the private keys themselves. This is very
378different from the encrypted media storage device we used earlier for
379backup purposes -- while that device is plugged in and mounted, the
380operating system is able to access the private key contents.
381
382Using external encrypted media is not a substitute to having a
383smartcard-capable device.
384
385Available smartcard devices
386---------------------------
387
388Unless all your laptops and workstations have smartcard readers, the
389easiest is to get a specialized USB device that implements smartcard
390functionality. There are several options available:
391
392- `Nitrokey Start`_: Open hardware and Free Software, based on FSI
393  Japan's `Gnuk`_. One of the cheapest options, but offers fewest
394  security features (such as resistance to tampering or some
395  side-channel attacks).
396- `Nitrokey 3`_: Similar to the Nitrokey Start, but more
397  tamper-resistant and offers more security features and USB
398  form-factors. Supports ECC cryptography (ED25519 and NISTP).
399- `Yubikey 5`_: proprietary hardware and software, but cheaper than
400  Nitrokey with a similar set of features. Supports ECC cryptography
401  (ED25519 and NISTP).
402
403Your choice will depend on cost, shipping availability in your
404geographical region, and open/proprietary hardware considerations.
405
406.. note::
407
408    If you are listed in MAINTAINERS or have an account at kernel.org,
409    you `qualify for a free Nitrokey Start`_ courtesy of The Linux
410    Foundation.
411
412.. _`Nitrokey Start`: https://www.nitrokey.com/products/nitrokeys
413.. _`Nitrokey 3`: https://www.nitrokey.com/products/nitrokeys
414.. _`Yubikey 5`: https://www.yubico.com/products/yubikey-5-overview/
415.. _Gnuk: https://www.fsij.org/doc-gnuk/
416.. _`qualify for a free Nitrokey Start`: https://www.kernel.org/nitrokey-digital-tokens-for-kernel-developers.html
417
418Configure your smartcard device
419-------------------------------
420
421Your smartcard device should Just Work (TM) the moment you plug it into
422any modern Linux workstation. You can verify it by running::
423
424    $ gpg --card-status
425
426If you see full smartcard details, then you are good to go.
427Unfortunately, troubleshooting all possible reasons why things may not
428be working for you is way beyond the scope of this guide. If you are
429having trouble getting the card to work with GnuPG, please seek help via
430usual support channels.
431
432To configure your smartcard, you will need to use the GnuPG menu system, as
433there are no convenient command-line switches::
434
435    $ gpg --card-edit
436    [...omitted...]
437    gpg/card> admin
438    Admin commands are allowed
439    gpg/card> passwd
440
441You should set the user PIN (1), Admin PIN (3), and the Reset Code (4).
442Please make sure to record and store these in a safe place -- especially
443the Admin PIN and the Reset Code (which allows you to completely wipe
444the smartcard). You so rarely need to use the Admin PIN, that you will
445inevitably forget what it is if you do not record it.
446
447Getting back to the main card menu, you can also set other values (such
448as name, gender, login data, etc), but it's not necessary and will
449additionally leak information about your smartcard should you lose it.
450
451.. note::
452
453    Despite having the name "PIN", neither the user PIN nor the admin
454    PIN on the card need to be numbers.
455
456.. warning::
457
458    Some devices may require that you move the subkeys onto the device
459    before you can change the passphrase. Please check the documentation
460    provided by the device manufacturer.
461
462Move the subkeys to your smartcard
463----------------------------------
464
465Exit the card menu (using "q") and save all changes. Next, let's move
466your subkeys onto the smartcard. You will need both your PGP key
467passphrase and the admin PIN of the card for most operations::
468
469    $ gpg --edit-key [fpr]
470
471    Secret subkeys are available.
472
473    pub  ed25519/AAAABBBBCCCCDDDD
474         created: 2022-12-20  expires: 2024-12-19  usage: SC
475         trust: ultimate      validity: ultimate
476    ssb  cv25519/1111222233334444
477         created: 2022-12-20  expires: never       usage: E
478    ssb  ed25519/5555666677778888
479         created: 2017-12-07  expires: never       usage: S
480    [ultimate] (1). Alice Dev <adev@kernel.org>
481
482    gpg>
483
484Using ``--edit-key`` puts us into the menu mode again, and you will
485notice that the key listing is a little different. From here on, all
486commands are done from inside this menu mode, as indicated by ``gpg>``.
487
488First, let's select the key we'll be putting onto the card -- you do
489this by typing ``key 1`` (it's the first one in the listing, the **[E]**
490subkey)::
491
492    gpg> key 1
493
494In the output, you should now see ``ssb*`` on the **[E]** key. The ``*``
495indicates which key is currently "selected." It works as a *toggle*,
496meaning that if you type ``key 1`` again, the ``*`` will disappear and
497the key will not be selected any more.
498
499Now, let's move that key onto the smartcard::
500
501    gpg> keytocard
502    Please select where to store the key:
503       (2) Encryption key
504    Your selection? 2
505
506Since it's our **[E]** key, it makes sense to put it into the Encryption
507slot.  When you submit your selection, you will be prompted first for
508your PGP key passphrase, and then for the admin PIN. If the command
509returns without an error, your key has been moved.
510
511**Important**: Now type ``key 1`` again to unselect the first key, and
512``key 2`` to select the **[S]** key::
513
514    gpg> key 1
515    gpg> key 2
516    gpg> keytocard
517    Please select where to store the key:
518       (1) Signature key
519       (3) Authentication key
520    Your selection? 1
521
522You can use the **[S]** key both for Signature and Authentication, but
523we want to make sure it's in the Signature slot, so choose (1). Once
524again, if your command returns without an error, then the operation was
525successful::
526
527    gpg> q
528    Save changes? (y/N) y
529
530Saving the changes will delete the keys you moved to the card from your
531home directory (but it's okay, because we have them in our backups
532should we need to do this again for a replacement smartcard).
533
534Verifying that the keys were moved
535~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
536
537If you perform ``--list-secret-keys`` now, you will see a subtle
538difference in the output::
539
540    $ gpg --list-secret-keys
541    sec#  ed25519 2022-12-20 [SC] [expires: 2024-12-19]
542          000000000000000000000000AAAABBBBCCCCDDDD
543    uid           [ultimate] Alice Dev <adev@kernel.org>
544    ssb>  cv25519 2022-12-20 [E] [expires: 2024-12-19]
545    ssb>  ed25519 2022-12-20 [S]
546
547The ``>`` in the ``ssb>`` output indicates that the subkey is only
548available on the smartcard. If you go back into your secret keys
549directory and look at the contents there, you will notice that the
550``.key`` files there have been replaced with stubs::
551
552    $ cd ~/.gnupg/private-keys-v1.d
553    $ strings *.key | grep 'private-key'
554
555The output should contain ``shadowed-private-key`` to indicate that
556these files are only stubs and the actual content is on the smartcard.
557
558Verifying that the smartcard is functioning
559~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
560
561To verify that the smartcard is working as intended, you can create a
562signature::
563
564    $ echo "Hello world" | gpg --clearsign > /tmp/test.asc
565    $ gpg --verify /tmp/test.asc
566
567This should ask for your smartcard PIN on your first command, and then
568show "Good signature" after you run ``gpg --verify``.
569
570Congratulations, you have successfully made it extremely difficult to
571steal your digital developer identity!
572
573Other common GnuPG operations
574-----------------------------
575
576Here is a quick reference for some common operations you'll need to do
577with your PGP key.
578
579Mounting your safe offline storage
580~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
581
582You will need your Certify key for any of the operations below, so you
583will first need to mount your backup offline storage and tell GnuPG to
584use it::
585
586    $ export GNUPGHOME=/media/disk/foo/gnupg-backup
587    $ gpg --list-secret-keys
588
589You want to make sure that you see ``sec`` and not ``sec#`` in the
590output (the ``#`` means the key is not available and you're still using
591your regular home directory location).
592
593Extending key expiration date
594~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
595
596The Certify key has the default expiration date of 2 years from the date
597of creation. This is done both for security reasons and to make obsolete
598keys eventually disappear from keyservers.
599
600To extend the expiration on your key by a year from current date, just
601run::
602
603    $ gpg --quick-set-expire [fpr] 1y
604
605You can also use a specific date if that is easier to remember (e.g.
606your birthday, January 1st, or Canada Day)::
607
608    $ gpg --quick-set-expire [fpr] 2038-07-01
609
610Remember to send the updated key back to keyservers::
611
612    $ gpg --send-key [fpr]
613
614Updating your work directory after any changes
615~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
616
617After you make any changes to your key using the offline storage, you will
618want to import these changes back into your regular working directory::
619
620    $ gpg --export | gpg --homedir ~/.gnupg --import
621    $ unset GNUPGHOME
622
623Using gpg-agent over ssh
624~~~~~~~~~~~~~~~~~~~~~~~~
625
626You can forward your gpg-agent over ssh if you need to sign tags or
627commits on a remote system. Please refer to the instructions provided
628on the GnuPG wiki:
629
630- `Agent Forwarding over SSH`_
631
632It works more smoothly if you can modify the sshd server settings on the
633remote end.
634
635.. _`Agent Forwarding over SSH`: https://wiki.gnupg.org/AgentForwarding
636
637.. _pgp_with_git:
638
639Using PGP with Git
640==================
641
642One of the core features of Git is its decentralized nature -- once a
643repository is cloned to your system, you have full history of the
644project, including all of its tags, commits and branches. However, with
645hundreds of cloned repositories floating around, how does anyone verify
646that their copy of linux.git has not been tampered with by a malicious
647third party?
648
649Or what happens if malicious code is discovered in the kernel and the
650"Author" line in the commit says it was done by you, while you're pretty
651sure you had `nothing to do with it`_?
652
653To address both of these issues, Git introduced PGP integration. Signed
654tags prove the repository integrity by assuring that its contents are
655exactly the same as on the workstation of the developer who created the
656tag, while signed commits make it nearly impossible for someone to
657impersonate you without having access to your PGP keys.
658
659.. _`nothing to do with it`: https://github.com/jayphelps/git-blame-someone-else
660
661Configure git to use your PGP key
662---------------------------------
663
664If you only have one secret key in your keyring, then you don't really
665need to do anything extra, as it becomes your default key.  However, if
666you happen to have multiple secret keys, you can tell git which key
667should be used (``[fpr]`` is the fingerprint of your key)::
668
669    $ git config --global user.signingKey [fpr]
670
671How to work with signed tags
672----------------------------
673
674To create a signed tag, pass the ``-s`` switch to the tag command::
675
676    $ git tag -s [tagname]
677
678Our recommendation is to always sign git tags, as this allows other
679developers to ensure that the git repository they are pulling from has
680not been maliciously altered.
681
682How to verify signed tags
683~~~~~~~~~~~~~~~~~~~~~~~~~
684
685To verify a signed tag, use the ``verify-tag`` command::
686
687    $ git verify-tag [tagname]
688
689If you are pulling a tag from another fork of the project repository,
690git should automatically verify the signature at the tip you're pulling
691and show you the results during the merge operation::
692
693    $ git pull [url] tags/sometag
694
695The merge message will contain something like this::
696
697    Merge tag 'sometag' of [url]
698
699    [Tag message]
700
701    # gpg: Signature made [...]
702    # gpg: Good signature from [...]
703
704If you are verifying someone else's git tag, you will first need to
705import their PGP key. Please refer to the ":ref:`verify_identities`"
706section below.
707
708Configure git to always sign annotated tags
709~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
710
711Chances are, if you're creating an annotated tag, you'll want to sign
712it. To force git to always sign annotated tags, you can set a global
713configuration option::
714
715    $ git config --global tag.forceSignAnnotated true
716
717How to work with signed commits
718-------------------------------
719
720It is also possible to create signed commits, but they have limited
721usefulness in Linux kernel development. The kernel contribution workflow
722relies on sending in patches, and converting commits to patches does not
723preserve git commit signatures. Furthermore, when rebasing your own
724repository on a newer upstream, PGP commit signatures will end up
725discarded. For this reason, most kernel developers don't bother signing
726their commits and will ignore signed commits in any external
727repositories that they rely upon in their work.
728
729That said, if you have your working git tree publicly available at some
730git hosting service (kernel.org, infradead.org, ozlabs.org, or others),
731then the recommendation is that you sign all your git commits even if
732upstream developers do not directly benefit from this practice.
733
734We recommend this for the following reasons:
735
7361. Should there ever be a need to perform code forensics or track code
737   provenance, even externally maintained trees carrying PGP commit
738   signatures will be valuable for such purposes.
7392. If you ever need to re-clone your local repository (for example,
740   after reinstalling your system), this lets you verify the repository
741   integrity before resuming your work.
7423. If someone needs to cherry-pick your commits, this allows them to
743   quickly verify their integrity before applying them.
744
745Creating signed commits
746~~~~~~~~~~~~~~~~~~~~~~~
747
748To create a signed commit, pass the ``-S`` flag to the ``git commit``
749command (it's capital ``-S`` due to collision with another flag)::
750
751    $ git commit -S
752
753Configure git to always sign commits
754~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
755
756You can tell git to always sign commits::
757
758    git config --global commit.gpgSign true
759
760.. note::
761
762    Make sure you configure ``gpg-agent`` before you turn this on.
763
764.. _verify_identities:
765
766How to work with signed patches
767-------------------------------
768
769It is possible to use your PGP key to sign patches sent to kernel
770developer mailing lists. Since existing email signature mechanisms
771(PGP-Mime or PGP-inline) tend to cause problems with regular code
772review tasks, you should use the tool kernel.org created for this
773purpose that puts cryptographic attestation signatures into message
774headers (a-la DKIM):
775
776- `Patatt Patch Attestation`_
777
778.. _`Patatt Patch Attestation`: https://pypi.org/project/patatt/
779
780Installing and configuring patatt
781~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
782
783.. note::
784
785    If you use B4 to send in your patches, patatt is already installed
786    and integrated into your workflow.
787
788Patatt is packaged for many distributions already, so please check there
789first. You can also install it from pypi using "``pip install patatt``".
790
791If you already have your PGP key configured with git (via the
792``user.signingKey`` configuration parameter), then patatt requires no
793further configuration. You can start signing your patches by installing
794the git-send-email hook in the repository you want::
795
796    patatt install-hook
797
798Now any patches you send with ``git send-email`` will be automatically
799signed with your cryptographic signature.
800
801Checking patatt signatures
802~~~~~~~~~~~~~~~~~~~~~~~~~~
803
804If you are using ``b4`` to retrieve and apply patches, then it will
805automatically attempt to verify all DKIM and patatt signatures it
806encounters, for example::
807
808    $ b4 am 20220720205013.890942-1-broonie@kernel.org
809    [...]
810    Checking attestation on all messages, may take a moment...
811    ---
812      ✓ [PATCH v1 1/3] kselftest/arm64: Correct buffer allocation for SVE Z registers
813      ✓ [PATCH v1 2/3] arm64/sve: Document our actual ABI for clearing registers on syscall
814      ✓ [PATCH v1 3/3] kselftest/arm64: Enforce actual ABI for SVE syscalls
815      ---
816      ✓ Signed: openpgp/broonie@kernel.org
817      ✓ Signed: DKIM/kernel.org
818
819.. note::
820
821    Patatt and b4 are still in active development and you should check
822    the latest documentation for these projects for any new or updated
823    features.
824
825.. _kernel_identities:
826
827How to verify kernel developer identities
828=========================================
829
830Signing tags and commits is straightforward, but how does one go about
831verifying that the key used to sign something belongs to the actual
832kernel developer and not to a malicious imposter?
833
834Configure auto-key-retrieval using WKD and DANE
835-----------------------------------------------
836
837If you are not already someone with an extensive collection of other
838developers' public keys, then you can jumpstart your keyring by relying
839on key auto-discovery and auto-retrieval. GnuPG can piggyback on other
840delegated trust technologies, namely DNSSEC and TLS, to get you going if
841the prospect of starting your own Web of Trust from scratch is too
842daunting.
843
844Add the following to your ``~/.gnupg/gpg.conf``::
845
846    auto-key-locate wkd,dane,local
847    auto-key-retrieve
848
849DNS-Based Authentication of Named Entities ("DANE") is a method for
850publishing public keys in DNS and securing them using DNSSEC signed
851zones. Web Key Directory ("WKD") is the alternative method that uses
852https lookups for the same purpose. When using either DANE or WKD for
853looking up public keys, GnuPG will validate DNSSEC or TLS certificates,
854respectively, before adding auto-retrieved public keys to your local
855keyring.
856
857Kernel.org publishes the WKD for all developers who have kernel.org
858accounts. Once you have the above changes in your ``gpg.conf``, you can
859auto-retrieve the keys for Linus Torvalds and Greg Kroah-Hartman (if you
860don't already have them)::
861
862    $ gpg --locate-keys torvalds@kernel.org gregkh@kernel.org
863
864If you have a kernel.org account, then you should `add the kernel.org
865UID to your key`_ to make WKD more useful to other kernel developers.
866
867.. _`add the kernel.org UID to your key`: https://korg.wiki.kernel.org/userdoc/mail#adding_a_kernelorg_uid_to_your_pgp_key
868
869Web of Trust (WOT) vs. Trust on First Use (TOFU)
870------------------------------------------------
871
872PGP incorporates a trust delegation mechanism known as the "Web of
873Trust." At its core, this is an attempt to replace the need for
874centralized Certification Authorities of the HTTPS/TLS world. Instead of
875various software makers dictating who should be your trusted certifying
876entity, PGP leaves this responsibility to each user.
877
878Unfortunately, very few people understand how the Web of Trust works.
879While it is still an important part of the OpenPGP specification,
880recent versions of GnuPG (2.2 and above) have implemented an alternative
881mechanism called "Trust on First Use" (TOFU). You can think of TOFU as
882"the SSH-like approach to trust." With SSH, the first time you connect
883to a remote system, its key fingerprint is recorded and remembered. If
884the key changes in the future, the SSH client will alert you and refuse
885to connect, forcing you to make a decision on whether you choose to
886trust the changed key or not. Similarly, the first time you import
887someone's PGP key, it is assumed to be valid. If at any point in the
888future GnuPG comes across another key with the same identity, both the
889previously imported key and the new key will be marked for verification
890and you will need to manually figure out which one to keep.
891
892We recommend that you use the combined TOFU+PGP trust model (which is
893the new default in GnuPG v2). To set it, add (or modify) the
894``trust-model`` setting in ``~/.gnupg/gpg.conf``::
895
896    trust-model tofu+pgp
897
898Using the kernel.org web of trust repository
899--------------------------------------------
900
901Kernel.org maintains a git repository with developers' public keys as a
902replacement for replicating keyserver networks that have gone mostly
903dark in the past few years. The full documentation for how to set up
904that repository as your source of public keys can be found here:
905
906- `Kernel developer PGP Keyring`_
907
908If you are a kernel developer, please consider submitting your key for
909inclusion into that keyring.
910
911.. _`Kernel developer PGP Keyring`: https://korg.docs.kernel.org/pgpkeys.html
912