1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
43
44
45
46
47
48
49
50
51
52
53
54
55
56
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
141
142
143
144
145
146
147
148
149
150
151
152
153
160
161
162
163
164
165
166
167
168
169
170
177
178
179
180
181
182
183
184
185
186
187
195
196
197
198
199
200
201
202
203
204
205
206
207
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
243
244
248
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
484
485
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
534
535
536
537
538
539
541
542
543
544
545
573
574
578
579
580
581
585
586
587
588
589
590
591
592
593
595
596
597
598
603
609
610
611
612
613
614
615
616
621
622
623
626
627
630
631
632
636
637
638
639
641
642
643
644
650
657
658
659
660
661
662
663
668
669
670
673
674
675
676
677
678
679
682
683
684
685
686
687
688
689
690
692
693
694
695
696
697
698
699
700
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
811
812
813
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
843
844
845
849
850
854
855
859
860
863
864
880
883
884
887
888
889
890
891
892
893
894
895
896
897
898
899
903
904
905
906
907
908
914
915
927
928
929
930
931
932
933
934
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
984
985
988
989
990
991
992
993
994
995
997
998
999
1003
1004
1005
1008
1009
1010
1017
1018
1019
1020
1021
1022
1023
1024
1027
1028
1029
1030
1031
1032
1033
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1101
1102
1103
1104
1105
1106
1116
1117
1119
1120
1121
1122
1123
1124
1125
1126
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
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
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1211
1212
1213
1216
1217
1218
1229
1230
1231
1236
1237
1238
1239
1240
1241
1242
1243
1244
1247
1248
1249
1254
1255
1256
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
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
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1373
1374
1375
1376
1377
1378
1379
1380
1382
1383
1384
1385
1386
1389
1390
1391
1392
1393
1394
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1426
1430
1431
1437
1438
1439
1440
1441
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1458
1459
1460
1461
1462
1463
1464
1467
1468
1469
1470
1471
1472
1473
1474
1475
1480
1481
1482
1483
1488
1489
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1516
1517
1518
1519
1522
1523
1524
1525
1526
1527
1528
1534
1535
1536
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1563
1564
1565
1568
1569
1570
1571
1572
1573
1574
1575
1580
1581
1582
1583
1584
1585
1586
1589
1590
1593
1594
1595
1596
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1652
1653
1654
1657
1658
1659
1660
1661
1662
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1706
1707
1708
1711
1712
1713
1714
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1843
1844
1845
1846
1847
1848
1849
1854
1855
1863
1864
1865
1866
1867
1868
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1956
1957
1958
1959
1960
1961
1962
1963
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1992
1995
1996
1997
1998
2000
2001
2002
2004
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2036
2037
2038
2039
2042
2043
2044
2045
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2105
2106
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2125
2126
2127
2131
2132
2133
2139
2140
2141
2161
2162
2163
2166
2167
2174
2175
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2222
2230
2231
2232
2233
2234
2235
2236
2243
2244
2245
2246
2249
2250
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2305
2306
2307
2308
2309
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2352
2353
2354
2355
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2379
2380
2381
2382
2383
2384
2387
2388
2389
2390
2394
2395
2396
2397
2398
2399
2400
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2457
2458
2459
2460
2461
2462
2463
2469
2470
2471
2472
2473
2474
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2487
2495
2496
2497
2498
2499
2500
2501
2508
2509
2510
2513
2514
2515
2516
2517
2518
2519
2520
2524
2525
2526
2527
2528
2532
2542
2543
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2581
2582
2583
2584
2585
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2639
2640
2641
2642
2643
2650
2651
2652
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2716
2719
2720
2721
2722
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2752
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2907
2908
2909
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2992
2993
2994
2995
3011
3012
3013
3015
3016
3030
3031
3032
3033
3042
3043
3046
3047
3048
3054
3055
3056
3057
3058
3059
3060
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3116
3117
3118
3119
3120
3121
3122
3125
3126
3127
3128
3131
3132
3133
3134
3135
3136
3137
3141
3142
3143
3144
3150
3151
3152
3153
3154
3155
3158
3159
3160
3161
3162
3163
3164
3165
3176
3177
3178
3181
3182
3183
3184
3190
3191
3194
3195
3196
3200
3201
3205
3206
3207
3208
3209
3210
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3300
3301
3306
3307
3308
3309
3315
3316
3317
3318
3322
3323
3324
3326
3335
3340
3341
3342
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3366
3367
3368
3369
3383
3384
3385
3386
3387
3388
3389
3393
3396
3397
3398
3399
3400
3409
3410
3411
3412
3413
3414
3415
3416
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3483
3487
3490
3491
3492
3493
3496
3497
3498
3499
3500
3501
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3584
3585
3586
3596
3597
3603
3604
3605
3606
3607
3608
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3677
3678
3680
3681
3682
3686
3687
3688
3689
3690
3691
3692
3703
3704
3705
3706
3707
3708
3709
3712
3713
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3793
3794
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3818
3819
3820
3821
3829
3830
3831
3832
3833
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3866
3867
3868
3869
3870
3871
3872
3873
3874
3879
3880
3881
3882
3883
3887
3888
3889
3890
3891
3894
3895
3896
3901
3902
3905
3906
3907
3908
3909
3910
3911
3912
3915
3916
3917
3920
3921
3930
3931
3937
3938
3939
3942
3943
3944
3945
3956
3957
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3975
3976
3977
3978
3979
3980
3981
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4005
4007
4008
4009
4010
4011
4012
4013
4016
4017
4018
4022
4023
4024
4025
4026
4029
4030
4031
4032
4033
4034
4035
4036
4041
4042
4043
4044
4045
4046
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4081
4082
4083
4086
4087
4088
4089
4090
4091
4092
4095
4102
4105
4106
4107
4108
4109
4123
4124
4125
4126
4127
4128
4129
4132
4133
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4170
4171
4172
4173
4174
4175
4176
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4210
4211
4212
4213
4214
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4277
4278
4279
4280
4281
4282
4283
4288
4289
4290
4291
4292
4295
4297
4298
4301
4302
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4333
4334
4335
4336
4337
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4414
4415
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4440
4441
4442
4450
4451
4452
4453
4458
4459
4460
4461
4462
4463
4464
4465
4466
4470
4471
4472
4473
4474
4479
4480
4488
4489
4490
4491
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4529
4530
4533
4534
4535
4536
4539
4540
4541
4542
4543
4544
4545
4546
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4572
4573
4574
4575
4576
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4629
4630
4631
4632
4633
4634
4635
4636
4641
4647
4648
4649
4655
4656
4657
4658
4659
4660
4661
4666
4667
4668
4669
4670
4671
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4716
4717
4718
4719
4722
4723
4728
4731
4732
4733
4734
4735
4736
4737
4741
4742
4743
4746
4747
4748
4750
4751
4752
4753
4754
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4817
4818
4821
4822
4823
4824
4825
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4900
4901
4902
4903
4904
4905
4906
4908
4909
4910
4912
4913
4914
4918
4919
4922
4923
4924
4936
4937
4938
4944
4948
4949
4950
4951
4952
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4970
4976
4977
4978
4979
4980
4981
4998
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5025
5026
5027
5030
5031
5032
5033
5044
5045
5046
5048
5049
5050
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5084
5085
5086
5087
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5166
5167
5168
5169
5172
5173
5174
5175
5176
5177
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5206
5207
5208
5209
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5248
5249
5250
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5276
5277
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5295
5296
5297
5298
5301
5302
5303
5304
5305
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5424
5425
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5454
5455
5456
5457
5458
5459
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5518
5519
5520
5521
5526
5527
5528
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5588
5589
5590
5591
5592
5593
5594
5595
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5691
5692
5693
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5831
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5899
5900
5901
5902
5905
5906
5907
5908
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
6001
6002
6003
6004
6005
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6096
6097
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6261
6262
6265
6266
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6379
6380
6381
6382
/* ... */
/* ... */
#include <stdarg.h>
#include <string.h>
#include "stack/bt_types.h"
#include "device/controller.h"
#include "stack/hcimsgs.h"
#include "stack/btu.h"
#include "btm_int.h"
#include "l2c_int.h"
#include "osi/fixed_queue.h"
#include "osi/alarm.h"
#include "stack/btm_ble_api.h"11 includes
#if (BT_USE_TRACES == TRUE && BT_TRACE_VERBOSE == FALSE)
#include <stdio.h>/* ... */
#endif
#if BLE_INCLUDED == TRUE
#include "gatt_int.h"
#endif
#define BTM_SEC_MAX_COLLISION_DELAY (5000)
#ifdef APPL_AUTH_WRITE_EXCEPTION
BOOLEAN (APPL_AUTH_WRITE_EXCEPTION)(BD_ADDR bd_addr);
#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
static tBTM_SEC_SERV_REC *btm_sec_find_next_serv (tBTM_SEC_SERV_REC *p_cur);
static tBTM_SEC_SERV_REC *btm_sec_find_mx_serv (UINT8 is_originator, UINT16 psm,
UINT32 mx_proto_id,
UINT32 mx_chan_id);
static tBTM_STATUS btm_sec_execute_procedure (tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_sec_start_get_name (tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_sec_start_authentication (tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_sec_start_encryption (tBTM_SEC_DEV_REC *p_dev_rec);
static void btm_sec_collision_timeout (TIMER_LIST_ENT *p_tle);
static void btm_restore_mode(void);
static void btm_sec_pairing_timeout (TIMER_LIST_ENT *p_tle);
static tBTM_STATUS btm_sec_dd_create_conn (tBTM_SEC_DEV_REC *p_dev_rec);
static void btm_sec_change_pairing_state (tBTM_PAIRING_STATE new_state);/* ... */
#endif
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
static char *btm_pair_state_descr (tBTM_PAIRING_STATE state);
#endif
#if (SMP_INCLUDED == TRUE)
static void btm_sec_check_pending_reqs(void);
static BOOLEAN btm_sec_queue_mx_request (BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_orig,
UINT32 mx_proto_id, UINT32 mx_chan_id,
tBTM_SEC_CALLBACK *p_callback, void *p_ref_data);
static void btm_sec_bond_cancel_complete (void);
static void btm_send_link_key_notif (tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_sec_check_prefetch_pin (tBTM_SEC_DEV_REC *p_dev_rec);
static UINT8 btm_sec_start_authorization (tBTM_SEC_DEV_REC *p_dev_rec);/* ... */
#endif
BOOLEAN btm_sec_are_all_trusted(UINT32 p_mask[]);
#if (SMP_INCLUDED == TRUE)
static tBTM_STATUS btm_sec_send_hci_disconnect (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 reason, UINT16 conn_handle);
#endif
UINT8 btm_sec_start_role_switch (tBTM_SEC_DEV_REC *p_dev_rec);
tBTM_SEC_DEV_REC *btm_sec_find_dev_by_sec_state (UINT8 state);
static BOOLEAN btm_sec_set_security_level ( CONNECTION_TYPE conn_type, const char *p_name, UINT8 service_id,
UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
UINT32 mx_chan_id);
#if (SMP_INCLUDED == TRUE)
static BOOLEAN btm_dev_authenticated(tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_dev_encrypted(tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_dev_authorized(tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_serv_trusted(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_SEC_SERV_REC *p_serv_rec);
static BOOLEAN btm_sec_is_serv_level0 (UINT16 psm);
static UINT16 btm_sec_set_serv_level4_flags (UINT16 cur_security, BOOLEAN is_originator);
static BOOLEAN btm_sec_queue_encrypt_request (BD_ADDR bd_addr, tBT_TRANSPORT transport,
tBTM_SEC_CALLBACK *p_callback, void *p_ref_data);
static void btm_sec_check_pending_enc_req (tBTM_SEC_DEV_REC *p_dev_rec, tBT_TRANSPORT transport,
UINT8 encr_enable);
static BOOLEAN btm_sec_use_smp_br_chnl(tBTM_SEC_DEV_REC *p_dev_rec);
static BOOLEAN btm_sec_is_master(tBTM_SEC_DEV_REC *p_dev_rec);/* ... */
#endif
static const BOOLEAN btm_sec_io_map [BTM_IO_CAP_MAX][BTM_IO_CAP_MAX] = {
{FALSE, FALSE, TRUE, FALSE},
{FALSE, TRUE, TRUE, FALSE},
{TRUE, TRUE, TRUE, FALSE},
{FALSE, FALSE, FALSE, FALSE}
}{...};
/* ... */
#if (SMP_INCLUDED == TRUE)
static BOOLEAN btm_dev_authenticated (tBTM_SEC_DEV_REC *p_dev_rec)
{
if (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED) {
return (TRUE);
}{...}
return (FALSE);
}{ ... }
/* ... */
static BOOLEAN btm_dev_encrypted (tBTM_SEC_DEV_REC *p_dev_rec)
{
if (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED) {
return (TRUE);
}{...}
return (FALSE);
}{ ... }
/* ... */
static BOOLEAN btm_dev_authorized (tBTM_SEC_DEV_REC *p_dev_rec)
{
if (p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED) {
return (TRUE);
}{...}
return (FALSE);
}{ ... }
/* ... */
static BOOLEAN btm_dev_16_digit_authenticated(tBTM_SEC_DEV_REC *p_dev_rec)
{
if (p_dev_rec->sec_flags & BTM_SEC_16_DIGIT_PIN_AUTHED) {
return (TRUE);
}{...}
return (FALSE);
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static BOOLEAN btm_serv_trusted(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_SEC_SERV_REC *p_serv_rec)
{
if (BTM_SEC_IS_SERVICE_TRUSTED(p_dev_rec->trusted_mask, p_serv_rec->service_id)) {
return (TRUE);
}{...}
return (FALSE);
}{ ... }
/* ... */#endif
/* ... */
BOOLEAN BTM_SecRegister(tBTM_APPL_INFO *p_cb_info)
{
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
BT_OCTET16 temp_value = {0};
#endif
BTM_TRACE_EVENT("%s application registered\n", __func__);
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
BTM_TRACE_DEBUG("%s p_cb_info->p_le_callback == 0x%p\n", __func__, p_cb_info->p_le_callback);
if (p_cb_info->p_le_callback) {
BTM_TRACE_EVENT("%s SMP_Register( btm_proc_smp_cback )\n", __func__);
SMP_Register(btm_proc_smp_cback);
if (memcmp(btm_cb.devcb.id_keys.ir, &temp_value, sizeof(BT_OCTET16)) == 0) {
btm_ble_reset_id();
}{...}
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
else {
btm_ble_add_default_entry_to_resolving_list();
}{...}
#endif/* ... */
}{...} else {
BTM_TRACE_WARNING("%s p_cb_info->p_le_callback == NULL\n", __func__);
}{...}
#endif/* ... */
btm_cb.api = *p_cb_info;
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
BTM_TRACE_DEBUG("%s btm_cb.api.p_le_callback = 0x%p\n", __func__, btm_cb.api.p_le_callback);
#endif
BTM_TRACE_EVENT("%s application registered\n", __func__);
return (TRUE);
}{ ... }
/* ... */
BOOLEAN BTM_SecRegisterLinkKeyNotificationCallback (tBTM_LINK_KEY_CALLBACK *p_callback)
{
btm_cb.api.p_link_key_callback = p_callback;
return TRUE;
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE) || (CLASSIC_BT_INCLUDED == TRUE)
BOOLEAN BTM_SecAddRmtNameNotifyCallback (tBTM_RMT_NAME_CALLBACK *p_callback)
{
int i;
for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
if (btm_cb.p_rmt_name_callback[i] == NULL) {
btm_cb.p_rmt_name_callback[i] = p_callback;
return (TRUE);
}{...}
}{...}
return (FALSE);
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE) || (CLASSIC_BT_INCLUDED == TRUE)
BOOLEAN BTM_SecDeleteRmtNameNotifyCallback (tBTM_RMT_NAME_CALLBACK *p_callback)
{
int i;
for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
if (btm_cb.p_rmt_name_callback[i] == p_callback) {
btm_cb.p_rmt_name_callback[i] = NULL;
return (TRUE);
}{...}
}{...}
return (FALSE);
}{ ... }
/* ... */#endif
/* ... */
BOOLEAN BTM_GetSecurityFlags (BD_ADDR bd_addr, UINT8 *p_sec_flags)
{
tBTM_SEC_DEV_REC *p_dev_rec;
if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
*p_sec_flags = (UINT8) p_dev_rec->sec_flags;
return (TRUE);
}{...}
BTM_TRACE_ERROR ("BTM_GetSecurityFlags false");
return (FALSE);
}{ ... }
/* ... */
BOOLEAN BTM_GetSecurityFlagsByTransport (BD_ADDR bd_addr, UINT8 *p_sec_flags,
tBT_TRANSPORT transport)
{
tBTM_SEC_DEV_REC *p_dev_rec;
if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
if (transport == BT_TRANSPORT_BR_EDR) {
*p_sec_flags = (UINT8) p_dev_rec->sec_flags;
}{...} else {
*p_sec_flags = (UINT8) (p_dev_rec->sec_flags >> 8);
}{...}
return (TRUE);
}{...}
BTM_TRACE_ERROR ("BTM_GetSecurityFlags false\n");
return (FALSE);
}{ ... }
#if (CLASSIC_BT_INCLUDED == TRUE)
/* ... */
void BTM_SetPinType (UINT8 pin_type, PIN_CODE pin_code, UINT8 pin_code_len)
{
BTM_TRACE_API ("BTM_SetPinType: pin type %d [variable-0, fixed-1], code %s, length %d\n",
pin_type, (char *) pin_code, pin_code_len);
if ( (btm_cb.cfg.pin_type != pin_type)
&& controller_get_interface()->get_is_ready() ) {
btsnd_hcic_write_pin_type (pin_type);
}{...}
btm_cb.cfg.pin_type = pin_type;
btm_cb.cfg.pin_code_len = pin_code_len;
memcpy (btm_cb.cfg.pin_code, pin_code, pin_code_len);
}{...}
/* ... */#endif
/* ... */
void BTM_SetPairableMode (BOOLEAN allow_pairing, BOOLEAN connect_only_paired)
{
BTM_TRACE_API ("BTM_SetPairableMode() allow_pairing: %u connect_only_paired: %u\n", allow_pairing, connect_only_paired);
btm_cb.pairing_disabled = !allow_pairing;
btm_cb.connect_only_paired = connect_only_paired;
}{ ... }
/* ... */
void BTM_SetSecureConnectionsOnly (BOOLEAN secure_connections_only_mode)
{
BTM_TRACE_API("%s: Mode : %u\n", __FUNCTION__,
secure_connections_only_mode);
btm_cb.devcb.secure_connections_only = secure_connections_only_mode;
btm_cb.security_mode = BTM_SEC_MODE_SC;
}{ ... }
#define BTM_NO_AVAIL_SEC_SERVICES ((UINT16) 0xffff)
/* ... */
BOOLEAN BTM_SetSecurityLevel (BOOLEAN is_originator, const char *p_name, UINT8 service_id,
UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
UINT32 mx_chan_id)
{
#if (L2CAP_UCD_INCLUDED == TRUE)
CONNECTION_TYPE conn_type;
if (is_originator) {
conn_type = CONN_ORIENT_ORIG;
}{...} else {
conn_type = CONN_ORIENT_TERM;
}{...}
return (btm_sec_set_security_level (conn_type, p_name, service_id,
sec_level, psm, mx_proto_id, mx_chan_id));/* ... */
#else
return (btm_sec_set_security_level (is_originator, p_name, service_id,
sec_level, psm, mx_proto_id, mx_chan_id));/* ... */
#endif
}{ ... }
/* ... */
static BOOLEAN btm_sec_set_security_level (CONNECTION_TYPE conn_type, const char *p_name, UINT8 service_id,
UINT16 sec_level, UINT16 psm, UINT32 mx_proto_id,
UINT32 mx_chan_id)
{
#if (SMP_INCLUDED == TRUE)
tBTM_SEC_SERV_REC *p_srec;
UINT16 index;
UINT16 first_unused_record = BTM_NO_AVAIL_SEC_SERVICES;
BOOLEAN record_allocated = FALSE;
BOOLEAN is_originator;
#if (L2CAP_UCD_INCLUDED == TRUE)
BOOLEAN is_ucd;
if (conn_type & CONNECTION_TYPE_ORIG_MASK) {
is_originator = TRUE;
}{...} else {
is_originator = FALSE;
}{...}
if (conn_type & CONNECTION_TYPE_CONNLESS_MASK ) {
is_ucd = TRUE;
}{...} else {
is_ucd = FALSE;
}{...}
#else/* ... */
is_originator = conn_type;
#endif
BTM_TRACE_API("%s : sec: 0x%x\n", __func__, sec_level);
/* ... */
p_srec = &btm_cb.sec_serv_rec[0];
for (index = 0; index < BTM_SEC_MAX_SERVICE_RECORDS; index++, p_srec++) {
if (p_srec->security_flags & BTM_SEC_IN_USE) {
#if BTM_SEC_SERVICE_NAME_LEN > 0
if (p_srec->psm == psm &&
p_srec->mx_proto_id == mx_proto_id &&
service_id == p_srec->service_id &&
(!strncmp (p_name, (char *) p_srec->orig_service_name,
BTM_SEC_SERVICE_NAME_LEN) ||
!strncmp (p_name, (char *) p_srec->term_service_name,
BTM_SEC_SERVICE_NAME_LEN)))/* ... */
#else
if (p_srec->psm == psm &&
p_srec->mx_proto_id == mx_proto_id &&
service_id == p_srec->service_id)/* ... */
#endif
{
record_allocated = TRUE;
break;
}{...}
}{...}
else if (!record_allocated) {
memset (p_srec, 0, sizeof(tBTM_SEC_SERV_REC));
record_allocated = TRUE;
first_unused_record = index;
}{...}
}{...}
if (!record_allocated) {
BTM_TRACE_WARNING("BTM_SEC_REG: Out of Service Records (%d)\n", BTM_SEC_MAX_SERVICE_RECORDS);
return (record_allocated);
}{...}
if (index >= BTM_SEC_MAX_SERVICE_RECORDS) {
index = first_unused_record;
p_srec = &btm_cb.sec_serv_rec[index];
}{...}
p_srec->psm = psm;
p_srec->service_id = service_id;
p_srec->mx_proto_id = mx_proto_id;
if (is_originator) {
p_srec->orig_mx_chan_id = mx_chan_id;
#if BTM_SEC_SERVICE_NAME_LEN > 0
BCM_STRNCPY_S ((char *)p_srec->orig_service_name, p_name, BTM_SEC_SERVICE_NAME_LEN);
p_srec->orig_service_name[BTM_SEC_SERVICE_NAME_LEN] = '\0';/* ... */
#endif
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( is_ucd ) {
p_srec->ucd_security_flags &=
~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_MITM |
BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE);
}{...} else
#endif
{
p_srec->security_flags &=
~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_MITM |
BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE);
}{...}
sec_level &= ~(BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHENTICATE
| BTM_SEC_IN_MITM | BTM_SEC_IN_MIN_16_DIGIT_PIN );
if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
btm_cb.security_mode == BTM_SEC_MODE_SC) {
if (sec_level & BTM_SEC_OUT_AUTHENTICATE) {
sec_level |= BTM_SEC_OUT_MITM;
}{...}
}{...}
if (sec_level & BTM_SEC_OUT_ENCRYPT) {
sec_level |= BTM_SEC_OUT_AUTHENTICATE;
}{...}
/* ... */
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( is_ucd == FALSE )
#endif
{
btm_cb.p_out_serv = p_srec;
}{...}
}{...} else {
p_srec->term_mx_chan_id = mx_chan_id;
#if BTM_SEC_SERVICE_NAME_LEN > 0
BCM_STRNCPY_S ((char *)p_srec->term_service_name, p_name, BTM_SEC_SERVICE_NAME_LEN);
p_srec->term_service_name[BTM_SEC_SERVICE_NAME_LEN] = '\0';/* ... */
#endif
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( is_ucd ) {
p_srec->ucd_security_flags &=
~(BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_MITM |
BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE
| BTM_SEC_IN_MIN_16_DIGIT_PIN);
}{...} else
#endif
{
p_srec->security_flags &=
~(BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_MITM |
BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER | BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE
| BTM_SEC_IN_MIN_16_DIGIT_PIN);
}{...}
sec_level &= ~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_OUT_ENCRYPT | BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_MITM);
if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
btm_cb.security_mode == BTM_SEC_MODE_SC) {
if (sec_level & BTM_SEC_IN_AUTHENTICATE) {
sec_level |= BTM_SEC_IN_MITM;
}{...}
}{...}
if (sec_level & BTM_SEC_IN_ENCRYPT) {
sec_level |= BTM_SEC_IN_AUTHENTICATE;
}{...}
}{...}
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( is_ucd ) {
p_srec->security_flags |= (UINT16)(BTM_SEC_IN_USE);
p_srec->ucd_security_flags |= (UINT16)(sec_level | BTM_SEC_IN_USE);
}{...} else {
p_srec->security_flags |= (UINT16)(sec_level | BTM_SEC_IN_USE);
}{...}
BTM_TRACE_API("BTM_SEC_REG[%d]: id %d, conn_type 0x%x, psm 0x%04x, proto_id %d, chan_id %d\n",
index, service_id, conn_type, psm, mx_proto_id, mx_chan_id);
BTM_TRACE_API(" : security_flags: 0x%04x, ucd_security_flags: 0x%04x\n",
p_srec->security_flags, p_srec->ucd_security_flags);
#if BTM_SEC_SERVICE_NAME_LEN > 0
BTM_TRACE_API(" : service name [%s] (up to %d chars saved)\n",
p_name, BTM_SEC_SERVICE_NAME_LEN);/* ... */
#endif/* ... */
#else
p_srec->security_flags |= (UINT16)(sec_level | BTM_SEC_IN_USE);
BTM_TRACE_API("BTM_SEC_REG[%d]: id %d, is_orig %d, psm 0x%04x, proto_id %d, chan_id %d\n",
index, service_id, is_originator, psm, mx_proto_id, mx_chan_id);
#if BTM_SEC_SERVICE_NAME_LEN > 0
BTM_TRACE_API(" : sec: 0x%x, service name [%s] (up to %d chars saved)\n",
p_srec->security_flags, p_name, BTM_SEC_SERVICE_NAME_LEN);/* ... */
#endif/* ... */
#endif
return (record_allocated);/* ... */
#else
return FALSE;
#endif
}{ ... }
/* ... */
#if (SDP_INCLUDED == TRUE)
UINT8 BTM_SecClrService (UINT8 service_id)
{
tBTM_SEC_SERV_REC *p_srec = &btm_cb.sec_serv_rec[0];
UINT8 num_freed = 0;
int i;
for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_srec++) {
if ((p_srec->security_flags & BTM_SEC_IN_USE) && (p_srec->psm != BT_PSM_SDP) &&
(!service_id || (service_id == p_srec->service_id))) {
BTM_TRACE_API("BTM_SEC_CLR[%d]: id %d\n", i, service_id);
p_srec->security_flags = 0;
#if (L2CAP_UCD_INCLUDED == TRUE)
p_srec->ucd_security_flags = 0;
#endif
num_freed++;
}{...}
}{...}
return (num_freed);
}{...}
/* ... */#endif
/* ... */
#if (SDP_INCLUDED== TRUE)
UINT8 btm_sec_clr_service_by_psm (UINT16 psm)
{
tBTM_SEC_SERV_REC *p_srec = &btm_cb.sec_serv_rec[0];
UINT8 num_freed = 0;
int i;
for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_srec++) {
if ((p_srec->security_flags & BTM_SEC_IN_USE) && (p_srec->psm == psm) ) {
BTM_TRACE_API("BTM_SEC_CLR[%d]: id %d\n", i, p_srec->service_id);
p_srec->security_flags = 0;
num_freed++;
}{...}
}{...}
BTM_TRACE_API("btm_sec_clr_service_by_psm psm:0x%x num_freed:%d\n", psm, num_freed);
return (num_freed);
}{...}
/* ... */#endif
/* ... */
void btm_sec_clr_temp_auth_service (BD_ADDR bda)
{
tBTM_SEC_DEV_REC *p_dev_rec;
if ((p_dev_rec = btm_find_dev (bda)) == NULL) {
BTM_TRACE_WARNING ("btm_sec_clr_temp_auth_service() - no dev CB\n");
return;
}{...}
if (p_dev_rec->last_author_service_id != BTM_SEC_NO_LAST_SERVICE_ID && p_dev_rec->p_cur_service) {
BTM_TRACE_DEBUG ("btm_sec_clr_auth_service_by_psm [clearing device: %02x:%02x:%02x:%02x:%02x:%02x]\n",
bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
p_dev_rec->last_author_service_id = BTM_SEC_NO_LAST_SERVICE_ID;
}{...}
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
#if (CLASSIC_BT_INCLUDED == TRUE)
void BTM_PINCodeReply (BD_ADDR bd_addr, UINT8 res, UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
{
tBTM_SEC_DEV_REC *p_dev_rec;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_API ("BTM_PINCodeReply(): PairState: %s PairFlags: 0x%02x PinLen:%d Result:%d\n",
btm_pair_state_descr(btm_cb.pairing_state), btm_cb.pairing_flags, pin_len, res);/* ... */
#endif
if (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_PIN) {
BTM_TRACE_WARNING ("BTM_PINCodeReply() - Wrong State: %d\n", btm_cb.pairing_state);
return;
}{...}
if (memcmp (bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) != 0) {
BTM_TRACE_ERROR ("BTM_PINCodeReply() - Wrong BD Addr\n");
return;
}{...}
if ((p_dev_rec = btm_find_dev (bd_addr)) == NULL) {
BTM_TRACE_ERROR ("BTM_PINCodeReply() - no dev CB\n");
return;
}{...}
if ( (pin_len > PIN_CODE_LEN) || (pin_len == 0) || (p_pin == NULL) ) {
res = BTM_ILLEGAL_VALUE;
}{...}
if (res != BTM_SUCCESS) {
if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_PEER_STARTED_DD) ||
((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) &&
(btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE)) ) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
btsnd_hcic_pin_code_neg_reply (bd_addr);
}{...} else {
p_dev_rec->security_required = BTM_SEC_NONE;
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
}{...}
return;
}{...}
if (trusted_mask) {
BTM_SEC_COPY_TRUSTED_DEVICE(trusted_mask, p_dev_rec->trusted_mask);
}{...}
p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_AUTHED;
if (pin_len >= 16) {
p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
}{...}
if ( (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
&& (p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE)
&& (btm_cb.security_mode_changed == FALSE) ) {
btm_cb.pin_code_len = pin_len;
p_dev_rec->pin_code_length = pin_len;
memcpy (btm_cb.pin_code, p_pin, pin_len);
btm_cb.security_mode_changed = TRUE;
#ifdef APPL_AUTH_WRITE_EXCEPTION
if (!(APPL_AUTH_WRITE_EXCEPTION)(p_dev_rec->bd_addr))
#endif
{
btsnd_hcic_write_auth_enable (TRUE);
}{...}
btm_cb.acl_disc_reason = 0xff ;
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT) {
BTM_TRACE_WARNING ("BTM_PINCodeReply(): waiting HCI_Connection_Complete after rejected incoming connection\n");
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
}{...}
else if (p_dev_rec->sm4 & BTM_SM4_CONN_PEND) {
BTM_TRACE_WARNING ("BTM_PINCodeReply(): link is connecting so wait pin code request from peer\n");
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
}{...} else if (btm_sec_dd_create_conn(p_dev_rec) != BTM_CMD_STARTED) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
p_dev_rec->sec_flags &= ~BTM_SEC_LINK_KEY_AUTHED;
if (btm_cb.api.p_auth_complete_callback) {
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_ERR_AUTH_FAILURE);
}{...}
}{...}
return;
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
btm_cb.acl_disc_reason = HCI_SUCCESS;
#ifdef PORCHE_PAIRING_CONFLICT
BTM_TRACE_EVENT("BTM_PINCodeReply(): Saving pin_len: %d btm_cb.pin_code_len: %d\n", pin_len, btm_cb.pin_code_len);
if (btm_cb.pin_code_len == 0) {
memcpy (btm_cb.pin_code, p_pin, pin_len);
}{...}
btm_cb.pin_code_len_saved = pin_len;/* ... */
#endif
btsnd_hcic_pin_code_req_reply (bd_addr, pin_len, p_pin);
}{...}
/* ... */#endif /* ... */
#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
tBTM_STATUS btm_sec_bond_by_transport (BD_ADDR bd_addr, tBT_TRANSPORT transport,
UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
{
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_STATUS status;
#if (!CONFIG_BT_STACK_NO_LOG)
UINT8 *p_features;
#endif
UINT8 ii;
tACL_CONN *p = btm_bda_to_acl(bd_addr, transport);
BTM_TRACE_API ("btm_sec_bond_by_transport BDA: %02x:%02x:%02x:%02x:%02x:%02x\n",
bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
BTM_TRACE_DEBUG("btm_sec_bond_by_transport: Transport used %d\n" , transport);
if (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) {
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_ERROR ("BTM_SecBond: already busy in state: %s\n", btm_pair_state_descr(btm_cb.pairing_state));
#endif
return (BTM_WRONG_MODE);
}{...}
if ((p_dev_rec = btm_find_or_alloc_dev (bd_addr)) == NULL) {
return (BTM_NO_RESOURCES);
}{...}
BTM_TRACE_DEBUG ("before update sec_flags=0x%x\n", p_dev_rec->sec_flags);
if ( ((p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE) && transport == BT_TRANSPORT_BR_EDR
&& (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))
#if (BLE_INCLUDED == TRUE)
|| ((p_dev_rec->ble_hci_handle != BTM_SEC_INVALID_HANDLE) && transport == BT_TRANSPORT_LE
&& (p_dev_rec->sec_flags & BTM_SEC_LE_AUTHENTICATED))/* ... */
#endif
) {
BTM_TRACE_WARNING("BTM_SecBond -> Already Paired\n");
return (BTM_SUCCESS);
}{...}
if ((BTM_DeleteStoredLinkKey (bd_addr, NULL)) != BTM_SUCCESS) {
return (BTM_NO_RESOURCES);
}{...}
#if (CLASSIC_BT_INCLUDED == TRUE)
if (p_pin && (pin_len <= PIN_CODE_LEN) && (pin_len != 0)) {
btm_cb.pin_code_len = pin_len;
p_dev_rec->pin_code_length = pin_len;
memcpy (btm_cb.pin_code, p_pin, PIN_CODE_LEN);
}{...}
#endif/* ... */
memcpy (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN);
btm_cb.pairing_flags = BTM_PAIR_FLAGS_WE_STARTED_DD;
p_dev_rec->security_required = BTM_SEC_OUT_AUTHENTICATE;
p_dev_rec->is_originator = TRUE;
if (trusted_mask) {
BTM_SEC_COPY_TRUSTED_DEVICE(trusted_mask, p_dev_rec->trusted_mask);
}{...}
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
if (transport == BT_TRANSPORT_LE) {
btm_ble_init_pseudo_addr (p_dev_rec, bd_addr);
p_dev_rec->sec_flags &= ~ BTM_SEC_LE_MASK;
if (SMP_Pair(bd_addr) == SMP_STARTED) {
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_LE_ACTIVE;
p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
return BTM_CMD_STARTED;
}{...}
btm_cb.pairing_flags = 0;
return (BTM_NO_RESOURCES);
}{...}
#endif/* ... */
p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED
| BTM_SEC_ROLE_SWITCHED | BTM_SEC_LINK_KEY_AUTHED);
BTM_TRACE_DEBUG ("after update sec_flags=0x%x\n", p_dev_rec->sec_flags);
#if (CLASSIC_BT_INCLUDED == TRUE)
if (!controller_get_interface()->supports_simple_pairing()) {
if (((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) == BTM_COD_MAJOR_PERIPHERAL)
&& (p_dev_rec->dev_class[2] & BTM_COD_MINOR_KEYBOARD)
&& (btm_cb.cfg.pin_type != HCI_PIN_TYPE_FIXED)) {
btm_cb.pin_type_changed = TRUE;
btsnd_hcic_write_pin_type (HCI_PIN_TYPE_FIXED);
}{...}
}{...}
#endif/* ... */
for (ii = 0; ii <= HCI_EXT_FEATURES_PAGE_MAX; ii++) {
#if (!CONFIG_BT_STACK_NO_LOG)
p_features = p_dev_rec->features[ii];
#endif
BTM_TRACE_EVENT(" remote_features page[%1d] = %02x-%02x-%02x-%02x\n",
ii, p_features[0], p_features[1], p_features[2], p_features[3]);
BTM_TRACE_EVENT(" %02x-%02x-%02x-%02x\n",
p_features[4], p_features[5], p_features[6], p_features[7]);
}{...}
BTM_TRACE_EVENT ("BTM_SecBond: Remote sm4: 0x%x HCI Handle: 0x%04x\n", p_dev_rec->sm4, p_dev_rec->hci_handle);
#if BTM_SEC_FORCE_RNR_FOR_DBOND == TRUE
p_dev_rec->sec_flags &= ~BTM_SEC_NAME_KNOWN;
#endif
if (p && p->hci_handle != BTM_SEC_INVALID_HANDLE) {
if (!btm_sec_start_authentication (p_dev_rec)) {
return (BTM_NO_RESOURCES);
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
l2cu_update_lcb_4_bonding (bd_addr, TRUE);
return (BTM_CMD_STARTED);
}{...}
BTM_TRACE_DEBUG ("sec mode: %d sm4:x%x\n", btm_cb.security_mode, p_dev_rec->sm4);
if (!controller_get_interface()->supports_simple_pairing()
|| (p_dev_rec->sm4 == BTM_SM4_KNOWN)) {
if ( btm_sec_check_prefetch_pin (p_dev_rec) ) {
return (BTM_CMD_STARTED);
}{...}
}{...}
if ((btm_cb.security_mode == BTM_SEC_MODE_SP ||
btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
btm_cb.security_mode == BTM_SEC_MODE_SC) &&
BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
if ((p_dev_rec->sm4 & BTM_SM4_CONN_PEND) == 0) {
/* ... */
btm_sec_change_pairing_state (BTM_PAIR_STATE_GET_REM_NAME);
BTM_ReadRemoteDeviceName(bd_addr, NULL, BT_TRANSPORT_BR_EDR);
}{...} else {
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
}{...}
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_DEBUG ("State:%s sm4: 0x%x sec_state:%d\n",
btm_pair_state_descr (btm_cb.pairing_state), p_dev_rec->sm4, p_dev_rec->sec_state);/* ... */
#endif
return BTM_CMD_STARTED;
}{...}
status = btm_sec_dd_create_conn(p_dev_rec);
if (status != BTM_CMD_STARTED) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
}{...}
return status;
}{ ... }
/* ... */
tBTM_STATUS BTM_SecBondByTransport (BD_ADDR bd_addr, tBT_TRANSPORT transport,
UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
{
#if (BLE_INCLUDED == TRUE)
tBT_DEVICE_TYPE dev_type;
tBLE_ADDR_TYPE addr_type = 0;
BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
if ((transport == BT_TRANSPORT_LE && (dev_type & BT_DEVICE_TYPE_BLE) == 0) ||
(transport == BT_TRANSPORT_BR_EDR && (dev_type & BT_DEVICE_TYPE_BREDR) == 0)) {
return BTM_ILLEGAL_ACTION;
}{...}
/* ... */#endif
return btm_sec_bond_by_transport(bd_addr, transport, pin_len, p_pin, trusted_mask);
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
tBTM_STATUS BTM_SecBond (BD_ADDR bd_addr, UINT8 pin_len, UINT8 *p_pin, UINT32 trusted_mask[])
{
tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
#if (BLE_INCLUDED == TRUE)
if (BTM_UseLeLink(bd_addr)) {
transport = BT_TRANSPORT_LE;
}{...}
#endif/* ... */
return btm_sec_bond_by_transport(bd_addr, transport, pin_len, p_pin, trusted_mask);
}{ ... }
/* ... */
tBTM_STATUS BTM_SecBondCancel (BD_ADDR bd_addr)
{
tBTM_SEC_DEV_REC *p_dev_rec;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_API ("BTM_SecBondCancel() State: %s flags:0x%x\n",
btm_pair_state_descr (btm_cb.pairing_state), btm_cb.pairing_flags);/* ... */
#endif
if (((p_dev_rec = btm_find_dev (bd_addr)) == NULL)
|| (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
return BTM_UNKNOWN_ADDR;
}{...}
#if SMP_INCLUDED == TRUE
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_LE_ACTIVE) {
if (p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING) {
BTM_TRACE_DEBUG ("Cancel LE pairing\n");
if (SMP_PairCancel(bd_addr)) {
return BTM_CMD_STARTED;
}{...}
}{...}
return BTM_WRONG_MODE;
}{...}
/* ... */#endif
BTM_TRACE_DEBUG ("hci_handle:0x%x sec_state:%d\n", p_dev_rec->hci_handle, p_dev_rec->sec_state );
if (BTM_PAIR_STATE_WAIT_LOCAL_PIN == btm_cb.pairing_state &&
BTM_PAIR_FLAGS_WE_STARTED_DD & btm_cb.pairing_flags) {
btm_sec_bond_cancel_complete();
return BTM_SUCCESS;
}{...}
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)) {
if (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE) {
if ((p_dev_rec->sec_state == BTM_SEC_STATE_DISCONNECTING) ||
(p_dev_rec->sec_state == BTM_SEC_STATE_DISCONNECTING_BOTH)) {
return (BTM_CMD_STARTED);
}{...}
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) {
return btm_sec_send_hci_disconnect(p_dev_rec, HCI_ERR_PEER_USER, p_dev_rec->hci_handle);
}{...} else {
l2cu_update_lcb_4_bonding(bd_addr, FALSE);
}{...}
return BTM_NOT_AUTHORIZED;
}{...} else {
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) {
if (btsnd_hcic_create_conn_cancel(bd_addr)) {
return BTM_CMD_STARTED;
}{...}
return BTM_NO_RESOURCES;
}{...}
if (btm_cb.pairing_state == BTM_PAIR_STATE_GET_REM_NAME) {
BTM_CancelRemoteDeviceName();
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_WE_CANCEL_DD;
return BTM_CMD_STARTED;
}{...}
return BTM_NOT_AUTHORIZED;
}{...}
}{...}
return BTM_WRONG_MODE;
}{ ... }
/* ... */
tBTM_STATUS BTM_SecGetDeviceLinkKey (BD_ADDR bd_addr, LINK_KEY link_key)
{
tBTM_SEC_DEV_REC *p_dev_rec;
if (((p_dev_rec = btm_find_dev (bd_addr)) != NULL)
&& (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
memcpy (link_key, p_dev_rec->link_key, LINK_KEY_LEN);
return (BTM_SUCCESS);
}{...}
return (BTM_UNKNOWN_ADDR);
}{ ... }
#endif/* ... */
/* ... */
tBTM_LINK_KEY_TYPE BTM_SecGetDeviceLinkKeyType (BD_ADDR bd_addr)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
if ((p_dev_rec != NULL) && (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
return p_dev_rec->link_key_type;
}{...}
return BTM_LKEY_TYPE_IGNORE;
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
tBTM_STATUS BTM_SetEncryption (BD_ADDR bd_addr, tBT_TRANSPORT transport, tBTM_SEC_CBACK *p_callback,
void *p_ref_data)
{
tBTM_STATUS rc = 0;
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
if (!p_dev_rec ||
(transport == BT_TRANSPORT_BR_EDR && p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE)
#if BLE_INCLUDED == TRUE
|| (transport == BT_TRANSPORT_LE && p_dev_rec->ble_hci_handle == BTM_SEC_INVALID_HANDLE)
#endif
) {
BTM_TRACE_WARNING ("Security Manager: BTM_SetEncryption not connected\n");
if (p_callback) {
(*p_callback) (bd_addr, transport, p_ref_data, BTM_WRONG_MODE);
}{...}
return (BTM_WRONG_MODE);
}{...}
if ((transport == BT_TRANSPORT_BR_EDR &&
(p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
|| (transport == BT_TRANSPORT_LE &&
(p_dev_rec->sec_flags & BTM_SEC_LE_ENCRYPTED))/* ... */
#endif
) {
BTM_TRACE_EVENT ("Security Manager: BTM_SetEncryption already encrypted\n");
if (p_callback) {
(*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS);
}{...}
return (BTM_SUCCESS);
}{...}
p_dev_rec->enc_init_by_we = TRUE;
if (p_dev_rec->p_callback || (p_dev_rec->sec_state != BTM_SEC_STATE_IDLE)) {
BTM_TRACE_WARNING ("Security Manager: BTM_SetEncryption busy, enqueue request\n");
if (btm_sec_queue_encrypt_request(bd_addr, transport, p_callback, p_ref_data)) {
return BTM_CMD_STARTED;
}{...} else {
if (p_callback) {
(*p_callback) (bd_addr, transport, p_ref_data, BTM_NO_RESOURCES);
}{...}
return BTM_NO_RESOURCES;
}{...}
}{...}
p_dev_rec->p_callback = p_callback;
p_dev_rec->p_ref_data = p_ref_data;
p_dev_rec->security_required |= (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT);
p_dev_rec->is_originator = FALSE;
BTM_TRACE_API ("Security Manager: BTM_SetEncryption Handle:%d State:%d Flags:0x%x Required:0x%x\n",
p_dev_rec->hci_handle, p_dev_rec->sec_state, p_dev_rec->sec_flags,
p_dev_rec->security_required);
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
if (transport == BT_TRANSPORT_LE) {
tACL_CONN *p = btm_bda_to_acl(bd_addr, transport);
if (p) {
rc = btm_ble_set_encryption(bd_addr, p_ref_data, p->link_role);
}{...} else {
rc = BTM_WRONG_MODE;
BTM_TRACE_WARNING("%s: cannot call btm_ble_set_encryption, p is NULL\n", __FUNCTION__);
}{...}
}{...} else
#endif
{
rc = btm_sec_execute_procedure (p_dev_rec);
}{...}
if (rc != BTM_CMD_STARTED && rc != BTM_BUSY) {
if (p_callback) {
p_dev_rec->p_callback = NULL;
(*p_callback) (bd_addr, transport, p_dev_rec->p_ref_data, rc);
}{...}
}{...}
return (rc);
}{ ... }
/* ... */
static tBTM_STATUS btm_sec_send_hci_disconnect (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 reason, UINT16 conn_handle)
{
UINT8 old_state = p_dev_rec->sec_state;
tBTM_STATUS status = BTM_CMD_STARTED;
BTM_TRACE_EVENT ("btm_sec_send_hci_disconnect: handle:0x%x, reason=0x%x\n",
conn_handle, reason);
switch (old_state) {
case BTM_SEC_STATE_DISCONNECTING:
if (conn_handle == p_dev_rec->hci_handle) {
return status;
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_DISCONNECTING_BOTH;
break;
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE...
case BTM_SEC_STATE_DISCONNECTING_BLE:
if (conn_handle == p_dev_rec->ble_hci_handle) {
return status;
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_DISCONNECTING_BOTH;
break;
...
case BTM_SEC_STATE_DISCONNECTING_BOTH:
return status;/* ... */
#endif
default:
p_dev_rec->sec_state = (conn_handle == p_dev_rec->hci_handle) ?
BTM_SEC_STATE_DISCONNECTING : BTM_SEC_STATE_DISCONNECTING_BLE;
break;...
}{...}
if (p_dev_rec->rs_disc_pending == BTM_SEC_RS_PENDING && p_dev_rec->hci_handle == conn_handle) {
BTM_TRACE_DEBUG("RS in progress - Set DISC Pending flag in btm_sec_send_hci_disconnect to delay disconnect\n");
p_dev_rec->rs_disc_pending = BTM_SEC_DISC_PENDING;
status = BTM_SUCCESS;
}{...}
else if (!btsnd_hcic_disconnect (conn_handle, reason)) {
p_dev_rec->sec_state = old_state;
status = BTM_NO_RESOURCES;
}{...}
return status;
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
void BTM_ConfirmReqReply(tBTM_STATUS res, BD_ADDR bd_addr)
{
tBTM_SEC_DEV_REC *p_dev_rec;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("BTM_ConfirmReqReply() State: %s Res: %u",
btm_pair_state_descr(btm_cb.pairing_state), res);/* ... */
#endif
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM)
|| (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
return;
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
if ( (res == BTM_SUCCESS) || (res == BTM_SUCCESS_NO_SECURITY) ) {
btm_cb.acl_disc_reason = HCI_SUCCESS;
if (res == BTM_SUCCESS) {
if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL) {
p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_AUTHED;
p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
}{...}
}{...}
btsnd_hcic_user_conf_reply (bd_addr, TRUE);
}{...} else {
btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
btsnd_hcic_user_conf_reply (bd_addr, FALSE);
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (CLASSIC_BT_INCLUDED == TRUE)
void BTM_PasskeyReqReply(tBTM_STATUS res, BD_ADDR bd_addr, UINT32 passkey)
{
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_API ("BTM_PasskeyReqReply: State: %s res:%d\n",
btm_pair_state_descr(btm_cb.pairing_state), res);/* ... */
#endif
if ( (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE)
|| (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
return;
}{...}
if ( (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_AUTH_COMPLETE) && (res != BTM_SUCCESS) ) {
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
if (p_dev_rec != NULL) {
btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
if (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE) {
btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
}{...} else {
BTM_SecBondCancel(bd_addr);
}{...}
p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_AUTHED | BTM_SEC_LINK_KEY_KNOWN);
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
return;
}{...}
}{...} else if (btm_cb.pairing_state != BTM_PAIR_STATE_KEY_ENTRY) {
return;
}{...}
if (passkey > BTM_MAX_PASSKEY_VAL) {
res = BTM_ILLEGAL_VALUE;
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
if (res != BTM_SUCCESS) {
btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
btsnd_hcic_user_passkey_neg_reply (bd_addr);
}{...} else {
btm_cb.acl_disc_reason = HCI_SUCCESS;
btsnd_hcic_user_passkey_reply (bd_addr, passkey);
}{...}
}{...}
/* ... */#endif
/* ... */
#if (CLASSIC_BT_INCLUDED == TRUE)
void BTM_SendKeypressNotif(BD_ADDR bd_addr, tBTM_SP_KEY_TYPE type)
{
if (btm_cb.pairing_state == BTM_PAIR_STATE_KEY_ENTRY) {
btsnd_hcic_send_keypress_notif (bd_addr, type);
}{...}
}{...}
/* ... */#endif
#if BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE
/* ... */
void BTM_IoCapRsp(BD_ADDR bd_addr, tBTM_IO_CAP io_cap, tBTM_OOB_DATA oob, tBTM_AUTH_REQ auth_req)
{
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("BTM_IoCapRsp: state: %s oob: %d io_cap: %d\n",
btm_pair_state_descr(btm_cb.pairing_state), oob, io_cap);/* ... */
#endif
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS)
|| (memcmp (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN) != 0) ) {
return;
}{...}
if (oob < BTM_OOB_UNKNOWN && io_cap < BTM_IO_CAP_MAX) {
btm_cb.devcb.loc_auth_req = auth_req;
btm_cb.devcb.loc_io_caps = io_cap;
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
auth_req = (BTM_AUTH_DD_BOND | (auth_req & BTM_AUTH_YN_BIT));
}{...}
btsnd_hcic_io_cap_req_reply (bd_addr, io_cap, oob, auth_req);
}{...}
}{ ... }
/* ... */
tBTM_STATUS BTM_ReadLocalOobData(void)
{
tBTM_STATUS status = BTM_SUCCESS;
if (btsnd_hcic_read_local_oob_data() == FALSE) {
status = BTM_NO_RESOURCES;
}{...}
return status;
}{ ... }
/* ... */
void BTM_RemoteOobDataReply(tBTM_STATUS res, BD_ADDR bd_addr, BT_OCTET16 c, BT_OCTET16 r)
{
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("%s() - State: %s res: %d\n", __func__,
btm_pair_state_descr(btm_cb.pairing_state), res);/* ... */
#endif
if (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP) {
return;
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
if (res != BTM_SUCCESS) {
btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
btsnd_hcic_rem_oob_neg_reply (bd_addr);
}{...} else {
btm_cb.acl_disc_reason = HCI_SUCCESS;
btsnd_hcic_rem_oob_reply (bd_addr, c, r);
}{...}
}{ ... }
/* ... */
UINT16 BTM_BuildOobData(UINT8 *p_data, UINT16 max_len, BT_OCTET16 c,
BT_OCTET16 r, UINT8 name_len)
{
UINT8 *p = p_data;
UINT16 len = 0;
#if BTM_MAX_LOC_BD_NAME_LEN > 0
UINT16 name_size;
UINT8 name_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;/* ... */
#endif
if (p_data && max_len >= BTM_OOB_MANDATORY_SIZE) {
UINT16_TO_STREAM(p, len);
BDADDR_TO_STREAM(p, controller_get_interface()->get_address()->address);
len = BTM_OOB_MANDATORY_SIZE;
max_len -= len;
UINT16 delta = BTM_OOB_HASH_C_SIZE + 2;
if (max_len >= delta) {
*p++ = BTM_OOB_HASH_C_SIZE + 1;
*p++ = BTM_EIR_OOB_SSP_HASH_C_TYPE;
ARRAY_TO_STREAM(p, c, BTM_OOB_HASH_C_SIZE);
len += delta;
max_len -= delta;
}{...}
delta = BTM_OOB_RAND_R_SIZE + 2;
if (max_len >= delta) {
*p++ = BTM_OOB_RAND_R_SIZE + 1;
*p++ = BTM_EIR_OOB_SSP_RAND_R_TYPE;
ARRAY_TO_STREAM(p, r, BTM_OOB_RAND_R_SIZE);
len += delta;
max_len -= delta;
}{...}
delta = BTM_OOB_COD_SIZE + 2;
if (max_len >= delta) {
*p++ = BTM_OOB_COD_SIZE + 1;
*p++ = BTM_EIR_OOB_COD_TYPE;
DEVCLASS_TO_STREAM(p, btm_cb.devcb.dev_class);
len += delta;
max_len -= delta;
}{...}
#if BTM_MAX_LOC_BD_NAME_LEN > 0
name_size = name_len;
if (name_size > strlen(btm_cb.cfg.bredr_bd_name)) {
name_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
name_size = (UINT16)strlen(btm_cb.cfg.bredr_bd_name);
}{...}
delta = name_size + 2;
if (max_len >= delta) {
*p++ = name_size + 1;
*p++ = name_type;
ARRAY_TO_STREAM (p, btm_cb.cfg.bredr_bd_name, name_size);
len += delta;
max_len -= delta;
}{...}
#endif/* ... */
p = p_data;
UINT16_TO_STREAM(p, len);
}{...}
return len;
}{ ... }
/* ... */
UINT8 *BTM_ReadOobData(UINT8 *p_data, UINT8 eir_tag, UINT8 *p_len)
{
UINT8 *p = p_data;
UINT16 max_len;
UINT8 len, type;
UINT8 *p_ret = NULL;
UINT8 ret_len = 0;
if (p_data) {
STREAM_TO_UINT16(max_len, p);
if (max_len >= BTM_OOB_MANDATORY_SIZE) {
if (BTM_EIR_OOB_BD_ADDR_TYPE == eir_tag) {
p_ret = p;
ret_len = BTM_OOB_BD_ADDR_SIZE;
}{...} else {
p += BD_ADDR_LEN;
max_len -= BTM_OOB_MANDATORY_SIZE;
while (max_len > 0) {
len = *p++;
type = *p++;
if (eir_tag == type) {
p_ret = p;
ret_len = len - 1;
break;
}{...}
if (max_len > len) {
max_len -= len;
max_len--;
len--;
p += len;
}{...} else {
max_len = 0;
}{...}
}{...}
}{...}
}{...}
}{...}
if (p_len) {
*p_len = ret_len;
}{...}
return p_ret;
}{ ... }
#endif/* ... */
#if (CLASSIC_BT_INCLUDED == TRUE)
/* ... */
BOOLEAN BTM_BothEndsSupportSecureConnections(BD_ADDR bd_addr)
{
return ((controller_get_interface()->supports_secure_connections()) &&
(BTM_PeerSupportsSecureConnections(bd_addr)));
}{...}
/* ... */
BOOLEAN BTM_PeerSupportsSecureConnections(BD_ADDR bd_addr)
{
tBTM_SEC_DEV_REC *p_dev_rec;
if ((p_dev_rec = btm_find_dev(bd_addr)) == NULL) {
BTM_TRACE_WARNING("%s: unknown BDA: %08x%04x\n", __FUNCTION__,
(bd_addr[0] << 24) + (bd_addr[1] << 16) + (bd_addr[2] << 8) + bd_addr[3],
(bd_addr[4] << 8) + bd_addr[5]);
return FALSE;
}{...}
return (p_dev_rec->remote_supports_secure_connections);
}{...}
/* ... */
void BTM_SetOutService(BD_ADDR bd_addr, UINT8 service_id, UINT32 mx_chan_id)
{
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
btm_cb.p_out_serv = p_serv_rec;
p_dev_rec = btm_find_dev (bd_addr);
for (int i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
if ((p_serv_rec->security_flags & BTM_SEC_IN_USE)
&& (p_serv_rec->service_id == service_id)
&& (p_serv_rec->orig_mx_chan_id == mx_chan_id)) {
BTM_TRACE_API("BTM_SetOutService p_out_serv id %d, psm 0x%04x, proto_id %d, chan_id %d\n",
p_serv_rec->service_id, p_serv_rec->psm, p_serv_rec->mx_proto_id, p_serv_rec->orig_mx_chan_id);
btm_cb.p_out_serv = p_serv_rec;
if (p_dev_rec) {
p_dev_rec->p_cur_service = p_serv_rec;
}{...}
break;
}{...}
}{...}
}{...}
/* ... */#endif
/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static BOOLEAN btm_sec_is_upgrade_possible(tBTM_SEC_DEV_REC *p_dev_rec, BOOLEAN is_originator)
{
UINT16 mtm_check = is_originator ? BTM_SEC_OUT_MITM : BTM_SEC_IN_MITM;
BOOLEAN is_possible = TRUE;
if (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) {
is_possible = FALSE;
if (p_dev_rec->p_cur_service) {
BTM_TRACE_DEBUG ("%s() id: %d, link_key_typet: %d, rmt_io_caps: %d, chk flags: 0x%x, flags: 0x%x\n",
__func__, p_dev_rec->p_cur_service->service_id, p_dev_rec->link_key_type,
p_dev_rec->rmt_io_caps, mtm_check, p_dev_rec->p_cur_service->security_flags);
}{...} else {
BTM_TRACE_DEBUG ("%s() link_key_typet: %d, rmt_io_caps: %d, chk flags: 0x%x\n",
__func__, p_dev_rec->link_key_type, p_dev_rec->rmt_io_caps, mtm_check);
}{...}
/* ... */
if ((p_dev_rec->security_required & mtm_check)
&& ((p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB) ||
(p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256))
/* ... */
&& (p_dev_rec->rmt_io_caps < BTM_IO_CAP_MAX)
&& (btm_sec_io_map[p_dev_rec->rmt_io_caps][btm_cb.devcb.loc_io_caps]))
/* ... */
{
/* ... */
is_possible = TRUE;
}{...}
}{...}
BTM_TRACE_DEBUG ("%s() is_possible: %d sec_flags: 0x%x\n", __func__, is_possible, p_dev_rec->sec_flags);
return is_possible;
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_sec_check_upgrade(tBTM_SEC_DEV_REC *p_dev_rec, BOOLEAN is_originator)
{
BTM_TRACE_DEBUG ("%s()\n", __func__);
if (!(p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
return;
}{...}
if (btm_sec_is_upgrade_possible (p_dev_rec, is_originator) == TRUE) {
BTM_TRACE_DEBUG ("need upgrade!! sec_flags:0x%x\n", p_dev_rec->sec_flags);
/* ... */
tBTM_SP_UPGRADE evt_data;
memcpy (evt_data.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
evt_data.upgrade = TRUE;
if (btm_cb.api.p_sp_callback) {
(*btm_cb.api.p_sp_callback) (BTM_SP_UPGRADE_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
}{...}
BTM_TRACE_DEBUG ("evt_data.upgrade:0x%x\n", evt_data.upgrade);
if (evt_data.upgrade) {
p_dev_rec->sm4 |= BTM_SM4_UPGRADE;
p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED);
p_dev_rec->sec_flags &= ~BTM_SEC_AUTHENTICATED;
BTM_TRACE_DEBUG ("sec_flags:0x%x\n", p_dev_rec->sec_flags);
}{...}
}{...}
}{ ... }
/* ... */#endif
/* ... */
tBTM_STATUS btm_sec_l2cap_access_req (BD_ADDR bd_addr, UINT16 psm, UINT16 handle,
CONNECTION_TYPE conn_type,
tBTM_SEC_CALLBACK *p_callback,
void *p_ref_data)
{
#if (SMP_INCLUDED == TRUE)
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_SEC_SERV_REC *p_serv_rec;
UINT16 security_required;
UINT16 old_security_required;
BOOLEAN old_is_originator;
tBTM_STATUS rc = BTM_SUCCESS;
BOOLEAN chk_acp_auth_done = FALSE;
BOOLEAN is_originator;
BOOLEAN transport = FALSE;
#if (L2CAP_UCD_INCLUDED == TRUE)
if (conn_type & CONNECTION_TYPE_ORIG_MASK) {
is_originator = TRUE;
}{...} else {
is_originator = FALSE;
}{...}
BTM_TRACE_DEBUG ("%s() conn_type: 0x%x, %p\n", __func__, conn_type, p_ref_data);/* ... */
#else
is_originator = conn_type;
BTM_TRACE_DEBUG ("%s() is_originator:%d, %p\n", __func__, is_originator, p_ref_data);/* ... */
#endif
p_dev_rec = btm_find_or_alloc_dev (bd_addr);
p_dev_rec->hci_handle = handle;
p_serv_rec = btm_sec_find_first_serv (conn_type, psm);
if (!p_serv_rec) {
BTM_TRACE_WARNING ("%s() PSM: %d no application registered\n", __func__, psm);
(*p_callback) (bd_addr, transport, p_ref_data, BTM_MODE_UNSUPPORTED);
return (BTM_MODE_UNSUPPORTED);
}{...}
if ((btm_sec_is_serv_level0(psm)) && (!btm_cb.devcb.secure_connections_only)) {
(*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS_NO_SECURITY);
return (BTM_SUCCESS);
}{...}
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( conn_type & CONNECTION_TYPE_CONNLESS_MASK ) {
if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
security_required = btm_sec_set_serv_level4_flags (p_serv_rec->ucd_security_flags,
is_originator);
}{...} else {
security_required = p_serv_rec->ucd_security_flags;
}{...}
rc = BTM_CMD_STARTED;
if (is_originator) {
if (((security_required & BTM_SEC_OUT_FLAGS) == 0) ||
((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_AUTHENTICATE) && (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))) ||
((((security_required & BTM_SEC_OUT_FLAGS) == (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT)) && (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))) ||
((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_FLAGS) && (p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED))) ) {
rc = BTM_SUCCESS;
}{...}
}{...} else {
if (((security_required & BTM_SEC_IN_FLAGS) == 0) ||
((((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHENTICATE) && (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))) ||
((((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT)) && (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))) ||
((((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_FLAGS) && (p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED))) ) {
if (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == 0) ||
(((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == BTM_SEC_IN_MIN_16_DIGIT_PIN) &&
btm_dev_16_digit_authenticated(p_dev_rec))) {
rc = BTM_SUCCESS;
}{...}
}{...}
}{...}
if ((rc == BTM_SUCCESS) && (security_required & BTM_SEC_MODE4_LEVEL4) &&
(p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
rc = BTM_CMD_STARTED;
}{...}
if (rc == BTM_SUCCESS) {
if (p_callback) {
(*p_callback) (bd_addr, transport, (void *)p_ref_data, BTM_SUCCESS);
}{...}
return (BTM_SUCCESS);
}{...}
}{...} else
#endif
{
if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
security_required = btm_sec_set_serv_level4_flags (p_serv_rec->security_flags,
is_originator);
}{...} else {
security_required = p_serv_rec->security_flags;
}{...}
}{...}
BTM_TRACE_DEBUG("%s: security_required 0x%04x, is_originator 0x%02x, psm 0x%04x\n",
__FUNCTION__, security_required, is_originator, psm);
if ((!is_originator) && (security_required & BTM_SEC_MODE4_LEVEL4)) {
BOOLEAN local_supports_sc = controller_get_interface()->supports_secure_connections();
if (!(local_supports_sc) || !(p_dev_rec->remote_supports_secure_connections)) {
BTM_TRACE_DEBUG("%s: SC only service, local_support_for_sc %d\n"
"rmt_support_for_sc : %d -> fail pairing\n", __FUNCTION__,
local_supports_sc,
p_dev_rec->remote_supports_secure_connections);
if (p_callback) {
(*p_callback) (bd_addr, transport, (void *)p_ref_data,
BTM_MODE4_LEVEL4_NOT_SUPPORTED);
}{...}
return (BTM_MODE4_LEVEL4_NOT_SUPPORTED);
}{...}
}{...}
if ( (p_dev_rec->p_callback) || (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) ) {
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("%s() - busy - PSM:%d delayed state: %s mode:%d, sm4:0x%x\n", __func__,
psm, btm_pair_state_descr(btm_cb.pairing_state), btm_cb.security_mode, p_dev_rec->sm4);/* ... */
#endif
BTM_TRACE_EVENT ("security_flags:x%x, sec_flags:x%x\n", security_required, p_dev_rec->sec_flags);
rc = BTM_CMD_STARTED;
if ((btm_cb.security_mode == BTM_SEC_MODE_UNDEFINED ||
btm_cb.security_mode == BTM_SEC_MODE_NONE ||
btm_cb.security_mode == BTM_SEC_MODE_SERVICE ||
btm_cb.security_mode == BTM_SEC_MODE_LINK) ||
(BTM_SM4_KNOWN == p_dev_rec->sm4) || (BTM_SEC_IS_SM4(p_dev_rec->sm4) &&
(btm_sec_is_upgrade_possible(p_dev_rec, is_originator) == FALSE))) {
/* ... */
if (is_originator) {
if (((security_required & BTM_SEC_OUT_FLAGS) == 0) ||
((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec))) ||
((((security_required & BTM_SEC_OUT_FLAGS) == (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT)) && btm_dev_encrypted(p_dev_rec))) ||
((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_FLAGS) && btm_dev_authorized(p_dev_rec) && btm_dev_encrypted(p_dev_rec))) ) {
rc = BTM_SUCCESS;
}{...}
}{...} else {
if (((security_required & BTM_SEC_IN_FLAGS) == 0) ||
(((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec)) ||
(((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT)) && btm_dev_encrypted(p_dev_rec)) ||
(((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHORIZE) && (btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec))) ||
(((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_AUTHORIZE)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_authenticated(p_dev_rec))) ||
(((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_ENCRYPT | BTM_SEC_IN_AUTHORIZE)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_encrypted(p_dev_rec))) ||
(((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_FLAGS) && btm_dev_encrypted(p_dev_rec) && (btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)))) {
if (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == 0) ||
(((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == BTM_SEC_IN_MIN_16_DIGIT_PIN) && btm_dev_16_digit_authenticated(p_dev_rec))) {
rc = BTM_SUCCESS;
}{...}
}{...}
}{...}
if ((rc == BTM_SUCCESS) && (security_required & BTM_SEC_MODE4_LEVEL4) &&
(p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
rc = BTM_CMD_STARTED;
}{...}
if (rc == BTM_SUCCESS) {
if (p_callback) {
(*p_callback) (bd_addr, transport, (void *)p_ref_data, BTM_SUCCESS);
}{...}
return (BTM_SUCCESS);
}{...}
}{...}
btm_cb.sec_req_pending = TRUE;
return (BTM_CMD_STARTED);
}{...}
p_dev_rec->p_cur_service = p_serv_rec;
if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
btm_cb.security_mode == BTM_SEC_MODE_SC) {
if (BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
if (is_originator) {
security_required |= (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT);
}{...} else {
chk_acp_auth_done = TRUE;
security_required |= (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT);
}{...}
}{...} else if (!(BTM_SM4_KNOWN & p_dev_rec->sm4)) {
BTM_TRACE_ERROR("%s: (%s) remote features unknown!!sec_flags:0x%02x\n", __FUNCTION__,
(is_originator) ? "initiator" : "acceptor", p_dev_rec->sec_flags);
p_dev_rec->sm4 |= BTM_SM4_REQ_PEND;
return (BTM_CMD_STARTED);
}{...}
}{...}
BTM_TRACE_DEBUG ("%s() sm4:0x%x, sec_flags:0x%x, security_required:0x%x chk:%d\n", __func__,
p_dev_rec->sm4, p_dev_rec->sec_flags, security_required, chk_acp_auth_done);
old_security_required = p_dev_rec->security_required;
old_is_originator = p_dev_rec->is_originator;
p_dev_rec->security_required = security_required;
p_dev_rec->p_ref_data = p_ref_data;
p_dev_rec->is_originator = is_originator;
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( conn_type & CONNECTION_TYPE_CONNLESS_MASK ) {
p_dev_rec->is_ucd = TRUE;
}{...} else {
p_dev_rec->is_ucd = FALSE;
}{...}
#endif/* ... */
#if (L2CAP_UCD_INCLUDED == TRUE)
if (((btm_sec_find_next_serv (p_serv_rec)) != NULL)
&& (!( conn_type & CONNECTION_TYPE_CONNLESS_MASK ))) /* ... */
#else
if ((btm_sec_find_next_serv (p_serv_rec)) != NULL)
#endif
{
BTM_TRACE_DEBUG ("no next_serv sm4:0x%x, chk:%d\n", p_dev_rec->sm4, chk_acp_auth_done);
if (!BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
BTM_TRACE_EVENT ("Security Manager: l2cap_access_req PSM:%d postponed for multiplexer\n", psm);
p_dev_rec->security_required = old_security_required;
p_dev_rec->is_originator = old_is_originator;
(*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS);
return (BTM_SUCCESS);
}{...}
}{...}
/* ... */
if (is_originator &&
((btm_cb.security_mode == BTM_SEC_MODE_UNDEFINED ||
btm_cb.security_mode == BTM_SEC_MODE_NONE ||
btm_cb.security_mode == BTM_SEC_MODE_SERVICE ||
btm_cb.security_mode == BTM_SEC_MODE_LINK) ||
!BTM_SEC_IS_SM4(p_dev_rec->sm4)) && (psm >= 0x1001)) {
BTM_TRACE_EVENT ("dynamic PSM:0x%x in legacy mode - postponed for upper layer\n", psm);
p_dev_rec->security_required = old_security_required;
p_dev_rec->is_originator = old_is_originator;
(*p_callback) (bd_addr, transport, p_ref_data, BTM_SUCCESS);
return (BTM_SUCCESS);
}{...}
if (chk_acp_auth_done) {
BTM_TRACE_DEBUG ("(SM4 to SM4) btm_sec_l2cap_access_req rspd. authenticated: x%x, enc: x%x\n",
(p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED), (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED));
/* ... */
if ((0 == (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))
|| (0 == (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED))) {
rc = BTM_DELAY_CHECK;
/* ... */
BTM_TRACE_API("%s peer should have initiated security process by now (SM4 to SM4)\n", __func__);
p_dev_rec->p_callback = p_callback;
p_dev_rec->sec_state = BTM_SEC_STATE_DELAY_FOR_ENC;
(*p_callback) (bd_addr, transport, p_ref_data, rc);
return BTM_SUCCESS;
}{...}
}{...}
p_dev_rec->p_callback = p_callback;
if (p_dev_rec->last_author_service_id == BTM_SEC_NO_LAST_SERVICE_ID
|| p_dev_rec->last_author_service_id != p_dev_rec->p_cur_service->service_id) {
/* ... */
p_dev_rec->sec_flags &= ~BTM_SEC_AUTHORIZED;
}{...}
if (BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
if ((p_dev_rec->security_required & BTM_SEC_MODE4_LEVEL4) &&
(p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
if ((p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) != 0) {
p_dev_rec->sm4 |= BTM_SM4_UPGRADE;
}{...}
p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED |
BTM_SEC_AUTHENTICATED);
BTM_TRACE_DEBUG ("%s: sec_flags:0x%x", __FUNCTION__, p_dev_rec->sec_flags);
}{...} else {
btm_sec_check_upgrade(p_dev_rec, is_originator);
}{...}
}{...}
BTM_TRACE_EVENT ("%s() PSM:%d Handle:%d State:%d Flags: 0x%x Required: 0x%x Service ID:%d\n",
__func__, psm, handle, p_dev_rec->sec_state, p_dev_rec->sec_flags,
p_dev_rec->security_required, p_dev_rec->p_cur_service->service_id);
if ((rc = btm_sec_execute_procedure (p_dev_rec)) != BTM_CMD_STARTED) {
p_dev_rec->p_callback = NULL;
(*p_callback) (bd_addr, transport, p_dev_rec->p_ref_data, (UINT8)rc);
}{...}
return (rc);/* ... */
#else
return BTM_MODE_UNSUPPORTED;
#endif
}{ ... }
/* ... */
tBTM_STATUS btm_sec_mx_access_request (BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator,
UINT32 mx_proto_id, UINT32 mx_chan_id,
tBTM_SEC_CALLBACK *p_callback, void *p_ref_data)
{
#if (SMP_INCLUDED == TRUE)
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_SEC_SERV_REC *p_serv_rec;
tBTM_STATUS rc;
UINT16 security_required;
BOOLEAN transport = FALSE;
BTM_TRACE_DEBUG ("%s() is_originator: %d\n", __func__, is_originator);
p_dev_rec = btm_find_or_alloc_dev (bd_addr);
p_serv_rec = btm_sec_find_mx_serv (is_originator, psm, mx_proto_id, mx_chan_id);
if (!p_serv_rec) {
if (p_callback) {
(*p_callback) (bd_addr, transport, p_ref_data, BTM_MODE_UNSUPPORTED);
}{...}
BTM_TRACE_ERROR ("Security Manager: MX service not found PSM:%d Proto:%d SCN:%d\n",
psm, mx_proto_id, mx_chan_id);
return BTM_NO_RESOURCES;
}{...}
if ((btm_cb.security_mode == BTM_SEC_MODE_SC) && (!btm_sec_is_serv_level0(psm))) {
security_required = btm_sec_set_serv_level4_flags (p_serv_rec->security_flags,
is_originator);
}{...} else {
security_required = p_serv_rec->security_flags;
}{...}
if ( (p_dev_rec->p_callback) || (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) ) {
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("%s() service PSM:%d Proto:%d SCN:%d delayed state: %s\n", __func__,
psm, mx_proto_id, mx_chan_id, btm_pair_state_descr(btm_cb.pairing_state));/* ... */
#endif
rc = BTM_CMD_STARTED;
if ((btm_cb.security_mode == BTM_SEC_MODE_UNDEFINED ||
btm_cb.security_mode == BTM_SEC_MODE_NONE ||
btm_cb.security_mode == BTM_SEC_MODE_SERVICE ||
btm_cb.security_mode == BTM_SEC_MODE_LINK) ||
(BTM_SM4_KNOWN == p_dev_rec->sm4) || (BTM_SEC_IS_SM4(p_dev_rec->sm4) &&
(btm_sec_is_upgrade_possible(p_dev_rec, is_originator) == FALSE))) {
/* ... */
if (is_originator) {
if (((security_required & BTM_SEC_OUT_FLAGS) == 0) ||
((((security_required & BTM_SEC_OUT_FLAGS) == BTM_SEC_OUT_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec))) ||
((((security_required & BTM_SEC_OUT_FLAGS) == (BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_OUT_ENCRYPT)) && btm_dev_encrypted(p_dev_rec)))
) {
rc = BTM_SUCCESS;
}{...}
}{...} else {
if (((security_required & BTM_SEC_IN_FLAGS) == 0) ||
((((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHENTICATE) && btm_dev_authenticated(p_dev_rec))) ||
(((security_required & BTM_SEC_IN_FLAGS) == BTM_SEC_IN_AUTHORIZE) && (btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec))) ||
(((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_AUTHENTICATE)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_authenticated(p_dev_rec))) ||
(((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHORIZE | BTM_SEC_IN_ENCRYPT)) && ((btm_dev_authorized(p_dev_rec) || btm_serv_trusted(p_dev_rec, p_serv_rec)) && btm_dev_encrypted(p_dev_rec))) ||
((((security_required & BTM_SEC_IN_FLAGS) == (BTM_SEC_IN_AUTHENTICATE | BTM_SEC_IN_ENCRYPT)) && btm_dev_encrypted(p_dev_rec)))
) {
if (((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == 0) ||
(((security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN) == BTM_SEC_IN_MIN_16_DIGIT_PIN) && btm_dev_16_digit_authenticated(p_dev_rec))) {
rc = BTM_SUCCESS;
}{...}
}{...}
}{...}
if ((rc == BTM_SUCCESS) && (security_required & BTM_SEC_MODE4_LEVEL4) &&
(p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
rc = BTM_CMD_STARTED;
}{...}
}{...}
if (rc == BTM_SUCCESS) {
BTM_TRACE_EVENT("%s: allow to bypass, checking authorization\n", __FUNCTION__);
if ((is_originator && (p_serv_rec->security_flags & BTM_SEC_OUT_AUTHORIZE)) ||
(!is_originator && (p_serv_rec->security_flags & BTM_SEC_IN_AUTHORIZE))) {
BTM_TRACE_EVENT("%s: still need authorization\n", __FUNCTION__);
rc = BTM_CMD_STARTED;
}{...}
}{...}
if (p_dev_rec->sec_state != BTM_SEC_STATE_IDLE) {
BTM_TRACE_EVENT("%s: There is a pending security procedure\n", __FUNCTION__);
rc = BTM_CMD_STARTED;
}{...}
if (rc == BTM_CMD_STARTED) {
BTM_TRACE_EVENT("%s: call btm_sec_queue_mx_request\n", __FUNCTION__);
btm_sec_queue_mx_request (bd_addr, psm, is_originator, mx_proto_id,
mx_chan_id, p_callback, p_ref_data);
}{...} else {
if (p_callback) {
(*p_callback) (bd_addr, transport, p_ref_data, (UINT8)rc);
}{...}
}{...}
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT("%s: return with rc = 0x%02x in delayed state %s\n", __FUNCTION__, rc,
btm_pair_state_descr(btm_cb.pairing_state));/* ... */
#endif
return rc;
}{...}
if ((!is_originator) && ((security_required & BTM_SEC_MODE4_LEVEL4) ||
(btm_cb.security_mode == BTM_SEC_MODE_SC))) {
BOOLEAN local_supports_sc = controller_get_interface()->supports_secure_connections();
if (!(local_supports_sc) || !(p_dev_rec->remote_supports_secure_connections)) {
BTM_TRACE_DEBUG("%s: SC only service,local_support_for_sc %d,\n"
"remote_support_for_sc %d: fail pairing\n", __FUNCTION__,
local_supports_sc, p_dev_rec->remote_supports_secure_connections);
if (p_callback) {
(*p_callback) (bd_addr, transport, (void *)p_ref_data,
BTM_MODE4_LEVEL4_NOT_SUPPORTED);
}{...}
return (BTM_MODE4_LEVEL4_NOT_SUPPORTED);
}{...}
}{...}
p_dev_rec->p_cur_service = p_serv_rec;
p_dev_rec->security_required = security_required;
if (btm_cb.security_mode == BTM_SEC_MODE_SP ||
btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
btm_cb.security_mode == BTM_SEC_MODE_SC) {
if (BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
if ((p_dev_rec->security_required & BTM_SEC_MODE4_LEVEL4) &&
(p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
if ((p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) != 0) {
p_dev_rec->sm4 |= BTM_SM4_UPGRADE;
}{...}
p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED |
BTM_SEC_AUTHENTICATED);
BTM_TRACE_DEBUG("%s: sec_flags:0x%x\n", __FUNCTION__, p_dev_rec->sec_flags);
}{...} else {
btm_sec_check_upgrade(p_dev_rec, is_originator);
}{...}
}{...}
}{...}
p_dev_rec->is_originator = is_originator;
p_dev_rec->p_callback = p_callback;
p_dev_rec->p_ref_data = p_ref_data;
p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED);
BTM_TRACE_EVENT ("%s() proto_id:%d chan_id:%d State:%d Flags:0x%x Required:0x%x Service ID:%d\n",
__func__, mx_proto_id, mx_chan_id, p_dev_rec->sec_state, p_dev_rec->sec_flags,
p_dev_rec->security_required, p_dev_rec->p_cur_service->service_id);
if ((rc = btm_sec_execute_procedure (p_dev_rec)) != BTM_CMD_STARTED) {
if (p_callback) {
p_dev_rec->p_callback = NULL;
(*p_callback) (bd_addr, transport, p_ref_data, (UINT8)rc);
}{...}
}{...}
return rc;/* ... */
#else
return BTM_MODE_UNSUPPORTED;
#endif
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_conn_req (UINT8 *bda, UINT8 *dc)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
if (!controller_get_interface()->get_is_ready()) {
BTM_TRACE_ERROR ("Security Manager: connect request when device not ready\n");
btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
return;
}{...}
if (btm_cb.connect_only_paired) {
if (!p_dev_rec || !(p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)) {
BTM_TRACE_ERROR ("Security Manager: connect request from non-paired device\n");
btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
return;
}{...}
}{...}
#if BTM_ALLOW_CONN_IF_NONDISCOVER == FALSE
if (btm_cb.btm_inq_vars.discoverable_mode == BTM_NON_DISCOVERABLE) {
if (!p_dev_rec) {
BTM_TRACE_ERROR ("Security Manager: connect request from not paired device\n");
btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
return;
}{...}
}{...}
#endif/* ... */
if ((btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
&& (!memcmp (btm_cb.pairing_bda, bda, BD_ADDR_LEN))) {
BTM_TRACE_ERROR ("Security Manager: reject connect request from bonding device\n");
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_REJECTED_CONNECT;
btsnd_hcic_reject_conn (bda, HCI_ERR_HOST_REJECT_DEVICE);
return;
}{...}
memcpy (btm_cb.connecting_bda, bda, BD_ADDR_LEN);
memcpy (btm_cb.connecting_dc, dc, DEV_CLASS_LEN);
if (l2c_link_hci_conn_req (bda)) {
if (!p_dev_rec) {
p_dev_rec = btm_sec_alloc_dev (bda);
}{...}
if (p_dev_rec) {
p_dev_rec->sm4 |= BTM_SM4_CONN_PEND;
}{...}
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_sec_bond_cancel_complete (void)
{
tBTM_SEC_DEV_REC *p_dev_rec;
if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) ||
(BTM_PAIR_STATE_WAIT_LOCAL_PIN == btm_cb.pairing_state &&
BTM_PAIR_FLAGS_WE_STARTED_DD & btm_cb.pairing_flags) ||
(btm_cb.pairing_state == BTM_PAIR_STATE_GET_REM_NAME &&
BTM_PAIR_FLAGS_WE_CANCEL_DD & btm_cb.pairing_flags)) {
/* ... */
if ((p_dev_rec = btm_find_dev (btm_cb.pairing_bda)) != NULL) {
p_dev_rec->security_required = BTM_SEC_NONE;
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if (btm_cb.api.p_bond_cancel_cmpl_callback) {
btm_cb.api.p_bond_cancel_cmpl_callback(BTM_SUCCESS);
}{...}
}{...}
}{ ... }
/* ... */#endif
/* ... */
void btm_create_conn_cancel_complete (UINT8 *p)
{
UINT8 status;
STREAM_TO_UINT8 (status, p);
/* ... */
switch (status) {
case HCI_SUCCESS:
#if (SMP_INCLUDED == TRUE)
btm_sec_bond_cancel_complete();
#endif
break;...
case HCI_ERR_CONNECTION_EXISTS:
case HCI_ERR_NO_CONNECTION:
default:
if (btm_cb.api.p_bond_cancel_cmpl_callback) {
btm_cb.api.p_bond_cancel_cmpl_callback(BTM_ERR_PROCESSING);
}{...}
break;...
}{...}
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_check_pending_reqs (void)
{
tBTM_SEC_QUEUE_ENTRY *p_e;
fixed_queue_t *bq;
if (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE) {
if (btm_cb.sec_req_pending) {
btm_cb.sec_req_pending = FALSE;
#if (CLASSIC_BT_INCLUDED == TRUE)
l2cu_resubmit_pending_sec_req (NULL);
#endif
}{...}
bq = btm_cb.sec_pending_q;
btm_cb.sec_pending_q = fixed_queue_new(QUEUE_SIZE_MAX);
while ((p_e = (tBTM_SEC_QUEUE_ENTRY *)fixed_queue_dequeue(bq, 0)) != NULL) {
if (btm_bda_to_acl(p_e->bd_addr, p_e->transport) != NULL) {
if (p_e->psm != 0) {
BTM_TRACE_EVENT("%s PSM:0x%04x Is_Orig:%u mx_proto_id:%u mx_chan_id:%u\n",
__FUNCTION__, p_e->psm, p_e->is_orig,
p_e->mx_proto_id, p_e->mx_chan_id);
btm_sec_mx_access_request (p_e->bd_addr, p_e->psm, p_e->is_orig,
p_e->mx_proto_id, p_e->mx_chan_id,
p_e->p_callback, p_e->p_ref_data);
}{...} else {
BTM_SetEncryption(p_e->bd_addr, p_e->transport, p_e->p_callback,
p_e->p_ref_data);
}{...}
}{...}
osi_free (p_e);
}{...}
fixed_queue_free(bq, NULL);
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_init (UINT8 sec_mode)
{
btm_cb.security_mode = sec_mode;
memset (btm_cb.pairing_bda, 0xff, BD_ADDR_LEN);
btm_cb.max_collision_delay = BTM_SEC_MAX_COLLISION_DELAY;
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_device_down (void)
{
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("%s() State: %s\n", __func__, btm_pair_state_descr(btm_cb.pairing_state));
#endif
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
}{ ... }
/* ... */#endif
/* ... */
void btm_sec_dev_reset (void)
{
if (controller_get_interface()->supports_simple_pairing()) {
btm_cb.devcb.loc_io_caps = BTM_LOCAL_IO_CAPS;
BTM_SetSecurityLevel(FALSE, "RFC_MUX\n", BTM_SEC_SERVICE_RFC_MUX,
BTM_SEC_NONE, BT_PSM_RFCOMM, BTM_SEC_PROTO_RFCOMM, 0);
}{...} else {
btm_cb.security_mode = BTM_SEC_MODE_SERVICE;
}{...}
BTM_TRACE_DEBUG ("btm_sec_dev_reset sec mode: %d\n", btm_cb.security_mode);
}{ ... }
/* ... */
void btm_sec_abort_access_req (BD_ADDR bd_addr)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
if (!p_dev_rec) {
return;
}{...}
if ((p_dev_rec->sec_state != BTM_SEC_STATE_AUTHORIZING)
&& (p_dev_rec->sec_state != BTM_SEC_STATE_AUTHENTICATING)) {
return;
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
p_dev_rec->p_callback = NULL;
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
static tBTM_STATUS btm_sec_dd_create_conn (tBTM_SEC_DEV_REC *p_dev_rec)
{
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR);
if (p_lcb && (p_lcb->link_state == LST_CONNECTED || p_lcb->link_state == LST_CONNECTING)) {
BTM_TRACE_WARNING("%s Connection already exists\n", __func__);
return BTM_CMD_STARTED;
}{...}
if (!p_lcb && (p_lcb = l2cu_allocate_lcb (p_dev_rec->bd_addr, TRUE, BT_TRANSPORT_BR_EDR)) == NULL) {
BTM_TRACE_WARNING ("Security Manager: failed allocate LCB [%02x%02x%02x%02x%02x%02x]\n",
p_dev_rec->bd_addr[0], p_dev_rec->bd_addr[1], p_dev_rec->bd_addr[2],
p_dev_rec->bd_addr[3], p_dev_rec->bd_addr[4], p_dev_rec->bd_addr[5]);
return (BTM_NO_RESOURCES);
}{...}
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_DISC_WHEN_DONE;
if (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == FALSE) {
BTM_TRACE_WARNING ("Security Manager: failed create [%02x%02x%02x%02x%02x%02x]\n",
p_dev_rec->bd_addr[0], p_dev_rec->bd_addr[1], p_dev_rec->bd_addr[2],
p_dev_rec->bd_addr[3], p_dev_rec->bd_addr[4], p_dev_rec->bd_addr[5]);
l2cu_release_lcb(p_lcb);
return (BTM_NO_RESOURCES);
}{...}
btm_acl_update_busy_level (BTM_BLI_PAGE_EVT);
BTM_TRACE_DEBUG ("Security Manager: btm_sec_dd_create_conn [%02x%02x%02x%02x%02x%02x]\n",
p_dev_rec->bd_addr[0], p_dev_rec->bd_addr[1], p_dev_rec->bd_addr[2],
p_dev_rec->bd_addr[3], p_dev_rec->bd_addr[4], p_dev_rec->bd_addr[5]);
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_PIN_REQ);
return (BTM_CMD_STARTED);
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_rmt_name_request_complete (UINT8 *p_bd_addr, UINT8 *p_bd_name, UINT8 status)
{
tBTM_SEC_DEV_REC *p_dev_rec = NULL;
int i;
DEV_CLASS dev_class;
UINT8 old_sec_state;
UINT8 res;
BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete\n");
if (((p_bd_addr == NULL) && !BTM_ACL_IS_CONNECTED(btm_cb.connecting_bda))
|| ((p_bd_addr != NULL) && !BTM_ACL_IS_CONNECTED(p_bd_addr))) {
btm_acl_resubmit_page();
}{...}
if (p_bd_addr) {
p_dev_rec = btm_find_dev (p_bd_addr);
}{...} else {
list_node_t *p_node = NULL;
for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
p_dev_rec = list_node(p_node);
if ((p_dev_rec->sec_flags & BTM_SEC_IN_USE)
&& (p_dev_rec->sec_state == BTM_SEC_STATE_GETTING_NAME)) {
p_bd_addr = p_dev_rec->bd_addr;
break;
}{...}
}{...}
if (!p_bd_addr) {
p_dev_rec = NULL;
}{...}
}{...}
/* ... */
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
if (!p_bd_name) {
p_bd_name = (UINT8 *)"";
}{...}
if (p_dev_rec) {
BTM_TRACE_EVENT ("Security Manager: rmt_name_complete PairState: %s RemName: %s status: %d State:%d p_dev_rec: %p \n",
btm_pair_state_descr (btm_cb.pairing_state), p_bd_name,
status, p_dev_rec->sec_state, p_dev_rec);
}{...} else {
BTM_TRACE_EVENT ("Security Manager: rmt_name_complete PairState: %s RemName: %s status: %d\n",
btm_pair_state_descr (btm_cb.pairing_state), p_bd_name,
status);
}{...}
#endif/* ... */
if (p_dev_rec) {
old_sec_state = p_dev_rec->sec_state;
if (status == HCI_SUCCESS) {
BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, (char *)p_bd_name, BTM_MAX_REM_BD_NAME_LEN);
p_dev_rec->sec_bd_name[BTM_MAX_REM_BD_NAME_LEN] = '\0';
p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
BTM_TRACE_EVENT ("setting BTM_SEC_NAME_KNOWN sec_flags:0x%x\n", p_dev_rec->sec_flags);
}{...} else {
p_dev_rec->sec_bd_name[0] = '\0';
}{...}
if (p_dev_rec->sec_state == BTM_SEC_STATE_GETTING_NAME) {
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
}{...}
for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
if (btm_cb.p_rmt_name_callback[i] && p_bd_addr) {
(*btm_cb.p_rmt_name_callback[i])(p_bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name);
}{...}
}{...}
}{...} else {
dev_class[0] = 0;
dev_class[1] = 0;
dev_class[2] = 0;
for (i = 0; i < BTM_SEC_MAX_RMT_NAME_CALLBACKS; i++) {
if (btm_cb.p_rmt_name_callback[i] && p_bd_addr) {
(*btm_cb.p_rmt_name_callback[i])(p_bd_addr, dev_class, (UINT8 *)"");
}{...}
}{...}
return;
}{...}
if ( (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_LOCAL_PIN) && p_bd_addr
&& (memcmp (btm_cb.pairing_bda, p_bd_addr, BD_ADDR_LEN) == 0) ) {
BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete() delayed pin now being requested flags:0x%x, (p_pin_callback=%p)\n", btm_cb.pairing_flags, btm_cb.api.p_pin_callback);
if (((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) == 0) &&
((btm_cb.pairing_flags & BTM_PAIR_FLAGS_PIN_REQD) == 0) &&
btm_cb.api.p_pin_callback) {
BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete() calling pin_callback\n");
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PIN_REQD;
(*btm_cb.api.p_pin_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class, p_bd_name,
(p_dev_rec->p_cur_service == NULL) ? FALSE
: (p_dev_rec->p_cur_service->security_flags & BTM_SEC_IN_MIN_16_DIGIT_PIN));
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_PIN);
return;
}{...}
if ( btm_cb.pairing_state == BTM_PAIR_STATE_GET_REM_NAME) {
if (p_bd_addr && memcmp (btm_cb.pairing_bda, p_bd_addr, BD_ADDR_LEN) == 0) {
BTM_TRACE_EVENT ("btm_sec_rmt_name_request_complete() continue bonding sm4: 0x%04x, status:0x%x\n", p_dev_rec->sm4, status);
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_CANCEL_DD) {
btm_sec_bond_cancel_complete();
return;
}{...}
if (status != HCI_SUCCESS) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if (btm_cb.api.p_auth_complete_callback)
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, status);
return;
}{...}
if (BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
BTM_TRACE_DEBUG ("btm_sec_rmt_name_request_complete IS_SM4_UNKNOWN Flags:0x%04x\n",
btm_cb.pairing_flags);
if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT) == 0) {
p_dev_rec->sm4 |= BTM_SM4_KNOWN;
}{...}
}{...}
BTM_TRACE_DEBUG("%s, SM4 Value: %x, Legacy:%d,IS SM4:%d, Unknown:%d\n", __FUNCTION__,
p_dev_rec->sm4, BTM_SEC_IS_SM4_LEGACY(p_dev_rec->sm4),
BTM_SEC_IS_SM4(p_dev_rec->sm4), BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4));
/* ... */
if ((p_dev_rec->sm4 != BTM_SM4_KNOWN) || !btm_sec_check_prefetch_pin(p_dev_rec)) {
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT) {
BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete: waiting HCI_Connection_Complete after rejecting connection\n");
}{...}
else if (btm_sec_dd_create_conn(p_dev_rec) != BTM_CMD_STARTED) {
BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete: failed to start connection\n");
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if (btm_cb.api.p_auth_complete_callback) {
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_ERR_MEMORY_FULL);
}{...}
}{...}
}{...}
return;
}{...} else {
BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete: wrong BDA, retry with pairing BDA\n");
BTM_ReadRemoteDeviceName (btm_cb.pairing_bda, NULL, BT_TRANSPORT_BR_EDR);
return;
}{...}
}{...}
if (p_dev_rec->link_key_not_sent) {
if (p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE) {
return;
}{...}
p_dev_rec->link_key_not_sent = FALSE;
btm_send_link_key_notif(p_dev_rec);
if (!(p_dev_rec->security_required & BTM_SEC_OUT_AUTHENTICATE)) {
if (btm_cb.api.p_auth_complete_callback) {
res = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_SUCCESS);
if (res == BTM_SEC_DEV_REC_REMOVED) {
p_dev_rec = NULL;
}{...}
}{...}
}{...}
}{...}
if(!p_dev_rec) {
return;
}{...}
if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
&& (p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED)) {
BTM_TRACE_WARNING ("btm_sec_rmt_name_request_complete (none/ce)\n");
p_dev_rec->security_required &= ~(BTM_SEC_OUT_AUTHENTICATE);
l2cu_start_post_bond_timer(p_dev_rec->hci_handle);
return;
}{...}
if (old_sec_state != BTM_SEC_STATE_GETTING_NAME) {
return;
}{...}
if (status != HCI_SUCCESS) {
btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
return;
}{...}
if (p_dev_rec->sm4 & BTM_SM4_REQ_PEND) {
BTM_TRACE_EVENT ("waiting for remote features!!\n");
return;
}{...}
status = (UINT8)btm_sec_execute_procedure (p_dev_rec);
if (status == BTM_CMD_STARTED) {
return;
}{...}
btm_sec_dev_rec_cback_event (p_dev_rec, status, FALSE);
}{ ... }
/* ... */#endif
/* ... */
void btm_sec_rmt_host_support_feat_evt (UINT8 *p)
{
tBTM_SEC_DEV_REC *p_dev_rec;
BD_ADDR bd_addr;
BD_FEATURES features;
STREAM_TO_BDADDR (bd_addr, p);
p_dev_rec = btm_find_or_alloc_dev (bd_addr);
BTM_TRACE_EVENT ("btm_sec_rmt_host_support_feat_evt sm4: 0x%x p[0]: 0x%x\n", p_dev_rec->sm4, p[0]);
if (BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
p_dev_rec->sm4 = BTM_SM4_KNOWN;
STREAM_TO_ARRAY(features, p, HCI_FEATURE_BYTES_PER_PAGE);
if (HCI_SSP_HOST_SUPPORTED(features)) {
p_dev_rec->sm4 = BTM_SM4_TRUE;
}{...}
BTM_TRACE_EVENT ("btm_sec_rmt_host_support_feat_evt sm4: 0x%x features[0]: 0x%x\n", p_dev_rec->sm4, features[0]);
}{...}
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_io_capabilities_req (UINT8 *p)
{
tBTM_SP_IO_REQ evt_data;
UINT8 err_code = 0;
tBTM_SEC_DEV_REC *p_dev_rec;
BOOLEAN is_orig = TRUE;
UINT8 callback_rc = BTM_SUCCESS;
STREAM_TO_BDADDR (evt_data.bd_addr, p);
/* ... */
evt_data.io_cap = btm_cb.devcb.loc_io_caps;
evt_data.oob_data = BTM_OOB_NONE;
evt_data.auth_req = BTM_DEFAULT_AUTH_REQ;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT("%s: State: %s\n", __FUNCTION__, btm_pair_state_descr(btm_cb.pairing_state));
#endif
p_dev_rec = btm_find_or_alloc_dev (evt_data.bd_addr);
BTM_TRACE_DEBUG("%s:Security mode: %d, Num Read Remote Feat pages: %d\n", __FUNCTION__,
btm_cb.security_mode, p_dev_rec->num_read_pages);
if ((btm_cb.security_mode == BTM_SEC_MODE_SC) && (p_dev_rec->num_read_pages == 0)) {
BTM_TRACE_EVENT("%s: Device security mode is SC only.\n"
"To continue need to know remote features.\n", __FUNCTION__);
p_dev_rec->remote_features_needed = TRUE;
return;
}{...}
p_dev_rec->sm4 |= BTM_SM4_TRUE;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT("%s: State: %s Flags: 0x%04x p_cur_service: %p\n",
__FUNCTION__, btm_pair_state_descr(btm_cb.pairing_state),
btm_cb.pairing_flags, p_dev_rec->p_cur_service);/* ... */
#endif
if (p_dev_rec->p_cur_service) {
BTM_TRACE_EVENT("%s: cur_service psm: 0x%04x, security_flags: 0x%04x\n",
__FUNCTION__, p_dev_rec->p_cur_service->psm,
p_dev_rec->p_cur_service->security_flags);
}{...}
switch (btm_cb.pairing_state) {
case BTM_PAIR_STATE_IDLE:
break;
...
case BTM_PAIR_STATE_INCOMING_SSP:
is_orig = FALSE;
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_PEER_STARTED_DD) {
evt_data.auth_req = BTM_DEFAULT_DD_AUTH_REQ;
}{...}
break;
/* ... */
... case BTM_PAIR_STATE_WAIT_PIN_REQ:
if (!memcmp (evt_data.bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN)) {
evt_data.auth_req = BTM_DEFAULT_DD_AUTH_REQ;
}{...} else {
err_code = HCI_ERR_HOST_BUSY_PAIRING;
}{...}
break;
...
default:
err_code = HCI_ERR_HOST_BUSY_PAIRING;
BTM_TRACE_ERROR("%s: Unexpected Pairing state received %d\n", __FUNCTION__,
btm_cb.pairing_state);
break;...
}{...}
if (btm_cb.pairing_disabled) {
BTM_TRACE_DEBUG("%s: Pairing is not allowed -> fail pairing.\n", __FUNCTION__);
err_code = HCI_ERR_PAIRING_NOT_ALLOWED;
}{...} else if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
BOOLEAN local_supports_sc = controller_get_interface()->supports_secure_connections();
if (!(local_supports_sc) || !(p_dev_rec->remote_supports_secure_connections)) {
BTM_TRACE_DEBUG("%s: SC only service, local_support_for_sc %d,\n"
" remote_support_for_sc 0x%02x -> fail pairing\n", __FUNCTION__,
local_supports_sc, p_dev_rec->remote_supports_secure_connections);
err_code = HCI_ERR_PAIRING_NOT_ALLOWED;
}{...}
}{...}
if (err_code != 0) {
/* ... */
btsnd_hcic_io_cap_req_neg_reply(evt_data.bd_addr, err_code);
return;
}{...}
evt_data.is_orig = is_orig;
if (is_orig) {
if (!(btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) &&
p_dev_rec->p_cur_service &&
(p_dev_rec->p_cur_service->security_flags & BTM_SEC_OUT_AUTHENTICATE)) {
if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
evt_data.auth_req = BTM_AUTH_SP_YES;
}{...} else {
evt_data.auth_req = (p_dev_rec->p_cur_service->security_flags &
BTM_SEC_OUT_MITM) ? BTM_AUTH_SP_YES : BTM_AUTH_SP_NO;
}{...}
}{...}
}{...}
l2c_pin_code_request (evt_data.bd_addr);
memcpy (btm_cb.pairing_bda, evt_data.bd_addr, BD_ADDR_LEN);
/* ... */
if (!memcmp (evt_data.bd_addr, btm_cb.connecting_bda, BD_ADDR_LEN)) {
memcpy (p_dev_rec->dev_class, btm_cb.connecting_dc, DEV_CLASS_LEN);
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS);
callback_rc = BTM_SUCCESS;
if (p_dev_rec->sm4 & BTM_SM4_UPGRADE) {
p_dev_rec->sm4 &= ~BTM_SM4_UPGRADE;
evt_data.auth_req = BTM_AUTH_SPGB_YES;
}{...} else if (btm_cb.api.p_sp_callback) {
callback_rc = (*btm_cb.api.p_sp_callback) (BTM_SP_IO_REQ_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
}{...}
#if BTM_OOB_INCLUDED == TRUE
if ((callback_rc == BTM_SUCCESS) || (BTM_OOB_UNKNOWN != evt_data.oob_data))
#else
if (callback_rc == BTM_SUCCESS)
#endif
{
if ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)) {
evt_data.auth_req = (BTM_AUTH_DD_BOND | (evt_data.auth_req & BTM_AUTH_YN_BIT));
}{...}
if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
evt_data.auth_req |= BTM_AUTH_YN_BIT;
BTM_TRACE_DEBUG("%s: for device in \"SC only\" mode set auth_req to 0x%02x\n",
__FUNCTION__, evt_data.auth_req);
}{...}
btm_cb.devcb.loc_auth_req = evt_data.auth_req;
btm_cb.devcb.loc_io_caps = evt_data.io_cap;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT("%s: State: %s IO_CAP:%d oob_data:%d auth_req:%d",
__FUNCTION__, btm_pair_state_descr(btm_cb.pairing_state), evt_data.io_cap,
evt_data.oob_data, evt_data.auth_req);/* ... */
#endif
btsnd_hcic_io_cap_req_reply(evt_data.bd_addr, evt_data.io_cap,
evt_data.oob_data, evt_data.auth_req);
}{...}
}{ ... }
/* ... */
void btm_io_capabilities_rsp (UINT8 *p)
{
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_SP_IO_RSP evt_data;
STREAM_TO_BDADDR (evt_data.bd_addr, p);
STREAM_TO_UINT8 (evt_data.io_cap, p);
STREAM_TO_UINT8 (evt_data.oob_data, p);
STREAM_TO_UINT8 (evt_data.auth_req, p);
p_dev_rec = btm_find_or_alloc_dev (evt_data.bd_addr);
if (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE) {
memcpy (btm_cb.pairing_bda, evt_data.bd_addr, BD_ADDR_LEN);
btm_sec_change_pairing_state (BTM_PAIR_STATE_INCOMING_SSP);
BTM_SEC_CLR_TRUSTED_DEVICE(p_dev_rec->trusted_mask);
btm_inq_stop_on_ssp();
}{...}
l2c_pin_code_request (evt_data.bd_addr);
/* ... */
/* ... */
if (!memcmp (evt_data.bd_addr, btm_cb.connecting_bda, BD_ADDR_LEN)) {
memcpy (p_dev_rec->dev_class, btm_cb.connecting_dc, DEV_CLASS_LEN);
}{...}
if (btm_cb.pairing_state == BTM_PAIR_STATE_INCOMING_SSP
&& (evt_data.auth_req & BTM_AUTH_DD_BOND) ) {
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PEER_STARTED_DD;
}{...}
p_dev_rec->rmt_io_caps = evt_data.io_cap;
p_dev_rec->rmt_auth_req = evt_data.auth_req;
if (btm_cb.api.p_sp_callback) {
(*btm_cb.api.p_sp_callback) (BTM_SP_IO_RSP_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
}{...}
}{ ... }
/* ... */
#if (CLASSIC_BT_INCLUDED == TRUE)
void btm_proc_sp_req_evt (tBTM_SP_EVT event, UINT8 *p)
{
tBTM_STATUS status = BTM_ERR_PROCESSING;
tBTM_SP_EVT_DATA evt_data;
UINT8 *p_bda = evt_data.cfm_req.bd_addr;
tBTM_SEC_DEV_REC *p_dev_rec;
STREAM_TO_BDADDR (p_bda, p);
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("btm_proc_sp_req_evt() BDA: %08x%04x event: 0x%x, State: %s\n",
(p_bda[0] << 24) + (p_bda[1] << 16) + (p_bda[2] << 8) + p_bda[3], (p_bda[4] << 8) + p_bda[5],
event, btm_pair_state_descr(btm_cb.pairing_state));/* ... */
#endif
if ( ((p_dev_rec = btm_find_dev (p_bda)) != NULL)
&& (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (memcmp (btm_cb.pairing_bda, p_bda, BD_ADDR_LEN) == 0) ) {
memcpy (evt_data.cfm_req.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
memcpy (evt_data.cfm_req.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN);
BCM_STRCPY_S ((char *)evt_data.cfm_req.bd_name,(char *)p_dev_rec->sec_bd_name);
switch (event) {
case BTM_SP_CFM_REQ_EVT:
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM);
STREAM_TO_UINT32 (evt_data.cfm_req.num_val, p);
evt_data.cfm_req.just_works = TRUE;
if ( (p_dev_rec->rmt_io_caps == BTM_IO_CAP_IO)
&& (btm_cb.devcb.loc_io_caps == BTM_IO_CAP_IO)
&& ((p_dev_rec->rmt_auth_req & BTM_AUTH_SP_YES) || (btm_cb.devcb.loc_auth_req & BTM_AUTH_SP_YES)) ) {
/* ... */
evt_data.cfm_req.just_works = FALSE;
}{...}
BTM_TRACE_DEBUG ("btm_proc_sp_req_evt() just_works:%d, io loc:%d, rmt:%d, auth loc:%d, rmt:%d\n",
evt_data.cfm_req.just_works, btm_cb.devcb.loc_io_caps, p_dev_rec->rmt_io_caps,
btm_cb.devcb.loc_auth_req, p_dev_rec->rmt_auth_req);
evt_data.cfm_req.loc_auth_req = btm_cb.devcb.loc_auth_req;
evt_data.cfm_req.rmt_auth_req = p_dev_rec->rmt_auth_req;
evt_data.cfm_req.loc_io_caps = btm_cb.devcb.loc_io_caps;
evt_data.cfm_req.rmt_io_caps = p_dev_rec->rmt_io_caps;
break;
...
case BTM_SP_KEY_NOTIF_EVT:
STREAM_TO_UINT32 (evt_data.key_notif.passkey, p);
BTM_TRACE_DEBUG ("BTM_SP_KEY_NOTIF_EVT: passkey: %u\n", evt_data.key_notif.passkey);
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
break;
...
case BTM_SP_KEY_REQ_EVT:
btm_sec_change_pairing_state (BTM_PAIR_STATE_KEY_ENTRY);
break;...
}{...}
if (btm_cb.api.p_sp_callback) {
status = (*btm_cb.api.p_sp_callback) (event, (tBTM_SP_EVT_DATA *)&evt_data);
if (status != BTM_NOT_AUTHORIZED) {
return;
}{...}
}{...} else if ( (event == BTM_SP_CFM_REQ_EVT) && (evt_data.cfm_req.just_works == TRUE) ) {
status = BTM_SUCCESS;
}{...}
if (event == BTM_SP_CFM_REQ_EVT) {
BTM_TRACE_DEBUG ("calling BTM_ConfirmReqReply with status: %d\n", status);
BTM_ConfirmReqReply (status, p_bda);
}{...} else if (event == BTM_SP_KEY_REQ_EVT) {
BTM_PasskeyReqReply(status, p_bda, 0);
}{...}
return;
}{...}
btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
if (BTM_SP_CFM_REQ_EVT == event) {
btsnd_hcic_user_conf_reply (p_bda, FALSE);
}{...} else if (BTM_SP_KEY_NOTIF_EVT == event) {
/* ... */
if (NULL != (p_dev_rec = btm_find_dev (p_bda)) ) {
btm_sec_disconnect (p_dev_rec->hci_handle, HCI_ERR_AUTH_FAILURE);
}{...}
}{...} else {
btsnd_hcic_user_passkey_neg_reply(p_bda);
}{...}
}{...}
/* ... */
void btm_keypress_notif_evt (UINT8 *p)
{
tBTM_SP_KEYPRESS evt_data;
UINT8 *p_bda;
if (btm_cb.api.p_sp_callback) {
p_bda = evt_data.bd_addr;
STREAM_TO_BDADDR (p_bda, p);
evt_data.notif_type = *p;
(*btm_cb.api.p_sp_callback) (BTM_SP_KEYPRESS_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
}{...}
}{...}
/* ... */
void btm_simple_pair_complete (UINT8 *p)
{
tBTM_SP_COMPLT evt_data;
tBTM_SEC_DEV_REC *p_dev_rec;
UINT8 status;
BOOLEAN disc = FALSE;
status = *p++;
STREAM_TO_BDADDR (evt_data.bd_addr, p);
if ((p_dev_rec = btm_find_dev (evt_data.bd_addr)) == NULL) {
BTM_TRACE_ERROR ("btm_simple_pair_complete() with unknown BDA: %08x%04x\n",
(evt_data.bd_addr[0] << 24) + (evt_data.bd_addr[1] << 16) + (evt_data.bd_addr[2] << 8) + evt_data.bd_addr[3],
(evt_data.bd_addr[4] << 8) + evt_data.bd_addr[5]);
return;
}{...}
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("btm_simple_pair_complete() Pair State: %s Status:%d sec_state: %u\n",
btm_pair_state_descr(btm_cb.pairing_state), status, p_dev_rec->sec_state);/* ... */
#endif
evt_data.status = BTM_ERR_PROCESSING;
if (status == HCI_SUCCESS) {
evt_data.status = BTM_SUCCESS;
p_dev_rec->sec_flags |= BTM_SEC_AUTHENTICATED;
}{...} else {
if (status == HCI_ERR_PAIRING_NOT_ALLOWED) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_DISCONNECT);
btu_start_timer (&btm_cb.pairing_tle, BTU_TTYPE_USER_FUNC, BT_1SEC_TIMEOUT);
}{...} else if (memcmp (btm_cb.pairing_bda, evt_data.bd_addr, BD_ADDR_LEN) == 0) {
btu_stop_timer (&btm_cb.pairing_tle);
if (p_dev_rec->sec_state != BTM_SEC_STATE_AUTHENTICATING) {
disc = TRUE;
}{...}
}{...} else {
disc = TRUE;
}{...}
}{...}
memcpy (evt_data.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
memcpy (evt_data.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN);
if (btm_cb.api.p_sp_callback) {
(*btm_cb.api.p_sp_callback) (BTM_SP_COMPLT_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
}{...}
if (disc) {
if ((status != HCI_ERR_PEER_USER) && (status != HCI_ERR_CONN_CAUSE_LOCAL_HOST)) {
btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
}{...}
}{...}
}{...}
/* ... */#endif /* ... */
#endif
#if BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE
/* ... */
void btm_rem_oob_req (UINT8 *p)
{
UINT8 *p_bda;
tBTM_SP_RMT_OOB evt_data;
tBTM_SEC_DEV_REC *p_dev_rec;
BT_OCTET16 c;
BT_OCTET16 r;
p_bda = evt_data.bd_addr;
STREAM_TO_BDADDR (p_bda, p);
BTM_TRACE_EVENT ("btm_rem_oob_req() BDA: %02x:%02x:%02x:%02x:%02x:%02x\n",
p_bda[0], p_bda[1], p_bda[2], p_bda[3], p_bda[4], p_bda[5]);
if ( (NULL != (p_dev_rec = btm_find_dev (p_bda))) &&
btm_cb.api.p_sp_callback) {
memcpy (evt_data.bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
memcpy (evt_data.dev_class, p_dev_rec->dev_class, DEV_CLASS_LEN);
BCM_STRNCPY_S((char *)evt_data.bd_name, (char *)p_dev_rec->sec_bd_name, BTM_MAX_REM_BD_NAME_LEN);
evt_data.bd_name[BTM_MAX_REM_BD_NAME_LEN] = '\0';
btm_sec_change_pairing_state(BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP);
if ((*btm_cb.api.p_sp_callback) (BTM_SP_RMT_OOB_EVT, (tBTM_SP_EVT_DATA *)&evt_data) == BTM_NOT_AUTHORIZED) {
BTM_RemoteOobDataReply(TRUE, p_bda, c, r);
}{...}
return;
}{...}
btm_cb.acl_disc_reason = HCI_ERR_HOST_REJECT_SECURITY;
btsnd_hcic_rem_oob_neg_reply (p_bda);
}{ ... }
/* ... */
void btm_read_local_oob_complete (UINT8 *p)
{
tBTM_SP_LOC_OOB evt_data;
UINT8 status = *p++;
BTM_TRACE_EVENT ("btm_read_local_oob_complete:%d\n", status);
if (status == HCI_SUCCESS) {
evt_data.status = BTM_SUCCESS;
STREAM_TO_ARRAY16(evt_data.c, p);
STREAM_TO_ARRAY16(evt_data.r, p);
}{...} else {
evt_data.status = BTM_ERR_PROCESSING;
}{...}
if (btm_cb.api.p_sp_callback) {
(*btm_cb.api.p_sp_callback) (BTM_SP_LOC_OOB_EVT, (tBTM_SP_EVT_DATA *)&evt_data);
}{...}
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_sec_auth_collision (UINT16 handle)
{
tBTM_SEC_DEV_REC *p_dev_rec;
if (!btm_cb.collision_start_time) {
btm_cb.collision_start_time = osi_time_get_os_boottime_ms();
}{...}
if ((osi_time_get_os_boottime_ms() - btm_cb.collision_start_time) < btm_cb.max_collision_delay)
{
if (handle == BTM_SEC_INVALID_HANDLE)
{
if ((p_dev_rec = btm_sec_find_dev_by_sec_state (BTM_SEC_STATE_AUTHENTICATING)) == NULL) {
p_dev_rec = btm_sec_find_dev_by_sec_state (BTM_SEC_STATE_ENCRYPTING);
}{...}
}{...} else {
p_dev_rec = btm_find_dev_by_handle (handle);
}{...}
if (p_dev_rec != NULL) {
BTM_TRACE_DEBUG ("btm_sec_auth_collision: state %d (retrying in a moment...)\n", p_dev_rec->sec_state);
if (p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING || p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING) {
p_dev_rec->sec_state = 0;
}{...}
btm_cb.p_collided_dev_rec = p_dev_rec;
btm_cb.sec_collision_tle.param = (UINT32) btm_sec_collision_timeout;
btu_start_timer (&btm_cb.sec_collision_tle, BTU_TTYPE_USER_FUNC, BT_1SEC_TIMEOUT);
}{...}
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_auth_complete (UINT16 handle, UINT8 status)
{
UINT8 res;
UINT8 old_sm4;
tBTM_PAIRING_STATE old_state = btm_cb.pairing_state;
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
BOOLEAN are_bonding = FALSE;
/* ... */
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
if (p_dev_rec) {
BTM_TRACE_EVENT ("Security Manager: auth_complete PairState: %s handle:%u status:%d dev->sec_state: %u Bda:%08x, RName:%s\n",
btm_pair_state_descr (btm_cb.pairing_state),
handle, status,
p_dev_rec->sec_state,
(p_dev_rec->bd_addr[2] << 24) + (p_dev_rec->bd_addr[3] << 16) + (p_dev_rec->bd_addr[4] << 8) + p_dev_rec->bd_addr[5],
p_dev_rec->sec_bd_name);
}{...} else {
BTM_TRACE_EVENT ("Security Manager: auth_complete PairState: %s handle:%u status:%d\n",
btm_pair_state_descr (btm_cb.pairing_state),
handle, status);
}{...}
#endif/* ... */
if ((status == HCI_ERR_LMP_ERR_TRANS_COLLISION) || (status == HCI_ERR_DIFF_TRANSACTION_COLLISION)) {
btm_sec_auth_collision(handle);
return;
}{...}
btm_cb.collision_start_time = 0;
btm_restore_mode();
/* ... */
if (p_dev_rec
&& (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
&& !(btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE)) {
p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
l2cu_start_post_bond_timer (p_dev_rec->hci_handle);
}{...}
if (!p_dev_rec) {
return;
}{...}
old_sm4 = p_dev_rec->sm4;
p_dev_rec->sm4 &= ~BTM_SM4_RETRY;
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD)
&& (memcmp (p_dev_rec->bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) ) {
are_bonding = TRUE;
}{...}
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (memcmp (p_dev_rec->bd_addr, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) ) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
}{...}
if (p_dev_rec->sec_state != BTM_SEC_STATE_AUTHENTICATING) {
if ( (btm_cb.api.p_auth_complete_callback && status != HCI_SUCCESS)
&& (old_state != BTM_PAIR_STATE_IDLE) ) {
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, status);
}{...}
return;
}{...}
/* ... */
if ((status == HCI_ERR_COMMAND_DISALLOWED)
&& ((p_dev_rec->sec_flags & (BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED)) ==
(BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED))) {
status = HCI_SUCCESS;
}{...}
if (btm_cb.api.p_auth_complete_callback) {
if (old_state != BTM_PAIR_STATE_IDLE) {
res = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, status);
if (res == BTM_SEC_DEV_REC_REMOVED) {
p_dev_rec = NULL;
}{...}
}{...}
}{...}
if(!p_dev_rec) {
return;
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
#if (CLASSIC_BT_INCLUDED == TRUE)
btm_sec_update_legacy_auth_state(btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR), BTM_ACL_LEGACY_AUTH_SELF);
#endif
if (are_bonding) {
p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
if (status != HCI_SUCCESS) {
if (((status != HCI_ERR_PEER_USER) && (status != HCI_ERR_CONN_CAUSE_LOCAL_HOST))) {
btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_PEER_USER, p_dev_rec->hci_handle);
}{...}
}{...} else {
BTM_TRACE_DEBUG ("TRYING TO DECIDE IF CAN USE SMP_BR_CHNL\n");
if (p_dev_rec->new_encryption_key_is_p256 && (btm_sec_use_smp_br_chnl(p_dev_rec))
&& (!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) ||
(!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED) &&
(p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)))) {
BTM_TRACE_DEBUG ("link encrypted after dedic bonding can use SMP_BR_CHNL\n");
if (btm_sec_is_master(p_dev_rec)) {
BTM_SetEncryption(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR, NULL, NULL);
}{...}
}{...}
l2cu_start_post_bond_timer (p_dev_rec->hci_handle);
}{...}
return;
}{...}
if (status != HCI_SUCCESS) {
if ((old_sm4 & BTM_SM4_RETRY) == 0) {
if (status == HCI_ERR_LMP_ERR_TRANS_COLLISION) {
p_dev_rec->sm4 |= BTM_SM4_RETRY;
BTM_TRACE_DEBUG ("Collision retry sm4:x%x sec_flags:0x%x\n", p_dev_rec->sm4, p_dev_rec->sec_flags);
}{...}
/* ... */
else if (HCI_ERR_KEY_MISSING == status && BTM_SEC_IS_SM4(p_dev_rec->sm4)) {
p_dev_rec->sm4 |= BTM_SM4_RETRY;
p_dev_rec->sec_flags &= ~BTM_SEC_LINK_KEY_KNOWN;
BTM_TRACE_DEBUG ("Retry for missing key sm4:x%x sec_flags:0x%x\n", p_dev_rec->sm4, p_dev_rec->sec_flags);
/* ... */
}{...}
if (p_dev_rec->sm4 & BTM_SM4_RETRY) {
btm_sec_execute_procedure (p_dev_rec);
return;
}{...}
}{...}
btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_DISC_WHEN_DONE) {
btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
}{...}
return;
}{...}
p_dev_rec->sec_flags |= BTM_SEC_AUTHENTICATED;
if (p_dev_rec->pin_code_length >= 16 ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
}{...}
status = btm_sec_execute_procedure (p_dev_rec);
if (status != BTM_CMD_STARTED) {
btm_sec_dev_rec_cback_event (p_dev_rec, status, FALSE);
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_encrypt_change (UINT16 handle, UINT8 status, UINT8 encr_enable)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
tACL_CONN *p_acl = NULL;
#endif
BTM_TRACE_EVENT ("Security Manager: encrypt_change status:%d State:%d, encr_enable = %d\n",
status, (p_dev_rec) ? p_dev_rec->sec_state : 0, encr_enable);
BTM_TRACE_DEBUG ("before update p_dev_rec->sec_flags=0x%x\n", (p_dev_rec) ? p_dev_rec->sec_flags : 0 );
if ((status == HCI_ERR_LMP_ERR_TRANS_COLLISION) ||
(status == HCI_ERR_DIFF_TRANSACTION_COLLISION)) {
btm_sec_auth_collision(handle);
return;
}{...}
btm_cb.collision_start_time = 0;
if (!p_dev_rec) {
return;
}{...}
if ((status == HCI_SUCCESS) && encr_enable) {
if (p_dev_rec->hci_handle == handle) {
p_dev_rec->sec_flags |= (BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED);
if (p_dev_rec->pin_code_length >= 16 ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
}{...}
if (p_dev_rec->enc_mode != encr_enable) {
p_dev_rec->enc_mode = encr_enable;
if (btm_cb.api.p_enc_change_callback) {
(*btm_cb.api.p_enc_change_callback) (p_dev_rec->bd_addr, encr_enable);
}{...}
}{...}
}{...} else {
p_dev_rec->sec_flags |= BTM_SEC_LE_ENCRYPTED;
}{...}
}{...}
if ((status == HCI_SUCCESS) && !encr_enable) {
if (p_dev_rec->hci_handle == handle) {
p_dev_rec->sec_flags &= ~BTM_SEC_ENCRYPTED;
if (p_dev_rec->enc_mode != encr_enable) {
p_dev_rec->enc_mode = encr_enable;
if (btm_cb.api.p_enc_change_callback) {
(*btm_cb.api.p_enc_change_callback) (p_dev_rec->bd_addr, encr_enable);
}{...}
}{...}
}{...} else {
p_dev_rec->sec_flags &= ~BTM_SEC_LE_ENCRYPTED;
}{...}
}{...}
BTM_TRACE_DEBUG ("after update p_dev_rec->sec_flags=0x%x\n", p_dev_rec->sec_flags );
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
p_acl = btm_handle_to_acl(handle);
if (p_acl != NULL) {
btm_sec_check_pending_enc_req(p_dev_rec, p_acl->transport, encr_enable);
}{...}
if (p_acl && p_acl->transport == BT_TRANSPORT_LE) {
if (status == HCI_ERR_KEY_MISSING || status == HCI_ERR_AUTH_FAILURE ||
status == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE) {
p_dev_rec->sec_flags &= ~ (BTM_SEC_LE_LINK_KEY_KNOWN);
p_dev_rec->ble.key_type = BTM_LE_KEY_NONE;
}{...}
btm_ble_link_encrypted(p_dev_rec->ble.pseudo_addr, encr_enable);
return;
}{...} else {
p_dev_rec->enc_key_size = 16;
}{...}
BTM_TRACE_DEBUG ("in %s new_encr_key_256 is %d\n",
__func__, p_dev_rec->new_encryption_key_is_p256);
if ((status == HCI_SUCCESS) && encr_enable && (p_dev_rec->hci_handle == handle)) {
if (p_dev_rec->new_encryption_key_is_p256) {
if (btm_sec_use_smp_br_chnl(p_dev_rec) &&
btm_sec_is_master(p_dev_rec) &&
(!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN) ||
(!(p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED)
&& (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)))) {
p_dev_rec->new_encryption_key_is_p256 = FALSE;
if (p_dev_rec->no_smp_on_br) {
BTM_TRACE_DEBUG ("%s NO SM over BR/EDR\n", __func__);
}{...} else {
#if (CLASSIC_BT_INCLUDED == TRUE)
BTM_TRACE_DEBUG ("%s start SM over BR/EDR\n", __func__);
SMP_BR_PairWith(p_dev_rec->bd_addr);/* ... */
#endif
}{...}
}{...}
}{...} else {
if ((encr_enable == 1) &&
(p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256 ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
if (p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256) {
p_dev_rec->link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB;
}{...} else {
p_dev_rec->link_key_type = BTM_LKEY_TYPE_AUTH_COMB;
}{...}
BTM_TRACE_DEBUG("updated link key type to %d\n", p_dev_rec->link_key_type);
btm_send_link_key_notif(p_dev_rec);
}{...}
}{...}
}{...}
#else/* ... */
btm_sec_check_pending_enc_req (p_dev_rec, BT_TRANSPORT_BR_EDR, encr_enable);
#endif
if (p_dev_rec->sec_state != BTM_SEC_STATE_ENCRYPTING) {
if (BTM_SEC_STATE_DELAY_FOR_ENC == p_dev_rec->sec_state) {
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
p_dev_rec->p_callback = NULL;
#if (CLASSIC_BT_INCLUDED == TRUE)
l2cu_resubmit_pending_sec_req (p_dev_rec->bd_addr);
#endif
}{...}
return;
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
if (status != HCI_SUCCESS) {
btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
return;
}{...}
status = (UINT8)btm_sec_execute_procedure (p_dev_rec);
if (status != BTM_CMD_STARTED) {
btm_sec_dev_rec_cback_event (p_dev_rec, status, FALSE);
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_sec_connect_after_reject_timeout (TIMER_LIST_ENT *p_tle)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_cb.p_collided_dev_rec;
UNUSED(p_tle);
BTM_TRACE_EVENT ("btm_sec_connect_after_reject_timeout()\n");
btm_cb.sec_collision_tle.param = 0;
btm_cb.p_collided_dev_rec = 0;
if (btm_sec_dd_create_conn(p_dev_rec) != BTM_CMD_STARTED) {
BTM_TRACE_WARNING ("Security Manager: btm_sec_connect_after_reject_timeout: failed to start connection\n");
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if (btm_cb.api.p_auth_complete_callback) {
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_ERR_MEMORY_FULL);
}{...}
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
void btm_sec_connected (UINT8 *bda, UINT16 handle, UINT8 status, UINT8 enc_mode)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
UINT8 res;
UINT8 sec_dev_rec_status;
BOOLEAN is_pairing_device = FALSE;
tACL_CONN *p_acl_cb;
UINT8 bit_shift = 0;
btm_acl_resubmit_page();
/* ... */
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
if (p_dev_rec) {
BTM_TRACE_EVENT ("Security Manager: btm_sec_connected in state: %s handle:%d status:%d enc_mode:%d bda:%x RName:%s\n",
btm_pair_state_descr(btm_cb.pairing_state), handle, status, enc_mode,
(bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5],
p_dev_rec->sec_bd_name);
}{...} else {
BTM_TRACE_EVENT ("Security Manager: btm_sec_connected in state: %s handle:%d status:%d enc_mode:%d bda:%x \n",
btm_pair_state_descr(btm_cb.pairing_state), handle, status, enc_mode,
(bda[2] << 24) + (bda[3] << 16) + (bda[4] << 8) + bda[5]);
}{...}
#endif/* ... */
if (!p_dev_rec) {
if (status == HCI_SUCCESS) {
p_dev_rec = btm_sec_alloc_dev (bda);
}{...} else {
/* ... */
if ((btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) &&
(memcmp (btm_cb.pairing_bda, bda, BD_ADDR_LEN) == 0)) {
btm_sec_change_pairing_state(BTM_PAIR_STATE_IDLE);
}{...}
/* ... */
return;
}{...}
}{...} else {
#if BLE_INCLUDED == TRUE
bit_shift = (handle == p_dev_rec->ble_hci_handle) ? 8 : 0;
#endif
p_dev_rec->timestamp = btm_cb.dev_rec_count++;
if (p_dev_rec->sm4 & BTM_SM4_CONN_PEND) {
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (memcmp (btm_cb.pairing_bda, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0)
&& (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) ) {
if ((status != HCI_SUCCESS) && (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_PIN_REQ)) {
BTM_TRACE_WARNING ("Security Manager: btm_sec_connected: incoming connection failed without asking PIN\n");
p_dev_rec->sm4 &= ~BTM_SM4_CONN_PEND;
if (p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN) {
btm_cb.p_collided_dev_rec = p_dev_rec;
btm_cb.sec_collision_tle.param = (UINT32) btm_sec_connect_after_reject_timeout;
btu_start_timer (&btm_cb.sec_collision_tle, BTU_TTYPE_USER_FUNC, 0);
}{...} else {
btm_sec_change_pairing_state (BTM_PAIR_STATE_GET_REM_NAME);
BTM_ReadRemoteDeviceName(p_dev_rec->bd_addr, NULL, BT_TRANSPORT_BR_EDR);
}{...}
#if BTM_DISC_DURING_RS == TRUE
p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
#endif
return;
}{...} else {
l2cu_update_lcb_4_bonding(p_dev_rec->bd_addr, TRUE);
}{...}
}{...}
p_dev_rec->sm4 &= ~BTM_SM4_CONN_PEND;
}{...}
}{...}
#if BLE_INCLUDED == TRUE
p_dev_rec->device_type |= BT_DEVICE_TYPE_BREDR;
#endif
#if BTM_DISC_DURING_RS == TRUE
p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
#endif
p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (memcmp (btm_cb.pairing_bda, bda, BD_ADDR_LEN) == 0) ) {
if ((status == HCI_ERR_HOST_REJECT_DEVICE)
&& (btm_cb.pairing_flags & BTM_PAIR_FLAGS_REJECTED_CONNECT)) {
BTM_TRACE_WARNING ("Security Manager: btm_sec_connected: HCI_Conn_Comp Flags:0x%04x, sm4: 0x%x\n",
btm_cb.pairing_flags, p_dev_rec->sm4);
btm_cb.pairing_flags &= ~BTM_PAIR_FLAGS_REJECTED_CONNECT;
if (BTM_SEC_IS_SM4_UNKNOWN(p_dev_rec->sm4)) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_GET_REM_NAME);
BTM_ReadRemoteDeviceName(bda, NULL, BT_TRANSPORT_BR_EDR);
return;
}{...}
if (btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_LOCAL_PIN) {
btm_cb.p_collided_dev_rec = p_dev_rec;
btm_cb.sec_collision_tle.param = (UINT32) btm_sec_connect_after_reject_timeout;
btu_start_timer (&btm_cb.sec_collision_tle, BTU_TTYPE_USER_FUNC, 0);
}{...}
return;
}{...}
else if (status == HCI_ERR_CONNECTION_EXISTS) {
BTM_TRACE_WARNING ("Security Manager: btm_sec_connected: Wait for incoming connection\n");
return;
}{...}
is_pairing_device = TRUE;
}{...}
btm_restore_mode();
if (status != HCI_SUCCESS) {
if (is_pairing_device) {
p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
p_dev_rec->sec_flags &= ~((BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED) << bit_shift);
BTM_TRACE_DEBUG ("security_required:%x \n", p_dev_rec->security_required );
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if (btm_cb.api.p_auth_complete_callback) {
sec_dev_rec_status = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, status);
if (sec_dev_rec_status == BTM_SEC_DEV_REC_REMOVED) {
p_dev_rec = NULL;
}{...}
}{...}
}{...}
/* ... */
else if ((p_dev_rec->link_key_type <= BTM_LKEY_TYPE_REMOTE_UNIT) &&
(((status == HCI_ERR_AUTH_FAILURE) ||
(status == HCI_ERR_KEY_MISSING) ||
(status == HCI_ERR_HOST_REJECT_SECURITY) ||
(status == HCI_ERR_PAIRING_NOT_ALLOWED) ||
(status == HCI_ERR_UNIT_KEY_USED) ||
(status == HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED) ||
(status == HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE) ||
(status == HCI_ERR_REPEATED_ATTEMPTS)))) {
p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
p_dev_rec->sec_flags &= ~ (BTM_SEC_LE_LINK_KEY_KNOWN << bit_shift);
#ifdef BRCM_NOT_4_BTE
if (btm_cb.acl_disc_reason == HCI_ERR_HOST_REJECT_SECURITY) {
status = HCI_ERR_HOST_REJECT_SECURITY;
}{...}
#endif/* ... */
if (btm_cb.api.p_auth_complete_callback) {
sec_dev_rec_status = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, status);
if (sec_dev_rec_status == BTM_SEC_DEV_REC_REMOVED) {
p_dev_rec = NULL;
}{...}
}{...}
}{...}
if (status == HCI_ERR_CONNECTION_TOUT || status == HCI_ERR_LMP_RESPONSE_TIMEOUT ||
status == HCI_ERR_UNSPECIFIED || status == HCI_ERR_PAGE_TIMEOUT) {
btm_sec_dev_rec_cback_event (p_dev_rec, BTM_DEVICE_TIMEOUT, FALSE);
}{...} else {
btm_sec_dev_rec_cback_event (p_dev_rec, BTM_ERR_PROCESSING, FALSE);
}{...}
return;
}{...}
if ( is_pairing_device
&& (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) ) {
if (p_dev_rec->link_key_not_sent) {
p_dev_rec->link_key_not_sent = FALSE;
btm_send_link_key_notif(p_dev_rec);
}{...}
p_dev_rec->security_required &= ~BTM_SEC_OUT_AUTHENTICATE;
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
res = TRUE;
}{...} else {
res = FALSE;
}{...}
if (btm_cb.api.p_auth_complete_callback) {
sec_dev_rec_status = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_SUCCESS);
if (sec_dev_rec_status == BTM_SEC_DEV_REC_REMOVED) {
p_dev_rec = NULL;
}{...}
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if ( res ) {
l2cu_update_lcb_4_bonding (p_dev_rec->bd_addr, TRUE);
}{...}
return;
}{...}
p_dev_rec->hci_handle = handle;
p_acl_cb = btm_bda_to_acl(bda, BT_TRANSPORT_BR_EDR);
if (p_acl_cb) {
#if (!defined(BTM_BYPASS_EXTRA_ACL_SETUP) || BTM_BYPASS_EXTRA_ACL_SETUP == FALSE)
btm_set_packet_types (p_acl_cb, btm_cb.btm_acl_pkt_types_supported);
if (btm_cb.btm_def_link_policy) {
BTM_SetLinkPolicy (p_acl_cb->remote_addr, &btm_cb.btm_def_link_policy);
}{...}
#endif/* ... */
}{...}
btm_acl_created (bda, p_dev_rec->dev_class, p_dev_rec->sec_bd_name, handle, HCI_ROLE_SLAVE, BT_TRANSPORT_BR_EDR);
p_dev_rec->sec_flags &= ~((BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED |
BTM_SEC_ENCRYPTED | BTM_SEC_ROLE_SWITCHED) << bit_shift);
if (enc_mode != HCI_ENCRYPT_MODE_DISABLED) {
p_dev_rec->sec_flags |= ((BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED) << bit_shift);
}{...}
if (btm_cb.security_mode == BTM_SEC_MODE_LINK) {
p_dev_rec->sec_flags |= (BTM_SEC_AUTHENTICATED << bit_shift);
}{...}
if (p_dev_rec->pin_code_length >= 16 ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
p_dev_rec->sec_flags |= (BTM_SEC_16_DIGIT_PIN_AUTHED << bit_shift);
}{...}
p_dev_rec->link_key_changed = FALSE;
BTM_TRACE_DEBUG ("is_originator:%d \n", p_dev_rec->is_originator);
if (!(p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN) || p_dev_rec->is_originator) {
if ((res = btm_sec_execute_procedure (p_dev_rec)) != BTM_CMD_STARTED) {
btm_sec_dev_rec_cback_event (p_dev_rec, res, FALSE);
}{...}
}{...}
return;
}{ ... }
/* ... */
tBTM_STATUS btm_sec_disconnect (UINT16 handle, UINT8 reason)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
if (!p_dev_rec) {
btsnd_hcic_disconnect (handle, reason);
return (BTM_SUCCESS);
}{...}
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (memcmp (btm_cb.pairing_bda, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0)
&& (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) ) {
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_DISC_WHEN_DONE;
return (BTM_BUSY);
}{...}
return (btm_sec_send_hci_disconnect(p_dev_rec, reason, handle));
}{ ... }
/* ... */
void btm_sec_disconnected (UINT16 handle, UINT8 reason)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
UINT8 old_pairing_flags = btm_cb.pairing_flags;
int result = HCI_ERR_AUTH_FAILURE;
tBTM_SEC_CALLBACK *p_callback = NULL;
tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
btm_cb.discing = FALSE;
#if (CLASSIC_BT_INCLUDED == TRUE)
btm_acl_resubmit_page();
#endif
if (!p_dev_rec) {
return;
}{...}
p_dev_rec->enc_init_by_we = FALSE;
transport = (handle == p_dev_rec->hci_handle) ? BT_TRANSPORT_BR_EDR : BT_TRANSPORT_LE;
p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
#if BTM_DISC_DURING_RS == TRUE
p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
#endif
p_dev_rec->sm4 &= BTM_SM4_TRUE;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
uint8_t *bd_addr = (uint8_t *)p_dev_rec->bd_addr;
BTM_TRACE_EVENT("%s sec_req:x%x state:%s reason:%d bd_addr:%02x:%02x:%02x:%02x:%02x:%02x"
" remote_name:%s\n", __func__, p_dev_rec->security_required, btm_pair_state_descr(btm_cb.pairing_state),
reason, bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5], p_dev_rec->sec_bd_name);/* ... */
#endif
BTM_TRACE_EVENT("%s before update sec_flags=0x%x\n", __func__, p_dev_rec->sec_flags);
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, p_dev_rec->bd_addr, HCI_SUCCESS);
if (transport == BT_TRANSPORT_LE) {
p_dev_rec->ble_hci_handle = BTM_SEC_INVALID_HANDLE;
p_dev_rec->sec_flags &= ~(BTM_SEC_LE_AUTHENTICATED | BTM_SEC_LE_ENCRYPTED);
p_dev_rec->enc_key_size = 0;
}{...} else
#endif
{
p_dev_rec->hci_handle = BTM_SEC_INVALID_HANDLE;
p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED | BTM_SEC_ENCRYPTED
| BTM_SEC_ROLE_SWITCHED | BTM_SEC_16_DIGIT_PIN_AUTHED);
}{...}
#if BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE
if (p_dev_rec->sec_state == BTM_SEC_STATE_DISCONNECTING_BOTH) {
p_dev_rec->sec_state = (transport == BT_TRANSPORT_LE) ?
BTM_SEC_STATE_DISCONNECTING : BTM_SEC_STATE_DISCONNECTING_BLE;
return;
}{...}
#endif/* ... */
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
p_dev_rec->security_required = BTM_SEC_NONE;
p_callback = p_dev_rec->p_callback;
if (p_callback) {
p_dev_rec->p_callback = NULL;
/* ... */
(*p_callback) (p_dev_rec->bd_addr, transport, p_dev_rec->p_ref_data, BTM_ERR_PROCESSING);
}{...}
BTM_TRACE_EVENT("%s after update sec_flags=0x%x\n", __func__, p_dev_rec->sec_flags);
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (memcmp (btm_cb.pairing_bda, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0)) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
p_dev_rec->sec_flags &= ~BTM_SEC_LINK_KEY_KNOWN;
if (btm_cb.api.p_auth_complete_callback) {
/* ... */
if (reason == HCI_ERR_REPEATED_ATTEMPTS) {
result = HCI_ERR_REPEATED_ATTEMPTS;
}{...} else if (old_pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
result = HCI_ERR_HOST_REJECT_SECURITY;
}{...}
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, result);
}{...}
}{...}
}{ ... }
/* ... */
void btm_sec_link_key_notification (UINT8 *p_bda, UINT8 *p_link_key, UINT8 key_type)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_bda);
BOOLEAN we_are_bonding = FALSE;
BOOLEAN ltk_derived_lk = FALSE;
UINT8 res;
BTM_TRACE_EVENT ("btm_sec_link_key_notification() BDA:%04x%08x, TYPE: %d\n",
(p_bda[0] << 8) + p_bda[1], (p_bda[2] << 24) + (p_bda[3] << 16) + (p_bda[4] << 8) + p_bda[5],
key_type);
if ((key_type >= BTM_LTK_DERIVED_LKEY_OFFSET + BTM_LKEY_TYPE_COMBINATION) &&
(key_type <= BTM_LTK_DERIVED_LKEY_OFFSET + BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
ltk_derived_lk = TRUE;
key_type -= BTM_LTK_DERIVED_LKEY_OFFSET;
}{...}
btm_restore_mode();
/* ... */
p_dev_rec->remote_secure_connection_previous_state = p_dev_rec->remote_supports_secure_connections;
if (p_dev_rec->remote_supports_secure_connections) {
BTM_TRACE_EVENT ("Remote device supports Secure Connection");
}{...} else {
BTM_TRACE_EVENT ("Remote device does not support Secure Connection");
}{...}
if (key_type != BTM_LKEY_TYPE_CHANGED_COMB) {
p_dev_rec->link_key_type = key_type;
}{...}
p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_KNOWN;
#if (CLASSIC_BT_INCLUDED == TRUE)
btm_sec_update_legacy_auth_state(btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR), BTM_ACL_LEGACY_AUTH_NONE);
#endif
/* ... */
if (p_dev_rec->pin_code_length >= 16 ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB ||
p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED;
}{...}
#if (BLE_INCLUDED == TRUE)
p_dev_rec->enc_key_size = 16;/* ... */
#endif
memcpy (p_dev_rec->link_key, p_link_key, LINK_KEY_LEN);
if ( (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE)
&& (memcmp (btm_cb.pairing_bda, p_bda, BD_ADDR_LEN) == 0) ) {
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
we_are_bonding = TRUE;
}{...} else {
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
}{...}
}{...}
if (ltk_derived_lk) {
if (btm_cb.api.p_link_key_callback) {
BTM_TRACE_DEBUG ("%s() Save LTK derived LK (key_type = %d)\n",
__FUNCTION__, p_dev_rec->link_key_type);
(*btm_cb.api.p_link_key_callback) (p_bda, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name,
p_link_key, p_dev_rec->link_key_type,
p_dev_rec->remote_supports_secure_connections);
}{...}
}{...} else {
if ((p_dev_rec->link_key_type == BTM_LKEY_TYPE_UNAUTH_COMB_P_256) ||
(p_dev_rec->link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
p_dev_rec->new_encryption_key_is_p256 = TRUE;
BTM_TRACE_DEBUG ("%s set new_encr_key_256 to %d\n",
__func__, p_dev_rec->new_encryption_key_is_p256);
}{...}
}{...}
if ((!(p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN)
&& ((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) != BTM_COD_MAJOR_PERIPHERAL))
&& !ltk_derived_lk) {
BTM_TRACE_EVENT ("btm_sec_link_key_notification() Delayed BDA: %08x%04x Type:%d\n",
(p_bda[0] << 24) + (p_bda[1] << 16) + (p_bda[2] << 8) + p_bda[3],
(p_bda[4] << 8) + p_bda[5], key_type);
p_dev_rec->link_key_not_sent = TRUE;
if (we_are_bonding) {
if (!(btsnd_hcic_rmt_name_req (p_bda, HCI_PAGE_SCAN_REP_MODE_R1, HCI_MANDATARY_PAGE_SCAN_MODE, 0))) {
btm_inq_rmt_name_failed();
}{...}
}{...}
BTM_TRACE_EVENT ("rmt_io_caps:%d, sec_flags:x%x, dev_class[1]:x%02x\n", p_dev_rec->rmt_io_caps, p_dev_rec->sec_flags, p_dev_rec->dev_class[1])
return;
}{...}
if (!(p_dev_rec->security_required & BTM_SEC_OUT_AUTHENTICATE)
|| ltk_derived_lk) {
if (btm_cb.api.p_auth_complete_callback) {
res = (*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_SUCCESS);
if (res == BTM_SEC_DEV_REC_REMOVED) {
p_dev_rec = NULL;
}{...}
}{...}
}{...}
if(!p_dev_rec) {
return;
}{...}
#ifdef BRCM_NONE_BTE
if (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_AUTHED)
#endif
{
if (btm_cb.api.p_link_key_callback) {
if (ltk_derived_lk) {
BTM_TRACE_DEBUG ("btm_sec_link_key_notification() LTK derived LK is saved already"
" (key_type = %d)\n", p_dev_rec->link_key_type);
}{...} else {
(*btm_cb.api.p_link_key_callback) (p_bda, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name,
p_link_key, p_dev_rec->link_key_type,
p_dev_rec->remote_supports_secure_connections);
}{...}
}{...}
}{...}
}{ ... }
/* ... */
void btm_sec_link_key_request (UINT8 *p_bda)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_bda);
BTM_TRACE_EVENT ("btm_sec_link_key_request() BDA: %02x:%02x:%02x:%02x:%02x:%02x\n",
p_bda[0], p_bda[1], p_bda[2], p_bda[3], p_bda[4], p_bda[5]);
if ( (btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_PIN_REQ) &&
(btm_cb.collision_start_time != 0) &&
(memcmp (btm_cb.p_collided_dev_rec->bd_addr, p_bda, BD_ADDR_LEN) == 0) ) {
BTM_TRACE_EVENT ("btm_sec_link_key_request() rejecting link key req "
"State: %d START_TIMEOUT : %d\n",
btm_cb.pairing_state, btm_cb.collision_start_time);
btsnd_hcic_link_key_neg_reply (p_bda);
return;
}{...}
if (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN) {
btsnd_hcic_link_key_req_reply (p_bda, p_dev_rec->link_key);
return;
}{...}
l2c_pin_code_request (p_bda);
btsnd_hcic_link_key_neg_reply (p_bda);
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_sec_pairing_timeout (TIMER_LIST_ENT *p_tle)
{
tBTM_CB *p_cb = &btm_cb;
tBTM_SEC_DEV_REC *p_dev_rec;
#if BTM_OOB_INCLUDED == TRUE
#if (BTM_LOCAL_IO_CAPS == BTM_IO_CAP_NONE)
tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO;
#else
tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_YES;
#endif/* ... */
#endif
UINT8 name[2];
UNUSED(p_tle);
p_cb->pairing_tle.param = 0;
/* ... */
p_dev_rec = btm_find_dev (p_cb->pairing_bda);
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("btm_sec_pairing_timeout() State: %s Flags: %u\n",
btm_pair_state_descr(p_cb->pairing_state), p_cb->pairing_flags);/* ... */
#endif
switch (p_cb->pairing_state) {
case BTM_PAIR_STATE_WAIT_PIN_REQ:
btm_sec_bond_cancel_complete();
break;
...
case BTM_PAIR_STATE_WAIT_LOCAL_PIN:
if ( (btm_cb.pairing_flags & BTM_PAIR_FLAGS_PRE_FETCH_PIN) == 0) {
btsnd_hcic_pin_code_neg_reply (p_cb->pairing_bda);
}{...}
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if (btm_cb.api.p_auth_complete_callback) {
if (p_dev_rec == NULL) {
name[0] = '\0';
(*btm_cb.api.p_auth_complete_callback) (p_cb->pairing_bda,
NULL,
name, HCI_ERR_CONNECTION_TOUT);
}{...} else {
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_ERR_CONNECTION_TOUT);
}{...}
}{...}
break;
...
case BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM:
btsnd_hcic_user_conf_reply (p_cb->pairing_bda, FALSE);
break;
#if (CLASSIC_BT_INCLUDED == TRUE)...
case BTM_PAIR_STATE_KEY_ENTRY:
btsnd_hcic_user_passkey_neg_reply(p_cb->pairing_bda);
break;/* ... */
#endif
#if BTM_OOB_INCLUDED == TRUE
case BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS:
if (btm_cb.pairing_flags & BTM_PAIR_FLAGS_WE_STARTED_DD) {
auth_req |= BTM_AUTH_DD_BOND;
}{...}
btsnd_hcic_io_cap_req_reply (p_cb->pairing_bda, btm_cb.devcb.loc_io_caps,
BTM_OOB_NONE, auth_req);
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
break;
...
case BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP:
btsnd_hcic_rem_oob_neg_reply (p_cb->pairing_bda);
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
break;/* ... */
#endif
case BTM_PAIR_STATE_WAIT_DISCONNECT:
/* ... */
if (p_dev_rec == NULL) {
BTM_TRACE_ERROR ("btm_sec_pairing_timeout() BTM_PAIR_STATE_WAIT_DISCONNECT unknown BDA: %08x%04x\n",
(p_cb->pairing_bda[0] << 24) + (p_cb->pairing_bda[1] << 16) + (p_cb->pairing_bda[2] << 8) + p_cb->pairing_bda[3],
(p_cb->pairing_bda[4] << 8) + p_cb->pairing_bda[5]);
break;
}{...}
btm_sec_send_hci_disconnect (p_dev_rec, HCI_ERR_AUTH_FAILURE, p_dev_rec->hci_handle);
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
break;
...
case BTM_PAIR_STATE_WAIT_AUTH_COMPLETE:
case BTM_PAIR_STATE_GET_REM_NAME:
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
if (btm_cb.api.p_auth_complete_callback) {
if (p_dev_rec == NULL) {
name[0] = '\0';
(*btm_cb.api.p_auth_complete_callback) (p_cb->pairing_bda,
NULL,
name, HCI_ERR_CONNECTION_TOUT);
}{...} else {
(*btm_cb.api.p_auth_complete_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, HCI_ERR_CONNECTION_TOUT);
}{...}
}{...}
break;
...
default:
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_WARNING ("btm_sec_pairing_timeout() not processed state: %s\n", btm_pair_state_descr(btm_cb.pairing_state));
#endif
btm_sec_change_pairing_state (BTM_PAIR_STATE_IDLE);
break;...
}{...}
}{ ... }
#if (CLASSIC_BT_INCLUDED == TRUE)
/* ... */
void btm_sec_pin_code_request (UINT8 *p_bda)
{
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_CB *p_cb = &btm_cb;
#ifdef PORCHE_PAIRING_CONFLICT
UINT8 default_pin_code_len = 4;
PIN_CODE default_pin_code = {0x30, 0x30, 0x30, 0x30};/* ... */
#endif
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("btm_sec_pin_code_request() State: %s, BDA:%04x%08x\n",
btm_pair_state_descr(btm_cb.pairing_state),
(p_bda[0] << 8) + p_bda[1], (p_bda[2] << 24) + (p_bda[3] << 16) + (p_bda[4] << 8) + p_bda[5] );/* ... */
#endif
if (btm_cb.pairing_state != BTM_PAIR_STATE_IDLE) {
if ( (memcmp (p_bda, btm_cb.pairing_bda, BD_ADDR_LEN) == 0) &&
(btm_cb.pairing_state == BTM_PAIR_STATE_WAIT_AUTH_COMPLETE) ) {
if (! btm_cb.pin_code_len_saved) {
btsnd_hcic_pin_code_neg_reply (p_bda);
return;
}{...} else {
btsnd_hcic_pin_code_req_reply (p_bda, btm_cb.pin_code_len_saved, p_cb->pin_code);
return;
}{...}
}{...} else if ((btm_cb.pairing_state != BTM_PAIR_STATE_WAIT_PIN_REQ)
|| memcmp (p_bda, btm_cb.pairing_bda, BD_ADDR_LEN) != 0) {
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_WARNING ("btm_sec_pin_code_request() rejected - state: %s\n",
btm_pair_state_descr(btm_cb.pairing_state));/* ... */
#endif
#ifdef PORCHE_PAIRING_CONFLICT
BTM_TRACE_EVENT ("btm_sec_pin_code_request from remote dev. for local initiated pairing\n");
if (! btm_cb.pin_code_len_saved) {
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
btsnd_hcic_pin_code_req_reply (p_bda, default_pin_code_len, default_pin_code);
}{...} else {
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
btsnd_hcic_pin_code_req_reply (p_bda, btm_cb.pin_code_len_saved, p_cb->pin_code);
}{...}
/* ... */#else
btsnd_hcic_pin_code_neg_reply (p_bda);
#endif
return;
}{...}
}{...}
p_dev_rec = btm_find_or_alloc_dev (p_bda);
p_dev_rec->sm4 = BTM_SM4_KNOWN;
if (btm_cb.pairing_state == BTM_PAIR_STATE_IDLE) {
memcpy (btm_cb.pairing_bda, p_bda, BD_ADDR_LEN);
btm_cb.pairing_flags = BTM_PAIR_FLAGS_PEER_STARTED_DD;
BTM_SEC_CLR_TRUSTED_DEVICE(p_dev_rec->trusted_mask);
}{...}
if (!p_cb->pairing_disabled && (p_cb->cfg.pin_type == HCI_PIN_TYPE_FIXED)) {
BTM_TRACE_EVENT ("btm_sec_pin_code_request fixed pin replying\n");
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
btsnd_hcic_pin_code_req_reply (p_bda, p_cb->cfg.pin_code_len, p_cb->cfg.pin_code);
return;
}{...}
if ( (!memcmp (p_bda, p_cb->connecting_bda, BD_ADDR_LEN))
&& (p_cb->connecting_dc[0] || p_cb->connecting_dc[1] || p_cb->connecting_dc[2]) ) {
memcpy (p_dev_rec->dev_class, p_cb->connecting_dc, DEV_CLASS_LEN);
}{...}
if (btm_cb.pin_code_len != 0) {
BTM_TRACE_EVENT ("btm_sec_pin_code_request bonding sending reply\n");
btsnd_hcic_pin_code_req_reply (p_bda, btm_cb.pin_code_len, p_cb->pin_code);
#ifdef PORCHE_PAIRING_CONFLICT
btm_cb.pin_code_len_saved = btm_cb.pin_code_len;
#endif
btm_cb.pin_code_len = 0;
/* ... */
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_AUTH_COMPLETE);
}{...}
else if (p_cb->pairing_disabled
|| (p_cb->api.p_pin_callback == NULL)
|| (!p_dev_rec->is_originator
&& ((p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK) == BTM_COD_MAJOR_PERIPHERAL)
&& (p_dev_rec->dev_class[2] & BTM_COD_MINOR_KEYBOARD)) ) {
BTM_TRACE_WARNING("btm_sec_pin_code_request(): Pairing disabled:%d; PIN callback:%p, Dev Rec:%p!\n",
p_cb->pairing_disabled, p_cb->api.p_pin_callback, p_dev_rec);
btsnd_hcic_pin_code_neg_reply (p_bda);
}{...}
else {
btm_cb.pin_code_len_saved = 0;
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_PIN);
memcpy (p_cb->connecting_bda, p_bda, BD_ADDR_LEN);
memcpy (p_cb->connecting_dc, p_dev_rec->dev_class, DEV_CLASS_LEN);
BTM_TRACE_EVENT ("btm_sec_pin_code_request going for callback\n");
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PIN_REQD;
if (p_cb->api.p_pin_callback) {
(*p_cb->api.p_pin_callback) (p_bda, p_dev_rec->dev_class, p_dev_rec->sec_bd_name,
(p_dev_rec->p_cur_service == NULL) ? FALSE
: (p_dev_rec->p_cur_service->security_flags
& BTM_SEC_IN_MIN_16_DIGIT_PIN));
}{...}
}{...}
return;
}{...}
/* ... */#endif /* ... */
#endif
/* ... */
void btm_sec_update_clock_offset (UINT16 handle, UINT16 clock_offset)
{
tBTM_SEC_DEV_REC *p_dev_rec;
tBTM_INQ_INFO *p_inq_info;
if ((p_dev_rec = btm_find_dev_by_handle (handle)) == NULL) {
return;
}{...}
p_dev_rec->clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
if ((p_inq_info = BTM_InqDbRead(p_dev_rec->bd_addr)) == NULL) {
return;
}{...}
p_inq_info->results.clock_offset = clock_offset | BTM_CLOCK_OFFSET_VALID;
}{ ... }
/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static tBTM_STATUS btm_sec_execute_procedure (tBTM_SEC_DEV_REC *p_dev_rec)
{
BTM_TRACE_EVENT ("btm_sec_execute_procedure: Required:0x%x Flags:0x%x State:%d\n",
p_dev_rec->security_required, p_dev_rec->sec_flags, p_dev_rec->sec_state);
if (p_dev_rec->sec_state != 0) {
return (BTM_CMD_STARTED);
}{...}
if (!(p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN)
&& (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE)) {
BTM_TRACE_EVENT ("Security Manager: Start get name\n");
if (!btm_sec_start_get_name (p_dev_rec)) {
return (BTM_NO_RESOURCES);
}{...}
return (BTM_CMD_STARTED);
}{...}
#if (CLASSIC_BT_INCLUDED == TRUE)
tACL_CONN *p_acl_cb = btm_handle_to_acl(p_dev_rec->hci_handle);
/* ... */
if ((p_acl_cb != NULL) && (BTM_BothEndsSupportSecureConnections(p_acl_cb->remote_addr) == 0) &&
((p_acl_cb->legacy_auth_state & BTM_ACL_LEGACY_AUTH_SELF) == 0)) {
p_dev_rec->sec_flags &= ~BTM_SEC_AUTHENTICATED;
}{...}
#endif/* ... */
if ((((!(p_dev_rec->sec_flags & BTM_SEC_AUTHENTICATED))
&& (( p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_OUT_AUTHENTICATE))
|| (!p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_IN_AUTHENTICATE))))
|| (!(p_dev_rec->sec_flags & BTM_SEC_16_DIGIT_PIN_AUTHED)
&& (!p_dev_rec->is_originator
&& (p_dev_rec->security_required & BTM_SEC_IN_MIN_16_DIGIT_PIN))))
&& (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE)) {
/* ... */
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( !p_dev_rec->is_originator && (p_dev_rec->is_ucd == TRUE )) {
return (BTM_FAILED_ON_SECURITY);
}{...}
#endif/* ... */
BTM_TRACE_EVENT ("Security Manager: Start authentication\n");
/* ... */
if ((p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)
&& (!(p_dev_rec->sec_flags & BTM_SEC_16_DIGIT_PIN_AUTHED)
&& (!p_dev_rec->is_originator && (p_dev_rec->security_required
& BTM_SEC_IN_MIN_16_DIGIT_PIN)))) {
p_dev_rec->sec_flags &= ~(BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED
| BTM_SEC_AUTHENTICATED);
}{...}
if (!btm_sec_start_authentication (p_dev_rec)) {
return (BTM_NO_RESOURCES);
}{...}
return (BTM_CMD_STARTED);
}{...}
if (!(p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED)
&& (( p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_OUT_ENCRYPT))
|| (!p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_IN_ENCRYPT)))
&& (p_dev_rec->hci_handle != BTM_SEC_INVALID_HANDLE)) {
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( !p_dev_rec->is_originator && (p_dev_rec->is_ucd == TRUE )) {
return (BTM_FAILED_ON_SECURITY);
}{...}
#endif/* ... */
BTM_TRACE_EVENT ("Security Manager: Start encryption\n");
if (!btm_sec_start_encryption (p_dev_rec)) {
return (BTM_NO_RESOURCES);
}{...}
return (BTM_CMD_STARTED);
}{...}
if ((p_dev_rec->security_required & BTM_SEC_MODE4_LEVEL4) &&
(p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
BTM_TRACE_EVENT("%s: Security Manager: SC only service, but link key type is 0x%02x -"
"security failure\n", __FUNCTION__, p_dev_rec->link_key_type);
return (BTM_FAILED_ON_SECURITY);
}{...}
if (!(p_dev_rec->sec_flags & BTM_SEC_AUTHORIZED)
&& (( p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_OUT_AUTHORIZE))
|| (!p_dev_rec->is_originator && (p_dev_rec->security_required & BTM_SEC_IN_AUTHORIZE)))) {
BTM_TRACE_EVENT ("service id:%d, is trusted:%d\n",
p_dev_rec->p_cur_service->service_id,
(BTM_SEC_IS_SERVICE_TRUSTED(p_dev_rec->trusted_mask,
p_dev_rec->p_cur_service->service_id)));
if ((btm_sec_are_all_trusted(p_dev_rec->trusted_mask) == FALSE) &&
(p_dev_rec->p_cur_service->service_id < BTM_SEC_MAX_SERVICES) &&
(BTM_SEC_IS_SERVICE_TRUSTED(p_dev_rec->trusted_mask,
p_dev_rec->p_cur_service->service_id) == FALSE)) {
BTM_TRACE_EVENT ("Security Manager: Start authorization\n");
return (btm_sec_start_authorization (p_dev_rec));
}{...}
}{...}
p_dev_rec->security_required &= ~(BTM_SEC_OUT_AUTHORIZE | BTM_SEC_IN_AUTHORIZE |
BTM_SEC_OUT_AUTHENTICATE | BTM_SEC_IN_AUTHENTICATE |
BTM_SEC_OUT_ENCRYPT | BTM_SEC_IN_ENCRYPT |
BTM_SEC_FORCE_MASTER | BTM_SEC_ATTEMPT_MASTER |
BTM_SEC_FORCE_SLAVE | BTM_SEC_ATTEMPT_SLAVE);
BTM_TRACE_EVENT ("Security Manager: trusted:0x%04x%04x\n", p_dev_rec->trusted_mask[1], p_dev_rec->trusted_mask[0]);
BTM_TRACE_EVENT ("Security Manager: access granted\n");
return (BTM_SUCCESS);
}{ ... }
/* ... */
static BOOLEAN btm_sec_start_get_name (tBTM_SEC_DEV_REC *p_dev_rec)
{
UINT8 tempstate = p_dev_rec->sec_state;
p_dev_rec->sec_state = BTM_SEC_STATE_GETTING_NAME;
if ((btm_initiate_rem_name (p_dev_rec->bd_addr, NULL, BTM_RMT_NAME_SEC,
0, NULL)) != BTM_CMD_STARTED) {
p_dev_rec->sec_state = tempstate;
return (FALSE);
}{...}
return (TRUE);
}{ ... }
/* ... */
static BOOLEAN btm_sec_start_authentication (tBTM_SEC_DEV_REC *p_dev_rec)
{
p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
return (btsnd_hcic_auth_request (p_dev_rec->hci_handle));
}{ ... }
/* ... */
static BOOLEAN btm_sec_start_encryption (tBTM_SEC_DEV_REC *p_dev_rec)
{
if (!btsnd_hcic_set_conn_encrypt (p_dev_rec->hci_handle, TRUE)) {
return (FALSE);
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
return (TRUE);
}{ ... }
/* ... */
static UINT8 btm_sec_start_authorization (tBTM_SEC_DEV_REC *p_dev_rec)
{
UINT8 result;
UINT8 *p_service_name = NULL;
UINT8 service_id;
if ((p_dev_rec->sec_flags & BTM_SEC_NAME_KNOWN)
|| (p_dev_rec->hci_handle == BTM_SEC_INVALID_HANDLE)) {
if (!btm_cb.api.p_authorize_callback) {
return (BTM_MODE_UNSUPPORTED);
}{...}
if (p_dev_rec->p_cur_service) {
#if BTM_SEC_SERVICE_NAME_LEN > 0
if (p_dev_rec->is_originator) {
p_service_name = p_dev_rec->p_cur_service->orig_service_name;
}{...} else {
p_service_name = p_dev_rec->p_cur_service->term_service_name;
}{...}
#endif/* ... */
service_id = p_dev_rec->p_cur_service->service_id;
}{...} else {
service_id = 0;
}{...}
if (p_dev_rec->last_author_service_id == BTM_SEC_NO_LAST_SERVICE_ID
|| p_dev_rec->last_author_service_id != service_id) {
p_dev_rec->sec_state = BTM_SEC_STATE_AUTHORIZING;
result = (*btm_cb.api.p_authorize_callback) (p_dev_rec->bd_addr,
p_dev_rec->dev_class,
p_dev_rec->sec_bd_name,
p_service_name,
service_id,
p_dev_rec->is_originator);
}{...}
else {
BTM_TRACE_DEBUG ("btm_sec_start_authorization: (Ignoring extra Authorization prompt for service %d)\n", service_id);
return (BTM_SUCCESS);
}{...}
if (result == BTM_SUCCESS) {
p_dev_rec->sec_flags |= BTM_SEC_AUTHORIZED;
if (!p_dev_rec->is_originator) {
p_dev_rec->last_author_service_id = service_id;
}{...}
p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
}{...}
return (result);
}{...}
btm_sec_start_get_name (p_dev_rec);
return (BTM_CMD_STARTED);
}{ ... }
#endif/* ... */
/* ... */
BOOLEAN btm_sec_are_all_trusted(UINT32 p_mask[])
{
UINT32 trusted_inx;
for (trusted_inx = 0; trusted_inx < BTM_SEC_SERVICE_ARRAY_SIZE; trusted_inx++) {
if (p_mask[trusted_inx] != BTM_SEC_TRUST_ALL) {
return (FALSE);
}{...}
}{...}
return (TRUE);
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
tBTM_SEC_SERV_REC *btm_sec_find_first_serv (CONNECTION_TYPE conn_type, UINT16 psm)
{
tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
int i;
BOOLEAN is_originator;
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( conn_type & CONNECTION_TYPE_ORIG_MASK ) {
is_originator = TRUE;
}{...} else {
is_originator = FALSE;
}{...}
#else/* ... */
is_originator = conn_type;
#endif
if (is_originator && btm_cb.p_out_serv && btm_cb.p_out_serv->psm == psm) {
/* ... */
return btm_cb.p_out_serv;
}{...}
for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
if ( (p_serv_rec->security_flags & BTM_SEC_IN_USE) && (p_serv_rec->psm == psm) ) {
return (p_serv_rec);
}{...}
}{...}
return (NULL);
}{ ... }
/* ... */
static tBTM_SEC_SERV_REC *btm_sec_find_next_serv (tBTM_SEC_SERV_REC *p_cur)
{
tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
int i;
for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
if ((p_serv_rec->security_flags & BTM_SEC_IN_USE)
&& (p_serv_rec->psm == p_cur->psm) ) {
if (p_cur != p_serv_rec) {
return (p_serv_rec);
}{...}
}{...}
}{...}
return (NULL);
}{ ... }
/* ... */
static tBTM_SEC_SERV_REC *btm_sec_find_mx_serv (UINT8 is_originator, UINT16 psm,
UINT32 mx_proto_id, UINT32 mx_chan_id)
{
tBTM_SEC_SERV_REC *p_out_serv = btm_cb.p_out_serv;
tBTM_SEC_SERV_REC *p_serv_rec = &btm_cb.sec_serv_rec[0];
int i;
BTM_TRACE_DEBUG ("%s()\n", __func__);
if (is_originator && p_out_serv && p_out_serv->psm == psm
&& p_out_serv->mx_proto_id == mx_proto_id
&& p_out_serv->orig_mx_chan_id == mx_chan_id) {
/* ... */
return btm_cb.p_out_serv;
}{...}
for (i = 0; i < BTM_SEC_MAX_SERVICE_RECORDS; i++, p_serv_rec++) {
if ((p_serv_rec->security_flags & BTM_SEC_IN_USE)
&& (p_serv_rec->psm == psm)
&& (p_serv_rec->mx_proto_id == mx_proto_id)
&& (( is_originator && (p_serv_rec->orig_mx_chan_id == mx_chan_id))
|| (!is_originator && (p_serv_rec->term_mx_chan_id == mx_chan_id)))) {
return (p_serv_rec);
}{...}
}{...}
return (NULL);
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_sec_collision_timeout (TIMER_LIST_ENT *p_tle)
{
UNUSED(p_tle);
BTM_TRACE_EVENT ("%s()\n", __func__);
btm_cb.sec_collision_tle.param = 0;
tBTM_STATUS status = btm_sec_execute_procedure (btm_cb.p_collided_dev_rec);
if (status != BTM_CMD_STARTED) {
btm_sec_dev_rec_cback_event (btm_cb.p_collided_dev_rec, status, FALSE);
}{...}
}{ ... }
/* ... */
static void btm_send_link_key_notif (tBTM_SEC_DEV_REC *p_dev_rec)
{
if (btm_cb.api.p_link_key_callback) {
(*btm_cb.api.p_link_key_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, p_dev_rec->link_key,
p_dev_rec->link_key_type,
p_dev_rec->remote_supports_secure_connections);
}{...}
}{ ... }
#endif/* ... */
/* ... */
UINT32 *BTM_ReadTrustedMask (BD_ADDR bd_addr)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
if (p_dev_rec != NULL) {
return (p_dev_rec->trusted_mask);
}{...}
return NULL;
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_restore_mode(void)
{
if (btm_cb.security_mode_changed) {
btm_cb.security_mode_changed = FALSE;
BTM_TRACE_DEBUG("%s() Auth enable -> %d\n", __func__, (btm_cb.security_mode == BTM_SEC_MODE_LINK));
btsnd_hcic_write_auth_enable ((UINT8)(btm_cb.security_mode == BTM_SEC_MODE_LINK));
}{...}
#if (CLASSIC_BT_INCLUDED == TRUE)
if (btm_cb.pin_type_changed) {
btm_cb.pin_type_changed = FALSE;
btsnd_hcic_write_pin_type (btm_cb.cfg.pin_type);
}{...}
#endif/* ... */
}{ ... }
/* ... */#endif
/* ... */
tBTM_SEC_DEV_REC *btm_sec_find_dev_by_sec_state (UINT8 state)
{
#if (SMP_INCLUDED == TRUE)
tBTM_SEC_DEV_REC *p_dev_rec = NULL;
list_node_t *p_node = NULL;
for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
p_dev_rec = list_node(p_node);
if ((p_dev_rec->sec_flags & BTM_SEC_IN_USE)
&& (p_dev_rec->sec_state == state)) {
return (p_dev_rec);
}{...}
}{...}
/* ... */#endif
return (NULL);
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
static void btm_sec_change_pairing_state (tBTM_PAIRING_STATE new_state)
{
tBTM_PAIRING_STATE old_state = btm_cb.pairing_state;
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
BTM_TRACE_EVENT ("%s() Old: %s\n", __func__, btm_pair_state_descr(btm_cb.pairing_state));
BTM_TRACE_EVENT ("%s() New: %s pairing_flags:0x%x\n\n", __func__,
btm_pair_state_descr(new_state), btm_cb.pairing_flags);/* ... */
#endif
btm_cb.pairing_state = new_state;
if (new_state == BTM_PAIR_STATE_IDLE) {
btu_stop_timer (&btm_cb.pairing_tle);
btm_cb.pairing_flags = 0;
#if (CLASSIC_BT_INCLUDED == TRUE)
btm_cb.pin_code_len = 0;
#endif
l2cu_update_lcb_4_bonding (btm_cb.pairing_bda, FALSE);
btm_restore_mode();
btm_sec_check_pending_reqs();
btm_inq_clear_ssp();
memset (btm_cb.pairing_bda, 0xFF, BD_ADDR_LEN);
}{...} else {
if (old_state == BTM_PAIR_STATE_IDLE) {
l2cu_update_lcb_4_bonding (btm_cb.pairing_bda, TRUE);
}{...}
btm_cb.pairing_tle.param = (TIMER_PARAM_TYPE)btm_sec_pairing_timeout;
btu_start_timer (&btm_cb.pairing_tle, BTU_TTYPE_USER_FUNC, BTM_SEC_TIMEOUT_VALUE);
}{...}
}{ ... }
/* ... */#endif
/* ... */
#if (BT_USE_TRACES == TRUE && SMP_INCLUDED == TRUE)
static char *btm_pair_state_descr (tBTM_PAIRING_STATE state)
{
#if (BT_TRACE_VERBOSE == TRUE)
switch (state) {
case BTM_PAIR_STATE_IDLE: return ("IDLE");
case BTM_PAIR_STATE_GET_REM_NAME: return ("GET_REM_NAME");
case BTM_PAIR_STATE_WAIT_PIN_REQ: return ("WAIT_PIN_REQ");
case BTM_PAIR_STATE_WAIT_LOCAL_PIN: return ("WAIT_LOCAL_PIN");
case BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM: return ("WAIT_NUM_CONFIRM");
case BTM_PAIR_STATE_KEY_ENTRY: return ("KEY_ENTRY");
case BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP: return ("WAIT_LOCAL_OOB_RSP");
case BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS: return ("WAIT_LOCAL_IOCAPS");
case BTM_PAIR_STATE_INCOMING_SSP: return ("INCOMING_SSP");
case BTM_PAIR_STATE_WAIT_AUTH_COMPLETE: return ("WAIT_AUTH_COMPLETE");
case BTM_PAIR_STATE_WAIT_DISCONNECT: return ("WAIT_DISCONNECT");
}{...}
return ("???");/* ... */
#else
sprintf(btm_cb.state_temp_buffer, "%d", state);
return (btm_cb.state_temp_buffer);/* ... */
#endif
}{...}
/* ... */#endif
/* ... */
void btm_sec_dev_rec_cback_event (tBTM_SEC_DEV_REC *p_dev_rec, UINT8 res, BOOLEAN is_le_transport)
{
tBTM_SEC_CALLBACK *p_callback;
if (p_dev_rec && p_dev_rec->p_callback) {
p_callback = p_dev_rec->p_callback;
p_dev_rec->p_callback = NULL;
#if BLE_INCLUDED == TRUE
if (is_le_transport) {
(*p_callback) (p_dev_rec->ble.pseudo_addr, BT_TRANSPORT_LE, p_dev_rec->p_ref_data, res);
}{...} else
#endif
{
(*p_callback) (p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR, p_dev_rec->p_ref_data, res);
}{...}
}{...}
#if (SMP_INCLUDED == TRUE)
btm_sec_check_pending_reqs();
#endif
}{ ... }
/* ... */
#if (SMP_INCLUDED == TRUE)
static BOOLEAN btm_sec_queue_mx_request (BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_orig,
UINT32 mx_proto_id, UINT32 mx_chan_id,
tBTM_SEC_CALLBACK *p_callback, void *p_ref_data)
{
tBTM_SEC_QUEUE_ENTRY *p_e = (tBTM_SEC_QUEUE_ENTRY *)osi_malloc (sizeof(tBTM_SEC_QUEUE_ENTRY));
if (p_e) {
p_e->psm = psm;
p_e->is_orig = is_orig;
p_e->p_callback = p_callback;
p_e->p_ref_data = p_ref_data;
p_e->mx_proto_id = mx_proto_id;
p_e->mx_chan_id = mx_chan_id;
p_e->transport = BT_TRANSPORT_BR_EDR;
memcpy (p_e->bd_addr, bd_addr, BD_ADDR_LEN);
BTM_TRACE_EVENT ("%s() PSM: 0x%04x Is_Orig: %u mx_proto_id: %u mx_chan_id: %u\n",
__func__, psm, is_orig, mx_proto_id, mx_chan_id);
fixed_queue_enqueue(btm_cb.sec_pending_q, p_e, FIXED_QUEUE_MAX_TIMEOUT);
return (TRUE);
}{...}
return (FALSE);
}{ ... }
static BOOLEAN btm_sec_check_prefetch_pin (tBTM_SEC_DEV_REC *p_dev_rec)
{
BOOLEAN rv = FALSE;
#if (CLASSIC_BT_INCLUDED == TRUE)
UINT8 major = (UINT8)(p_dev_rec->dev_class[1] & BTM_COD_MAJOR_CLASS_MASK);
UINT8 minor = (UINT8)(p_dev_rec->dev_class[2] & BTM_COD_MINOR_CLASS_MASK);
rv = TRUE;
if ((major == BTM_COD_MAJOR_AUDIO)
&& ((minor == BTM_COD_MINOR_CONFM_HANDSFREE) || (minor == BTM_COD_MINOR_CAR_AUDIO)) ) {
BTM_TRACE_EVENT ("%s() Skipping pre-fetch PIN for carkit COD Major: 0x%02x Minor: 0x%02x\n",
__func__, major, minor);
if (btm_cb.security_mode_changed == FALSE) {
btm_cb.security_mode_changed = TRUE;
#ifdef APPL_AUTH_WRITE_EXCEPTION
if (!(APPL_AUTH_WRITE_EXCEPTION)(p_dev_rec->bd_addr))
#endif
{
btsnd_hcic_write_auth_enable (TRUE);
}{...}
}{...}
}{...} else {
btm_sec_change_pairing_state (BTM_PAIR_STATE_WAIT_LOCAL_PIN);
if (btm_cb.pin_code_len) {
BTM_PINCodeReply (p_dev_rec->bd_addr, BTM_SUCCESS, btm_cb.pin_code_len, btm_cb.pin_code, p_dev_rec->trusted_mask);
}{...} else {
if (btm_cb.api.p_pin_callback && ((btm_cb.pairing_flags & BTM_PAIR_FLAGS_PIN_REQD) == 0)) {
BTM_TRACE_DEBUG("%s() PIN code callback called\n", __func__);
if (btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR) == NULL) {
btm_cb.pairing_flags |= BTM_PAIR_FLAGS_PIN_REQD;
}{...}
(btm_cb.api.p_pin_callback) (p_dev_rec->bd_addr, p_dev_rec->dev_class,
p_dev_rec->sec_bd_name, (p_dev_rec->p_cur_service == NULL) ? FALSE
: (p_dev_rec->p_cur_service->security_flags
& BTM_SEC_IN_MIN_16_DIGIT_PIN));
}{...}
}{...}
rv = TRUE;
}{...}
#endif/* ... */
#
return rv;
}{ ... }
/* ... */
void btm_sec_auth_payload_tout (UINT8 *p, UINT16 hci_evt_len)
{
UINT16 handle;
STREAM_TO_UINT16 (handle, p);
handle = HCID_GET_HANDLE (handle);
BTM_TRACE_ERROR ("%s on handle 0x%02x\n", __func__, handle);
}{ ... }
/* ... */
static BOOLEAN btm_sec_queue_encrypt_request (BD_ADDR bd_addr, tBT_TRANSPORT transport,
tBTM_SEC_CALLBACK *p_callback, void *p_ref_data)
{
tBTM_SEC_QUEUE_ENTRY *p_e;
p_e = (tBTM_SEC_QUEUE_ENTRY *)osi_malloc(sizeof(tBTM_SEC_QUEUE_ENTRY) + 1);
if (p_e) {
p_e->psm = 0;
p_e->p_callback = p_callback;
p_e->p_ref_data = (void *)(p_e + 1);
*(UINT8 *)p_e->p_ref_data = *(UINT8 *)(p_ref_data);
p_e->transport = transport;
memcpy(p_e->bd_addr, bd_addr, BD_ADDR_LEN);
fixed_queue_enqueue(btm_cb.sec_pending_q, p_e, FIXED_QUEUE_MAX_TIMEOUT);
return TRUE;
}{...}
return FALSE;
}{ ... }
/* ... */
void btm_sec_set_peer_sec_caps(tACL_CONN *p_acl_cb, tBTM_SEC_DEV_REC *p_dev_rec)
{
BD_ADDR rem_bd_addr;
UINT8 *p_rem_bd_addr;
if ((btm_cb.security_mode == BTM_SEC_MODE_SP ||
btm_cb.security_mode == BTM_SEC_MODE_SP_DEBUG ||
btm_cb.security_mode == BTM_SEC_MODE_SC) &&
HCI_SSP_HOST_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_1])) {
p_dev_rec->sm4 = BTM_SM4_TRUE;
p_dev_rec->remote_supports_secure_connections =
(HCI_SC_HOST_SUPPORTED(p_acl_cb->peer_lmp_features[HCI_EXT_FEATURES_PAGE_1]));
}{...} else {
p_dev_rec->sm4 = BTM_SM4_KNOWN;
p_dev_rec->remote_supports_secure_connections = FALSE;
}{...}
BTM_TRACE_API("%s: sm4: 0x%02x, rmt_support_for_secure_connections %d\n", __FUNCTION__,
p_dev_rec->sm4, p_dev_rec->remote_supports_secure_connections);
/* ... */
#if (CLASSIC_BT_INCLUDED == TRUE)
if (p_dev_rec->remote_supports_secure_connections >= p_dev_rec->remote_secure_connection_previous_state) {
p_dev_rec->remote_secure_connection_previous_state = p_dev_rec->remote_supports_secure_connections;
}{...} else {
BTM_TRACE_ERROR("Remote Device downgraded security from SC, deleting Link Key");
p_acl_cb->sc_downgrade = 1;
p_dev_rec->remote_secure_connection_previous_state = 0;
/* ... */
memset(p_dev_rec->link_key, 0, LINK_KEY_LEN);
p_dev_rec->sec_flags &= ~(BTM_SEC_AUTHORIZED | BTM_SEC_AUTHENTICATED
| BTM_SEC_ENCRYPTED | BTM_SEC_NAME_KNOWN
| BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LINK_KEY_AUTHED
| BTM_SEC_ROLE_SWITCHED | BTM_SEC_16_DIGIT_PIN_AUTHED);
return;
}{...}
#endif/* ... */
if (p_dev_rec->remote_features_needed) {
BTM_TRACE_EVENT("%s: Now device in SC Only mode, waiting for peer remote features!\n",
__FUNCTION__);
p_rem_bd_addr = (UINT8 *) rem_bd_addr;
BDADDR_TO_STREAM(p_rem_bd_addr, p_dev_rec->bd_addr);
p_rem_bd_addr = (UINT8 *) rem_bd_addr;
btm_io_capabilities_req(p_rem_bd_addr);
p_dev_rec->remote_features_needed = FALSE;
}{...}
}{ ... }
/* ... */
static BOOLEAN btm_sec_is_serv_level0(UINT16 psm)
{
if (psm == BT_PSM_SDP) {
BTM_TRACE_DEBUG("%s: PSM: 0x%04x -> mode 4 level 0 service\n", __FUNCTION__, psm);
return TRUE;
}{...}
return FALSE;
}{ ... }
/* ... */
static void btm_sec_check_pending_enc_req (tBTM_SEC_DEV_REC *p_dev_rec, tBT_TRANSPORT transport,
UINT8 encr_enable)
{
if (fixed_queue_is_empty(btm_cb.sec_pending_q)) {
return;
}{...}
UINT8 res = encr_enable ? BTM_SUCCESS : BTM_ERR_PROCESSING;
list_t *list = fixed_queue_get_list(btm_cb.sec_pending_q);
for (const list_node_t *node = list_begin(list); node != list_end(list); ) {
tBTM_SEC_QUEUE_ENTRY *p_e = (tBTM_SEC_QUEUE_ENTRY *)list_node(node);
node = list_next(node);
if (memcmp(p_e->bd_addr, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0 && p_e->psm == 0
#if BLE_INCLUDED == TRUE
&& p_e->transport == transport
#endif
) {
#if BLE_INCLUDED == TRUE
UINT8 sec_act = *(UINT8 *)(p_e->p_ref_data);
#endif
if (encr_enable == 0 || transport == BT_TRANSPORT_BR_EDR
#if BLE_INCLUDED == TRUE
|| (sec_act == BTM_BLE_SEC_ENCRYPT || sec_act == BTM_BLE_SEC_ENCRYPT_NO_MITM)
|| (sec_act == BTM_BLE_SEC_ENCRYPT_MITM && p_dev_rec->sec_flags
& BTM_SEC_LE_AUTHENTICATED)/* ... */
#endif
) {
if (p_e->p_callback) {
(*p_e->p_callback) (p_dev_rec->bd_addr, transport, p_e->p_ref_data, res);
}{...}
fixed_queue_try_remove_from_queue(btm_cb.sec_pending_q, (void *)p_e);
}{...}
}{...}
}{...}
}{ ... }
#endif/* ... */
/* ... */
#if (SMP_INCLUDED == TRUE)
static UINT16 btm_sec_set_serv_level4_flags(UINT16 cur_security, BOOLEAN is_originator)
{
UINT16 sec_level4_flags = is_originator ? BTM_SEC_OUT_LEVEL4_FLAGS : BTM_SEC_IN_LEVEL4_FLAGS;
return cur_security | sec_level4_flags;
}{ ... }
/* ... */#endif
/* ... */
#if (BLE_INCLUDED == TRUE)
void btm_sec_clear_ble_keys (tBTM_SEC_DEV_REC *p_dev_rec)
{
BTM_TRACE_DEBUG ("%s() Clearing BLE Keys\n", __func__);
#if (SMP_INCLUDED== TRUE)
p_dev_rec->ble.key_type = BTM_LE_KEY_NONE;
memset (&p_dev_rec->ble.keys, 0, sizeof(tBTM_SEC_BLE_KEYS));
#if (BLE_PRIVACY_SPT == TRUE)
btm_ble_resolving_list_remove_dev(p_dev_rec);
#endif/* ... */
#endif
}{ ... }
/* ... */#endif
/* ... */
BOOLEAN btm_sec_is_a_bonded_dev (BD_ADDR bda)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
BOOLEAN is_bonded = FALSE;
if (p_dev_rec &&
#if (SMP_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
((p_dev_rec->ble.key_type && (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_KNOWN)) ||
#else
(
#endif
(p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN))) {
is_bonded = TRUE;
}{...}
BTM_TRACE_DEBUG ("%s() is_bonded=%d\n", __func__, is_bonded);
return (is_bonded);
}{ ... }
/* ... */
BOOLEAN btm_sec_is_le_capable_dev (BD_ADDR bda)
{
BOOLEAN le_capable = FALSE;
#if (BLE_INCLUDED== TRUE)
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
if (p_dev_rec && (p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) {
le_capable = TRUE;
}{...}
/* ... */#endif
return le_capable;
}{...}
/* ... */
#if (BLE_INCLUDED == TRUE)
BOOLEAN btm_sec_find_bonded_dev (UINT8 start_idx, UINT16 *p_found_handle, tBTM_SEC_DEV_REC **p_rec)
{
BOOLEAN found = FALSE;
#if (SMP_INCLUDED== TRUE)
tBTM_SEC_DEV_REC *p_dev_rec;
list_node_t *p_node = NULL;
for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
p_dev_rec = list_node(p_node);
if (p_dev_rec->ble.key_type || (p_dev_rec->sec_flags & BTM_SEC_LINK_KEY_KNOWN)) {
*p_found_handle = p_dev_rec->hci_handle;
*p_rec = p_dev_rec;
break;
}{...}
}{...}
BTM_TRACE_DEBUG ("%s() found=%d\n", __func__, found);/* ... */
#endif
return (found);
}{...}
/* ... */#endif
/* ... */
#if (SMP_INCLUDED == TRUE)
static BOOLEAN btm_sec_use_smp_br_chnl(tBTM_SEC_DEV_REC *p_dev_rec)
{
UINT32 ext_feat;
UINT8 chnl_mask[L2CAP_FIXED_CHNL_ARRAY_SIZE];
BTM_TRACE_DEBUG ("%s() link_key_type = 0x%x\n", __func__,
p_dev_rec->link_key_type);
if ((p_dev_rec->link_key_type != BTM_LKEY_TYPE_UNAUTH_COMB_P_256) &&
(p_dev_rec->link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256)) {
return FALSE;
}{...}
if (!L2CA_GetPeerFeatures (p_dev_rec->bd_addr, &ext_feat, chnl_mask)) {
return FALSE;
}{...}
if (!(chnl_mask[0] & L2CAP_FIXED_CHNL_SMP_BR_BIT)) {
return FALSE;
}{...}
return TRUE;
}{...}
/* ... */
static BOOLEAN btm_sec_is_master(tBTM_SEC_DEV_REC *p_dev_rec)
{
tACL_CONN *p = btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR);
return (p && (p->link_role == BTM_ROLE_MASTER));
}{...}
#if (CLASSIC_BT_INCLUDED == TRUE)
/* ... */
BOOLEAN btm_sec_legacy_authentication_mutual (tBTM_SEC_DEV_REC *p_dev_rec)
{
return (btm_sec_start_authentication (p_dev_rec));
}{...}
/* ... */
void btm_sec_update_legacy_auth_state(tACL_CONN *p_acl_cb, UINT8 legacy_auth_state)
{
if (p_acl_cb) {
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (p_acl_cb->hci_handle);
if (p_dev_rec) {
if ((BTM_BothEndsSupportSecureConnections(p_dev_rec->bd_addr) == 0) &&
(legacy_auth_state != BTM_ACL_LEGACY_AUTH_NONE)) {
p_acl_cb->legacy_auth_state |= legacy_auth_state;
}{...} else {
p_acl_cb->legacy_auth_state = BTM_ACL_LEGACY_AUTH_NONE;
}{...}
}{...}
}{...}
}{...}
/* ... */
void btm_sec_handle_remote_legacy_auth_cmp(UINT16 handle)
{
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
tACL_CONN *p_acl_cb = btm_bda_to_acl(p_dev_rec->bd_addr, BT_TRANSPORT_BR_EDR);
btm_sec_update_legacy_auth_state(p_acl_cb, BTM_ACL_LEGACY_AUTH_REMOTE);
}{...}
/* ... */#endif /* ... */
#endif
/* ... */
#if (BLE_INCLUDED == TRUE)
BOOLEAN btm_sec_dev_authorization(BD_ADDR bd_addr, BOOLEAN authorized)
{
#if (SMP_INCLUDED == TRUE)
UINT8 sec_flag = 0;
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
if (p_dev_rec) {
sec_flag = (UINT8)(p_dev_rec->sec_flags >> 8);
if (!(sec_flag & BTM_SEC_LINK_KEY_AUTHED)) {
BTM_TRACE_ERROR("Authorized should after successful Authentication(MITM protection)\n");
return FALSE;
}{...}
if (authorized) {
p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHORIZATION;
}{...} else {
p_dev_rec->sec_flags &= ~(BTM_SEC_LE_AUTHORIZATION);
}{...}
}{...} else {
BTM_TRACE_ERROR("%s, can't find device\n", __func__);
return FALSE;
}{...}
return TRUE;/* ... */
#endif
return FALSE;
}{...}
/* ... */#endif