1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
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
1749
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
1794
1795
1796
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
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
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
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
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
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
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
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
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
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
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
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
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
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
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
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
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
3242
3243
3244
3245
3246
3247
3248
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
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
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
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
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
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
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
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
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
3760
3761
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
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
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
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
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
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
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
4246
4247
4248
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
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
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
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
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
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
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
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
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
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
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
4867
4868
4869
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
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
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
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
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
5625
5626
5627
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
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
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
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
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
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
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
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
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
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
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
6146
6147
6148
6149
6150
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
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
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
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
//! keyroost — desktop GUI for programming Token2 Molto2 / Molto2v2 tokens.
//!
//! Dark-themed by default, modeled loosely on Token2's PyQt5 layout: device
//! status across the top, 100-slot grid on the left, edit form on the right.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::time::{SystemTime, UNIX_EPOCH};
mod ui;
use ui::device::{self, CapTab, Caps, DeviceId, DeviceKind, UiDevice};
use ui::theme::{self, BtnKind, Mode, Palette};
use eframe::egui;
use keyroost_import::parse_otpauth;
use keyroost_proto::commands::{
DisplayTimeout, HmacAlgo, OtpDigits, ProfileConfig, TimeStep, DEFAULT_CUSTOMER_KEY,
};
use keyroost_transport::{DeviceInfo, Session, TransportError};
use keyroost_ctap::client_pin::PinUvAuthToken;
use keyroost_ctap::cred_mgmt::{Credential, CredsMetadata, RelyingParty};
use keyroost_ctap::{AuthenticatorInfo, CtapHidDevice, InitResponse};
use keyroost_hid::HidDevice;
const PROFILES: u8 = 100;
#[derive(Default)]
struct SecurityKeysState {
/// CTAP info for the selected device, fetched lazily after selection.
info: Option<AuthenticatorInfo>,
init: Option<InitResponse>,
/// User-facing error from the last enumeration / open / GetInfo call.
error: Option<String>,
/// Live PIN entry field (cleared after submit).
pin_input: String,
/// Active unlocked session: token + cached resident credentials.
session: Option<UnlockedSession>,
/// Change-PIN modal state.
change_pin: ChangePinDialog,
/// Reset-confirmation modal state.
reset: ResetDialog,
}
/// State for the "reset key" confirmation. Reset wipes all credentials and the
/// PIN, so the user must type a confirmation word and then touch the key.
#[derive(Default)]
struct ResetDialog {
open: bool,
/// Typed confirmation (`reset` required to enable the button).
confirm_input: String,
}
/// "Armed" reset: a FIDO authenticator only accepts a reset within ~10 s of
/// being powered on, which the plug-then-navigate-then-confirm flow can never
/// hit. So we confirm intent first, then watch for the user to unplug and
/// replug the key and fire the reset the instant it reconnects. Polled in
/// `update()` against the live FIDO HID list (no PC/SC, so it can't disturb
/// other cards).
struct ResetArm {
/// The armed key's USB/HID serial, if it exposes one (SoloKeys / Nitrokey
/// do; most YubiKeys don't). When present we track the key by serial, so it
/// is recognised on re-insertion into *any* USB port, not just the same one.
target_serial: Option<String>,
/// The armed key's HID path at arm time. Used as the identity when there is
/// no serial (`target_serial` is `None`): its disappearance is the "unplug"
/// half of the dance, and a fresh path is treated as the re-insert.
target_path: Option<std::path::PathBuf>,
/// The armed key's USB vendor/product ids, captured at arm time. In path
/// mode a fresh path must match these — so plugging in a *different*
/// model while a reset is armed doesn't get it reset by mistake.
target_ids: Option<(u16, u16)>,
/// FIDO HID paths present at the previous poll, to diff against (path mode).
prev_paths: Vec<std::path::PathBuf>,
/// True once the armed key has been unplugged; the next fresh insertion then
/// fires the reset.
saw_removal: bool,
}
struct UnlockedSession {
token: PinUvAuthToken,
metadata: CredsMetadata,
/// Behind an `Arc`: the pane clones this every frame to escape the borrow
/// of `self`, and credential lists (ids, user blobs) are not per-frame
/// clone material.
rps: std::sync::Arc<Vec<(RelyingParty, Vec<Credential>)>>,
}
/// One FIDO HID device as the armed-reset poll sees it.
struct FidoHid {
path: std::path::PathBuf,
serial: Option<String>,
ids: (u16, u16),
}
#[derive(Default)]
struct ChangePinDialog {
/// Whether the inline PIN form is expanded.
open: bool,
old: String,
new: String,
/// Confirmation field, used only when *setting* a first-time PIN.
confirm: String,
}
/// State for the OATH (TOTP) pane. The applet is driven over PC/SC, so a
/// "reader name" identifies the key rather than a hidraw path.
#[derive(Default)]
struct OathState {
/// Credentials listed from the selected key, with their freshly-computed code.
creds: Vec<OathRow>,
/// True when the selected applet is password-protected and not yet unlocked.
locked: bool,
/// Password entry field (cleared after an unlock attempt).
password_input: String,
/// User-facing error/status from the last OATH operation.
error: Option<String>,
/// True once a list has been fetched for the current selection.
loaded: bool,
/// "Add credential" dialog state.
add: OathAddDialog,
/// Credential name awaiting a delete confirmation, if any.
confirm_delete: Option<String>,
}
/// One credential row in the OATH pane: its stored name and the last code we
/// computed for it (empty until "Show code" / refresh).
struct OathRow {
name: String,
code: Option<String>,
}
/// "Add credential" form state for the OATH pane.
#[derive(Default)]
struct OathAddDialog {
open: bool,
name: String,
/// Base32 secret (entered masked).
secret: String,
/// True = TOTP, false = HOTP.
totp: bool,
require_touch: bool,
}
// The form is replaced wholesale after submit; wipe the typed seed on drop.
impl Drop for OathAddDialog {
fn drop(&mut self) {
wipe(&mut self.secret);
}
}
/// State for the OpenPGP pane. Like OATH, the applet is driven over PC/SC, so a
/// reader name identifies the card.
#[derive(Default)]
struct OpenPgpState {
/// Last status read from the selected card.
status: Option<keyroost_transport::OpenPgpStatus>,
/// User-facing error from the last operation.
error: Option<String>,
/// Success/info line from the last write operation.
notice: Option<String>,
/// True once a status has been fetched for the current selection.
loaded: bool,
/// Admin PIN (PW3) entry, used for every write op. Cleared after use.
admin_pin: String,
/// Cardholder-name entry.
name_input: String,
/// Public-key-URL entry.
url_input: String,
/// Slot selected in the generate-key control.
gen_slot: OpenPgpSlotSel,
/// Generate-key confirmation modal state.
confirm_generate: bool,
/// Slot selected in the import-key control.
import_slot: OpenPgpSlotSel,
/// Path to an RSA key file for import-from-file (text-entered).
import_path: String,
/// Import confirmation modal: the chosen key source (modal open iff `Some`).
confirm_import: Option<ImportSource>,
/// Reset confirmation modal: typed-`reset` text (modal open iff `Some`).
confirm_reset: Option<String>,
/// Change-user-PIN (PW1) old/new entries. Cleared after use.
user_pin_old: String,
user_pin_new: String,
/// Change-admin-PIN (PW3) old/new entries. Cleared after use.
admin_pin_old: String,
admin_pin_new: String,
/// New user PIN for the unblock flow (reset retry counter); authorised by
/// `admin_pin`. Cleared after use.
unblock_new: String,
}
impl OpenPgpState {
/// Zeroize every PIN entry field. Called when the selection changes (a PIN
/// typed for one card must not ride along to another) and on drop.
fn wipe_secrets(&mut self) {
wipe(&mut self.admin_pin);
wipe(&mut self.user_pin_old);
wipe(&mut self.user_pin_new);
wipe(&mut self.admin_pin_old);
wipe(&mut self.admin_pin_new);
wipe(&mut self.unblock_new);
}
}
/// Zeroize a secret-bearing text field (wipes the bytes, then leaves the
/// string empty — strictly better than `.clear()`, which only resets the
/// length and leaves the secret in the buffer).
fn wipe(s: &mut String) {
use zeroize::Zeroize;
s.zeroize();
}
/// Where an OpenPGP key import gets its key material. Mirrors the CLI's
/// `--generate` / `--in <FILE>` choice.
#[derive(Clone, Copy, PartialEq, Eq)]
enum ImportSource {
/// Generate a fresh RSA-2048 key on the host.
Generate,
/// Load an RSA-2048 key from a file (PKCS#1/PKCS#8, PEM or DER).
FromFile,
}
/// Which key slot a GUI generate targets. Mirrors the CLI's slot choice.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
enum OpenPgpSlotSel {
#[default]
Sign,
Decrypt,
Auth,
}
impl OpenPgpSlotSel {
fn to_crt(self) -> keyroost_transport::KeyCrt {
match self {
OpenPgpSlotSel::Sign => keyroost_transport::KeyCrt::Sign,
OpenPgpSlotSel::Decrypt => keyroost_transport::KeyCrt::Decrypt,
OpenPgpSlotSel::Auth => keyroost_transport::KeyCrt::Auth,
}
}
fn label(self) -> &'static str {
match self {
OpenPgpSlotSel::Sign => "signature",
OpenPgpSlotSel::Decrypt => "decryption",
OpenPgpSlotSel::Auth => "authentication",
}
}
fn from_label(s: &str) -> Self {
match s {
"decryption" => OpenPgpSlotSel::Decrypt,
"authentication" => OpenPgpSlotSel::Auth,
_ => OpenPgpSlotSel::Sign,
}
}
}
/// State for the PIV pane: a status snapshot plus the entry fields for the full
/// management surface (PIN/PUK, management key, key generation, certificate
/// import/export, reset), keyed by the selected device's reader name.
struct PivState {
/// Last status read from the selected card.
status: Option<keyroost_transport::PivStatus>,
/// User-facing error from the last read/write.
error: Option<String>,
/// Success/info line from the last write operation.
notice: Option<String>,
/// True once a status has been fetched for the current selection.
loaded: bool,
/// Management key (hex) entered to authorize key-gen / cert-import /
/// set-retries / management-key change. Cleared after use.
mgmt_key_input: String,
/// Change-PIN old/new entries. Cleared after use.
pin_old: String,
pin_new: String,
/// Change-PUK old/new entries. Cleared after use.
puk_old: String,
puk_new: String,
/// Unblock-PIN: PUK + new PIN entries. Cleared after use.
unblock_puk: String,
unblock_new_pin: String,
/// PIN/PUK retry counts for set-retries, and the PIN that authorizes it.
retries_pin: u8,
retries_puk: u8,
retries_pin_auth: String,
/// Key-generation slot + algorithm selectors.
gen_slot: PivSlotSel,
gen_alg: PivKeyAlgSel,
/// PEM of the most recently generated public key, shown for copying.
gen_pubkey_pem: Option<String>,
/// Certificate import slot + file path.
cert_slot: PivSlotSel,
cert_path: String,
/// Certificate export slot + destination path.
export_slot: PivSlotSel,
export_path: String,
/// New management key (hex) + algorithm for a management-key rotation.
new_mgmt_key_input: String,
new_mgmt_alg: PivMgmtAlgSel,
/// Certificate creation: slot, subject (bare name or full DN), validity,
/// the PIN that authorizes the on-card signature, and the CSR destination.
certify_slot: PivSlotSel,
cert_subject: String,
cert_days: u32,
sign_pin: String,
csr_path: String,
/// Reset confirmation modal: typed-`reset` text (modal open iff `Some`).
confirm_reset: Option<String>,
}
// The pane is replaced wholesale on device switch (`self.piv =
// PivState::default()`); wiping on drop means the discarded fields don't
// leave PINs/keys in freed memory.
impl Drop for PivState {
fn drop(&mut self) {
wipe(&mut self.mgmt_key_input);
wipe(&mut self.pin_old);
wipe(&mut self.pin_new);
wipe(&mut self.puk_old);
wipe(&mut self.puk_new);
wipe(&mut self.unblock_puk);
wipe(&mut self.unblock_new_pin);
wipe(&mut self.retries_pin_auth);
wipe(&mut self.new_mgmt_key_input);
wipe(&mut self.sign_pin);
}
}
impl Default for PivState {
fn default() -> Self {
PivState {
status: None,
error: None,
notice: None,
loaded: false,
mgmt_key_input: String::new(),
pin_old: String::new(),
pin_new: String::new(),
puk_old: String::new(),
puk_new: String::new(),
unblock_puk: String::new(),
unblock_new_pin: String::new(),
retries_pin: 3,
retries_puk: 3,
retries_pin_auth: String::new(),
gen_slot: PivSlotSel::default(),
gen_alg: PivKeyAlgSel::default(),
gen_pubkey_pem: None,
cert_slot: PivSlotSel::default(),
cert_path: String::new(),
export_slot: PivSlotSel::default(),
export_path: String::new(),
new_mgmt_key_input: String::new(),
new_mgmt_alg: PivMgmtAlgSel::default(),
certify_slot: PivSlotSel::default(),
cert_subject: String::new(),
cert_days: 365,
sign_pin: String::new(),
csr_path: String::new(),
confirm_reset: None,
}
}
}
/// PIV key-slot selector for the GUI controls.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
enum PivSlotSel {
#[default]
Auth,
Sign,
KeyMgmt,
CardAuth,
}
impl PivSlotSel {
fn to_slot(self) -> keyroost_piv::Slot {
match self {
PivSlotSel::Auth => keyroost_piv::Slot::Authentication,
PivSlotSel::Sign => keyroost_piv::Slot::Signature,
PivSlotSel::KeyMgmt => keyroost_piv::Slot::KeyManagement,
PivSlotSel::CardAuth => keyroost_piv::Slot::CardAuthentication,
}
}
fn label(self) -> &'static str {
self.to_slot().label()
}
const ALL: [PivSlotSel; 4] = [
PivSlotSel::Auth,
PivSlotSel::Sign,
PivSlotSel::KeyMgmt,
PivSlotSel::CardAuth,
];
}
/// PIV key-generation algorithm selector.
#[derive(Clone, Copy, Default, PartialEq, Eq)]
enum PivKeyAlgSel {
#[default]
EccP256,
EccP384,
Rsa2048,
Rsa3072,
Rsa4096,
Ed25519,
}
impl PivKeyAlgSel {
fn to_alg(self) -> keyroost_piv::KeyAlg {
use keyroost_piv::KeyAlg::*;
match self {
PivKeyAlgSel::EccP256 => EccP256,
PivKeyAlgSel::EccP384 => EccP384,
PivKeyAlgSel::Rsa2048 => Rsa2048,
PivKeyAlgSel::Rsa3072 => Rsa3072,
PivKeyAlgSel::Rsa4096 => Rsa4096,
PivKeyAlgSel::Ed25519 => Ed25519,
}
}
fn label(self) -> &'static str {
self.to_alg().label()
}
const ALL: [PivKeyAlgSel; 6] = [
PivKeyAlgSel::EccP256,
PivKeyAlgSel::EccP384,
PivKeyAlgSel::Rsa2048,
PivKeyAlgSel::Rsa3072,
PivKeyAlgSel::Rsa4096,
PivKeyAlgSel::Ed25519,
];
}
/// PIV management-key algorithm selector (for rotation).
#[derive(Clone, Copy, Default, PartialEq, Eq)]
enum PivMgmtAlgSel {
#[default]
Aes192,
Aes128,
Aes256,
TripleDes,
}
impl PivMgmtAlgSel {
fn to_alg(self) -> keyroost_piv::MgmtAlg {
use keyroost_piv::MgmtAlg::*;
match self {
PivMgmtAlgSel::Aes192 => Aes192,
PivMgmtAlgSel::Aes128 => Aes128,
PivMgmtAlgSel::Aes256 => Aes256,
PivMgmtAlgSel::TripleDes => TripleDes,
}
}
fn label(self) -> &'static str {
self.to_alg().label()
}
const ALL: [PivMgmtAlgSel; 4] = [
PivMgmtAlgSel::Aes192,
PivMgmtAlgSel::Aes128,
PivMgmtAlgSel::Aes256,
PivMgmtAlgSel::TripleDes,
];
}
/// A unit of work applied back to the [`App`] on the UI thread once a background
/// job finishes. Returned by the job closure and run inside `update()`.
type ApplyFn = Box<dyn FnOnce(&mut App) + Send>;
/// A background job: blocking device I/O that yields an [`ApplyFn`].
type Job = Box<dyn FnOnce() -> ApplyFn + Send>;
/// A single background worker thread. Device calls (CTAP / OATH over PC/SC) can
/// block for seconds — a touch-required credential or a 30s Reset window — so we
/// run them off the egui frame thread and apply their results back on the UI
/// thread. One thread keeps device access serialized (no concurrent card I/O).
struct Worker {
job_tx: std::sync::mpsc::Sender<Job>,
result_rx: std::sync::mpsc::Receiver<ApplyFn>,
}
impl Worker {
fn spawn(ctx: egui::Context) -> Self {
let (job_tx, job_rx) = std::sync::mpsc::channel::<Job>();
let (result_tx, result_rx) = std::sync::mpsc::channel::<ApplyFn>();
std::thread::Builder::new()
.name("keyroost-worker".into())
.spawn(move || {
while let Ok(job) = job_rx.recv() {
let apply = job();
if result_tx.send(apply).is_err() {
break; // UI gone
}
ctx.request_repaint(); // wake the frame loop to apply it
}
})
.expect("spawn worker thread");
Worker { job_tx, result_rx }
}
}
fn main() -> eframe::Result<()> {
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([1180.0, 760.0])
.with_min_inner_size([900.0, 560.0])
.with_title("keyroost"),
..Default::default()
};
eframe::run_native(
"keyroost",
options,
Box::new(|cc| {
// Register IBM Plex Sans + JetBrains Mono so the redesign's type
// weights resolve. (Vendored under assets/.)
theme::install_fonts(&cc.egui_ctx);
// Restore the persisted theme (mode + accent), defaulting to the
// refined dark + blue accent the prototype ships with.
let (mode, accent_idx, colorblind) = cc
.storage
.map(|s| {
let mode = if s.get_string("mode").as_deref() == Some("light") {
Mode::Light
} else {
Mode::Dark
};
let accent_idx = s
.get_string("accent")
.and_then(|v| v.parse::<usize>().ok())
.unwrap_or(0)
.min(Palette::ACCENTS.len() - 1);
let colorblind = s.get_string("colorblind").as_deref() == Some("1");
(mode, accent_idx, colorblind)
})
.unwrap_or((Mode::Dark, 0, false));
Palette::new(mode, Palette::ACCENTS[accent_idx], colorblind).apply(&cc.egui_ctx, mode);
// Watch for reader hotplug so already-plugged-in or newly-inserted
// devices appear without a manual Refresh. The watcher only flags a
// shared bit and wakes the frame loop; `update()` does the rescan.
let devices_dirty = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let reader_watch = {
let dirty = devices_dirty.clone();
let egui_ctx = cc.egui_ctx.clone();
keyroost_transport::ReaderWatcher::spawn(move || {
dirty.store(true, std::sync::atomic::Ordering::Relaxed);
egui_ctx.request_repaint();
})
};
let app = App {
mode,
accent_idx,
colorblind,
worker: Some(Worker::spawn(cc.egui_ctx.clone())),
egui_ctx: Some(cc.egui_ctx.clone()),
devices_dirty,
reader_watch: Some(reader_watch),
..Default::default()
};
Ok(Box::new(app))
}),
)
}
#[derive(Default)]
struct App {
/// Active PC/SC session, if any.
session: Option<Session>,
/// Last device info read.
info: Option<DeviceInfo>,
/// Whether the session has been authenticated.
authenticated: bool,
/// Customer key text the user typed.
customer_key_input: String,
/// Treat customer_key_input as hex vs ASCII.
customer_key_hex: bool,
/// Currently selected Molto2 profile slot (0..PROFILES-1).
slot: u8,
/// Draft of the form fields for `selected` (per-slot; cleared on slot switch).
draft: Draft,
/// Rolling log of operations (newest last).
log: Vec<LogLine>,
/// otpauth:// import dialog state.
import_dialog: ImportDialog,
/// Bulk-import dialog state.
bulk_dialog: BulkDialog,
/// Unified device list (physical keys + Molto2 tokens), most recent scan.
devices: Vec<UiDevice>,
/// Selected device id — stable across refreshes so the pane doesn't jump
/// when an *unrelated* key is plugged or unplugged.
selected_device: Option<DeviceId>,
/// Which capability pane is showing for the selected key.
cap_tab: CapTab,
/// Error from the last device enumeration, surfaced in the sidebar.
devices_error: Option<String>,
/// When set: the OTP code we placed on the clipboard and the time
/// (now_secs_f64 epoch) to clear it — codes shouldn't sit in
/// clipboard-manager history forever. Cleared conditionally: only if the
/// clipboard still holds that exact code.
clipboard_clear_at: Option<(String, f64)>,
/// True once the first automatic device scan has been kicked off.
scanned: bool,
/// Sidebar filter text (filters the visible device list by vendor/model).
filter: String,
/// FIDO security-key view state (CTAP info, PIN session, errors).
security_keys: SecurityKeysState,
/// OATH (TOTP) view state.
oath: OathState,
/// OpenPGP view state.
openpgp: OpenPgpState,
/// PIV read-only status view state.
piv: PivState,
/// Dark / light theme (persisted via eframe storage).
mode: Mode,
/// Accent index into `Palette::ACCENTS` (persisted).
accent_idx: usize,
/// Colorblind-safe palette (blue/vermillion status colors) — persisted.
colorblind: bool,
/// Whether the activity-log drawer is open.
log_open: bool,
/// Open help topic id, or `None` when the popover is closed.
help_open: Option<&'static str>,
/// Anchor point (the clicked "?" button's left-bottom) for the popover.
help_anchor: egui::Pos2,
/// OATH "copied" flash: (credential name, expiry unix-secs). Cleared after.
copied: Option<(String, f64)>,
/// Whether the OATH pane has auto-attempted a read for the current selection
/// (so a hard error doesn't retry every frame). Reset on selection change.
oath_tried: bool,
/// Same guard for the PIV pane's auto-read.
piv_tried: bool,
/// True while the Molto2 factory-reset confirmation is showing.
molto_reset_confirm: bool,
/// True while the selected device's inline rename field is open.
rename_open: bool,
/// Friendly-name draft for the selected device.
rename_input: String,
/// Background worker for blocking device I/O. `None` only in tests.
worker: Option<Worker>,
/// Number of in-flight background jobs. While >0 the UI shows a spinner and
/// disables actions that would issue overlapping device I/O.
busy_jobs: u32,
/// Human-readable description of what the worker is currently doing.
busy_label: Option<String>,
/// Set by the reader watcher on a PC/SC hotplug; consumed in `update()` to
/// trigger a re-enumeration. Shared with the watcher thread.
devices_dirty: std::sync::Arc<std::sync::atomic::AtomicBool>,
/// While `Some`, a FIDO reset is armed and waiting for the key to be
/// unplugged and plugged back in (see [`ResetArm`]).
reset_arm: Option<ResetArm>,
/// Remaining scans in the current burst. A single scan races slow-to-
/// register readers (the Molto2's CCID interface appears in pcscd a beat
/// after the USB device), so startup and every hotplug schedule several
/// staggered rescans instead of one.
pending_scans: u8,
/// When the next burst scan is due.
next_scan_at: Option<std::time::Instant>,
/// Background PC/SC hotplug watcher. `None` in tests / if it can't start.
/// Held only to keep the thread alive; dropped on app exit.
#[allow(dead_code)]
reader_watch: Option<keyroost_transport::ReaderWatcher>,
/// Result channel of the in-flight bulk-import stage (QR decode, vault
/// decrypt, export parse). `Some` while one is running — these can take
/// seconds (scrypt: minutes at the caps) and run on their own thread so
/// they block neither the frame loop nor the device worker. One at a time.
import_rx: Option<std::sync::mpsc::Receiver<ApplyFn>>,
/// What the import thread is doing, for the dialog's progress row.
import_label: Option<String>,
/// Frame-loop handle for waking egui from helper threads. `None` in tests.
egui_ctx: Option<egui::Context>,
/// The `(mode, accent, colorblind)` triple whose Visuals are currently
/// applied to the egui context — re-applied on change, not per frame.
applied_theme: Option<(Mode, usize, bool)>,
}
#[derive(Default)]
struct Draft {
/// 1..=12 byte title.
title: String,
/// Base32-encoded TOTP secret.
secret_base32: String,
algorithm: AlgoChoice,
digits: DigitsChoice,
time_step: StepChoice,
display_timeout: TimeoutChoice,
}
#[derive(Default, Clone, Copy, PartialEq, Eq)]
enum AlgoChoice {
#[default]
Sha1,
Sha256,
}
impl AlgoChoice {
fn to_proto(self) -> HmacAlgo {
match self {
AlgoChoice::Sha1 => HmacAlgo::Sha1,
AlgoChoice::Sha256 => HmacAlgo::Sha256,
}
}
}
#[derive(Default, Clone, Copy, PartialEq, Eq)]
enum DigitsChoice {
Four,
#[default]
Six,
Eight,
Ten,
}
impl DigitsChoice {
fn to_proto(self) -> OtpDigits {
match self {
DigitsChoice::Four => OtpDigits::Four,
DigitsChoice::Six => OtpDigits::Six,
DigitsChoice::Eight => OtpDigits::Eight,
DigitsChoice::Ten => OtpDigits::Ten,
}
}
}
#[derive(Default, Clone, Copy, PartialEq, Eq)]
enum StepChoice {
#[default]
S30,
S60,
}
impl StepChoice {
fn to_proto(self) -> TimeStep {
match self {
StepChoice::S30 => TimeStep::Seconds30,
StepChoice::S60 => TimeStep::Seconds60,
}
}
}
#[derive(Default, Clone, Copy, PartialEq, Eq)]
enum TimeoutChoice {
S15,
#[default]
S30,
S60,
S120,
}
impl TimeoutChoice {
fn to_proto(self) -> DisplayTimeout {
match self {
TimeoutChoice::S15 => DisplayTimeout::Sec15,
TimeoutChoice::S30 => DisplayTimeout::Sec30,
TimeoutChoice::S60 => DisplayTimeout::Sec60,
TimeoutChoice::S120 => DisplayTimeout::Sec120,
}
}
}
#[derive(Default)]
struct ImportDialog {
open: bool,
uri: String,
}
#[derive(Default)]
struct BulkDialog {
open: bool,
path: String,
/// Successfully parsed entries (cleared on each load).
entries: Vec<keyroost_import::BulkEntry>,
/// Last load error message, if any.
error: Option<String>,
/// Starting slot for programming.
start: u8,
/// Display timeout applied to every entry.
display_timeout: TimeoutChoice,
/// Password for encrypted Aegis vaults (revealed when the loader detects one).
password: String,
/// True once the loader has seen an encrypted vault at the current path.
needs_password: bool,
}
struct LogLine {
severity: Severity,
text: String,
}
#[derive(Clone, Copy)]
enum Severity {
Info,
Ok,
Warn,
Err,
}
impl App {
/// Queue a blocking device job on the worker thread. `label` describes it for
/// the busy indicator; `job` runs off-thread and returns a closure applied
/// back to `self` on the UI thread. Falls back to running inline if there's
/// no worker (tests).
/// Returns `false` when the job was *not* queued (a job is already in
/// flight, or the worker died). Callers that consumed user state to build
/// the job — a typed PIN, a confirmed modal, a one-shot arm — must check
/// the return and keep (or restore) that state on `false`, otherwise a
/// click during a background read silently swallows the action.
fn spawn_job<F>(&mut self, label: impl Into<String>, job: F) -> bool
where
F: FnOnce() -> ApplyFn + Send + 'static,
{
// Serialize device access: ignore a new job while one is in flight rather
// than queueing overlapping card I/O behind a click the user can't see
// landed. (A single worker thread would serialize anyway, but this also
// stops a growing backlog of duplicate refreshes from rapid clicks.)
if self.busy() {
return false;
}
match &self.worker {
Some(worker) => {
self.busy_jobs += 1;
self.busy_label = Some(label.into());
if worker.job_tx.send(Box::new(job)).is_err() {
// Worker died; undo the bookkeeping so the UI doesn't hang.
self.busy_jobs -= 1;
self.busy_label = None;
return false;
}
true
}
None => {
let apply = job();
apply(self);
true
}
}
}
/// Apply any finished background jobs. Called once per frame from `update()`.
fn drain_worker(&mut self) {
let applies: Vec<ApplyFn> = match &self.worker {
Some(w) => w.result_rx.try_iter().collect(),
None => Vec::new(),
};
for apply in applies {
// Decrement *before* applying so an apply closure that chains another
// job (e.g. refresh-readers → probe-lock) isn't blocked by the busy
// guard in `spawn_job`.
self.busy_jobs = self.busy_jobs.saturating_sub(1);
if self.busy_jobs == 0 {
self.busy_label = None;
}
apply(self);
}
}
/// True while any background device job is in flight.
fn busy(&self) -> bool {
self.busy_jobs > 0
}
/// True while a bulk-import stage runs on its own thread.
fn import_busy(&self) -> bool {
self.import_rx.is_some()
}
/// Run a bulk-import stage (QR decode / vault decrypt / export parse) on a
/// dedicated thread. Not `spawn_job`: that queue serializes device I/O,
/// and a multi-second scrypt must not park card operations behind it.
fn run_import<F>(&mut self, label: impl Into<String>, job: F)
where
F: FnOnce() -> ApplyFn + Send + 'static,
{
if self.import_busy() {
return;
}
let Some(ctx) = self.egui_ctx.clone() else {
// Tests: no frame loop to wake; run inline.
let apply = job();
apply(self);
return;
};
let (tx, rx) = std::sync::mpsc::channel::<ApplyFn>();
let spawned = std::thread::Builder::new()
.name("keyroost-import".into())
.spawn(move || {
let apply = job();
if tx.send(apply).is_ok() {
ctx.request_repaint(); // wake the frame loop to apply it
}
});
match spawned {
Ok(_) => {
self.import_rx = Some(rx);
self.import_label = Some(label.into());
}
Err(e) => {
self.bulk_dialog.error = Some(format!("could not start import thread: {}", e));
}
}
}
/// Apply a finished bulk-import stage. Called once per frame from
/// `update()`, alongside `drain_worker`.
fn drain_import(&mut self) {
let received = match &self.import_rx {
Some(rx) => rx.try_recv(),
None => return,
};
match received {
Ok(apply) => {
self.import_rx = None;
self.import_label = None;
apply(self);
}
Err(std::sync::mpsc::TryRecvError::Empty) => {}
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
// Import thread died (panicked) without sending a result.
self.import_rx = None;
self.import_label = None;
self.bulk_dialog.error = Some("import failed unexpectedly".into());
}
}
}
fn log(&mut self, severity: Severity, text: impl Into<String>) {
self.log.push(LogLine {
severity,
text: text.into(),
});
// Keep the log bounded; oldest entries are least interesting.
if self.log.len() > 200 {
let overflow = self.log.len() - 200;
self.log.drain(0..overflow);
}
}
fn customer_key_bytes(&self) -> Result<Vec<u8>, String> {
if self.customer_key_input.is_empty() {
return Ok(DEFAULT_CUSTOMER_KEY.to_vec());
}
if self.customer_key_hex {
keyroost_proto::codec::hex_decode(&self.customer_key_input)
.map_err(|e| format!("invalid customer key hex: {}", e))
} else {
Ok(self.customer_key_input.as_bytes().to_vec())
}
}
/// Take the Molto2 session so a worker job can use it (the apply step
/// hands it back). `None` — with the reason logged — when not connected;
/// silently `None` while another job runs, so a click during a background
/// read is dropped *before* any state is consumed.
fn take_molto_session(&mut self) -> Option<Session> {
if self.busy() {
return None;
}
match self.session.take() {
Some(s) => Some(s),
None => {
self.log(Severity::Warn, "not connected");
None
}
}
}
fn authenticate(&mut self) {
let key = match self.customer_key_bytes() {
Ok(k) => zeroize::Zeroizing::new(k),
Err(e) => {
self.log(Severity::Err, e);
return;
}
};
let Some(mut s) = self.take_molto_session() else {
return;
};
self.spawn_job("Authenticating\u{2026}", move || {
let result = s.authenticate(&key);
Box::new(move |app: &mut App| {
app.session = Some(s);
match result {
Ok(()) => {
app.authenticated = true;
app.log(Severity::Ok, "authenticated");
}
Err(TransportError::AuthFailed { tries_remaining }) => {
app.log(
Severity::Err,
format!(
"authentication failed (wrong customer key); {} attempt(s) left",
tries_remaining
),
);
}
Err(e) => app.log(Severity::Err, format!("auth failed: {}", e)),
}
})
});
}
fn apply_draft(&mut self) {
if !self.ensure_auth() {
return;
}
let secret = match keyroost_proto::codec::base32_decode(&self.draft.secret_base32) {
Ok(s) if !s.is_empty() && s.len() <= 63 => zeroize::Zeroizing::new(s),
Ok(s) => {
self.log(
Severity::Err,
format!("seed must decode to 1..=63 bytes (got {})", s.len()),
);
return;
}
Err(e) => {
self.log(Severity::Err, format!("seed base32 invalid: {}", e));
return;
}
};
let title = self.draft.title.trim().to_owned();
if title.is_empty() || title.len() > 12 {
self.log(Severity::Err, "title must be 1..=12 bytes");
return;
}
let cfg = ProfileConfig {
display_timeout: self.draft.display_timeout.to_proto(),
algorithm: self.draft.algorithm.to_proto(),
digits: self.draft.digits.to_proto(),
time_step: self.draft.time_step.to_proto(),
utc_time: unix_now(),
};
let p = self.slot;
let Some(mut s) = self.take_molto_session() else {
return;
};
self.spawn_job(format!("Writing profile #{p}\u{2026}"), move || {
let result = s
.set_seed(p, &secret)
.map_err(|e| format!("set_seed #{}: {}", p, e))
.and_then(|()| {
s.set_title(p, &title)
.map_err(|e| format!("set_title #{}: {}", p, e))
})
.and_then(|()| {
s.set_config(p, &cfg)
.map_err(|e| format!("set_config #{}: {}", p, e))
});
Box::new(move |app: &mut App| {
app.session = Some(s);
match result {
Ok(()) => {
// The seed now lives on the device; keeping it in the
// (masked) field for the app's lifetime is pure
// liability. Title/config drafts stay — convenient for
// programming a run of similar slots.
wipe(&mut app.draft.secret_base32);
app.log(Severity::Ok, format!("profile #{} written", p));
}
Err(e) => app.log(Severity::Err, e),
}
})
});
}
fn sync_time_selected(&mut self) {
if !self.ensure_auth() {
return;
}
let p = self.slot;
let Some(mut s) = self.take_molto_session() else {
return;
};
self.spawn_job("Syncing time\u{2026}", move || {
let result = s.sync_time(p, unix_now());
Box::new(move |app: &mut App| {
app.session = Some(s);
match result {
Ok(()) => app.log(Severity::Ok, format!("time synced on #{}", p)),
Err(e) => app.log(Severity::Err, format!("sync_time #{}: {}", p, e)),
}
})
});
}
fn sync_time_all(&mut self) {
if !self.ensure_auth() {
return;
}
let Some(mut s) = self.take_molto_session() else {
return;
};
// 100 slots × one APDU each — emphatically not frame-loop work.
self.spawn_job("Syncing time on all profiles\u{2026}", move || {
let mut ok = 0;
let mut fail = 0;
for p in 0..PROFILES {
match s.sync_time(p, unix_now()) {
Ok(()) => ok += 1,
Err(_) => fail += 1,
}
}
Box::new(move |app: &mut App| {
app.session = Some(s);
let sev = if fail == 0 {
Severity::Ok
} else {
Severity::Warn
};
app.log(sev, format!("time-sync-all: {} ok, {} failed", ok, fail));
})
});
}
fn import_otpauth(&mut self) {
let uri = self.import_dialog.uri.trim().to_owned();
let parsed = match parse_otpauth(&uri) {
Ok(p) => p,
Err(e) => {
self.log(Severity::Err, format!("import: {}", e));
return;
}
};
// Push parsed fields into the draft so the user can review before pushing.
self.draft.title = parsed.suggested_title();
// Show the original base32 from the URI rather than re-encoding the bytes.
self.draft.secret_base32 = uri
.find("secret=")
.map(|i| {
let rest = &uri[i + 7..];
let end = rest.find('&').unwrap_or(rest.len());
rest[..end].to_owned()
})
.unwrap_or_default();
self.draft.algorithm = match parsed.algorithm {
HmacAlgo::Sha1 => AlgoChoice::Sha1,
HmacAlgo::Sha256 => AlgoChoice::Sha256,
};
self.draft.digits = match parsed.digits {
OtpDigits::Four => DigitsChoice::Four,
OtpDigits::Six => DigitsChoice::Six,
OtpDigits::Eight => DigitsChoice::Eight,
OtpDigits::Ten => DigitsChoice::Ten,
};
self.draft.time_step = match parsed.time_step {
TimeStep::Seconds30 => StepChoice::S30,
TimeStep::Seconds60 => StepChoice::S60,
};
self.log(
Severity::Info,
format!(
"imported draft for #{} from URI; review and click Write profile",
self.slot
),
);
self.import_dialog.open = false;
self.import_dialog.uri.clear();
}
fn factory_reset(&mut self) {
let Some(mut s) = self.take_molto_session() else {
return;
};
self.spawn_job("Requesting factory reset\u{2026}", move || {
let result = s.factory_reset();
Box::new(move |app: &mut App| {
app.session = Some(s);
match result {
Ok(()) => app.log(
Severity::Warn,
"factory-reset requested. Confirm with the ▲ button on the device.",
),
Err(e) => app.log(Severity::Err, format!("factory_reset: {}", e)),
}
})
});
}
fn bulk_load(&mut self) {
if self.import_busy() {
return;
}
let path = self.bulk_dialog.path.trim().to_owned();
if path.is_empty() {
self.bulk_dialog.error = Some("enter a file path first".into());
return;
}
let password = zeroize::Zeroizing::new(self.bulk_dialog.password.clone());
// Everything below — including the file read (slow media, network
// mounts) and the format branching — runs on the import thread; the
// frame loop only ever sees the finished apply step.
self.run_import("Loading import file\u{2026}", move || {
let bytes = match std::fs::read(&path) {
Ok(b) => b,
Err(e) => {
let msg = format!("read failed: {}", e);
return Box::new(move |app: &mut App| app.bulk_dialog.error = Some(msg));
}
};
// Screenshot import: a PNG/JPEG (by magic bytes) goes through QR
// decode — handles both standard otpauth:// enrollment codes and
// Google Authenticator export batches.
if keyroost_qr::looks_like_image(&bytes) {
let result = keyroost_qr::entries_from_image(&bytes);
return Box::new(move |app: &mut App| app.bulk_qr_loaded(result));
}
// Plaintext exports carry the seeds in clear — wipe-on-drop, same
// as the decrypted variant.
let text = match String::from_utf8(bytes) {
Ok(t) => zeroize::Zeroizing::new(t),
Err(_) => {
return Box::new(move |app: &mut App| {
app.bulk_dialog.error =
Some("file is neither a text export nor a PNG/JPEG image".into());
})
}
};
// An encrypted Aegis vault needs the password; the scrypt KDF is
// seconds of CPU at stock parameters — exactly why this thread
// exists.
let is_encrypted_aegis = keyroost_import::aegis::is_encrypted(&text).unwrap_or(false);
if is_encrypted_aegis {
if password.is_empty() {
return Box::new(move |app: &mut App| {
app.bulk_dialog.needs_password = true;
app.bulk_dialog.entries.clear();
app.bulk_dialog.error = Some(
"encrypted Aegis vault — enter password and click Load again".into(),
);
});
}
let result = match keyroost_import::aegis::decrypt(&text, password.as_bytes()) {
Ok(plaintext) => {
keyroost_import::parse_bulk_any(&plaintext).map_err(|e| e.to_string())
}
Err(e) => Err(format!("decrypt: {}", e)),
};
return Box::new(move |app: &mut App| {
app.bulk_dialog.needs_password = true;
app.bulk_text_loaded(result, path);
});
}
let result = keyroost_import::parse_bulk_any(&text).map_err(|e| e.to_string());
Box::new(move |app: &mut App| {
app.bulk_dialog.needs_password = false;
app.bulk_text_loaded(result, path);
})
});
}
/// Apply a finished QR-image decode to the bulk dialog.
fn bulk_qr_loaded(&mut self, result: Result<keyroost_qr::QrImport, keyroost_qr::QrError>) {
match result {
Ok(import) => {
self.bulk_dialog.error = None;
self.bulk_dialog.needs_password = false;
for s in &import.skipped {
self.log(
Severity::Err,
format!("skipped {:?}: {}", s.label, s.reason),
);
}
if let Some((i, n)) = import.batch {
self.log(
Severity::Info,
format!(
"this is QR {} of {} in the export — load the others too",
i + 1,
n
),
);
}
self.log(
Severity::Info,
format!(
"loaded {} entries from QR image — delete the screenshot after a \
successful import",
import.entries.len()
),
);
self.bulk_dialog.entries = import.entries;
}
Err(e) => {
self.bulk_dialog.entries.clear();
self.bulk_dialog.error = Some(e.to_string());
}
}
}
/// Apply a finished text-export parse (with or without vault decryption)
/// to the bulk dialog.
fn bulk_text_loaded(
&mut self,
result: Result<Vec<keyroost_import::BulkEntry>, String>,
path: String,
) {
match result {
Ok(entries) => {
self.bulk_dialog.entries = entries;
self.bulk_dialog.error = None;
wipe(&mut self.bulk_dialog.password);
self.log(
Severity::Info,
format!(
"loaded {} entries from {}",
self.bulk_dialog.entries.len(),
path
),
);
}
Err(e) => {
self.bulk_dialog.entries.clear();
self.bulk_dialog.error = Some(e);
}
}
}
fn bulk_apply(&mut self) {
if !self.ensure_auth() {
return;
}
if self.bulk_dialog.entries.is_empty() {
self.log(Severity::Warn, "no entries loaded");
return;
}
let start = self.bulk_dialog.start;
let n = self.bulk_dialog.entries.len();
let last = (start as usize).saturating_add(n);
if last > PROFILES as usize {
self.log(
Severity::Err,
format!(
"{} entries starting at #{} would overflow slot 99 (would need #{})",
n,
start,
last - 1
),
);
return;
}
let timeout = self.bulk_dialog.display_timeout.to_proto();
let entries = self.bulk_dialog.entries.clone();
let Some(mut s) = self.take_molto_session() else {
return;
};
// Up to 100 × 3 card writes — runs on the worker, log lines are
// collected and replayed in the apply step.
self.spawn_job(format!("Programming {n} profiles\u{2026}"), move || {
let mut ok = 0;
let mut fail = 0;
let mut lines: Vec<(Severity, String)> = Vec::new();
for (i, entry) in entries.into_iter().enumerate() {
let p = start + i as u8;
let title = entry.suggested_title();
if title.is_empty() {
lines.push((Severity::Warn, format!("#{}: no title; skipping", p)));
fail += 1;
continue;
}
if let Err(e) = s.set_seed(p, &entry.secret) {
lines.push((Severity::Err, format!("#{} set_seed: {}", p, e)));
fail += 1;
continue;
}
if let Err(e) = s.set_title(p, &title) {
lines.push((Severity::Err, format!("#{} set_title: {}", p, e)));
fail += 1;
continue;
}
if let Err(e) = s.set_config(p, &entry.to_profile_config(unix_now(), timeout)) {
lines.push((Severity::Err, format!("#{} set_config: {}", p, e)));
fail += 1;
continue;
}
ok += 1;
}
Box::new(move |app: &mut App| {
app.session = Some(s);
for (sev, line) in lines {
app.log(sev, line);
}
let sev = if fail == 0 {
Severity::Ok
} else {
Severity::Warn
};
app.log(sev, format!("bulk import: {} ok, {} failed", ok, fail));
if fail == 0 {
app.bulk_dialog.open = false;
}
})
});
}
fn ensure_auth(&mut self) -> bool {
if self.authenticated {
return true;
}
self.log(
Severity::Warn,
"not authenticated; click Authenticate first",
);
false
}
/// Open the currently-selected hidraw device, run CTAPHID_INIT and
/// authenticatorGetInfo, and cache the result. Blocks the UI briefly —
/// CTAP GetInfo typically completes in a few milliseconds.
fn fetch_selected_info(&mut self) {
self.security_keys.info = None;
self.security_keys.init = None;
self.security_keys.error = None;
let Some(path) = self.selected_fido_path() else {
return;
};
// Tag the job with the device it reads — if the user switches devices
// while it's in flight, the result must not be painted into the new
// device's pane (or its row in the sidebar).
let for_device = self.selected_device.clone();
self.spawn_job("Reading key info\u{2026}", move || {
// Off-thread: open the hidraw, run INIT + GetInfo.
let outcome = match CtapHidDevice::open(&path) {
Ok((mut hid, init)) => {
let info = if init.supports_cbor() {
Some(keyroost_ctap::get_info(&mut hid).map_err(|e| e.to_string()))
} else {
None
};
Ok((init, info))
}
Err(e) => Err(format!(
"could not open {}: {} (have you installed udev/70-keyroost-fido.rules?)",
path.display(),
e
)),
};
// Back on the UI thread: store the results.
Box::new(move |app: &mut App| {
if app.selected_device != for_device {
return; // selection changed mid-read; discard
}
match outcome {
Ok((init, info)) => {
// Surface the key's firmware on the hero (e.g. "fw 5.7.4").
let fw = format!(
"{}.{}.{}",
init.device_major, init.device_minor, init.device_build
);
app.security_keys.init = Some(init);
if let Some(id) = for_device.clone() {
if let Some(dev) = app.devices.iter_mut().find(|d| d.id == id) {
dev.firmware = fw;
}
}
match info {
Some(Ok(info)) => {
// Refine the model from the AAGUID (e.g. "YubiKey" ->
// "YubiKey 5 Series with NFC") on the read device.
if let Some(model) = ui::aaguid::model_for_aaguid(&info.aaguid) {
if let Some(id) = for_device {
if let Some(dev) =
app.devices.iter_mut().find(|d| d.id == id)
{
dev.model = model.to_string();
}
}
}
app.security_keys.info = Some(info);
}
Some(Err(e)) => {
app.security_keys.error = Some(format!("GetInfo failed: {}", e))
}
None => {}
}
}
Err(e) => app.security_keys.error = Some(e),
}
})
});
}
/// Open the selected hidraw, run the PIN exchange, and populate the
/// session with metadata + credential listing. Errors land in
/// `security_keys.error`.
fn try_unlock(&mut self) {
// Check busy *before* consuming the typed PIN — spawn_job would drop
// the job (and the PIN with it) if a background read is in flight.
if self.busy() {
return;
}
let Some(path) = self.selected_fido_path() else {
return;
};
let pin = std::mem::take(&mut self.security_keys.pin_input);
if pin.is_empty() {
self.security_keys.error = Some("PIN is empty".into());
return;
}
self.spawn_job("Unlocking\u{2026} (enter PIN / touch)", move || {
let result = Self::open_and_unlock(&path, &pin).map_err(|e| e.to_string());
Box::new(move |app: &mut App| match result {
Ok(sess) => {
app.security_keys.session = Some(sess);
app.security_keys.error = None;
}
Err(e) => app.security_keys.error = Some(format!("unlock failed: {}", e)),
})
});
}
fn open_and_unlock(
path: &std::path::Path,
pin: &str,
) -> Result<UnlockedSession, Box<dyn std::error::Error>> {
let (mut dev, init) = CtapHidDevice::open(path)?;
if !init.supports_cbor() {
return Err("device is U2F-only".into());
}
let info = keyroost_ctap::get_info(&mut dev)?;
let token = keyroost_ctap::client_pin::get_pin_uv_auth_token(
&mut dev,
pin,
&info,
keyroost_ctap::client_pin::permissions::CREDENTIAL_MANAGEMENT,
)?;
// Hand the manager a clone and keep `token` for the cached session; the
// token stays valid for the device session, so this avoids a redundant
// second PIN/ECDH exchange just to rebuild it.
let mut mgr =
keyroost_ctap::cred_mgmt::CredentialManager::new(&mut dev, token.clone(), &info)?;
let metadata = mgr.metadata()?;
let parties = mgr.list_relying_parties()?;
let mut rps = Vec::with_capacity(parties.len());
for rp in parties {
let creds = mgr.list_credentials(&rp.rp_id_hash).unwrap_or_default();
rps.push((rp, creds));
}
Ok(UnlockedSession {
token,
metadata,
rps: std::sync::Arc::new(rps),
})
}
fn lock_session(&mut self) {
self.security_keys.session = None;
wipe(&mut self.security_keys.pin_input);
}
fn refresh_credentials(&mut self) {
// Check busy *before* taking the session — spawn_job would silently
// drop the job, destroying the unlocked session (and its PIN token)
// and logging the user out for clicking Reload at the wrong moment.
if self.busy() {
return;
}
let Some(path) = self.selected_fido_path() else {
return;
};
let Some(session) = self.security_keys.session.take() else {
return;
};
let token = session.token;
self.spawn_job("Refreshing credentials\u{2026}", move || {
let result = Self::refresh_with_token(&path, token).map_err(|e| e.to_string());
Box::new(move |app: &mut App| match result {
Ok(fresh) => app.security_keys.session = Some(fresh),
Err(e) => app.security_keys.error = Some(format!("refresh failed: {}", e)),
})
});
}
fn refresh_with_token(
path: &std::path::Path,
token: PinUvAuthToken,
) -> Result<UnlockedSession, Box<dyn std::error::Error>> {
let (mut dev, _) = CtapHidDevice::open(path)?;
let info = keyroost_ctap::get_info(&mut dev)?;
let mut mgr =
keyroost_ctap::cred_mgmt::CredentialManager::new(&mut dev, token.clone(), &info)?;
let metadata = mgr.metadata()?;
let parties = mgr.list_relying_parties()?;
let mut rps = Vec::with_capacity(parties.len());
for rp in parties {
let creds = mgr.list_credentials(&rp.rp_id_hash).unwrap_or_default();
rps.push((rp, creds));
}
Ok(UnlockedSession {
token,
metadata,
rps: std::sync::Arc::new(rps),
})
}
fn delete_credential(&mut self, cred_id: Vec<u8>) {
let Some(path) = self.selected_fido_path() else {
return;
};
let Some(session) = self.security_keys.session.as_ref() else {
return;
};
let token = session.token.clone();
// The refresh after delete needs its own token; clone for the chained op.
let token_refresh = token.clone();
self.spawn_job("Deleting credential\u{2026}", move || {
// Delete, then re-list in the same job so the UI updates atomically.
let result = Self::try_delete(&path, token, &cred_id)
.and_then(|()| Self::refresh_with_token(&path, token_refresh))
.map_err(|e| e.to_string());
Box::new(move |app: &mut App| match result {
Ok(fresh) => {
app.security_keys.session = Some(fresh);
app.security_keys.error = None;
}
Err(e) => app.security_keys.error = Some(format!("delete failed: {}", e)),
})
});
}
fn try_delete(
path: &std::path::Path,
token: PinUvAuthToken,
cred_id: &[u8],
) -> Result<(), Box<dyn std::error::Error>> {
let (mut dev, _) = CtapHidDevice::open(path)?;
let info = keyroost_ctap::get_info(&mut dev)?;
let mut mgr = keyroost_ctap::cred_mgmt::CredentialManager::new(&mut dev, token, &info)?;
mgr.delete(cred_id)?;
Ok(())
}
fn submit_change_pin(&mut self) {
// Check busy *before* consuming the typed PINs (see try_unlock).
if self.busy() {
return;
}
let Some(path) = self.selected_fido_path() else {
return;
};
let old = std::mem::take(&mut self.security_keys.change_pin.old);
let new = std::mem::take(&mut self.security_keys.change_pin.new);
if old.is_empty() || new.is_empty() {
self.security_keys.error = Some("both PIN fields are required".into());
return;
}
self.spawn_job("Changing PIN\u{2026} (touch)", move || {
let result = Self::try_change_pin(&path, &old, &new).map_err(|e| e.to_string());
Box::new(move |app: &mut App| match result {
Ok(()) => {
app.security_keys.change_pin.open = false;
app.security_keys.error = None;
// Force re-unlock with the new PIN.
app.lock_session();
}
Err(e) => app.security_keys.error = Some(format!("change PIN failed: {}", e)),
})
});
}
fn try_change_pin(
path: &std::path::Path,
old: &str,
new: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let (mut dev, _) = CtapHidDevice::open(path)?;
keyroost_ctap::client_pin::change_pin(&mut dev, old, new)?;
Ok(())
}
/// Set a first-time PIN on a key that has none (CTAP setPIN). Validates that
/// the two entries match and meet the 4-char minimum, then re-reads info so
/// the status flips to "PIN set".
fn submit_set_pin(&mut self) {
// Check busy *before* consuming the typed PINs (see try_unlock).
if self.busy() {
return;
}
let Some(path) = self.selected_fido_path() else {
return;
};
let new = std::mem::take(&mut self.security_keys.change_pin.new);
let confirm = std::mem::take(&mut self.security_keys.change_pin.confirm);
if new.chars().count() < 4 {
self.security_keys.error = Some("PIN must be at least 4 characters".into());
return;
}
if new != confirm {
self.security_keys.error = Some("the two PINs don't match".into());
return;
}
self.spawn_job("Setting PIN\u{2026} (touch the key)", move || {
let result = (|| -> Result<(), String> {
let (mut dev, _) = CtapHidDevice::open(&path).map_err(|e| e.to_string())?;
keyroost_ctap::client_pin::set_pin(&mut dev, &new).map_err(|e| e.to_string())
})();
Box::new(move |app: &mut App| match result {
Ok(()) => {
app.security_keys.change_pin = ChangePinDialog::default();
app.security_keys.error = None;
app.fetch_selected_info();
}
Err(e) => app.security_keys.error = Some(format!("set PIN failed: {e}")),
})
});
}
fn selected_fido_path(&self) -> Option<std::path::PathBuf> {
self.selected_device().and_then(|d| d.hid_path.clone())
}
/// Wipe the FIDO key at `path` (authenticatorReset). Runs on the worker
/// thread — the card needs a touch within ~30s, which the worker keeps off
/// the UI frame. Used by the armed-reset flow, which targets the
/// just-reconnected key rather than the (now stale) selection. On success
/// the cached session and CTAP info are cleared.
fn submit_reset_path(&mut self, path: std::path::PathBuf) -> bool {
self.spawn_job("Resetting key\u{2026} (touch now)", move || {
let result = (|| -> Result<(), String> {
// A just-replugged node (especially on a fresh port) can take a
// moment to accept opens; retry briefly before giving up.
let mut dev = None;
for attempt in 0..10 {
match CtapHidDevice::open(&path) {
Ok((d, _)) => {
dev = Some(d);
break;
}
Err(e) if attempt == 9 => return Err(e.to_string()),
Err(_) => std::thread::sleep(std::time::Duration::from_millis(100)),
}
}
let mut dev = dev.ok_or_else(|| "could not open key".to_string())?;
keyroost_ctap::reset(&mut dev).map_err(|e| e.to_string())
})();
Box::new(move |app: &mut App| match result {
Ok(()) => {
app.security_keys.session = None;
app.security_keys.info = None;
app.security_keys.error = None;
// Re-read info so the PIN status reflects the wipe.
app.fetch_selected_info();
}
Err(e) => {
let msg = if e.contains("NOT_ALLOWED") || e.contains("0x30") {
"This key refused the reset. Most security keys (YubiKey \
included) only allow a FIDO reset within about 10 seconds \
of being plugged in. Unplug the key, plug it back in, then \
click \u{201C}Reset key\u{201D} again right away."
.to_string()
} else {
format!("reset failed: {e}")
};
app.security_keys.error = Some(msg);
}
})
})
}
fn selected_oath_reader(&self) -> Option<String> {
self.selected_device().and_then(|d| d.reader.clone())
}
/// Off-thread helper: open `name` and unlock with `password` if protected.
fn oath_open_unlock(
name: &str,
password: &str,
) -> Result<keyroost_transport::OathSession, TransportError> {
let mut session = keyroost_transport::OathSession::open(name)?;
if session.password_required() {
session.unlock(password)?;
}
Ok(session)
}
/// Off-thread helper: list credentials and compute each current TOTP.
fn oath_list_rows(
session: &mut keyroost_transport::OathSession,
) -> Result<Vec<OathRow>, TransportError> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let mut rows = Vec::new();
for c in session.list()? {
// A touch-required credential blocks until touched; fine off-thread.
let code = session
.calculate_totp(&c.name, now, 30)
.ok()
.map(|otp| otp.code);
rows.push(OathRow { name: c.name, code });
}
Ok(rows)
}
/// Store the outcome of an op that ends by (re)listing credentials.
fn apply_oath_rows(app: &mut App, result: Result<Vec<OathRow>, TransportError>) {
// The typed password is consumed whatever the outcome — success,
// wrong-password, or a transport error must not leave it buffered for
// an automatic retry against (potentially) a different key.
wipe(&mut app.oath.password_input);
match result {
Ok(rows) => {
app.oath.creds = rows;
app.oath.loaded = true;
app.oath.locked = false;
}
Err(TransportError::OathPasswordRejected) => {
app.oath.locked = true;
app.oath.error = Some("wrong OATH password".into());
}
Err(e) => app.oath.error = Some(e.to_string()),
}
}
/// List credentials on the selected key, computing each current TOTP. Unlocks
/// first with the entered password when the applet is protected.
fn load_oath_creds(&mut self) {
self.oath.error = None;
let Some(name) = self.selected_oath_reader() else {
self.oath.error = Some("no OATH key selected".into());
return;
};
let password = zeroize::Zeroizing::new(self.oath.password_input.clone());
let for_device = self.selected_device.clone();
self.spawn_job("Reading OATH codes\u{2026}", move || {
let result = Self::oath_open_unlock(&name, &password)
.and_then(|mut session| Self::oath_list_rows(&mut session));
Box::new(move |app: &mut App| {
// Discard if the user switched devices while the read (which
// can block on a touch) was in flight — device B's pane must
// not show device A's codes.
if app.selected_device == for_device {
Self::apply_oath_rows(app, result);
}
})
});
}
/// Provision the credential described by the add-dialog fields.
fn provision_oath(&mut self) {
self.oath.error = None;
let Some(name) = self.selected_oath_reader() else {
self.oath.error = Some("no OATH key selected".into());
return;
};
let cred_name = self.oath.add.name.trim().to_owned();
if cred_name.is_empty() {
self.oath.error = Some("credential name is required".into());
return;
}
let secret = match keyroost_proto::codec::base32_decode(self.oath.add.secret.trim()) {
Ok(s) if !s.is_empty() => zeroize::Zeroizing::new(s),
Ok(_) => {
self.oath.error = Some("secret is empty".into());
return;
}
Err(e) => {
self.oath.error = Some(format!("invalid base32 secret: {e}"));
return;
}
};
let oath_type = if self.oath.add.totp {
keyroost_oath::OathType::Totp
} else {
keyroost_oath::OathType::Hotp
};
let require_touch = self.oath.add.require_touch;
let password = zeroize::Zeroizing::new(self.oath.password_input.clone());
// Clear the form now; on error the message is surfaced separately.
self.oath.add = OathAddDialog::default();
self.spawn_job("Adding credential\u{2026}", move || {
let result = (|| -> Result<Vec<OathRow>, TransportError> {
let mut session = Self::oath_open_unlock(&name, &password)?;
session.put(&keyroost_oath::PutParams {
name: &cred_name,
secret: &secret,
oath_type,
algorithm: keyroost_oath::Algorithm::Sha1,
digits: 6,
require_touch,
imf: 0,
})?;
Self::oath_list_rows(&mut session)
})();
Box::new(move |app: &mut App| Self::apply_oath_rows(app, result))
});
}
/// Delete the named credential (already confirmed).
fn delete_oath(&mut self, name: &str) {
self.oath.error = None;
let Some(reader) = self.selected_oath_reader() else {
self.oath.error = Some("no OATH key selected".into());
return;
};
let cred_name = name.to_owned();
let password = zeroize::Zeroizing::new(self.oath.password_input.clone());
self.spawn_job("Deleting credential\u{2026}", move || {
let result = (|| -> Result<Vec<OathRow>, TransportError> {
let mut session = Self::oath_open_unlock(&reader, &password)?;
session.delete(&cred_name)?;
Self::oath_list_rows(&mut session)
})();
Box::new(move |app: &mut App| Self::apply_oath_rows(app, result))
});
}
/// The inline "Add credential" form, shown when the add dialog is open.
fn render_oath_add_form(&mut self, ui: &mut egui::Ui) {
if !self.oath.add.open {
return;
}
egui::Frame::group(ui.style()).show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(egui::RichText::new("Add credential").strong());
helper_bubble(
ui,
"The secret is sent to the key to store the credential; it is \
not written to disk by keyroost. Use the base32 secret from the \
service's enrollment (the text behind the QR code).",
);
});
ui.horizontal(|ui| {
ui.label("Name:");
ui.add(
egui::TextEdit::singleline(&mut self.oath.add.name)
.hint_text("issuer:account")
.desired_width(220.0),
);
});
ui.horizontal(|ui| {
ui.label("Secret (base32):");
ui.add(
egui::TextEdit::singleline(&mut self.oath.add.secret)
.password(true)
.desired_width(220.0),
);
});
ui.horizontal(|ui| {
ui.label("Type:");
ui.selectable_value(&mut self.oath.add.totp, true, "TOTP");
ui.selectable_value(&mut self.oath.add.totp, false, "HOTP");
ui.checkbox(&mut self.oath.add.require_touch, "Require touch");
});
ui.horizontal(|ui| {
if ui.button("Save").clicked() {
self.provision_oath();
}
if ui.button("Cancel").clicked() {
self.oath.add = OathAddDialog::default();
}
});
});
}
/// Modal confirmation before deleting a credential (irreversible).
fn render_oath_delete_confirm(&mut self, ctx: &egui::Context) {
let Some(name) = self.oath.confirm_delete.clone() else {
return;
};
let mut decision: Option<bool> = None;
egui::Window::new("Delete credential?")
.collapsible(false)
.resizable(false)
.anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
.show(ctx, |ui| {
ui.label(format!(
"Permanently delete \u{201c}{name}\u{201d} from this key? \
This cannot be undone."
));
ui.add_space(6.0);
ui.horizontal(|ui| {
if ui.button("Delete").clicked() {
decision = Some(true);
}
if ui.button("Cancel").clicked() {
decision = Some(false);
}
});
});
match decision {
Some(true) => {
self.oath.confirm_delete = None;
self.delete_oath(&name);
}
Some(false) => self.oath.confirm_delete = None,
None => {}
}
}
/// Reset confirmation: wiping a key is irreversible, so require the user to
/// type `reset` before the button activates, then a physical touch.
fn render_reset_dialog(&mut self, ctx: &egui::Context) {
if !self.security_keys.reset.open {
return;
}
let label = self
.selected_device()
.map(|d| match &d.name {
Some(name) => name.clone(),
None => format!("{} {}", d.vendor, d.model).trim().to_string(),
})
.unwrap_or_else(|| "this key".into());
let mut window_open = true;
let mut arm = false;
let mut cancel = false;
let waiting = self.reset_arm.is_some();
egui::Window::new("Reset security key?")
.collapsible(false)
.resizable(false)
.anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
.open(&mut window_open)
.show(ctx, |ui| {
ui.colored_label(
egui::Color32::from_rgb(220, 110, 110),
format!("This wipes ALL credentials and the PIN on {label}."),
);
ui.label("This cannot be undone.");
ui.add_space(6.0);
if waiting {
// Armed: the clock starts when the key is re-inserted, so we
// wait for the unplug/replug and fire the moment it returns.
ui.add_space(2.0);
ui.horizontal(|ui| {
ui.spinner();
ui.label(
egui::RichText::new("Now unplug the key, then plug it back in.")
.strong(),
);
});
ui.label("The reset is sent the instant it reconnects \u{2014} then the");
ui.label("key will blink. Touch it to finish the wipe.");
ui.add_space(6.0);
if ui.button("Cancel").clicked() {
cancel = true;
}
} else {
ui.label("A key only accepts a reset within ~10 seconds of being");
ui.label("plugged in, so after you confirm you'll re-insert it.");
ui.add_space(6.0);
ui.horizontal(|ui| {
ui.label("Type \u{201c}reset\u{201d} to confirm:");
ui.add(
egui::TextEdit::singleline(&mut self.security_keys.reset.confirm_input)
.desired_width(120.0),
);
});
ui.add_space(6.0);
let ready = self.security_keys.reset.confirm_input.trim() == "reset";
ui.horizontal(|ui| {
if ui
.add_enabled(ready, egui::Button::new("Arm reset"))
.clicked()
{
arm = true;
}
if ui.button("Cancel").clicked() {
cancel = true;
}
});
}
});
if arm {
// Snapshot the current FIDO keys so the poll can tell when ours
// leaves and a fresh one arrives. Prefer the armed key's HID serial
// (port-independent) and fall back to its path + USB ids.
let target_path = self.selected_fido_path();
let devices = Self::fido_devices();
let target = target_path
.as_ref()
.and_then(|p| devices.iter().find(|d| &d.path == p));
let target_serial = target.and_then(|d| d.serial.clone());
let target_ids = target.map(|d| d.ids);
self.reset_arm = Some(ResetArm {
target_serial,
target_path,
target_ids,
prev_paths: devices.into_iter().map(|d| d.path).collect(),
saw_removal: false,
});
} else if cancel || !window_open {
// Cancel button, or the window's [x] close.
self.security_keys.reset = ResetDialog::default();
self.reset_arm = None;
}
}
/// Current FIDO HID devices (cheap sysfs read, no PC/SC).
fn fido_devices() -> Vec<FidoHid> {
keyroost_hid::enumerate()
.unwrap_or_default()
.into_iter()
.filter(HidDevice::is_fido)
.map(|h| FidoHid {
path: h.path,
serial: h.serial_number,
ids: (h.vendor_id, h.product_id),
})
.collect()
}
/// Poll the live FIDO list while a reset is armed: once the armed key has
/// been unplugged, fire the reset on its re-insertion (which the user does),
/// inside the ~10 s window. Matches by HID serial when the key exposes one
/// (so it works across USB ports) and falls back to the HID path otherwise.
fn poll_reset_arm(&mut self) {
let current = Self::fido_devices();
let mut fire: Option<std::path::PathBuf> = None;
if let Some(arm) = self.reset_arm.as_mut() {
if let Some(target_serial) = arm.target_serial.clone() {
// Serial mode: identity is port-independent.
let here = current
.iter()
.find(|d| d.serial.as_deref() == Some(target_serial.as_str()));
match here {
None => arm.saw_removal = true, // unplugged
Some(d) if arm.saw_removal => fire = Some(d.path.clone()),
Some(_) => {}
}
} else {
// Path mode (no serial, e.g. most YubiKeys): the armed path
// leaving is the unplug; a fresh path with the armed key's
// vendor/product ids is the re-insert. (Without the id match,
// any newly plugged key would receive the reset.)
let present = |p: &std::path::PathBuf| current.iter().any(|d| &d.path == p);
match &arm.target_path {
Some(t) if !present(t) => arm.saw_removal = true,
_ => {}
}
if arm.saw_removal {
fire = current
.iter()
.filter(|d| arm.target_ids.is_none() || arm.target_ids == Some(d.ids))
.map(|d| &d.path)
.find(|p| !arm.prev_paths.contains(p))
.cloned();
}
}
}
match fire {
Some(path) => {
// The replug window is one-shot. If the worker is mid-job the
// submission is refused — keep the arm (and the stale
// prev_paths, so the fresh path still counts as new) and let
// the next poll retry instead of silently losing the reset.
if self.submit_reset_path(path) {
self.reset_arm = None;
self.security_keys.reset = ResetDialog::default();
}
}
None => {
if let Some(arm) = self.reset_arm.as_mut() {
arm.prev_paths = current.into_iter().map(|d| d.path).collect();
}
}
}
}
fn selected_openpgp_reader(&self) -> Option<String> {
self.selected_device().and_then(|d| d.reader.clone())
}
/// Read the selected card's status on the worker thread.
fn load_openpgp_status(&mut self) {
self.openpgp.error = None;
let Some(name) = self.selected_openpgp_reader() else {
self.openpgp.error = Some("no OpenPGP card selected".into());
return;
};
let for_device = self.selected_device.clone();
self.spawn_job("Reading OpenPGP status\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::OpenPgpStatus, TransportError> {
let mut session = keyroost_transport::OpenPgpSession::open(&name)?;
session.status()
})();
Box::new(move |app: &mut App| {
if app.selected_device != for_device {
return; // selection changed mid-read; discard
}
match result {
Ok(status) => {
app.openpgp.status = Some(status);
app.openpgp.loaded = true;
}
Err(e) => app.openpgp.error = Some(e.to_string()),
}
})
});
}
/// Off-thread helper: open the card and verify the admin PIN (PW3). Shared by
/// every write op so they all gate on PW3.
fn openpgp_open_admin(
name: &str,
admin_pin: &str,
) -> Result<keyroost_transport::OpenPgpSession, TransportError> {
let mut session = keyroost_transport::OpenPgpSession::open(name)?;
session.verify_admin_pin(admin_pin.as_bytes())?;
Ok(session)
}
/// Re-read status after a write and store it, surfacing `notice` on success.
fn apply_openpgp_write(
app: &mut App,
result: Result<keyroost_transport::OpenPgpStatus, TransportError>,
notice: String,
) {
match result {
Ok(status) => {
app.openpgp.status = Some(status);
app.openpgp.loaded = true;
app.openpgp.error = None;
app.openpgp.notice = Some(notice);
wipe(&mut app.openpgp.admin_pin);
}
Err(e) => {
app.openpgp.error = Some(e.to_string());
wipe(&mut app.openpgp.admin_pin);
}
}
}
/// Set the cardholder name (PW3-gated), then refresh status.
fn set_openpgp_name(&mut self) {
let Some(name) = self.selected_openpgp_reader() else {
return;
};
let pin = zeroize::Zeroizing::new(self.openpgp.admin_pin.clone());
let value = self.openpgp.name_input.clone();
self.openpgp.notice = None;
self.spawn_job("Setting cardholder name…", move || {
let result = (|| -> Result<keyroost_transport::OpenPgpStatus, TransportError> {
let mut s = Self::openpgp_open_admin(&name, &pin)?;
s.set_cardholder_name(value.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
Self::apply_openpgp_write(app, result, "Cardholder name set.".into())
})
});
}
/// Set the public-key URL (PW3-gated), then refresh status.
fn set_openpgp_url(&mut self) {
let Some(name) = self.selected_openpgp_reader() else {
return;
};
let pin = zeroize::Zeroizing::new(self.openpgp.admin_pin.clone());
let value = self.openpgp.url_input.clone();
self.openpgp.notice = None;
self.spawn_job("Setting public-key URL…", move || {
let result = (|| -> Result<keyroost_transport::OpenPgpStatus, TransportError> {
let mut s = Self::openpgp_open_admin(&name, &pin)?;
s.set_url(value.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
Self::apply_openpgp_write(app, result, "Public-key URL set.".into())
})
});
}
/// Generate + register a key in the selected slot (PW3-gated, destructive),
/// then refresh status. May require a touch on the key.
/// Returns `false` when the job couldn't be queued (worker busy) — the
/// caller's confirm modal stays open so the confirmed click isn't lost.
fn generate_openpgp_key(&mut self) -> bool {
let Some(name) = self.selected_openpgp_reader() else {
return true; // nothing to do; let the modal close
};
let pin = zeroize::Zeroizing::new(self.openpgp.admin_pin.clone());
let slot = self.openpgp.gen_slot;
let creation_time = unix_now();
self.openpgp.notice = None;
self.spawn_job("Generating key… (touch the key if it blinks)", move || {
let result = (|| -> Result<(keyroost_transport::OpenPgpStatus, [u8; 20]), TransportError> {
let mut s = Self::openpgp_open_admin(&name, &pin)?;
let _ = s.generate_key(slot.to_crt())?;
let fpr = s.register_key(slot.to_crt(), creation_time)?;
Ok((s.status()?, fpr))
})();
Box::new(move |app: &mut App| match result {
Ok((status, fpr)) => {
app.openpgp.status = Some(status);
app.openpgp.loaded = true;
app.openpgp.error = None;
app.openpgp.notice = Some(format!("Generated {} key: {}", slot.label(), hex_lower(&fpr)));
wipe(&mut app.openpgp.admin_pin);
}
Err(e) => {
app.openpgp.error = Some(e.to_string());
wipe(&mut app.openpgp.admin_pin);
}
})
})
}
/// Import a key into the selected slot (PW3-gated, destructive), then refresh
/// status. The key material comes from host keygen or a file, obtained on the
/// worker thread (keygen is slow). May require a touch on the key. Returns
/// `false` when the job couldn't be queued (worker busy).
fn import_openpgp_key(&mut self, source: ImportSource) -> bool {
let Some(name) = self.selected_openpgp_reader() else {
return true; // nothing to do; let the modal close
};
let pin = zeroize::Zeroizing::new(self.openpgp.admin_pin.clone());
let slot = self.openpgp.import_slot;
let path = self.openpgp.import_path.clone();
let creation_time = unix_now();
self.openpgp.notice = None;
let label = match source {
ImportSource::Generate => "Generating & importing RSA-2048 key… (touch if it blinks)",
ImportSource::FromFile => "Importing RSA key from file… (touch if it blinks)",
};
self.spawn_job(label, move || {
// Obtain the key parts first (keygen / file parse on this worker
// thread). Map every error to a String so success and the various
// failure kinds (key, PIN, card) flow through one result channel.
let result = (|| -> Result<(keyroost_transport::OpenPgpStatus, [u8; 20]), String> {
let k = match source {
ImportSource::Generate => keyroost_rsakey::generate_2048(),
ImportSource::FromFile => {
keyroost_rsakey::load_from_file(std::path::Path::new(&path))
}
}
.map_err(|e| e.to_string())?;
let mut s = Self::openpgp_open_admin(&name, &pin).map_err(|e| e.to_string())?;
let parts = keyroost_transport::RsaPrivateKeyParts {
e: &k.e,
p: &k.p,
q: &k.q,
u: &k.u,
dp: &k.dp,
dq: &k.dq,
n: &k.n,
};
s.import_key(slot.to_crt(), &parts)
.map_err(|e| e.to_string())?;
let fpr = s
.register_key(slot.to_crt(), creation_time)
.map_err(|e| e.to_string())?;
let status = s.status().map_err(|e| e.to_string())?;
Ok((status, fpr))
})();
Box::new(move |app: &mut App| match result {
Ok((status, fpr)) => {
app.openpgp.status = Some(status);
app.openpgp.loaded = true;
app.openpgp.error = None;
app.openpgp.notice = Some(format!(
"Imported {} key: {}",
slot.label(),
hex_lower(&fpr)
));
wipe(&mut app.openpgp.admin_pin);
app.openpgp.import_path.clear();
}
Err(e) => {
app.openpgp.error = Some(e);
wipe(&mut app.openpgp.admin_pin);
}
})
})
}
/// Factory-reset the OpenPGP applet (destructive), then refresh status. No
/// PIN needed — reset blocks the PINs itself. Returns `false` when the job
/// couldn't be queued (worker busy).
fn reset_openpgp(&mut self) -> bool {
let Some(name) = self.selected_openpgp_reader() else {
return true; // nothing to do; let the modal close
};
self.openpgp.notice = None;
self.spawn_job("Resetting OpenPGP applet…", move || {
let result = (|| -> Result<keyroost_transport::OpenPgpStatus, TransportError> {
let mut s = keyroost_transport::OpenPgpSession::open(&name)?;
s.factory_reset()?;
s.status()
})();
Box::new(move |app: &mut App| {
Self::apply_openpgp_write(
app,
result,
"OpenPGP applet reset; keys wiped, PINs back to defaults.".into(),
)
})
})
}
/// Change the user PIN (PW1) from old to new, then refresh status. No admin
/// PIN needed — CHANGE REFERENCE DATA carries the old PIN itself.
fn change_openpgp_user_pin(&mut self) {
let Some(name) = self.selected_openpgp_reader() else {
return;
};
let old = zeroize::Zeroizing::new(self.openpgp.user_pin_old.clone());
let new = zeroize::Zeroizing::new(self.openpgp.user_pin_new.clone());
self.openpgp.notice = None;
self.spawn_job("Changing user PIN\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::OpenPgpStatus, TransportError> {
let mut s = keyroost_transport::OpenPgpSession::open(&name)?;
s.change_user_pin(old.as_bytes(), new.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.openpgp.user_pin_old);
wipe(&mut app.openpgp.user_pin_new);
Self::apply_openpgp_write(app, result, "User PIN (PW1) changed.".into());
})
});
}
/// Change the admin PIN (PW3) from old to new, then refresh status.
fn change_openpgp_admin_pin(&mut self) {
let Some(name) = self.selected_openpgp_reader() else {
return;
};
let old = zeroize::Zeroizing::new(self.openpgp.admin_pin_old.clone());
let new = zeroize::Zeroizing::new(self.openpgp.admin_pin_new.clone());
self.openpgp.notice = None;
self.spawn_job("Changing admin PIN\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::OpenPgpStatus, TransportError> {
let mut s = keyroost_transport::OpenPgpSession::open(&name)?;
s.change_admin_pin(old.as_bytes(), new.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.openpgp.admin_pin_old);
wipe(&mut app.openpgp.admin_pin_new);
Self::apply_openpgp_write(app, result, "Admin PIN (PW3) changed.".into());
})
});
}
/// Unblock the user PIN (PW1): set it to a new value, authorised by the admin
/// PIN (PW3). Recovers a card whose user PIN is blocked without a reset.
fn unblock_openpgp_user_pin(&mut self) {
let Some(name) = self.selected_openpgp_reader() else {
return;
};
let admin = zeroize::Zeroizing::new(self.openpgp.admin_pin.clone());
let new = zeroize::Zeroizing::new(self.openpgp.unblock_new.clone());
self.openpgp.notice = None;
self.spawn_job("Unblocking user PIN\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::OpenPgpStatus, TransportError> {
let mut s = keyroost_transport::OpenPgpSession::open(&name)?;
s.reset_retry_counter(admin.as_bytes(), new.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.openpgp.unblock_new);
Self::apply_openpgp_write(app, result, "User PIN (PW1) unblocked.".into());
})
});
}
/// Write-operations section: cardholder name / URL, generate key, reset.
/// All write ops use the admin PIN (PW3) entered here; reset is the exception
/// (it blocks the PINs itself). Destructive ops route through confirm modals.
fn render_openpgp_manage(&mut self, ui: &mut egui::Ui, p: &Palette) {
// Intents collected inside the UI closures and applied afterwards, so a
// submit method's `&mut self` never overlaps the card's borrow.
let mut go_name = false;
let mut go_url = false;
let mut arm_generate = false;
let mut arm_import: Option<ImportSource> = None;
let mut go_change_user = false;
let mut go_change_admin = false;
let mut go_unblock = false;
let mut arm_reset = false;
let head = |ui: &mut egui::Ui, t: &str| card_head(ui, p, t);
let sub = |ui: &mut egui::Ui, t: &str| card_sub(ui, p, t);
let note = |ui: &mut egui::Ui, t: &str| card_note(ui, p, t);
// --- Admin PIN: shared by the card-detail and key writes (PW3) ---
theme::card_frame(p).show(ui, |ui| {
head(ui, "Admin PIN (PW3)");
note(
ui,
"Authorizes the card-detail and key writes below. Sent for the \
operation only; never stored.",
);
ui.add_space(6.0);
pin_field(ui, p, "Admin PIN", &mut self.openpgp.admin_pin);
});
ui.add_space(10.0);
// --- Card details: cardholder name + public-key URL ---
theme::card_frame(p).show(ui, |ui| {
head(ui, "Card details");
ui.add_space(6.0);
text_field(
ui,
p,
"Name",
&mut self.openpgp.name_input,
"Surname<<Given",
200.0,
);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Set name").clicked() {
go_name = true;
}
});
ui.add_space(6.0);
text_field(
ui,
p,
"URL",
&mut self.openpgp.url_input,
"https://\u{2026}",
240.0,
);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Set URL").clicked() {
go_url = true;
}
});
});
ui.add_space(10.0);
// --- Keys: generate on-card / import per slot (both overwrite) ---
theme::card_frame(p).show(ui, |ui| {
head(ui, "Keys");
note(
ui,
"Generating or importing OVERWRITES that slot (clearable only by a \
full reset). Needs the admin PIN; may need a touch.",
);
ui.add_space(8.0);
sub(ui, "Generate on-card");
if let Some(s) = theme::segmented(
ui,
p,
&["signature", "decryption", "authentication"],
self.openpgp.gen_slot.label(),
p.accent,
) {
self.openpgp.gen_slot = OpenPgpSlotSel::from_label(&s);
}
ui.add_space(2.0);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Generate\u{2026}").clicked() {
arm_generate = true;
}
});
ui.add_space(10.0);
sub(ui, "Import RSA-2048");
if let Some(s) = theme::segmented(
ui,
p,
&["signature", "decryption", "authentication"],
self.openpgp.import_slot.label(),
p.accent,
) {
self.openpgp.import_slot = OpenPgpSlotSel::from_label(&s);
}
text_field(
ui,
p,
"From file",
&mut self.openpgp.import_path,
"/path/to/key.pem (PKCS#1/8, PEM or DER)",
260.0,
);
let have_path = !self.openpgp.import_path.trim().is_empty();
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Generate & import\u{2026}").clicked() {
arm_import = Some(ImportSource::Generate);
}
if theme::button(ui, p, BtnKind::Default, "Import file\u{2026}").clicked()
&& have_path
{
arm_import = Some(ImportSource::FromFile);
}
});
});
ui.add_space(10.0);
// --- PINs: change user / admin, unblock user ---
theme::card_frame(p).show(ui, |ui| {
head(ui, "PINs");
ui.add_space(6.0);
sub(ui, "Change user PIN (PW1)");
pin_field(ui, p, "Current", &mut self.openpgp.user_pin_old);
pin_field(ui, p, "New", &mut self.openpgp.user_pin_new);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Change user PIN").clicked() {
go_change_user = true;
}
});
ui.add_space(10.0);
sub(ui, "Change admin PIN (PW3)");
pin_field(ui, p, "Current", &mut self.openpgp.admin_pin_old);
pin_field(ui, p, "New", &mut self.openpgp.admin_pin_new);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Change admin PIN").clicked() {
go_change_admin = true;
}
});
ui.add_space(10.0);
sub(ui, "Unblock user PIN");
note(ui, "Sets a new user PIN using the admin PIN entered above.");
pin_field(ui, p, "New user PIN", &mut self.openpgp.unblock_new);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Unblock user PIN").clicked() {
go_unblock = true;
}
});
});
ui.add_space(10.0);
// --- Danger: factory reset ---
theme::card_frame(p)
.stroke(egui::Stroke::new(1.0, theme::tint(p.err, 90)))
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("Reset applet")
.font(theme::f_sb(14.0))
.color(p.err),
);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Danger, "Reset applet\u{2026}").clicked() {
arm_reset = true;
}
});
});
ui.label(
egui::RichText::new(
"Wipes ALL OpenPGP keys and restores default PINs. Works even if the PINs are forgotten.",
)
.font(theme::f_reg(12.0))
.color(p.txt2),
);
});
// Apply collected intents now that the card borrows have ended.
if go_name {
self.set_openpgp_name();
}
if go_url {
self.set_openpgp_url();
}
if arm_generate {
self.openpgp.confirm_generate = true;
}
if let Some(src) = arm_import {
self.openpgp.confirm_import = Some(src);
}
if go_change_user {
self.change_openpgp_user_pin();
}
if go_change_admin {
self.change_openpgp_admin_pin();
}
if go_unblock {
self.unblock_openpgp_user_pin();
}
if arm_reset {
self.openpgp.confirm_reset = Some(String::new());
}
}
/// The generate-key and reset confirmation modals for the OpenPGP pane.
fn render_openpgp_confirms(&mut self, ctx: &egui::Context) {
if self.openpgp.confirm_generate {
let slot = self.openpgp.gen_slot.label();
let mut do_it = false;
let mut cancel = false;
let mut window_open = true;
egui::Window::new("Generate key?")
.collapsible(false)
.resizable(false)
.anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
.open(&mut window_open)
.show(ctx, |ui| {
ui.colored_label(
egui::Color32::from_rgb(220, 180, 80),
format!("Generate a fresh RSA key in the {slot} slot?"),
);
ui.label("This OVERWRITES any existing key in that slot (a slot");
ui.label("can only be cleared by a full applet reset). May need a touch.");
if self.openpgp.admin_pin.is_empty() {
ui.colored_label(
egui::Color32::from_rgb(220, 110, 110),
"Enter the admin PIN (PW3) above first.",
);
}
ui.add_space(6.0);
ui.horizontal(|ui| {
let armed = !self.openpgp.admin_pin.is_empty();
if ui
.add_enabled(armed, egui::Button::new("Generate"))
.clicked()
{
do_it = true;
}
if ui.button("Cancel").clicked() {
cancel = true;
}
});
});
if do_it {
// Close the modal only once the job was actually queued — a
// busy worker would otherwise silently swallow the confirmed
// click.
if self.generate_openpgp_key() {
self.openpgp.confirm_generate = false;
}
} else if cancel || !window_open {
self.openpgp.confirm_generate = false;
}
}
if let Some(source) = self.openpgp.confirm_import {
let slot = self.openpgp.import_slot.label();
let mut do_it = false;
let mut cancel = false;
let mut window_open = true;
egui::Window::new("Import key?")
.collapsible(false)
.resizable(false)
.anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
.open(&mut window_open)
.show(ctx, |ui| {
let what = match source {
ImportSource::Generate => "a fresh host-generated RSA-2048 key".to_string(),
ImportSource::FromFile => {
format!("the RSA key from {}", self.openpgp.import_path.trim())
}
};
ui.colored_label(
egui::Color32::from_rgb(220, 180, 80),
format!("Import {what} into the {slot} slot?"),
);
ui.label("This OVERWRITES any existing key in that slot (a slot");
ui.label("can only be cleared by a full applet reset). May need a touch.");
if self.openpgp.admin_pin.is_empty() {
ui.colored_label(
egui::Color32::from_rgb(220, 110, 110),
"Enter the admin PIN (PW3) above first.",
);
}
ui.add_space(6.0);
ui.horizontal(|ui| {
let armed = !self.openpgp.admin_pin.is_empty();
if ui.add_enabled(armed, egui::Button::new("Import")).clicked() {
do_it = true;
}
if ui.button("Cancel").clicked() {
cancel = true;
}
});
});
if do_it {
// Close the modal only once the job was actually queued (see
// the generate modal above).
if self.import_openpgp_key(source) {
self.openpgp.confirm_import = None;
}
} else if cancel || !window_open {
self.openpgp.confirm_import = None;
}
}
if typed_reset_modal(
ctx,
"Reset OpenPGP applet?",
"This wipes ALL OpenPGP keys and resets the PINs to defaults.",
&[],
&mut self.openpgp.confirm_reset,
) && self.reset_openpgp()
{
self.openpgp.confirm_reset = None;
}
}
/// The reset confirmation modal for the PIV pane.
fn render_piv_confirms(&mut self, ctx: &egui::Context) {
if typed_reset_modal(
ctx,
"Reset PIV applet?",
"This wipes ALL PIV keys, certificates, and PINs.",
&["Only works when the PIN and PUK are already blocked."],
&mut self.piv.confirm_reset,
) && self.piv_reset()
{
self.piv.confirm_reset = None;
}
}
}
/// A typed-"reset" confirmation modal, shared by the OpenPGP and PIV panes.
/// `confirm` is the modal state (`Some(typed text)` = open). Returns `true`
/// when the user confirmed; the *caller* fires the action and closes the
/// modal only if the action was actually queued — a busy worker must not
/// swallow a confirmed destructive click.
fn typed_reset_modal(
ctx: &egui::Context,
title: &str,
warning: &str,
extra_lines: &[&str],
confirm: &mut Option<String>,
) -> bool {
let Some(typed) = confirm.clone() else {
return false;
};
let mut do_it = false;
let mut cancel = false;
let mut window_open = true;
let mut buf = typed;
egui::Window::new(title)
.collapsible(false)
.resizable(false)
.anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
.open(&mut window_open)
.show(ctx, |ui| {
ui.colored_label(egui::Color32::from_rgb(220, 110, 110), warning);
for line in extra_lines {
ui.label(*line);
}
ui.label("This cannot be undone.");
ui.add_space(6.0);
ui.horizontal(|ui| {
ui.label("Type \u{201c}reset\u{201d} to confirm:");
ui.add(egui::TextEdit::singleline(&mut buf).desired_width(120.0));
});
ui.add_space(6.0);
ui.horizontal(|ui| {
let armed = buf.trim() == "reset";
if ui.add_enabled(armed, egui::Button::new("Reset")).clicked() {
do_it = true;
}
if ui.button("Cancel").clicked() {
cancel = true;
}
});
});
if do_it {
true
} else {
if cancel || !window_open {
*confirm = None;
} else {
*confirm = Some(buf);
}
false
}
}
/// Map an OpenPGP algorithm id (first attribute byte) to a short label.
fn algo_id_label(id: Option<u8>) -> &'static str {
match id {
Some(0x01) => "RSA",
Some(0x12) => "ECDH",
Some(0x13) => "ECDSA",
Some(0x16) => "EdDSA",
Some(_) => "other",
None => "none",
}
}
/// Render a 20-byte fingerprint, or "(no key)" when the slot is all-zero.
fn fpr_label(fpr: &[u8; 20]) -> String {
if fpr.iter().all(|&b| b == 0) {
"(no key)".to_string()
} else {
hex_lower(fpr)
}
}
/// Lowercase hex of a byte slice.
fn hex_lower(bytes: &[u8]) -> String {
keyroost_proto::codec::hex_encode(bytes)
}
/// Decode a management-key hex field into wipe-on-drop bytes.
fn piv_mgmt_key_bytes(hex: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, String> {
keyroost_proto::codec::hex_decode(hex.trim())
.map(zeroize::Zeroizing::new)
.map_err(|e| format!("management key is not valid hex: {}", e))
}
fn kv(ui: &mut egui::Ui, key: &str, value: &str) {
ui.horizontal_wrapped(|ui| {
ui.label(egui::RichText::new(format!("{key}:")).color(ui.visuals().weak_text_color()));
ui.label(value);
});
}
/// The reusable "helper-bubble": a small information glyph that reveals a
/// plain-English note on hover. Used to disclose, concisely, any choice that
/// persists data or affects security (here, that naming a key writes its serial
/// to disk) without cluttering the layout with a wall of text.
fn helper_bubble(ui: &mut egui::Ui, text: &str) {
let d = 15.0;
let (rect, resp) = ui.allocate_exact_size(egui::vec2(d, d), egui::Sense::hover());
let c = ui.visuals().weak_text_color();
ui.painter()
.circle_stroke(rect.center(), d / 2.0 - 1.0, egui::Stroke::new(1.0, c));
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
"?",
egui::FontId::proportional(10.0),
c,
);
resp.on_hover_text(text);
}
fn hex_short(bytes: &[u8]) -> String {
let mut s = String::with_capacity(bytes.len() * 2);
for b in bytes.iter().take(8) {
s.push_str(&format!("{:02x}", b));
}
if bytes.len() > 8 {
s.push('…');
}
s
}
fn unix_now() -> u32 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs() as u32)
.unwrap_or(0)
}
// --- Device-centric coordination (selection, theme, per-device loads) --------
impl App {
/// The palette for the current theme + accent.
fn palette(&self) -> Palette {
Palette::new(
self.mode,
Palette::ACCENTS[self.accent_idx],
self.colorblind,
)
}
/// The currently selected device, if the id still resolves to a present one.
fn selected_device(&self) -> Option<&UiDevice> {
let id = self.selected_device.as_ref()?;
self.devices.iter().find(|d| &d.id == id)
}
/// (Re)build the unified device list off-thread, then re-resolve selection.
/// Queue a burst of staggered rescans (see `pending_scans`). The first runs
/// as soon as the worker is free; the rest follow over the next few seconds
/// so a slow-registering reader is caught without the user touching Refresh.
fn schedule_scan_burst(&mut self) {
self.pending_scans = 4;
self.next_scan_at = None; // first scan is due immediately
}
fn refresh_devices(&mut self) {
self.scanned = true;
self.spawn_job("Scanning for devices\u{2026}", move || {
let result = device::enumerate();
Box::new(move |app: &mut App| match result {
Ok(devices) => {
app.devices_error = None;
// Keep the current selection if that device is still present;
// otherwise fall back to the first device.
let keep = app
.selected_device
.as_ref()
.filter(|id| devices.iter().any(|d| &d.id == *id))
.cloned();
let next = keep.or_else(|| devices.first().map(|d| d.id.clone()));
let changed = next != app.selected_device;
app.selected_device = next;
app.devices = devices;
if changed {
app.on_device_selected();
}
}
Err(e) => {
app.devices_error = Some(e);
app.devices.clear();
app.selected_device = None;
}
})
});
}
/// Select a device by id (no-op if already selected).
fn select_device(&mut self, id: DeviceId) {
if self.selected_device.as_deref() == Some(id.as_str()) {
return;
}
self.selected_device = Some(id);
self.on_device_selected();
}
/// Reset per-applet state for a new selection and kick off the cheap,
/// no-touch reads (FIDO GetInfo; Molto2 session open). OATH/PGP/PIV reads
/// stay deferred to their tab so selecting a key never triggers a surprise
/// touch prompt.
fn on_device_selected(&mut self) {
self.cap_tab = CapTab::Overview;
self.security_keys.info = None;
self.security_keys.init = None;
self.security_keys.session = None;
self.security_keys.error = None;
self.oath.creds.clear();
self.oath.loaded = false;
self.oath.locked = false;
self.oath.error = None;
self.openpgp.status = None;
self.openpgp.loaded = false;
self.openpgp.error = None;
self.openpgp.notice = None;
self.openpgp.name_input.clear();
self.openpgp.url_input.clear();
self.piv = PivState::default();
// Typed secrets must never survive a selection change — a PIN entered
// for one key would otherwise be sent to another (the OATH pane even
// auto-submits its password on tab open), silently burning retry
// counters on the wrong device.
wipe(&mut self.security_keys.pin_input);
wipe(&mut self.security_keys.change_pin.old);
wipe(&mut self.security_keys.change_pin.new);
wipe(&mut self.security_keys.change_pin.confirm);
self.security_keys.change_pin.open = false;
wipe(&mut self.oath.password_input);
wipe(&mut self.oath.add.secret);
self.openpgp.wipe_secrets();
self.oath_tried = false;
self.piv_tried = false;
self.molto_reset_confirm = false;
self.rename_open = false;
self.rename_input.clear();
self.authenticated = false;
self.session = None;
self.info = None;
let Some(dev) = self.selected_device().cloned() else {
return;
};
match dev.kind {
DeviceKind::Token => self.open_molto(),
DeviceKind::Key if dev.caps.has(Caps::FIDO2) => self.fetch_selected_info(),
DeviceKind::Key => {}
}
}
/// Open the selected Molto2 token's PC/SC session and read its info, so the
/// dedicated token pane has a live handle.
fn open_molto(&mut self) {
let Some(reader) = self.selected_device().and_then(|d| d.reader.clone()) else {
return;
};
self.spawn_job("Opening Molto2\u{2026}", move || {
let result = (|| -> Result<(Session, DeviceInfo), TransportError> {
let mut s = Session::open_named(&reader)?;
let info = s.read_info()?;
Ok((s, info))
})();
Box::new(move |app: &mut App| match result {
Ok((s, info)) => {
app.log(Severity::Ok, format!("opened Molto2 {}", info.serial));
// Enumeration's gentle probe usually fills these already;
// refresh them here as a fallback in case that read failed.
if let Some(id) = app.selected_device.clone() {
let named = keyroost_keyring::Keyring::load_default()
.ok()
.and_then(|k| k.name_for(Some(&info.serial)).map(str::to_owned));
if let Some(dev) = app.devices.iter_mut().find(|d| d.id == id) {
dev.serial = info.serial.clone();
if dev.name.is_none() {
dev.name = named;
}
}
}
app.session = Some(s);
app.info = Some(info);
app.authenticated = false;
}
Err(e) => app.log(Severity::Err, format!("open Molto2: {e}")),
})
});
}
/// Read the selected card's read-only PIV status snapshot.
fn load_piv_status(&mut self) {
self.piv.error = None;
let Some(reader) = self.selected_oath_reader() else {
return;
};
let for_device = self.selected_device.clone();
self.spawn_job("Reading PIV status\u{2026}", move || {
let result = keyroost_transport::PivSession::open(&reader).and_then(|mut s| s.status());
Box::new(move |app: &mut App| {
if app.selected_device != for_device {
return; // selection changed mid-read; discard
}
match result {
Ok(status) => {
app.piv.status = Some(status);
app.piv.loaded = true;
}
Err(e) => app.piv.error = Some(e.to_string()),
}
})
});
}
/// Apply a PIV write result: on success store the notice and refreshed
/// status; on error store the message. Shared by every PIV write action.
fn apply_piv_write(
app: &mut App,
result: Result<keyroost_transport::PivStatus, TransportError>,
notice: String,
) {
match result {
Ok(status) => {
app.piv.status = Some(status);
app.piv.error = None;
app.piv.notice = Some(notice);
}
Err(e) => {
app.piv.notice = None;
app.piv.error = Some(e.to_string());
}
}
}
/// Decode the management-key hex field, surfacing a parse error in the pane.
fn piv_change_pin(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let (old, new) = (self.piv.pin_old.clone(), self.piv.pin_new.clone());
self.piv.notice = None;
self.spawn_job("Changing PIV PIN\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
s.change_pin(old.as_bytes(), new.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.pin_old);
wipe(&mut app.piv.pin_new);
Self::apply_piv_write(app, result, "PIN changed.".into());
})
});
}
fn piv_change_puk(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let (old, new) = (self.piv.puk_old.clone(), self.piv.puk_new.clone());
self.piv.notice = None;
self.spawn_job("Changing PUK\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
s.change_puk(old.as_bytes(), new.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.puk_old);
wipe(&mut app.piv.puk_new);
Self::apply_piv_write(app, result, "PUK changed.".into());
})
});
}
fn piv_unblock_pin(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let (puk, new) = (
self.piv.unblock_puk.clone(),
self.piv.unblock_new_pin.clone(),
);
self.piv.notice = None;
self.spawn_job("Unblocking PIN\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
s.unblock_pin(puk.as_bytes(), new.as_bytes())?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.unblock_puk);
wipe(&mut app.piv.unblock_new_pin);
Self::apply_piv_write(app, result, "PIN unblocked and reset.".into());
})
});
}
fn piv_set_retries(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let mgmt = match piv_mgmt_key_bytes(&self.piv.mgmt_key_input) {
Ok(b) => b,
Err(e) => {
self.piv.error = Some(e);
return;
}
};
let pin = zeroize::Zeroizing::new(self.piv.retries_pin_auth.clone());
let (pin_tries, puk_tries) = (self.piv.retries_pin, self.piv.retries_puk);
self.piv.notice = None;
self.spawn_job("Setting PIV retry counts\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
let alg = s.management_key_algorithm();
s.authenticate_management(alg, &mgmt)?;
s.verify_pin(pin.as_bytes())?;
s.set_pin_retries(pin_tries, puk_tries)?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.mgmt_key_input);
wipe(&mut app.piv.retries_pin_auth);
Self::apply_piv_write(
app,
result,
"Retry counts set; PIN/PUK reset to defaults.".into(),
);
})
});
}
fn piv_generate_key(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let mgmt = match piv_mgmt_key_bytes(&self.piv.mgmt_key_input) {
Ok(b) => b,
Err(e) => {
self.piv.error = Some(e);
return;
}
};
let slot = self.piv.gen_slot.to_slot();
let alg = self.piv.gen_alg.to_alg();
self.piv.notice = None;
self.piv.gen_pubkey_pem = None;
self.spawn_job("Generating key\u{2026} (touch if it blinks)", move || {
let result = (|| -> Result<
(keyroost_piv::PublicKey, keyroost_transport::PivStatus),
TransportError,
> {
let mut s = keyroost_transport::PivSession::open(&name)?;
let mgmt_alg = s.management_key_algorithm();
s.authenticate_management(mgmt_alg, &mgmt)?;
let pubkey = s.generate_key(
slot,
alg,
keyroost_piv::PinPolicy::Default,
keyroost_piv::TouchPolicy::Default,
)?;
Ok((pubkey, s.status()?))
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.mgmt_key_input);
match result {
Ok((pubkey, status)) => {
app.piv.status = Some(status);
app.piv.error = None;
match keyroost_piv::spki::subject_public_key_info(&pubkey, alg) {
Ok(der) => {
app.piv.gen_pubkey_pem = Some(keyroost_piv::spki::to_pem(&der));
app.piv.notice = Some(format!("Generated {} key.", alg.label()));
}
Err(e) => {
app.piv.notice = Some(format!(
"Generated {} key (public-key encoding failed: {})",
alg.label(),
e
));
}
}
}
Err(e) => {
app.piv.notice = None;
app.piv.error = Some(e.to_string());
}
}
})
});
}
fn piv_import_cert(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let mgmt = match piv_mgmt_key_bytes(&self.piv.mgmt_key_input) {
Ok(b) => b,
Err(e) => {
self.piv.error = Some(e);
return;
}
};
let slot = self.piv.cert_slot.to_slot();
let path = self.piv.cert_path.trim().to_owned();
self.piv.notice = None;
self.spawn_job("Importing certificate\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let bytes = std::fs::read(&path).map_err(|_| {
TransportError::MalformedResponse("cannot read certificate file")
})?;
let der = cert_bytes_to_der(&bytes)
.ok_or(TransportError::MalformedResponse("file is not PEM or DER"))?;
let mut s = keyroost_transport::PivSession::open(&name)?;
let mgmt_alg = s.management_key_algorithm();
s.authenticate_management(mgmt_alg, &mgmt)?;
s.import_certificate(slot, &der)?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.mgmt_key_input);
Self::apply_piv_write(app, result, "Certificate imported.".into());
})
});
}
/// Normalize the certificate-subject field: a bare name becomes `CN=name`;
/// anything containing `=` is taken as a full distinguished name.
fn piv_subject(&self) -> Option<String> {
let s = self.piv.cert_subject.trim();
if s.is_empty() {
return None;
}
Some(if s.contains('=') {
s.to_owned()
} else {
format!("CN={s}")
})
}
/// Create a self-signed certificate for the selected slot's key and store
/// it in the slot (management key authorizes the import, PIN the signing).
fn piv_self_sign(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let mgmt = match piv_mgmt_key_bytes(&self.piv.mgmt_key_input) {
Ok(b) => b,
Err(e) => {
self.piv.error = Some(e);
return;
}
};
let Some(subject) = self.piv_subject() else {
self.piv.error = Some("enter a name for the certificate".into());
return;
};
let pin = zeroize::Zeroizing::new(self.piv.sign_pin.clone());
let slot = self.piv.certify_slot.to_slot();
let days = i64::from(self.piv.cert_days.max(1));
self.piv.notice = None;
self.spawn_job(
"Creating self-signed certificate\u{2026} (touch if it blinks)",
move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
let mgmt_alg = s.management_key_algorithm();
s.authenticate_management(mgmt_alg, &mgmt)?;
s.verify_pin(pin.as_bytes())?;
let now = i64::from(unix_now());
s.self_signed_certificate(slot, &subject, now, now + days * 86_400)?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.sign_pin);
wipe(&mut app.piv.mgmt_key_input);
Self::apply_piv_write(
app,
result,
format!("Self-signed certificate stored in {}.", slot.label()),
);
})
},
);
}
/// Sign a PKCS#10 certificate request on the card and save it as PEM.
fn piv_request_csr(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let Some(subject) = self.piv_subject() else {
self.piv.error = Some("enter a name for the certificate request".into());
return;
};
let path = self.piv.csr_path.trim().to_owned();
if path.is_empty() {
self.piv.error = Some("enter a destination path for the request".into());
return;
}
if std::path::Path::new(&path).exists() {
self.piv.error = Some(format!(
"{path} already exists — delete it first or choose another name"
));
return;
}
let pin = zeroize::Zeroizing::new(self.piv.sign_pin.clone());
let slot = self.piv.certify_slot.to_slot();
self.piv.notice = None;
self.spawn_job(
"Signing certificate request\u{2026} (touch if it blinks)",
move || {
let result = (|| -> Result<(), TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
s.verify_pin(pin.as_bytes())?;
let pem = s.generate_csr(slot, &subject)?;
std::fs::write(&path, pem.as_bytes()).map_err(|_| {
TransportError::MalformedResponse("cannot write destination file")
})?;
Ok(())
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.sign_pin);
match result {
Ok(()) => {
app.piv.error = None;
app.piv.notice = Some("Certificate request signed and saved.".into());
}
Err(e) => {
app.piv.notice = None;
app.piv.error = Some(e.to_string());
}
}
})
},
);
}
fn piv_export_cert(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let slot = self.piv.export_slot.to_slot();
let path = self.piv.export_path.trim().to_owned();
if path.is_empty() {
self.piv.error = Some("enter a destination path for the certificate".into());
return;
}
// Refuse to clobber an existing file — the user can delete it or pick
// another name; there is no undo for an overwritten file.
if std::path::Path::new(&path).exists() {
self.piv.error = Some(format!(
"{path} already exists — delete it first or choose another name"
));
return;
}
self.piv.notice = None;
self.spawn_job("Exporting certificate\u{2026}", move || {
let result = (|| -> Result<usize, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
let der = s
.read_certificate(slot)?
.ok_or(TransportError::MalformedResponse(
"slot holds no certificate",
))?;
std::fs::write(&path, &der).map_err(|_| {
TransportError::MalformedResponse("cannot write destination file")
})?;
Ok(der.len())
})();
Box::new(move |app: &mut App| match result {
Ok(n) => {
app.piv.error = None;
app.piv.notice = Some(format!("Exported {}-byte DER certificate.", n));
}
Err(e) => {
app.piv.notice = None;
app.piv.error = Some(e.to_string());
}
})
});
}
fn piv_change_management_key(&mut self) {
let Some(name) = self.selected_oath_reader() else {
return;
};
let old = match piv_mgmt_key_bytes(&self.piv.mgmt_key_input) {
Ok(b) => b,
Err(e) => {
self.piv.error = Some(e);
return;
}
};
let new = match piv_mgmt_key_bytes(&self.piv.new_mgmt_key_input) {
Ok(b) => b,
Err(e) => {
self.piv.error = Some(e);
return;
}
};
let new_alg = self.piv.new_mgmt_alg.to_alg();
if new.len() != new_alg.key_len() {
self.piv.error = Some(format!(
"new management key is {} bytes; {} needs {}",
new.len(),
new_alg.label(),
new_alg.key_len()
));
return;
}
self.piv.notice = None;
self.spawn_job("Changing management key\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
let cur_alg = s.management_key_algorithm();
s.authenticate_management(cur_alg, &old)?;
s.set_management_key(new_alg, &new, false)?;
s.status()
})();
Box::new(move |app: &mut App| {
wipe(&mut app.piv.mgmt_key_input);
wipe(&mut app.piv.new_mgmt_key_input);
Self::apply_piv_write(
app,
result,
format!("Management key changed to {}.", new_alg.label()),
);
})
});
}
/// Returns `false` when the job couldn't be queued (worker busy) — the
/// caller's confirm modal stays open so the confirmed click isn't lost.
fn piv_reset(&mut self) -> bool {
let Some(name) = self.selected_oath_reader() else {
return true; // nothing to do; let the modal close
};
self.piv.notice = None;
self.spawn_job("Resetting PIV applet\u{2026}", move || {
let result = (|| -> Result<keyroost_transport::PivStatus, TransportError> {
let mut s = keyroost_transport::PivSession::open(&name)?;
s.reset()?;
s.status()
})();
Box::new(move |app: &mut App| {
Self::apply_piv_write(
app,
result,
"PIV application reset to factory defaults.".into(),
);
})
})
}
/// Apply the three rename-dialog actions shared by the security-key hero and
/// the Molto2 hero: open the inline field, cancel it, or commit the name.
/// The flags are collected during the `ui` closures (where `self` is already
/// borrowed) and applied here afterwards.
fn apply_rename_actions(&mut self, dev: &UiDevice, open: bool, cancel: bool, save: bool) {
if open {
self.rename_open = true;
self.rename_input = dev.name.clone().unwrap_or_default();
}
if cancel {
self.rename_open = false;
self.rename_input.clear();
}
if save {
self.save_device_name();
}
}
/// Persist (or clear) the selected device's friendly name in `keys.json`,
/// keyed by its serial. Empty input removes the existing name.
fn save_device_name(&mut self) {
let Some(dev) = self.selected_device().cloned() else {
return;
};
if dev.serial.is_empty() {
self.log(
Severity::Warn,
"this device exposes no serial, so it can't be named yet",
);
self.rename_open = false;
return;
}
let name = self.rename_input.trim().to_owned();
if !name.is_empty() {
if let Err(e) = keyroost_keyring::validate_name(&name) {
self.log(Severity::Err, format!("invalid name: {e}"));
return;
}
}
let mut keyring = keyroost_keyring::Keyring::load_default().unwrap_or_default();
// Drop any existing name for this device first (covers rename + clear).
if let Some(current) = dev.name.clone() {
keyring.remove(¤t);
}
if !name.is_empty() {
let entry = keyroost_keyring::KeyEntry {
name,
serial: dev.serial.clone(),
source: keyroost_keyring::IdSource::default(),
vendor: Some(dev.vendor.to_ascii_lowercase()),
aaguid: None,
note: None,
};
if let Err(e) = keyring.add(entry) {
self.log(Severity::Err, format!("name: {e}"));
return;
}
}
match keyring.save_default() {
Ok(_) => self.log(Severity::Ok, "name saved"),
Err(e) => self.log(Severity::Err, format!("save names: {e}")),
}
self.rename_open = false;
self.rename_input.clear();
self.refresh_devices();
}
/// Toggle the help popover for `topic`, anchored under the clicked "?".
fn toggle_help(&mut self, topic: &'static str, anchor: egui::Pos2) {
self.help_anchor = anchor;
self.help_open = if self.help_open == Some(topic) {
None
} else {
Some(topic)
};
}
}
/// Current unix time as a float (for the OATH "copied" flash window).
fn now_secs_f64() -> f64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs_f64())
.unwrap_or(0.0)
}
/// A flat panel frame with a fill and symmetric inner padding.
fn panel_frame(fill: egui::Color32, mx: f32, my: f32) -> egui::Frame {
egui::Frame::none()
.fill(fill)
.inner_margin(egui::Margin::symmetric(mx, my))
}
/// A rounded square "glyph tile". `ch = Some(c)` paints a letter; `None` paints a
/// small clock (the Molto2 token mark).
fn glyph_tile(
ui: &mut egui::Ui,
size: f32,
fill: egui::Color32,
fg: egui::Color32,
ch: Option<char>,
) {
let (rect, _) = ui.allocate_exact_size(egui::vec2(size, size), egui::Sense::hover());
ui.painter()
.rect_filled(rect, egui::Rounding::same(size * 0.28), fill);
match ch {
Some(c) => {
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
c,
theme::f_bold(size * 0.5),
fg,
);
}
None => {
let c = rect.center();
let r = size * 0.26;
ui.painter().circle_stroke(c, r, egui::Stroke::new(1.6, fg));
ui.painter().line_segment(
[c, c + egui::vec2(0.0, -r * 0.72)],
egui::Stroke::new(1.6, fg),
);
ui.painter().line_segment(
[c, c + egui::vec2(r * 0.55, 0.0)],
egui::Stroke::new(1.6, fg),
);
}
}
}
/// Does the device match the sidebar filter text?
fn matches_filter(d: &UiDevice, q: &str) -> bool {
let q = q.trim().to_ascii_lowercase();
if q.is_empty() {
return true;
}
d.vendor.to_ascii_lowercase().contains(&q)
|| d.model.to_ascii_lowercase().contains(&q)
|| d.title().to_ascii_lowercase().contains(&q)
}
/// Short label for a capability tab.
fn cap_tab_label(t: CapTab) -> &'static str {
match t {
CapTab::Overview => "Overview",
CapTab::Fido2 => "Passkeys",
CapTab::Oath => "Authenticator",
CapTab::Pgp => "OpenPGP",
CapTab::Piv => "PIV",
}
}
/// A labelled password field row for the inline PIN form.
fn pin_field(ui: &mut egui::Ui, p: &Palette, label: &str, buf: &mut String) {
ui.horizontal(|ui| {
ui.add_sized(
[96.0, 22.0],
egui::Label::new(
egui::RichText::new(label)
.font(theme::f_reg(13.0))
.color(p.txt2),
),
);
ui.add(
egui::TextEdit::singleline(buf)
.password(true)
.desired_width(200.0),
);
});
ui.add_space(4.0);
}
/// Like [`pin_field`] but with a hint and custom width — for secrets that are
/// longer than a PIN (the PIV management key). Masked: a management key is a
/// card-write credential and shouldn't sit readable on screen.
fn secret_field(ui: &mut egui::Ui, p: &Palette, label: &str, buf: &mut String, hint: &str, w: f32) {
ui.horizontal(|ui| {
ui.add_sized(
[96.0, 22.0],
egui::Label::new(
egui::RichText::new(label)
.font(theme::f_reg(13.0))
.color(p.txt2),
),
);
ui.add(
egui::TextEdit::singleline(buf)
.password(true)
.hint_text(hint)
.desired_width(w),
);
});
ui.add_space(4.0);
}
/// Section heading inside a management card (shared by the OpenPGP and PIV
/// panes).
fn card_head(ui: &mut egui::Ui, p: &Palette, t: &str) {
ui.label(egui::RichText::new(t).font(theme::f_sb(14.0)).color(p.txt));
}
/// Sub-heading inside a management card.
fn card_sub(ui: &mut egui::Ui, p: &Palette, t: &str) {
ui.label(egui::RichText::new(t).font(theme::f_sb(12.5)).color(p.txt2));
}
/// Fine-print note inside a management card.
fn card_note(ui: &mut egui::Ui, p: &Palette, t: &str) {
ui.label(
egui::RichText::new(t)
.font(theme::f_reg(12.0))
.color(p.txt3),
);
}
/// A themed single-line text input with a fixed-width label — the non-password
/// sibling of [`pin_field`], for cardholder name / URL / file path.
fn text_field(ui: &mut egui::Ui, p: &Palette, label: &str, buf: &mut String, hint: &str, w: f32) {
ui.horizontal(|ui| {
ui.add_sized(
[96.0, 22.0],
egui::Label::new(
egui::RichText::new(label)
.font(theme::f_reg(13.0))
.color(p.txt2),
),
);
ui.add(
egui::TextEdit::singleline(buf)
.hint_text(hint)
.desired_width(w),
);
});
ui.add_space(4.0);
}
/// A PIV slot picker combo.
fn piv_slot_combo(ui: &mut egui::Ui, id: &str, sel: &mut PivSlotSel) {
egui::ComboBox::from_id_salt(id)
.selected_text(sel.label())
.show_ui(ui, |ui| {
for opt in PivSlotSel::ALL {
ui.selectable_value(sel, opt, opt.label());
}
});
}
/// A PIV key-algorithm picker combo.
fn piv_keyalg_combo(ui: &mut egui::Ui, id: &str, sel: &mut PivKeyAlgSel) {
egui::ComboBox::from_id_salt(id)
.selected_text(sel.label())
.show_ui(ui, |ui| {
for opt in PivKeyAlgSel::ALL {
ui.selectable_value(sel, opt, opt.label());
}
});
}
/// A PIV management-key-algorithm picker combo.
fn piv_mgmtalg_combo(ui: &mut egui::Ui, id: &str, sel: &mut PivMgmtAlgSel) {
egui::ComboBox::from_id_salt(id)
.selected_text(sel.label())
.show_ui(ui, |ui| {
for opt in PivMgmtAlgSel::ALL {
ui.selectable_value(sel, opt, opt.label());
}
});
}
/// Accept a certificate file's bytes as PEM or DER, returning DER. `None` when
/// the bytes are neither.
fn cert_bytes_to_der(bytes: &[u8]) -> Option<Vec<u8>> {
if let Ok(text) = std::str::from_utf8(bytes) {
if let Some(start) = text.find("-----BEGIN CERTIFICATE-----") {
let after = &text[start + "-----BEGIN CERTIFICATE-----".len()..];
let end = after.find("-----END CERTIFICATE-----")?;
let b64: String = after[..end].split_whitespace().collect();
return keyroost_proto::codec::base64_decode(&b64).ok();
}
}
// Not PEM — accept DER (must begin with a SEQUENCE tag).
if bytes.first() == Some(&0x30) {
Some(bytes.to_vec())
} else {
None
}
}
/// Paint a rounded glyph tile at `rect` with the painter (no widget allocation,
/// so it never steals clicks from a surrounding row). `ch = None` draws a clock.
fn paint_glyph_tile(
ui: &egui::Ui,
rect: egui::Rect,
fill: egui::Color32,
fg: egui::Color32,
ch: Option<char>,
) {
ui.painter()
.rect_filled(rect, egui::Rounding::same(rect.width() * 0.28), fill);
match ch {
Some(c) => {
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
c,
theme::f_bold(rect.width() * 0.5),
fg,
);
}
None => {
let c = rect.center();
let r = rect.width() * 0.26;
ui.painter().circle_stroke(c, r, egui::Stroke::new(1.6, fg));
ui.painter().line_segment(
[c, c + egui::vec2(0.0, -r * 0.72)],
egui::Stroke::new(1.6, fg),
);
ui.painter().line_segment(
[c, c + egui::vec2(r * 0.55, 0.0)],
egui::Stroke::new(1.6, fg),
);
}
}
}
/// Paint a small rounded pill at `left_top` with the painter; returns its width
/// so the caller can advance a cursor.
fn paint_pill(
ui: &egui::Ui,
left_top: egui::Pos2,
text: &str,
fg: egui::Color32,
bg: egui::Color32,
) -> f32 {
let galley = ui.fonts(|f| f.layout_no_wrap(text.to_string(), theme::f_sb(11.0), fg));
let pad_x = 8.0;
let h = 18.0;
let w = galley.size().x + pad_x * 2.0;
let rect = egui::Rect::from_min_size(left_top, egui::vec2(w, h));
ui.painter()
.rect_filled(rect, egui::Rounding::same(999.0), bg);
let pos = egui::pos2(left_top.x + pad_x, rect.center().y - galley.size().y / 2.0);
ui.painter().galley(pos, galley, fg);
w
}
// ---- small vector icons, painted so they render regardless of font coverage
// (IBM Plex Sans lacks ⓘ / ◐ / ↻ / ⧉ / ✓ / ✕, which otherwise show as tofu) ----
/// Half-filled circle — the light/dark theme toggle.
fn paint_theme_icon(ui: &egui::Ui, center: egui::Pos2, r: f32, color: egui::Color32) {
use std::f32::consts::{FRAC_PI_2, PI};
ui.painter()
.circle_stroke(center, r, egui::Stroke::new(1.3, color));
let n = 20;
let pts: Vec<egui::Pos2> = (0..=n)
.map(|i| {
let a = FRAC_PI_2 + PI * (i as f32 / n as f32);
center + r * egui::vec2(a.cos(), a.sin())
})
.collect();
ui.painter()
.add(egui::Shape::convex_polygon(pts, color, egui::Stroke::NONE));
}
/// An eye (two lids + a pupil) — the colorblind-mode toggle.
fn paint_eye_icon(ui: &egui::Ui, center: egui::Pos2, color: egui::Color32) {
let stroke = egui::Stroke::new(1.3, color);
let (w, h) = (8.0_f32, 4.6_f32);
let n = 14;
let lid = |sign: f32| -> Vec<egui::Pos2> {
(0..=n)
.map(|i| {
let t = -1.0 + 2.0 * (i as f32 / n as f32);
egui::pos2(center.x + t * w, center.y + sign * h * (1.0 - t * t))
})
.collect()
};
ui.painter().add(egui::Shape::line(lid(-1.0), stroke));
ui.painter().add(egui::Shape::line(lid(1.0), stroke));
ui.painter().circle_filled(center, 2.2, color);
}
/// Circular arrow — refresh / rescan. Clockwise, with a filled arrowhead at the
/// leading end (egui's y-down space makes increasing angle clockwise).
fn paint_refresh_icon(ui: &egui::Ui, center: egui::Pos2, r: f32, color: egui::Color32) {
let stroke = egui::Stroke::new(1.4, color);
let (a0, a1) = (0.5_f32, 5.6_f32);
let n = 24;
let pts: Vec<egui::Pos2> = (0..=n)
.map(|i| {
let a = a0 + (a1 - a0) * (i as f32 / n as f32);
center + r * egui::vec2(a.cos(), a.sin())
})
.collect();
ui.painter().add(egui::Shape::line(pts, stroke));
// Filled arrowhead at the leading (clockwise) end, pointing along the motion.
let end = center + r * egui::vec2(a1.cos(), a1.sin());
let tangent = egui::vec2(-a1.sin(), a1.cos());
let radial = egui::vec2(a1.cos(), a1.sin());
let tip = end + tangent * 3.5;
let b1 = end + radial * 2.6 - tangent * 0.8;
let b2 = end - radial * 2.6 - tangent * 0.8;
ui.painter().add(egui::Shape::convex_polygon(
vec![tip, b1, b2],
color,
egui::Stroke::NONE,
));
}
/// Two stacked sheets — copy.
fn paint_copy_icon(ui: &egui::Ui, center: egui::Pos2, color: egui::Color32) {
let s = egui::Stroke::new(1.3, color);
let back = egui::Rect::from_min_size(center + egui::vec2(-1.0, -5.0), egui::vec2(7.0, 8.0));
let front = egui::Rect::from_min_size(center + egui::vec2(-5.0, -1.0), egui::vec2(7.0, 8.0));
ui.painter().rect_stroke(back, egui::Rounding::same(1.5), s);
ui.painter()
.rect_stroke(front, egui::Rounding::same(1.5), s);
}
/// Checkmark — copied / confirmed.
fn paint_check_icon(ui: &egui::Ui, center: egui::Pos2, color: egui::Color32) {
let s = egui::Stroke::new(1.7, color);
ui.painter().line_segment(
[
center + egui::vec2(-4.0, 0.0),
center + egui::vec2(-1.0, 3.0),
],
s,
);
ui.painter().line_segment(
[
center + egui::vec2(-1.0, 3.0),
center + egui::vec2(4.0, -3.5),
],
s,
);
}
/// Cross — delete / dismiss.
fn paint_x_icon(ui: &egui::Ui, center: egui::Pos2, color: egui::Color32) {
let s = egui::Stroke::new(1.4, color);
ui.painter().line_segment(
[
center + egui::vec2(-3.5, -3.5),
center + egui::vec2(3.5, 3.5),
],
s,
);
ui.painter().line_segment(
[
center + egui::vec2(-3.5, 3.5),
center + egui::vec2(3.5, -3.5),
],
s,
);
}
/// Paint one selectable device row. The whole row is a single painter-drawn
/// click target (no nested widgets), so clicking anywhere in it selects the
/// device — fixing the "only the gaps are clickable" inconsistency.
fn device_row(ui: &mut egui::Ui, p: &Palette, dev: &UiDevice, selected: bool) -> bool {
let w = ui.available_width();
let h = 68.0;
let (rect, resp) = ui.allocate_exact_size(egui::vec2(w, h), egui::Sense::click());
let bg = if selected {
p.raised
} else if resp.hovered() {
p.line_soft
} else {
egui::Color32::TRANSPARENT
};
ui.painter().rect(
rect,
egui::Rounding::same(11.0),
bg,
egui::Stroke::new(
1.0,
if selected {
p.line
} else {
egui::Color32::TRANSPARENT
},
),
);
if selected {
ui.painter().rect_filled(
egui::Rect::from_min_size(
rect.left_top() + egui::vec2(0.0, 13.0),
egui::vec2(3.0, h - 26.0),
),
egui::Rounding::same(3.0),
p.accent,
);
}
let token = dev.kind == DeviceKind::Token;
let tile = egui::Rect::from_min_size(
rect.left_top() + egui::vec2(14.0, (h - 38.0) / 2.0),
egui::vec2(38.0, 38.0),
);
if token {
paint_glyph_tile(ui, tile, p.brand_soft(), p.brand, None);
} else {
paint_glyph_tile(
ui,
tile,
p.raised2,
p.txt2,
Some(
dev.vendor
.chars()
.next()
.unwrap_or('?')
.to_ascii_uppercase(),
),
);
}
let tx = tile.right() + 11.0;
let right_pad = 16.0;
// status dot, top-right
ui.painter().circle_filled(
egui::pos2(rect.right() - right_pad, rect.top() + 18.0),
3.5,
p.ok,
);
// vendor eyebrow
ui.painter().text(
egui::pos2(tx, rect.top() + 13.0),
egui::Align2::LEFT_TOP,
&dev.vendor,
theme::f_sb(11.0),
p.txt3,
);
// model, truncated to the available width
let avail = (rect.right() - right_pad - 8.0) - tx;
let galley = ui.fonts(|f| {
let mut job = egui::text::LayoutJob::single_section(
dev.title().to_string(),
egui::TextFormat {
font_id: theme::f_sb(13.5),
color: p.txt,
..Default::default()
},
);
job.wrap = egui::text::TextWrapping {
max_width: avail.max(0.0),
max_rows: 1,
break_anywhere: true,
overflow_character: Some('\u{2026}'),
};
f.layout_job(job)
});
ui.painter()
.galley(egui::pos2(tx, rect.top() + 26.0), galley, p.txt);
// capability pills
let py = rect.top() + 46.0;
if token {
paint_pill(
ui,
egui::pos2(tx, py),
"TOTP token",
p.brand,
p.brand_soft(),
);
} else {
let mut px = tx;
for (cap, label) in [
(Caps::FIDO2, "FIDO2"),
(Caps::OATH, "OATH"),
(Caps::PGP, "PGP"),
(Caps::PIV, "PIV"),
] {
if dev.caps.has(cap) {
px += paint_pill(ui, egui::pos2(px, py), label, p.txt2, p.raised2) + 5.0;
}
}
}
if resp.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
}
resp.clicked()
}
impl eframe::App for App {
/// Persist the theme so it survives a restart.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
storage.set_string(
"mode",
match self.mode {
Mode::Dark => "dark",
Mode::Light => "light",
}
.to_string(),
);
storage.set_string("accent", self.accent_idx.to_string());
storage.set_string(
"colorblind",
if self.colorblind { "1" } else { "0" }.to_string(),
);
}
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
// Clipboard hygiene: ~45s after an OTP code was copied, clear the
// clipboard so the code doesn't live on in clipboard-manager history —
// but only if the clipboard still holds that exact code. Content the
// user copied elsewhere in the meantime is never clobbered. arboard is
// already in the tree via eframe's clipboard integration; if the
// clipboard can't be read (e.g. some Wayland setups), fail open and
// clear nothing rather than risk destroying foreign content.
if let Some((ref code, t)) = self.clipboard_clear_at {
let now = now_secs_f64();
if now >= t {
if let Ok(mut cb) = arboard::Clipboard::new() {
if cb.get_text().is_ok_and(|current| current == *code) {
let _ = cb.clear();
}
}
self.clipboard_clear_at = None;
} else {
ctx.request_repaint_after(std::time::Duration::from_secs_f64((t - now).max(0.1)));
}
}
// Dropping a file on the window routes it to the bulk-import dialog —
// the natural gesture for "import this screenshot/export".
let dropped = ctx.input(|i| {
i.raw
.dropped_files
.first()
.and_then(|f| f.path.as_ref())
.map(|p| p.display().to_string())
});
if let Some(path) = dropped {
self.bulk_dialog.open = true;
self.bulk_dialog.path = path;
self.bulk_load();
}
// Apply any results from background device jobs and the import
// thread before drawing.
self.drain_worker();
self.drain_import();
let p = self.palette();
// Rebuilding + re-applying egui Visuals every frame is wasted work;
// only do it when a theme knob actually changed.
let theme_key = (self.mode, self.accent_idx, self.colorblind);
if self.applied_theme != Some(theme_key) {
p.apply(ctx, self.mode);
self.applied_theme = Some(theme_key);
}
// First frame: scan for devices automatically so the user isn't staring
// at an empty pane wondering whether the app is broken.
if !self.scanned {
self.scanned = true;
self.schedule_scan_burst();
}
// Armed FIDO reset: poll the live key list so we can fire the reset the
// instant the user re-inserts the key. Runs before the hotplug refresh
// and holds the worker slot, so a replug-triggered rescan can't beat the
// reset to it.
if self.reset_arm.is_some() {
self.poll_reset_arm();
ctx.request_repaint_after(std::time::Duration::from_millis(200));
}
// Reader hotplug: the watcher set this flag (and woke us). Start a
// rescan burst (suppressed while a reset is armed, so its job can't
// steal the worker slot).
if self.reset_arm.is_none()
&& self
.devices_dirty
.swap(false, std::sync::atomic::Ordering::Relaxed)
{
self.schedule_scan_burst();
}
// Drive the rescan burst: run a scan when one is due and the worker is
// free, then schedule the next. Spacing gives a slow reader (Molto2)
// time to register with pcscd between attempts.
if self.reset_arm.is_none() && self.pending_scans > 0 {
let now = std::time::Instant::now();
let due = self.next_scan_at.map_or(true, |t| now >= t);
if due && !self.busy() {
self.refresh_devices();
self.pending_scans -= 1;
self.next_scan_at = Some(now + std::time::Duration::from_millis(1500));
}
if self.pending_scans > 0 {
ctx.request_repaint_after(std::time::Duration::from_millis(500));
}
}
self.top_bar(ctx, &p);
self.device_sidebar(ctx, &p);
if self.log_open {
self.activity_log(ctx, &p);
}
self.central(ctx, &p);
// Modal dialogs (reused from the per-applet logic) + Molto2 import dialogs.
self.render_reset_dialog(ctx);
self.render_oath_delete_confirm(ctx);
self.render_openpgp_confirms(ctx);
self.render_piv_confirms(ctx);
self.molto_dialogs(ctx, &p);
// Help popover, painted last so it sits above everything.
if let Some(topic) = self.help_open {
if ui::help_popover(ctx, &p, topic, self.help_anchor) {
self.help_open = None;
}
}
// Keep OATH rings / Molto2 time ticking while a device is selected.
// Only keep ticking when something actually animates (OATH countdown
// rings, the Molto2 view, or the "copied" flash) — not on every static
// pane, which would burn frames and feel sluggish.
let animating = self.copied.is_some()
|| matches!(self.cap_tab, CapTab::Oath)
|| (matches!(self.cap_tab, CapTab::Overview)
&& self.oath.loaded
&& !self.oath.creds.is_empty())
|| self
.selected_device()
.is_some_and(|d| d.kind == DeviceKind::Token);
if animating {
ctx.request_repaint_after(std::time::Duration::from_millis(500));
}
}
}
// --- Device-centric rendering ------------------------------------------------
impl App {
/// Toggle the help popover for `topic`, anchored under the clicked "?" button.
fn help_dot(&mut self, ui: &mut egui::Ui, p: &Palette, topic: &'static str) {
let r = ui::help_button(ui, p, self.help_open == Some(topic));
if r.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
}
if r.clicked() {
self.toggle_help(topic, r.rect.left_bottom());
}
}
/// Top bar: brand · "keyroost" · connected count | accents · theme · Learn ·
/// Activity log · Refresh.
fn top_bar(&mut self, ctx: &egui::Context, p: &Palette) {
egui::TopBottomPanel::top("bar")
.exact_height(52.0)
.frame(panel_frame(p.bar, 16.0, 0.0))
.show(ctx, |ui| {
ui.horizontal_centered(|ui| {
glyph_tile(ui, 26.0, p.brand, p.accent_ink, Some('k'));
ui.add_space(8.0);
ui.label(
egui::RichText::new("keyroost")
.font(theme::f_bold(14.0))
.color(p.txt),
);
ui.add_space(12.0);
theme::status_dot(ui, p.ok, 7.0);
ui.add_space(5.0);
ui.label(
egui::RichText::new(format!("{} connected", self.devices.len()))
.font(theme::f_reg(12.0))
.color(p.txt2),
);
if self.busy() {
ui.add_space(12.0);
ui.spinner();
if let Some(label) = &self.busy_label {
ui.label(
egui::RichText::new(label.as_str())
.font(theme::f_reg(12.0))
.color(p.txt3),
);
}
}
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Ghost, "Refresh").clicked() {
self.schedule_scan_burst();
}
ui.add_space(4.0);
let log_color = if self.log_open { p.accent } else { p.txt2 };
if ui
.add(
egui::Label::new(
egui::RichText::new("Activity log")
.font(theme::f_sb(12.5))
.color(log_color),
)
.sense(egui::Sense::click()),
)
.on_hover_cursor(egui::CursorIcon::PointingHand)
.clicked()
{
self.log_open = !self.log_open;
}
ui.add_space(10.0);
ui.hyperlink_to(
egui::RichText::new("Learn \u{2197}")
.font(theme::f_sb(12.5))
.color(p.txt2),
ui::help::LEARN_BASE,
);
ui.add_space(10.0);
let (trect, tresp) =
ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::click());
paint_theme_icon(ui, trect.center(), 7.0, p.txt2);
if tresp
.on_hover_text("Toggle light / dark")
.on_hover_cursor(egui::CursorIcon::PointingHand)
.clicked()
{
self.mode = match self.mode {
Mode::Dark => Mode::Light,
Mode::Light => Mode::Dark,
};
}
ui.add_space(10.0);
let (erect, eresp) =
ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::click());
paint_eye_icon(
ui,
erect.center(),
if self.colorblind { p.accent } else { p.txt2 },
);
if eresp
.on_hover_text("Colorblind-safe colors")
.on_hover_cursor(egui::CursorIcon::PointingHand)
.clicked()
{
self.colorblind = !self.colorblind;
}
ui.add_space(10.0);
for (i, c) in Palette::ACCENTS.iter().enumerate().rev() {
let (rect, resp) = ui
.allocate_exact_size(egui::vec2(16.0, 16.0), egui::Sense::click());
let on = i == self.accent_idx;
ui.painter().circle_filled(
rect.center(),
if on { 6.0 } else { 5.0 },
*c,
);
if on {
ui.painter().circle_stroke(
rect.center(),
7.5,
egui::Stroke::new(1.5, p.txt),
);
}
if resp.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
}
if resp.clicked() {
self.accent_idx = i;
}
}
});
});
});
}
/// Left device bar: header · filter · rows · footer tip.
fn device_sidebar(&mut self, ctx: &egui::Context, p: &Palette) {
egui::SidePanel::left("devices")
.exact_width(286.0)
.resizable(false)
.frame(panel_frame(p.side, 14.0, 12.0))
.show(ctx, |ui| {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("DEVICES")
.font(theme::f_bold(11.0))
.color(p.txt3),
);
ui.add_space(6.0);
theme::pill(ui, &self.devices.len().to_string(), p.txt2, p.raised2);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let (rrect, rresp) =
ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::click());
paint_refresh_icon(ui, rrect.center(), 6.5, p.txt2);
if rresp
.on_hover_text("Rescan")
.on_hover_cursor(egui::CursorIcon::PointingHand)
.clicked()
{
self.schedule_scan_burst();
}
});
});
ui.add_space(8.0);
ui.add(
egui::TextEdit::singleline(&mut self.filter)
.hint_text("Filter keys\u{2026}")
.desired_width(f32::INFINITY),
);
ui.add_space(8.0);
if let Some(err) = &self.devices_error {
ui.colored_label(p.err, err);
ui.add_space(6.0);
}
let mut clicked: Option<DeviceId> = None;
egui::ScrollArea::vertical()
.auto_shrink([false, false])
.show(ui, |ui| {
if self.devices.is_empty() {
ui.add_space(12.0);
ui.vertical_centered(|ui| {
ui.label(
egui::RichText::new("No keys detected yet.")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
});
}
for dev in self
.devices
.iter()
.filter(|d| matches_filter(d, &self.filter))
{
let selected = self.selected_device.as_deref() == Some(dev.id.as_str());
if device_row(ui, p, dev, selected) {
clicked = Some(dev.id.clone());
}
ui.add_space(2.0);
}
});
if let Some(id) = clicked {
self.select_device(id);
}
// Footer tip (only worth showing once a key is present).
if !self.devices.is_empty() {
ui.with_layout(egui::Layout::bottom_up(egui::Align::Min), |ui| {
ui.add_space(4.0);
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 4.0;
ui.label(
egui::RichText::new("Several keys plugged in?")
.font(theme::f_reg(12.0))
.color(p.txt3),
);
ui.hyperlink_to(
egui::RichText::new("Give them names")
.font(theme::f_sb(12.0))
.color(p.accent),
ui::help::learn_url("/naming"),
);
});
});
}
});
}
/// Global activity-log drawer (bottom), replacing the Molto2-only log.
fn activity_log(&mut self, ctx: &egui::Context, p: &Palette) {
egui::TopBottomPanel::bottom("log")
.exact_height(180.0)
.frame(panel_frame(p.bar, 16.0, 10.0))
.show(ctx, |ui| {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("ACTIVITY LOG")
.font(theme::f_bold(11.0))
.color(p.txt3),
);
ui.add_space(6.0);
theme::pill(ui, "live", p.ok, p.ok_soft());
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Ghost, "Collapse").clicked() {
self.log_open = false;
}
ui.add_space(4.0);
if theme::button(ui, p, BtnKind::Ghost, "Copy").clicked() {
let all = self
.log
.iter()
.map(|l| l.text.clone())
.collect::<Vec<_>>()
.join("\n");
ui.output_mut(|o| o.copied_text = all);
// The user copied non-secret log text over the
// code; a pending auto-clear would clobber it.
self.clipboard_clear_at = None;
}
});
});
ui.add_space(6.0);
egui::ScrollArea::vertical()
.stick_to_bottom(true)
.auto_shrink([false, false])
.show(ui, |ui| {
for line in &self.log {
let color = match line.severity {
Severity::Ok => p.ok,
Severity::Warn => p.warn,
Severity::Err => p.err,
Severity::Info => p.txt2,
};
ui.label(
egui::RichText::new(&line.text)
.font(theme::f_mono(12.0))
.color(color),
);
}
});
});
}
/// The central pane: empty state, the Molto2 token view, or the selected
/// key's hero + capability tabs + active capability panel.
fn central(&mut self, ctx: &egui::Context, p: &Palette) {
egui::CentralPanel::default()
.frame(panel_frame(p.surface, 0.0, 0.0))
.show(ctx, |ui| match self.selected_device().cloned() {
None => self.empty_state(ui, p),
Some(dev) if dev.kind == DeviceKind::Token => self.molto_view(ui, p, &dev),
Some(dev) => {
egui::Frame::none()
.inner_margin(egui::Margin::symmetric(26.0, 16.0))
.show(ui, |ui| {
self.device_hero(ui, p, &dev);
self.cap_tabs(ui, p, &dev);
ui.add_space(16.0);
// Hero + tabs stay pinned; the active pane scrolls.
// This is the one place every capability pane gets
// its overflow handling — a card-heavy OpenPGP pane
// or a key holding dozens of passkeys/TOTP codes
// scrolls instead of clipping at the window edge.
// Salted per tab so each pane keeps its own
// scroll position.
//
// Solid bar style: reserve a real gutter for the
// scrollbar instead of floating it over the cards'
// right edge (the floating bar sat on top of card
// borders and the panes' top-right action buttons).
ui.spacing_mut().scroll = egui::style::ScrollStyle::solid();
egui::ScrollArea::vertical()
.id_salt(("cap-pane", self.cap_tab as u8))
.auto_shrink([false, false])
.show(ui, |ui| {
match self.cap_tab {
CapTab::Overview => self.overview(ui, p, &dev),
CapTab::Fido2 => self.cap_fido2(ui, p),
CapTab::Oath => self.cap_oath(ui, p),
CapTab::Pgp => self.cap_pgp(ui, p),
CapTab::Piv => self.cap_piv(ui, p),
}
// Breathing room below the last card.
ui.add_space(18.0);
});
});
}
});
}
/// Welcoming first-run state shown when nothing is plugged in.
fn empty_state(&mut self, ui: &mut egui::Ui, p: &Palette) {
// Manually center a fixed-width column. `vertical_centered` doesn't
// reliably center nested rows (a `horizontal` fills full width and
// left-aligns), which is what jammed the buttons against the divider.
let col_w = 440.0_f32;
let pad = ((ui.available_width() - col_w) * 0.5).max(12.0);
ui.add_space(90.0);
ui.horizontal(|ui| {
ui.add_space(pad);
ui.allocate_ui_with_layout(
egui::vec2(col_w, ui.available_height()),
egui::Layout::top_down(egui::Align::Center),
|ui| {
let (rect, _) = ui.allocate_exact_size(egui::vec2(64.0, 64.0), egui::Sense::hover());
ui.painter()
.rect_stroke(rect, egui::Rounding::same(16.0), egui::Stroke::new(1.5, p.line));
ui.painter().text(
rect.center(),
egui::Align2::CENTER_CENTER,
"\u{1F511}",
theme::f_reg(26.0),
p.txt3,
);
ui.add_space(18.0);
ui.label(egui::RichText::new("Plug in a security key to begin").font(theme::f_bold(19.0)).color(p.txt));
ui.add_space(8.0);
ui.label(
egui::RichText::new(
"keyroost manages YubiKeys, Nitrokeys, SoloKeys and Token2 tokens. Connect one over USB and it shows up in the list on the left.",
)
.font(theme::f_reg(13.0))
.color(p.txt2),
);
ui.add_space(22.0);
// Numbered steps: left-aligned within the centered column.
ui.allocate_ui_with_layout(egui::vec2(360.0, 0.0), egui::Layout::top_down(egui::Align::Min), |ui| {
for (n, step) in [
"Insert your key into a USB port",
"It appears in the Devices list automatically",
"Select it to view and manage everything it can do",
]
.iter()
.enumerate()
{
ui.horizontal(|ui| {
let (badge, _) = ui.allocate_exact_size(egui::vec2(22.0, 22.0), egui::Sense::hover());
ui.painter().circle_filled(badge.center(), 11.0, p.accent_soft());
ui.painter().text(
badge.center(),
egui::Align2::CENTER_CENTER,
format!("{}", n + 1),
theme::f_sb(12.0),
p.accent,
);
ui.add_space(10.0);
ui.label(egui::RichText::new(*step).font(theme::f_reg(13.0)).color(p.txt));
});
ui.add_space(10.0);
}
});
ui.add_space(14.0);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Primary, "Scan for devices").clicked() {
self.schedule_scan_burst();
}
ui.add_space(8.0);
ui.hyperlink_to(
egui::RichText::new("Supported devices \u{2197}").font(theme::f_sb(12.5)).color(p.accent),
ui::help::learn_url("/devices"),
);
});
// Be honest on platforms without a HID backend yet: a plugged-in
// FIDO key simply won't appear, so say why rather than letting
// the user think it's broken.
if !keyroost_hid::hid_supported() {
ui.add_space(16.0);
ui.label(
egui::RichText::new(
"Note: FIDO / passkey security keys aren't supported on this platform yet. Smart-card features (OATH, OpenPGP, PIV, Token2 Molto2) work over PC/SC.",
)
.font(theme::f_reg(12.5))
.color(p.txt3),
);
}
},
);
});
}
/// Device hero strip at the top of a key's pane.
fn device_hero(&mut self, ui: &mut egui::Ui, p: &Palette, dev: &UiDevice) {
let mut open_rename = false;
let mut do_save = false;
let mut do_cancel = false;
ui.horizontal(|ui| {
glyph_tile(ui, 46.0, p.raised2, p.txt2, Some(dev.vendor.chars().next().unwrap_or('?').to_ascii_uppercase()));
ui.add_space(12.0);
ui.vertical(|ui| {
ui.horizontal(|ui| {
if self.rename_open {
let resp = ui.add(
egui::TextEdit::singleline(&mut self.rename_input)
.hint_text("friendly-name")
.desired_width(200.0),
);
let enter = resp.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
if theme::button(ui, p, BtnKind::Primary, "Save").clicked() || enter {
do_save = true;
}
ui.add_space(4.0);
if theme::button(ui, p, BtnKind::Ghost, "Cancel").clicked() {
do_cancel = true;
}
} else {
ui.label(egui::RichText::new(dev.title()).font(theme::f_bold(21.0)).color(p.txt));
ui.add_space(5.0);
self.help_dot(ui, p, "device");
ui.add_space(8.0);
let label = if dev.name.is_some() { "Rename" } else { "Name this key" };
if ui
.add(
egui::Label::new(egui::RichText::new(label).font(theme::f_sb(12.0)).color(p.accent))
.sense(egui::Sense::click()),
)
.clicked()
{
open_rename = true;
}
}
});
if self.rename_open {
ui.add_space(3.0);
ui.label(
egui::RichText::new(
"Saves this key's serial with the name to keys.json on this computer \u{2014} nothing leaves your machine. Lowercase letters, digits, - and _.",
)
.font(theme::f_reg(11.5))
.color(p.txt3),
);
}
ui.add_space(2.0);
let serial = if dev.serial.is_empty() { "\u{2014}".to_string() } else { dev.serial.clone() };
let mut meta = format!("{} \u{00B7} #{} \u{00B7} {}", dev.vendor, serial, dev.transport);
if !dev.firmware.is_empty() {
meta.push_str(&format!(" \u{00B7} fw {}", dev.firmware));
}
ui.label(egui::RichText::new(meta).font(theme::f_reg(12.5)).color(p.txt2));
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.label(egui::RichText::new("Connected").font(theme::f_sb(12.5)).color(p.txt2));
ui.add_space(5.0);
theme::status_dot(ui, p.ok, 8.0);
});
});
self.apply_rename_actions(dev, open_rename, do_cancel, do_save);
ui.add_space(14.0);
let y = ui.cursor().top();
ui.painter()
.hline(ui.max_rect().x_range(), y, egui::Stroke::new(1.0, p.line));
}
/// Capability tab bar under the hero. The active tab gets `txt` + an accent
/// underline; the rest are muted.
fn cap_tabs(&mut self, ui: &mut egui::Ui, p: &Palette, dev: &UiDevice) {
ui.add_space(12.0);
let mut next = None;
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 20.0;
for t in dev.tabs() {
let active = self.cap_tab == t;
let color = if active { p.txt } else { p.txt3 };
let resp = ui.add(
egui::Label::new(
egui::RichText::new(cap_tab_label(t))
.font(theme::f_sb(13.5))
.color(color),
)
.sense(egui::Sense::click()),
);
if active {
let y = resp.rect.bottom() + 6.0;
ui.painter().line_segment(
[
egui::pos2(resp.rect.left(), y),
egui::pos2(resp.rect.right(), y),
],
egui::Stroke::new(2.0, p.accent),
);
}
if resp.clicked() {
next = Some(t);
}
}
});
if let Some(t) = next {
self.cap_tab = t;
}
}
/// A card header row: title · "?" help · right-aligned "Manage →". Returns
/// true when Manage is clicked (caller switches `cap_tab`).
fn card_head(
&mut self,
ui: &mut egui::Ui,
p: &Palette,
title: &str,
topic: &'static str,
) -> bool {
let mut go = false;
ui.horizontal(|ui| {
ui.label(
egui::RichText::new(title)
.font(theme::f_sb(14.5))
.color(p.txt),
);
ui.add_space(6.0);
self.help_dot(ui, p, topic);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui
.add(
egui::Label::new(
egui::RichText::new("Manage \u{2192}")
.font(theme::f_sb(12.5))
.color(p.accent),
)
.sense(egui::Sense::click()),
)
.clicked()
{
go = true;
}
});
});
go
}
/// Overview tab: one summary card per capability, each with a `Manage →` jump.
/// Scrolling comes from the shared capability-pane scroller in `central`.
fn overview(&mut self, ui: &mut egui::Ui, p: &Palette, dev: &UiDevice) {
ui.vertical(|ui| {
if dev.caps.has(Caps::FIDO2) {
theme::card_frame(p).show(ui, |ui| {
if self.card_head(ui, p, "Passkeys & sign-in (FIDO2)", "fido2") {
self.cap_tab = CapTab::Fido2;
}
ui.add_space(8.0);
match self
.security_keys
.info
.as_ref()
.and_then(|i| i.option("clientPin"))
{
Some(true) => {
ui.horizontal(|ui| {
theme::pill(ui, "PIN set", p.ok, p.ok_soft());
ui.add_space(8.0);
ui.label(
egui::RichText::new(
"PIN configured \u{00B7} ready for passkeys",
)
.font(theme::f_reg(13.0))
.color(p.txt2),
);
});
}
Some(false) => {
theme::pill(ui, "No PIN configured", p.warn, p.warn_soft());
}
None => {
ui.label(
egui::RichText::new("Reading key\u{2026}")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
}
});
ui.add_space(14.0);
}
if dev.caps.has(Caps::OATH) {
theme::card_frame(p).show(ui, |ui| {
if self.card_head(ui, p, "Authenticator codes (OATH)", "oath") {
self.cap_tab = CapTab::Oath;
}
ui.add_space(8.0);
if self.oath.loaded && !self.oath.creds.is_empty() {
let total = self.oath.creds.len();
let mut copy = None;
for row in self.oath.creds.iter().take(2) {
let is_copied =
self.copied.as_ref().is_some_and(|(n, _)| n == &row.name);
if let (Some(code), _) =
oath_row(ui, p, &row.name, row.code.as_deref(), is_copied, false)
{
copy = Some((row.name.clone(), code));
}
ui.add_space(6.0);
}
if total > 2 {
ui.label(
egui::RichText::new(format!("+{} more codes", total - 2))
.font(theme::f_reg(12.5))
.color(p.txt3),
);
}
if let Some((name, code)) = copy {
ui.output_mut(|o| o.copied_text = code.clone());
self.copied = Some((name, now_secs_f64() + 1.2));
self.clipboard_clear_at = Some((code, now_secs_f64() + 45.0));
}
} else {
ui.label(
egui::RichText::new("Open Authenticator to view live codes.")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
});
ui.add_space(14.0);
}
if dev.caps.has(Caps::PGP) {
theme::card_frame(p).show(ui, |ui| {
if self.card_head(ui, p, "OpenPGP", "pgp") {
self.cap_tab = CapTab::Pgp;
}
ui.add_space(8.0);
if let Some(st) = &self.openpgp.status {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 5.0;
theme::pill(
ui,
&format!(
"Signature \u{00B7} {}",
slot_summary(st.sig_algo_id, &st.fingerprint_sig)
),
p.txt2,
p.raised2,
);
theme::pill(
ui,
&format!(
"Encryption \u{00B7} {}",
slot_summary(st.dec_algo_id, &st.fingerprint_dec)
),
p.txt2,
p.raised2,
);
theme::pill(
ui,
&format!(
"Auth \u{00B7} {}",
slot_summary(st.aut_algo_id, &st.fingerprint_aut)
),
p.txt2,
p.raised2,
);
});
} else {
ui.label(
egui::RichText::new("Open OpenPGP and Read status to view key slots.")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
});
ui.add_space(14.0);
}
if dev.caps.has(Caps::PIV) {
theme::card_frame(p).show(ui, |ui| {
if self.card_head(ui, p, "PIV smart card", "piv") {
self.cap_tab = CapTab::Piv;
}
ui.add_space(8.0);
if let Some(st) = &self.piv.status {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 5.0;
for slot in &st.slots {
let lab = format!(
"{:02X} \u{00B7} {}",
slot.slot.key_ref(),
if slot.cert_present { "cert" } else { "empty" }
);
theme::pill(ui, &lab, p.txt2, p.raised2);
}
});
} else {
ui.label(
egui::RichText::new("Open PIV to read certificate slots.")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
});
}
});
}
/// FIDO2 / Passkeys tab — reuses the existing PIN + credentials section.
fn cap_fido2(&mut self, ui: &mut egui::Ui, p: &Palette) {
let pin_set = self
.security_keys
.info
.as_ref()
.and_then(|i| i.option("clientPin"));
// --- PIN & sign-in card (inline Set / Change PIN; no floating modal) ---
let mut go_set = false;
let mut go_change = false;
let mut cancel = false;
theme::card_frame(p).show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("PIN & sign-in")
.font(theme::f_sb(14.5))
.color(p.txt),
);
ui.add_space(6.0);
self.help_dot(ui, p, "pin");
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let (kind, label) = match pin_set {
Some(true) => (BtnKind::Default, "Change PIN"),
Some(false) => (BtnKind::Primary, "Set a PIN"),
None => return,
};
if theme::button(ui, p, kind, label).clicked() {
let open = !self.security_keys.change_pin.open;
self.security_keys.change_pin = ChangePinDialog {
open,
..Default::default()
};
self.security_keys.error = None;
}
});
});
ui.add_space(8.0);
match pin_set {
Some(true) => {
ui.horizontal(|ui| {
theme::pill(ui, "PIN set", p.ok, p.ok_soft());
ui.add_space(8.0);
ui.label(
egui::RichText::new(
"This key has a PIN. Unlock below to manage passkeys.",
)
.font(theme::f_reg(13.0))
.color(p.txt2),
);
});
}
Some(false) => {
ui.horizontal(|ui| {
theme::pill(ui, "No PIN yet", p.warn, p.warn_soft());
ui.add_space(8.0);
ui.label(
egui::RichText::new(
"Set a PIN to protect this key and turn on passkeys.",
)
.font(theme::f_reg(13.0))
.color(p.txt2),
);
});
}
None => {
let msg = if self.security_keys.error.is_some() {
"Couldn't read this key."
} else {
// The initial read can be dropped if the worker was
// busy at selection time; retry rather than showing
// "Reading key…" forever.
if self.security_keys.init.is_none() && !self.busy() {
self.fetch_selected_info();
}
"Reading key\u{2026}"
};
ui.label(
egui::RichText::new(msg)
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
}
if self.security_keys.change_pin.open {
let setting = pin_set == Some(false);
ui.add_space(10.0);
egui::Frame::none()
.fill(p.raised)
.inner_margin(egui::Margin::same(12.0))
.rounding(egui::Rounding::same(8.0))
.show(ui, |ui| {
ui.label(
egui::RichText::new(if setting {
"Create a PIN"
} else {
"Change PIN"
})
.font(theme::f_sb(13.0))
.color(p.txt),
);
ui.add_space(8.0);
if setting {
pin_field(ui, p, "New PIN", &mut self.security_keys.change_pin.new);
pin_field(ui, p, "Confirm", &mut self.security_keys.change_pin.confirm);
} else {
pin_field(ui, p, "Current PIN", &mut self.security_keys.change_pin.old);
pin_field(ui, p, "New PIN", &mut self.security_keys.change_pin.new);
}
ui.add_space(8.0);
ui.horizontal(|ui| {
if theme::button(
ui,
p,
BtnKind::Primary,
if setting { "Set PIN" } else { "Change PIN" },
)
.clicked()
{
if setting {
go_set = true;
} else {
go_change = true;
}
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Ghost, "Cancel").clicked() {
cancel = true;
}
});
ui.add_space(4.0);
ui.label(
egui::RichText::new(
"4\u{2013}63 characters. You'll touch the key to confirm.",
)
.font(theme::f_reg(11.5))
.color(p.txt3),
);
});
}
if let Some(err) = &self.security_keys.error {
ui.add_space(6.0);
ui.colored_label(p.err, err);
}
});
if cancel {
self.security_keys.change_pin = ChangePinDialog::default();
}
if go_set {
self.submit_set_pin();
}
if go_change {
self.submit_change_pin();
}
// --- Resident passkeys (only meaningful once a PIN exists) ---
if pin_set == Some(true) {
ui.add_space(14.0);
let mut lock = false;
let mut reload = false;
let mut unlock = false;
let mut delete: Option<Vec<u8>> = None;
theme::card_frame(p).show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("Resident passkeys")
.font(theme::f_sb(14.5))
.color(p.txt),
);
ui.add_space(6.0);
self.help_dot(ui, p, "passkeys");
if self.security_keys.session.is_some() {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Ghost, "Lock").clicked() {
lock = true;
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Default, "Reload").clicked() {
reload = true;
}
});
}
});
ui.add_space(8.0);
let session_info = self.security_keys.session.as_ref().map(|s| {
(
s.metadata.existing_count,
s.metadata.max_remaining,
s.rps.clone(),
)
});
if let Some((existing, max_remaining, rps)) = session_info {
ui.label(
egui::RichText::new(format!(
"{existing} stored \u{00B7} room for {max_remaining} more"
))
.font(theme::f_reg(12.5))
.color(p.txt2),
);
ui.add_space(6.0);
if rps.is_empty() {
ui.label(
egui::RichText::new("No passkeys stored on this key yet.")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
// No inner scroller: the shared capability-pane scroller in
// `central` handles overflow, so a long passkey list flows
// down the page instead of trapping the wheel in a box.
ui.vertical(|ui| {
for (rp, creds) in rps.iter() {
let header =
if let Some(name) = rp.name.as_ref().filter(|s| !s.is_empty()) {
format!("{} ({})", rp.id, name)
} else {
rp.id.clone()
};
ui.collapsing(header, |ui| {
if creds.is_empty() {
ui.label("(no credentials)");
}
for c in creds {
ui.horizontal(|ui| {
ui.monospace(hex_short(&c.credential_id));
let user_field = c
.user
.display_name
.clone()
.or_else(|| c.user.name.clone())
.unwrap_or_else(|| {
String::from_utf8_lossy(&c.user.id).into_owned()
});
ui.label(user_field);
ui.with_layout(
egui::Layout::right_to_left(egui::Align::Center),
|ui| {
if theme::button(ui, p, BtnKind::Ghost, "Remove")
.clicked()
{
delete = Some(c.credential_id.clone());
}
},
);
});
}
});
}
});
} else {
ui.horizontal(|ui| {
let resp = ui.add(
egui::TextEdit::singleline(&mut self.security_keys.pin_input)
.password(true)
.hint_text("Enter PIN to view passkeys")
.desired_width(220.0),
);
let submit = theme::button(ui, p, BtnKind::Primary, "Unlock").clicked();
if submit
|| (resp.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)))
{
unlock = true;
}
});
}
});
if lock {
self.lock_session();
}
if reload {
self.refresh_credentials();
}
if unlock {
self.try_unlock();
}
if let Some(id) = delete {
self.delete_credential(id);
}
}
// --- Danger: reset key (typed-confirm modal stays) ---
ui.add_space(14.0);
let mut arm_reset = false;
theme::card_frame(p)
.stroke(egui::Stroke::new(1.0, theme::tint(p.err, 90)))
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("Reset this key")
.font(theme::f_sb(14.5))
.color(p.err),
);
ui.add_space(6.0);
self.help_dot(ui, p, "reset");
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Danger, "Reset key\u{2026}").clicked() {
arm_reset = true;
}
});
});
ui.label(
egui::RichText::new(
"Wipes every passkey and the PIN on this key. Cannot be undone.",
)
.font(theme::f_reg(12.5))
.color(p.txt2),
);
});
if arm_reset {
self.security_keys.reset = ResetDialog {
open: true,
..Default::default()
};
}
}
/// Authenticator / OATH tab — live codes with countdown rings + copy.
fn cap_oath(&mut self, ui: &mut egui::Ui, p: &Palette) {
// Auto-attempt a read once per selection (a hard error won't retry).
if !self.oath_tried && !self.busy() && self.oath.error.is_none() && !self.oath.locked {
self.oath_tried = true;
self.load_oath_creds();
}
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("Authenticator codes")
.font(theme::f_sb(14.5))
.color(p.txt),
);
ui.add_space(6.0);
self.help_dot(ui, p, "oath");
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Primary, "+ Add credential").clicked() {
// No struct-update syntax: OathAddDialog has a Drop impl
// (it wipes the typed seed), which forbids `..Default`.
self.oath.add = OathAddDialog {
open: true,
name: String::new(),
secret: String::new(),
totp: true,
require_touch: false,
};
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Default, "Refresh").clicked() {
self.load_oath_creds();
}
});
});
ui.add_space(12.0);
if let Some(err) = &self.oath.error {
ui.colored_label(p.err, err);
ui.add_space(6.0);
}
self.render_oath_add_form(ui);
if self.oath.locked {
theme::card_frame(p).show(ui, |ui| {
ui.label(
egui::RichText::new("This key's OATH applet is password-protected.")
.font(theme::f_reg(13.0))
.color(p.txt),
);
ui.add_space(8.0);
ui.horizontal(|ui| {
ui.add(
egui::TextEdit::singleline(&mut self.oath.password_input)
.password(true)
.desired_width(220.0),
);
if theme::button(ui, p, BtnKind::Primary, "Unlock").clicked() {
self.load_oath_creds();
}
});
});
return;
}
if !self.oath.loaded {
ui.label(
egui::RichText::new("Reading codes\u{2026}")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
return;
}
if self.oath.creds.is_empty() {
ui.label(
egui::RichText::new("No authenticator codes on this key.")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
return;
}
let mut copy: Option<(String, String)> = None;
let mut delete: Option<String> = None;
theme::card_frame(p).show(ui, |ui| {
let n = self.oath.creds.len();
for (i, row) in self.oath.creds.iter().enumerate() {
let is_copied = self.copied.as_ref().is_some_and(|(nm, _)| nm == &row.name);
let (c, d) = oath_row(ui, p, &row.name, row.code.as_deref(), is_copied, true);
if let Some(code) = c {
copy = Some((row.name.clone(), code));
}
if d {
delete = Some(row.name.clone());
}
if i + 1 < n {
ui.add_space(5.0);
let y = ui.cursor().top();
ui.painter().hline(
ui.max_rect().x_range(),
y,
egui::Stroke::new(1.0, p.line_soft),
);
ui.add_space(5.0);
}
}
});
if let Some((name, code)) = copy {
ui.output_mut(|o| o.copied_text = code.clone());
self.copied = Some((name, now_secs_f64() + 1.2));
self.clipboard_clear_at = Some((code, now_secs_f64() + 45.0));
}
if let Some(name) = delete {
self.oath.confirm_delete = Some(name);
}
}
/// OpenPGP tab — read-only status + the existing management section.
fn cap_pgp(&mut self, ui: &mut egui::Ui, p: &Palette) {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("OpenPGP")
.font(theme::f_sb(14.5))
.color(p.txt),
);
ui.add_space(6.0);
self.help_dot(ui, p, "pgp");
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Default, "Read status").clicked() {
self.load_openpgp_status();
}
});
});
ui.add_space(12.0);
if let Some(err) = &self.openpgp.error {
ui.colored_label(p.err, err);
ui.add_space(6.0);
}
if let Some(notice) = &self.openpgp.notice {
ui.colored_label(p.ok, notice);
ui.add_space(6.0);
}
if let Some(status) = &self.openpgp.status {
theme::card_frame(p).show(ui, |ui| {
kv(ui, "AID", &hex_lower(&status.aid));
if let Some(serial) = status.serial() {
kv(ui, "Serial", &format!("{serial} (0x{serial:08X})"));
}
kv(
ui,
"Signature key",
&format!(
"{} {}",
algo_id_label(status.sig_algo_id),
fpr_label(&status.fingerprint_sig)
),
);
kv(
ui,
"Decryption key",
&format!(
"{} {}",
algo_id_label(status.dec_algo_id),
fpr_label(&status.fingerprint_dec)
),
);
kv(
ui,
"Authentication key",
&format!(
"{} {}",
algo_id_label(status.aut_algo_id),
fpr_label(&status.fingerprint_aut)
),
);
kv(
ui,
"PIN retries",
&format!(
"PW1={} RC={} PW3={}",
status.tries_pw1, status.tries_rc, status.tries_pw3
),
);
kv(
ui,
"Signatures made",
&status
.signature_count
.map_or("(unavailable)".to_string(), |n| n.to_string()),
);
});
} else {
ui.label(
egui::RichText::new("Click Read status to read this card (no PIN or touch).")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
ui.add_space(10.0);
self.render_openpgp_manage(ui, p);
}
/// PIV tab — read-only status snapshot (auto-read on first view).
fn cap_piv(&mut self, ui: &mut egui::Ui, p: &Palette) {
if !self.piv_tried && !self.busy() {
self.piv_tried = true;
self.load_piv_status();
}
// Intents collected inside the UI closures and applied afterwards, so a
// submit method's `&mut self` never overlaps a card's borrow.
let mut do_refresh = false;
let mut go_change_pin = false;
let mut go_change_puk = false;
let mut go_unblock = false;
let mut go_generate = false;
let mut go_import = false;
let mut go_export = false;
let mut go_self_sign = false;
let mut go_csr = false;
let mut go_set_retries = false;
let mut go_change_mgmt = false;
let mut arm_reset = false;
let mut copy_pem: Option<String> = None;
let head = |ui: &mut egui::Ui, t: &str| card_head(ui, p, t);
let sub = |ui: &mut egui::Ui, t: &str| card_sub(ui, p, t);
let note = |ui: &mut egui::Ui, t: &str| card_note(ui, p, t);
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("PIV smart card")
.font(theme::f_sb(14.5))
.color(p.txt),
);
ui.add_space(6.0);
self.help_dot(ui, p, "piv");
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Default, "Refresh").clicked() {
do_refresh = true;
}
});
});
ui.add_space(10.0);
if let Some(err) = &self.piv.error {
ui.colored_label(p.err, err);
ui.add_space(6.0);
}
if let Some(n) = &self.piv.notice {
ui.colored_label(p.ok, n);
ui.add_space(6.0);
}
// --- Status ---
if let Some(st) = &self.piv.status {
theme::card_frame(p).show(ui, |ui| {
kv(
ui,
"Applet version",
&st.version
.map_or("\u{2014}".to_string(), |(a, b, c)| format!("{a}.{b}.{c}")),
);
kv(
ui,
"Serial",
&st.serial.map_or("\u{2014}".to_string(), |s| s.to_string()),
);
kv(
ui,
"PIN retries",
&st.pin_retries
.map_or("\u{2014}".to_string(), |n| n.to_string()),
);
ui.add_space(6.0);
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 5.0;
for slot in &st.slots {
let lab = format!(
"{:02X} \u{00B7} {}",
slot.slot.key_ref(),
if slot.cert_present { "cert" } else { "empty" }
);
theme::pill(ui, &lab, p.txt2, p.raised2);
}
});
});
} else if self.piv.error.is_none() {
ui.label(
egui::RichText::new("Reading PIV status\u{2026}")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
ui.add_space(10.0);
// --- Management key: authorizes key-gen / cert-import / retries / rotation ---
theme::card_frame(p).show(ui, |ui| {
head(ui, "Management key");
note(
ui,
"Hex key authorizing key generation, certificate import, retry \
changes, and rotation below. Factory default is \
010203040506070801020304050607080102030405060708. Sent for the \
operation only; never stored.",
);
ui.add_space(6.0);
secret_field(
ui,
p,
"Management key",
&mut self.piv.mgmt_key_input,
"hex (48/32/64 chars)",
300.0,
);
});
ui.add_space(10.0);
// --- PIN & PUK ---
egui::CollapsingHeader::new(
egui::RichText::new("PIN & PUK")
.font(theme::f_sb(13.5))
.color(p.txt),
)
.id_salt("piv-pin-puk")
.show(ui, |ui| {
theme::card_frame(p).show(ui, |ui| {
sub(ui, "Change PIN");
pin_field(ui, p, "Current PIN", &mut self.piv.pin_old);
pin_field(ui, p, "New PIN", &mut self.piv.pin_new);
if theme::button(ui, p, BtnKind::Default, "Change PIN").clicked() {
go_change_pin = true;
}
ui.add_space(10.0);
sub(ui, "Change PUK");
pin_field(ui, p, "Current PUK", &mut self.piv.puk_old);
pin_field(ui, p, "New PUK", &mut self.piv.puk_new);
if theme::button(ui, p, BtnKind::Default, "Change PUK").clicked() {
go_change_puk = true;
}
ui.add_space(10.0);
sub(ui, "Unblock PIN with PUK");
note(ui, "Recovers a blocked PIN without wiping any keys.");
pin_field(ui, p, "PUK", &mut self.piv.unblock_puk);
pin_field(ui, p, "New PIN", &mut self.piv.unblock_new_pin);
if theme::button(ui, p, BtnKind::Default, "Unblock PIN").clicked() {
go_unblock = true;
}
});
});
ui.add_space(6.0);
// --- Keys & certificates ---
egui::CollapsingHeader::new(
egui::RichText::new("Keys & certificates")
.font(theme::f_sb(13.5))
.color(p.txt),
)
.id_salt("piv-keys")
.default_open(true)
.show(ui, |ui| {
theme::card_frame(p).show(ui, |ui| {
sub(ui, "Generate key pair");
note(
ui,
"Creates a fresh key in the slot (OVERWRITES it) and shows its \
public key. Needs the management key above.",
);
ui.horizontal(|ui| {
piv_slot_combo(ui, "piv-gen-slot", &mut self.piv.gen_slot);
piv_keyalg_combo(ui, "piv-gen-alg", &mut self.piv.gen_alg);
if theme::button(ui, p, BtnKind::Default, "Generate\u{2026}").clicked() {
go_generate = true;
}
});
if let Some(pem) = &self.piv.gen_pubkey_pem {
ui.add_space(4.0);
ui.add(
egui::TextEdit::multiline(&mut pem.as_str())
.desired_rows(4)
.desired_width(360.0)
.font(egui::TextStyle::Monospace),
);
if theme::button(ui, p, BtnKind::Ghost, "Copy public key").clicked() {
copy_pem = Some(pem.clone());
}
}
ui.add_space(10.0);
sub(ui, "Create certificate");
note(
ui,
"Signed on the card with the slot's key (generate one above \
first). \u{201c}Self-signed\u{201d} stores the certificate in \
the slot — needs the PIN and the management key. \u{201c}Save \
CSR\u{201d} writes a request file to hand to a certificate \
authority — needs only the PIN.",
);
ui.horizontal(|ui| {
piv_slot_combo(ui, "piv-certify-slot", &mut self.piv.certify_slot);
});
text_field(
ui,
p,
"Name",
&mut self.piv.cert_subject,
"e.g. Alice — or full CN=Alice,O=Example,C=US",
300.0,
);
pin_field(ui, p, "PIN", &mut self.piv.sign_pin);
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("Valid for")
.font(theme::f_reg(13.0))
.color(p.txt2),
);
ui.add(
egui::DragValue::new(&mut self.piv.cert_days)
.range(1..=3650u32)
.suffix(" days"),
);
ui.add_space(8.0);
if theme::button(ui, p, BtnKind::Default, "Self-signed \u{2192} slot").clicked()
{
go_self_sign = true;
}
});
text_field(
ui,
p,
"CSR file",
&mut self.piv.csr_path,
"/path/to/request.csr",
240.0,
);
if theme::button(ui, p, BtnKind::Default, "Sign & save CSR").clicked() {
go_csr = true;
}
ui.add_space(10.0);
sub(ui, "Import certificate");
note(ui, "PEM or DER X.509 file. Needs the management key.");
ui.horizontal(|ui| {
piv_slot_combo(ui, "piv-cert-slot", &mut self.piv.cert_slot);
});
text_field(
ui,
p,
"File",
&mut self.piv.cert_path,
"/path/to/cert.pem",
240.0,
);
if theme::button(ui, p, BtnKind::Default, "Import certificate").clicked() {
go_import = true;
}
ui.add_space(10.0);
sub(ui, "Export certificate");
note(ui, "Writes the slot's certificate as DER. No PIN needed.");
ui.horizontal(|ui| {
piv_slot_combo(ui, "piv-export-slot", &mut self.piv.export_slot);
});
text_field(
ui,
p,
"Destination",
&mut self.piv.export_path,
"/path/to/out.der",
240.0,
);
if theme::button(ui, p, BtnKind::Default, "Export certificate").clicked() {
go_export = true;
}
});
});
ui.add_space(6.0);
// --- Retry counts & management-key rotation ---
egui::CollapsingHeader::new(
egui::RichText::new("Retry counts & key rotation")
.font(theme::f_sb(13.5))
.color(p.txt),
)
.id_salt("piv-admin")
.show(ui, |ui| {
theme::card_frame(p).show(ui, |ui| {
sub(ui, "Set PIN/PUK retry counts");
note(
ui,
"Resets PIN and PUK to factory defaults. Needs the management \
key above and the current PIN.",
);
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("PIN tries")
.font(theme::f_reg(13.0))
.color(p.txt2),
);
ui.add(egui::DragValue::new(&mut self.piv.retries_pin).range(1..=15u8));
ui.add_space(8.0);
ui.label(
egui::RichText::new("PUK tries")
.font(theme::f_reg(13.0))
.color(p.txt2),
);
ui.add(egui::DragValue::new(&mut self.piv.retries_puk).range(1..=15u8));
});
pin_field(ui, p, "Current PIN", &mut self.piv.retries_pin_auth);
if theme::button(ui, p, BtnKind::Default, "Set retry counts").clicked() {
go_set_retries = true;
}
ui.add_space(10.0);
sub(ui, "Change management key");
note(ui, "Enter the current key above; the new key here.");
secret_field(
ui,
p,
"New key",
&mut self.piv.new_mgmt_key_input,
"hex (48/32/64 chars)",
300.0,
);
ui.horizontal(|ui| {
piv_mgmtalg_combo(ui, "piv-new-mgmt-alg", &mut self.piv.new_mgmt_alg);
if theme::button(ui, p, BtnKind::Default, "Change management key").clicked() {
go_change_mgmt = true;
}
});
});
});
ui.add_space(10.0);
// --- Danger: reset applet ---
theme::card_frame(p)
.stroke(egui::Stroke::new(1.0, theme::tint(p.err, 90)))
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(
egui::RichText::new("Reset applet")
.font(theme::f_sb(14.0))
.color(p.err),
);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if theme::button(ui, p, BtnKind::Danger, "Reset applet\u{2026}").clicked() {
arm_reset = true;
}
});
});
ui.label(
egui::RichText::new(
"Wipes ALL PIV keys, certificates, and PINs. Only works when both \
the PIN and PUK are already blocked.",
)
.font(theme::f_reg(12.0))
.color(p.txt2),
);
});
// Apply collected intents now that the card borrows have ended.
if do_refresh {
self.load_piv_status();
}
if go_change_pin {
self.piv_change_pin();
}
if go_change_puk {
self.piv_change_puk();
}
if go_unblock {
self.piv_unblock_pin();
}
if go_generate {
self.piv_generate_key();
}
if go_import {
self.piv_import_cert();
}
if go_export {
self.piv_export_cert();
}
if go_self_sign {
self.piv_self_sign();
}
if go_csr {
self.piv_request_csr();
}
if go_set_retries {
self.piv_set_retries();
}
if go_change_mgmt {
self.piv_change_management_key();
}
if arm_reset {
self.piv.confirm_reset = Some(String::new());
}
if let Some(pem) = copy_pem {
ui.output_mut(|o| o.copied_text = pem);
self.clipboard_clear_at = None; // public key, not a secret to auto-clear
}
}
/// The Molto2 token's dedicated amber view: hero band · customer-key strip ·
/// 100-slot rail + editor.
fn molto_view(&mut self, ui: &mut egui::Ui, p: &Palette, dev: &UiDevice) {
// Make brand-orange the accent throughout the Molto2 view, so its help
// dots, links, selection highlights and primary action are all one
// orange rather than mixing the app's blue accent into the token's
// identity. Green stays for status, red for danger.
let mp = Palette {
accent: p.brand,
..*p
};
let p = ∓
let mut open_rename = false;
let mut do_save = false;
let mut do_cancel = false;
// Hero (no amber band — the orange comes from the clock glyph + accents,
// matching the security-key hero layout and avoiding a muddy brown tint).
egui::Frame::none()
.inner_margin(egui::Margin::symmetric(26.0, 16.0))
.show(ui, |ui| {
ui.horizontal(|ui| {
glyph_tile(ui, 46.0, p.brand, p.accent_ink, None);
ui.add_space(12.0);
ui.vertical(|ui| {
ui.horizontal(|ui| {
if self.rename_open {
let resp = ui.add(
egui::TextEdit::singleline(&mut self.rename_input)
.hint_text("friendly-name")
.desired_width(200.0),
);
let enter = resp.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
if theme::button(ui, p, BtnKind::Primary, "Save").clicked() || enter {
do_save = true;
}
ui.add_space(4.0);
if theme::button(ui, p, BtnKind::Ghost, "Cancel").clicked() {
do_cancel = true;
}
} else {
ui.label(egui::RichText::new(dev.title()).font(theme::f_bold(21.0)).color(p.txt));
ui.add_space(6.0);
theme::pill(ui, "Programmable TOTP token", p.brand, p.brand_soft());
ui.add_space(4.0);
self.help_dot(ui, p, "molto");
ui.add_space(6.0);
let label = if dev.name.is_some() { "Rename" } else { "Name this token" };
if ui
.add(
egui::Label::new(egui::RichText::new(label).font(theme::f_sb(12.0)).color(p.accent))
.sense(egui::Sense::click()),
)
.clicked()
{
open_rename = true;
}
}
});
if self.rename_open {
ui.add_space(3.0);
ui.label(
egui::RichText::new(
"Saves this token's serial with the name to keys.json on this computer \u{2014} nothing leaves your machine.",
)
.font(theme::f_reg(11.5))
.color(p.txt3),
);
}
ui.add_space(2.0);
let serial = if dev.serial.is_empty() { "\u{2014}".to_string() } else { dev.serial.clone() };
ui.label(
egui::RichText::new(format!("{} \u{00B7} #{} \u{00B7} {} slots", dev.vendor, serial, PROFILES))
.font(theme::f_reg(12.5))
.color(p.txt2),
);
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.label(egui::RichText::new("Connected").font(theme::f_sb(12.5)).color(p.txt2));
ui.add_space(5.0);
theme::status_dot(ui, p.ok, 8.0);
});
});
});
self.apply_rename_actions(dev, open_rename, do_cancel, do_save);
// --- Device-wide settings (apply to the whole token) ---
egui::Frame::none()
.inner_margin(egui::Margin::symmetric(26.0, 14.0))
.show(ui, |ui| {
theme::card_frame(p).show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(egui::RichText::new("Device").font(theme::f_sb(14.5)).color(p.txt));
ui.add_space(6.0);
self.help_dot(ui, p, "molto");
});
ui.add_space(10.0);
ui.horizontal(|ui| {
ui.add_sized(
[104.0, 22.0],
egui::Label::new(egui::RichText::new("Customer key").font(theme::f_reg(13.0)).color(p.txt2)),
);
self.help_dot(ui, p, "custkey");
ui.add_space(6.0);
ui.add(
egui::TextEdit::singleline(&mut self.customer_key_input)
.password(true)
.hint_text("default if empty")
.desired_width(200.0),
);
ui.checkbox(&mut self.customer_key_hex, "hex");
if theme::button(ui, p, BtnKind::Default, "Authenticate").clicked() {
self.authenticate();
}
if self.authenticated {
theme::pill(ui, "authed", p.ok, p.ok_soft());
}
});
ui.add_space(4.0);
ui.label(
egui::RichText::new("Programming any slot first needs the token's customer key (blank = factory default).")
.font(theme::f_reg(11.5))
.color(p.txt3),
);
ui.add_space(12.0);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Default, "Sync time on all").clicked() {
self.sync_time_all();
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Default, "Bulk import\u{2026}").clicked() {
self.bulk_dialog.open = true;
self.bulk_dialog.start = self.slot;
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Danger, "Factory reset\u{2026}").clicked() {
self.molto_reset_confirm = true;
}
});
if self.molto_reset_confirm {
ui.add_space(10.0);
egui::Frame::none()
.fill(p.err_soft())
.inner_margin(egui::Margin::same(12.0))
.rounding(egui::Rounding::same(8.0))
.show(ui, |ui| {
ui.label(
egui::RichText::new("Factory-reset the token? This wipes all slots, then asks you to confirm with the \u{25B2} button on the device itself.")
.font(theme::f_reg(12.5))
.color(p.txt),
);
ui.add_space(8.0);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Danger, "Yes, factory reset").clicked() {
self.molto_reset_confirm = false;
self.factory_reset();
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Default, "Cancel").clicked() {
self.molto_reset_confirm = false;
}
});
});
}
});
});
// --- Per-slot programming (applies only to the selected slot) ---
egui::Frame::none()
.inner_margin(egui::Margin::symmetric(26.0, 4.0))
.show(ui, |ui| {
theme::card_frame(p).show(ui, |ui| {
ui.label(egui::RichText::new("Program a slot").font(theme::f_sb(14.5)).color(p.txt));
ui.add_space(4.0);
ui.label(
egui::RichText::new("The token is write-only: pick a slot and program it. The Molto2 shows codes on its own screen \u{2014} they can't be read back here.")
.font(theme::f_reg(11.5))
.color(p.txt3),
);
ui.add_space(10.0);
ui.horizontal_top(|ui| {
ui.vertical(|ui| {
ui.set_width(140.0);
// Fill the remaining height so the rail isn't a fixed
// block jammed at the window bottom; leave a margin.
let rail_h = (ui.available_height() - 48.0).max(160.0);
let mut pick = None;
egui::ScrollArea::vertical().auto_shrink([false, false]).max_height(rail_h).show(ui, |ui| {
for s in 0..PROFILES {
let selected = s == self.slot;
let (rect, resp) = ui.allocate_exact_size(egui::vec2(ui.available_width(), 30.0), egui::Sense::click());
let bg = if selected {
p.brand_soft()
} else if resp.hovered() {
p.line_soft
} else {
egui::Color32::TRANSPARENT
};
ui.painter().rect(rect, egui::Rounding::same(8.0), bg, egui::Stroke::NONE);
ui.painter().text(
rect.left_center() + egui::vec2(12.0, 0.0),
egui::Align2::LEFT_CENTER,
format!("Slot {s:02}"),
theme::f_mono(12.5),
if selected { p.brand } else { p.txt2 },
);
if resp.clicked() {
pick = Some(s);
}
}
});
if let Some(s) = pick {
self.slot = s;
}
});
ui.add_space(24.0);
ui.vertical(|ui| {
ui.label(egui::RichText::new(format!("SLOT {:02}", self.slot)).font(theme::f_bold(11.0)).color(p.brand));
ui.add_space(10.0);
editor_row(ui, p, "Title", |ui| {
ui.add(egui::TextEdit::singleline(&mut self.draft.title).hint_text("\u{2264} 12 chars").desired_width(360.0));
});
editor_row(ui, p, "Secret", |ui| {
ui.add(
egui::TextEdit::singleline(&mut self.draft.secret_base32)
.password(true)
.hint_text("base32 secret")
.desired_width(360.0),
);
});
// Two columns for the short choices, to use the width.
let field_label = |ui: &mut egui::Ui, w: f32, text: &str| {
ui.add_sized([w, 22.0], egui::Label::new(egui::RichText::new(text).font(theme::f_reg(13.0)).color(p.txt2)));
};
ui.horizontal(|ui| {
field_label(ui, 92.0, "Algorithm");
let cur = match self.draft.algorithm {
AlgoChoice::Sha1 => "SHA1",
AlgoChoice::Sha256 => "SHA256",
};
if let Some(v) = theme::segmented(ui, p, &["SHA1", "SHA256"], cur, p.brand) {
self.draft.algorithm = if v == "SHA256" { AlgoChoice::Sha256 } else { AlgoChoice::Sha1 };
}
ui.add_space(30.0);
field_label(ui, 50.0, "Digits");
let cur = match self.draft.digits {
DigitsChoice::Four => "4",
DigitsChoice::Six => "6",
DigitsChoice::Eight => "8",
DigitsChoice::Ten => "10",
};
if let Some(v) = theme::segmented(ui, p, &["4", "6", "8", "10"], cur, p.brand) {
self.draft.digits = match v.as_str() {
"4" => DigitsChoice::Four,
"8" => DigitsChoice::Eight,
"10" => DigitsChoice::Ten,
_ => DigitsChoice::Six,
};
}
});
ui.add_space(8.0);
ui.horizontal(|ui| {
field_label(ui, 92.0, "Time step");
let cur = match self.draft.time_step {
StepChoice::S30 => "30s",
StepChoice::S60 => "60s",
};
if let Some(v) = theme::segmented(ui, p, &["30s", "60s"], cur, p.brand) {
self.draft.time_step = if v == "60s" { StepChoice::S60 } else { StepChoice::S30 };
}
ui.add_space(30.0);
field_label(ui, 50.0, "Display");
let cur = match self.draft.display_timeout {
TimeoutChoice::S15 => "15s",
TimeoutChoice::S30 => "30s",
TimeoutChoice::S60 => "60s",
TimeoutChoice::S120 => "120s",
};
if let Some(v) = theme::segmented(ui, p, &["15s", "30s", "60s", "120s"], cur, p.brand) {
self.draft.display_timeout = match v.as_str() {
"15s" => TimeoutChoice::S15,
"60s" => TimeoutChoice::S60,
"120s" => TimeoutChoice::S120,
_ => TimeoutChoice::S30,
};
}
});
ui.add_space(8.0);
ui.horizontal(|ui| {
if theme::button(ui, p, BtnKind::Primary, "Write to slot").clicked() {
self.apply_draft();
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Default, "Import otpauth\u{2026}").clicked() {
self.import_dialog.open = true;
}
ui.add_space(6.0);
if theme::button(ui, p, BtnKind::Default, "Sync time").clicked() {
self.sync_time_selected();
}
});
});
});
});
});
}
/// The Molto2 import dialogs (otpauth:// + bulk). Reused verbatim from the
/// original Molto2 view; only the entry point changed.
fn molto_dialogs(&mut self, ctx: &egui::Context, _p: &Palette) {
if self.bulk_dialog.open {
let mut open = self.bulk_dialog.open;
let mut do_load = false;
let mut do_apply = false;
egui::Window::new("Bulk import")
.open(&mut open)
.collapsible(false)
.default_width(560.0)
.show(ctx, |ui| {
ui.label("Export file path (Aegis JSON [plain or encrypted], 2FAS JSON, or otpauth:// list):");
ui.add(
egui::TextEdit::singleline(&mut self.bulk_dialog.path)
.desired_width(540.0)
.hint_text("/path/to/export.json"),
);
if self.bulk_dialog.needs_password {
ui.label("Aegis vault password:");
ui.add(
egui::TextEdit::singleline(&mut self.bulk_dialog.password)
.password(true)
.desired_width(360.0),
);
}
ui.horizontal(|ui| {
let importing = self.import_busy();
if ui
.add_enabled(!importing, egui::Button::new("Load"))
.clicked()
{
do_load = true;
}
if importing {
ui.spinner();
if let Some(label) = &self.import_label {
ui.label(label.as_str());
}
}
ui.label("Start at profile:");
ui.add(
egui::DragValue::new(&mut self.bulk_dialog.start)
.clamp_existing_to_range(true)
.range(0..=99u8),
);
ui.label("Display timeout:");
egui::ComboBox::from_id_salt("bulk-timeout")
.selected_text(match self.bulk_dialog.display_timeout {
TimeoutChoice::S15 => "15s",
TimeoutChoice::S30 => "30s",
TimeoutChoice::S60 => "60s",
TimeoutChoice::S120 => "120s",
})
.show_ui(ui, |ui| {
ui.selectable_value(
&mut self.bulk_dialog.display_timeout,
TimeoutChoice::S15,
"15s",
);
ui.selectable_value(
&mut self.bulk_dialog.display_timeout,
TimeoutChoice::S30,
"30s",
);
ui.selectable_value(
&mut self.bulk_dialog.display_timeout,
TimeoutChoice::S60,
"60s",
);
ui.selectable_value(
&mut self.bulk_dialog.display_timeout,
TimeoutChoice::S120,
"120s",
);
});
});
if let Some(err) = &self.bulk_dialog.error {
ui.colored_label(egui::Color32::from_rgb(220, 100, 100), err);
}
if !self.bulk_dialog.entries.is_empty() {
ui.separator();
ui.label(format!(
"{} entries — will fill slots #{:02}..#{:02}",
self.bulk_dialog.entries.len(),
self.bulk_dialog.start,
self.bulk_dialog.start as usize + self.bulk_dialog.entries.len() - 1,
));
egui::ScrollArea::vertical()
.max_height(220.0)
.show(ui, |ui| {
for (i, e) in self.bulk_dialog.entries.iter().enumerate() {
let slot = self.bulk_dialog.start as usize + i;
ui.label(format!(
"#{:02} {} ({}/{:?}/{}d)",
slot,
e.suggested_title(),
match e.algorithm {
HmacAlgo::Sha1 => "SHA1",
HmacAlgo::Sha256 => "SHA256",
},
e.time_step,
e.digits as u8
));
}
});
ui.horizontal(|ui| {
// Block programming while a load is in flight so a
// stale entry list can't be written mid-replace.
let can_apply = self.authenticated && !self.import_busy();
if ui
.add_enabled(can_apply, egui::Button::new("Program all"))
.on_hover_text("Write seed, title, and config for every entry")
.clicked()
{
do_apply = true;
}
if ui.button("Close").clicked() {
self.bulk_dialog.open = false;
}
});
}
});
self.bulk_dialog.open = open;
if do_load {
self.bulk_load();
}
if do_apply {
self.bulk_apply();
}
}
if self.import_dialog.open {
let mut open = self.import_dialog.open;
let mut should_apply = false;
egui::Window::new(format!("Import to profile #{:02}", self.slot))
.open(&mut open)
.collapsible(false)
.resizable(false)
.show(ctx, |ui| {
ui.label("Paste an otpauth:// URI:");
ui.add(
egui::TextEdit::multiline(&mut self.import_dialog.uri)
.desired_rows(3)
.desired_width(420.0)
.font(egui::TextStyle::Monospace),
);
ui.add_space(4.0);
ui.horizontal(|ui| {
if ui.button("Parse & populate fields").clicked() {
should_apply = true;
}
if ui.button("Cancel").clicked() {
self.import_dialog.open = false;
self.import_dialog.uri.clear();
}
});
});
self.import_dialog.open = open;
if should_apply {
self.import_otpauth();
}
}
}
}
/// Paint one OATH credential row: issuer/account · code · countdown ring ·
/// seconds · copy (· delete). Returns `(Some(code) if copy clicked, delete?)`.
fn oath_row(
ui: &mut egui::Ui,
p: &Palette,
name: &str,
code: Option<&str>,
is_copied: bool,
with_delete: bool,
) -> (Option<String>, bool) {
let (issuer, account) = match name.split_once(':') {
Some((a, b)) => (a.to_string(), b.to_string()),
None => (name.to_string(), String::new()),
};
let (secs, pct) = theme::totp_window(30);
let code_color = if secs <= 5 { p.warn } else { p.accent };
let mut copy = None;
let mut delete = false;
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.label(
egui::RichText::new(issuer)
.font(theme::f_sb(13.5))
.color(p.txt),
);
if !account.is_empty() {
ui.label(
egui::RichText::new(account)
.font(theme::f_reg(12.0))
.color(p.txt3),
);
}
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if with_delete {
let (xr, xresp) =
ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::click());
paint_x_icon(ui, xr.center(), p.txt3);
if xresp.on_hover_text("Delete").clicked() {
delete = true;
}
ui.add_space(8.0);
}
let (cr, cresp) = ui.allocate_exact_size(egui::vec2(20.0, 18.0), egui::Sense::click());
if is_copied {
paint_check_icon(ui, cr.center(), p.ok);
} else {
paint_copy_icon(ui, cr.center(), p.txt3);
}
if cresp.on_hover_text("Copy code").clicked() {
if let Some(c) = code {
copy = Some(c.to_string());
}
}
ui.add_space(10.0);
ui.label(
egui::RichText::new(format!("{secs}s"))
.font(theme::f_reg(11.0))
.color(p.txt3),
);
ui.add_space(5.0);
theme::ring(ui, pct, 20.0, code_color, p.line);
ui.add_space(14.0);
match code {
Some(c) => {
ui.label(
egui::RichText::new(c)
.font(theme::f_mono(19.0))
.color(code_color),
);
}
None => {
ui.label(
egui::RichText::new("touch")
.font(theme::f_reg(13.0))
.color(p.txt3),
);
}
}
});
});
(copy, delete)
}
/// One labelled editor row in the Molto2 form: fixed-width label + a field.
fn editor_row(ui: &mut egui::Ui, p: &Palette, label: &str, add: impl FnOnce(&mut egui::Ui)) {
ui.horizontal(|ui| {
ui.add_sized(
[92.0, 22.0],
egui::Label::new(
egui::RichText::new(label)
.font(theme::f_reg(13.0))
.color(p.txt2),
),
);
add(ui);
});
ui.add_space(8.0);
}
/// Summarize an OpenPGP key slot: its algorithm label, or "empty" when no key.
fn slot_summary(algo: Option<u8>, fpr: &[u8; 20]) -> &'static str {
if fpr.iter().all(|&b| b == 0) {
"empty"
} else {
algo_id_label(algo)
}
}
#[cfg(test)]
mod tests {
use super::*;
/// A job dispatched to a real worker thread runs off-thread, and its result
/// applies back to the App with the busy bookkeeping cleared.
#[test]
fn worker_round_trip_applies_result_and_clears_busy() {
let mut app = App {
worker: Some(Worker::spawn(egui::Context::default())),
..Default::default()
};
app.spawn_job("test", || Box::new(|app: &mut App| app.slot = 42));
assert!(app.busy(), "busy should be set right after dispatch");
// Wait for the worker to produce a result, then drain it.
let worker = app.worker.as_ref().unwrap();
let apply = worker
.result_rx
.recv_timeout(std::time::Duration::from_secs(5))
.expect("worker produced a result");
// Mimic drain_worker: decrement before applying.
app.busy_jobs -= 1;
apply(&mut app);
assert_eq!(app.slot, 42);
assert!(!app.busy(), "busy should clear once the result is applied");
}
/// While a job is in flight, a second dispatch is dropped (device access is
/// serialized; rapid clicks don't queue overlapping card I/O).
#[test]
fn spawn_job_ignored_while_busy() {
let mut app = App {
worker: Some(Worker::spawn(egui::Context::default())),
..Default::default()
};
// Occupy the worker with a job whose result we don't drain.
app.spawn_job("first", || Box::new(|_: &mut App| {}));
assert_eq!(app.busy_jobs, 1);
app.spawn_job("second", || Box::new(|app: &mut App| app.slot = 99));
assert_eq!(
app.busy_jobs, 1,
"second dispatch must be ignored while busy"
);
}
/// With no worker (the default), a job runs inline so headless tests and any
/// non-GUI use still apply results.
#[test]
fn inline_when_no_worker() {
let mut app = App::default();
assert!(app.worker.is_none());
app.spawn_job("inline", || Box::new(|app: &mut App| app.slot = 7));
assert_eq!(app.slot, 7);
assert!(!app.busy());
}
}