kc-cli 0.1.0

Read and write macOS keychain files without the Security framework
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
//! Table indexes: the sorted lookup structures in the trailing part of a table.
//!
//! Not described by the dtformats specification, and *not* opaque. Two things
//! make it impossible to treat this region as bytes to be copied:
//!
//! * Every offset in it is relative to the start of the *table*, so inserting a
//!   record moves the index region and invalidates all of them. macOS then
//!   follows a stale offset into the middle of a record and reports
//!   `errSecNoSuchAttr`.
//! * It holds one entry per record. A record with no index entry is not
//!   findable through the Security framework.
//!
//! ```text
//! region  := size, count, count x index offset
//! index   := size, id, kind, attribute count, attribute ids,
//!            entry count, entry offsets, record numbers, entries
//! entry   := payload size (excluding itself), key values
//! value   := four raw bytes for an integer attribute,
//!            else length prefix and bytes padded to four
//! ```
//!
//! Entries are sorted by key; the record-number array is in the same order, so
//! together they map a sorted key to a record. `tests/keychain_index.rs` parses
//! every index region in keychains written by macOS, re-serializes them byte for
//! byte, and checks that rebuilding an index from scratch reproduces macOS's
//! ordering.

use std::cmp::Ordering;

use crate::error::{Error, Result};
use crate::format::Value;
use crate::schema::{AttributeFormat, Relation};

/// One key value inside an index entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IndexValue {
    /// An integer attribute: four raw bytes, no length prefix.
    Integer(u32),
    /// Anything else: length-prefixed bytes, padded to a 4-byte boundary.
    Bytes(Vec<u8>),
}

impl IndexValue {
    /// The key form of an attribute value, given the attribute's format.
    pub fn from_value(value: Option<&Value>, format: AttributeFormat) -> Self {
        match (value, format) {
            (None, AttributeFormat::Sint32 | AttributeFormat::Uint32) => Self::Integer(0),
            (None, _) => Self::Bytes(Vec::new()),
            (Some(Value::Uint32(number)), _) => Self::Integer(*number),
            (Some(Value::Sint32(number)), _) => Self::Integer(*number as u32),
            (Some(Value::Date(bytes) | Value::String(bytes) | Value::Blob(bytes)), _) => {
                Self::Bytes(bytes.clone())
            }
        }
    }

    fn encoded_len(&self) -> usize {
        match self {
            Self::Integer(_) => 4,
            Self::Bytes(bytes) => pad4(4 + bytes.len()),
        }
    }

    fn write(&self, out: &mut Vec<u8>) {
        match self {
            Self::Integer(number) => out.extend_from_slice(&number.to_be_bytes()),
            Self::Bytes(bytes) => {
                out.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
                out.extend_from_slice(bytes);
                out.resize(pad4(out.len()), 0);
            }
        }
    }
}

impl Ord for IndexValue {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            (Self::Integer(left), Self::Integer(right)) => left.cmp(right),
            (Self::Bytes(left), Self::Bytes(right)) => left.cmp(right),
            // Mixed formats never occur within one index.
            (Self::Integer(_), Self::Bytes(_)) => Ordering::Less,
            (Self::Bytes(_), Self::Integer(_)) => Ordering::Greater,
        }
    }
}

impl PartialOrd for IndexValue {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

/// One indexed record: its key, and the record it points at.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexEntry {
    pub record_number: u32,
    pub key: Vec<IndexValue>,
}

impl IndexEntry {
    /// Size as stored, which excludes the size word itself.
    fn payload_len(&self) -> usize {
        self.key.iter().map(IndexValue::encoded_len).sum()
    }

    fn encoded_len(&self) -> usize {
        4 + self.payload_len()
    }
}

/// One index over one or more attributes of a relation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Index {
    /// Matches `IndexID` in the schema's index table.
    pub id: u32,
    /// Matches `IndexType`: `1` for the relation's unique index, `0` otherwise.
    pub kind: u32,
    /// The attributes indexed, by id.
    pub attribute_ids: Vec<u32>,
    /// Entries, sorted by key.
    pub entries: Vec<IndexEntry>,
}

impl Index {
    /// Words before the entry section: size, id, kind, count, ids.
    fn header_len(&self) -> usize {
        4 * (4 + self.attribute_ids.len())
    }

    fn encoded_len(&self) -> usize {
        self.header_len()
            + 4                                   // entry count
            + 8 * self.entries.len()              // offsets, then record numbers
            + self.entries.iter().map(IndexEntry::encoded_len).sum::<usize>()
    }

    /// Insert in sorted position, keeping the entry and record-number arrays in
    /// step.
    fn insert(&mut self, entry: IndexEntry) {
        let at = self
            .entries
            .iter()
            .position(|existing| existing.key > entry.key)
            .unwrap_or(self.entries.len());
        self.entries.insert(at, entry);
    }
}

/// The index region of one table.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexBlob {
    pub indexes: Vec<Index>,
}

impl IndexBlob {
    /// Parse the region.
    ///
    /// `table_offset` is where the region sits within its table, which the stored
    /// offsets are measured against. `relation` supplies the attribute formats
    /// needed to tell an integer key from a length-prefixed one; without it the
    /// entries cannot be decoded.
    pub fn parse(data: &[u8], table_offset: usize, relation: Option<&Relation>) -> Result<Self> {
        let mut reader = Reader { data, at: 0 };
        let size = reader.u32()? as usize;
        if size != data.len() {
            return Err(Error::format(format!(
                "index region claims {size} bytes but has {}",
                data.len()
            )));
        }
        let count = reader.u32()? as usize;
        if count > 64 {
            return Err(Error::format(format!(
                "index region claims {count} indexes"
            )));
        }

        let mut offsets = Vec::with_capacity(count);
        for _ in 0..count {
            offsets.push(reader.u32()? as usize);
        }

        let mut indexes = Vec::with_capacity(count);
        for offset in offsets {
            let start = offset
                .checked_sub(table_offset)
                .ok_or_else(|| Error::format("index offset points before its table"))?;
            indexes.push(parse_index(data, start, table_offset, relation)?);
        }
        Ok(Self { indexes })
    }

    /// Serialize, recomputing every offset from `table_offset`.
    pub fn to_bytes(&self, table_offset: usize) -> Vec<u8> {
        let header_len = 8 + 4 * self.indexes.len();
        let mut out = Vec::with_capacity(self.encoded_len());

        out.extend_from_slice(&(self.encoded_len() as u32).to_be_bytes());
        out.extend_from_slice(&(self.indexes.len() as u32).to_be_bytes());
        let mut position = header_len;
        for index in &self.indexes {
            out.extend_from_slice(&((table_offset + position) as u32).to_be_bytes());
            position += index.encoded_len();
        }

        let mut position = header_len;
        for index in &self.indexes {
            write_index(&mut out, index, table_offset, position);
            position += index.encoded_len();
        }
        out
    }

    pub fn encoded_len(&self) -> usize {
        8 + 4 * self.indexes.len() + self.indexes.iter().map(Index::encoded_len).sum::<usize>()
    }

    /// Index a record in every index of the table.
    ///
    /// `key` maps an attribute id to that attribute's key value.
    pub fn insert_record(&mut self, record_number: u32, key: impl Fn(u32) -> IndexValue) {
        self.insert_record_where(record_number, key, |_| true);
    }

    /// Index a record only in the indexes `indexable` accepts.
    ///
    /// It is called with an index's attribute ids, and answers whether the
    /// record belongs in that index — macOS leaves a record out of an index when
    /// it has no value for an indexed attribute.
    pub fn insert_record_where(
        &mut self,
        record_number: u32,
        key: impl Fn(u32) -> IndexValue,
        indexable: impl Fn(&[u32]) -> bool,
    ) {
        for index in self
            .indexes
            .iter_mut()
            .filter(|index| indexable(&index.attribute_ids))
        {
            let values = index.attribute_ids.iter().map(|id| key(*id)).collect();
            index.insert(IndexEntry {
                record_number,
                key: values,
            });
        }
    }

    /// Drop entries for a record, and renumber the entries that referred to
    /// later records.
    pub fn remove_record(&mut self, record_number: u32) {
        for index in &mut self.indexes {
            index
                .entries
                .retain(|entry| entry.record_number != record_number);
        }
    }

    pub fn entry_count(&self) -> usize {
        self.indexes.iter().map(|index| index.entries.len()).sum()
    }

    /// Record numbers this region indexes, in key order, for the given index.
    pub fn record_numbers(&self, index_id: u32) -> Option<Vec<u32>> {
        self.indexes
            .iter()
            .find(|index| index.id == index_id)
            .map(|index| {
                index
                    .entries
                    .iter()
                    .map(|entry| entry.record_number)
                    .collect()
            })
    }
}

fn parse_index(
    data: &[u8],
    start: usize,
    table_offset: usize,
    relation: Option<&Relation>,
) -> Result<Index> {
    let mut reader = Reader { data, at: start };
    let size = reader.u32()? as usize;
    let id = reader.u32()?;
    let kind = reader.u32()?;
    let attribute_count = reader.u32()? as usize;
    if attribute_count > 32 {
        return Err(Error::format(format!(
            "index claims {attribute_count} attributes"
        )));
    }
    let mut attribute_ids = Vec::with_capacity(attribute_count);
    for _ in 0..attribute_count {
        attribute_ids.push(reader.u32()?);
    }

    let formats: Vec<AttributeFormat> = attribute_ids
        .iter()
        .map(|id| format_of(relation, *id))
        .collect();

    let entry_count = reader.u32()? as usize;
    let mut entry_offsets = Vec::with_capacity(entry_count);
    for _ in 0..entry_count {
        entry_offsets.push(reader.u32()? as usize);
    }
    let mut record_numbers = Vec::with_capacity(entry_count);
    for _ in 0..entry_count {
        record_numbers.push(reader.u32()?);
    }

    let mut entries = Vec::with_capacity(entry_count);
    for (offset, record_number) in entry_offsets.into_iter().zip(record_numbers) {
        let at = offset
            .checked_sub(table_offset)
            .ok_or_else(|| Error::format("index entry offset points before its table"))?;
        entries.push(parse_entry(data, at, record_number, &formats)?);
    }

    let index = Index {
        id,
        kind,
        attribute_ids,
        entries,
    };
    if index.encoded_len() != size {
        return Err(Error::format(format!(
            "index {id} claims {size} bytes but parses as {}",
            index.encoded_len()
        )));
    }
    Ok(index)
}

/// The format of an attribute, defaulting to a length-prefixed blob when the
/// relation is unknown. Getting this wrong misreads every key in the index, so
/// the caller is expected to pass the relation.
fn format_of(relation: Option<&Relation>, attribute_id: u32) -> AttributeFormat {
    relation
        .and_then(|relation| {
            relation
                .attributes
                .iter()
                .find(|attribute| attribute.id == attribute_id)
        })
        .map(|attribute| attribute.format)
        .unwrap_or(AttributeFormat::Blob)
}

fn parse_entry(
    data: &[u8],
    at: usize,
    record_number: u32,
    formats: &[AttributeFormat],
) -> Result<IndexEntry> {
    let mut reader = Reader { data, at };
    let payload = reader.u32()? as usize;
    let mut key = Vec::with_capacity(formats.len());
    for format in formats {
        key.push(match format {
            AttributeFormat::Sint32 | AttributeFormat::Uint32 => IndexValue::Integer(reader.u32()?),
            _ => IndexValue::Bytes(reader.value()?),
        });
    }
    let entry = IndexEntry { record_number, key };
    if entry.payload_len() != payload {
        return Err(Error::format(format!(
            "index entry claims {payload} bytes but parses as {}",
            entry.payload_len()
        )));
    }
    Ok(entry)
}

fn write_index(out: &mut Vec<u8>, index: &Index, table_offset: usize, position: usize) {
    out.extend_from_slice(&(index.encoded_len() as u32).to_be_bytes());
    out.extend_from_slice(&index.id.to_be_bytes());
    out.extend_from_slice(&index.kind.to_be_bytes());
    out.extend_from_slice(&(index.attribute_ids.len() as u32).to_be_bytes());
    for id in &index.attribute_ids {
        out.extend_from_slice(&id.to_be_bytes());
    }

    out.extend_from_slice(&(index.entries.len() as u32).to_be_bytes());
    let mut entry_position = position + index.header_len() + 4 + 8 * index.entries.len();
    for entry in &index.entries {
        out.extend_from_slice(&((table_offset + entry_position) as u32).to_be_bytes());
        entry_position += entry.encoded_len();
    }
    for entry in &index.entries {
        out.extend_from_slice(&entry.record_number.to_be_bytes());
    }
    for entry in &index.entries {
        out.extend_from_slice(&(entry.payload_len() as u32).to_be_bytes());
        for value in &entry.key {
            value.write(out);
        }
    }
}

struct Reader<'a> {
    data: &'a [u8],
    at: usize,
}

impl Reader<'_> {
    fn u32(&mut self) -> Result<u32> {
        let bytes = self
            .data
            .get(self.at..self.at + 4)
            .ok_or_else(|| Error::format("index region ends mid-word"))?;
        self.at += 4;
        Ok(u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }

    fn value(&mut self) -> Result<Vec<u8>> {
        let len = self.u32()? as usize;
        let bytes = self
            .data
            .get(self.at..self.at + len)
            .ok_or_else(|| Error::format("index value runs past the region"))?
            .to_vec();
        self.at += pad4(4 + len) - 4;
        Ok(bytes)
    }
}

fn pad4(len: usize) -> usize {
    (len + 3) & !3
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{AttributeDef, RecordType};

    fn bytes(text: &str) -> IndexValue {
        IndexValue::Bytes(text.as_bytes().to_vec())
    }

    fn sample() -> IndexBlob {
        IndexBlob {
            indexes: vec![
                Index {
                    id: 0,
                    kind: 1,
                    attribute_ids: vec![u32::from_be_bytes(*b"acct"), u32::from_be_bytes(*b"svce")],
                    entries: vec![
                        IndexEntry {
                            record_number: 0,
                            key: vec![bytes("alice"), bytes("myservice")],
                        },
                        IndexEntry {
                            record_number: 1,
                            key: vec![bytes("carol"), bytes("other")],
                        },
                    ],
                },
                Index {
                    id: 3,
                    kind: 0,
                    attribute_ids: vec![u32::from_be_bytes(*b"port")],
                    entries: vec![
                        IndexEntry {
                            record_number: 1,
                            key: vec![IndexValue::Integer(80)],
                        },
                        IndexEntry {
                            record_number: 0,
                            key: vec![IndexValue::Integer(8080)],
                        },
                    ],
                },
            ],
        }
    }

    fn relation() -> Relation {
        Relation {
            record_type: RecordType::GENERIC_PASSWORD,
            name: None,
            attributes: vec![
                AttributeDef {
                    id: u32::from_be_bytes(*b"acct"),
                    name: "acct".into(),
                    format: AttributeFormat::Blob,
                },
                AttributeDef {
                    id: u32::from_be_bytes(*b"svce"),
                    name: "svce".into(),
                    format: AttributeFormat::Blob,
                },
                AttributeDef {
                    id: u32::from_be_bytes(*b"port"),
                    name: "port".into(),
                    format: AttributeFormat::Uint32,
                },
            ],
        }
    }

    #[test]
    fn round_trips_at_any_table_offset() {
        let blob = sample();
        for table_offset in [0, 32, 492, 4096] {
            let encoded = blob.to_bytes(table_offset);
            assert_eq!(encoded.len(), blob.encoded_len());
            assert_eq!(
                IndexBlob::parse(&encoded, table_offset, Some(&relation())).unwrap(),
                blob
            );
        }
    }

    #[test]
    fn offsets_follow_the_regions_position_in_the_table() {
        let blob = sample();
        let low = blob.to_bytes(100);
        let high = blob.to_bytes(200);
        assert_eq!(low.len(), high.len(), "only the offsets differ");
        assert_ne!(low, high, "a moved region must renumber its offsets");
    }

    #[test]
    fn integer_keys_are_stored_raw_and_blob_keys_length_prefixed() {
        let blob = IndexBlob {
            indexes: vec![Index {
                id: 0,
                kind: 1,
                attribute_ids: vec![u32::from_be_bytes(*b"port"), u32::from_be_bytes(*b"acct")],
                entries: vec![IndexEntry {
                    record_number: 7,
                    key: vec![IndexValue::Integer(8080), bytes("bob")],
                }],
            }],
        };
        // 4 raw bytes for the integer, 4 + 4 (padded from 3) for the blob.
        assert_eq!(blob.indexes[0].entries[0].payload_len(), 4 + 8);

        let encoded = blob.to_bytes(0);
        assert_eq!(
            IndexBlob::parse(&encoded, 0, Some(&relation())).unwrap(),
            blob
        );
    }

    #[test]
    fn empty_indexes_have_no_entry_arrays() {
        let blob = IndexBlob {
            indexes: vec![Index {
                id: 0,
                kind: 1,
                attribute_ids: vec![u32::from_be_bytes(*b"acct")],
                entries: Vec::new(),
            }],
        };
        // region header 8 + one offset 4 + index header 20 + entry count 4
        assert_eq!(blob.encoded_len(), 36);
        let encoded = blob.to_bytes(32);
        assert_eq!(
            IndexBlob::parse(&encoded, 32, Some(&relation())).unwrap(),
            blob
        );
    }

    #[test]
    fn insert_keeps_keys_sorted_and_record_numbers_aligned() {
        let mut blob = IndexBlob {
            indexes: vec![Index {
                id: 0,
                kind: 1,
                attribute_ids: vec![u32::from_be_bytes(*b"acct")],
                entries: Vec::new(),
            }],
        };

        for (number, account) in [(0u32, "carol"), (1, "alice"), (2, "bob")] {
            blob.insert_record(number, |_| bytes(account));
        }

        let entries = &blob.indexes[0].entries;
        assert_eq!(
            entries
                .iter()
                .map(|entry| entry.key[0].clone())
                .collect::<Vec<_>>(),
            vec![bytes("alice"), bytes("bob"), bytes("carol")]
        );
        // The record numbers travel with their keys, not with insertion order.
        assert_eq!(blob.record_numbers(0).unwrap(), vec![1, 2, 0]);
    }

    #[test]
    fn remove_drops_only_that_records_entries() {
        let mut blob = sample();
        assert_eq!(blob.entry_count(), 4);
        blob.remove_record(0);
        assert_eq!(blob.entry_count(), 2);
        assert!(
            blob.indexes
                .iter()
                .all(|index| index.entries.iter().all(|entry| entry.record_number != 0))
        );
    }

    #[test]
    fn absent_values_index_as_empty_or_zero() {
        assert_eq!(
            IndexValue::from_value(None, AttributeFormat::Blob),
            IndexValue::Bytes(Vec::new())
        );
        assert_eq!(
            IndexValue::from_value(None, AttributeFormat::Uint32),
            IndexValue::Integer(0)
        );
        assert_eq!(
            IndexValue::from_value(Some(&Value::Uint32(5)), AttributeFormat::Uint32),
            IndexValue::Integer(5)
        );
        assert_eq!(
            IndexValue::from_value(Some(&Value::Blob(b"x".to_vec())), AttributeFormat::Blob),
            IndexValue::Bytes(b"x".to_vec())
        );
    }

    #[test]
    fn malformed_regions_are_rejected() {
        assert!(IndexBlob::parse(&[], 0, None).is_err());
        let mut encoded = sample().to_bytes(0);
        encoded[3] = 0xff;
        assert!(IndexBlob::parse(&encoded, 0, Some(&relation())).is_err());
    }
}