aurelia 0.1.0

Embeddable service mesh for Rust distributed applications.
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
// This file is part of the Aurelia workspace.
// SPDX-FileCopyrightText: 2026 Zivatar Limited
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{UnixListener, UnixStream};
use tokio::sync::{oneshot, Mutex, RwLock};
use tokio::time::timeout;
use tokio_rustls::rustls::pki_types::{
    CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, UnixTime,
};
use tokio_rustls::rustls::server::danger::ClientCertVerifier;
use tokio_rustls::rustls::server::WebPkiClientVerifier;
use tokio_rustls::rustls::RootCertStore;

use asn1_rs::Oid;
use ring::rand::{SecureRandom, SystemRandom};
use ring::signature;
use x509_parser::oid_registry::{
    OID_EC_P256, OID_KEY_TYPE_EC_PUBLIC_KEY, OID_NIST_EC_P384, OID_PKCS1_RSAENCRYPTION,
    OID_SIG_ED25519,
};

use crate::ids::{AureliaError, ErrorId};
use crate::peering::address::DomusAddr;
use crate::peering::auth::DomusAuthConfig;
use crate::peering::config::DomusConfigAccess;

use super::backend::{AuthenticatedStream, TransportBackend};
use super::pkcs8::parse_pkcs8_auth_material;

const AUTH_VERSION: u8 = 1;
const MSG_AUTH_INIT: u8 = 1;
const MSG_AUTH_CHALLENGE: u8 = 2;
const MSG_AUTH_PROOF: u8 = 3;
const MSG_CALLBACK_INIT: u8 = 4;
const MAX_FRAME_LEN: usize = 64 * 1024;
const NONCE_LEN: usize = 32;
const PATH_MAX_LEN: usize = libc::PATH_MAX as usize;
const SOCKET_FS_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1);

pub struct SocketBackend {
    auth: RwLock<Arc<SocketAuthMaterial>>,
    config: DomusConfigAccess,
    local_path: Arc<Mutex<Option<PathBuf>>>,
    preauth_gate: Arc<super::limits::PreAuthGate>,
    pending_callbacks: Arc<Mutex<HashMap<Vec<u8>, oneshot::Sender<CallbackInfo>>>>,
    rng: SystemRandom,
}

struct SocketAuthMaterial {
    cert_der: Vec<u8>,
    signer: LocalSigner,
    verifier: Arc<dyn ClientCertVerifier>,
}

struct CallbackInfo {
    origin_path: PathBuf,
    nonce_b_cb: Vec<u8>,
}

enum LocalSigner {
    Rsa(signature::RsaKeyPair),
    EcdsaP256(signature::EcdsaKeyPair),
    EcdsaP384(signature::EcdsaKeyPair),
    Ed25519(signature::Ed25519KeyPair),
}

impl SocketBackend {
    async fn path_exists(path: &Path) -> Result<bool, AureliaError> {
        match timeout(SOCKET_FS_TIMEOUT, tokio::fs::metadata(path)).await {
            Ok(Ok(_)) => Ok(true),
            Ok(Err(err)) if err.kind() == std::io::ErrorKind::NotFound => Ok(false),
            Ok(Err(err)) => Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                err.to_string(),
            )),
            Err(_) => Err(AureliaError::new(ErrorId::PeerUnavailable)),
        }
    }

    async fn canonicalize_path(path: &Path) -> Result<PathBuf, AureliaError> {
        match timeout(SOCKET_FS_TIMEOUT, tokio::fs::canonicalize(path)).await {
            Ok(Ok(canonical)) => Ok(canonical),
            Ok(Err(err)) => Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                err.to_string(),
            )),
            Err(_) => Err(AureliaError::new(ErrorId::PeerUnavailable)),
        }
    }

    async fn remove_socket_file(path: &Path) -> Result<(), AureliaError> {
        match timeout(SOCKET_FS_TIMEOUT, tokio::fs::remove_file(path)).await {
            Ok(Ok(())) => Ok(()),
            Ok(Err(err)) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Ok(Err(err)) => Err(AureliaError::with_message(
                ErrorId::PeerUnavailable,
                err.to_string(),
            )),
            Err(_) => Err(AureliaError::new(ErrorId::PeerUnavailable)),
        }
    }

    pub fn new(
        auth: DomusAuthConfig,
        config: DomusConfigAccess,
        _runtime_handle: tokio::runtime::Handle,
    ) -> Result<Self, AureliaError> {
        let auth = parse_pkcs8_auth(auth)?;
        Ok(Self {
            auth: RwLock::new(Arc::new(auth)),
            config,
            local_path: Arc::new(Mutex::new(None)),
            preauth_gate: Arc::new(super::limits::PreAuthGate::new()),
            pending_callbacks: Arc::new(Mutex::new(HashMap::new())),
            rng: SystemRandom::new(),
        })
    }

    pub async fn reload_auth(&self, auth: DomusAuthConfig) -> Result<(), AureliaError> {
        let auth = parse_pkcs8_auth(auth)?;
        let mut guard = self.auth.write().await;
        *guard = Arc::new(auth);
        let mut pending = self.pending_callbacks.lock().await;
        pending.clear();
        Ok(())
    }

    pub(super) async fn canonicalize_socket_path(path: &Path) -> Result<PathBuf, AureliaError> {
        if !path.is_absolute() {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "socket path must be absolute",
            ));
        }
        let raw = path.to_str().ok_or_else(|| {
            AureliaError::with_message(ErrorId::ProtocolViolation, "socket path not utf8")
        })?;
        if raw.is_empty() || raw.len() > PATH_MAX_LEN {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "socket path length invalid",
            ));
        }
        if Self::path_exists(path).await? {
            let canonical = Self::canonicalize_path(path).await?;
            let canonical_str = canonical.to_str().ok_or_else(|| {
                AureliaError::with_message(ErrorId::ProtocolViolation, "socket path not utf8")
            })?;
            if canonical_str.is_empty() || canonical_str.len() > PATH_MAX_LEN {
                return Err(AureliaError::with_message(
                    ErrorId::ProtocolViolation,
                    "socket path length invalid",
                ));
            }
            return Ok(canonical);
        }
        let parent = path.parent().ok_or_else(|| {
            AureliaError::with_message(ErrorId::ProtocolViolation, "socket path missing parent")
        })?;
        let parent = Self::canonicalize_path(parent).await?;
        if parent.to_str().is_none() {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "socket path not utf8",
            ));
        }
        let file_name = path.file_name().ok_or_else(|| {
            AureliaError::with_message(ErrorId::ProtocolViolation, "socket path missing filename")
        })?;
        Ok(parent.join(file_name))
    }

    async fn local_path(&self) -> Result<PathBuf, AureliaError> {
        let guard = self.local_path.lock().await;
        guard.as_ref().cloned().ok_or_else(|| {
            AureliaError::with_message(ErrorId::PeerUnavailable, "socket backend not bound")
        })
    }

    async fn register_pending_callback(
        &self,
        nonce_a_cb: Vec<u8>,
    ) -> oneshot::Receiver<CallbackInfo> {
        let (tx, rx) = oneshot::channel();
        let mut guard = self.pending_callbacks.lock().await;
        guard.insert(nonce_a_cb, tx);
        rx
    }

    async fn clear_pending_callback(&self, nonce_a_cb: &[u8]) {
        let mut guard = self.pending_callbacks.lock().await;
        guard.remove(nonce_a_cb);
    }

    async fn fulfill_callback(&self, nonce_a_cb: &[u8], info: CallbackInfo) -> bool {
        let mut guard = self.pending_callbacks.lock().await;
        if let Some(tx) = guard.remove(nonce_a_cb) {
            let _ = tx.send(info);
            true
        } else {
            false
        }
    }

    async fn read_first_message(&self, stream: &mut UnixStream) -> Result<Vec<u8>, AureliaError> {
        read_framed(stream).await
    }

    async fn handle_callback_message(&self, payload: &[u8]) -> Result<(), AureliaError> {
        let msg = parse_callback_init(payload).await?;
        let local = self.local_path().await?;
        if msg.destination_path != local {
            return Err(AureliaError::new(ErrorId::ProtocolViolation));
        }
        let info = CallbackInfo {
            origin_path: msg.origin_path,
            nonce_b_cb: msg.nonce_b_cb,
        };
        if !self.fulfill_callback(&msg.echo_nonce_a_cb, info).await {
            return Err(AureliaError::new(ErrorId::ProtocolViolation));
        }
        Ok(())
    }

    async fn accept_inbound(
        &self,
        mut stream: UnixStream,
    ) -> Result<Option<AuthenticatedStream<UnixStream, DomusAddr>>, AureliaError> {
        let payload = self.read_first_message(&mut stream).await?;
        let msg_type = payload
            .first()
            .copied()
            .ok_or_else(|| AureliaError::new(ErrorId::ProtocolViolation))?;
        if msg_type == MSG_CALLBACK_INIT {
            self.handle_callback_message(&payload).await?;
            return Ok(None);
        }
        let handshake_timeout = self.config.snapshot().await.socket_handshake_timeout;
        match msg_type {
            MSG_AUTH_INIT => {
                let result =
                    timeout(handshake_timeout, self.handle_auth_init(stream, payload)).await;
                result
                    .map_err(|_| AureliaError::new(ErrorId::PeerUnavailable))?
                    .map(Some)
            }
            _ => Err(AureliaError::new(ErrorId::ProtocolViolation)),
        }
    }

    async fn handle_auth_init(
        &self,
        mut stream: UnixStream,
        payload: Vec<u8>,
    ) -> Result<AuthenticatedStream<UnixStream, DomusAddr>, AureliaError> {
        let msg = parse_auth_init(&payload).await?;
        let local = self.local_path().await?;
        let auth = self.auth.read().await.clone();
        if msg.destination_path != local {
            return Err(AureliaError::new(ErrorId::ProtocolViolation));
        }
        self.verify_peer_cert(&auth, &msg.cert_der, &msg.origin_path)
            .await?;
        let nonce_b = random_bytes(&self.rng, NONCE_LEN)?;
        let nonce_b_cb = random_bytes(&self.rng, NONCE_LEN)?;
        self.send_callback(&msg.origin_path, &local, &nonce_b_cb, &msg.nonce_a_cb)
            .await?;
        let signature = self.sign_message(
            &auth,
            &auth_challenge_sig(&msg.nonce_a, &nonce_b, &local, &msg.origin_path)?,
        )?;
        let challenge = AuthChallenge {
            origin_path: local.clone(),
            destination_path: msg.origin_path.clone(),
            cert_der: auth.cert_der.clone(),
            nonce_b: nonce_b.clone(),
            signature,
        };
        write_framed(&mut stream, &encode_auth_challenge(&challenge)?).await?;
        let proof_payload = read_framed(&mut stream).await?;
        let proof = parse_auth_proof(&proof_payload)?;
        if proof.echo_nonce_b_cb != nonce_b_cb {
            return Err(AureliaError::new(ErrorId::ProtocolViolation));
        }
        let proof_message = auth_proof_sig(
            &nonce_b,
            &msg.nonce_a,
            &nonce_b_cb,
            &msg.origin_path,
            &local,
        )?;
        self.verify_signature(&msg.cert_der, &proof_message, &proof.signature)?;
        Ok(AuthenticatedStream {
            stream,
            peer_addr: DomusAddr::Socket(msg.origin_path),
        })
    }

    async fn send_callback(
        &self,
        peer_path: &Path,
        local_path: &Path,
        nonce_b_cb: &[u8],
        echo_nonce_a_cb: &[u8],
    ) -> Result<(), AureliaError> {
        let timeout_duration = self.config.snapshot().await.socket_callback_timeout;
        let stream = timeout(timeout_duration, UnixStream::connect(peer_path))
            .await
            .map_err(|_| AureliaError::new(ErrorId::PeerUnavailable))?
            .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))?;
        let msg = CallbackInit {
            origin_path: local_path.to_path_buf(),
            destination_path: peer_path.to_path_buf(),
            nonce_b_cb: nonce_b_cb.to_vec(),
            echo_nonce_a_cb: echo_nonce_a_cb.to_vec(),
        };
        let mut stream = stream;
        write_framed(&mut stream, &encode_callback_init(&msg)?).await?;
        let _ = stream.shutdown().await;
        Ok(())
    }

    async fn outbound_handshake(
        &self,
        mut stream: UnixStream,
        peer_path: PathBuf,
    ) -> Result<AuthenticatedStream<UnixStream, DomusAddr>, AureliaError> {
        let auth = self.auth.read().await.clone();
        let local = self.local_path().await?;
        let nonce_a = random_bytes(&self.rng, NONCE_LEN)?;
        let nonce_a_cb = random_bytes(&self.rng, NONCE_LEN)?;
        let auth_init = AuthInit {
            origin_path: local.clone(),
            destination_path: peer_path.clone(),
            cert_der: auth.cert_der.clone(),
            nonce_a: nonce_a.clone(),
            nonce_a_cb: nonce_a_cb.clone(),
        };
        let callback_rx = self.register_pending_callback(nonce_a_cb.clone()).await;
        write_framed(&mut stream, &encode_auth_init(&auth_init)?).await?;
        let callback_timeout = self.config.snapshot().await.socket_callback_timeout;
        let callback = match timeout(callback_timeout, callback_rx).await {
            Ok(Ok(value)) => value,
            _ => {
                self.clear_pending_callback(&nonce_a_cb).await;
                return Err(AureliaError::new(ErrorId::PeerUnavailable));
            }
        };
        let challenge_payload = read_framed(&mut stream).await.map_err(|err| {
            AureliaError::with_message(err.kind, format!("read auth challenge: {err}"))
        })?;
        let challenge = parse_auth_challenge(&challenge_payload)
            .await
            .map_err(|err| {
                AureliaError::with_message(err.kind, format!("parse auth challenge: {err}"))
            })?;
        if challenge.origin_path != callback.origin_path {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "challenge origin mismatch",
            ));
        }
        if challenge.destination_path != local {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "challenge destination mismatch",
            ));
        }
        self.verify_peer_cert(&auth, &challenge.cert_der, &challenge.origin_path)
            .await
            .map_err(|err| {
                AureliaError::with_message(err.kind, format!("verify peer cert: {err}"))
            })?;
        let challenge_message = auth_challenge_sig(
            &nonce_a,
            &challenge.nonce_b,
            &challenge.origin_path,
            &challenge.destination_path,
        )
        .map_err(|err| {
            AureliaError::with_message(err.kind, format!("challenge sig input: {err}"))
        })?;
        self.verify_signature(
            &challenge.cert_der,
            &challenge_message,
            &challenge.signature,
        )
        .map_err(|err| {
            AureliaError::with_message(err.kind, format!("verify challenge signature: {err}"))
        })?;
        let proof_signature = self
            .sign_message(
                &auth,
                &auth_proof_sig(
                    &challenge.nonce_b,
                    &nonce_a,
                    &callback.nonce_b_cb,
                    &local,
                    &challenge.origin_path,
                )?,
            )
            .map_err(|err| AureliaError::with_message(err.kind, format!("sign proof: {err}")))?;
        let proof = AuthProof {
            echo_nonce_b_cb: callback.nonce_b_cb.clone(),
            signature: proof_signature,
        };
        write_framed(&mut stream, &encode_auth_proof(&proof)?)
            .await
            .map_err(|err| {
                AureliaError::with_message(err.kind, format!("send auth proof: {err}"))
            })?;
        let _ = nonce_a;
        let _ = nonce_a_cb;
        let _ = callback.nonce_b_cb;
        Ok(AuthenticatedStream {
            stream,
            peer_addr: DomusAddr::Socket(peer_path),
        })
    }

    async fn verify_peer_cert(
        &self,
        auth: &SocketAuthMaterial,
        cert_der: &[u8],
        expected_path: &Path,
    ) -> Result<(), AureliaError> {
        let cert = CertificateDer::from(cert_der.to_vec());
        auth.verifier
            .verify_client_cert(&cert, &[], UnixTime::now())
            .map_err(|_| {
                AureliaError::with_message(ErrorId::ProtocolViolation, "cert verify failed")
            })?;
        let peer_path = extract_peer_uri_san_socket(cert_der).await?;
        if peer_path != expected_path {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "cert SAN mismatch",
            ));
        }
        Ok(())
    }

    fn sign_message(
        &self,
        auth: &SocketAuthMaterial,
        message: &[u8],
    ) -> Result<Vec<u8>, AureliaError> {
        match &auth.signer {
            LocalSigner::Rsa(key) => {
                let mut sig = vec![0u8; key.public().modulus_len()];
                key.sign(&signature::RSA_PSS_SHA256, &self.rng, message, &mut sig)
                    .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
                Ok(sig)
            }
            LocalSigner::EcdsaP256(key) => {
                let sig = key
                    .sign(&self.rng, message)
                    .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
                Ok(sig.as_ref().to_vec())
            }
            LocalSigner::EcdsaP384(key) => {
                let sig = key
                    .sign(&self.rng, message)
                    .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
                Ok(sig.as_ref().to_vec())
            }
            LocalSigner::Ed25519(key) => Ok(key.sign(message).as_ref().to_vec()),
        }
    }

    fn verify_signature(
        &self,
        cert_der: &[u8],
        message: &[u8],
        signature_bytes: &[u8],
    ) -> Result<(), AureliaError> {
        let (_, cert) = x509_parser::parse_x509_certificate(cert_der)
            .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
        let spki = &cert.tbs_certificate.subject_pki;
        let algo = signature_algorithm_for_cert(spki)?;
        let public_key_bytes = spki.subject_public_key.data.as_ref();
        let public_key = signature::UnparsedPublicKey::new(algo, public_key_bytes);
        public_key.verify(message, signature_bytes).map_err(|_| {
            AureliaError::with_message(ErrorId::ProtocolViolation, "signature verify failed")
        })
    }
}

#[async_trait::async_trait]
impl TransportBackend for SocketBackend {
    type Addr = DomusAddr;
    type Listener = UnixListener;
    type Stream = UnixStream;

    async fn bind(&self, local: &Self::Addr) -> Result<Self::Listener, AureliaError> {
        let DomusAddr::Socket(path) = local else {
            return Err(AureliaError::with_message(
                ErrorId::PeerUnavailable,
                "socket backend cannot bind non-socket address",
            ));
        };
        let canonical = Self::canonicalize_socket_path(path).await?;
        {
            let mut guard = self.local_path.lock().await;
            if let Some(existing) = guard.as_ref() {
                if existing != &canonical {
                    return Err(AureliaError::new(ErrorId::ProtocolViolation));
                }
            } else {
                *guard = Some(canonical.clone());
            }
        }
        if Self::path_exists(&canonical).await? {
            Self::remove_socket_file(&canonical).await?;
        }
        UnixListener::bind(&canonical)
            .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))
    }

    async fn accept(
        &self,
        listener: &mut Self::Listener,
    ) -> Result<AuthenticatedStream<Self::Stream, Self::Addr>, AureliaError> {
        loop {
            let (mut stream, _) = listener.accept().await.map_err(|err| {
                AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string())
            })?;
            let permit = match self.preauth_gate.try_acquire(&self.config).await {
                Some(permit) => permit,
                None => {
                    let _ = stream.shutdown().await;
                    continue;
                }
            };
            if let Some(authenticated) = self.accept_inbound(stream).await? {
                drop(permit);
                return Ok(authenticated);
            }
        }
    }

    async fn dial(
        &self,
        peer: &Self::Addr,
    ) -> Result<AuthenticatedStream<Self::Stream, Self::Addr>, AureliaError> {
        let DomusAddr::Socket(path) = peer else {
            return Err(AureliaError::with_message(
                ErrorId::PeerUnavailable,
                "socket backend cannot dial non-socket address",
            ));
        };
        let peer_path = Self::canonicalize_socket_path(path).await?;
        let stream = UnixStream::connect(&peer_path)
            .await
            .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))?;
        let handshake_timeout = self.config.snapshot().await.socket_handshake_timeout;
        let result = timeout(
            handshake_timeout,
            self.outbound_handshake(stream, peer_path),
        )
        .await;
        result.map_err(|_| AureliaError::new(ErrorId::PeerUnavailable))?
    }
}

fn parse_pkcs8_auth(auth: DomusAuthConfig) -> Result<SocketAuthMaterial, AureliaError> {
    let DomusAuthConfig::Pkcs8(pkcs8) = auth;
    let material = parse_pkcs8_auth_material(pkcs8)?;
    let roots = material.roots;
    let certs = material.certs;
    let key_bytes = material.key_der;
    let mut root_store = RootCertStore::empty();
    for root in roots.iter().cloned() {
        root_store
            .add(root)
            .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
    }
    let verifier = WebPkiClientVerifier::builder(Arc::new(root_store))
        .build()
        .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
    let key_der = PrivateKeyDer::from(PrivatePkcs8KeyDer::from(key_bytes.as_slice()));
    let signer = build_local_signer(&key_der)
        .ok_or_else(|| AureliaError::new(ErrorId::ProtocolViolation))?;
    let cert = certs
        .first()
        .ok_or_else(|| AureliaError::new(ErrorId::ProtocolViolation))?
        .as_ref()
        .to_vec();
    Ok(SocketAuthMaterial {
        cert_der: cert,
        signer,
        verifier,
    })
}

fn build_local_signer(key_der: &PrivateKeyDer<'_>) -> Option<LocalSigner> {
    if let PrivateKeyDer::Pkcs8(pkcs8) = key_der {
        if let Ok(key) = signature::Ed25519KeyPair::from_pkcs8(pkcs8.secret_pkcs8_der()) {
            return Some(LocalSigner::Ed25519(key));
        }
        let rng = SystemRandom::new();
        if let Ok(key) = signature::EcdsaKeyPair::from_pkcs8(
            &signature::ECDSA_P256_SHA256_ASN1_SIGNING,
            pkcs8.secret_pkcs8_der(),
            &rng,
        ) {
            return Some(LocalSigner::EcdsaP256(key));
        }
        if let Ok(key) = signature::EcdsaKeyPair::from_pkcs8(
            &signature::ECDSA_P384_SHA384_ASN1_SIGNING,
            pkcs8.secret_pkcs8_der(),
            &rng,
        ) {
            return Some(LocalSigner::EcdsaP384(key));
        }
        if let Ok(key) = signature::RsaKeyPair::from_pkcs8(pkcs8.secret_pkcs8_der()) {
            return Some(LocalSigner::Rsa(key));
        }
    }
    if let PrivateKeyDer::Pkcs1(pkcs1) = key_der {
        if let Ok(key) = signature::RsaKeyPair::from_der(pkcs1.secret_pkcs1_der()) {
            return Some(LocalSigner::Rsa(key));
        }
    }
    None
}

async fn extract_peer_uri_san_socket(cert_der: &[u8]) -> Result<PathBuf, AureliaError> {
    let (_, cert) = x509_parser::parse_x509_certificate(cert_der)
        .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
    let san = cert
        .subject_alternative_name()
        .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
    let san = san.ok_or_else(|| AureliaError::new(ErrorId::ProtocolViolation))?;
    let mut found: Option<PathBuf> = None;
    for entry in san.value.general_names.iter() {
        if let x509_parser::extensions::GeneralName::URI(uri) = entry {
            if let Some(path) = parse_aurelia_unix_uri(uri).await? {
                if let Some(existing) = &found {
                    if existing != &path {
                        return Err(AureliaError::new(ErrorId::ProtocolViolation));
                    }
                } else {
                    found = Some(path);
                }
            }
        }
    }
    found.ok_or_else(|| AureliaError::new(ErrorId::ProtocolViolation))
}

async fn parse_aurelia_unix_uri(uri: &str) -> Result<Option<PathBuf>, AureliaError> {
    const PREFIX: &str = "aurelia+unix://";
    let Some(rest) = uri.strip_prefix(PREFIX) else {
        return Ok(None);
    };
    let path = PathBuf::from(rest);
    let canonical = SocketBackend::canonicalize_socket_path(&path).await?;
    if canonical != path {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "SAN path not canonical",
        ));
    }
    Ok(Some(path))
}

fn signature_algorithm_for_cert(
    spki: &x509_parser::x509::SubjectPublicKeyInfo<'_>,
) -> Result<&'static dyn signature::VerificationAlgorithm, AureliaError> {
    if spki.algorithm.algorithm == OID_PKCS1_RSAENCRYPTION {
        return Ok(&signature::RSA_PSS_2048_8192_SHA256);
    }
    if spki.algorithm.algorithm == OID_KEY_TYPE_EC_PUBLIC_KEY {
        let params = spki
            .algorithm
            .parameters
            .as_ref()
            .ok_or_else(|| AureliaError::new(ErrorId::ProtocolViolation))?;
        let curve_oid =
            Oid::try_from(params).map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
        if curve_oid == OID_EC_P256 {
            return Ok(&signature::ECDSA_P256_SHA256_ASN1);
        }
        if curve_oid == OID_NIST_EC_P384 {
            return Ok(&signature::ECDSA_P384_SHA384_ASN1);
        }
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    if spki.algorithm.algorithm == OID_SIG_ED25519 {
        return Ok(&signature::ED25519);
    }
    Err(AureliaError::new(ErrorId::ProtocolViolation))
}

fn random_bytes(rng: &SystemRandom, len: usize) -> Result<Vec<u8>, AureliaError> {
    let mut buf = vec![0u8; len];
    rng.fill(&mut buf)
        .map_err(|_| AureliaError::new(ErrorId::ProtocolViolation))?;
    Ok(buf)
}

fn auth_challenge_sig(
    nonce_a: &[u8],
    nonce_b: &[u8],
    origin_path: &Path,
    destination_path: &Path,
) -> Result<Vec<u8>, AureliaError> {
    if nonce_a.len() != NONCE_LEN || nonce_b.len() != NONCE_LEN {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "invalid challenge nonce length",
        ));
    }
    let origin = path_bytes(origin_path)?;
    let dest = path_bytes(destination_path)?;
    let mut data = Vec::new();
    data.extend_from_slice(nonce_a);
    data.extend_from_slice(nonce_b);
    put_u16(&mut data, origin.len() as u16);
    put_u16(&mut data, dest.len() as u16);
    data.extend_from_slice(&origin);
    data.extend_from_slice(&dest);
    Ok(data)
}

fn auth_proof_sig(
    nonce_b: &[u8],
    nonce_a: &[u8],
    nonce_b_cb: &[u8],
    origin_path: &Path,
    destination_path: &Path,
) -> Result<Vec<u8>, AureliaError> {
    if nonce_a.len() != NONCE_LEN || nonce_b.len() != NONCE_LEN || nonce_b_cb.len() != NONCE_LEN {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "invalid proof nonce length",
        ));
    }
    let origin = path_bytes(origin_path)?;
    let dest = path_bytes(destination_path)?;
    let mut data = Vec::new();
    data.extend_from_slice(nonce_b);
    data.extend_from_slice(nonce_a);
    data.extend_from_slice(nonce_b_cb);
    put_u16(&mut data, origin.len() as u16);
    put_u16(&mut data, dest.len() as u16);
    data.extend_from_slice(&origin);
    data.extend_from_slice(&dest);
    Ok(data)
}

fn path_bytes(path: &Path) -> Result<Vec<u8>, AureliaError> {
    let text = path
        .to_str()
        .ok_or_else(|| AureliaError::with_message(ErrorId::ProtocolViolation, "path not utf8"))?;
    let bytes = text.as_bytes();
    if bytes.is_empty() || bytes.len() > PATH_MAX_LEN || bytes.len() > u16::MAX as usize {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "path length out of bounds",
        ));
    }
    Ok(bytes.to_vec())
}

async fn read_framed(stream: &mut UnixStream) -> Result<Vec<u8>, AureliaError> {
    let mut len_buf = [0u8; 4];
    stream
        .read_exact(&mut len_buf)
        .await
        .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))?;
    let len = u32::from_be_bytes(len_buf) as usize;
    if len == 0 || len > MAX_FRAME_LEN {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let mut payload = vec![0u8; len];
    stream
        .read_exact(&mut payload)
        .await
        .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))?;
    Ok(payload)
}

async fn write_framed(stream: &mut UnixStream, payload: &[u8]) -> Result<(), AureliaError> {
    if payload.is_empty() || payload.len() > MAX_FRAME_LEN {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let len = (payload.len() as u32).to_be_bytes();
    stream
        .write_all(&len)
        .await
        .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))?;
    stream
        .write_all(payload)
        .await
        .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))?;
    stream
        .flush()
        .await
        .map_err(|err| AureliaError::with_message(ErrorId::PeerUnavailable, err.to_string()))?;
    Ok(())
}

struct AuthInit {
    origin_path: PathBuf,
    destination_path: PathBuf,
    cert_der: Vec<u8>,
    nonce_a: Vec<u8>,
    nonce_a_cb: Vec<u8>,
}

struct CallbackInit {
    origin_path: PathBuf,
    destination_path: PathBuf,
    nonce_b_cb: Vec<u8>,
    echo_nonce_a_cb: Vec<u8>,
}

struct AuthChallenge {
    origin_path: PathBuf,
    destination_path: PathBuf,
    cert_der: Vec<u8>,
    nonce_b: Vec<u8>,
    signature: Vec<u8>,
}

struct AuthProof {
    echo_nonce_b_cb: Vec<u8>,
    signature: Vec<u8>,
}

fn encode_auth_init(msg: &AuthInit) -> Result<Vec<u8>, AureliaError> {
    if msg.nonce_a.len() != NONCE_LEN || msg.nonce_a_cb.len() != NONCE_LEN {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin = path_bytes(&msg.origin_path)?;
    let dest = path_bytes(&msg.destination_path)?;
    let mut buf = Vec::new();
    buf.push(MSG_AUTH_INIT);
    buf.push(AUTH_VERSION);
    put_u16(&mut buf, origin.len() as u16);
    put_u16(&mut buf, dest.len() as u16);
    put_u32(&mut buf, msg.cert_der.len() as u32);
    put_u16(&mut buf, msg.nonce_a.len() as u16);
    put_u16(&mut buf, msg.nonce_a_cb.len() as u16);
    buf.extend_from_slice(&origin);
    buf.extend_from_slice(&dest);
    buf.extend_from_slice(&msg.cert_der);
    buf.extend_from_slice(&msg.nonce_a);
    buf.extend_from_slice(&msg.nonce_a_cb);
    Ok(buf)
}

async fn parse_auth_init(payload: &[u8]) -> Result<AuthInit, AureliaError> {
    let mut cursor = Cursor::new(payload);
    let msg_type = cursor.read_u8()?;
    let version = cursor.read_u8()?;
    if msg_type != MSG_AUTH_INIT || version != AUTH_VERSION {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin_len = cursor.read_u16()? as usize;
    let dest_len = cursor.read_u16()? as usize;
    let cert_len = cursor.read_u32()? as usize;
    let nonce_len = cursor.read_u16()? as usize;
    let callback_len = cursor.read_u16()? as usize;
    if origin_len == 0
        || dest_len == 0
        || origin_len > PATH_MAX_LEN
        || dest_len > PATH_MAX_LEN
        || nonce_len != NONCE_LEN
        || callback_len != NONCE_LEN
    {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin = cursor.read_bytes(origin_len)?;
    let dest = cursor.read_bytes(dest_len)?;
    let cert = cursor.read_bytes(cert_len)?;
    let nonce_a = cursor.read_bytes(nonce_len)?;
    let nonce_a_cb = cursor.read_bytes(callback_len)?;
    if cursor.has_remaining() {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin_path = parse_path(origin).await?;
    let destination_path = parse_path(dest).await?;
    Ok(AuthInit {
        origin_path,
        destination_path,
        cert_der: cert,
        nonce_a,
        nonce_a_cb,
    })
}

fn encode_callback_init(msg: &CallbackInit) -> Result<Vec<u8>, AureliaError> {
    if msg.nonce_b_cb.len() != NONCE_LEN || msg.echo_nonce_a_cb.len() != NONCE_LEN {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin = path_bytes(&msg.origin_path)?;
    let dest = path_bytes(&msg.destination_path)?;
    let mut buf = Vec::new();
    buf.push(MSG_CALLBACK_INIT);
    buf.push(AUTH_VERSION);
    put_u16(&mut buf, origin.len() as u16);
    put_u16(&mut buf, dest.len() as u16);
    put_u16(&mut buf, msg.nonce_b_cb.len() as u16);
    put_u16(&mut buf, msg.echo_nonce_a_cb.len() as u16);
    buf.extend_from_slice(&origin);
    buf.extend_from_slice(&dest);
    buf.extend_from_slice(&msg.nonce_b_cb);
    buf.extend_from_slice(&msg.echo_nonce_a_cb);
    Ok(buf)
}

async fn parse_callback_init(payload: &[u8]) -> Result<CallbackInit, AureliaError> {
    let mut cursor = Cursor::new(payload);
    let msg_type = cursor.read_u8()?;
    let version = cursor.read_u8()?;
    if msg_type != MSG_CALLBACK_INIT || version != AUTH_VERSION {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin_len = cursor.read_u16()? as usize;
    let dest_len = cursor.read_u16()? as usize;
    let nonce_len = cursor.read_u16()? as usize;
    let echo_len = cursor.read_u16()? as usize;
    if origin_len == 0
        || dest_len == 0
        || origin_len > PATH_MAX_LEN
        || dest_len > PATH_MAX_LEN
        || nonce_len != NONCE_LEN
        || echo_len != NONCE_LEN
    {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin = cursor.read_bytes(origin_len)?;
    let dest = cursor.read_bytes(dest_len)?;
    let nonce_b_cb = cursor.read_bytes(nonce_len)?;
    let echo_nonce_a_cb = cursor.read_bytes(echo_len)?;
    if cursor.has_remaining() {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin_path = parse_path(origin).await?;
    let destination_path = parse_path(dest).await?;
    Ok(CallbackInit {
        origin_path,
        destination_path,
        nonce_b_cb,
        echo_nonce_a_cb,
    })
}

fn encode_auth_challenge(msg: &AuthChallenge) -> Result<Vec<u8>, AureliaError> {
    if msg.nonce_b.len() != NONCE_LEN {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let origin = path_bytes(&msg.origin_path)?;
    let dest = path_bytes(&msg.destination_path)?;
    let mut buf = Vec::new();
    buf.push(MSG_AUTH_CHALLENGE);
    buf.push(AUTH_VERSION);
    put_u16(&mut buf, origin.len() as u16);
    put_u16(&mut buf, dest.len() as u16);
    put_u32(&mut buf, msg.cert_der.len() as u32);
    put_u16(&mut buf, msg.nonce_b.len() as u16);
    put_u32(&mut buf, msg.signature.len() as u32);
    buf.extend_from_slice(&origin);
    buf.extend_from_slice(&dest);
    buf.extend_from_slice(&msg.cert_der);
    buf.extend_from_slice(&msg.nonce_b);
    buf.extend_from_slice(&msg.signature);
    Ok(buf)
}

async fn parse_auth_challenge(payload: &[u8]) -> Result<AuthChallenge, AureliaError> {
    let mut cursor = Cursor::new(payload);
    let msg_type = cursor.read_u8()?;
    let version = cursor.read_u8()?;
    if msg_type != MSG_AUTH_CHALLENGE || version != AUTH_VERSION {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "auth_challenge header mismatch",
        ));
    }
    let origin_len = cursor.read_u16()? as usize;
    let dest_len = cursor.read_u16()? as usize;
    let cert_len = cursor.read_u32()? as usize;
    let nonce_len = cursor.read_u16()? as usize;
    let signature_len = cursor.read_u32()? as usize;
    if origin_len == 0
        || dest_len == 0
        || origin_len > PATH_MAX_LEN
        || dest_len > PATH_MAX_LEN
        || nonce_len != NONCE_LEN
    {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "auth_challenge length invalid",
        ));
    }
    let origin = cursor.read_bytes(origin_len)?;
    let dest = cursor.read_bytes(dest_len)?;
    let cert = cursor.read_bytes(cert_len)?;
    let nonce_b = cursor.read_bytes(nonce_len)?;
    let signature = cursor.read_bytes(signature_len)?;
    if cursor.has_remaining() {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "auth_challenge trailing bytes",
        ));
    }
    let origin_path = parse_path(origin).await?;
    let destination_path = parse_path(dest).await?;
    Ok(AuthChallenge {
        origin_path,
        destination_path,
        cert_der: cert,
        nonce_b,
        signature,
    })
}

fn encode_auth_proof(msg: &AuthProof) -> Result<Vec<u8>, AureliaError> {
    if msg.echo_nonce_b_cb.len() != NONCE_LEN {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let mut buf = Vec::new();
    buf.push(MSG_AUTH_PROOF);
    buf.push(AUTH_VERSION);
    put_u16(&mut buf, msg.echo_nonce_b_cb.len() as u16);
    put_u32(&mut buf, msg.signature.len() as u32);
    buf.extend_from_slice(&msg.echo_nonce_b_cb);
    buf.extend_from_slice(&msg.signature);
    Ok(buf)
}

fn parse_auth_proof(payload: &[u8]) -> Result<AuthProof, AureliaError> {
    let mut cursor = Cursor::new(payload);
    let msg_type = cursor.read_u8()?;
    let version = cursor.read_u8()?;
    if msg_type != MSG_AUTH_PROOF || version != AUTH_VERSION {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let echo_len = cursor.read_u16()? as usize;
    let signature_len = cursor.read_u32()? as usize;
    if echo_len != NONCE_LEN {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    let echo_nonce_b_cb = cursor.read_bytes(echo_len)?;
    let signature = cursor.read_bytes(signature_len)?;
    if cursor.has_remaining() {
        return Err(AureliaError::new(ErrorId::ProtocolViolation));
    }
    Ok(AuthProof {
        echo_nonce_b_cb,
        signature,
    })
}

struct Cursor<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> Cursor<'a> {
    fn new(data: &'a [u8]) -> Self {
        Self { data, pos: 0 }
    }

    fn read_u8(&mut self) -> Result<u8, AureliaError> {
        if self.pos + 1 > self.data.len() {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "cursor underrun",
            ));
        }
        let value = self.data[self.pos];
        self.pos += 1;
        Ok(value)
    }

    fn read_u16(&mut self) -> Result<u16, AureliaError> {
        if self.pos + 2 > self.data.len() {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "cursor underrun",
            ));
        }
        let value = u16::from_be_bytes([self.data[self.pos], self.data[self.pos + 1]]);
        self.pos += 2;
        Ok(value)
    }

    fn read_u32(&mut self) -> Result<u32, AureliaError> {
        if self.pos + 4 > self.data.len() {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "cursor underrun",
            ));
        }
        let value = u32::from_be_bytes([
            self.data[self.pos],
            self.data[self.pos + 1],
            self.data[self.pos + 2],
            self.data[self.pos + 3],
        ]);
        self.pos += 4;
        Ok(value)
    }

    fn read_bytes(&mut self, len: usize) -> Result<Vec<u8>, AureliaError> {
        if self.pos + len > self.data.len() {
            return Err(AureliaError::with_message(
                ErrorId::ProtocolViolation,
                "cursor underrun",
            ));
        }
        let value = self.data[self.pos..self.pos + len].to_vec();
        self.pos += len;
        Ok(value)
    }

    fn has_remaining(&self) -> bool {
        self.pos != self.data.len()
    }
}

fn put_u16(buf: &mut Vec<u8>, value: u16) {
    buf.extend_from_slice(&value.to_be_bytes());
}

fn put_u32(buf: &mut Vec<u8>, value: u32) {
    buf.extend_from_slice(&value.to_be_bytes());
}

async fn parse_path(value: Vec<u8>) -> Result<PathBuf, AureliaError> {
    if value.is_empty() || value.len() > PATH_MAX_LEN {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "path length invalid",
        ));
    }
    let text = std::str::from_utf8(&value)
        .map_err(|_| AureliaError::with_message(ErrorId::ProtocolViolation, "path not utf8"))?;
    let path = PathBuf::from(text);
    let canonical = SocketBackend::canonicalize_socket_path(&path).await?;
    if canonical != path {
        return Err(AureliaError::with_message(
            ErrorId::ProtocolViolation,
            "path not canonical",
        ));
    }
    Ok(path)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::PathBuf;
    use std::sync::Arc;
    use std::time::Duration;

    use crate::peering::address::DomusAddr;
    use crate::peering::auth::{DomusAuthConfig, Pkcs8AuthConfig, Pkcs8DerConfig};
    use crate::peering::config::{DomusConfig, DomusConfigAccess};
    use rcgen::{BasicConstraints, Certificate, CertificateParams, IsCa, SanType};

    fn build_ca() -> Certificate {
        let mut params = CertificateParams::new(Vec::new());
        params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
        Certificate::from_params(params).expect("ca cert")
    }

    fn build_domus_cert(ca: &Certificate, path: &Path) -> (Vec<u8>, Vec<u8>) {
        let mut params = CertificateParams::new(Vec::new());
        let uri = format!("aurelia+unix://{}", path.to_string_lossy());
        params.subject_alt_names.push(SanType::URI(uri));
        let cert = Certificate::from_params(params).expect("domus cert");
        let cert_der = cert.serialize_der_with_signer(ca).expect("sign cert");
        let key_der = cert.serialize_private_key_der();
        (cert_der, key_der)
    }

    fn build_auth(ca: &Certificate, path: &Path) -> DomusAuthConfig {
        let (cert_der, key_der) = build_domus_cert(ca, path);
        DomusAuthConfig::Pkcs8(Pkcs8AuthConfig::Pkcs8Der(Pkcs8DerConfig {
            ca_der: ca.serialize_der().expect("ca der"),
            cert_der,
            pkcs8_key_der: key_der,
        }))
    }

    fn temp_dir(name: &str) -> PathBuf {
        let root = workspace_root().join("tmp/peering-socket-tests");
        let dir = root.join(name);
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).expect("create temp dir");
        fs::canonicalize(&dir).expect("canonicalize temp dir")
    }

    fn workspace_root() -> PathBuf {
        let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        manifest
            .parent()
            .and_then(|dir| dir.parent())
            .and_then(|dir| dir.parent())
            .map(PathBuf::from)
            .expect("workspace root")
    }

    #[tokio::test]
    async fn socket_connect_back_success() {
        let dir = temp_dir("connect-back-success");
        let path_a = dir.join("domus-a.sock");
        let path_b = dir.join("domus-b.sock");

        let ca = build_ca();
        let auth_a = build_auth(&ca, &path_a);
        let auth_b = build_auth(&ca, &path_b);

        let config: DomusConfigAccess = DomusConfigAccess::from_config(DomusConfig::default());
        let backend_a = Arc::new(
            SocketBackend::new(auth_a, config.clone(), tokio::runtime::Handle::current())
                .expect("backend a"),
        );
        let backend_b = Arc::new(
            SocketBackend::new(auth_b, config.clone(), tokio::runtime::Handle::current())
                .expect("backend b"),
        );

        let mut listener_a = backend_a
            .bind(&DomusAddr::Socket(path_a.clone()))
            .await
            .expect("bind a");
        let mut listener_b = backend_b
            .bind(&DomusAddr::Socket(path_b.clone()))
            .await
            .expect("bind b");

        let backend_a_accept = Arc::clone(&backend_a);
        let accept_a = tokio::spawn(async move {
            let _ = backend_a_accept.accept(&mut listener_a).await;
        });
        let backend_b_accept = Arc::clone(&backend_b);
        let accept_b = tokio::spawn(async move {
            backend_b_accept
                .accept(&mut listener_b)
                .await
                .expect("accept b")
        });

        let outbound = backend_a
            .dial(&DomusAddr::Socket(path_b.clone()))
            .await
            .expect("dial");
        let addr = outbound.peer_addr;
        drop(outbound.stream);
        let inbound = accept_b.await.expect("accept task");
        let peer_addr = inbound.peer_addr;
        assert_eq!(addr, DomusAddr::Socket(path_b));
        assert_eq!(peer_addr, DomusAddr::Socket(path_a));

        accept_a.abort();
    }

    #[tokio::test]
    async fn socket_callback_timeout_fails() {
        let dir = temp_dir("callback-timeout");
        let path_a = dir.join("domus-a.sock");
        let path_b = dir.join("domus-b.sock");

        let ca = build_ca();
        let auth_a = build_auth(&ca, &path_a);
        let auth_b = build_auth(&ca, &path_b);

        let cfg = DomusConfig {
            socket_callback_timeout: Duration::from_millis(50),
            ..Default::default()
        };
        let config: DomusConfigAccess = DomusConfigAccess::from_config(cfg);
        let backend_a = Arc::new(
            SocketBackend::new(auth_a, config.clone(), tokio::runtime::Handle::current())
                .expect("backend a"),
        );
        let backend_b = Arc::new(
            SocketBackend::new(auth_b, config.clone(), tokio::runtime::Handle::current())
                .expect("backend b"),
        );

        let _listener_a = backend_a
            .bind(&DomusAddr::Socket(path_a.clone()))
            .await
            .expect("bind a");
        let mut listener_b = backend_b
            .bind(&DomusAddr::Socket(path_b.clone()))
            .await
            .expect("bind b");

        let backend_b_accept = Arc::clone(&backend_b);
        let accept_b = tokio::spawn(async move { backend_b_accept.accept(&mut listener_b).await });

        let result = backend_a.dial(&DomusAddr::Socket(path_b)).await;
        assert!(result.is_err());

        let _ = accept_b.await;
    }

    #[tokio::test]
    async fn auth_challenge_rejects_signature_length_overflow() {
        let msg = AuthChallenge {
            origin_path: PathBuf::from("/tmp/aurelia-auth-a.sock"),
            destination_path: PathBuf::from("/tmp/aurelia-auth-b.sock"),
            cert_der: vec![1, 2, 3],
            nonce_b: vec![0u8; NONCE_LEN],
            signature: vec![9; 4],
        };
        let mut payload = encode_auth_challenge(&msg).expect("encode");
        let sig_len_offset = 12;
        let bad_len = msg.signature.len() as u32 + 10;
        payload[sig_len_offset..sig_len_offset + 4].copy_from_slice(&bad_len.to_be_bytes());

        let err = match parse_auth_challenge(&payload).await {
            Ok(_) => panic!("expected error"),
            Err(err) => err,
        };
        assert_eq!(err.kind, ErrorId::ProtocolViolation);
    }

    #[test]
    fn auth_proof_rejects_signature_length_overflow() {
        let msg = AuthProof {
            echo_nonce_b_cb: vec![0u8; NONCE_LEN],
            signature: vec![3; 5],
        };
        let mut payload = encode_auth_proof(&msg).expect("encode");
        let sig_len_offset = 4;
        let bad_len = msg.signature.len() as u32 + 7;
        payload[sig_len_offset..sig_len_offset + 4].copy_from_slice(&bad_len.to_be_bytes());

        let err = match parse_auth_proof(&payload) {
            Ok(_) => panic!("expected error"),
            Err(err) => err,
        };
        assert_eq!(err.kind, ErrorId::ProtocolViolation);
    }
}