WXK
2025-01-21 8f1a91a8ec98e430cfe4357bda099d495917198e
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
 
#include <inttypes.h>
#include <string.h>
 
#include "sysinit/sysinit.h"
#include "nimble_syscfg.h"
#include "host/ble_hs.h"
#include "ble_store_config_priv.h"
#include "os/panchip_mempool.h"
#if MYNEWT_VAL_BLE_STORE_CONFIG_PERSIST
#include "kv_store_api.h"
#endif
 
#if SMP_MULTI_ADDR_MODE_EN
#include "host/ble_hs_id.h"
#endif
 
#define BLE_STORE_LOG_EN     0
 
#ifdef IP_101x
 
struct ble_store_value_sec *ble_store_config_our_secs;
int ble_store_config_num_our_secs;
 
struct ble_store_value_sec *ble_store_config_peer_secs;
int ble_store_config_num_peer_secs;
 
struct ble_store_value_cccd *ble_store_config_cccds;
int ble_store_config_num_cccds;
 
#else
struct ble_store_value_sec
    ble_store_config_our_secs[MYNEWT_VAL(BLE_STORE_MAX_BONDS)];
int ble_store_config_num_our_secs;
 
struct ble_store_value_sec
    ble_store_config_peer_secs[MYNEWT_VAL(BLE_STORE_MAX_BONDS)];
int ble_store_config_num_peer_secs;
 
struct ble_store_value_cccd
    ble_store_config_cccds[MYNEWT_VAL(BLE_STORE_MAX_CCCDS)];
int ble_store_config_num_cccds;
#endif
 
#if SMP_MULTI_ADDR_MODE_EN
struct ble_store_value_sec *ble_store_config_get_our_secs(uint8_t id)
{
    return &ble_store_config_our_secs[id];
}
 
struct ble_store_value_sec *ble_store_config_get_peer_secs(uint8_t id)
{
    return &ble_store_config_peer_secs[id];
}
 
struct ble_store_value_cccd *ble_store_config_get_cccds(uint8_t id)
{
    return &ble_store_config_cccds[id];
}
 
int  ble_store_config_get_our_secs_num(void)
{
    return ble_store_config_num_our_secs;
}
 
int ble_store_config_get_peer_secs_num(void)
{
    return ble_store_config_num_peer_secs;
}
 
int ble_store_config_get_cccds_num(void)
{
    return ble_store_config_num_cccds;
}
#endif
 
/*****************************************************************************
 * $sec                                                                      *
 *****************************************************************************/
#if BLE_STORE_LOG_EN
void
ble_hs_log_flat_buf_priv(const void *data, int len)
{
    const uint8_t *u8ptr;
    int i;
 
    u8ptr = data;
    for (i = 0; i < len; i++) {
        printf( "%02x", u8ptr[i]);
    }
}
#endif
 
static void
ble_store_config_print_value_sec(const struct ble_store_value_sec *sec)
{
#if BLE_STORE_LOG_EN
    if (sec->ltk_present) {
        printf( "ediv=%u rand=%llu authenticated=%d ltk= ",
                       sec->ediv, sec->rand_num, sec->authenticated);
        ble_hs_log_flat_buf_priv(sec->ltk, 16);
        printf(" ");
    }
    if (sec->irk_present) {
        printf("irk= ");
        ble_hs_log_flat_buf_priv(sec->irk, 16);
        printf(" ");
    }
    if (sec->csrk_present) {
        printf( "csrk= ");
        ble_hs_log_flat_buf_priv(sec->csrk, 16);
        printf(" ");
    }
 
    printf( "\n");
#endif
}
 
static void
ble_store_config_print_key_sec(const struct ble_store_key_sec *key_sec)
{
    if (ble_addr_cmp(&key_sec->peer_addr, BLE_ADDR_ANY)) {
        BLE_HS_LOG(DEBUG, "peer_addr_type=%d peer_addr=",
                       key_sec->peer_addr.type);
        ble_hs_log_flat_buf(key_sec->peer_addr.val, 6);
        BLE_HS_LOG(DEBUG, " ");
    }
    if (key_sec->ediv_rand_present) {
        BLE_HS_LOG(DEBUG, "ediv=0x%02x rand=0x%llx ",
                       key_sec->ediv, key_sec->rand_num);
    }
}
 
static int
ble_store_config_find_sec(const struct ble_store_key_sec *key_sec,
                          const struct ble_store_value_sec *value_secs,
                          int num_value_secs)
{
    const struct ble_store_value_sec *cur;
    int skipped;
    int i;
 
    skipped = 0;
 
    for (i = 0; i < num_value_secs; i++) {
        cur = value_secs + i;
 
        if (ble_addr_cmp(&key_sec->peer_addr, BLE_ADDR_ANY)) {
        #if SMP_MULTI_ADDR_MODE_EN
            ble_addr_t own_addr;
            if(key_sec->is_own_addr_valid == 0xAA){
                own_addr = key_sec->own_addr;
            }
            else{
                own_addr.type = ble_hs_id_get_used_addr_type();
                int rc = ble_hs_id_copy_addr(own_addr.type, own_addr.val, NULL);
                if(rc != 0){
                    return -1;
                }
            }
 
            if (ble_addr_cmp(&cur->peer_addr, &key_sec->peer_addr) ||
                ble_addr_cmp(&cur->own_addr, &own_addr)) {
                continue;
            }
        #else
            if (ble_addr_cmp(&cur->peer_addr, &key_sec->peer_addr)) {
                continue;
            }
        #endif
        }
 
        if (key_sec->ediv_rand_present) {
            if (cur->ediv != key_sec->ediv) {
                continue;
            }
 
            if (cur->rand_num != key_sec->rand_num) {
                continue;
            }
        }
 
        if (key_sec->idx > skipped) {
            skipped++;
            continue;
        }
 
        return i;
    }
 
    return -1;
}
 
static int
ble_store_config_read_our_sec(const struct ble_store_key_sec *key_sec,
                              struct ble_store_value_sec *value_sec)
{
    int idx;
 
    idx = ble_store_config_find_sec(key_sec, ble_store_config_our_secs,
                                    ble_store_config_num_our_secs);
    if (idx == -1) {
        return BLE_HS_ENOENT;
    }
 
    *value_sec = ble_store_config_our_secs[idx];
    return 0;
}
 
 
static int
ble_store_config_write_our_sec(const struct ble_store_value_sec *value_sec)
{
    struct ble_store_key_sec key_sec;
    int idx;
    int rc;
 
    BLE_HS_LOG(DEBUG, "persisting our sec; ");
    ble_store_config_print_value_sec(value_sec);
 
    ble_store_key_from_value_sec(&key_sec, value_sec);
    idx = ble_store_config_find_sec(&key_sec, ble_store_config_our_secs,
                                    ble_store_config_num_our_secs);
    if (idx == -1) {
        if (ble_store_config_num_our_secs >= MYNEWT_VAL(BLE_STORE_MAX_BONDS)) {
            BLE_HS_LOG(DEBUG, "error persisting our sec; too many entries "
                              "(%d)\n", ble_store_config_num_our_secs);
            return BLE_HS_ESTORE_CAP;
        }
 
        idx = ble_store_config_num_our_secs;
        ble_store_config_num_our_secs++;
    }
 
    ble_store_config_our_secs[idx] = *value_sec;
 
    rc = ble_store_config_persist_our_secs();
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}
 
static int
ble_store_config_delete_obj(void *values, int value_size, int idx,
                            int *num_values)
{
    uint8_t *dst;
    uint8_t *src;
    int move_count;
 
    (*num_values)--;
    if (idx < *num_values) {
        dst = values;
        dst += idx * value_size;
        src = dst + value_size;
 
        move_count = *num_values - idx;
        memmove(dst, src, move_count * value_size);
    }
 
    return 0;
}
 
static int
ble_store_config_delete_sec(const struct ble_store_key_sec *key_sec,
                            struct ble_store_value_sec *value_secs,
                            int *num_value_secs)
{
    int idx;
    int rc;
 
    idx = ble_store_config_find_sec(key_sec, value_secs, *num_value_secs);
    if (idx == -1) {
        return BLE_HS_ENOENT;
    }
 
    rc = ble_store_config_delete_obj(value_secs, sizeof *value_secs, idx,
                                  num_value_secs);
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}
 
static int
ble_store_config_delete_our_sec(const struct ble_store_key_sec *key_sec)
{
    int rc;
 
    rc = ble_store_config_delete_sec(key_sec, ble_store_config_our_secs,
                                     &ble_store_config_num_our_secs);
    if (rc != 0) {
        return rc;
    }
 
    rc = ble_store_config_persist_our_secs();
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}
 
static int
ble_store_config_delete_peer_sec(const struct ble_store_key_sec *key_sec)
{
    int rc;
 
    rc = ble_store_config_delete_sec(key_sec, ble_store_config_peer_secs,
                                  &ble_store_config_num_peer_secs);
    if (rc != 0) {
        return rc;
    }
 
    rc = ble_store_config_persist_peer_secs();
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}
 
static int
ble_store_config_read_peer_sec(const struct ble_store_key_sec *key_sec,
                               struct ble_store_value_sec *value_sec)
{
    int idx;
 
    idx = ble_store_config_find_sec(key_sec, ble_store_config_peer_secs,
                             ble_store_config_num_peer_secs);
    if (idx == -1) {
        return BLE_HS_ENOENT;
    }
 
    *value_sec = ble_store_config_peer_secs[idx];
    return 0;
}
 
static int
ble_store_config_write_peer_sec(const struct ble_store_value_sec *value_sec)
{
    struct ble_store_key_sec key_sec;
    int idx;
    int rc;
 
    BLE_HS_LOG(DEBUG, "persisting peer sec; ");
    ble_store_config_print_value_sec(value_sec);
 
    ble_store_key_from_value_sec(&key_sec, value_sec);
    idx = ble_store_config_find_sec(&key_sec, ble_store_config_peer_secs,
                                 ble_store_config_num_peer_secs);
    if (idx == -1) {
        if (ble_store_config_num_peer_secs >= MYNEWT_VAL(BLE_STORE_MAX_BONDS)) {
            BLE_HS_LOG(DEBUG, "error persisting peer sec; too many entries "
                             "(%d)\n", ble_store_config_num_peer_secs);
            return BLE_HS_ESTORE_CAP;
        }
 
        idx = ble_store_config_num_peer_secs;
        ble_store_config_num_peer_secs++;
    }
 
    ble_store_config_peer_secs[idx] = *value_sec;
 
    rc = ble_store_config_persist_peer_secs();
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}
 
/*****************************************************************************
 * $cccd                                                                     *
 *****************************************************************************/
 
static int
ble_store_config_find_cccd(const struct ble_store_key_cccd *key)
{
    struct ble_store_value_cccd *cccd;
    int skipped;
    int i;
 
    skipped = 0;
    for (i = 0; i < ble_store_config_num_cccds; i++) {
        cccd = ble_store_config_cccds + i;
 
        if (ble_addr_cmp(&key->peer_addr, BLE_ADDR_ANY)) {
        #if SMP_MULTI_ADDR_MODE_EN
            ble_addr_t own_addr;
            if(key->is_own_addr_valid == 0xAA){
                own_addr = key->own_addr;
            }
            else{
                own_addr.type = ble_hs_id_get_used_addr_type();
                int rc = ble_hs_id_copy_addr(own_addr.type, own_addr.val, NULL);
                if(rc != 0){
                    return -1;
                }
            }
 
            if (ble_addr_cmp(&cccd->peer_addr, &key->peer_addr) ||
                ble_addr_cmp(&cccd->own_addr, &own_addr)) {
                continue;
            }
        #else
            if (ble_addr_cmp(&cccd->peer_addr, &key->peer_addr)) {
                continue;
            }
        #endif
        }
 
        if (key->chr_val_handle != 0) {
            if (cccd->chr_val_handle != key->chr_val_handle) {
                continue;
            }
        }
 
        if (key->idx > skipped) {
            skipped++;
            continue;
        }
 
        return i;
    }
 
    return -1;
}
 
static int
ble_store_config_delete_cccd(const struct ble_store_key_cccd *key_cccd)
{
    int idx;
    int rc;
 
    idx = ble_store_config_find_cccd(key_cccd);
    if (idx == -1) {
        return BLE_HS_ENOENT;
    }
 
    rc = ble_store_config_delete_obj(ble_store_config_cccds,
                                     sizeof *ble_store_config_cccds,
                                     idx,
                                     &ble_store_config_num_cccds);
    if (rc != 0) {
        return rc;
    }
 
    rc = ble_store_config_persist_cccds();
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}
 
static int
ble_store_config_read_cccd(const struct ble_store_key_cccd *key_cccd,
                           struct ble_store_value_cccd *value_cccd)
{
    int idx;
 
    idx = ble_store_config_find_cccd(key_cccd);
    if (idx == -1) {
        return BLE_HS_ENOENT;
    }
 
    *value_cccd = ble_store_config_cccds[idx];
    return 0;
}
 
static int
ble_store_config_write_cccd(const struct ble_store_value_cccd *value_cccd)
{
    struct ble_store_key_cccd key_cccd;
    int idx;
    int rc;
 
    ble_store_key_from_value_cccd(&key_cccd, value_cccd);
    idx = ble_store_config_find_cccd(&key_cccd);
    if (idx == -1) {
        if (ble_store_config_num_cccds >= MYNEWT_VAL(BLE_STORE_MAX_CCCDS)) {
        #if BLE_STORE_LOG_EN
            printf("error persisting cccd; too many entries (%d)\n", ble_store_config_num_cccds);
        #endif
            return BLE_HS_ESTORE_CAP;
        }
 
        idx = ble_store_config_num_cccds;
        ble_store_config_num_cccds++;
    }
 
    ble_store_config_cccds[idx] = *value_cccd;
 
    rc = ble_store_config_persist_cccds();
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}
 
/*****************************************************************************
 * $api                                                                      *
 *****************************************************************************/
 
/**
 * Searches the database for an object matching the specified criteria.
 *
 * @return                      0 if a key was found; else BLE_HS_ENOENT.
 */
int
ble_store_config_read(int obj_type, const union ble_store_key *key,
                      union ble_store_value *value)
{
    int rc;
 
    switch (obj_type) {
    case BLE_STORE_OBJ_TYPE_PEER_SEC:
        /* An encryption procedure (bonding) is being attempted.  The nimble
         * stack is asking us to look in our key database for a long-term key
         * corresponding to the specified ediv and random number.
         *
         * Perform a key lookup and populate the context object with the
         * result.  The nimble stack will use this key if this function returns
         * success.
         */
        BLE_HS_LOG(DEBUG, "looking up peer sec; ");
        ble_store_config_print_key_sec(&key->sec);
        BLE_HS_LOG(DEBUG, "\n");
        rc = ble_store_config_read_peer_sec(&key->sec, &value->sec);
        return rc;
 
    case BLE_STORE_OBJ_TYPE_OUR_SEC:
        BLE_HS_LOG(DEBUG, "looking up our sec; ");
        ble_store_config_print_key_sec(&key->sec);
        BLE_HS_LOG(DEBUG, "\n");
        rc = ble_store_config_read_our_sec(&key->sec, &value->sec);
        return rc;
 
    case BLE_STORE_OBJ_TYPE_CCCD:
        rc = ble_store_config_read_cccd(&key->cccd, &value->cccd);
        return rc;
 
    default:
        return BLE_HS_ENOTSUP;
    }
}
 
/**
 * Adds the specified object to the database.
 *
 * @return                      0 on success;
 *                              BLE_HS_ESTORE_CAP if the database is full.
 */
int
ble_store_config_write(int obj_type, const union ble_store_value *val)
{
    int rc;
 
    switch (obj_type) {
    case BLE_STORE_OBJ_TYPE_PEER_SEC:
        rc = ble_store_config_write_peer_sec(&val->sec);
        return rc;
 
    case BLE_STORE_OBJ_TYPE_OUR_SEC:
        rc = ble_store_config_write_our_sec(&val->sec);
        return rc;
 
    case BLE_STORE_OBJ_TYPE_CCCD:
        rc = ble_store_config_write_cccd(&val->cccd);
        return rc;
 
    default:
        return BLE_HS_ENOTSUP;
    }
}
 
int
ble_store_config_delete(int obj_type, const union ble_store_key *key)
{
    int rc;
 
    switch (obj_type) {
    case BLE_STORE_OBJ_TYPE_PEER_SEC:
        rc = ble_store_config_delete_peer_sec(&key->sec);
        return rc;
 
    case BLE_STORE_OBJ_TYPE_OUR_SEC:
        rc = ble_store_config_delete_our_sec(&key->sec);
        return rc;
 
    case BLE_STORE_OBJ_TYPE_CCCD:
        rc = ble_store_config_delete_cccd(&key->cccd);
        return rc;
 
    default:
        return BLE_HS_ENOTSUP;
    }
}
 
void
ble_store_config_init(void)
{
    /* Ensure this function only gets called by sysinit. */
#ifdef IP_101x
    ble_store_config_our_secs = panchip_mem_get((MYNEWT_VAL(BLE_STORE_MAX_BONDS)) * sizeof(struct ble_store_value_sec), 8);
    ble_store_config_peer_secs = panchip_mem_get((MYNEWT_VAL(BLE_STORE_MAX_BONDS)) * sizeof(struct ble_store_value_sec), 8);
    ble_store_config_cccds = panchip_mem_get((MYNEWT_VAL(BLE_STORE_MAX_CCCDS)) * sizeof(struct ble_store_value_cccd), 4);
#endif
    
    SYSINIT_ASSERT_ACTIVE();
 
#if MYNEWT_VAL_BLE_STORE_CONFIG_PERSIST
    /* kv initialization */
    extern void kv_init(void);
    kv_init();
#endif
    
    ble_hs_cfg.store_read_cb = ble_store_config_read;
    ble_hs_cfg.store_write_cb = ble_store_config_write;
    ble_hs_cfg.store_delete_cb = ble_store_config_delete;
 
    /* Re-initialize BSS values in case of unit tests. */
    ble_store_config_num_our_secs = 0;
    ble_store_config_num_peer_secs = 0;
    ble_store_config_num_cccds = 0;
 
    ble_store_config_conf_init();
}
 
 
struct find_replace_addr {
    bool found_addr;
    ble_addr_t* src_addr;
    ble_addr_t* target_addr;
};
 
extern uint8_t ble_hs_misc_peer_addr_type_to_id(uint8_t peer_addr_type);
 
static int
ble_store_util_iter_replace_mac(int obj_type,
                          union ble_store_value *val,
                          void *arg)
{
    int rc;
 
    struct find_replace_addr* addrs = arg;
 
    if(val->cccd.peer_addr.type == 1) /*random address, maybe rpa addr*/
    {
        if (ble_addr_cmp(addrs->src_addr, &val->cccd.peer_addr) == 0) {
            memcpy(&val->cccd.peer_addr, addrs->target_addr, sizeof(ble_addr_t));
            val->cccd.peer_addr.type = ble_hs_misc_peer_addr_type_to_id(val->cccd.peer_addr.type);
            rc = ble_store_config_write(BLE_STORE_OBJ_TYPE_CCCD, val);
            addrs->found_addr = true;
            if (rc != 0) {
                return rc;
            }
        }
    }
 
    return 0;
}
 
int ble_store_iterate_replace_cccd(ble_addr_t* src_addr, ble_addr_t* target_addr)
{
    int rc;
    struct find_replace_addr addrs;
    
    addrs.found_addr = false;
    addrs.src_addr = src_addr;
    addrs.target_addr = target_addr;
 
    rc = ble_store_iterate(BLE_STORE_OBJ_TYPE_CCCD,
                           ble_store_util_iter_replace_mac,
                           &addrs);
    
    if(addrs.found_addr)
    {
        union ble_store_key key;
 
        memset(&key, 0, sizeof key);
        key.sec.peer_addr = *src_addr;
 
        rc = ble_store_util_delete_all(BLE_STORE_OBJ_TYPE_CCCD, &key);
    }
    
    if (rc != 0) {
        return rc;
    }
 
    return 0;
}