Banner image for Wesley T. Honeycutt's website.  His contact emails are listed in the image.  Click to return home

Okular Digital Signatures in Ubuntu

Theory and Practice of Creating Okular Digital Signatures in Ubuntu

Recently, I needed to sign and return some documents, but I realized that there was no simple documentation for how to use the new Okular digital signatures in Ubuntu.  Digital signatures have long been available to those using Adobe Acrobat for PDFs, but the ability to sign PDFs using free and open-source software (FOSS) is recent.  The Okular package in apt-get Ubuntu 22.04 LTS "Jammy Jellyfish" is the oldest release with the digital signatures enabled. I recently switched to this LTS release — I wait longer than most to switch since I prefer KDE, and new releases often have a "growing pains" period for the environment.  When I finally tried to hack together a the Okular digital signatures in Ubuntu, once updated, I found the documentation wanting.  New features like this often have sparse documentation until someone makes a post for the masses (like this one). TL;DR skip to The Code section at the bottom.

A Brief History Lesson

Danger of Identifiers

In the dark ages, we were forced to insert and flatten images of signatures into PDFs using Xournal or GIMP.  This was a dangerous hack, since images of signatures are sensitive information capable of identifying a userI am guilty of this too.  Signatures are not meant to be security feature for most cases.  However, this sentiment by security professionals on "should" does not reflect reality.  For example, every American is assigned a social security number which is only meant to identify the person for social welfare programs and NOT to be used as a general identifier. But the reality is that lazy designers have begun to use these numbers as human identifiers.  As the prominence of computers and automation have proceeded to envelope our lives, the attack surface has grown.  Now an American's social security number may be enough for identity thieves to fraudulently access lines of credit in the holder's name.  Similarly, users should avoid practices which expose human identifiers, such as unprotected signatures.  Since applicants need only "sign on the dotted line" for many things, an identity thief with access to an unsecured signature .png can sign anything they choose.

FOSS Poppler

A proper digital signature reduces the attack surface somewhat.  To my knowledge, only non-FOSS software like Adobe gave users the ability to digitally sign documents.  The Adobe method is quite convenient.  The user only needs to input their information into the GUI and a certificate is signed for the user by one of Adobe's partners.  But as good stewards of the net, we should do our best to avoid non-FOSS software.  The FOSS back-end of most modern FOSS PDF tools, Poppler, has had this on their TODO list for at least 5 years (see original Bugzilla and current Gitlab), and the issue was closed only a year prior to this post.  Okular was one of the first (if not the first) to officially support this new change when the programmer announced it in January.

The Challenge of Okular Digital Signatures in Ubuntu

According to the manual, the new capability requires user to generate certificates with Mozilla's NSS tools.  But currently the documentation is sparse.  As of this writing, Stack Exchange barely has any useful information on the topic, mostly through this answer.  Hacking my way through internet search results, I found a user named Rajeesh with an example of how to incorporate digital signatures into Okular using similar, but he relies on an Indian-based cert authority download that I don't know enough about to trust blindly.  Reminder: always know what the code does before copy-pasting from somebody on the internet.  And that includes me! If we hunt around Rajesh's description for NSS, we can find some decent info on it from the Arch (btw) Wiki.  Sadly, the Arch pacman package nss is not available in apt-get.  If we apt search nss, we return too many packages to be useful.  Instead, let's focus on the command Arch recommends called certutil.  Sure enough, an Ubuntu man page exists for certutil.  The second line of that page tells us what provides certutil.  Now we are in business!

RTFM and Pain

To my dismay, when I RTFM, I see a slew of security jargon.  So, I started hacking my way through the examples on the man page, running into loads of issues since the jargon makes assumptions about the user's knowledge level. Plus, this is an old program.  The NSS back end is old enough to drink, smoke, and die for its country.  I consider myself comfortable with the terminal, but this is odd.  Some of the interactive loops are so poorly documented, you may encounter seemingly infinite loops without pre-existing knowledge of how they work.  Yet this program will probably never change.  NSS is a core part of security of the internet.  Too many things use it now to change it for silly reasons like "user friendliness." Someday ImageMagick will finally break for good and we'll have a long period of scrambling as we try to reassemble civilization from the rubble. Courtesy of XKCD's Randall Munroe

Ubuntu as the Linux Gateway

Based on market share, Ubuntu is the most popular Linux distro, dropping to 32.4% as of the start of 2023.  Ubuntu is often touted as the gateway to Linux distros.  Experienced Linux users sometimes switch to other distros after learning on Ubuntu.  Often these other distros meet some need that Ubuntu could not.  In my opinion, distro-hopping is a luxury of the young and energetic.  Some of us have real work to do, and we just want something FOSS to do it on.  For me, I can usually get something to work well enough on my Kubuntu system.  I spend more time doing productive work this way rather than chasing FOSS issues.  Ironically, issues still come up, and here we are!

The Code

With that lengthy discussion out of the way, let's look at the code to make this work... kinda.  NOTE: I am not an expert in security.  Never blindly trust code you see on the internet.  Someone may come up with a better, more refined method.  However, this seems to work.  This has only been tested on Kubuntu 22.04.1 LTS x86_64. First, we need to get the tools we need from apt.
sudo apt-get update
sudo apt-get install libnss3-tools
Check if you have the correct database already made.  If you have Firefox, Thunderbird, or other NSS native application, you may already have this on your system.  Check with:
if ls $HOME/.pki/nssdb/cert* 1> /dev/null 2>&1; then echo "db exists"; else echo "You need to install db"; fi
If you "You need to install db" in your terminal, do the following. Otherwise, skip this block.
mkdir $HOME/.pki/
mkdir $HOME/.pki/nssdb
certutil -N -d sql:$HOME/.pki/nssdb
This created the folders where the NSS database needs to be stored and initializes the SQL database at that path with the -N argument. Now we get to the meat of the operation.  We need to create a self-signed certificate with the appropriate trust information.  We will then refer to this certificate later in Okular.  Edit and use this one-liner with your information:
certutil -S  -s "CN=John Smith,O=Business or School,OU=Nerd department,L=Townsville,ST=State,C=US,[email protected]" -g 2048 -d sql:$HOME/.pki/nssdb -n my-ca-cert -x -t "Cu,Cu,Cu" -p 405-555-5555 --email [email protected] -m 1234
Let me break down what we just did in this step.  We have told certutil to do the following: We execute this command and are told to button mash for a few seconds to introduce entropy into the system.  And voilà, the certificate is ready for use in Okular.
This is a screenshot of the terminal after the command to create a new certificate is used.

Other Useful certutil Commands

Here are a few things that you may find useful when working with certutil.  Notably, this is how you check your work and fix mistakes.

Enabling Okular Digital Signatures in Ubuntu

Now that our certificate is created, we are ready to tell Okular how to sign things.  At this point, we mostly have to follow the instructions in Okular.  For completeness, this is the method:
  1. Open Okular and click on the dropdown menu option "Settings" > "Configure Backends..."
  2. In the PDF tab, add your path to the NSS databases.  It should look like $HOME/.pki/nssdb.  You will see your available certificates in the database.  At some point during this step, you will be required to restart Okular.
    The correct PDF backend format, with some of my personal info redacted.

  3. In Okular again, go to "Tools" > "Digitally Sign...".  You will be instructed to draw a rectangle for the signature region.  If the rectangle you draw is too small, you may need to redraw it to be larger.  Next, you will select the certificate you want to sign with from a drop-down menu on a popup box.  You will be asked to save a new copy of the signed document.  Here, I have drawn a rectangle to digitally sign a cookbook I wrote recently.
    An Okular Digital Signature in Ubuntu of my cookbook.

    The signature block is an ugly gray rectangle.  Hopefully there will be a way to make it pretty in the future, but this is what we have for now.  If you poke around the "Signatures Panel", you will see the date that this document was signed.  Right clicking on the part that says "Rev. 1 signed by John Smith" or similar, you get more detailed information.  You can read the information in the cert about who signed this.
  Good luck!
URL slug: okular-digital-signatures-in-ubuntu
Originally published: 2023-01-05 04:11:41
Content last regnerated: 2024-09-22 16:59:04