Skip to main content

second_brain_core/keys/
pairing.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use age::x25519::Recipient;
4use anyhow::{Context, Result, anyhow, bail, ensure};
5use ed25519_dalek::VerifyingKey;
6
7use super::{DeviceKeys, DeviceRecord, Epoch, IdentityTag, SignedRecord};
8use crate::sqlite_store::SqliteStore;
9
10/// Which identity a joining device held before it took its paired row.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum PriorIdentity {
13    None,
14    Same,
15    Other(IdentityTag),
16}
17
18/// On the pairing device: signs `record` as the joining device's first row
19/// in this device's identity and stores it. Pairing a device again with the
20/// same keys returns its stored first row, countersigned by this device,
21/// whatever else in the row changed since. A device this identity's stored
22/// revocations reach is refused before anything is written, even when only
23/// its revocation is stored; one that kept its machine id but not its keys
24/// needs a fresh machine id. A row the joiner held under another identity
25/// is replaced.
26pub fn admit_device(
27    store: &SqliteStore,
28    pairer: &DeviceKeys,
29    record: DeviceRecord,
30) -> Result<SignedRecord> {
31    let own = store.machine_id().to_string();
32    let joiner = record.machine_id.clone();
33    check_fields(&record)?;
34    ensure!(
35        joiner != own,
36        "the joining device has this device's machine id {own}; one of them is a copy and needs a \
37         fresh machine id"
38    );
39    ensure!(record.version == 1, "a first row has version 1");
40    let own_row = store
41        .device_record(&own)?
42        .context("this device has no signed row, so it has no identity to pair into")?;
43    ensure!(
44        own_row.current.record.identity_tag == record.identity_tag,
45        "the row names identity {}, not this device's identity",
46        record.identity_tag
47    );
48    let tag = IdentityTag::from(record.identity_tag.clone());
49    ensure!(
50        !store.revoked_by_rule(&tag, None)?.contains(&joiner),
51        "{}",
52        removed_joiner(&joiner)
53    );
54
55    match store.device_record(&joiner)? {
56        Some(stored) if stored.current.record.identity_tag == record.identity_tag => {
57            ensure!(!stored.revoked, "{}", removed_joiner(&joiner));
58            ensure!(
59                same_keys(&stored.first.record, &record),
60                "device {joiner} is already paired with other keys; a device that lost its keys \
61                 needs a fresh machine id"
62            );
63            if !stored.first.signatures.iter().any(|s| s.signer == own) {
64                let mut countersigned = stored.first.clone();
65                countersigned.countersign(&own, pairer.signing_key());
66                store.insert_first_device_record(&countersigned, &own, &pairer.verifying_key())?;
67            }
68        }
69        Some(_) => {
70            let signed = record.sign(&own, pairer.signing_key());
71            store.replace_device_record_for_new_identity(&signed, &own, &pairer.verifying_key())?;
72            store.apply_revocations(&tag)?;
73        }
74        None => {
75            let signed = record.sign(&own, pairer.signing_key());
76            store.insert_first_device_record(&signed, &own, &pairer.verifying_key())?;
77        }
78    }
79    Ok(store
80        .device_record(&joiner)?
81        .context("the admitted row is not in the store after the write")?
82        .first)
83}
84
85fn removed_joiner(joiner: &str) -> String {
86    format!(
87        "device {joiner} was removed from this identity, so it cannot rejoin it under this \
88         machine id; nothing was changed"
89    )
90}
91
92fn same_keys(a: &DeviceRecord, b: &DeviceRecord) -> bool {
93    a.age_recipient == b.age_recipient
94        && a.ssh_public_key == b.ssh_public_key
95        && a.signing_public_key == b.signing_public_key
96}
97
98/// The row is signed and spreads to every peer, and later items write its
99/// ssh fields into authorized_keys and known_hosts.
100fn check_fields(record: &DeviceRecord) -> Result<()> {
101    let token = |field: &str, value: &str| {
102        ensure!(
103            is_token(value),
104            "the joining device sent a {field} that is empty or holds spaces or control characters"
105        );
106        Ok(())
107    };
108    let line = |field: &str, value: &str| {
109        ensure!(
110            !value.chars().any(char::is_control),
111            "the joining device sent a {field} holding control characters"
112        );
113        Ok(())
114    };
115    ensure!(
116        super::valid_machine_id(&record.machine_id),
117        "the joining device sent a machine id that is not 1 to 128 of A-Z a-z 0-9 . _ -"
118    );
119    line("name", &record.name)?;
120    if let Some(login) = &record.ssh_login {
121        token("ssh login", login)?;
122    }
123    let ssh_key: Vec<&str> = record.ssh_public_key.split(' ').collect();
124    ensure!(
125        matches!(ssh_key.as_slice(), ["ssh-ed25519", body] if is_base64(body)),
126        "the joining device sent an ssh key that is not one `ssh-ed25519 <base64>` line"
127    );
128    if let Some(host_key) = &record.ssh_host_key {
129        ensure!(
130            valid_host_key(host_key),
131            "the joining device sent a host key that is not one `<type> <base64>` line"
132        );
133    }
134    record
135        .age_recipient
136        .parse::<Recipient>()
137        .map_err(|e| anyhow!("the joining device sent an invalid age recipient: {e}"))?;
138    record.signing_public_key()?;
139    Ok(())
140}
141
142/// One `<type> <base64>` public key line, as a signed row carries a host
143/// key into every peer's known_hosts.
144pub fn valid_host_key(host_key: &str) -> bool {
145    let parts: Vec<&str> = host_key.split(' ').collect();
146    matches!(parts.as_slice(), [kind, body] if is_token(kind) && is_base64(body))
147}
148
149fn is_token(text: &str) -> bool {
150    !text.is_empty() && !text.chars().any(|c| c.is_whitespace() || c.is_control())
151}
152
153fn is_base64(text: &str) -> bool {
154    !text.is_empty()
155        && text
156            .bytes()
157            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'/' | b'='))
158}
159
160/// On the joining device, before anything is written: the row `host` sent
161/// for this device carries this device's three keys and verifies under the
162/// host's key.
163pub fn check_own_paired_row(
164    own: &str,
165    device: &DeviceKeys,
166    row: &SignedRecord,
167    host: &str,
168    host_key: &VerifyingKey,
169) -> Result<()> {
170    let record = &row.record;
171    ensure!(
172        record.machine_id == own,
173        "the pairing device sent a row for machine {}, not this device ({own})",
174        record.machine_id
175    );
176    ensure!(
177        record.age_recipient == device.age_recipient().to_string()
178            && record.ssh_public_key == device.ssh_public_key()
179            && record.signing_public_key == device.verifying_key().to_bytes(),
180        "the pairing device sent a row for other keys than this device's"
181    );
182    row.verify_by(host, host_key)
183}
184
185/// On the joining device: takes the first row `host` signed for it as this
186/// device's own, after `check_own_paired_row`. Floor bookkeeping is the
187/// caller's, once the identity's other rows are stored.
188pub fn take_own_paired_row(
189    store: &SqliteStore,
190    device: &DeviceKeys,
191    row: &SignedRecord,
192    host: &str,
193    host_key: &VerifyingKey,
194) -> Result<PriorIdentity> {
195    let own = store.machine_id();
196    check_own_paired_row(own, device, row, host, host_key)?;
197    let prior = match store.device_record(own)? {
198        Some(stored) if stored.current.record.identity_tag == row.record.identity_tag => {
199            store.insert_first_device_record(row, host, host_key)?;
200            PriorIdentity::Same
201        }
202        Some(stored) => {
203            store.replace_device_record_for_new_identity(row, host, host_key)?;
204            PriorIdentity::Other(IdentityTag::from(stored.current.record.identity_tag))
205        }
206        None => {
207            store.insert_first_device_record(row, host, host_key)?;
208            PriorIdentity::None
209        }
210    };
211    Ok(prior)
212}
213
214/// On the joining device: takes the device list `host` sent over the
215/// pairing channel. The host's own rows are taken on the channel's word. Any
216/// other row needs a signature by a device already taken, or is
217/// self-signed by the identity's founder (its tag derives from an epoch's
218/// key id and that device's key) or by a member of a migrated identity,
219/// whose rows the host took under the migration rules. Rows that never get
220/// there are skipped. Returns how many other devices are stored.
221pub fn take_paired_rows(
222    store: &SqliteStore,
223    tag: &IdentityTag,
224    epochs: &[Epoch],
225    host: &str,
226    rows: &[SignedRecord],
227) -> Result<usize> {
228    let own = store.machine_id();
229    let mut by_machine: BTreeMap<&str, Vec<&SignedRecord>> = BTreeMap::new();
230    for row in rows {
231        if row.record.identity_tag == tag.as_str() {
232            by_machine
233                .entry(row.record.machine_id.as_str())
234                .or_default()
235                .push(row);
236        }
237    }
238    let mut keys = BTreeMap::new();
239    for (id, versions) in &mut by_machine {
240        versions.sort_by_key(|row| row.record.version);
241        match versions[0].record.signing_public_key() {
242            Ok(key) => {
243                keys.insert(*id, key);
244            }
245            Err(e) => tracing::debug!("skipping the paired row for {id}: {e:#}"),
246        }
247    }
248    let self_signed_ok = |id: &str| {
249        keys.get(id).is_some_and(|key| {
250            epochs.iter().any(|epoch| {
251                *tag == IdentityTag::migrated(&epoch.key_id)
252                    || *tag == IdentityTag::founded(&epoch.key_id, key)
253            })
254        })
255    };
256
257    let mut taken: BTreeSet<&str> = BTreeSet::from([own]);
258    let mut pending: Vec<&str> = by_machine.keys().copied().filter(|id| *id != own).collect();
259    pending.sort_by_key(|id| *id != host);
260    let mut stored = 0;
261    loop {
262        let before = pending.len();
263        let mut left = Vec::new();
264        for id in pending {
265            let versions = &by_machine[id];
266            let first = versions[0];
267            let signers: Vec<(&str, &VerifyingKey)> = first
268                .signatures
269                .iter()
270                .filter_map(|s| {
271                    let signer = s.signer.as_str();
272                    let vouched = if signer == id {
273                        id == host || self_signed_ok(id)
274                    } else {
275                        taken.contains(signer) || id == host
276                    };
277                    let key = keys.get(signer)?;
278                    (vouched && first.verify_by(signer, key).is_ok()).then_some((signer, key))
279                })
280                .collect();
281            if signers.is_empty() {
282                left.push(id);
283                continue;
284            }
285            match take_first(store, first, &signers) {
286                Ok(()) => {
287                    taken.insert(id);
288                    stored += 1;
289                }
290                Err(e) => {
291                    tracing::debug!("not taking the paired row for {id}: {e:#}");
292                    continue;
293                }
294            }
295            for later in &versions[1..] {
296                if let Err(e) = store.update_device_record(later) {
297                    tracing::debug!(
298                        "not taking version {} of the paired row for {id}: {e:#}",
299                        later.record.version
300                    );
301                }
302            }
303        }
304        pending = left;
305        if pending.is_empty() || pending.len() == before {
306            for id in pending {
307                tracing::debug!(
308                    "not taking the paired row for {id}: no trusted signer vouches for it"
309                );
310            }
311            return Ok(stored);
312        }
313    }
314}
315
316fn take_first(
317    store: &SqliteStore,
318    first: &SignedRecord,
319    signers: &[(&str, &VerifyingKey)],
320) -> Result<()> {
321    let Some(((signer, key), rest)) = signers.split_first() else {
322        bail!("no signer vouches for the row");
323    };
324    let held_elsewhere = store
325        .device_record(&first.record.machine_id)?
326        .is_some_and(|row| row.current.record.identity_tag != first.record.identity_tag);
327    if held_elsewhere {
328        store.replace_device_record_for_new_identity(first, signer, key)?;
329    } else {
330        store.insert_first_device_record(first, signer, key)?;
331    }
332    for (signer, key) in rest {
333        store.insert_first_device_record(first, signer, key)?;
334    }
335    Ok(())
336}
337
338/// The signing key of `machine_id` from a device list, from its first row.
339pub fn listed_signing_key(rows: &[SignedRecord], machine_id: &str) -> Result<VerifyingKey> {
340    let Some(first) = rows
341        .iter()
342        .filter(|row| row.record.machine_id == machine_id)
343        .min_by_key(|row| row.record.version)
344    else {
345        bail!("the device list has no row for {machine_id}");
346    };
347    first.record.signing_public_key()
348}
349
350#[cfg(test)]
351mod tests {
352    use super::*;
353    use crate::keys::{Revocation, found_identity};
354
355    const NOW: i64 = 1_793_600_000_000_000;
356
357    struct Side {
358        store: SqliteStore,
359        device: DeviceKeys,
360    }
361
362    fn side(id: &str) -> Side {
363        Side {
364            store: SqliteStore::in_memory(id.to_string()).unwrap(),
365            device: DeviceKeys::generate().unwrap(),
366        }
367    }
368
369    fn founded(id: &str) -> (Side, IdentityTag) {
370        let side = side(id);
371        let tag = found_identity(&side.store, &side.device, id, &[], NOW)
372            .unwrap()
373            .tag;
374        (side, tag)
375    }
376
377    fn joining_record(joiner: &Side, tag: &IdentityTag) -> DeviceRecord {
378        DeviceRecord {
379            machine_id: joiner.store.machine_id().to_string(),
380            identity_tag: tag.as_str().to_string(),
381            version: 1,
382            name: "laptop".into(),
383            age_recipient: joiner.device.age_recipient().to_string(),
384            ssh_public_key: joiner.device.ssh_public_key(),
385            signing_public_key: joiner.device.verifying_key().to_bytes(),
386            recovery_fingerprints: Vec::new(),
387            ssh_login: Some("user".into()),
388            ssh_port: Some(22),
389            ssh_host_key: None,
390        }
391    }
392
393    fn rows_of(store: &SqliteStore, tag: &IdentityTag) -> Vec<SignedRecord> {
394        let mut rows = Vec::new();
395        for row in store.device_records().unwrap() {
396            if row.current.record.identity_tag != tag.as_str() {
397                continue;
398            }
399            if row.current != row.first {
400                rows.push(row.first);
401            }
402            rows.push(row.current);
403        }
404        rows
405    }
406
407    fn epochs_of(store: &SqliteStore, tag: &IdentityTag) -> Vec<Epoch> {
408        store
409            .key_epochs(tag)
410            .unwrap()
411            .into_iter()
412            .map(|e| e.epoch)
413            .collect()
414    }
415
416    #[test]
417    fn a_joining_device_takes_the_row_its_pairer_signed_and_the_device_list() {
418        let (host, tag) = founded("desk");
419        let joiner = side("laptop");
420        let row = admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap();
421        row.verify_by("desk", &host.device.verifying_key()).unwrap();
422
423        let list = rows_of(&host.store, &tag);
424        let host_key = listed_signing_key(&list, "desk").unwrap();
425        assert_eq!(
426            take_own_paired_row(&joiner.store, &joiner.device, &row, "desk", &host_key).unwrap(),
427            PriorIdentity::None
428        );
429        let epochs = epochs_of(&host.store, &tag);
430        assert_eq!(
431            take_paired_rows(&joiner.store, &tag, &epochs, "desk", &list).unwrap(),
432            1
433        );
434
435        let own = joiner.store.device_record("laptop").unwrap().unwrap();
436        assert_eq!(own.current.record.identity_tag, tag.as_str());
437        let desk = joiner.store.device_record("desk").unwrap().unwrap();
438        desk.first.verify_self().unwrap();
439        assert_eq!(
440            desk.current,
441            host.store.device_record("desk").unwrap().unwrap().current
442        );
443    }
444
445    #[test]
446    fn pairing_the_same_device_again_returns_its_row_even_after_its_fields_changed() {
447        let (host, tag) = founded("desk");
448        let joiner = side("laptop");
449        let first = admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap();
450        let again = admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap();
451        assert_eq!(first, again);
452        let changed = DeviceRecord {
453            name: "renamed".into(),
454            recovery_fingerprints: vec!["fp".into()],
455            ssh_port: Some(2222),
456            ..joining_record(&joiner, &tag)
457        };
458        assert_eq!(
459            admit_device(&host.store, &host.device, changed).unwrap(),
460            first
461        );
462    }
463
464    #[test]
465    fn a_kept_machine_id_with_new_keys_or_a_revoked_one_is_refused() {
466        let (host, tag) = founded("desk");
467        let joiner = side("laptop");
468        admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap();
469        let reinstalled = side("laptop");
470        let err = admit_device(
471            &host.store,
472            &host.device,
473            joining_record(&reinstalled, &tag),
474        )
475        .unwrap_err();
476        assert!(format!("{err:#}").contains("fresh machine id"), "{err:#}");
477
478        host.store
479            .conn()
480            .execute("UPDATE machines SET revoked = 1 WHERE id = 'laptop'", [])
481            .unwrap();
482        let err =
483            admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap_err();
484        assert!(format!("{err:#}").contains("removed"), "{err:#}");
485    }
486
487    #[test]
488    fn a_joiner_known_only_by_its_stored_revocation_is_refused_before_any_write() {
489        let (host, tag) = founded("desk");
490        let joiner = side("laptop");
491        let revocation = Revocation {
492            identity_tag: tag.as_str().to_string(),
493            revoker: "desk".into(),
494            target: "laptop".into(),
495            keep: BTreeSet::new(),
496            created_at_micros: NOW,
497        }
498        .sign(host.device.signing_key());
499        host.store
500            .put_revocation(&revocation, &host.device.verifying_key())
501            .unwrap();
502        host.store.apply_revocations(&tag).unwrap();
503        assert!(host.store.device_record("laptop").unwrap().is_none());
504
505        let err =
506            admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap_err();
507        let text = format!("{err:#}");
508        assert!(text.contains("was removed from this identity"), "{text}");
509        assert!(
510            text.contains("cannot rejoin it under this machine id"),
511            "{text}"
512        );
513        assert!(host.store.device_record("laptop").unwrap().is_none());
514    }
515
516    #[test]
517    fn fields_that_would_break_an_ssh_file_are_refused_before_signing() {
518        let (host, tag) = founded("desk");
519        let joiner = side("laptop");
520        let key = joiner.device.ssh_public_key();
521        let bad = [
522            DeviceRecord {
523                machine_id: "lap top".into(),
524                ..joining_record(&joiner, &tag)
525            },
526            DeviceRecord {
527                machine_id: "lap/top".into(),
528                ..joining_record(&joiner, &tag)
529            },
530            DeviceRecord {
531                ssh_public_key: format!("{key}\ncommand=\"sh\" {key}"),
532                ..joining_record(&joiner, &tag)
533            },
534            DeviceRecord {
535                ssh_public_key: format!("{key} comment"),
536                ..joining_record(&joiner, &tag)
537            },
538            DeviceRecord {
539                ssh_login: Some("user\nroot".into()),
540                ..joining_record(&joiner, &tag)
541            },
542            DeviceRecord {
543                ssh_host_key: Some("ssh-ed25519 AAAA\n@cert-authority * x".into()),
544                ..joining_record(&joiner, &tag)
545            },
546            DeviceRecord {
547                name: "laptop\u{1b}[2J".into(),
548                ..joining_record(&joiner, &tag)
549            },
550        ];
551        for record in bad {
552            assert!(
553                admit_device(&host.store, &host.device, record.clone()).is_err(),
554                "{record:?}"
555            );
556        }
557        assert!(host.store.device_record("laptop").unwrap().is_none());
558        let good = DeviceRecord {
559            ssh_host_key: Some("ssh-ed25519 AAAAC3Nza+/=".into()),
560            ..joining_record(&joiner, &tag)
561        };
562        admit_device(&host.store, &host.device, good).unwrap();
563    }
564
565    #[test]
566    fn a_row_for_other_keys_or_not_signed_by_the_host_is_refused() {
567        let (host, tag) = founded("desk");
568        let joiner = side("laptop");
569        let row = admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap();
570        let impostor = side("laptop");
571        let host_key = host.device.verifying_key();
572        let err = take_own_paired_row(&impostor.store, &impostor.device, &row, "desk", &host_key)
573            .unwrap_err();
574        assert!(format!("{err:#}").contains("other keys"), "{err:#}");
575
576        let stranger = DeviceKeys::generate().unwrap();
577        assert!(
578            take_own_paired_row(
579                &joiner.store,
580                &joiner.device,
581                &row,
582                "desk",
583                &stranger.verifying_key()
584            )
585            .is_err()
586        );
587        assert!(joiner.store.device_record("laptop").unwrap().is_none());
588    }
589
590    #[test]
591    fn a_device_with_its_own_identity_moves_into_the_pairers() {
592        let (host, tag) = founded("desk");
593        let (joiner, old_tag) = founded("laptop");
594        let row = admit_device(&host.store, &host.device, joining_record(&joiner, &tag)).unwrap();
595        let host_key = host.device.verifying_key();
596        assert_eq!(
597            take_own_paired_row(&joiner.store, &joiner.device, &row, "desk", &host_key).unwrap(),
598            PriorIdentity::Other(old_tag)
599        );
600        let own = joiner.store.device_record("laptop").unwrap().unwrap();
601        assert_eq!(own.first, row);
602        assert_eq!(
603            take_own_paired_row(&joiner.store, &joiner.device, &row, "desk", &host_key).unwrap(),
604            PriorIdentity::Same,
605            "taking the same row again changes nothing"
606        );
607    }
608
609    #[test]
610    fn only_the_founder_or_a_taken_device_vouches_for_a_listed_row() {
611        let (host, tag) = founded("desk");
612        let joiner = side("laptop");
613        let phone = side("phone");
614        let ghost = side("ghost");
615        let ghost_child = side("child");
616        let outsider = DeviceKeys::generate().unwrap();
617
618        let phone_row = DeviceRecord {
619            machine_id: "phone".into(),
620            ..joining_record(&phone, &tag)
621        }
622        .sign("desk", host.device.signing_key());
623        let mut chained = DeviceRecord {
624            machine_id: "tablet".into(),
625            ..joining_record(&side("tablet"), &tag)
626        }
627        .sign("phone", phone.device.signing_key());
628        chained.countersign("nobody", outsider.signing_key());
629        let ghost_row = DeviceRecord {
630            machine_id: "ghost".into(),
631            ..joining_record(&ghost, &tag)
632        }
633        .sign("ghost", ghost.device.signing_key());
634        let child_row = DeviceRecord {
635            machine_id: "child".into(),
636            ..joining_record(&ghost_child, &tag)
637        }
638        .sign("ghost", ghost.device.signing_key());
639
640        let mut list = rows_of(&host.store, &tag);
641        list.extend([child_row, ghost_row, chained, phone_row]);
642        let epochs = epochs_of(&host.store, &tag);
643        assert_eq!(
644            take_paired_rows(&joiner.store, &tag, &epochs, "desk", &list).unwrap(),
645            3,
646            "desk (the founder), phone (signed by desk) and tablet (signed by phone)"
647        );
648        assert!(joiner.store.device_record("desk").unwrap().is_some());
649        assert!(joiner.store.device_record("phone").unwrap().is_some());
650        let tablet = joiner.store.device_record("tablet").unwrap().unwrap();
651        assert_eq!(
652            tablet
653                .first
654                .signatures
655                .iter()
656                .map(|s| s.signer.as_str())
657                .collect::<Vec<_>>(),
658            vec!["phone"]
659        );
660        assert!(joiner.store.device_record("ghost").unwrap().is_none());
661        assert!(joiner.store.device_record("child").unwrap().is_none());
662    }
663
664    #[test]
665    fn a_self_signed_founder_row_needs_the_tag_its_key_derives() {
666        let (host, tag) = founded("desk");
667        let joiner = side("laptop");
668        let list = rows_of(&host.store, &tag);
669        let unrelated = [Epoch {
670            number: 1,
671            key_id: crate::keys::IdentityKey::generate().unwrap().key_id(),
672        }];
673        assert_eq!(
674            take_paired_rows(&joiner.store, &tag, &unrelated, "laptop-host", &list).unwrap(),
675            0
676        );
677        let epochs = epochs_of(&host.store, &tag);
678        assert_eq!(
679            take_paired_rows(&joiner.store, &tag, &epochs, "laptop-host", &list).unwrap(),
680            1
681        );
682    }
683}