Client Certificate Login — A Step-by-Step Walkthrough¶
Overview and Motivation¶
Authentication
describes what the framework offers for client certificates: the TLS handshake verifies the
certificate, the server hands the verified chain on to the provider chain in an
AuthenticationContext, and an AuthenticationProvider maps it onto the application's user. This
document works the case out end to end for an application generated by the
project archetype,
called MyApp here. Most of the work is not Java code. It is the PKI: a CA, a keystore for every
client, and truststores on both sides, all of them generated by the build.
A client certificate proves that the client holds a private key, and the TLS handshake proves it before the first TRIP message is exchanged. A client without a valid certificate never gets to the server's code at all. What the certificate does not say is which user of the application it belongs to. That last step is the application's job, and it takes one class.
CLIENT / DAEMON (jlink image) SERVER (jlink image)
───────────────────────────── ────────────────────
conf/keystore.p12 ── key + chain [client, MyApp CA]
│ unlocked by the keystore password
│ (prompted in the client, ~-encrypted in the daemon)
▼
TLS handshake (trips) ── presents the certificate ───► conf/truststore.p12 (MyApp CA only)
▲ │ clientAuth=need: no certificate, no connection
conf/truststore.p12 ◄── verifies the server's cert ─── │ TransportClientInfo.peerCertificates()
▼
CertificateCredentialsProvider RemoteDbSessionImpl.verifySessionInfo()
│ authentication=certificate │ AuthenticationContext.getPeerCertificate()
▼ ▼
CertificateCredentials ─────────TRIP────────────────► AuthenticationManager
(empty, or a PIN) │
MyAppCertificateAuthenticationProvider
│ CN of the subject → User, loginAllowed?
▼ Authentication(userId, userClassId)
the ordinary session machinery
One new class and a handful of configuration changes, all of them in modules the archetype already generated:
| Module / file | Change |
|---|---|
myapp-server MyAppCertificateAuthenticationProvider |
New. The server SPI: maps the certificate onto a User. |
myapp-server module-info.java |
requires java.naming (for LdapName). |
jlink/pom.xml |
Properties of the CA and the client certificates. |
jlink/server/pom.xml |
Generates the CA and a server truststore holding it. |
jlink/client/pom.xml, jlink/daemon/pom.xml |
Generate and sign the client's key pair. |
jlink/*/src/main/filtered-resources/backend.properties |
Request, present and select the certificate. |
No model change is needed, nor a change of the user interface or the jlink plugin configuration.
The certificate's common name (CN) is the user's name, and the login view adapts by itself to the
interactive certificate login method. The generated password provider stays where it is.
What You Need Beforehand¶
A certificate authority. Every client certificate is signed by a CA, and the server accepts exactly the certificates of the CAs in its truststore.
This walkthrough lets the build generate a CA of its own, into jlink/server/target/ca, and sign
one certificate for the desktop client and one for the daemon with it. That is all a demo needs,
and it keeps the build self-contained. It is not how a real deployment works:
- The CA's private key is the key to every account. Keep it outside the build, on a machine that doesn't ship anything.
- Issue one certificate per user, not per image. A jlink image with a certificate baked in logs in everybody who runs it as the same user. That's fine for a daemon, but not for a desktop client handed out to many people.
- Hand out the certificates in a PKCS#12 file, which the user (or the installer) puts where
backend.propertiespoints to.
The CN is a safe user name only because the server trusts nothing but its own CA. A CA decides what goes into the subject of the certificates it signs. If the server's truststore held a public CA, anybody could buy a certificate with
CN=ADMIN. Step 4 maps the CN onto the user, so step 1 must make sure the truststore holds the application's CA and nothing else.
Step 1 — The CA and the Server Truststore¶
The archetype already generates the server's key pair in jlink/server/pom.xml and exports its
certificate to server/target/server.cer, which the client and daemon modules import into their
truststores. The CA is generated the same way, next to it.
First the properties, in the <properties> of jlink/pom.xml, so that all three image modules see
them:
<!--
CA signing the client certificates of the client and daemon images.
It is generated by the server module into target/ca, outside the packaged pkcs12 directory,
so its private key never ends up in an image.
A real deployment keeps its CA outside the build and issues the client certificates per user.
-->
<ca.dir>${project.parent.basedir}/server/target/ca</ca.dir>
<ca.keystore>${ca.dir}/ca.p12</ca.keystore>
<ca.file>${ca.dir}/ca.cer</ca.file>
<ca.alias>ca</ca.alias>
<ca.password>gruenkohl</ca.password>
<ca.dname>cn=MyApp CA, ou=MyApp, o=Example, c=US</ca.dname>
<!-- client certificate: client.cert.user and client.cert.password are set by the client and daemon modules -->
<client.cert.dname>cn=${client.cert.user}, ou=MyApp, o=Example, c=US</client.cert.dname>
<client.cert.alias>client</client.cert.alias>
<client.cert.csr>${project.build.directory}/client.csr</client.cert.csr>
<client.cert.file>${project.build.directory}/client.cer</client.cert.file>
ca.dir deliberately lies outside target/pkcs12. That directory is what ends up in the image's
conf/ folder, and the CA's keystore must never ship.
In jlink/server/pom.xml, the generated keytool-maven-plugin has a single execution with the
configuration at plugin level. It now needs three, so move the existing configuration into an
execution of its own (server-keystore, unchanged otherwise), and add two more:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>keytool-maven-plugin</artifactId>
<executions>
<!-- the server's key pair and its self-signed certificate, imported by the clients' truststores -->
<execution>
<id>server-keystore</id>
<goals>
<goal>clean</goal>
<goal>generateKeyPair</goal>
<goal>exportCertificate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<!-- the configuration the archetype generated, unchanged -->
...
</configuration>
</execution>
<!--
The CA signing the client certificates (see the client and daemon modules).
Its keystore lives in target/ca and is NOT packaged into the image.
-->
<execution>
<id>ca-keystore</id>
<goals>
<goal>clean</goal>
<goal>generateKeyPair</goal>
<goal>exportCertificate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${ca.keystore}</keystore>
<file>${ca.file}</file>
<storepass>${ca.password}</storepass>
<keypass>${ca.password}</keypass>
<alias>${ca.alias}</alias>
<dname>${ca.dname}</dname>
<exts>
<!-- keytool-maven-plugin syntax: name[:c]=value -->
<ext>bc:c=ca:true,pathlen:0</ext>
<ext>ku:c=keyCertSign,cRLSign</ext>
</exts>
<keyalg>RSA</keyalg>
<sigalg>SHA256WithRSA</sigalg>
<keysize>2048</keysize>
<validity>3650</validity>
</configuration>
</execution>
<!--
The server's truststore holds only the CA: client certificates
are accepted if, and only if, they were issued by it.
-->
<execution>
<id>server-truststore</id>
<goals>
<goal>clean</goal>
<goal>importCertificate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${cert.dir}/truststore.p12</keystore>
<file>${ca.file}</file>
<storepass>${cert.password}</storepass>
<alias>${ca.alias}</alias>
<noprompt>true</noprompt>
<trustcacerts>true</trustcacerts>
</configuration>
</execution>
</executions>
</plugin>
The basic constraints ca:true,pathlen:0 make it a CA that may sign end-entity certificates, but
no further CAs. The truststore reuses the existing cert.password, so the already encrypted
encryptedCertPasswd serves it as well.
keytool-maven-plugin is not keytool. Version 2.x of the plugin is a reimplementation based on BouncyCastle, not a wrapper around the JDK's
keytool, and its extension syntax differs in places:bc:calone yields a critical extension with an empty value, which meansCA:false. Writebc:c=ca:true,pathlen:0as above.
Step 2 — The Server: Requiring Client Certificates¶
In jlink/server/src/main/filtered-resources/backend.properties, the server already configures its
keystore for the trips service. Add the truststore and the client certificate policy:
^tentackle.ssl.keyStore=@{RUNTIMEDIR?.}/conf/keystore.p12
^tentackle.ssl.keyStorePassword=~${encryptedCertPasswd}
# client certificates: required and must be issued by the MyApp CA in the truststore
^tentackle.ssl.trustStore=@{RUNTIMEDIR?.}/conf/truststore.p12
^tentackle.ssl.trustStorePassword=~${encryptedCertPasswd}
^tentackle.ssl.clientAuth=need
The leading ^ turns a backend property into a system property, and
SslParameters resolves
every setting from the service URI's query first, then from tentackle.ssl.<name>, then from
javax.net.ssl.<name>. The service URI itself stays as generated (trips://host:port/MyAppServer).
To scope the settings to one service instead of the whole JVM, put them into the URI:
?tsfile=...&tspass=...&clientauth=need. See the SSL section of
TRIP.
clientAuth has three values:
| Value | A client without a certificate… |
|---|---|
none (default) |
connects; no certificate reaches the provider chain |
want |
connects; password logins work, certificate logins are vetoed |
need |
fails the TLS handshake and never reaches the server's code |
This walkthrough uses need: every client of the application has a certificate. Note what that
means for the generated password provider. It stays in place and keeps working, but only for
clients that present a valid certificate and log in by password. Use want when certificate and
password users must coexist, for example during a migration.
Certificates are supported by the TCP transports trips and tripcs, and by the quiche QUIC
transports tripj and tripcj (leaf certificate only, see
quiche). The KWIK transports tripq/tripcq
cannot request a client certificate: a KWIK server with clientAuth=need refuses to start rather
than silently admitting everybody (see QUIC).
Step 3 — Issuing the Client Certificates¶
The client and daemon images each get a keystore holding their key pair and the certificate chain
[client, CA]. In this walkthrough the build generates both, so the two modules get nearly the same
keytool executions and differ only in two properties.
jlink/client/pom.xml:
<properties>
<rootBasedir>${project.parent.parent.basedir}</rootBasedir>
<!-- the user the client certificate is issued to -->
<client.cert.user>GONZO</client.cert.user>
<!-- the password of the client keystore: the user is asked for it when signing in, since it is not configured in backend.properties -->
<client.cert.password>gonzo</client.cert.password>
</properties>
jlink/daemon/pom.xml:
<properties>
<rootBasedir>${project.parent.parent.basedir}</rootBasedir>
<!-- the user the client certificate is issued to -->
<client.cert.user>DAEMON</client.cert.user>
<!-- the password of the client keystore: configured encrypted in backend.properties, since the daemon runs unattended -->
<client.cert.password>daemon</client.cert.password>
</properties>
client.cert.user becomes the CN, so it must be the name of an existing user, exactly as stored in
the database (the archetype's user names are uppercase).
Then the keytool executions. Again, move the generated plugin-level configuration (the truststore
holding server.cer) into an execution of its own, and put five executions in front of it. They run
in the order they are declared:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>keytool-maven-plugin</artifactId>
<executions>
<!-- 1. the key pair and a certificate signing request for it -->
<execution>
<id>client-keypair</id>
<goals>
<goal>clean</goal>
<goal>generateKeyPair</goal>
<goal>generateCertificateRequest</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${cert.dir}/keystore.p12</keystore>
<file>${client.cert.csr}</file>
<storepass>${client.cert.password}</storepass>
<keypass>${client.cert.password}</keypass>
<alias>${client.cert.alias}</alias>
<dname>${client.cert.dname}</dname>
<keyalg>RSA</keyalg>
<sigalg>SHA256WithRSA</sigalg>
<keysize>2048</keysize>
</configuration>
</execution>
<!-- 2. the CA signs the request -->
<execution>
<id>client-sign</id>
<goals>
<goal>generateCertificate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${ca.keystore}</keystore>
<storepass>${ca.password}</storepass>
<keypass>${ca.password}</keypass>
<alias>${ca.alias}</alias>
<infile>${client.cert.csr}</infile>
<outfile>${client.cert.file}</outfile>
<exts>
<!-- keytool-maven-plugin does not support eku: without it, the certificate is valid for client authentication -->
<ext>ku:c=digitalSignature</ext>
</exts>
<sigalg>SHA256WithRSA</sigalg>
<validity>3650</validity>
</configuration>
</execution>
<!-- 3. the CA certificate must be known to import the signed certificate as the key's chain -->
<execution>
<id>client-import-ca</id>
<goals>
<goal>importCertificate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${cert.dir}/keystore.p12</keystore>
<file>${ca.file}</file>
<storepass>${client.cert.password}</storepass>
<alias>${ca.alias}</alias>
<noprompt>true</noprompt>
<trustcacerts>true</trustcacerts>
</configuration>
</execution>
<!-- 4. replace the self-signed certificate by the chain [client, CA] -->
<execution>
<id>client-import-reply</id>
<goals>
<goal>importCertificate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${cert.dir}/keystore.p12</keystore>
<file>${client.cert.file}</file>
<storepass>${client.cert.password}</storepass>
<keypass>${client.cert.password}</keypass>
<alias>${client.cert.alias}</alias>
<noprompt>true</noprompt>
</configuration>
</execution>
<!-- 5. the keystore holds the key entry only -->
<execution>
<id>client-remove-ca</id>
<goals>
<goal>deleteAlias</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${cert.dir}/keystore.p12</keystore>
<storepass>${client.cert.password}</storepass>
<alias>${ca.alias}</alias>
</configuration>
</execution>
<!-- the truststore holds the server's certificate, as generated by the archetype -->
<execution>
<id>truststore</id>
<goals>
<goal>clean</goal>
<goal>importCertificate</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<storetype>PKCS12</storetype>
<keystore>${cert.dir}/truststore.p12</keystore>
<file>${cert.file}</file>
<storepass>${cert.password}</storepass>
<alias>${cert.alias}</alias>
<noprompt>true</noprompt>
<trustcacerts>true</trustcacerts>
</configuration>
</execution>
</executions>
</plugin>
Why five steps for one certificate: generateKeyPair creates the key with a self-signed
certificate. The CA signs a request for it, and importing the signed certificate under the key's
alias replaces the self-signed one, but only if the keystore can build the chain up to the CA,
which is why the CA certificate is imported first and deleted afterwards. The client presents the
whole chain in the handshake, and a keystore holding a single key entry needs no keyStoreAlias.
The client and daemon modules read the CA from server/target/ca, so the server module must have
been built first. The reactor order of the jlink parent (server, client, daemon) takes
care of that; a build of the client module alone fails.
The same with the JDK keytool¶
For a CA outside the build, or to issue a certificate by hand, these are the equivalent commands:
# the CA, once
keytool -genkeypair -storetype PKCS12 -keystore ca.p12 -alias ca \
-dname "cn=MyApp CA, ou=MyApp, o=Example, c=US" \
-ext bc:c=ca:true,pathlen:0 -ext ku:c=keyCertSign,cRLSign \
-keyalg RSA -keysize 2048 -validity 3650
keytool -exportcert -keystore ca.p12 -alias ca -file ca.cer
# the server's truststore, once
keytool -importcert -storetype PKCS12 -keystore truststore.p12 -alias ca -file ca.cer -noprompt
# per user
keytool -genkeypair -storetype PKCS12 -keystore gonzo.p12 -alias client \
-dname "cn=GONZO, ou=MyApp, o=Example, c=US" -keyalg RSA -keysize 2048
keytool -certreq -keystore gonzo.p12 -alias client -file gonzo.csr
keytool -gencert -keystore ca.p12 -alias ca -infile gonzo.csr -outfile gonzo.cer \
-ext ku:c=digitalSignature -ext eku=clientAuth -validity 365
keytool -importcert -keystore gonzo.p12 -alias ca -file ca.cer -noprompt
keytool -importcert -keystore gonzo.p12 -alias client -file gonzo.cer
keytool -delete -keystore gonzo.p12 -alias ca
The JDK keytool understands the extended key usage eku=clientAuth, which restricts the
certificate to client authentication. Use it whenever you can. In a real deployment, the user
generates the key pair and sends only the request (gonzo.csr) to whoever runs the CA, so the
private key never leaves the user's machine.
Step 4 — The Server: Mapping the Certificate onto a User¶
When the provider chain runs, the TLS handshake has already verified the certificate's chain against
the truststore. AbstractCertificateAuthenticationProvider
does the generic part: it supports CertificateCredentials, takes the verified certificate from the
AuthenticationContext, and vetoes the login if the connection carries none, instead of
abstaining. A client that asked for a certificate login it cannot prove must not be logged in by
another provider. What is left to the application is to find the user.
myapp-server/src/main/java/com/example/myapp/server/auth/MyAppCertificateAuthenticationProvider.java:
package com.example.myapp.server.auth;
import com.example.myapp.pdo.md.User;
import org.tentackle.common.Service;
import org.tentackle.log.Logger;
import org.tentackle.pdo.DomainContext;
import org.tentackle.pdo.Pdo;
import org.tentackle.session.Session;
import org.tentackle.session.auth.AbstractCertificateAuthenticationProvider;
import org.tentackle.session.auth.Authentication;
import org.tentackle.session.auth.AuthenticationContext;
import org.tentackle.session.auth.AuthenticationException;
import org.tentackle.session.auth.AuthenticationProvider;
import org.tentackle.session.auth.CertificateCredentials;
import org.tentackle.session.auth.DefaultAuthentication;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;
import java.security.cert.X509Certificate;
/**
* Authenticates a user by its TLS client certificate.
* <p>
* The TLS handshake has already verified the certificate chain against the server's truststore,
* which holds nothing but the MyApp CA (see {@code jlink/server/pom.xml}). The common name (CN) of
* the certificate's subject is therefore what that CA vouches for, and it is mapped onto the
* {@link User} of the same name.
* <p>
* If the truststore holds CAs the application does not control, the CN is just a free-text field:
* map the certificate by a subject alternative name or by a fingerprint registered with the user instead.
* <p>
* This provider coexists with {@link MyAppPasswordAuthenticationProvider}: each one handles its own
* kind of credentials.
*/
@Service(AuthenticationProvider.class)
public class MyAppCertificateAuthenticationProvider extends AbstractCertificateAuthenticationProvider {
private static final Logger LOGGER = Logger.get();
/**
* Creates the provider.
*/
public MyAppCertificateAuthenticationProvider() {
// services need a public no-arg constructor
}
@Override
protected Authentication authenticate(CertificateCredentials credentials, X509Certificate certificate,
char[] secret, AuthenticationContext context) {
String subject = certificate.getSubjectX500Principal().getName();
String username = getCommonName(subject);
User user = null;
if (username == null) {
LOGGER.warning("client certificate {0} without a common name", subject);
}
else {
DomainContext domainContext = Pdo.createDomainContext(Session.getSession());
user = Pdo.create(User.class, domainContext).selectByUniqueDomainKey(username);
if (user == null) {
LOGGER.warning("attempt to login with client certificate {0} for unknown user", subject);
}
else if (!user.isLoginAllowed()) {
LOGGER.warning("attempt to login with client certificate {0} for disabled user {1}", subject, username);
user = null;
}
}
if (user == null) {
throw new AuthenticationException("login refused");
}
// we know the user already, so there is nothing left for a UserResolver to do
Authentication authentication = new DefaultAuthentication(user.getName(), credentials);
authentication.setUserId(user.getId());
authentication.setUserClassId(user.getClassId());
return authentication;
}
/**
* Gets the common name from the subject's distinguished name.
*
* @param subject the subject in RFC 2253 format
* @return the common name, null if none
*/
private String getCommonName(String subject) {
try {
for (Rdn rdn : new LdapName(subject).getRdns()) {
if ("CN".equalsIgnoreCase(rdn.getType()) && rdn.getValue() instanceof String cn && !cn.isBlank()) {
return cn;
}
}
return null;
}
catch (InvalidNameException ex) {
throw new AuthenticationException("malformed subject in client certificate", ex);
}
}
}
A few points:
- The subject is parsed, not searched.
LdapNamehandles escaped commas and multi-valued RDNs correctly; asplit(",")or a regular expression doesn't. - The user is looked up by its unique domain key, the user name, and must be allowed to log in. Refusing the login with the same message for an unknown and a disabled user tells an attacker nothing; the log tells the administrator what happened.
- The provider sets the user ids itself, like the generated password provider does, so no
UserResolveris needed. The session is already current when the provider runs, so the persistence layer is available. - The
secretis the PIN, null unless the client is configured to ask for one (see the optional PIN). This provider ignores it.
LdapName lives in java.naming, which the server module doesn't require yet. In
myapp-server/src/main/java/module-info.java:
module com.example.myapp.server {
...
requires transitive org.tentackle.update;
requires java.naming; // LdapName to parse the subject of client certificates
...
}
The META-INF service entry is written at build time by the
tentackle-maven-plugin.
Nothing has to be registered by hand, but the server module must be built with clean after the
class was added (see Pitfalls).
Mapping by something stronger than the CN. The CN is fine as long as the truststore holds the application's own CA only. Otherwise, or if a user's certificate must be revocable without touching the CA, map by a stable attribute the CA vouches for: an e-mail address in the subject alternative names (
getSubjectAlternativeNames(certificate, 1), see Authentication), or the SHA-256 fingerprint of the certificate or its public key, registered with the user. The latter needs an attribute in theUsermodel. Revocation is not checked by the JDK's default trust manager either. With long-lived certificates, keep a list of the ones the provider no longer accepts, or disable the user.
Step 5 — The Desktop Client: Signing In¶
jlink/client/src/main/filtered-resources/backend.properties:
url=${dbService}
updateService=${updateService}
# log in by the client certificate
authentication=certificate
# no keyStorePassword: the user is asked for it when signing in
^tentackle.ssl.keyStore=@{RUNTIMEDIR?.}/conf/keystore.p12
^tentackle.ssl.trustStore=@{RUNTIMEDIR?.}/conf/truststore.p12
^tentackle.ssl.trustStorePassword=~${encryptedCertPasswd}
authentication=certificate selects the built-in
CertificateCredentialsProvider.
It is interactive, so the login view
shows a sign-in button instead of the username and password fields. The user is determined by the
certificate; there is nothing to type in.
The keystore password is deliberately not configured. The client keystore is the user's key,
and a password next to it in a configuration file would make the keystore worth no more than the
file permissions. When the user presses the sign-in button, the provider notices the missing
password, asks for it in a dialog, checks it against the keystore right away and registers it at the
KeyStorePasswordProvider,
where the TLS layer picks it up when the connection is opened. A wrong password is reported at once
and may be entered again, up to three times. The password never leaves the client.
The certificate is presented in the handshake, so CertificateCredentials carry nothing but an
optional PIN. Anything a client sends is a claim; the certificate in the AuthenticationContext has
been proven.
Step 6 — The Daemon: Logging In Unattended¶
The daemon runs without anybody to type a password. The archetype gives it a database user and
password in backend.properties, and that is what the certificate replaces.
jlink/daemon/src/main/filtered-resources/backend.properties:
url=${dbService}
# log in by the client certificate, unattended
authentication=certificate
^tentackle.ssl.keyStore=@{RUNTIMEDIR?.}/conf/keystore.p12
^tentackle.ssl.keyStorePassword=~${encryptedClientCertPasswd}
^tentackle.ssl.trustStore=@{RUNTIMEDIR?.}/conf/truststore.p12
^tentackle.ssl.trustStorePassword=~${encryptedCertPasswd}
user= and password= are gone. ConsoleApplication asks the configured CredentialsProvider for
the credentials, and since the keystore password is configured, the provider has nothing to ask.
The keystore password is ~-encrypted with the application's
Cryptor, like the other secrets of the image.
Add its property descriptor to the tentackle-maven-plugin configuration of jlink/daemon/pom.xml,
appending to the descriptors inherited from the parent:
<plugin>
<groupId>org.tentackle</groupId>
<artifactId>tentackle-maven-plugin</artifactId>
<configuration>
<propertyDescriptors combine.children="append">
<!-- the client keystore password, configured in backend.properties -->
<propertyDescriptor>
<input>${client.cert.password}</input>
<converter>@org.tentackle.common.Cryptor</converter>
<property>encryptedClientCertPasswd</property>
</propertyDescriptor>
</propertyDescriptors>
</configuration>
</plugin>
It belongs to the daemon module, not to the parent: client.cert.password is only defined in the
client and daemon modules, and an unresolved input breaks the server module's build (see
Pitfalls).
Finally, remove the now unused daemon.password property and its encryptedDaemonPasswd
descriptor from jlink/pom.xml.
A keystore password in a file is still a password in a file. The daemon's certificate is only as safe as its image directory. Restrict the file permissions of
conf/, and give the daemon's user no more rights than it needs.
Optional: a PIN as a Second Factor¶
A certificate is something the user has. If the keystore password is weak, or is stored next to the keystore, a stolen laptop is enough to log in. A PIN verified by the server adds something the user knows:
The provider then asks for the PIN after unlocking the keystore and sends it in the
CertificateCredentials. Do not confuse the two secrets:
| Secret | Asked for if | Verified by | Leaves the client? |
|---|---|---|---|
| the keystore password | no keyStorePassword is configured |
the client, unlocking the key | never |
| the PIN | authenticationSecret=true |
the server's provider | yes, in the credentials |
On the server, the PIN arrives as the secret argument of authenticate and is wiped after the
method returns. Store it as a hash in a User attribute, like the password, and verify it in the
provider of step 4:
if (user == null || secret == null || !user.verifyPin(secret)) {
throw new AuthenticationException("login refused");
}
The user editor then needs a field to set it. None of this is needed for the walkthrough above.
Building and Trying It Out¶
Always build with clean. Then start the images as usual (see the
project archetype walkthrough).
The server lists both providers at startup:
authentication providers: [MyAppCertificateAuthenticationProvider, MyAppPasswordAuthenticationProvider]
If the certificate provider is missing, the service entry was lost by an incremental build.
The daemon connects without any interaction. The server logs the connection with the certificate's subject, and the session belongs to the certificate's user:
The desktop client shows a sign-in button. Pressing it asks for the keystore password (gonzo
in the walkthrough), and the session belongs to GONZO.
Worth checking on the way:
- A wrong keystore password is reported in the login view and may be entered again.
- A client without a keystore doesn't get past the handshake: it reports
tlsv13 alert certificate required, and the server logs(certificate_required) Empty client certificate chain. - A certificate for an unknown or disabled user is refused with
login refused, and the server logs why. - The chain of a client certificate verifies for client authentication:
(
openssl pkcs12 -in keystore.p12 -clcerts -nokeys -out client.pemandopenssl x509 -in ca.cer -inform DER -out ca.pemproduce the PEM files.) ca.p12is in none of the images:find jlink -name ca.p12lists onlyjlink/server/target/ca/ca.p12.
Pitfalls¶
- keytool-maven-plugin 2.x supports only the
san,ian,bcandkuextensions.eku=clientAuthis skipped with the warningUnsupported extension type: eku. That is harmless: JSSE accepts a certificate without an extended key usage for client authentication. Certificates issued with the JDK keytool should carry it. bc:cis not a CA. In the plugin's syntax it yieldsCA:false, and the client certificates it signs are rejected. Writebc:c=ca:true,pathlen:0.generateCertificateneeds<keypass>for the CA's key, or it fails with aNullPointerException"password is null".- A property descriptor with an unresolved input breaks the build of every module inheriting
it, with a
NullPointerException"text is null" intentackle-maven-plugin:properties. Declare it in the module that defines the input, withcombine.children="append". - The server truststore must hold the application's CA, and only that. Without a truststore, the JVM's default CAs apply, and any certificate a public CA ever issued passes the handshake. With the server's own certificate in it instead of the CA, no client certificate passes.
- The server's host name must be in its certificate. The clients verify it against the
certificate's subject alternative name, which the archetype sets to
san=dns:${serverHost}. Moving the server to another host means regenerating its certificate.
See Also¶
- Authentication — the client-certificate API:
CertificateCredentials,AuthenticationContext, the two secrets. - TRIP — the SSL parameters and how the verified chain reaches the application.
- QUIC and quiche — client certificates over QUIC.
- Backend Properties —
authentication,authenticationSecretand the^system properties. - Cryptor and EncryptedProperties — the
~-encrypted passwords. - JLink and JPackage — the images and their
conf/directory. - Single Sign-On with Google — the same kind of walkthrough for an OpenID Connect login.