Skip to content

feat(crypto): import PEM keys and validate symmetric key length - #393

Open
abdullahelmarghany wants to merge 1 commit into
eclipse-score:mainfrom
abdullahelmarghany:feat/wp3-import-validation
Open

abdullahelmarghany wants to merge 1 commit into
eclipse-score:mainfrom
abdullahelmarghany:feat/wp3-import-validation

Conversation

@abdullahelmarghany

@abdullahelmarghany abdullahelmarghany commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Symmetric key import accepted any byte count and ignored the requested format. Asymmetric import accepted DER only, and every caller had to name a format even when the key type fixes it.

  • ImportKeyParams::format is now std::optional<FormatType>, unset by default. Unset means the native format of the key type: raw bytes for AES/HMAC; for asymmetric keys PEM, an uncompressed EC point (EC only) or DER, detected from the data. Detection only picks the parser; curve, modulus and type checks still apply. An explicit format stays strict.
  • On the IPC the unset format is operations::kFormatNative (0xFFFFFFFF). Any other value outside FormatType is rejected instead of being cast.
  • Symmetric import (OpenSSL and PKCS#11) goes through a shared IKeyFactory::ValidateSymmetricImport: format unset or kRaw, else kInvalidFormat; AES exactly 16/24/32 bytes and HMAC at least the digest length, else kIncompatibleKeyType.
  • The file-backed slot handler reads the documented key_format deployment property (raw/der/pem); absent means native format, any other value fails the load.
  • OpenSSL decodes one unencrypted PEM block (PRIVATE KEY, EC PRIVATE KEY, RSA PRIVATE KEY, PUBLIC KEY). Encrypted PEM, unknown labels, data around the block and a label that disagrees with the key are rejected; no passphrase callback is ever invoked. PKCS#11 asymmetric import stays unsupported.
  • The mediator forwards the key-binding error instead of always reporting kInvalidArgument. A slot whose material is refused at load (for example, a 16-byte key labelled AES-256) now reports kIncompatibleKeyType, which KeySlotBindingTest from feat(crypto): refuse keys that do not fit the context algorithm #345 expects.

Tests: OpenSSL key factory unit tests (symmetric rules, PEM, native format per key family, slot loading with each key_format), a request parser test for the format encoding, integration tests for AES-256 and PEM/DER/raw-point public key import, and PEM vectors derived from the existing NIST DER vectors.

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

License Check Results

🚀 The license check job ran with the Bazel command:

bazel run --lockfile_mode=error //:license-check

Status: ⚠️ Needs Review

Click to expand output
[License Check Output]
Extracting Bazel installation...
Starting local Bazel server (8.8.2) and connecting to it...
INFO: Invocation ID: 8cb638e7-9b16-40b8-8c89-5f3773dae5aa
Computing main repo mapping: 
Computing main repo mapping: 
Computing main repo mapping: 
Loading: 
Loading: 1 packages loaded
Loading: 1 packages loaded
    currently loading: 
Loading: 1 packages loaded
    currently loading: 
Loading: 1 packages loaded
    currently loading: 
Loading: 1 packages loaded
    currently loading: 
Loading: 1 packages loaded
    currently loading: 
WARNING: Target pattern parsing failed.
ERROR: Skipping '//:license-check': no such target '//:license-check': target 'license-check' not declared in package '' defined by /home/runner/work/crypto/crypto/BUILD
ERROR: no such target '//:license-check': target 'license-check' not declared in package '' defined by /home/runner/work/crypto/crypto/BUILD
INFO: Elapsed time: 15.415s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview for this pull request is available at:
pr-393: https://eclipse-score.github.io/inc_security_crypto/pr-393/

@abdullahelmarghany
abdullahelmarghany force-pushed the feat/wp3-import-validation branch from 1f9064b to b3e50d2 Compare November 12, 2026 15:10
@abdullahelmarghany
abdullahelmarghany force-pushed the feat/wp3-import-validation branch from b3e50d2 to 9fada7d Compare November 16, 2026 12:45
@abdullahelmarghany
abdullahelmarghany marked this pull request as ready for review November 16, 2026 15:10
@abdullahelmarghany
abdullahelmarghany force-pushed the feat/wp3-import-validation branch from 9fada7d to 8856f60 Compare November 23, 2026 10:30
@abdullahelmarghany
abdullahelmarghany force-pushed the feat/wp3-import-validation branch from 8856f60 to 73a3f38 Compare November 25, 2026 10:00
Symmetric key import accepted any byte count and ignored the requested
format. Asymmetric import accepted DER only, and every caller had to
name a format even when the key type fixes it.

ImportKeyParams::format is now std::optional<FormatType>, unset by
default. Unset means the native format of the key type: raw bytes for
AES/HMAC, and for asymmetric keys PEM (data starting with "-----BEGIN"),
an uncompressed EC point (leading 0x04, EC algorithms only) or DER.
Detection only picks the parser; curve, modulus and type checks still
apply. An explicit format stays strict. On the IPC the unset format is
sent as operations::kFormatNative (0xFFFFFFFF), with EncodeFormat /
DecodeFormat in key_management_operations.hpp; any other value outside
FormatType is rejected instead of being cast.

Symmetric imports go through a shared
IKeyFactory::ValidateSymmetricImport, which both the OpenSSL and the
PKCS#11 factory use. The format must be unset or kRaw, otherwise the
import fails with kInvalidFormat. An AES key must be exactly 16/24/32
bytes and an HMAC key at least as long as its digest, otherwise the
import fails with kIncompatibleKeyType.

The file-backed slot handler maps the deployment "key_format" property
raw/der/pem to that format, maps an absent property to the native
format, and fails the load for any other value.

The OpenSSL factory decodes one unencrypted PEM block labelled
PRIVATE KEY, EC PRIVATE KEY, RSA PRIVATE KEY or PUBLIC KEY. Encrypted
PEM, unknown labels, data around the block and a label that disagrees
with the key are rejected. PEM_FLAG_SECURE keeps the decoded body on
the secure heap, and no passphrase callback is ever invoked. PKCS#11
asymmetric import stays unsupported.

Tests:
- OpenSSL key factory unit tests for the symmetric rules, PEM import,
  native-format import for each key family, and file-backed slot
  loading with absent, raw, der, pem and unknown key_format.
- A request parser test for the format wire encoding.
- Integration tests for native and explicit AES-256 import and for PEM,
  DER and raw-point public key import with raw export.
- PEM test vectors derived from the existing NIST DER vectors.

Signed-off-by: Abdullah Elmarghany <abdullah.elmarghany@bmw.de>
@abdullahelmarghany
abdullahelmarghany force-pushed the feat/wp3-import-validation branch from 73a3f38 to 508288a Compare November 27, 2026 15:04

This branch is waiting to be deployed

1 waiting deployment
workflow-approval — 508288a4 Waiting Nov 27, 2026 by abdullahelmarghany via Build and Test aarch64-qnx / approval #1264
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant