# RFC-0034: Key Material Migration - **Status:** Proposed; implements the `local-keystore` and `pem-der` rows of RFC-0008, deferred by RFC-0033. - **Owner:** Compatibility and migration architecture. - **Last updated:** 2026-11-30 ## Context RFC-0008 gives v3 key material two rows: a `local-keystore` is read for an inventory only, with "no raw private-key transfer", and a `pem-der` key file is a "provider-approved import only", with a declared key purpose, a 16 MiB ceiling, the password out of band, and an unencrypted private key only under an "exceptional migration policy". Reading v3 settled what those files hold: - **`LocalKeyStore`** writes `.pem`, a PKCS #8 private key, and `.json` metadata. It stores Ed25519, ECDSA, and RSA keys, which v3 used for signatures and RSA decryption; `import_key` can also store any PEM private key under a caller-chosen identifier. - **The key serializers of 3.0.0** (`serialize_private_key`, `serialize_public_key`, `utils.to_pem`) write encrypted or unencrypted PKCS #8 and SubjectPublicKeyInfo PEM. v3 encrypted to X25519 keys only (`ec_encrypt`); it never used an EC key for key agreement. - **Encryption** is `BestAvailableEncryption`: PBES2 with PBKDF2-HMAC-SHA256 at 2,048 iterations and AES-256-CBC. v4 holds local keys in one place: HPKE recipients (RFC-0012), which take X25519 and EC P-384 keys. It has no local signing or RSA provider. ## Finding K-1: 3.0.0 stores keystore passwords in clear `LocalKeyStore.add_key(key, name, password)` of the 3.0.0 release writes the password into the key's metadata (`meta["password"] = password`), and `_load_key` reads it back from there to decrypt the key. Anyone who can read a 3.0.0 keystore directory can therefore decrypt every "encrypted" key in it. The unreleased 3.x line stopped writing the field and ignores it, but files written by 3.0.0 still contain it. The Phase 1 baseline, which analyzed the later line, does not record this. Consequence: every encrypted key in a 3.0.0 keystore is to be treated as stored unencrypted and replaced. The inventory reports it (`password-in-metadata`) without reading the value, and the 3.0.0 advisory that RFC-0010 calls for should include it. ## Binding decision ### Inventory: `local-keystore` `cryptography_suite.migration.inventory_keystore(directory, policy=...)` returns a `KeystoreInventory` (`cs-key-inventory/1`) of one `KeystoreEntry` per key, and writes nothing. - **Permission:** the policy grants `local-keystore` until its deadline. - **What is read:** only `*.pem` and `*.json` entries of the directory itself, each at most 1 MiB, at most 10,000 entries. A file is opened without following links (`O_NOFOLLOW` where available), and its identity is compared with the one checked before opening; a link, a reparse point, another file type, a race, an oversized or unreadable file, a name outside `[A-Za-z0-9][A-Za-z0-9_.-]{0,127}`, metadata without a key, or a key file that is not one PKCS #8 PEM block is quarantined with a reason. A disallowed name is reported by its SHA-256, never echoed. - **No private key is decrypted.** An unencrypted key is parsed to verify its algorithm and fingerprint, which are reported as verified. An encrypted key is described by its metadata, marked unverified; the PBES2 parameters are read to report weak or unsupported encryption. - **Metadata is untrusted:** strict JSON (no duplicate fields, no `NaN`), scalar values only, bounded fields. A `password` field is reported, never read. Names are omitted unless `include_names=True`, as RFC-0008's privacy section asks. - **`v4_use`** is `hpke-recipient` for a verified X25519 or P-384 key that v3 did not record as a signing key, `unknown` for an encrypted key whose metadata names no signing type, and `none` otherwise. - **Findings** are stable codes: `unencrypted-private-key`, `password-in-metadata`, `weak-key-encryption` (under 600,000 PBKDF2 iterations), `unsupported-key-encryption`, `metadata-missing`, `metadata-invalid`, `metadata-mismatch`, `metadata-unverified`, and `unknown-metadata-fields`. ### Import: `pem-der` `cryptography_suite.migration.import_legacy_key(provider, key_id, source, policy=..., purpose=KeyPurpose.HPKE_RECIPIENT, password=...)` registers one key with an `HpkeRecipientProvider` and returns a `KeyImportReceipt` (`cs-key-import-receipt/1`). - **Structure is identified, never guessed:** the PEM label (`PRIVATE KEY`, `ENCRYPTED PRIVATE KEY`, `PUBLIC KEY`) or the outer DER shape selects one parser. A PEM file is exactly one block with no headers and strict Base64, and its content must have the structure its label names. PKCS #1, SEC 1, certificates, and other labels are `FORMAT_UNSUPPORTED`. Files are at most 64 KiB, below RFC-0008's ceiling. - **Encryption:** only PBES2 with PBKDF2, the scheme v3 wrote. The iteration count is read and bounded (10,000,000) before the password is tried. The password is required exactly when the key is encrypted; a wrong password or changed encrypted bytes are `AUTHENTICATION_FAILED`. - **Permission:** the policy grants `pem-der`; an unencrypted private key also needs the `pem-der-unencrypted` grant, RFC-0008's exceptional policy. The provider must be in `allowed_provider_ids` and the key's wrapping algorithm in `approved_wrapping_algorithms`, when they are set, so a FIPS policy admits P-384 and refuses X25519. - **Provider approval:** only keys an HPKE recipient can use are imported. Ed25519, RSA, and every other type are `CAPABILITY_UNSUPPORTED`: v4 has no local provider for them, and a signing key is never made a recipient. - **Declared purpose:** `KeyPurpose.HPKE_RECIPIENT` is the caller's statement that the key was used for key agreement or decryption. Because v3 used EC keys only to sign, importing a P-384 key adds a warning. - **Nothing is exported:** a private key becomes an identity in the caller's provider; no key material is written or returned. The receipt records the source digest, structure, algorithm, the v3 fingerprint (SHA-256 of the SubjectPublicKeyInfo, which matches the inventory), the v4 key reference and public key text, the policy, and warnings. - **A failure registers nothing.** ### Command line `cryptosuite migrate --legacy-format local-keystore --input DIR --receipt FILE [--include-names]` writes the inventory. `cryptosuite migrate --legacy-format pem-der --input FILE --key-id NAME --receipt FILE [--password-fd FD]` checks a key file and writes the receipt, whose `public_key_text` and `key_version` go into a provider configuration; the password is asked for only when the file's structure says the key is encrypted. Options that do not apply to a format are refused. ## Deferred - Importing keys into cloud key services. Each service has its own import procedure (wrapping keys, import tokens) and is used with its own tools. - Migrating v3 signing keys. v4 has no local signing provider; the audit chain uses its own keys, and reusing a general signing key there would let any v3 signature be mistaken for an audit record. - Reading the data v3 encrypted to these keys (`ec_encrypt`, hybrid JSON); RFC-0008 keeps those formats out of the stable package. ## Test and validation requirements - `tests/vectors/legacy/v3-key-vectors.json`: two keystores written by `LocalKeyStore` of 3.0.0 and of the baseline commit, which then signed with every signing key they stored, and ten key files written by 3.0.0's serializers and loaded again by its loaders; the DER vectors are the DER inside those files (`tools/generate_legacy_key_vectors.py`). - `tests/integration/test_legacy_keys.py`: every vector is read with v3's fingerprint; a v3 public key file and private key file work together as a v4 recipient and identity; every structure, label, length form, scheme, and iteration bound is checked; permission, provider, algorithm, and key type refusals register nothing; both keystores are inventoried with K-1 reported and no password in the output; quarantine, metadata, races, and I/O failures are covered. - `tests/cli/test_cli.py` covers both commands and their refusals.