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:
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.
Sign the Application. The
app-signerGradle plugin signs the.fofile into a.fo.signedfile, with the private key of the signer.Verify the signature at installation. The
app-verifierlibrary checks the.fo.signedfile, and the Kernel installs the Application only when the check passes.
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 plugincom.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) andSHA256withRSA(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.
Recommended: a Key Store of the Device
The best option is to store the certificate in a key store of the device, such as a Secure Element or a PKCS#11 token. The certificate can then be replaced without a Kernel update, for example when the signing key changes. Each device must be provisioned before the first installation.
The Crypto Foundation Library (see Crypto) gives access to the key store of the device through the EXTERNALKS key store type, which the VEE Port implements (see Crypto).
load() takes a null stream and the password of the key store.
The following code reads the certificate stored under an alias:
KeyStore keyStore = KeyStore.getInstance("EXTERNALKS");
keyStore.load(null, password); // Opens the key store of the device.
Certificate certificate = keyStore.getCertificate("APP_SIGNATURE_CERT");
X509Certificate trustedCertificate = (X509Certificate) certificate;
Alternative: the User Space
When the device has no key store, the Kernel can read the certificate from the user space, for example:
a file of the file system,
a resource of the Kernel, with
DefaultCertificateChainValidator.fromResource()(see Use app-verifier in the Kernel).
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 |
|---|---|---|
|
Path to the key store file, relative to the project. |
Required |
|
Type of the key store, for example |
|
|
Password of the key store. |
Required |
|
Alias of the signing key and its certificate chain. |
Required |
|
Password of the signing key. |
The key store password |
|
When |
|
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.
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 installation follows these steps:
The Application management service of the Kernel receives the
.fo.signedfile, for example through AppConnect.app-verifiervalidates the certificate chain of the.fo.signedfile against the trusted certificate, then reads the Application binary and computes its signature.The Application binary (
.fo) is written to the storage of the device.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. Theappverifier.trustedCAs.pathBON constant gives the path of this resource (/trustedCAs.derby 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=falsein a*.constants.listfile of the Kernel, then build the Kernel again (see AppConnect). In the Application project, setappSigner.signOnBuild=false(or do not apply theapp-signerplugin), and deploy the.fofile.
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.
