Back to home page

OSCL-LXR

 
 

    


0001 .. _pgpguide:
0002 
0003 ===========================
0004 Kernel Maintainer PGP guide
0005 ===========================
0006 
0007 :Author: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
0008 
0009 This document is aimed at Linux kernel developers, and especially at
0010 subsystem maintainers. It contains a subset of information discussed in
0011 the more general "`Protecting Code Integrity`_" guide published by the
0012 Linux Foundation. Please read that document for more in-depth discussion
0013 on some of the topics mentioned in this guide.
0014 
0015 .. _`Protecting Code Integrity`: https://github.com/lfit/itpol/blob/master/protecting-code-integrity.md
0016 
0017 The role of PGP in Linux Kernel development
0018 ===========================================
0019 
0020 PGP helps ensure the integrity of the code that is produced by the Linux
0021 kernel development community and, to a lesser degree, establish trusted
0022 communication channels between developers via PGP-signed email exchange.
0023 
0024 The Linux kernel source code is available in two main formats:
0025 
0026 - Distributed source repositories (git)
0027 - Periodic release snapshots (tarballs)
0028 
0029 Both git repositories and tarballs carry PGP signatures of the kernel
0030 developers who create official kernel releases. These signatures offer a
0031 cryptographic guarantee that downloadable versions made available via
0032 kernel.org or any other mirrors are identical to what these developers
0033 have on their workstations. To this end:
0034 
0035 - git repositories provide PGP signatures on all tags
0036 - tarballs provide detached PGP signatures with all downloads
0037 
0038 .. _devs_not_infra:
0039 
0040 Trusting the developers, not infrastructure
0041 -------------------------------------------
0042 
0043 Ever since the 2011 compromise of core kernel.org systems, the main
0044 operating principle of the Kernel Archives project has been to assume
0045 that any part of the infrastructure can be compromised at any time. For
0046 this reason, the administrators have taken deliberate steps to emphasize
0047 that trust must always be placed with developers and never with the code
0048 hosting infrastructure, regardless of how good the security practices
0049 for the latter may be.
0050 
0051 The above guiding principle is the reason why this guide is needed. We
0052 want to make sure that by placing trust into developers we do not simply
0053 shift the blame for potential future security incidents to someone else.
0054 The goal is to provide a set of guidelines developers can use to create
0055 a secure working environment and safeguard the PGP keys used to
0056 establish the integrity of the Linux kernel itself.
0057 
0058 .. _pgp_tools:
0059 
0060 PGP tools
0061 =========
0062 
0063 Use GnuPG v2
0064 ------------
0065 
0066 Your distro should already have GnuPG installed by default, you just
0067 need to verify that you are using version 2.x and not the legacy 1.4
0068 release -- many distributions still package both, with the default
0069 ``gpg`` command invoking GnuPG v.1. To check, run::
0070 
0071     $ gpg --version | head -n1
0072 
0073 If you see ``gpg (GnuPG) 1.4.x``, then you are using GnuPG v.1. Try the
0074 ``gpg2`` command (if you don't have it, you may need to install the
0075 gnupg2 package)::
0076 
0077     $ gpg2 --version | head -n1
0078 
0079 If you see ``gpg (GnuPG) 2.x.x``, then you are good to go. This guide
0080 will assume you have the version 2.2 of GnuPG (or later). If you are
0081 using version 2.0 of GnuPG, then some of the commands in this guide will
0082 not work, and you should consider installing the latest 2.2 version of
0083 GnuPG. Versions of gnupg-2.1.11 and later should be compatible for the
0084 purposes of this guide as well.
0085 
0086 If you have both ``gpg`` and ``gpg2`` commands, you should make sure you
0087 are always using GnuPG v2, not the legacy version. You can enforce this
0088 by setting the appropriate alias::
0089 
0090     $ alias gpg=gpg2
0091 
0092 You can put that in your ``.bashrc`` to make sure it's always the case.
0093 
0094 Configure gpg-agent options
0095 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0096 
0097 The GnuPG agent is a helper tool that will start automatically whenever
0098 you use the ``gpg`` command and run in the background with the purpose
0099 of caching the private key passphrase. There are two options you should
0100 know in order to tweak when the passphrase should be expired from cache:
0101 
0102 - ``default-cache-ttl`` (seconds): If you use the same key again before
0103   the time-to-live expires, the countdown will reset for another period.
0104   The default is 600 (10 minutes).
0105 - ``max-cache-ttl`` (seconds): Regardless of how recently you've used
0106   the key since initial passphrase entry, if the maximum time-to-live
0107   countdown expires, you'll have to enter the passphrase again. The
0108   default is 30 minutes.
0109 
0110 If you find either of these defaults too short (or too long), you can
0111 edit your ``~/.gnupg/gpg-agent.conf`` file to set your own values::
0112 
0113     # set to 30 minutes for regular ttl, and 2 hours for max ttl
0114     default-cache-ttl 1800
0115     max-cache-ttl 7200
0116 
0117 .. note::
0118 
0119     It is no longer necessary to start gpg-agent manually at the
0120     beginning of your shell session. You may want to check your rc files
0121     to remove anything you had in place for older versions of GnuPG, as
0122     it may not be doing the right thing any more.
0123 
0124 Set up a refresh cronjob
0125 ~~~~~~~~~~~~~~~~~~~~~~~~
0126 
0127 You will need to regularly refresh your keyring in order to get the
0128 latest changes on other people's public keys, which is best done with a
0129 daily cronjob::
0130 
0131     @daily /usr/bin/gpg2 --refresh >/dev/null 2>&1
0132 
0133 Check the full path to your ``gpg`` or ``gpg2`` command and use the
0134 ``gpg2`` command if regular ``gpg`` for you is the legacy GnuPG v.1.
0135 
0136 .. _master_key:
0137 
0138 Protect your master PGP key
0139 ===========================
0140 
0141 This guide assumes that you already have a PGP key that you use for Linux
0142 kernel development purposes. If you do not yet have one, please see the
0143 "`Protecting Code Integrity`_" document mentioned earlier for guidance
0144 on how to create a new one.
0145 
0146 You should also make a new key if your current one is weaker than 2048 bits
0147 (RSA).
0148 
0149 Master key vs. Subkeys
0150 ----------------------
0151 
0152 Subkeys are fully independent PGP keypairs that are tied to the "master"
0153 key using certifying key signatures (certificates). It is important to
0154 understand the following:
0155 
0156 1. There are no technical differences between the "master key" and "subkeys."
0157 2. At creation time, we assign functional limitations to each key by
0158    giving it specific capabilities.
0159 3. A PGP key can have 4 capabilities:
0160 
0161    - **[S]** key can be used for signing
0162    - **[E]** key can be used for encryption
0163    - **[A]** key can be used for authentication
0164    - **[C]** key can be used for certifying other keys
0165 
0166 4. A single key may have multiple capabilities.
0167 5. A subkey is fully independent from the master key. A message
0168    encrypted to a subkey cannot be decrypted with the master key. If you
0169    lose your private subkey, it cannot be recreated from the master key
0170    in any way.
0171 
0172 The key carrying the **[C]** (certify) capability is considered the
0173 "master" key because it is the only key that can be used to indicate
0174 relationship with other keys. Only the **[C]** key can be used to:
0175 
0176 - add or revoke other keys (subkeys) with S/E/A capabilities
0177 - add, change or revoke identities (uids) associated with the key
0178 - add or change the expiration date on itself or any subkey
0179 - sign other people's keys for web of trust purposes
0180 
0181 By default, GnuPG creates the following when generating new keys:
0182 
0183 - A master key carrying both Certify and Sign capabilities (**[SC]**)
0184 - A separate subkey with the Encryption capability (**[E]**)
0185 
0186 If you used the default parameters when generating your key, then that
0187 is what you will have. You can verify by running ``gpg --list-secret-keys``,
0188 for example::
0189 
0190     sec   rsa2048 2018-01-23 [SC] [expires: 2020-01-23]
0191           000000000000000000000000AAAABBBBCCCCDDDD
0192     uid           [ultimate] Alice Dev <adev@kernel.org>
0193     ssb   rsa2048 2018-01-23 [E] [expires: 2020-01-23]
0194 
0195 Any key carrying the **[C]** capability is your master key, regardless
0196 of any other capabilities it may have assigned to it.
0197 
0198 The long line under the ``sec`` entry is your key fingerprint --
0199 whenever you see ``[fpr]`` in the examples below, that 40-character
0200 string is what it refers to.
0201 
0202 Ensure your passphrase is strong
0203 --------------------------------
0204 
0205 GnuPG uses passphrases to encrypt your private keys before storing them on
0206 disk. This way, even if your ``.gnupg`` directory is leaked or stolen in
0207 its entirety, the attackers cannot use your private keys without first
0208 obtaining the passphrase to decrypt them.
0209 
0210 It is absolutely essential that your private keys are protected by a
0211 strong passphrase. To set it or change it, use::
0212 
0213     $ gpg --change-passphrase [fpr]
0214 
0215 Create a separate Signing subkey
0216 --------------------------------
0217 
0218 Our goal is to protect your master key by moving it to offline media, so
0219 if you only have a combined **[SC]** key, then you should create a separate
0220 signing subkey::
0221 
0222     $ gpg --quick-addkey [fpr] ed25519 sign
0223 
0224 Remember to tell the keyservers about this change, so others can pull down
0225 your new subkey::
0226 
0227     $ gpg --send-key [fpr]
0228 
0229 .. note:: ECC support in GnuPG
0230 
0231     GnuPG 2.1 and later has full support for Elliptic Curve
0232     Cryptography, with ability to combine ECC subkeys with traditional
0233     RSA master keys. The main upside of ECC cryptography is that it is
0234     much faster computationally and creates much smaller signatures when
0235     compared byte for byte with 2048+ bit RSA keys. Unless you plan on
0236     using a smartcard device that does not support ECC operations, we
0237     recommend that you create an ECC signing subkey for your kernel
0238     work.
0239 
0240     If for some reason you prefer to stay with RSA subkeys, just replace
0241     "ed25519" with "rsa2048" in the above command. Additionally, if you
0242     plan to use a hardware device that does not support ED25519 ECC
0243     keys, like Nitrokey Pro or a Yubikey, then you should use
0244     "nistp256" instead or "ed25519."
0245 
0246 
0247 Back up your master key for disaster recovery
0248 ---------------------------------------------
0249 
0250 The more signatures you have on your PGP key from other developers, the
0251 more reasons you have to create a backup version that lives on something
0252 other than digital media, for disaster recovery reasons.
0253 
0254 The best way to create a printable hardcopy of your private key is by
0255 using the ``paperkey`` software written for this very purpose. See ``man
0256 paperkey`` for more details on the output format and its benefits over
0257 other solutions. Paperkey should already be packaged for most
0258 distributions.
0259 
0260 Run the following command to create a hardcopy backup of your private
0261 key::
0262 
0263     $ gpg --export-secret-key [fpr] | paperkey -o /tmp/key-backup.txt
0264 
0265 Print out that file (or pipe the output straight to lpr), then take a
0266 pen and write your passphrase on the margin of the paper. **This is
0267 strongly recommended** because the key printout is still encrypted with
0268 that passphrase, and if you ever change it you will not remember what it
0269 used to be when you had created the backup -- *guaranteed*.
0270 
0271 Put the resulting printout and the hand-written passphrase into an envelope
0272 and store in a secure and well-protected place, preferably away from your
0273 home, such as your bank vault.
0274 
0275 .. note::
0276 
0277     Your printer is probably no longer a simple dumb device connected to
0278     your parallel port, but since the output is still encrypted with
0279     your passphrase, printing out even to "cloud-integrated" modern
0280     printers should remain a relatively safe operation. One option is to
0281     change the passphrase on your master key immediately after you are
0282     done with paperkey.
0283 
0284 Back up your whole GnuPG directory
0285 ----------------------------------
0286 
0287 .. warning::
0288 
0289     **!!!Do not skip this step!!!**
0290 
0291 It is important to have a readily available backup of your PGP keys
0292 should you need to recover them. This is different from the
0293 disaster-level preparedness we did with ``paperkey``. You will also rely
0294 on these external copies whenever you need to use your Certify key --
0295 such as when making changes to your own key or signing other people's
0296 keys after conferences and summits.
0297 
0298 Start by getting a small USB "thumb" drive (preferably two!) that you
0299 will use for backup purposes. You will need to encrypt them using LUKS
0300 -- refer to your distro's documentation on how to accomplish this.
0301 
0302 For the encryption passphrase, you can use the same one as on your
0303 master key.
0304 
0305 Once the encryption process is over, re-insert the USB drive and make
0306 sure it gets properly mounted. Copy your entire ``.gnupg`` directory
0307 over to the encrypted storage::
0308 
0309     $ cp -a ~/.gnupg /media/disk/foo/gnupg-backup
0310 
0311 You should now test to make sure everything still works::
0312 
0313     $ gpg --homedir=/media/disk/foo/gnupg-backup --list-key [fpr]
0314 
0315 If you don't get any errors, then you should be good to go. Unmount the
0316 USB drive, distinctly label it so you don't blow it away next time you
0317 need to use a random USB drive, and put in a safe place -- but not too
0318 far away, because you'll need to use it every now and again for things
0319 like editing identities, adding or revoking subkeys, or signing other
0320 people's keys.
0321 
0322 Remove the master key from  your homedir
0323 ----------------------------------------
0324 
0325 The files in our home directory are not as well protected as we like to
0326 think.  They can be leaked or stolen via many different means:
0327 
0328 - by accident when making quick homedir copies to set up a new workstation
0329 - by systems administrator negligence or malice
0330 - via poorly secured backups
0331 - via malware in desktop apps (browsers, pdf viewers, etc)
0332 - via coercion when crossing international borders
0333 
0334 Protecting your key with a good passphrase greatly helps reduce the risk
0335 of any of the above, but passphrases can be discovered via keyloggers,
0336 shoulder-surfing, or any number of other means. For this reason, the
0337 recommended setup is to remove your master key from your home directory
0338 and store it on offline storage.
0339 
0340 .. warning::
0341 
0342     Please see the previous section and make sure you have backed up
0343     your GnuPG directory in its entirety. What we are about to do will
0344     render your key useless if you do not have a usable backup!
0345 
0346 First, identify the keygrip of your master key::
0347 
0348     $ gpg --with-keygrip --list-key [fpr]
0349 
0350 The output will be something like this::
0351 
0352     pub   rsa2048 2018-01-24 [SC] [expires: 2020-01-24]
0353           000000000000000000000000AAAABBBBCCCCDDDD
0354           Keygrip = 1111000000000000000000000000000000000000
0355     uid           [ultimate] Alice Dev <adev@kernel.org>
0356     sub   rsa2048 2018-01-24 [E] [expires: 2020-01-24]
0357           Keygrip = 2222000000000000000000000000000000000000
0358     sub   ed25519 2018-01-24 [S]
0359           Keygrip = 3333000000000000000000000000000000000000
0360 
0361 Find the keygrip entry that is beneath the ``pub`` line (right under the
0362 master key fingerprint). This will correspond directly to a file in your
0363 ``~/.gnupg`` directory::
0364 
0365     $ cd ~/.gnupg/private-keys-v1.d
0366     $ ls
0367     1111000000000000000000000000000000000000.key
0368     2222000000000000000000000000000000000000.key
0369     3333000000000000000000000000000000000000.key
0370 
0371 All you have to do is simply remove the .key file that corresponds to
0372 the master keygrip::
0373 
0374     $ cd ~/.gnupg/private-keys-v1.d
0375     $ rm 1111000000000000000000000000000000000000.key
0376 
0377 Now, if you issue the ``--list-secret-keys`` command, it will show that
0378 the master key is missing (the ``#`` indicates it is not available)::
0379 
0380     $ gpg --list-secret-keys
0381     sec#  rsa2048 2018-01-24 [SC] [expires: 2020-01-24]
0382           000000000000000000000000AAAABBBBCCCCDDDD
0383     uid           [ultimate] Alice Dev <adev@kernel.org>
0384     ssb   rsa2048 2018-01-24 [E] [expires: 2020-01-24]
0385     ssb   ed25519 2018-01-24 [S]
0386 
0387 You should also remove any ``secring.gpg`` files in the ``~/.gnupg``
0388 directory, which are left over from earlier versions of GnuPG.
0389 
0390 If you don't have the "private-keys-v1.d" directory
0391 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0392 
0393 If you do not have a ``~/.gnupg/private-keys-v1.d`` directory, then your
0394 secret keys are still stored in the legacy ``secring.gpg`` file used by
0395 GnuPG v1. Making any changes to your key, such as changing the
0396 passphrase or adding a subkey, should automatically convert the old
0397 ``secring.gpg`` format to use ``private-keys-v1.d`` instead.
0398 
0399 Once you get that done, make sure to delete the obsolete ``secring.gpg``
0400 file, which still contains your private keys.
0401 
0402 .. _smartcards:
0403 
0404 Move the subkeys to a dedicated crypto device
0405 =============================================
0406 
0407 Even though the master key is now safe from being leaked or stolen, the
0408 subkeys are still in your home directory. Anyone who manages to get
0409 their hands on those will be able to decrypt your communication or fake
0410 your signatures (if they know the passphrase). Furthermore, each time a
0411 GnuPG operation is performed, the keys are loaded into system memory and
0412 can be stolen from there by sufficiently advanced malware (think
0413 Meltdown and Spectre).
0414 
0415 The best way to completely protect your keys is to move them to a
0416 specialized hardware device that is capable of smartcard operations.
0417 
0418 The benefits of smartcards
0419 --------------------------
0420 
0421 A smartcard contains a cryptographic chip that is capable of storing
0422 private keys and performing crypto operations directly on the card
0423 itself. Because the key contents never leave the smartcard, the
0424 operating system of the computer into which you plug in the hardware
0425 device is not able to retrieve the private keys themselves. This is very
0426 different from the encrypted USB storage device we used earlier for
0427 backup purposes -- while that USB device is plugged in and mounted, the
0428 operating system is able to access the private key contents.
0429 
0430 Using external encrypted USB media is not a substitute to having a
0431 smartcard-capable device.
0432 
0433 Available smartcard devices
0434 ---------------------------
0435 
0436 Unless all your laptops and workstations have smartcard readers, the
0437 easiest is to get a specialized USB device that implements smartcard
0438 functionality. There are several options available:
0439 
0440 - `Nitrokey Start`_: Open hardware and Free Software, based on FSI
0441   Japan's `Gnuk`_. One of the few available commercial devices that
0442   support ED25519 ECC keys, but offer fewest security features (such as
0443   resistance to tampering or some side-channel attacks).
0444 - `Nitrokey Pro 2`_: Similar to the Nitrokey Start, but more
0445   tamper-resistant and offers more security features. Pro 2 supports ECC
0446   cryptography (NISTP).
0447 - `Yubikey 5`_: proprietary hardware and software, but cheaper than
0448   Nitrokey Pro and comes available in the USB-C form that is more useful
0449   with newer laptops. Offers additional security features such as FIDO
0450   U2F, among others, and now finally supports ECC keys (NISTP).
0451 
0452 `LWN has a good review`_ of some of the above models, as well as several
0453 others. Your choice will depend on cost, shipping availability in your
0454 geographical region, and open/proprietary hardware considerations.
0455 
0456 .. note::
0457 
0458     If you are listed in MAINTAINERS or have an account at kernel.org,
0459     you `qualify for a free Nitrokey Start`_ courtesy of The Linux
0460     Foundation.
0461 
0462 .. _`Nitrokey Start`: https://shop.nitrokey.com/shop/product/nitrokey-start-6
0463 .. _`Nitrokey Pro 2`: https://shop.nitrokey.com/shop/product/nitrokey-pro-2-3
0464 .. _`Yubikey 5`: https://www.yubico.com/products/yubikey-5-overview/
0465 .. _Gnuk: https://www.fsij.org/doc-gnuk/
0466 .. _`LWN has a good review`: https://lwn.net/Articles/736231/
0467 .. _`qualify for a free Nitrokey Start`: https://www.kernel.org/nitrokey-digital-tokens-for-kernel-developers.html
0468 
0469 Configure your smartcard device
0470 -------------------------------
0471 
0472 Your smartcard device should Just Work (TM) the moment you plug it into
0473 any modern Linux workstation. You can verify it by running::
0474 
0475     $ gpg --card-status
0476 
0477 If you see full smartcard details, then you are good to go.
0478 Unfortunately, troubleshooting all possible reasons why things may not
0479 be working for you is way beyond the scope of this guide. If you are
0480 having trouble getting the card to work with GnuPG, please seek help via
0481 usual support channels.
0482 
0483 To configure your smartcard, you will need to use the GnuPG menu system, as
0484 there are no convenient command-line switches::
0485 
0486     $ gpg --card-edit
0487     [...omitted...]
0488     gpg/card> admin
0489     Admin commands are allowed
0490     gpg/card> passwd
0491 
0492 You should set the user PIN (1), Admin PIN (3), and the Reset Code (4).
0493 Please make sure to record and store these in a safe place -- especially
0494 the Admin PIN and the Reset Code (which allows you to completely wipe
0495 the smartcard). You so rarely need to use the Admin PIN, that you will
0496 inevitably forget what it is if you do not record it.
0497 
0498 Getting back to the main card menu, you can also set other values (such
0499 as name, sex, login data, etc), but it's not necessary and will
0500 additionally leak information about your smartcard should you lose it.
0501 
0502 .. note::
0503 
0504     Despite having the name "PIN", neither the user PIN nor the admin
0505     PIN on the card need to be numbers.
0506 
0507 .. warning::
0508 
0509     Some devices may require that you move the subkeys onto the device
0510     before you can change the passphrase. Please check the documentation
0511     provided by the device manufacturer.
0512 
0513 Move the subkeys to your smartcard
0514 ----------------------------------
0515 
0516 Exit the card menu (using "q") and save all changes. Next, let's move
0517 your subkeys onto the smartcard. You will need both your PGP key
0518 passphrase and the admin PIN of the card for most operations::
0519 
0520     $ gpg --edit-key [fpr]
0521 
0522     Secret subkeys are available.
0523 
0524     pub  rsa2048/AAAABBBBCCCCDDDD
0525          created: 2018-01-23  expires: 2020-01-23  usage: SC
0526          trust: ultimate      validity: ultimate
0527     ssb  rsa2048/1111222233334444
0528          created: 2018-01-23  expires: never       usage: E
0529     ssb  ed25519/5555666677778888
0530          created: 2017-12-07  expires: never       usage: S
0531     [ultimate] (1). Alice Dev <adev@kernel.org>
0532 
0533     gpg>
0534 
0535 Using ``--edit-key`` puts us into the menu mode again, and you will
0536 notice that the key listing is a little different. From here on, all
0537 commands are done from inside this menu mode, as indicated by ``gpg>``.
0538 
0539 First, let's select the key we'll be putting onto the card -- you do
0540 this by typing ``key 1`` (it's the first one in the listing, the **[E]**
0541 subkey)::
0542 
0543     gpg> key 1
0544 
0545 In the output, you should now see ``ssb*`` on the **[E]** key. The ``*``
0546 indicates which key is currently "selected." It works as a *toggle*,
0547 meaning that if you type ``key 1`` again, the ``*`` will disappear and
0548 the key will not be selected any more.
0549 
0550 Now, let's move that key onto the smartcard::
0551 
0552     gpg> keytocard
0553     Please select where to store the key:
0554        (2) Encryption key
0555     Your selection? 2
0556 
0557 Since it's our **[E]** key, it makes sense to put it into the Encryption
0558 slot.  When you submit your selection, you will be prompted first for
0559 your PGP key passphrase, and then for the admin PIN. If the command
0560 returns without an error, your key has been moved.
0561 
0562 **Important**: Now type ``key 1`` again to unselect the first key, and
0563 ``key 2`` to select the **[S]** key::
0564 
0565     gpg> key 1
0566     gpg> key 2
0567     gpg> keytocard
0568     Please select where to store the key:
0569        (1) Signature key
0570        (3) Authentication key
0571     Your selection? 1
0572 
0573 You can use the **[S]** key both for Signature and Authentication, but
0574 we want to make sure it's in the Signature slot, so choose (1). Once
0575 again, if your command returns without an error, then the operation was
0576 successful::
0577 
0578     gpg> q
0579     Save changes? (y/N) y
0580 
0581 Saving the changes will delete the keys you moved to the card from your
0582 home directory (but it's okay, because we have them in our backups
0583 should we need to do this again for a replacement smartcard).
0584 
0585 Verifying that the keys were moved
0586 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0587 
0588 If you perform ``--list-secret-keys`` now, you will see a subtle
0589 difference in the output::
0590 
0591     $ gpg --list-secret-keys
0592     sec#  rsa2048 2018-01-24 [SC] [expires: 2020-01-24]
0593           000000000000000000000000AAAABBBBCCCCDDDD
0594     uid           [ultimate] Alice Dev <adev@kernel.org>
0595     ssb>  rsa2048 2018-01-24 [E] [expires: 2020-01-24]
0596     ssb>  ed25519 2018-01-24 [S]
0597 
0598 The ``>`` in the ``ssb>`` output indicates that the subkey is only
0599 available on the smartcard. If you go back into your secret keys
0600 directory and look at the contents there, you will notice that the
0601 ``.key`` files there have been replaced with stubs::
0602 
0603     $ cd ~/.gnupg/private-keys-v1.d
0604     $ strings *.key | grep 'private-key'
0605 
0606 The output should contain ``shadowed-private-key`` to indicate that
0607 these files are only stubs and the actual content is on the smartcard.
0608 
0609 Verifying that the smartcard is functioning
0610 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0611 
0612 To verify that the smartcard is working as intended, you can create a
0613 signature::
0614 
0615     $ echo "Hello world" | gpg --clearsign > /tmp/test.asc
0616     $ gpg --verify /tmp/test.asc
0617 
0618 This should ask for your smartcard PIN on your first command, and then
0619 show "Good signature" after you run ``gpg --verify``.
0620 
0621 Congratulations, you have successfully made it extremely difficult to
0622 steal your digital developer identity!
0623 
0624 Other common GnuPG operations
0625 -----------------------------
0626 
0627 Here is a quick reference for some common operations you'll need to do
0628 with your PGP key.
0629 
0630 Mounting your master key offline storage
0631 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0632 
0633 You will need your master key for any of the operations below, so you
0634 will first need to mount your backup offline storage and tell GnuPG to
0635 use it::
0636 
0637     $ export GNUPGHOME=/media/disk/foo/gnupg-backup
0638     $ gpg --list-secret-keys
0639 
0640 You want to make sure that you see ``sec`` and not ``sec#`` in the
0641 output (the ``#`` means the key is not available and you're still using
0642 your regular home directory location).
0643 
0644 Extending key expiration date
0645 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0646 
0647 The master key has the default expiration date of 2 years from the date
0648 of creation. This is done both for security reasons and to make obsolete
0649 keys eventually disappear from keyservers.
0650 
0651 To extend the expiration on your key by a year from current date, just
0652 run::
0653 
0654     $ gpg --quick-set-expire [fpr] 1y
0655 
0656 You can also use a specific date if that is easier to remember (e.g.
0657 your birthday, January 1st, or Canada Day)::
0658 
0659     $ gpg --quick-set-expire [fpr] 2020-07-01
0660 
0661 Remember to send the updated key back to keyservers::
0662 
0663     $ gpg --send-key [fpr]
0664 
0665 Updating your work directory after any changes
0666 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0667 
0668 After you make any changes to your key using the offline storage, you will
0669 want to import these changes back into your regular working directory::
0670 
0671     $ gpg --export | gpg --homedir ~/.gnupg --import
0672     $ unset GNUPGHOME
0673 
0674 Using gpg-agent over ssh
0675 ~~~~~~~~~~~~~~~~~~~~~~~~
0676 
0677 You can forward your gpg-agent over ssh if you need to sign tags or
0678 commits on a remote system. Please refer to the instructions provided
0679 on the GnuPG wiki:
0680 
0681 - `Agent Forwarding over SSH`_
0682 
0683 It works more smoothly if you can modify the sshd server settings on the
0684 remote end.
0685 
0686 .. _`Agent Forwarding over SSH`: https://wiki.gnupg.org/AgentForwarding
0687 
0688 
0689 Using PGP with Git
0690 ==================
0691 
0692 One of the core features of Git is its decentralized nature -- once a
0693 repository is cloned to your system, you have full history of the
0694 project, including all of its tags, commits and branches. However, with
0695 hundreds of cloned repositories floating around, how does anyone verify
0696 that their copy of linux.git has not been tampered with by a malicious
0697 third party?
0698 
0699 Or what happens if a backdoor is discovered in the code and the "Author"
0700 line in the commit says it was done by you, while you're pretty sure you
0701 had `nothing to do with it`_?
0702 
0703 To address both of these issues, Git introduced PGP integration. Signed
0704 tags prove the repository integrity by assuring that its contents are
0705 exactly the same as on the workstation of the developer who created the
0706 tag, while signed commits make it nearly impossible for someone to
0707 impersonate you without having access to your PGP keys.
0708 
0709 .. _`nothing to do with it`: https://github.com/jayphelps/git-blame-someone-else
0710 
0711 Configure git to use your PGP key
0712 ---------------------------------
0713 
0714 If you only have one secret key in your keyring, then you don't really
0715 need to do anything extra, as it becomes your default key.  However, if
0716 you happen to have multiple secret keys, you can tell git which key
0717 should be used (``[fpr]`` is the fingerprint of your key)::
0718 
0719     $ git config --global user.signingKey [fpr]
0720 
0721 **IMPORTANT**: If you have a distinct ``gpg2`` command, then you should
0722 tell git to always use it instead of the legacy ``gpg`` from version 1::
0723 
0724     $ git config --global gpg.program gpg2
0725     $ git config --global gpgv.program gpgv2
0726 
0727 How to work with signed tags
0728 ----------------------------
0729 
0730 To create a signed tag, simply pass the ``-s`` switch to the tag
0731 command::
0732 
0733     $ git tag -s [tagname]
0734 
0735 Our recommendation is to always sign git tags, as this allows other
0736 developers to ensure that the git repository they are pulling from has
0737 not been maliciously altered.
0738 
0739 How to verify signed tags
0740 ~~~~~~~~~~~~~~~~~~~~~~~~~
0741 
0742 To verify a signed tag, simply use the ``verify-tag`` command::
0743 
0744     $ git verify-tag [tagname]
0745 
0746 If you are pulling a tag from another fork of the project repository,
0747 git should automatically verify the signature at the tip you're pulling
0748 and show you the results during the merge operation::
0749 
0750     $ git pull [url] tags/sometag
0751 
0752 The merge message will contain something like this::
0753 
0754     Merge tag 'sometag' of [url]
0755 
0756     [Tag message]
0757 
0758     # gpg: Signature made [...]
0759     # gpg: Good signature from [...]
0760 
0761 If you are verifying someone else's git tag, then you will need to
0762 import their PGP key. Please refer to the
0763 ":ref:`verify_identities`" section below.
0764 
0765 .. note::
0766 
0767     If you get "``gpg: Can't check signature: unknown pubkey
0768     algorithm``" error, you need to tell git to use gpgv2 for
0769     verification, so it properly processes signatures made by ECC keys.
0770     See instructions at the start of this section.
0771 
0772 Configure git to always sign annotated tags
0773 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0774 
0775 Chances are, if you're creating an annotated tag, you'll want to sign
0776 it. To force git to always sign annotated tags, you can set a global
0777 configuration option::
0778 
0779     $ git config --global tag.forceSignAnnotated true
0780 
0781 How to work with signed commits
0782 -------------------------------
0783 
0784 It is easy to create signed commits, but it is much more difficult to
0785 use them in Linux kernel development, since it relies on patches sent to
0786 the mailing list, and this workflow does not preserve PGP commit
0787 signatures. Furthermore, when rebasing your repository to match
0788 upstream, even your own PGP commit signatures will end up discarded. For
0789 this reason, most kernel developers don't bother signing their commits
0790 and will ignore signed commits in any external repositories that they
0791 rely upon in their work.
0792 
0793 However, if you have your working git tree publicly available at some
0794 git hosting service (kernel.org, infradead.org, ozlabs.org, or others),
0795 then the recommendation is that you sign all your git commits even if
0796 upstream developers do not directly benefit from this practice.
0797 
0798 We recommend this for the following reasons:
0799 
0800 1. Should there ever be a need to perform code forensics or track code
0801    provenance, even externally maintained trees carrying PGP commit
0802    signatures will be valuable for such purposes.
0803 2. If you ever need to re-clone your local repository (for example,
0804    after a disk failure), this lets you easily verify the repository
0805    integrity before resuming your work.
0806 3. If someone needs to cherry-pick your commits, this allows them to
0807    quickly verify their integrity before applying them.
0808 
0809 Creating signed commits
0810 ~~~~~~~~~~~~~~~~~~~~~~~
0811 
0812 To create a signed commit, you just need to pass the ``-S`` flag to the
0813 ``git commit`` command (it's capital ``-S`` due to collision with
0814 another flag)::
0815 
0816     $ git commit -S
0817 
0818 Configure git to always sign commits
0819 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0820 
0821 You can tell git to always sign commits::
0822 
0823     git config --global commit.gpgSign true
0824 
0825 .. note::
0826 
0827     Make sure you configure ``gpg-agent`` before you turn this on.
0828 
0829 .. _verify_identities:
0830 
0831 How to verify kernel developer identities
0832 =========================================
0833 
0834 Signing tags and commits is easy, but how does one go about verifying
0835 that the key used to sign something belongs to the actual kernel
0836 developer and not to a malicious imposter?
0837 
0838 Configure auto-key-retrieval using WKD and DANE
0839 -----------------------------------------------
0840 
0841 If you are not already someone with an extensive collection of other
0842 developers' public keys, then you can jumpstart your keyring by relying
0843 on key auto-discovery and auto-retrieval. GnuPG can piggyback on other
0844 delegated trust technologies, namely DNSSEC and TLS, to get you going if
0845 the prospect of starting your own Web of Trust from scratch is too
0846 daunting.
0847 
0848 Add the following to your ``~/.gnupg/gpg.conf``::
0849 
0850     auto-key-locate wkd,dane,local
0851     auto-key-retrieve
0852 
0853 DNS-Based Authentication of Named Entities ("DANE") is a method for
0854 publishing public keys in DNS and securing them using DNSSEC signed
0855 zones. Web Key Directory ("WKD") is the alternative method that uses
0856 https lookups for the same purpose. When using either DANE or WKD for
0857 looking up public keys, GnuPG will validate DNSSEC or TLS certificates,
0858 respectively, before adding auto-retrieved public keys to your local
0859 keyring.
0860 
0861 Kernel.org publishes the WKD for all developers who have kernel.org
0862 accounts. Once you have the above changes in your ``gpg.conf``, you can
0863 auto-retrieve the keys for Linus Torvalds and Greg Kroah-Hartman (if you
0864 don't already have them)::
0865 
0866     $ gpg --locate-keys torvalds@kernel.org gregkh@kernel.org
0867 
0868 If you have a kernel.org account, then you should `add the kernel.org
0869 UID to your key`_ to make WKD more useful to other kernel developers.
0870 
0871 .. _`add the kernel.org UID to your key`: https://korg.wiki.kernel.org/userdoc/mail#adding_a_kernelorg_uid_to_your_pgp_key
0872 
0873 Web of Trust (WOT) vs. Trust on First Use (TOFU)
0874 ------------------------------------------------
0875 
0876 PGP incorporates a trust delegation mechanism known as the "Web of
0877 Trust." At its core, this is an attempt to replace the need for
0878 centralized Certification Authorities of the HTTPS/TLS world. Instead of
0879 various software makers dictating who should be your trusted certifying
0880 entity, PGP leaves this responsibility to each user.
0881 
0882 Unfortunately, very few people understand how the Web of Trust works.
0883 While it remains an important aspect of the OpenPGP specification,
0884 recent versions of GnuPG (2.2 and above) have implemented an alternative
0885 mechanism called "Trust on First Use" (TOFU). You can think of TOFU as
0886 "the SSH-like approach to trust." With SSH, the first time you connect
0887 to a remote system, its key fingerprint is recorded and remembered. If
0888 the key changes in the future, the SSH client will alert you and refuse
0889 to connect, forcing you to make a decision on whether you choose to
0890 trust the changed key or not. Similarly, the first time you import
0891 someone's PGP key, it is assumed to be valid. If at any point in the
0892 future GnuPG comes across another key with the same identity, both the
0893 previously imported key and the new key will be marked as invalid and
0894 you will need to manually figure out which one to keep.
0895 
0896 We recommend that you use the combined TOFU+PGP trust model (which is
0897 the new default in GnuPG v2). To set it, add (or modify) the
0898 ``trust-model`` setting in ``~/.gnupg/gpg.conf``::
0899 
0900     trust-model tofu+pgp
0901 
0902 How to use keyservers (more) safely
0903 -----------------------------------
0904 
0905 If you get a "No public key" error when trying to validate someone's
0906 tag, then you should attempt to lookup that key using a keyserver. It is
0907 important to keep in mind that there is absolutely no guarantee that the
0908 key you retrieve from PGP keyservers belongs to the actual person --
0909 that much is by design. You are supposed to use the Web of Trust to
0910 establish key validity.
0911 
0912 How to properly maintain the Web of Trust is beyond the scope of this
0913 document, simply because doing it properly requires both effort and
0914 dedication that tends to be beyond the caring threshold of most human
0915 beings. Here are some shortcuts that will help you reduce the risk of
0916 importing a malicious key.
0917 
0918 First, let's say you've tried to run ``git verify-tag`` but it returned
0919 an error saying the key is not found::
0920 
0921     $ git verify-tag sunxi-fixes-for-4.15-2
0922     gpg: Signature made Sun 07 Jan 2018 10:51:55 PM EST
0923     gpg:                using RSA key DA73759BF8619E484E5A3B47389A54219C0F2430
0924     gpg:                issuer "wens@...org"
0925     gpg: Can't check signature: No public key
0926 
0927 Let's query the keyserver for more info about that key fingerprint (the
0928 fingerprint probably belongs to a subkey, so we can't use it directly
0929 without finding out the ID of the master key it is associated with)::
0930 
0931     $ gpg --search DA73759BF8619E484E5A3B47389A54219C0F2430
0932     gpg: data source: hkp://keys.gnupg.net
0933     (1) Chen-Yu Tsai <wens@...org>
0934           4096 bit RSA key C94035C21B4F2AEB, created: 2017-03-14, expires: 2019-03-15
0935     Keys 1-1 of 1 for "DA73759BF8619E484E5A3B47389A54219C0F2430".  Enter number(s), N)ext, or Q)uit > q
0936 
0937 Locate the ID of the master key in the output, in our example
0938 ``C94035C21B4F2AEB``. Now display the key of Linus Torvalds that you
0939 have on your keyring::
0940 
0941     $ gpg --list-key torvalds@kernel.org
0942     pub   rsa2048 2011-09-20 [SC]
0943           ABAF11C65A2970B130ABE3C479BE3E4300411886
0944     uid           [ unknown] Linus Torvalds <torvalds@kernel.org>
0945     sub   rsa2048 2011-09-20 [E]
0946 
0947 Next, find a trust path from Linus Torvalds to the key-id you found via ``gpg
0948 --search`` of the unknown key.  For this, you can use several tools including
0949 https://github.com/mricon/wotmate,
0950 https://git.kernel.org/pub/scm/docs/kernel/pgpkeys.git/tree/graphs, and
0951 https://the.earth.li/~noodles/pathfind.html.
0952 
0953 If you get a few decent trust paths, then it's a pretty good indication
0954 that it is a valid key. You can add it to your keyring from the
0955 keyserver now::
0956 
0957     $ gpg --recv-key C94035C21B4F2AEB
0958 
0959 This process is not perfect, and you are obviously trusting the
0960 administrators of the PGP Pathfinder service to not be malicious (in
0961 fact, this goes against :ref:`devs_not_infra`). However, if you
0962 do not carefully maintain your own web of trust, then it is a marked
0963 improvement over blindly trusting keyservers.