How to sign a key using gpg key (password or password-less) to an rpm in Linux

In my last article I had shared the steps to build an rpm from scratch

How to build a signed rpm from scratch by building a source archive using Red Hat Linux

In this article I will share the steps to sign GPG key to an rpm

Step 1: Generate GPG key

Execute the below command to generate the key. You can select they key type and bit size as per your requirements.

The highlighted options will be prompted for an input

# gpg --gen-key
gpg (GnuPG) 2.0.22; Copyright (C) 2013 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection?
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N)

GnuPG needs to construct a user ID to identify your key.

Real name: GoLinuxHub
Email address: golinuxhub1@gmail.com
Comment: Test
You selected this USER-ID:
    "GoLinuxHub (Test) <golinuxhub1@gmail.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
You need a Passphrase to protect your secret key.

You don't want a passphrase - this is probably a *bad* idea!
I will do it anyway.  You can change your passphrase at any time,
using this program with the option "--edit-key".

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 5CC4FA77 marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/5CC4FA77 2018-05-12
      Key fingerprint = 9CDE 27AC C1DD C0F9 8377  F5A9 A605 65CD 5CC4 FA77
uid                  GoLinuxHub (Test) <golinuxhub1@gmail.com>
sub   2048R/17D27D49 2018-05-12

Here below are the important details for me

GPG Name: GoLinuxHub
GPG Key : 5CC4FA77

2. Export Public Key

Export your public key from your key ring to a text file

# gpg --export -a '5CC4FA77' > /root/RPM-GPG-KEY-deepak

3. Import your public key to your RPM DB

# rpm --import /root/RPM-GPG-KEY-deepak

4. Configure your rpmmacros file

Create (if not already exists) rpmmacros file in your home folder and add below content

# vim /root/.rpmmacros
%_gpg_path /root/.gnupg
%_gpg_name GoLinuxHub

5. Add Sign

Next you need to add signature to your rpm

# rpm --resign /tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm
Enter pass phrase:
Pass phrase is good.
/tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm:

OR you can also execute below command

# rpm --addsign /tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm
Enter pass phrase:
Pass phrase is good.
/tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm:

6. Check Signature

Next validate your signature on the rpm you just assigned

# rpm --checksig /tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm
/tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm: sha1 md5 OK

OR you can use below command to validate the same

# rpm -K /tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm
/tmp/rpmbuild/RPMS/x86_64/deepak-1.0.0-1.x86_64.rpm: sha1 md5 OK

7. Build rpm with signature

If you plan to build any more rpms then you can assign signature while building the same using below command

# rpmbuild -ba --sign /tmp/rpmbuild/SPECS/deepak.spec

Below article explains in detail with

Step by Step guide on how to build a signed rpm from scratch by building a source archive using Red Hat Linux

I hope the article was useful.