Skip to content

derive_dh_secret hashes PKCS#8 DER — fragile to library upgrades #553

@pbeza

Description

@pbeza

The key derivation path in dstack/kms/src/main_service.rs feeds a DER-encoded public key into the HKDF info field. Since DER is not a canonical encoding for all key types, semantically identical keys with different DER encodings would derive different secrets.

Root Cause

The derive_dh_secret function computes SHA-256 over the full PKCS#8 DER encoding of a private key, which includes ASN.1 structure metadata (OIDs, sequence wrappers, version numbers) in addition to the raw key material. If a library upgrade changes the DER encoding format (e.g., different ASN.1 version, different optional field inclusion), the hash changes silently, breaking all derived secrets.

// kdf.rs:69-73
let dh_secret = sha2::Sha256::digest(&private_key.to_pkcs8_der()?);

Attack Path

  1. dstack upgrades the p256 or pkcs8 crate dependency
  2. The new version produces a slightly different DER encoding (e.g., adds or removes optional fields)
  3. derive_dh_secret produces a different hash from the same underlying key
  4. All RA-TLS connections using DH-derived keys break silently
  5. Existing disk encryption keys or other secrets derived from DH secrets become unreachable

Impact

Silent breakage of all DH-derived secrets after a library upgrade. This could cause data loss if disk encryption keys are derived through this path, or communication failures if RA-TLS session keys change unexpectedly.

Suggested Fix

Hash only the raw key bytes instead of the full DER encoding:

let scalar_bytes = private_key.to_bytes();
let dh_secret = sha2::Sha256::digest(&scalar_bytes);

SecretKey::to_bytes() returns the raw 32-byte scalar value, which is stable across library upgrades since it represents the mathematical key itself rather than an encoding format.


Note: This issue was created automatically. The vulnerability report was generated by Claude and has not been verified by a human.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions