fix(crypto): K-01 — format v3 canonical metadata authentication

This commit is contained in:
2026-09-19 00:26:27 +02:00
parent 151fdba09d
commit ad531d8393
7 changed files with 609 additions and 12 deletions
+28 -2
View File
@@ -4,7 +4,9 @@ use std::path::Path;
use anyhow::{bail, Context, Result};
use zeroize::Zeroizing;
use crate::crypto::{decrypt_chunk, FORMAT_VERSION_V1, FORMAT_VERSION_V2};
use crate::crypto::{
decrypt_chunk, FORMAT_VERSION_V1, FORMAT_VERSION_V2, FORMAT_VERSION_V3,
};
use crate::storage::Database;
/// Bericht über das Ergebnis einer Container-Integritätsprüfung.
@@ -81,7 +83,10 @@ pub fn verify_container(
let meta = match db.read_meta() {
Ok(m) => {
report.format_version = m.version;
if m.version != FORMAT_VERSION_V1 && m.version != FORMAT_VERSION_V2 {
if m.version != FORMAT_VERSION_V1
&& m.version != FORMAT_VERSION_V2
&& m.version != FORMAT_VERSION_V3
{
report.header_ok = false;
report.header_error = Some(format!("Unbekannte Formatversion: {}", m.version));
}
@@ -247,6 +252,25 @@ pub fn verify_container(
None
};
// 3b. Metadaten-Authentifizierung (HMAC-SHA256) für Format V3 (K-01)
if let Some(active_dek) = dek {
if let Some(ref m) = meta {
if m.version >= FORMAT_VERSION_V3 {
match db.verify_metadata_mac(active_dek) {
Ok(true) => {}
Ok(false) => {
report.errors.push(
"Kritischer Metadaten-Authentifizierungsfehler (K-01): Metadaten-MAC ungültig. Manipulationsversuch an Dateinamen, Größen oder Verzeichnisstruktur in der SQLite-Datenbank erkannt.".to_string(),
);
}
Err(e) => {
report.errors.push(format!("Fehler bei der Metadaten-MAC-Prüfung: {e}"));
}
}
}
}
}
// 4. Kryptografische Chunk- & AEAD-Authentifizierungsprüfung
let chunk_headers = db
.list_all_chunk_headers()
@@ -339,6 +363,7 @@ mod tests {
let (wrapped_dek, nonce, tag) = wrap_dek(&kek, &dek).unwrap();
let db = Database::open(&container_path).unwrap();
db.set_active_dek(dek.clone());
db.init_schema(&salt, &kdf_params, &wrapped_dek, &nonce, &tag)
.unwrap();
@@ -401,6 +426,7 @@ mod tests {
let (wrapped_dek, nonce, tag) = wrap_dek(&kek, &dek).unwrap();
let db = Database::open(&container_path).unwrap();
db.set_active_dek(dek.clone());
db.init_schema(&salt, &kdf_params, &wrapped_dek, &nonce, &tag)
.unwrap();