Categories
Encryption Mail

S/MIME certificates in MailCraft on Linux: ECC directly, RSA through a PKCS#11 token

Why MailCraft on Linux refuses a .p12 with an RSA key, how to check what your certificate file contains with OpenSSL, and how to use it anyway: an ECC certificate directly, an RSA one through a SoftHSM2 software token.

MailCraft, an open-source mail, calendar and contacts program written in Rust (source and releases on GitHub), signs and decrypts S/MIME mail, but on Linux it handles private keys differently from most mail programs: an RSA private key is never used inside MailCraft’s own process. RSA keys stay where a system store or a token keeps them (the Windows certificate store, the macOS keychain, a PKCS#11 smartcard or token, gpg-agent for OpenPGP). So a .p12 file with an RSA key, which is what most certificate authorities hand out, is refused on Linux with “This is an RSA certificate, which MailCraft doesn’t use by itself on this system”.

This post shows how to find out what your .p12 contains and how to get it into MailCraft anyway: directly for an ECC certificate, or through a software token (SoftHSM2) for an RSA one. File names, names and values in the output are examples.

1. What’s in the .p12?

$ openssl pkcs12 -in harica-smime.p12 -info -noout
Enter Import Password:
MAC: sha1, Iteration 2000
MAC length: 20, salt length: 20
PKCS7 Encrypted data: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2000
Certificate bag
Certificate bag
Certificate bag
PKCS7 Data
Shrouded Keybag: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2000

Three certificates (yours plus the issuing chain) and one encrypted private key. The old 3DES encryption is fine: both OpenSSL 3 and MailCraft read it (only RC2/40-bit files need -legacy in OpenSSL, and MailCraft refuses those).

The deciding question is the key type:

$ openssl pkcs12 -in harica-smime.p12 -clcerts -nokeys | openssl x509 -noout -subject -enddate -text | grep -E "subject=|notAfter=|Public Key Algorithm|Public-Key:"
Enter Import Password:
subject=CN = Alice Example, emailAddress = alice@example.org
notAfter=Nov 10 12:00:00 2028 GMT
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
  • id-ecPublicKey with 256 or 384 bit: an ECC certificate (P-256/P-384), see step 2.
  • rsaEncryption: an RSA certificate, see step 3.

2. ECC certificate: import it directly

In MailCraft, open File › Options › Security and import the .p12 with its passphrase. MailCraft keeps the file, still protected by the passphrase, in the system’s credential store (Secret Service / KWallet). The private key must be encrypted in the file; an unprotected export is refused.

At the time of writing, keys kept this way are listed but don’t sign or decrypt yet. For signing and decrypting today, use a token as in step 3; that works for ECC keys too.

3. RSA certificate: put it on a software token

A smartcard or USB token works best. Without one, SoftHSM2 provides a PKCS#11 token in software: the key lives in a file protected by the token’s PIN, and MailCraft talks to it like to a smartcard.

Install and configure SoftHSM2

On Debian/Ubuntu the system-wide token directory belongs to the group softhsm, so as a normal user softhsm2-util fails:

$ softhsm2-util --init-token --free --label mailcraft
ERROR: Could not load the SoftHSM configuration.
ERROR: Please verify that the SoftHSM configuration is correct.

A configuration in your home directory avoids that (replace /home/alice with your home directory):

# ~/.config/softhsm2/softhsm2.conf
directories.tokendir = /home/alice/.config/softhsm2/tokens
objectstore.backend = file
log.level = ERROR
slots.removable = false
$ sudo apt install softhsm2 opensc
$ mkdir -p ~/.config/softhsm2/tokens
$ export SOFTHSM2_CONF=~/.config/softhsm2/softhsm2.conf
$ softhsm2-util --init-token --free --label mailcraft
=== SO PIN (4-255 characters) ===
Please enter SO PIN: ********
Please reenter SO PIN: ********
=== User PIN (4-255 characters) ===
Please enter user PIN: ******
Please reenter user PIN: ******
The token has been initialized and is reassigned to slot 1608221475

Split the .p12

The key is written unencrypted for a moment, so work in a private temporary directory and delete the files afterwards:

$ umask 077; cd "$(mktemp -d)"
$ openssl pkcs12 -in ~/harica-smime.p12 -nocerts -nodes | openssl pkey -outform DER -out key.der
Enter Import Password:
$ openssl pkcs12 -in ~/harica-smime.p12 -clcerts -nokeys | openssl x509 -outform DER -out cert.der
Enter Import Password:
$ openssl pkcs12 -in ~/harica-smime.p12 -cacerts -nokeys -out chain.pem
Enter Import Password:
$ awk '/BEGIN CERTIFICATE/{n++} n{print > ("ca" n ".pem")}' chain.pem
$ for f in ca*.pem; do openssl x509 -in "$f" -noout -subject; done
subject=C = GR, O = Hellenic Academic and Research Institutions CA, CN = HARICA S/MIME RSA
subject=C = GR, O = Hellenic Academic and Research Institutions CA, CN = HARICA Client RSA Root CA 2021

Write key and certificates to the token

Key and certificate get the same ID, so MailCraft pairs them. The chain goes on the token too: on Linux, MailCraft sends only the intermediate certificates the token holds along with a signature, and recipients may need them to verify it.

$ M=/usr/lib/softhsm/libsofthsm2.so
$ read -rsp "User PIN: " PIN; echo
User PIN:
$ pkcs11-tool --module $M --token-label mailcraft --login --pin "$PIN" \
    --write-object key.der --type privkey --id 01 --label "S/MIME"
Created private key:
Private Key Object; RSA
  label:      S/MIME
  ID:         01
  Usage:      decrypt, sign, signRecover, unwrap
  Access:     sensitive
$ pkcs11-tool --module $M --token-label mailcraft --login --pin "$PIN" \
    --write-object cert.der --type cert --id 01 --label "S/MIME"
Created certificate:
Certificate Object; type = X.509 cert
  label:      S/MIME
  subject:    DN: CN=Alice Example/emailAddress=alice@example.org
  ID:         01
$ i=2; for f in ca*.pem; do
    openssl x509 -in "$f" -outform DER -out "$f.der"
    pkcs11-tool --module $M --token-label mailcraft --login --pin "$PIN" \
      --write-object "$f.der" --type cert --id 0$i --label "CA $i" >/dev/null
    i=$((i+1))
  done
$ shred -u key.der; rm -f cert.der chain.pem ca*; unset PIN

Check the result:

$ pkcs11-tool --module $M --token-label mailcraft --login --list-objects
Logging in to "mailcraft".
Please enter User PIN:
Private Key Object; RSA
  label:      S/MIME
  ID:         01
  Usage:      decrypt, sign, signRecover, unwrap
Certificate Object; type = X.509 cert
  label:      S/MIME
  subject:    DN: CN=Alice Example/emailAddress=alice@example.org
  ID:         01
Certificate Object; type = X.509 cert
  label:      CA 2
  ID:         02
Certificate Object; type = X.509 cert
  label:      CA 3
  ID:         03

Start MailCraft with the token

MailCraft looks for PKCS#11 tokens through p11-kit-proxy.so, or through the module named in MAILCRAFT_PKCS11_MODULE. Naming SoftHSM directly is the most reliable:

$ SOFTHSM2_CONF=~/.config/softhsm2/softhsm2.conf \
  MAILCRAFT_PKCS11_MODULE=/usr/lib/softhsm/libsofthsm2.so mailcraft

To make it permanent, put both as export lines into ~/.profile. The certificate then shows up under File › Options › Security among your own certificates. In the account’s Digital Signatures settings, choose it and switch on Always, or use Options › Sign per message. MailCraft asks for the token PIN when it signs or decrypts. A message that can’t be signed (no key, no PIN given) stays in the Outbox and is never sent unsigned.

Why not just import the RSA key?

It’s a deliberate project rule: a mail program reads a lot of hostile input, and keeping RSA private-key operations out of its process (in a token, a system store or gpg-agent) limits what a bug could leak. On Windows and macOS, the .p12 goes into the system’s own store, and RSA is fine there. On Linux there is no such store, hence the token. The PKCS#11 path is still young and hasn’t met many real tokens yet, so feedback is welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *