schemapin 1.3.0

Cryptographic schema integrity verification for AI tools - Rust implementation
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
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::canonicalize::canonicalize_and_hash;
use crate::crypto;
use crate::discovery::validate_well_known_response;
use crate::error::ErrorCode;
use crate::pinning::{check_pinning, KeyPinStore, PinningResult};
use crate::resolver::SchemaResolver;
use crate::revocation::check_revocation_combined;
use crate::types::discovery::WellKnownResponse;
use crate::types::revocation::RevocationDocument;

/// Structured verification result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VerificationResult {
    pub valid: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domain: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub developer_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key_pinning: Option<KeyPinningStatus>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_code: Option<ErrorCode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_message: Option<String>,
    #[serde(default)]
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyPinningStatus {
    pub status: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub first_seen: Option<String>,
}

impl VerificationResult {
    pub fn success(
        domain: &str,
        developer_name: Option<&str>,
        pin_status: KeyPinningStatus,
    ) -> Self {
        Self {
            valid: true,
            domain: Some(domain.to_string()),
            developer_name: developer_name.map(|s| s.to_string()),
            key_pinning: Some(pin_status),
            error_code: None,
            error_message: None,
            warnings: vec![],
        }
    }

    pub fn failure(code: ErrorCode, message: &str) -> Self {
        Self {
            valid: false,
            domain: None,
            developer_name: None,
            key_pinning: None,
            error_code: Some(code),
            error_message: Some(message.to_string()),
            warnings: vec![],
        }
    }
}

/// Verify a schema offline using caller-provided documents.
///
/// Implements a 7-step verification flow:
/// 1. Validate discovery document
/// 2. Extract public key and compute fingerprint
/// 3. Check revocation (simple list + standalone document)
/// 4. TOFU key pinning
/// 5. Canonicalize schema and compute hash
/// 6. Verify ECDSA signature against hash
/// 7. Return result
pub fn verify_schema_offline(
    schema: &Value,
    signature_b64: &str,
    domain: &str,
    tool_id: &str,
    discovery: &WellKnownResponse,
    revocation: Option<&RevocationDocument>,
    pin_store: &mut KeyPinStore,
) -> VerificationResult {
    // Step 1: Validate discovery document
    if let Err(e) = validate_well_known_response(discovery) {
        return VerificationResult::failure(
            ErrorCode::DiscoveryInvalid,
            &format!("Discovery validation failed: {}", e),
        );
    }

    // Step 2: Extract public key and compute fingerprint
    let fingerprint = match crypto::calculate_key_id(&discovery.public_key_pem) {
        Ok(fp) => fp,
        Err(e) => {
            return VerificationResult::failure(
                ErrorCode::KeyNotFound,
                &format!("Failed to compute key fingerprint: {}", e),
            )
        }
    };

    // Step 3: Check revocation (both simple list and standalone doc)
    if let Err(e) = check_revocation_combined(&discovery.revoked_keys, revocation, &fingerprint) {
        let code = match &e {
            crate::error::Error::Verification { code, .. } => *code,
            _ => ErrorCode::KeyRevoked,
        };
        return VerificationResult::failure(code, &e.to_string());
    }

    // Step 4: TOFU key pinning
    let pin_result = match check_pinning(pin_store, tool_id, domain, &fingerprint) {
        Ok(r) => r,
        Err(e) => {
            return VerificationResult::failure(ErrorCode::KeyPinMismatch, &e.to_string());
        }
    };

    // Step 5: Canonicalize schema and compute hash
    let schema_hash = canonicalize_and_hash(schema);

    // Step 6: Verify ECDSA signature
    let valid =
        match crypto::verify_signature(&discovery.public_key_pem, &schema_hash, signature_b64) {
            Ok(v) => v,
            Err(e) => {
                return VerificationResult::failure(
                    ErrorCode::SignatureInvalid,
                    &format!("Signature verification error: {}", e),
                );
            }
        };

    if !valid {
        return VerificationResult::failure(
            ErrorCode::SignatureInvalid,
            "Schema signature is invalid",
        );
    }

    // Step 7: Build success result
    let pin_status = match pin_result {
        PinningResult::FirstUse => KeyPinningStatus {
            status: "first_use".to_string(),
            first_seen: Some(chrono::Utc::now().to_rfc3339()),
        },
        PinningResult::Matched => {
            let first_seen = pin_store
                .get_tool(tool_id, domain)
                .and_then(|t| t.pinned_keys.first())
                .map(|pk| pk.first_seen.clone());
            KeyPinningStatus {
                status: "pinned".to_string(),
                first_seen,
            }
        }
        PinningResult::Changed => unreachable!("Changed case handled above"),
    };

    VerificationResult::success(domain, discovery.developer_name.as_deref(), pin_status)
}

/// Verify a schema using a sync [`SchemaResolver`].
///
/// Resolves discovery and revocation documents from the resolver, then
/// delegates to [`verify_schema_offline`].
pub fn verify_schema_with_resolver(
    schema: &Value,
    signature_b64: &str,
    domain: &str,
    tool_id: &str,
    resolver: &dyn SchemaResolver,
    pin_store: &mut KeyPinStore,
) -> VerificationResult {
    let discovery = match resolver.resolve_discovery(domain) {
        Ok(doc) => doc,
        Err(e) => {
            return VerificationResult::failure(
                ErrorCode::DiscoveryFetchFailed,
                &format!("Failed to resolve discovery document: {}", e),
            )
        }
    };

    let revocation = match resolver.resolve_revocation(domain, &discovery) {
        Ok(doc) => doc,
        Err(_) => {
            return VerificationResult::failure(
                ErrorCode::DiscoveryFetchFailed,
                "Revocation document unreachable (fail-closed)",
            );
        }
    };

    verify_schema_offline(
        schema,
        signature_b64,
        domain,
        tool_id,
        &discovery,
        revocation.as_ref(),
        pin_store,
    )
}

/// Online verification that fetches discovery/revocation documents.
///
/// Uses [`WellKnownResolver`](crate::resolver::WellKnownResolver) to fetch
/// documents from standard `.well-known` HTTPS endpoints.
#[cfg(feature = "fetch")]
pub async fn verify_schema(
    schema: &Value,
    signature_b64: &str,
    domain: &str,
    tool_id: &str,
    pin_store: &mut KeyPinStore,
) -> VerificationResult {
    // Fetch discovery document
    let discovery = match crate::discovery::fetch_well_known(domain).await {
        Ok(doc) => doc,
        Err(e) => {
            return VerificationResult::failure(
                ErrorCode::DiscoveryFetchFailed,
                &format!("Failed to fetch discovery document: {}", e),
            )
        }
    };

    // Fetch revocation document
    let revocation = if let Some(ref endpoint) = discovery.revocation_endpoint {
        match crate::revocation::fetch_revocation_document(endpoint).await {
            Ok(doc) => Some(doc),
            Err(_) => {
                // Fail closed: if revocation endpoint is unreachable, reject
                return VerificationResult::failure(
                    ErrorCode::DiscoveryFetchFailed,
                    "Revocation endpoint unreachable (fail-closed)",
                );
            }
        }
    } else {
        None
    };

    verify_schema_offline(
        schema,
        signature_b64,
        domain,
        tool_id,
        &discovery,
        revocation.as_ref(),
        pin_store,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::canonicalize::canonicalize_and_hash;
    use crate::crypto::{generate_key_pair, sign_data};
    use crate::discovery::build_well_known_response;
    use crate::pinning::KeyPinStore;
    use crate::resolver::TrustBundleResolver;
    use crate::revocation::{add_revoked_key, build_revocation_document};
    use crate::types::bundle::{BundledDiscovery, SchemaPinTrustBundle};
    use crate::types::revocation::RevocationReason;
    use serde_json::json;

    struct TestFixture {
        schema: Value,
        signature: String,
        discovery: WellKnownResponse,
        pin_store: KeyPinStore,
    }

    fn setup() -> TestFixture {
        let kp = generate_key_pair().unwrap();
        let schema = json!({
            "name": "calculate_sum",
            "description": "Calculates the sum of two numbers",
            "parameters": { "a": "integer", "b": "integer" }
        });
        let hash = canonicalize_and_hash(&schema);
        let signature = sign_data(&kp.private_key_pem, &hash).unwrap();
        let discovery =
            build_well_known_response(&kp.public_key_pem, Some("Test Developer"), vec![], "1.2");

        TestFixture {
            schema,
            signature,
            discovery,
            pin_store: KeyPinStore::new(),
        }
    }

    #[test]
    fn test_happy_path_verification() {
        let mut f = setup();
        let result = verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(result.valid, "Expected valid, got: {:?}", result);
        assert_eq!(result.domain, Some("example.com".to_string()));
        assert_eq!(result.developer_name, Some("Test Developer".to_string()));
    }

    #[test]
    fn test_invalid_signature() {
        let mut f = setup();
        let result = verify_schema_offline(
            &f.schema,
            "bm90LWEtdmFsaWQtc2lnbmF0dXJl",
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(!result.valid);
        assert_eq!(result.error_code, Some(ErrorCode::SignatureInvalid));
    }

    #[test]
    fn test_tampered_schema() {
        let mut f = setup();
        let tampered = json!({
            "name": "calculate_sum",
            "description": "TAMPERED description",
            "parameters": { "a": "integer", "b": "integer" }
        });
        let result = verify_schema_offline(
            &tampered,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(!result.valid);
        assert_eq!(result.error_code, Some(ErrorCode::SignatureInvalid));
    }

    #[test]
    fn test_revoked_key() {
        let mut f = setup();
        let fingerprint = crypto::calculate_key_id(&f.discovery.public_key_pem).unwrap();
        f.discovery.revoked_keys = vec![fingerprint];

        let result = verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(!result.valid);
        assert_eq!(result.error_code, Some(ErrorCode::KeyRevoked));
    }

    #[test]
    fn test_revoked_key_in_doc() {
        let mut f = setup();
        let fingerprint = crypto::calculate_key_id(&f.discovery.public_key_pem).unwrap();
        let mut rev_doc = build_revocation_document("example.com");
        add_revoked_key(&mut rev_doc, &fingerprint, RevocationReason::KeyCompromise);

        let result = verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            Some(&rev_doc),
            &mut f.pin_store,
        );
        assert!(!result.valid);
        assert_eq!(result.error_code, Some(ErrorCode::KeyRevoked));
    }

    #[test]
    fn test_key_pin_change_rejected() {
        let mut f = setup();
        // First verification pins the key
        let result1 = verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(result1.valid);

        // Now change the key
        let kp2 = generate_key_pair().unwrap();
        let hash = canonicalize_and_hash(&f.schema);
        let sig2 = sign_data(&kp2.private_key_pem, &hash).unwrap();
        let disc2 = build_well_known_response(
            &kp2.public_key_pem,
            Some("Different Developer"),
            vec![],
            "1.2",
        );

        let result2 = verify_schema_offline(
            &f.schema,
            &sig2,
            "example.com",
            "calculate_sum",
            &disc2,
            None,
            &mut f.pin_store,
        );
        assert!(!result2.valid);
        assert_eq!(result2.error_code, Some(ErrorCode::KeyPinMismatch));
    }

    #[test]
    fn test_first_use_pinning_status() {
        let mut f = setup();
        let result = verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(result.valid);
        let pin = result.key_pinning.unwrap();
        assert_eq!(pin.status, "first_use");
        assert!(pin.first_seen.is_some());
    }

    #[test]
    fn test_matched_pinning_status() {
        let mut f = setup();
        // First verification
        verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        // Second verification
        let result = verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(result.valid);
        let pin = result.key_pinning.unwrap();
        assert_eq!(pin.status, "pinned");
    }

    #[test]
    fn test_verify_with_trust_bundle_resolver() {
        let f = setup();
        let bundle = SchemaPinTrustBundle {
            schemapin_bundle_version: "1.2".to_string(),
            created_at: "2026-02-10T00:00:00Z".to_string(),
            documents: vec![BundledDiscovery {
                domain: "example.com".to_string(),
                well_known: f.discovery.clone(),
            }],
            revocations: vec![],
        };
        let resolver = TrustBundleResolver::new(&bundle);
        let mut pin_store = KeyPinStore::new();

        let result = verify_schema_with_resolver(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &resolver,
            &mut pin_store,
        );
        assert!(result.valid, "Expected valid, got: {:?}", result);
        assert_eq!(result.domain, Some("example.com".to_string()));
    }

    #[test]
    fn test_verify_with_resolver_missing_domain() {
        let f = setup();
        let bundle = SchemaPinTrustBundle::new("2026-02-10T00:00:00Z");
        let resolver = TrustBundleResolver::new(&bundle);
        let mut pin_store = KeyPinStore::new();

        let result = verify_schema_with_resolver(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &resolver,
            &mut pin_store,
        );
        assert!(!result.valid);
        assert_eq!(result.error_code, Some(ErrorCode::DiscoveryFetchFailed));
    }

    #[test]
    fn test_invalid_discovery_document() {
        let mut f = setup();
        f.discovery.public_key_pem = "".to_string();

        let result = verify_schema_offline(
            &f.schema,
            &f.signature,
            "example.com",
            "calculate_sum",
            &f.discovery,
            None,
            &mut f.pin_store,
        );
        assert!(!result.valid);
        assert_eq!(result.error_code, Some(ErrorCode::DiscoveryInvalid));
    }
}