oxirs-star 0.2.4

RDF-star and SPARQL-star grammar support for quoted triples
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Cryptographic provenance chains with digital signatures
//!
//! This module provides secure, verifiable provenance tracking using
//! Ed25519 digital signatures for RDF-star annotations. Each provenance
//! record can be cryptographically signed to ensure authenticity and
//! integrity.

use crate::annotations::{ProvenanceRecord, TripleAnnotation};
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
use scirs2_core::random::{rng, Rng};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use thiserror::Error;
use tracing::{debug, info, warn};

/// Errors related to cryptographic provenance operations
#[derive(Error, Debug)]
pub enum CryptoProvenanceError {
    #[error("Signature verification failed: {0}")]
    VerificationFailed(String),

    #[error("Invalid signature format: {0}")]
    InvalidSignature(String),

    #[error("Invalid public key format: {0}")]
    InvalidPublicKey(String),

    #[error("Missing signature in provenance record")]
    MissingSignature,

    #[error("Provenance chain broken at index {0}")]
    BrokenChain(usize),

    #[error("Serialization error: {0}")]
    SerializationError(String),
}

/// Cryptographically signed provenance record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedProvenanceRecord {
    /// The underlying provenance record
    pub record: ProvenanceRecord,

    /// Digital signature of the record content
    pub signature: String,

    /// Public key of the signer (hex-encoded)
    pub public_key: String,

    /// Hash of the previous record in the chain (for integrity)
    pub previous_hash: Option<String>,

    /// Chain index (position in the provenance chain)
    pub chain_index: usize,
}

/// Key pair for signing provenance records
#[derive(Clone)]
pub struct ProvenanceKeyPair {
    signing_key: SigningKey,
    verifying_key: VerifyingKey,
}

impl ProvenanceKeyPair {
    /// Generate a new random key pair
    pub fn generate() -> Self {
        let mut rng_instance = rng();
        let mut seed = [0u8; 32];
        rng_instance.fill_bytes(&mut seed);

        let signing_key = SigningKey::from_bytes(&seed);
        let verifying_key = signing_key.verifying_key();

        Self {
            signing_key,
            verifying_key,
        }
    }

    /// Create key pair from existing seed
    pub fn from_seed(seed: &[u8; 32]) -> Self {
        let signing_key = SigningKey::from_bytes(seed);
        let verifying_key = signing_key.verifying_key();

        Self {
            signing_key,
            verifying_key,
        }
    }

    /// Get the public key as hex string
    pub fn public_key_hex(&self) -> String {
        hex::encode(self.verifying_key.as_bytes())
    }

    /// Get the public key bytes
    pub fn public_key_bytes(&self) -> &[u8; 32] {
        self.verifying_key.as_bytes()
    }

    /// Sign a message
    pub fn sign(&self, message: &[u8]) -> Signature {
        self.signing_key.sign(message)
    }

    /// Verify a signature
    pub fn verify(
        &self,
        message: &[u8],
        signature: &Signature,
    ) -> Result<(), CryptoProvenanceError> {
        self.verifying_key
            .verify(message, signature)
            .map_err(|e| CryptoProvenanceError::VerificationFailed(e.to_string()))
    }
}

/// Manager for cryptographic provenance chains
pub struct CryptoProvenanceManager {
    /// Key pairs indexed by agent identifier
    key_pairs: HashMap<String, ProvenanceKeyPair>,
}

impl CryptoProvenanceManager {
    /// Create a new provenance manager
    pub fn new() -> Self {
        Self {
            key_pairs: HashMap::new(),
        }
    }

    /// Register a key pair for an agent
    pub fn register_agent(&mut self, agent: String, key_pair: ProvenanceKeyPair) {
        debug!(
            "Registering agent '{}' with public key {}",
            agent,
            key_pair.public_key_hex()
        );
        self.key_pairs.insert(agent, key_pair);
    }

    /// Generate and register a new key pair for an agent
    pub fn generate_agent_key(&mut self, agent: String) -> ProvenanceKeyPair {
        let key_pair = ProvenanceKeyPair::generate();
        info!("Generated new key pair for agent '{}'", agent);
        self.register_agent(agent, key_pair.clone());
        key_pair
    }

    /// Sign a provenance record
    pub fn sign_record(
        &self,
        record: ProvenanceRecord,
        agent: &str,
        previous_hash: Option<String>,
        chain_index: usize,
    ) -> Result<SignedProvenanceRecord, CryptoProvenanceError> {
        let key_pair = self.key_pairs.get(agent).ok_or_else(|| {
            CryptoProvenanceError::VerificationFailed(format!("Agent '{}' not registered", agent))
        })?;

        // Serialize the record for signing
        let record_json = serde_json::to_string(&record)
            .map_err(|e| CryptoProvenanceError::SerializationError(e.to_string()))?;

        // Create message to sign (includes previous hash for chain integrity)
        let mut message = record_json.clone();
        if let Some(ref prev_hash) = previous_hash {
            message.push_str(prev_hash);
        }
        message.push_str(&chain_index.to_string());

        // Sign the message
        let signature = key_pair.sign(message.as_bytes());

        Ok(SignedProvenanceRecord {
            record,
            signature: hex::encode(signature.to_bytes()),
            public_key: key_pair.public_key_hex(),
            previous_hash,
            chain_index,
        })
    }

    /// Verify a signed provenance record
    pub fn verify_record(
        &self,
        signed_record: &SignedProvenanceRecord,
    ) -> Result<(), CryptoProvenanceError> {
        // Decode public key
        let public_key_bytes = hex::decode(&signed_record.public_key)
            .map_err(|e| CryptoProvenanceError::InvalidPublicKey(e.to_string()))?;

        let public_key_array: [u8; 32] = public_key_bytes.try_into().map_err(|_| {
            CryptoProvenanceError::InvalidPublicKey("Invalid key length".to_string())
        })?;

        let verifying_key = VerifyingKey::from_bytes(&public_key_array)
            .map_err(|e| CryptoProvenanceError::InvalidPublicKey(e.to_string()))?;

        // Decode signature
        let signature_bytes = hex::decode(&signed_record.signature)
            .map_err(|e| CryptoProvenanceError::InvalidSignature(e.to_string()))?;

        let signature_array: [u8; 64] = signature_bytes.try_into().map_err(|_| {
            CryptoProvenanceError::InvalidSignature("Invalid signature length".to_string())
        })?;

        let signature = Signature::from_bytes(&signature_array);

        // Reconstruct the signed message
        let record_json = serde_json::to_string(&signed_record.record)
            .map_err(|e| CryptoProvenanceError::SerializationError(e.to_string()))?;

        let mut message = record_json;
        if let Some(ref prev_hash) = signed_record.previous_hash {
            message.push_str(prev_hash);
        }
        message.push_str(&signed_record.chain_index.to_string());

        // Verify signature
        verifying_key
            .verify(message.as_bytes(), &signature)
            .map_err(|e| CryptoProvenanceError::VerificationFailed(e.to_string()))
    }

    /// Verify an entire provenance chain
    pub fn verify_chain(
        &self,
        chain: &[SignedProvenanceRecord],
    ) -> Result<(), CryptoProvenanceError> {
        if chain.is_empty() {
            return Ok(());
        }

        // Verify first record has no previous hash
        if chain[0].previous_hash.is_some() {
            return Err(CryptoProvenanceError::BrokenChain(0));
        }

        // Verify each record
        for (i, record) in chain.iter().enumerate() {
            // Verify signature
            self.verify_record(record)?;

            // Verify chain index
            if record.chain_index != i {
                warn!(
                    "Chain index mismatch at position {}: expected {}, got {}",
                    i, i, record.chain_index
                );
                return Err(CryptoProvenanceError::BrokenChain(i));
            }

            // Verify hash linkage (except for first record)
            if i > 0 {
                let prev_hash = compute_record_hash(&chain[i - 1]);
                if record.previous_hash.as_ref() != Some(&prev_hash) {
                    warn!("Hash chain broken at position {}", i);
                    return Err(CryptoProvenanceError::BrokenChain(i));
                }
            }
        }

        info!(
            "Provenance chain verified successfully ({} records)",
            chain.len()
        );
        Ok(())
    }

    /// Create a signed provenance chain from regular provenance records
    pub fn create_signed_chain(
        &self,
        records: Vec<ProvenanceRecord>,
    ) -> Result<Vec<SignedProvenanceRecord>, CryptoProvenanceError> {
        let mut signed_chain = Vec::new();
        let mut previous_hash = None;

        for (index, record) in records.into_iter().enumerate() {
            let agent = record.agent.clone();
            let signed_record = self.sign_record(record, &agent, previous_hash.clone(), index)?;

            previous_hash = Some(compute_record_hash(&signed_record));
            signed_chain.push(signed_record);
        }

        Ok(signed_chain)
    }
}

impl Default for CryptoProvenanceManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Compute SHA-256 hash of a signed provenance record
pub fn compute_record_hash(record: &SignedProvenanceRecord) -> String {
    let mut hasher = Sha256::new();

    // Hash the record content
    let record_json = serde_json::to_string(&record.record).unwrap_or_default();
    hasher.update(record_json.as_bytes());
    hasher.update(&record.signature);
    hasher.update(&record.public_key);
    hasher.update(record.chain_index.to_le_bytes());

    hex::encode(hasher.finalize())
}

/// Extension trait for TripleAnnotation to add cryptographic provenance
pub trait CryptoProvenanceExt {
    /// Convert regular provenance to signed provenance
    fn to_signed_provenance(
        &self,
        manager: &CryptoProvenanceManager,
    ) -> Result<Vec<SignedProvenanceRecord>, CryptoProvenanceError>;

    /// Verify all signed provenance in the annotation
    fn verify_provenance(
        &self,
        manager: &CryptoProvenanceManager,
    ) -> Result<(), CryptoProvenanceError>;

    /// Add a new signed provenance record
    fn add_signed_provenance(
        &mut self,
        record: ProvenanceRecord,
        manager: &CryptoProvenanceManager,
    ) -> Result<SignedProvenanceRecord, CryptoProvenanceError>;
}

/// Signed annotation with cryptographic provenance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedAnnotation {
    /// The base annotation
    pub annotation: TripleAnnotation,

    /// Cryptographically signed provenance chain
    pub signed_provenance: Vec<SignedProvenanceRecord>,

    /// Annotation signature (signature of the entire annotation)
    pub annotation_signature: Option<String>,

    /// Public key of annotation creator
    pub creator_public_key: Option<String>,
}

impl SignedAnnotation {
    /// Create a new signed annotation
    pub fn new(annotation: TripleAnnotation) -> Self {
        Self {
            annotation,
            signed_provenance: Vec::new(),
            annotation_signature: None,
            creator_public_key: None,
        }
    }

    /// Sign the entire annotation
    pub fn sign_annotation(
        &mut self,
        key_pair: &ProvenanceKeyPair,
    ) -> Result<(), CryptoProvenanceError> {
        let annotation_json = serde_json::to_string(&self.annotation)
            .map_err(|e| CryptoProvenanceError::SerializationError(e.to_string()))?;

        let signature = key_pair.sign(annotation_json.as_bytes());

        self.annotation_signature = Some(hex::encode(signature.to_bytes()));
        self.creator_public_key = Some(key_pair.public_key_hex());

        Ok(())
    }

    /// Verify the annotation signature
    pub fn verify_annotation_signature(&self) -> Result<(), CryptoProvenanceError> {
        let signature_hex = self
            .annotation_signature
            .as_ref()
            .ok_or(CryptoProvenanceError::MissingSignature)?;

        let public_key_hex =
            self.creator_public_key
                .as_ref()
                .ok_or(CryptoProvenanceError::InvalidPublicKey(
                    "Missing public key".to_string(),
                ))?;

        // Decode public key
        let public_key_bytes = hex::decode(public_key_hex)
            .map_err(|e| CryptoProvenanceError::InvalidPublicKey(e.to_string()))?;

        let public_key_array: [u8; 32] = public_key_bytes.try_into().map_err(|_| {
            CryptoProvenanceError::InvalidPublicKey("Invalid key length".to_string())
        })?;

        let verifying_key = VerifyingKey::from_bytes(&public_key_array)
            .map_err(|e| CryptoProvenanceError::InvalidPublicKey(e.to_string()))?;

        // Decode signature
        let signature_bytes = hex::decode(signature_hex)
            .map_err(|e| CryptoProvenanceError::InvalidSignature(e.to_string()))?;

        let signature_array: [u8; 64] = signature_bytes.try_into().map_err(|_| {
            CryptoProvenanceError::InvalidSignature("Invalid signature length".to_string())
        })?;

        let signature = Signature::from_bytes(&signature_array);

        // Reconstruct message
        let annotation_json = serde_json::to_string(&self.annotation)
            .map_err(|e| CryptoProvenanceError::SerializationError(e.to_string()))?;

        verifying_key
            .verify(annotation_json.as_bytes(), &signature)
            .map_err(|e| CryptoProvenanceError::VerificationFailed(e.to_string()))
    }

    /// Verify both annotation signature and provenance chain
    pub fn verify_all(
        &self,
        manager: &CryptoProvenanceManager,
    ) -> Result<(), CryptoProvenanceError> {
        // Verify annotation signature if present
        if self.annotation_signature.is_some() {
            self.verify_annotation_signature()?;
        }

        // Verify provenance chain
        manager.verify_chain(&self.signed_provenance)?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;

    #[test]
    fn test_key_pair_generation() {
        let key_pair = ProvenanceKeyPair::generate();
        let public_key = key_pair.public_key_hex();

        assert_eq!(public_key.len(), 64); // 32 bytes = 64 hex chars
    }

    #[test]
    fn test_sign_and_verify_record() {
        let mut manager = CryptoProvenanceManager::new();
        let agent = "agent1".to_string();

        manager.generate_agent_key(agent.clone());

        let record = ProvenanceRecord {
            action: "created".to_string(),
            agent: agent.clone(),
            timestamp: Utc::now(),
            activity: Some("test activity".to_string()),
            method: Some("manual".to_string()),
        };

        let signed = manager.sign_record(record, &agent, None, 0).unwrap();

        assert!(manager.verify_record(&signed).is_ok());
    }

    #[test]
    fn test_provenance_chain() {
        let mut manager = CryptoProvenanceManager::new();
        let agent = "agent1".to_string();

        manager.generate_agent_key(agent.clone());

        let records = vec![
            ProvenanceRecord {
                action: "created".to_string(),
                agent: agent.clone(),
                timestamp: Utc::now(),
                activity: Some("creation".to_string()),
                method: Some("manual".to_string()),
            },
            ProvenanceRecord {
                action: "modified".to_string(),
                agent: agent.clone(),
                timestamp: Utc::now(),
                activity: Some("modification".to_string()),
                method: Some("automatic".to_string()),
            },
        ];

        let chain = manager.create_signed_chain(records).unwrap();

        assert_eq!(chain.len(), 2);
        assert!(manager.verify_chain(&chain).is_ok());
    }

    #[test]
    fn test_tampered_chain_detection() {
        let mut manager = CryptoProvenanceManager::new();
        let agent = "agent1".to_string();

        manager.generate_agent_key(agent.clone());

        let records = vec![
            ProvenanceRecord {
                action: "created".to_string(),
                agent: agent.clone(),
                timestamp: Utc::now(),
                activity: Some("creation".to_string()),
                method: Some("manual".to_string()),
            },
            ProvenanceRecord {
                action: "modified".to_string(),
                agent: agent.clone(),
                timestamp: Utc::now(),
                activity: Some("modification".to_string()),
                method: Some("automatic".to_string()),
            },
        ];

        let mut chain = manager.create_signed_chain(records).unwrap();

        // Tamper with the second record
        chain[1].record.action = "tampered".to_string();

        // Verification should fail
        assert!(manager.verify_record(&chain[1]).is_err());
    }

    #[test]
    fn test_signed_annotation() {
        let key_pair = ProvenanceKeyPair::generate();
        let mut annotation = TripleAnnotation::new();
        annotation.confidence = Some(0.9);
        annotation.source = Some("test source".to_string());

        let mut signed_annotation = SignedAnnotation::new(annotation);
        signed_annotation.sign_annotation(&key_pair).unwrap();

        assert!(signed_annotation.verify_annotation_signature().is_ok());
    }

    #[test]
    fn test_tampered_annotation_detection() {
        let key_pair = ProvenanceKeyPair::generate();
        let mut annotation = TripleAnnotation::new();
        annotation.confidence = Some(0.9);

        let mut signed_annotation = SignedAnnotation::new(annotation);
        signed_annotation.sign_annotation(&key_pair).unwrap();

        // Tamper with the annotation
        signed_annotation.annotation.confidence = Some(0.1);

        // Verification should fail
        assert!(signed_annotation.verify_annotation_signature().is_err());
    }
}