-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
1246 lines (1125 loc) · 46 KB
/
Copy pathmain.rs
File metadata and controls
1246 lines (1125 loc) · 46 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![no_main]
sp1_zkvm::entrypoint!(main);
use aes_gcm::{
aead::{AeadInPlace, KeyInit},
Aes128Gcm,
};
use der::Decode;
use hkdf::Hkdf;
use hmac::{Hmac, Mac};
use p256::ecdsa::{signature::Verifier as EcVerifier, DerSignature, VerifyingKey};
use p384::ecdsa::{
signature::Verifier as P384Verifier, DerSignature as P384DerSignature,
VerifyingKey as P384VerifyingKey,
};
use rsa::{pkcs1v15, pkcs8::DecodePublicKey, RsaPublicKey};
use sha2::{Digest, Sha256};
use sp1_demo_common::{PublicClaim, TlsWitness};
use x25519_dalek::{PublicKey, StaticSecret};
use x509_cert::Certificate;
pub fn main() {
let witness: TlsWitness = sp1_zkvm::io::read();
// Notary-attested path: skip the X25519/HKDF/handshake-verification flow.
// The notary's Ed25519 signature attests to the session; we just need to
// verify the signature, check each ciphertext's commit hash matches the
// signed record, reconstruct K = K_N XOR K_C, decrypt, and run the claim.
if witness.notary.is_some() {
run_notary_path(&witness);
return;
}
// -----------------------------------------------------------------------
// 1. Parse ClientHello key_share → epk_client_wire. RFC 8446 §4.1.2, §4.2.8
// Assert epk_client_wire == esk_client × G.
// The server's CertificateVerify (step 9) signs the transcript that
// includes ClientHello, which includes epk_client. This assertion
// ties the proof to the specific ephemeral key used in the handshake.
// -----------------------------------------------------------------------
let epk_client_wire = parse_client_hello_key_share(&witness.raw_outbound)
.expect("epk_client not found in ClientHello key_share");
let derived_epk = PublicKey::from(&StaticSecret::from(witness.esk_client));
assert_eq!(
derived_epk.as_bytes(),
&epk_client_wire,
"epk_client in ClientHello does not match esk_client"
);
// -----------------------------------------------------------------------
// 2. Parse ServerHello key_share → epk_server. RFC 8446 §4.1.3, §4.2.8
// ServerHello key_share contains a single KeyShareEntry (unlike
// ClientHello which sends a list). NamedGroup 0x001d = X25519.
// -----------------------------------------------------------------------
let epk_server_bytes = parse_server_hello_key_share(&witness.raw_inbound)
.expect("epk_server not found in ServerHello key_share");
// -----------------------------------------------------------------------
// 3. X25519 ECDH + HKDF key schedule. RFC 8446 §7.1
//
// shared_secret = X25519(esk_client, epk_server)
//
// early_secret = HKDF-Extract(0×00·32, 0×00·32)
// derived1 = HKDF-Expand-Label(early_secret, "derived", SHA256(""), 32)
// hs_secret = HKDF-Extract(derived1, shared_secret)
// server_hs_secret = HKDF-Expand-Label(hs_secret, "s hs traffic", transcript_after_sh, 32)
// derived2 = HKDF-Expand-Label(hs_secret, "derived", SHA256(""), 32)
// master_secret = HKDF-Extract(derived2, 0×00·32)
//
// transcript_after_sh = SHA256(ClientHello || ServerHello)
// -----------------------------------------------------------------------
let client_secret = StaticSecret::from(witness.esk_client);
let server_pub = PublicKey::from(epk_server_bytes);
let ecdh = client_secret.diffie_hellman(&server_pub);
// Build transcript hash after ServerHello for HS key derivation.
// Need to find the raw ClientHello and ServerHello message bytes.
let ch_sh_transcript = {
// outbound first record payload = ClientHello message (type 22 record, payload is the HS messages)
let out_records = parse_records(&witness.raw_outbound);
let in_records = parse_records(&witness.raw_inbound);
let ch_payload = out_records
.iter()
.find(|r| r.content_type == 22)
.expect("ClientHello record not found")
.payload
.clone();
let sh_payload = in_records
.iter()
.find(|r| r.content_type == 22)
.expect("ServerHello record not found")
.payload
.clone();
let mut h = Sha256::new();
h.update(&ch_payload);
h.update(&sh_payload);
h.finalize()
};
let transcript_after_sh: [u8; 32] = ch_sh_transcript.into();
let (early_secret, _) = Hkdf::<Sha256>::extract(Some(&[0u8; 32]), &[0u8; 32]);
let derived1 = expand_label(&early_secret, "derived", &empty_hash(), 32);
let (hs_secret, _) = Hkdf::<Sha256>::extract(Some(&derived1), ecdh.as_bytes());
let server_hs_secret = expand_label(&hs_secret, "s hs traffic", &transcript_after_sh, 32);
let derived2 = expand_label(&hs_secret, "derived", &empty_hash(), 32);
let (master_secret, _) = Hkdf::<Sha256>::extract(Some(&derived2), &[0u8; 32]);
// -----------------------------------------------------------------------
// 4. Decrypt encrypted handshake records. RFC 8446 §5.2, §4.4
//
// Encrypted records have outer content_type=23 (application_data).
// After AES-128-GCM decryption the last byte is the inner content
// type (22 = handshake). RFC 8446 §5.2
//
// hs_key = HKDF-Expand-Label(server_hs_secret, "key", "", 16)
// hs_iv = HKDF-Expand-Label(server_hs_secret, "iv", "", 12)
// nonce = hs_iv XOR seq (seq resets to 0 for each key)
//
// Expected handshake messages in order:
// 8 EncryptedExtensions RFC 8446 §4.3.1
// 11 Certificate RFC 8446 §4.4.2
// 15 CertificateVerify RFC 8446 §4.4.3
// 20 Finished RFC 8446 §4.4.4
// -----------------------------------------------------------------------
let hs_key_vec = expand_label(&server_hs_secret, "key", &[], 16);
let hs_iv_vec = expand_label(&server_hs_secret, "iv", &[], 12);
let mut hs_key = [0u8; 16];
let mut hs_iv = [0u8; 12];
hs_key.copy_from_slice(&hs_key_vec);
hs_iv.copy_from_slice(&hs_iv_vec);
let out_records = parse_records(&witness.raw_outbound);
let in_records = parse_records(&witness.raw_inbound);
// Handshake transcript messages in order: [CH, SH, EE, Cert, CertVerify, Finished]
let mut handshake_messages: Vec<Vec<u8>> = Vec::new();
let ch_payload = out_records
.iter()
.find(|r| r.content_type == 22)
.expect("ClientHello not found")
.payload
.clone();
handshake_messages.push(ch_payload);
let sh_payload = in_records
.iter()
.find(|r| r.content_type == 22)
.expect("ServerHello not found")
.payload
.clone();
handshake_messages.push(sh_payload);
const HS_ENCRYPTED_EXTENSIONS: u8 = 8;
const HS_CERTIFICATE: u8 = 11;
const HS_CERTIFICATE_VERIFY: u8 = 15;
const HS_FINISHED: u8 = 20;
let mut hs_seq: u64 = 0;
let mut cert_chain_der: Option<Vec<Vec<u8>>> = None;
let mut cert_verify_msg: Option<Vec<u8>> = None;
let mut server_finished_body: Option<Vec<u8>> = None;
let mut handshake_done = false;
let mut encrypted_app_records: Vec<Vec<u8>> = Vec::new();
for r in &in_records {
if r.content_type != 23 {
continue;
}
if handshake_done {
encrypted_app_records.push(record_to_bytes(r));
continue;
}
let (plaintext, inner_ct) =
decrypt_record(&hs_key, &hs_iv, hs_seq, r).expect("HS record decrypt failed");
hs_seq += 1;
if inner_ct == 23 {
// Application data snuck in before Finished — treat as app record.
handshake_done = true;
encrypted_app_records.push(record_to_bytes(r));
continue;
}
let msgs = parse_handshake_messages(&plaintext).expect("parse HS messages failed");
for msg in &msgs {
let raw: Vec<u8> = hs_header(msg).iter().chain(&msg.body).copied().collect();
match msg.msg_type {
HS_ENCRYPTED_EXTENSIONS => {
handshake_messages.push(raw);
}
HS_CERTIFICATE => {
cert_chain_der =
Some(parse_cert_message(&msg.body).expect("parse Certificate failed"));
handshake_messages.push(raw);
}
HS_CERTIFICATE_VERIFY => {
cert_verify_msg = Some(msg.body.clone());
handshake_messages.push(raw);
}
HS_FINISHED => {
server_finished_body = Some(msg.body.clone());
handshake_messages.push(raw);
handshake_done = true;
}
_ => {
handshake_messages.push(raw);
}
}
}
}
assert!(
handshake_messages.len() >= 6,
"need at least 6 handshake messages, got {}",
handshake_messages.len()
);
let cert_chain_der = cert_chain_der.expect("Certificate message not found");
let cert_verify_msg = cert_verify_msg.expect("CertificateVerify not found");
let server_finished_body = server_finished_body.expect("ServerFinished not found");
// -----------------------------------------------------------------------
// 5. Transcript hashes. RFC 8446 §4.4 (transcript hash definition)
//
// transcript_before_cv = SHA256(CH || SH || EE || Cert)
// transcript_before_finished = SHA256(CH || SH || EE || Cert || CV)
// transcript_after_finished = SHA256(CH || SH || EE || Cert || CV || Fin)
//
// Each hash covers the 4-byte handshake header (type + 3-byte length)
// plus the message body, exactly as transmitted on the wire.
// -----------------------------------------------------------------------
let transcript_before_cv: [u8; 32] = {
let mut h = Sha256::new();
for msg in &handshake_messages[..4] {
h.update(msg);
}
h.finalize().into()
};
let transcript_before_finished: [u8; 32] = {
let mut h = Sha256::new();
for msg in &handshake_messages[..5] {
h.update(msg);
}
h.finalize().into()
};
let transcript_after_finished: [u8; 32] = {
let mut h = Sha256::new();
for msg in &handshake_messages[..6] {
h.update(msg);
}
h.finalize().into()
};
// -----------------------------------------------------------------------
// 6. Verify Server Finished HMAC. RFC 8446 §4.4.4
//
// finished_key = HKDF-Expand-Label(server_hs_secret, "finished", "", 32)
// verify_data = HMAC-SHA256(finished_key, transcript_before_finished)
//
// Proves the server completed the handshake: only a party that derived
// server_hs_secret (i.e. performed X25519 with the real server) can
// produce a valid verify_data.
// -----------------------------------------------------------------------
let finished_key = expand_label(&server_hs_secret, "finished", &[], 32);
let mut mac = <Hmac<Sha256> as KeyInit>::new_from_slice(&finished_key).unwrap();
mac.update(&transcript_before_finished);
let expected_verify_data = mac.finalize().into_bytes();
assert_eq!(
expected_verify_data.as_ref() as &[u8],
server_finished_body.as_slice(),
"Server Finished HMAC verification failed"
);
// -----------------------------------------------------------------------
// 7. Certificate chain verification. RFC 8446 §4.4.2, RFC 5280 §6
//
// Walk chain[0] (leaf) → chain[n-1], verify each cert's signature
// against the next cert's SPKI (or a trust anchor from webpki-roots).
// Trust anchor matching: subject bytes or last-cert issuer bytes
// compared against Mozilla root store (webpki-roots crate).
// Supported signature algorithms: ECDSA-P256-SHA256, ECDSA-P384-SHA384,
// RSA-PKCS1-SHA256, RSA-PKCS1-SHA384. RFC 5280 §4.1.1.2
// -----------------------------------------------------------------------
let chain: Vec<Certificate> = cert_chain_der
.iter()
.map(|der| Certificate::from_der(der).expect("invalid DER cert"))
.collect();
assert!(!chain.is_empty(), "empty cert chain");
let (anchor_boundary, anchor_spki) = find_trust_anchor(&chain, &cert_chain_der);
for i in 0..anchor_boundary {
let issuer_spki = if i + 1 < anchor_boundary {
chain[i + 1].tbs_certificate.subject_public_key_info.clone()
} else {
anchor_spki.clone()
};
verify_cert_signature(&chain[i], &issuer_spki);
}
// -----------------------------------------------------------------------
// 8. Hostname verification via leaf cert SAN. RFC 9525, RFC 5280 §4.2.1.6
//
// Check SubjectAltName extension (OID 2.5.29.17) for a dNSName entry
// matching the requested hostname. Wildcard: *.example.com matches
// foo.example.com but not foo.bar.example.com (single label only).
// RFC 9525 §6.3 prohibits multi-level wildcard matching.
// -----------------------------------------------------------------------
verify_hostname(&chain[0], &witness.hostname);
// -----------------------------------------------------------------------
// 9. CertificateVerify. RFC 8446 §4.4.3, §4.2.3
//
// signed_content = 0x20×64 || "TLS 1.3, server CertificateVerify" || 0x00
// || transcript_before_cv
//
// The 64-byte padding and context string prevent cross-protocol attacks.
// Supported signature schemes (RFC 8446 §4.2.3):
// 0x0403 ecdsa_secp256r1_sha256 (P-256)
// 0x0503 ecdsa_secp384r1_sha384 (P-384)
// 0x0804 rsa_pss_rsae_sha256
// 0x0805 rsa_pss_rsae_sha384
// 0x0401 rsa_pkcs1_sha256
//
// This is the key security step: proves the real server (holder of the
// cert private key) signed a transcript that includes epk_client.
// -----------------------------------------------------------------------
let cv_msg = &cert_verify_msg;
assert!(cv_msg.len() >= 4, "CertificateVerify too short");
let scheme = u16::from_be_bytes([cv_msg[0], cv_msg[1]]);
let sig_len = u16::from_be_bytes([cv_msg[2], cv_msg[3]]) as usize;
assert!(cv_msg.len() >= 4 + sig_len, "CertificateVerify truncated");
let sig_bytes = &cv_msg[4..4 + sig_len];
let mut signed_content = vec![0x20u8; 64];
signed_content.extend_from_slice(b"TLS 1.3, server CertificateVerify");
signed_content.push(0x00);
signed_content.extend_from_slice(&transcript_before_cv);
let leaf_spki = &chain[0].tbs_certificate.subject_public_key_info;
let leaf_pk = leaf_spki.subject_public_key.raw_bytes();
match scheme {
0x0403 => {
let vk = VerifyingKey::from_sec1_bytes(leaf_pk).expect("leaf cert not P-256");
let sig = DerSignature::try_from(sig_bytes).expect("invalid ECDSA-P256 sig DER");
vk.verify(&signed_content, &sig)
.expect("CertificateVerify ECDSA-P256 sig invalid");
}
0x0503 => {
let vk = P384VerifyingKey::from_sec1_bytes(leaf_pk).expect("leaf cert not P-384");
let sig = P384DerSignature::try_from(sig_bytes).expect("invalid ECDSA-P384 sig DER");
P384Verifier::verify(&vk, &signed_content, &sig)
.expect("CertificateVerify ECDSA-P384 sig invalid");
}
0x0804 => {
use der::Encode;
use rsa::pss::{Signature as PssSignature, VerifyingKey as PssVk};
let spki_der = leaf_spki.to_der().expect("encode leaf SPKI");
let pk = RsaPublicKey::from_public_key_der(&spki_der).expect("parse RSA leaf key");
let vk = PssVk::<Sha256>::new(pk);
let sig = PssSignature::try_from(sig_bytes).expect("invalid RSA-PSS sig");
rsa::signature::Verifier::verify(&vk, &signed_content, &sig)
.expect("CertificateVerify RSA-PSS-SHA256 sig invalid");
}
0x0805 => {
use der::Encode;
use rsa::pss::{Signature as PssSignature, VerifyingKey as PssVk};
use sha2::Sha384;
let spki_der = leaf_spki.to_der().expect("encode leaf SPKI");
let pk = RsaPublicKey::from_public_key_der(&spki_der).expect("parse RSA leaf key");
let vk = PssVk::<Sha384>::new(pk);
let sig = PssSignature::try_from(sig_bytes).expect("invalid RSA-PSS sig");
rsa::signature::Verifier::verify(&vk, &signed_content, &sig)
.expect("CertificateVerify RSA-PSS-SHA384 sig invalid");
}
0x0401 => {
use der::Encode;
let spki_der = leaf_spki.to_der().expect("encode leaf SPKI");
let pk = RsaPublicKey::from_public_key_der(&spki_der).expect("parse RSA leaf key");
let vk = pkcs1v15::VerifyingKey::<Sha256>::new(pk);
let sig = pkcs1v15::Signature::try_from(sig_bytes).expect("invalid RSA-PKCS1 sig");
rsa::signature::Verifier::verify(&vk, &signed_content, &sig)
.expect("CertificateVerify RSA-PKCS1-SHA256 sig invalid");
}
other => panic!("unsupported CertificateVerify scheme: 0x{:04x}", other),
}
// -----------------------------------------------------------------------
// 10. App traffic key derivation + application record decryption.
// RFC 8446 §7.3 (traffic key calculation), §5.2, §5.3
//
// server_app_secret = HKDF-Expand-Label(master_secret, "s ap traffic",
// transcript_after_finished, 32)
// app_key = HKDF-Expand-Label(server_app_secret, "key", "", 16)
// app_iv = HKDF-Expand-Label(server_app_secret, "iv", "", 12)
// nonce = app_iv XOR seq_be (RFC 8446 §5.3)
// aad = 5-byte TLS record header (RFC 8446 §5.2)
// -----------------------------------------------------------------------
let server_app_secret = expand_label(
&master_secret,
"s ap traffic",
&transcript_after_finished,
32,
);
let app_key_vec = expand_label(&server_app_secret, "key", &[], 16);
let app_iv_vec = expand_label(&server_app_secret, "iv", &[], 12);
let mut app_key = [0u8; 16];
let mut app_iv = [0u8; 12];
app_key.copy_from_slice(&app_key_vec);
app_iv.copy_from_slice(&app_iv_vec);
// Sequence-number continuity: the AES-128-GCM nonce is `app_iv XOR seq`,
// so if the host omits any record in the middle the subsequent decryptions
// will use the wrong nonce and GCM auth will fail. Records at the tail
// can still be withheld without triggering this check; protection against
// tail omission requires verifying HTTP response completeness (Content-
// Length or terminal chunked boundary) — tracked in NEXT.md.
let mut plaintext = Vec::new();
for (seq, record_bytes) in encrypted_app_records.iter().enumerate() {
assert!(record_bytes.len() >= 5);
let ct = record_bytes[0];
let version = u16::from_be_bytes([record_bytes[1], record_bytes[2]]);
let payload = &record_bytes[5..];
let nonce = xor_nonce(&app_iv, seq as u64);
let aad = {
let mut h = [0u8; 5];
h[0] = ct;
h[1..3].copy_from_slice(&version.to_be_bytes());
h[3..5].copy_from_slice(&(payload.len() as u16).to_be_bytes());
h
};
let tag_start = payload.len() - 16;
let mut tag = [0u8; 16];
tag.copy_from_slice(&payload[tag_start..]);
let mut buf = payload[..tag_start].to_vec();
<Aes128Gcm as KeyInit>::new(&app_key.into())
.decrypt_in_place_detached(nonce.as_ref().into(), &aad, &mut buf, &tag.into())
.expect("AES-GCM app record auth failed");
let inner_ct = *buf.last().expect("empty decrypted record");
buf.pop();
if inner_ct == 23 {
plaintext.extend_from_slice(&buf);
}
}
// -----------------------------------------------------------------------
// 11. HTTP/1.1 response parsing + JSON assertion.
// RFC 9112 §6.1 (chunked transfer encoding), RFC 6901 (JSON Pointer)
// -----------------------------------------------------------------------
let response = String::from_utf8(plaintext).expect("response is not UTF-8");
let raw_body = response.split("\r\n\r\n").nth(1).unwrap_or("");
let body = unchunk(raw_body);
let json: serde_json::Value = serde_json::from_str(&body).expect("body is not valid JSON");
let field_value = json
.pointer(&witness.json_field)
.unwrap_or_else(|| panic!("field '{}' not found", witness.json_field))
.as_f64()
.unwrap_or_else(|| panic!("field '{}' is not a number", witness.json_field));
assert!(
field_value > witness.threshold,
"{} = {} is not > {}",
witness.json_field,
field_value,
witness.threshold
);
// -----------------------------------------------------------------------
// 12. Commit public values.
// -----------------------------------------------------------------------
sp1_zkvm::io::commit(&PublicClaim {
host: witness.hostname,
field: witness.json_field,
threshold: witness.threshold,
value: field_value,
});
}
// ---------------------------------------------------------------------------
// Notary-attested path: skip all handshake derivation, trust the bundle's
// signature, reconstruct K = K_N XOR K_C, decrypt records, claim.
// ---------------------------------------------------------------------------
fn run_notary_path(witness: &TlsWitness) {
let att = witness
.notary
.as_ref()
.expect("run_notary_path called without notary attestation");
// 1. Verify the notary's Ed25519 signature over the bundle.
assert!(
att.bundle.verify(),
"notary bundle signature did not verify"
);
if att.bundle.bundle_version >= sp1_demo_common::NotaryBundle::BUNDLE_VERSION_BINDING {
let b = &att.bundle.binding;
let ht: [u8; 32] = {
let mut h = Sha256::new();
h.update((witness.raw_outbound.len() as u64).to_be_bytes());
h.update(&witness.raw_outbound);
h.update((witness.raw_inbound.len() as u64).to_be_bytes());
h.update(&witness.raw_inbound);
h.finalize().into()
};
assert_eq!(
ht, b.handshake_transcript_hash,
"bundle handshake_transcript_hash != witness raw bytes"
);
let epk = parse_server_hello_key_share(&witness.raw_inbound)
.expect("epk_server in witness inbound");
assert_eq!(
epk, b.server_epk,
"bundle server_epk != ServerHello key_share"
);
}
// 2. Bundle's server_name must match the hostname we're claiming about.
assert_eq!(
att.bundle.server_name, witness.hostname,
"bundle.server_name != witness.hostname"
);
// 3. Reconstruct rx side key. K = K_N XOR K_C.
let mut k_rx = [0u8; 16];
for i in 0..16 {
k_rx[i] = att.bundle.k_n_rx[i] ^ att.k_c_rx[i];
}
let iv_rx = att.bundle.iv_rx;
// 4. Walk raw_inbound; for each application_data record, check the
// notary's commit_hash matches SHA-256(payload), then decrypt with K_rx.
let records = parse_records(&witness.raw_inbound);
let mut plaintext = Vec::new();
let mut rx_seq: u64 = 0;
for r in &records {
if r.content_type != 23 {
continue; // only encrypted application records carry app data in TLS 1.3
}
let commit_hash: [u8; 32] = {
let mut h = Sha256::new();
h.update(&r.payload);
h.finalize().into()
};
let bundle_rec = att
.bundle
.records
.iter()
.find(|br| br.op == 0x02 && br.seq == rx_seq)
.expect("notary did not commit to this decrypt record (op=0x02)");
assert_eq!(
commit_hash, bundle_rec.commit_hash,
"record at rx_seq={} doesn't match bundle commit",
rx_seq
);
// AAD = the 5-byte TLS record header (RFC 8446 §5.2).
let aad = {
let mut h = [0u8; 5];
h[0] = r.content_type;
h[1..3].copy_from_slice(&r.legacy_version.to_be_bytes());
h[3..5].copy_from_slice(&(r.payload.len() as u16).to_be_bytes());
h
};
let nonce = xor_nonce(&iv_rx, rx_seq);
rx_seq += 1;
// ciphertext_payload = ct || tag (tag is 16 bytes for AES-128-GCM)
let tag_start = r.payload.len() - 16;
let mut tag = [0u8; 16];
tag.copy_from_slice(&r.payload[tag_start..]);
let mut buf = r.payload[..tag_start].to_vec();
<Aes128Gcm as KeyInit>::new(&k_rx.into())
.decrypt_in_place_detached(nonce.as_ref().into(), &aad, &mut buf, &tag.into())
.expect("notarized AES-GCM auth failed");
// Strip trailing zeros then the inner content type (TLS 1.3 §5.2).
while buf.last() == Some(&0u8) {
buf.pop();
}
let Some(inner_ct) = buf.pop() else {
panic!("empty decrypted notarized record");
};
if inner_ct == 23 {
plaintext.extend_from_slice(&buf);
}
}
// 5. Parse HTTP + JSON + threshold check (same as the self-proving path).
let response = String::from_utf8(plaintext).expect("response not UTF-8");
let raw_body = response.split("\r\n\r\n").nth(1).unwrap_or("");
let body = unchunk(raw_body);
let json: serde_json::Value = serde_json::from_str(&body).expect("body is not valid JSON");
let field_value = json
.pointer(&witness.json_field)
.unwrap_or_else(|| panic!("field '{}' not found", witness.json_field))
.as_f64()
.unwrap_or_else(|| panic!("field '{}' is not a number", witness.json_field));
assert!(
field_value > witness.threshold,
"{} = {} is not > {}",
witness.json_field,
field_value,
witness.threshold
);
sp1_zkvm::io::commit(&PublicClaim {
host: witness.hostname.clone(),
field: witness.json_field.clone(),
threshold: witness.threshold,
value: field_value,
});
}
// ---------------------------------------------------------------------------
// TLS record layer
// ---------------------------------------------------------------------------
struct RawRecord {
content_type: u8,
legacy_version: u16,
payload: Vec<u8>,
}
// TLS record header: content_type(1) + legacy_version(2) + length(2). RFC 8446 §5.1
fn parse_records(bytes: &[u8]) -> Vec<RawRecord> {
let mut records = Vec::new();
let mut pos = 0;
while pos + 5 <= bytes.len() {
let ct = bytes[pos];
let version = u16::from_be_bytes([bytes[pos + 1], bytes[pos + 2]]);
let length = u16::from_be_bytes([bytes[pos + 3], bytes[pos + 4]]) as usize;
pos += 5;
assert!(pos + length <= bytes.len(), "truncated TLS record");
records.push(RawRecord {
content_type: ct,
legacy_version: version,
payload: bytes[pos..pos + length].to_vec(),
});
pos += length;
}
records
}
fn record_to_bytes(r: &RawRecord) -> Vec<u8> {
let mut out = Vec::with_capacity(5 + r.payload.len());
out.push(r.content_type);
out.extend_from_slice(&r.legacy_version.to_be_bytes());
out.extend_from_slice(&(r.payload.len() as u16).to_be_bytes());
out.extend_from_slice(&r.payload);
out
}
// Decrypt one TLS 1.3 record; strip inner content type byte. RFC 8446 §5.2
fn decrypt_record(
key: &[u8; 16],
iv: &[u8; 12],
seq: u64,
record: &RawRecord,
) -> Result<(Vec<u8>, u8), &'static str> {
let nonce = xor_nonce(iv, seq);
let aad = {
let mut h = [0u8; 5];
h[0] = record.content_type;
h[1..3].copy_from_slice(&record.legacy_version.to_be_bytes());
h[3..5].copy_from_slice(&(record.payload.len() as u16).to_be_bytes());
h
};
let mut buf = record.payload.clone();
if buf.len() < 16 {
return Err("record too short for GCM tag");
}
let tag_start = buf.len() - 16;
let mut tag = [0u8; 16];
tag.copy_from_slice(&buf[tag_start..]);
buf.truncate(tag_start);
<Aes128Gcm as KeyInit>::new(key.into())
.decrypt_in_place_detached(nonce.as_ref().into(), &aad, &mut buf, &tag.into())
.map_err(|_| "AES-GCM authentication failed")?;
let inner_ct = *buf.last().ok_or("empty decrypted record")?;
buf.pop();
Ok((buf, inner_ct))
}
// ---------------------------------------------------------------------------
// Handshake message parsing
// ---------------------------------------------------------------------------
struct HandshakeMsg {
msg_type: u8,
body: Vec<u8>,
}
// Handshake message: msg_type(1) + length(3) + body. RFC 8446 §4
fn parse_handshake_messages(data: &[u8]) -> Result<Vec<HandshakeMsg>, &'static str> {
let mut msgs = Vec::new();
let mut pos = 0;
while pos < data.len() {
if data.len() - pos < 4 {
return Err("truncated handshake header");
}
let msg_type = data[pos];
let length = u32::from_be_bytes([0, data[pos + 1], data[pos + 2], data[pos + 3]]) as usize;
pos += 4;
if data.len() - pos < length {
return Err("truncated handshake body");
}
msgs.push(HandshakeMsg {
msg_type,
body: data[pos..pos + length].to_vec(),
});
pos += length;
}
Ok(msgs)
}
fn hs_header(msg: &HandshakeMsg) -> [u8; 4] {
let len = msg.body.len() as u32;
[msg.msg_type, (len >> 16) as u8, (len >> 8) as u8, len as u8]
}
/// Certificate message body layout. RFC 8446 §4.4.2
/// certificate_request_context(1+N) + certificate_list(3-byte len, then entries)
/// Each entry: cert_data(3-byte len + DER) + extensions(2-byte len + data)
fn parse_cert_message(body: &[u8]) -> Result<Vec<Vec<u8>>, &'static str> {
let mut pos = 0;
if pos >= body.len() {
return Err("empty Certificate message");
}
let ctx_len = body[pos] as usize;
pos += 1 + ctx_len;
if body.len() - pos < 3 {
return Err("truncated certificate_list length");
}
let list_len = u32::from_be_bytes([0, body[pos], body[pos + 1], body[pos + 2]]) as usize;
pos += 3;
let list_end = pos + list_len;
let mut certs = Vec::new();
while pos < list_end {
if list_end - pos < 3 {
return Err("truncated cert_data length");
}
let cert_len = u32::from_be_bytes([0, body[pos], body[pos + 1], body[pos + 2]]) as usize;
pos += 3;
if list_end - pos < cert_len {
return Err("truncated cert_data");
}
certs.push(body[pos..pos + cert_len].to_vec());
pos += cert_len;
if list_end - pos < 2 {
return Err("truncated cert extensions length");
}
let ext_len = u16::from_be_bytes([body[pos], body[pos + 1]]) as usize;
pos += 2 + ext_len;
}
Ok(certs)
}
// ---------------------------------------------------------------------------
// TLS extension key_share parsers
// ---------------------------------------------------------------------------
/// Extract the X25519 public key from the key_share extension of a
/// ClientHello record payload (the raw handshake bytes, not the record header).
///
/// ClientHello layout (simplified):
/// 2 legacy_version
/// 32 client_random
/// 1 session_id_len + session_id
/// 2 cipher_suites_len + cipher_suites
/// 1 compression_methods_len + compression_methods
/// 2 extensions_len
/// extensions...
///
/// key_share extension (type 0x0033) in ClientHello contains a list of
/// KeyShareEntry: { 2-byte NamedGroup, 2-byte key_len, key_bytes }.
fn parse_client_hello_key_share(outbound: &[u8]) -> Option<[u8; 32]> {
// Find the ClientHello record (content_type = 22, plaintext handshake).
let records = parse_records(outbound);
let ch_payload = records
.iter()
.find(|r| r.content_type == 22)?
.payload
.clone();
// ch_payload = one or more handshake messages; first should be ClientHello (type 1).
let msgs = parse_handshake_messages(&ch_payload).ok()?;
let ch = msgs.iter().find(|m| m.msg_type == 1)?; // ClientHello
let body = &ch.body;
let mut pos = 0;
// legacy_version (2)
pos += 2;
// client_random (32)
pos += 32;
// session_id
if pos >= body.len() {
return None;
}
let sid_len = body[pos] as usize;
pos += 1 + sid_len;
// cipher_suites
if pos + 2 > body.len() {
return None;
}
let cs_len = u16::from_be_bytes([body[pos], body[pos + 1]]) as usize;
pos += 2 + cs_len;
// compression_methods
if pos >= body.len() {
return None;
}
let cm_len = body[pos] as usize;
pos += 1 + cm_len;
// extensions_len
if pos + 2 > body.len() {
return None;
}
let ext_total = u16::from_be_bytes([body[pos], body[pos + 1]]) as usize;
pos += 2;
let ext_end = pos + ext_total;
parse_key_share_client(&body[pos..ext_end.min(body.len())])
}
/// Parse extension list (ClientHello format) looking for key_share (0x0033),
/// then find the X25519 (0x001d) entry.
fn parse_key_share_client(exts: &[u8]) -> Option<[u8; 32]> {
let mut pos = 0;
while pos + 4 <= exts.len() {
let ext_type = u16::from_be_bytes([exts[pos], exts[pos + 1]]);
let ext_len = u16::from_be_bytes([exts[pos + 2], exts[pos + 3]]) as usize;
pos += 4;
if pos + ext_len > exts.len() {
break;
}
let ext_data = &exts[pos..pos + ext_len];
if ext_type == 0x0033 {
// client_shares list: 2-byte list_len, then entries
if ext_data.len() < 2 {
return None;
}
let list_len = u16::from_be_bytes([ext_data[0], ext_data[1]]) as usize;
let mut epos = 2;
let list_end = 2 + list_len;
while epos + 4 <= list_end.min(ext_data.len()) {
let group = u16::from_be_bytes([ext_data[epos], ext_data[epos + 1]]);
let klen = u16::from_be_bytes([ext_data[epos + 2], ext_data[epos + 3]]) as usize;
epos += 4;
if epos + klen > ext_data.len() {
break;
}
if group == 0x001d && klen == 32 {
let mut key = [0u8; 32];
key.copy_from_slice(&ext_data[epos..epos + 32]);
return Some(key);
}
epos += klen;
}
}
pos += ext_len;
}
None
}
/// Extract the X25519 public key from the key_share extension of a
/// ServerHello record payload.
///
/// ServerHello key_share (type 0x0033) contains a single KeyShareEntry:
/// { 2-byte NamedGroup, 2-byte key_len, key_bytes }
fn parse_server_hello_key_share(inbound: &[u8]) -> Option<[u8; 32]> {
let records = parse_records(inbound);
let sh_payload = records
.iter()
.find(|r| r.content_type == 22)?
.payload
.clone();
let msgs = parse_handshake_messages(&sh_payload).ok()?;
let sh = msgs.iter().find(|m| m.msg_type == 2)?; // ServerHello
let body = &sh.body;
let mut pos = 0;
// legacy_version (2)
pos += 2;
// server_random (32)
pos += 32;
// session_id
if pos >= body.len() {
return None;
}
let sid_len = body[pos] as usize;
pos += 1 + sid_len;
// cipher_suite (2)
pos += 2;
// compression_method (1)
pos += 1;
// extensions_len
if pos + 2 > body.len() {
return None;
}
let ext_total = u16::from_be_bytes([body[pos], body[pos + 1]]) as usize;
pos += 2;
let ext_end = pos + ext_total;
parse_key_share_server(&body[pos..ext_end.min(body.len())])
}
/// Parse extension list (ServerHello format) for key_share (0x0033),
/// which contains a single entry: { NamedGroup(2), key_len(2), key }.
fn parse_key_share_server(exts: &[u8]) -> Option<[u8; 32]> {
let mut pos = 0;
while pos + 4 <= exts.len() {
let ext_type = u16::from_be_bytes([exts[pos], exts[pos + 1]]);
let ext_len = u16::from_be_bytes([exts[pos + 2], exts[pos + 3]]) as usize;
pos += 4;
if pos + ext_len > exts.len() {
break;
}
let ext_data = &exts[pos..pos + ext_len];
if ext_type == 0x0033 {
// Single entry: 2-byte group + 2-byte key_len + key
if ext_data.len() < 4 {
return None;
}
let group = u16::from_be_bytes([ext_data[0], ext_data[1]]);
let klen = u16::from_be_bytes([ext_data[2], ext_data[3]]) as usize;
if group == 0x001d && klen == 32 && ext_data.len() >= 4 + 32 {
let mut key = [0u8; 32];
key.copy_from_slice(&ext_data[4..36]);
return Some(key);
}
}
pos += ext_len;
}
None
}
// ---------------------------------------------------------------------------
// Certificate helpers
// ---------------------------------------------------------------------------
const OID_ECDSA_SHA256: &str = "1.2.840.10045.4.3.2";
const OID_ECDSA_SHA384: &str = "1.2.840.10045.4.3.3";
const OID_RSA_SHA256: &str = "1.2.840.113549.1.1.11";
const OID_RSA_SHA384: &str = "1.2.840.113549.1.1.12";
fn verify_cert_signature(
cert: &Certificate,
issuer_spki: &x509_cert::spki::SubjectPublicKeyInfoOwned,
) {
use der::Encode;
let tbs_der = cert.tbs_certificate.to_der().expect("encode TBS");
let sig_bytes = cert.signature.raw_bytes();
let oid = cert.signature_algorithm.oid.to_string();
match oid.as_str() {
OID_ECDSA_SHA256 => {
let pk = issuer_spki.subject_public_key.raw_bytes();
let vk = VerifyingKey::from_sec1_bytes(pk).expect("issuer not P-256");
let sig = DerSignature::try_from(sig_bytes).expect("invalid ECDSA-P256 sig");
EcVerifier::verify(&vk, &tbs_der, &sig).expect("cert ECDSA-P256 sig invalid");
}
OID_ECDSA_SHA384 => {
let pk = issuer_spki.subject_public_key.raw_bytes();
let vk = P384VerifyingKey::from_sec1_bytes(pk).expect("issuer not P-384");
let sig = P384DerSignature::try_from(sig_bytes).expect("invalid ECDSA-P384 sig");
P384Verifier::verify(&vk, &tbs_der, &sig).expect("cert ECDSA-P384 sig invalid");
}
OID_RSA_SHA256 => {
use der::Encode;
let spki_der = issuer_spki.to_der().expect("encode SPKI");
let pk = RsaPublicKey::from_public_key_der(&spki_der).expect("parse RSA key");
let vk = pkcs1v15::VerifyingKey::<Sha256>::new(pk);
let sig = pkcs1v15::Signature::try_from(sig_bytes).expect("invalid RSA sig");