Application Signature

A Kernel can refuse any Application that does not come from a trusted source. For that, the Application binary (.fo) is signed when it is built, and the Kernel verifies the signature before it installs the Application. An unsigned Application, a modified Application, or an Application signed with another key is rejected before any of its code runs.

The process has three steps:

  1. Provision the trusted certificate on the device. The device holds the certificate that verifies the signatures. This step is done once per device, usually before the Applications are built, and again when the signing key changes. It is not done for each Application.

  2. Sign the Application. The app-signer Gradle plugin signs the .fo file into a .fo.signed file, with the private key of the signer.

  3. Verify the signature at installation. The app-verifier library checks the .fo.signed file, and the Kernel installs the Application only when the check passes.

Application signature flow: the Application is built and signed in a trusted environment, the trusted certificate is provisioned on the device, and the Kernel verifies the signature before it installs the Application.

Application Signature Flow

The following modules implement this process. They are available on the Central Repository:

  • com.microej.tool.util:app-signer-gradle-plugin: the Gradle plugin com.microej.gradle.app-signer, which signs the Application.

  • com.microej.tool.util:app-signer: the signing tool that the plugin uses. It can also sign an Application from Java code.

  • com.microej.library.util:app-verifier: the library that the Kernel uses to verify a signed Application.

Note

  • The supported signature algorithms are SHA256withECDSA (EC keys) and SHA256withRSA (RSA keys).

  • The management of the key lifecycle (key rotation, renewal and revocation) is not described in this page. It depends on the security policy of each product.

Provision the Trusted Certificate on the Device

Before the device can install a signed Application, it must hold the certificate that verifies the signatures. This certificate is the trust anchor of the device:

  • the certificate of the Certificate Authority that issued the certificate of the signer,

  • or the self-signed certificate of the signer.

The private key never leaves the signing environment: the device needs only the certificate, which contains the public key.

The certificate can be stored in two ways, described below.

Alternative: the User Space

When the device has no key store, the Kernel can read the certificate from the user space, for example:

Warning

This option gives a lower security level than a key store. The certificate is protected only as well as the storage that holds it: a component that can replace it can make the device trust another signer.

Sign the Application

The com.microej.gradle.app-signer plugin adds the signFeature task to an Application project. This task signs the Application binary (.fo) produced by the buildFeature task (see Build a Feature file).

Apply the plugin next to the MicroEJ Application plugin, in the build.gradle.kts file of the project:

plugins {
    id("com.microej.gradle.application")
    id("com.microej.gradle.app-signer") version "3.0.0"
}

Then set the signing parameters in the gradle.properties file of the project. Put the passwords in the gradle.properties file of your Gradle user home (~/.gradle/gradle.properties), not in the project:

appSigner.keyStorePath=keys/keystore.p12
appSigner.alias=my-signing-key
appSigner.keyStorePassword=changeit

The key store holds the private key of the signer and its certificate chain. The table below gives the main parameters. The app-signer-gradle-plugin README gives the complete list.

Property

Description

Default

appSigner.keyStorePath

Path to the key store file, relative to the project.

Required

appSigner.keyStoreType

Type of the key store, for example PKCS12 or JKS.

PKCS12

appSigner.keyStorePassword

Password of the key store.

Required

appSigner.alias

Alias of the signing key and its certificate chain.

Required

appSigner.keyPassword

Password of the signing key.

The key store password

appSigner.signOnBuild

When true, buildFeature also runs signFeature.

true

By default, every buildFeature also signs the Application. To sign on demand only, set appSigner.signOnBuild=false, then run the task:

$ ./gradlew signFeature

The signed Application binary is written to build/application/feature/application.fo.signed.

The plugin can also sign a .fo file built elsewhere, without the MicroEJ Application plugin. This is useful when a separate signing pipeline holds the private key. See the app-signer-gradle-plugin README for this mode.

Signed Application Format

The .fo.signed file wraps the Application binary with the information that the device needs to verify it:

  • the identifier of the signature algorithm,

  • the certificate chain, which ends with the certificate of the signer,

  • the Application metadata and the Application binary (.fo),

  • the signature, which applies to the metadata and the Application binary.

The app-signer tool takes the key store and the Application binary, and produces the signed Application: the signature algorithm, the certificate chain, the metadata and the binary code, and the signature.

Signed Application Format

Verify the Signature at Installation

The Kernel verifies the signature before it installs the Application. The Application is installed, linked and started only when the verification passes.

Secured installation on the device: the signed Application is deployed, app-verifier checks its signature with the Security library and the trusted certificate, the Application binary is stored, then the Kernel installs and links it.

Secured Installation on the Device

The installation follows these steps:

  1. The Application management service of the Kernel receives the .fo.signed file, for example through AppConnect.

  2. app-verifier validates the certificate chain of the .fo.signed file against the trusted certificate, then reads the Application binary and computes its signature.

  3. The Application binary (.fo) is written to the storage of the device.

  4. Kernel.install(InputStream) loads the Application, then the Kernel links and starts it.

When the verification fails, the installation is aborted and no Application code runs.

Note

This model trusts the storage of the device. The Application is verified once, when the Kernel receives it, then stored as a .fo file. When the Kernel installs it again from the storage, for example at startup, the signature is not verified again. If another component can modify the stored files, adapt the Kernel to your needs. For example, store the .fo.signed file, and verify it with app-verifier each time the Kernel installs the Application (see Use app-verifier in the Kernel).

Use app-verifier in the Kernel

Add the app-verifier library to the Kernel project:

implementation("com.microej.library.util:app-verifier:2.0.0")

SignedApplicationVerifier checks a signed Application. It takes a CertificateChainValidator, which decides which certificate chains are trusted. DefaultCertificateChainValidator trusts the chains signed by a set of trusted certificates:

  • give the trusted certificates to its constructor, for example after you read them from a key store of the device,

  • or call DefaultCertificateChainValidator.fromResource() to read them from a resource of the Kernel. The appverifier.trustedCAs.path BON constant gives the path of this resource (/trustedCAs.der by default).

verify() validates the certificate chain, then returns the Application binary as a stream:

SignedApplicationVerifier verifier = new SignedApplicationVerifier(certificateChainValidator);
try {
    SignedApplicationBinaryInputStream applicationBinary = verifier.verify(signedApplicationStream);
    // Read the Application binary to the end, for example to store it.
    // ...
    applicationBinary.isVerified(); // Throws InvalidSignatureException if the signature is not valid.
    // The signature is valid: install the stored Application binary.
} catch (InvalidSignatureException e) {
    // The signature is not valid: reject the Application, and delete what was stored.
}

Warning

app-verifier checks the signature only when the stream is read to the end. The Application binary is therefore read, and possibly written to the storage, before the signature is confirmed. Call isVerified() after the stream is read to the end, and call Kernel.install(InputStream) only when it returns. Delete the stored binary when it throws InvalidSignatureException.

The app-verifier README describes the signed Application format and the error codes of the library.

Use AppConnect

The appconnect-kf-signed module of AppConnect verifies the signature of each Application that AppConnect installs. It uses app-verifier: the Kernel registers its CertificateChainValidator as a service, and AppConnect calls it before each installation. See the AppConnect Signature Verification section of AppConnect for its usage and configuration.

Signature During Development

During development, two options avoid the management of the production key:

  • Sign with a development key. Generate a key and a self-signed certificate for development, sign the Applications with this key (appSigner.* properties), and provision the development certificate on the development devices. The Kernel verifies the signatures as in production.

  • Turn the verification off. In the Kernel, with AppConnect, set appconnect.signature.verification.enabled=false in a *.constants.list file of the Kernel, then build the Kernel again (see AppConnect). In the Application project, set appSigner.signOnBuild=false (or do not apply the app-signer plugin), and deploy the .fo file.

Warning

Never provision the development certificate on a production device, and never ship a Kernel with the verification turned off. The development key is not kept secret: anyone who has it can sign an Application that such a device accepts.