Mastodon

One Old JavaCard, Two Lives: Kerberos PKINIT and SSH From a Drawer Find



A blank white smart card in a small white ACS USB reader with a green LED, lying next to a ThinkPad keyboard on a desk with a FreeBSD mousepad

Table of Contents

There was a smart card in my drawer that I had not touched in years. An NXP J3A081, a JavaCard running JCOP 2.4.1, white plastic with a gold contact pad and nothing printed on it. It came as a free extra with a rather large order of SmartCard-HSMs, and I had never found a use for it. I was sorting out old hardware, and not for the first time I was about to throw it out.

Then I wondered what it would take to make it useful. My laptop logs in to my Identity Management realm with a commercial PKCS#15 card, and my SSH keys live on a Nitrokey. Both of those are things a JavaCard can be, in principle. So the question for the evening was: can a blank card of that age become an enterprise login token, with nothing but open source tools?

It can. At 20:30 it fetched a Kerberos ticket for a test user. At 22:08 the same card, wiped and reloaded, logged in to a FreeBSD server over SSH as an OpenPGP card. This article walks through both lives, including the six places where it did not work on the first try.

All hostnames, realm names and paths in this post are examples. The card, the tools and the error codes are real.

A Card Is a Small Computer

A JavaCard is not a “Kerberos card” or an “OpenPGP card”. It is a tiny computer with a few dozen kilobytes of persistent memory, a crypto coprocessor and a stripped-down Java virtual machine. What it does depends entirely on the applet that is installed on it.

Three terms are enough to follow the rest:

  • CAP file. The compiled applet, the JavaCard equivalent of a JAR. It contains a package and one or more applet classes.
  • AID. The application identifier, a byte string of 5 to 16 bytes. Packages, applets and installed instances each have one. A host program selects an applet by its AID before it talks to it.
  • GlobalPlatform. The standard for managing what is on the card. A card manager on the card accepts authenticated commands to load packages, install instances and delete them. Authentication uses a set of symmetric keys that the card issuer chooses.

The tool for the GlobalPlatform side is GlobalPlatformPro, a single gp.jar. The first command only looks at the card:

java -jar gp.jar --info -r 'Alcor Link AK9563 02 00'

-r picks the reader, in my case the one built into the laptop. With Java 25, --enable-native-access=ALL-UNNAMED in front of -jar silences a warning about native access. In the rest of this post, gp stands for that whole Java command line with the reader option.

My card accepted the default test keys that GlobalPlatformPro tries when you do not pass --key. That is common for development cards and it is the only reason this worked at all. If you find a card with unknown keys, be careful. The card counts failed authentication attempts, and after a small number of them it locks its card manager for good. There is no reset for that.

The software on the laptop, for reference: Fedora 45, GlobalPlatformPro 26.06.04, OpenSC 0.27.1, OpenSSL 4.0.3 with pkcs11-provider 1.3.0, MIT Kerberos 1.22.2 and OpenSSH 10.5.

First Life: A PKCS#15 Token for Kerberos

The applet for the first life is IsoApplet by Philip Wendland, version 0.6.1. It implements an ISO 7816 file system with on-card key generation, and OpenSC has a driver for it. Together they turn the card into a regular PKCS#15 token. The 0.6 line needs Java Card 2.2.2 or newer, which this card just about provides. Pick the right release: the current IsoApplet 1.x on the main branch targets Java Card 3.0.4 and needs extended APDUs. For cards of this generation, the legacy branch main-javacard-v2.2.2 and its last release, 0.6.1 from 2019, are the ones that fit. Its README also warns that ECDSA is hardly usable there, so RSA it is.

Stumbling Block 1: gp --install Says 6A80

The obvious command loads the CAP file and creates an instance in one go:

gp --install IsoApplet.cap

It failed. The load went through, and the card answered the following INSTALL command with status word 6A80, “incorrect data”. Splitting the step with --package, --applet and --create gave the same result.

gp -d prints every APDU, and the trace of the --create attempt shows the problem directly. Shortened, with the secure-channel MAC left out:

A>> 84E60C00 36 0BF276A288BCFBA69D34F310 0CF276A288BCFBA69D34F31001 0CF276A288BCFBA69D34F31001 03000000 02C900 00
A<< 6A80

The INSTALL command carries a privileges field, here 03 000000: a length of three, then three bytes. GlobalPlatform 2.1.1, which is what this card implements, defines that field as exactly one byte. Newer versions of the specification allow three. Current GlobalPlatformPro sends three, and the card wants one. The command I built by hand is identical except for that field, and the card accepted it.

GlobalPlatformPro can send raw APDUs over its authenticated channel with -s. So the package gets loaded without installing, and the INSTALL is typed out manually:

gp --load IsoApplet.cap
gp -s 80E60C002C0BF276A288BCFBA69D34F3100CF276A288BCFBA69D34F310010CF276A288BCFBA69D34F31001010002C90000

That hex string is less scary than it looks:

Bytes Meaning
80 E6 0C 00 Class, instruction INSTALL, P1 0C means “for install and make selectable”
2C Length of the data field, 44 bytes
0B F276A288BCFBA69D34F310 Package AID, 11 bytes
0C F276A288BCFBA69D34F31001 Applet AID inside the package, 12 bytes
0C F276A288BCFBA69D34F31001 AID of the new instance, 12 bytes
01 00 Privileges: one byte, no special privileges
02 C9 00 Install parameters, an empty C9 element
00 Length of the install token, none

Every field is a length byte followed by its content. After that, gp --list showed the package and a selectable instance.

A First Init, and a Reason to Start Over

With the applet in place, OpenSC takes over:

pkcs15-init --create-pkcs15
pkcs15-init --generate-key "rsa/2048" --auth-id "FF" --label "test" --id "1"

That worked immediately. It also produced a token called “JavaCard isoApplet” from the manufacturer “unknown”. If I program my own card, I want my own name on it. Label, manufacturer and serial number are written when the PKCS#15 structure is created, so the card had to go back to empty.

Stumbling Block 2: Deleting the Instance Says 6985

gp --delete F276A288BCFBA69D34F31001

Status word 6985, “conditions of use not satisfied”. gp even suggests that some app might still be active, so I closed every program that had OpenSC’s PKCS#11 module loaded. No change. The gp -d trace shows a plain DELETE with nothing but the instance AID, so this is not another format problem. This card refuses to delete the applet instance on its own, and I did not find out why. What it does accept is deleting the package together with everything that depends on it:

gp --delete F276A288BCFBA69D34F310 -f

Note the shorter AID. That is the package, not the instance, and -f forces the removal of its instances as well. All data in the applet is gone with it, including the test key.

So the reliable recipe for this card has three parts: --load to get the package on, a raw INSTALL with a one-byte privileges field to create the instance, and --delete <package AID> -f to get rid of it. It carried me through the rest of the evening.

Stumbling Block 3: The OpenSC Profile

pkcs15-init takes the token’s label, manufacturer and PIN policy from profile files in /usr/share/opensc/. I did not want to edit files that belong to an RPM, so I gave OpenSC a private copy:

mkdir -p ~/javacard/opensc-profile
cp /usr/share/opensc/pkcs15.profile /usr/share/opensc/isoApplet.profile ~/javacard/opensc-profile/

Both files are needed. isoApplet.profile includes pkcs15.profile, and OpenSC looks for it in the same directory. A small configuration file points OpenSC at the copy:

~/javacard/opensc-custom.conf

app default {
    profile_dir = /home/alice/javacard/opensc-profile;
}

In the copied isoApplet.profile, I changed two lines:

-   label          ="JavaCard isoApplet";
-   manufacturer   = "unknown";
+   label          ="Personal IdM SmartCard";
+   manufacturer   = "Larvitz PKI Enterprises";

Then the initialization again, this time with the private profile and a serial number of my choice:

env OPENSC_CONF=$HOME/javacard/opensc-custom.conf pkcs15-init --create-pkcs15 --serial 232323424242
pkcs15-init --generate-key "rsa/2048" --auth-id "FF" --label "l0ls3c" --id "1"

The env in front is there because my shell is fish, which does not accept VAR=value command. In bash you can drop it.

--create-pkcs15 asks for a user PIN and a PUK. IsoApplet insists on a PUK of at least 16 characters and answers anything shorter with PIN too short (min 16 characters). The PUK cannot be skipped either.

One detail differs from other PKCS#15 cards I have used. On IsoApplet, the user PIN object has the ID ff, not 01. Every --auth-id has to say so, otherwise pkcs15-init looks for a PIN that does not exist.

The result:

token label        : Personal IdM SmartCard
token manufacturer : Larvitz PKI Enterprises
token model        : PKCS#15
serial num         : 232323424242

PIN [User PIN]            ID: ff, Reference 1, ascii-numeric
Private RSA Key [l0ls3c]  ID: 01, 2048 bit, Usage: sign,
                          Access Flags: sensitive, alwaysSensitive, neverExtract, local

The last line is the point of the whole exercise. local means the key was generated on the card. neverExtract means it cannot leave it. No copy of this private key ever existed on the laptop.

The IdM Side

The card needs someone to be. With an admin ticket on one of the IdM servers, I created a test user:

ipa user-add john --first=John --last=Testuser --cn="John Testuser"
ipa user-add-certmapdata john \
    --issuer "CN=Certificate Authority,O=IPA.EXAMPLE.COM" \
    --subject "CN=john,O=IPA.EXAMPLE.COM"

john has no password and no Kerberos keys. The card is his only way in.

The second command is the one that is easy to forget. My certificate mapping rules match on issuer and subject, and without mapping data on the user, the KDC rejects a perfectly valid certificate. I learned that a few days earlier while provisioning my regular card, so this time it went in first.

Next, a certificate signing request. The private key is on the card, so OpenSSL has to sign the request through PKCS#11. On Fedora 45 with OpenSSL 4 that is the job of the pkcs11 provider:

env PKCS11_PROVIDER_MODULE=/usr/lib64/pkcs11/opensc-pkcs11.so \
    openssl req -new -provider pkcs11 -provider default \
    -key "pkcs11:token=Personal%20IdM%20SmartCard;id=%01;type=private" \
    -subj "/CN=john" -out john.csr

OpenSSL asks for the card PIN and writes the request. The IdM CA turns it into a certificate:

ipa cert-request john.csr --principal john --certificate-out john.pem

Before writing anything to the card, I compared the public key of the CSR, of the issued certificate and of key 01 on the card. All three were identical. That was not paranoia. A few minutes earlier I had been unsure which directory the CSR had ended up in, and a certificate for the wrong key is a quiet kind of failure.

Then the certificate goes next to its key:

env OPENSC_CONF=$HOME/javacard/opensc-custom.conf \
    pkcs15-init --store-certificate john.pem --auth-id ff --id 01 --format pem

The Ticket

A separate credential cache keeps my own tickets out of the experiment:

~ ❯ env KRB5CCNAME=FILE:/tmp/krb5cc_john_test kinit -X X509_user_identity="PKCS11:token=Personal IdM SmartCard" john@IPA.EXAMPLE.COM
Personal IdM SmartCard           PIN:

~ took 4s ❯ klist -c FILE:/tmp/krb5cc_john_test
Default principal: john@IPA.EXAMPLE.COM

Valid starting       Expires              Service principal
17.11.2026 20:30:41  18.11.2026 20:27:37  krbtgt/IPA.EXAMPLE.COM@IPA.EXAMPLE.COM
        renew until 24.11.2026 20:30:36

First attempt, about four seconds including typing the PIN. A ticket-granting ticket for a user without a password, authenticated by a card that had been on its way to the bin that morning.

A few observations:

  • Signing is enough. The key on the card has the usage sign and nothing else. PKINIT uses Diffie-Hellman for the key exchange here, and the card only has to sign the request.
  • Nothing in the chain cares who made the card. The KDC sees a certificate from its own CA and a valid signature. Anything OpenSC can present as a PKCS#15 token with an RSA key works, as long as the certificate and the mapping data are right.
  • The ticket came from home. The KDC that answered was the replica I installed at home a few days ago. It has no CA. The certificate was issued by a server in the data center, and the home replica validated it with the CA certificate it already had. That split works exactly as designed.

Second Life: An OpenPGP Card for SSH

With a ticket in the cache, the first question was answered. The second one followed naturally. My daily SSH key lives on a Nitrokey and is served by the Rust OpenPGP card stack. Would that stack accept this card as well?

Swapping the Applet

The same three-part recipe as before, with a different CAP file. The shell history has a comment at this point, and I am leaving it in:

# Sorry John :)
gp --delete F276A288BCFBA69D34F310 -f
gp --load gpg.cap
gp -s 80E60C003A10D276000124000200000000000000000010D276000124010200000000000000000010D276000124010200FFFF232342420000010003C9010000

The applet is OpenPGP-Card by FluffyKaon, an open source implementation of version 2.0 of the OpenPGP card specification for JavaCard. The repository has not changed since 2015, and it ships an install script written for JCOP cards in GlobalPlatform 2.1.1 mode, so it is a good match for a card of this generation. The INSTALL command has the same structure as the first one:

Bytes Meaning
80 E6 0C 00 3A INSTALL for install and make selectable, 58 data bytes
10 D2760001240002000000000000000000 Package AID, 16 bytes
10 D2760001240102000000000000000000 Applet AID, 16 bytes
10 D276000124010200FFFF232342420000 Instance AID, 16 bytes
01 00 Privileges, one byte
03 C9 01 00 Install parameters
00 Length of the install token, none

The AID Is the Identity

The instance AID in that command is worth a closer look, because on an OpenPGP card it is not just an address:

D2 76 00 01 24 01   02 00   FF FF   23 23 42 42   00 00
└─ OpenPGP app ─┘   └ver┘   └mfr┘   └─ serial ─┘

The first six bytes say “this is an OpenPGP application”. Then follow the specification version, a manufacturer ID and a serial number. The card ident that tools like oct show is built from those last two fields.

Which means the manufacturer and the serial number of an OpenPGP card are whatever the person typing the INSTALL command wants them to be. I picked FFFF, which is the range reserved for test cards, and a serial number I can remember:

~ ❯ oct list
Available OpenPGP cards:
FFFF:23234242

oct info reads the rest of the application data without complaint: version 2.0, manufacturer “Testcard”, key import, command chaining, at most 255 bytes per command.

For a card you buy, the vendor does this step and you never see it. Doing it by hand takes some of the magic out of those idents.

Stumbling Block 4: GnuPG Says the Card Is Not There

The Rust tools were happy. GnuPG was not:

~ ❯ gpg --card-status
gpg: OpenPGP card not available: No such file or directory

A branded OpenPGP card in the same reader worked, so the reader and my scdaemon.conf were not the problem. Restarting scdaemon changed nothing, and asking it for the serial number directly worked fine. The failure came later. A debug log of scdaemon during LEARN, which is what --card-status triggers, ends like this:

send apdu: c=00 i=47 p1=81 p2=00 lc=2 le=256 em=0
 response: sw=6A82  datalen=0
reading public key failed: No such file or directory

While learning a card, scdaemon reads the public key of every key slot. On a card without keys, this applet answers 6A82, “file not found”, and GnuPG takes that as a fatal error for the whole card. oct treats an empty slot as an empty slot. The fix is simply to put keys on the card first.

Keys

oct generates all three keys on the card:

oct admin -c FFFF:23234242 generate -o test.pub -u "Test <test@example.org>" rsa2048
~ ❯ oct status --card FFFF:23234242
Signature key:       a968bee8297bbca56504d4360be76a1d645622e1   RSA 2048   created 2026-11-17 20:54:00 UTC
Decryption key:      558b9967f8391e73759dff5728e4004ef71fdd5c   RSA 2048   created 2026-11-17 20:54:17 UTC
Authentication key:  49538308792d7ec8ae84ca1fe81f681dee915448   RSA 2048   created 2026-11-17 20:54:34 UTC
Signatures made: 3
Remaining PIN attempts: User: 3, Admin: 3, Reset Code: 0

Roughly 17 seconds per RSA 2048 key. That is the speed of prime number search on a smart card CPU from that era, and a good reminder of how little hardware is doing this work. oct ssh prints the SSH public key of the authentication slot.

With keys in all three slots, GnuPG reads the card as well:

~ ❯ gpg --card-status
Reader ...........: Alcor Link AK9563 02 00
Application ID ...: D276000124010200FFFF232342420000
Application type .: OpenPGP
Version ..........: 2.0
Manufacturer .....: test card
Serial number ....: 23234242
Key attributes ...: rsa2048 rsa2048 rsa2048
Signature key ....: A968 BEE8 297B BCA5 6504  D436 0BE7 6A1D 6456 22E1
Encryption key....: 558B 9967 F839 1E73 759D  FF57 28E4 004E F71F DD5C
Authentication key: 4953 8308 792D 7EC8 AE84  CA1F E81F 681D EE91 5448

Stumbling Block 5: The Agent Has No Identities

~ ❯ echo $SSH_AUTH_SOCK
/run/user/1000/openpgp-card/ssh-agent.sock

~ ❯ ssh-add -L
The agent has no identities.

The card was fine. oct list, oct status and oct ssh all worked. Only the SSH agent could not see it.

This one I did to myself. In January I wrote about hiding a smart card reader from the agent with LD_PRELOAD. The agent’s systemd unit still has that drop-in:

[Service]
Environment="LD_PRELOAD=%h/.local/lib/libhackyreaderfix.so"

The library hides the laptop’s built-in Alcor reader from the agent, because my PKCS#15 card normally lives there and the agent used to abort when it met a card without an OpenPGP applet. And the built-in Alcor reader is exactly where the freshly minted OpenPGP JavaCard was sitting. My own workaround from ten months ago was filtering out my own experiment.

The proper fix exists upstream by now. I sent pull requests so that the Rust stack skips cards without an OpenPGP applet instead of failing. They are released in openpgp-card 0.7.3 and openpgp-card-ssh-agent 0.3.9, and the maintainers fixed the same thing in openpgp-card-tools 0.11.16. My laptop still runs 0.3.7 of the agent, so the workaround is still active and can go with the next package rebuild. For that evening, the pragmatic fix was a different reader. I have enough of them. In an external ACS ACR39U:

~ ❯ ssh-add -L
ssh-rsa AAAAB3NzaC1yc2E...q3h FFFF:23234242

Stumbling Block 6: Agent Refused Operation

With the public key in authorized_keys on a FreeBSD test host, the first login attempt:

sign_and_send_pubkey: signing failed for RSA "FFFF:23234242" from agent: agent refused operation

The agent’s journal explains it:

ERROR openpgp_card_ssh_agent] No User PIN for card FFFF:23234242 found via openpgp_card_state

openpgp-card-ssh-agent never prompts for a PIN in the middle of a signature. It looks the PIN up in its state store, and for a card that was born ten minutes ago there is nothing to find. The way to store one is the standard smart card option of ssh-add, with the card ident as the argument:

~ ❯ ssh-add -s FFFF:23234242
Enter passphrase for PKCS#11:
Card added: FFFF:23234242

The prompt says “PKCS#11” because that text is hard-coded in OpenSSH. What it wants is the OpenPGP user PIN. In the interest of honesty: my first attempt was answered with Could not add card, and the cause was a mistyped PIN. The agent verifies the PIN against the card before storing it, which cost one of three tries. A correct PIN resets the counter.

The Login

The relevant lines of ssh -vvv:

debug1: Will attempt key: FFFF:23234242 RSA SHA256:c6Rum6kzcDSeS01b4DTr+SUVAsAaGe8qrEhhmNlqUww agent
debug1: Offering public key: FFFF:23234242 RSA SHA256:c6Rum6kzcDSeS01b4DTr+SUVAsAaGe8qrEhhmNlqUww agent
debug1: Server accepts key: FFFF:23234242 RSA SHA256:c6Rum6kzcDSeS01b4DTr+SUVAsAaGe8qrEhhmNlqUww agent
debug3: sign_and_send_pubkey: signing using rsa-sha2-256 SHA256:c6Rum6kzcDSeS01b4DTr+SUVAsAaGe8qrEhhmNlqUww
Authenticated to bsd01.example.com ([192.0.2.20]:22) using "publickey".
...
FreeBSD 15.1-RELEASE-p3 (GENERIC)

A shell on FreeBSD 15.1, authenticated by a signature that was computed inside a card I had nearly thrown out.

Two things in the same log look like problems and are not:

  • ssh_agent_bind_hostkey: agent refused operation. Newer OpenSSH clients send a session-bind extension to the agent. This agent does not implement it, says so, and SSH carries on.
  • Connection multiplexing hides the card. My SSH config keeps master connections open for hours. A second login reuses the existing connection and never asks the card for anything. For repeated tests, close the master first with ssh -O exit <host>.

Stumbling Blocks at a Glance

# Symptom Cause Fix
1 gp --install fails with 6A80 GlobalPlatformPro sends a three-byte privileges field, the GP 2.1.1 card wants one byte gp --load, then a raw INSTALL with one-byte privileges
2 gp --delete <instance AID> fails with 6985 This card will not remove an instance on its own gp --delete <package AID> -f
3 Token has a generic label Label and manufacturer come from the OpenSC profile Private profile copy via OPENSC_CONF, and --auth-id ff
4 gpg --card-status: card not available scdaemon reads every key slot, the empty applet answers 6A82 Generate keys first
5 ssh-add -L shows no identities My own LD_PRELOAD workaround hides the reader Another reader, or a current agent without the workaround
6 agent refused operation when signing The agent has no stored PIN and never prompts ssh-add -s <card ident>

There is one more that did not happen: a valid certificate that the KDC rejects because the user has no certificate mapping data. ipa user-add-certmapdata prevents it.

What I Did Not Test

The evening proved two things and it is worth being precise about the rest.

  • The two applets were never on the card at the same time. IsoApplet was deleted before the OpenPGP applet was loaded. Whether the J3A081 has room for both is an open question, and an interesting one, because a single card that does PKINIT and SSH would be a nice thing to have.
  • Only kinit. I did not log in to a host as john, neither through SSH with GSSAPI nor through SSSD on a console.
  • Only SSH authentication. No Git signing and no decryption with the OpenPGP keys.
  • Only RSA 2048. No elliptic curve keys on either applet.
  • GnuPG only looked. Once the card had keys, gpg --card-status read it, but every operation that used a key went through the Rust stack with oct and openpgp-card-ssh-agent.
  • No benchmarks. “About four seconds” is a shell prompt timer, not a measurement.

Two caveats belong here as well. The card accepted GlobalPlatform’s well-known default keys, so anyone who gets hold of it can replace its applets. They cannot read the private keys out, but they can wipe them. For a card that is meant to be used seriously, changing those keys is the first step, and it is also the step with the most potential to brick the card. And RSA 2048 on a chip of this age is fine for an experiment. My daily keys stay on much younger hardware.

As for john: his private key was destroyed together with the IsoApplet, so the certificate that is still valid in IdM until 2028 has nothing left to prove possession with. Revoking it and deleting the user is the cleanup that remains.

Conclusion

The same piece of plastic was an enterprise login token at 20:30 and an SSH key at 22:08. Nothing about the hardware changed in between. Only the applet did.

That is the part I found worth writing down. Smart cards tend to be treated as fixed-function devices, a “PIV card” or an “OpenPGP card”, bought in that shape and used in that shape. Underneath, many of them are general-purpose computers with a loader, and the function is a file of a few kilobytes. Everything else in the chain was standard software talking to standard interfaces: OpenSC, the OpenSSL PKCS#11 provider, MIT Kerberos and IdM on one side, the Rust OpenPGP card tools and OpenSSH on the other. None of them asked where the card came from.

The stumbling blocks were all at the edges. A management tool that is many years younger than the card. A profile file in a system directory. A workaround I had written myself and forgotten. A client that treats an empty key slot as a broken card. None of them had anything to do with cryptography, and each was solved in minutes once the error code was taken literally.

The card is not going to the recycling yard. As I write this, it still answers to FFFF:23234242.


References