File: sql_authentication.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (6091 lines) | stat: -rw-r--r-- 221,502 bytes parent folder | download
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
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#define LOG_COMPONENT_TAG "mysql_native_password"

#include "sql/auth/sql_authentication.h"

#include <fcntl.h>
#include <mysql/components/my_service.h>
#include <sql/ssl_acceptor_context_operator.h>
#include <sql/ssl_init_callback.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <string> /* std::string */
#include <utility>
#include <vector> /* std::vector */

#include "crypt_genhash_impl.h"  // generate_user_salt
#include "include/compression.h"
#include "m_string.h"
#include "map_helpers.h"
#include "mutex_lock.h"  // Mutex_lock
#include "my_byteorder.h"
#include "my_command.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_dir.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_psi_config.h"
#include "my_sys.h"
#include "my_time.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/plugin.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/service_my_plugin_log.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/service_mysql_password_policy.h"
#include "mysql_com.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "password.h"  // my_make_scrambled_password
#include "pfs_thread_provider.h"
#include "prealloced_array.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h"
#include "sql/auth/auth_internal.h"  // optimize_plugin_compare_by_pointer
#include "sql/auth/partial_revokes.h"
#include "sql/auth/sql_auth_cache.h"  // acl_cache
#include "sql/auth/sql_security_ctx.h"
#include "sql/conn_handler/connection_handler_manager.h"  // Connection_handler_manager
#include "sql/current_thd.h"                              // current_thd
#include "sql/debug_sync.h"                               // DEBUG_SYNC
#include "sql/derror.h"                                   // ER_THD
#include "sql/hostname_cache.h"  // Host_errors, inc_host_errors
#include "sql/log.h"             // query_logger
#include "sql/mysqld.h"          // global_system_variables
#include "sql/protocol.h"
#include "sql/protocol_classic.h"
#include "sql/psi_memory_key.h"  // key_memory_MPVIO_EXT_auth_info
#include "sql/sql_class.h"       // THD
#include "sql/sql_connect.h"     // thd_init_client_charset
#include "sql/sql_const.h"
#include "sql/sql_db.h"  // mysql_change_db
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_plugin.h"  // my_plugin_lock_by_name
#include "sql/sql_time.h"    // Interval
#include "sql/strfunc.h"
#include "sql/system_variables.h"
#include "sql/tztime.h"  // Time_zone
#include "sql_common.h"  // mpvio_info
#include "sql_string.h"
#include "template_utils.h"
#include "violite.h"

struct MEM_ROOT;

#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/x509v3.h>

constexpr const std::array rsa_key_sizes{2048, 2048, 2048, 3072, 7680, 15360};

/**
   @file sql_authentication.cc

   AUTHENTICATION CODE

   including initial connect handshake, invoking appropriate plugins,
   client-server plugin negotiation, COM_CHANGE_USER, and native
   MySQL authentication plugins.
*/

/* clang-format off */
/**
  @page page_protocol_connection_phase Connection Phase

  The Connection Phase performs these tasks:
    - exchange the capabilities of client and server
    - setup SSL communication channel if requested
    - authenticate the client against the server

  It starts with the client connect()ing to the server which may send a
  ERR packet and finish the handshake or send a Initial Handshake Packet
  which the client answers with a Handshake Response Packet. At this stage
  client can request SSL connection, in which case an SSL communication
  channel is established before client sends its authentication response.

  @note In case the server sent a ERR packet as first packet it will happen
  before the client and server negotiated any capabilities.
  Therefore the ERR packet will not contain the SQL-state.

  After initial handshake, server informs client about the method to be used
  for authentication (unless it was already established during the handshake)
  and the authentication exchange continues until server either accepts
  connection by sending an @ref page_protocol_basic_ok_packet or rejects it
  with @ref page_protocol_basic_err_packet.

  @startuml
  (*) --> "Initial Handshake Packet"

  "Initial Handshake Packet" --> "Client Response"
  "Initial Handshake Packet" --> "SSL Exchange"
  "SSL Exchange" --> "Client Response"

  "Client Response" --> "Authentication method switch"
  "Client Response" --> "Authentication exchange continuation"
  "Client Response" --> [ Insufficient client capabilities] ERR

  "Authentication method switch" --> [ Client does not know requested auth method ] DISCONNECT
  "Authentication method switch" --> "Authentication exchange continuation"

  "Authentication exchange continuation" --> "Server Response"
  "Authentication exchange continuation" --> [ No more factors to authenticate] OK
  "Authentication exchange continuation" --> ERR

  "Server Response" --> "Authenticate 2nd Factor"
  "Server Response" --> "Authenticate 3rd Factor"

  "Authenticate 2nd Factor" --> "MFA Authentication Client Response"

  "Authenticate 3rd Factor" --> "MFA Authentication Client Response"

  "MFA Authentication Client Response" --> "Authentication exchange continuation"
  "MFA Authentication Client Response" --> [ Client does not know requested auth method ] DISCONNECT

  @enduml

  @section sect_protocol_connection_phase_initial_handshake Initial Handshake

  The initial handshake starts with the server sending the
  @ref page_protocol_connection_phase_packets_protocol_handshake packet.
  After this, optionally, the client can request an SSL connection to be
  established with the @ref page_protocol_connection_phase_packets_protocol_ssl_request
  packet and then the client sends the
  @ref page_protocol_connection_phase_packets_protocol_handshake_response packet.

  @subsection sect_protocol_connection_phase_initial_handshake_plain_handshake Plain Handshake

  1. Server sending @ref page_protocol_connection_phase_packets_protocol_handshake.
  2. Client replying with @ref page_protocol_connection_phase_packets_protocol_handshake_response

  @startuml
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet
  Server -> Server: Check client capabilities and authentication method to use
  @enduml

  @subsection sect_protocol_connection_phase_initial_handshake_ssl_handshake SSL Handshake

  1. Server sending @ref page_protocol_connection_phase_packets_protocol_handshake
  2. Client replying with @ref page_protocol_connection_phase_packets_protocol_ssl_request
  3. The usual SSL exchange leading to establishing SSL connection
  4. Client sends @ref page_protocol_connection_phase_packets_protocol_handshake_response

  @startuml
  Server -> Client: Initial Handshake Packet
  Client -> Server: SSL Connection Request Packet
  == SSL Exchange ==
  Client -> Server: Handshake Response Packet
  Server -> Server: Check client capabilities and authentication method to use
  @enduml

  @subsection sect_protocol_connection_phase_initial_handshake_capabilities Capability Negotiation

  To permit an old client to connect to newer servers,
  the @ref page_protocol_connection_phase_packets_protocol_handshake contains

  * the MySQL Server version
  * the server's @ref group_cs_capabilities_flags

  The client should only announce the capabilities in the
  @ref page_protocol_connection_phase_packets_protocol_handshake_response
  that it has in common with the server.

  They can agree on:
  * use of @ref CLIENT_TRANSACTIONS "status flags"
  * use of @ref CLIENT_PROTOCOL_41 "SQL states for error codes"
  * @ref sect_protocol_connection_phase_initial_handshake_auth_method "authentication methods"
  * @ref CLIENT_SSL "SSL Support"
  * @ref CLIENT_COMPRESS "Compression"

  @subsection sect_protocol_connection_phase_initial_handshake_auth_method Determining Authentication Method

  Method used for authentication is tied to the user account and stored in
  the plugin column of mysql.user table. Client informs about the user
  account it wants to log into in the
  @ref page_protocol_connection_phase_packets_protocol_handshake_response packet.
  Only then server can look up the mysql.user table and find the authentication
  method to be used.

  However, to save round-trips, server and client start authentication exchange
  already in the initial handshake using an optimistic guess of the
  authentication method to be used.

  Server uses its default authentication method @ref default_auth_plugin to
  produce initial authentication data payload and sends it to the client inside
  @ref page_protocol_connection_phase_packets_protocol_handshake, together with
  the name of the method used.

  Client can include in the
  @ref page_protocol_connection_phase_packets_protocol_handshake_response packet
  its reply to the authentication data sent by the server.

  When including authentication reply in the
  @ref page_protocol_connection_phase_packets_protocol_handshake_response,
  client is not obligated to use the same authentication method that was used
  by the server in the
  @ref page_protocol_connection_phase_packets_protocol_handshake packet.
  The name of the authentication method used by the client is stored in the
  packet. If the guessed authentication method used either by the client or
  the server in the initial handshake was not correct, server informs client
  which authentication method should be used using
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_request.
  See section @ref sect_protocol_connection_phase_auth_method_mismatch for
  more details.

  Up to MySQL 4.0 the MySQL protocol only supported the
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication.
  In MySQL 4.1 the
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  method was added and in MySQL 5.5 arbitrtary authentication methods can be implemented
  by means of authentication plugins.

  If the client or server do no support pluggable authentication
  (i.e. @ref CLIENT_PLUGIN_AUTH capability flag is not set) then
  authentication method used is inherited from client and server
  capabilities as follows:
    * The method used is
    @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
    if @ref CLIENT_PROTOCOL_41 or @ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION"
    are not set.
    * The method used is
    @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
    if both @ref CLIENT_PROTOCOL_41 and @ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION"
    are set, but @ref CLIENT_PLUGIN_AUTH is not set.

  @section sect_protocol_connection_phase_fast_path Authentication Phase Fast Path

  Assume the client wants to log in via user account U and that user account
  is defined to use authentication method `server_method`. The fast
  authentication path is used when:
  <ul>
  <li>the server used `server_method` to generate authentication data in the
    @ref page_protocol_connection_phase_packets_protocol_handshake packet.</li>
  <li>the client used a `client_authentication_method` in
    @ref page_protocol_connection_phase_packets_protocol_handshake_response
    that is compatible with the `server_method` used by the server.</li>
  </ul>

  In that case the first round of authentication has been already commenced
  during the handshake. Now, depending on the authentication method
  `server_method`, further authentication can be exchanged until the server
  either accepts or refuses the authentication.

  @subsection sect_protocol_connection_phase_fast_path_success Successful Authentication

  A successful fast authentication path looks as follows:

  1. The client connects to the server
  2. The server sends @ref page_protocol_connection_phase_packets_protocol_handshake
  3. The client responds with
     @ref page_protocol_connection_phase_packets_protocol_handshake_response
  4. Client and server possibly exchange further packets as required by the server
     authentication method for the user account the client is trying to authenticate
     against.
  5. The server responds with an @ref page_protocol_basic_ok_packet

  @startuml
  Client -> Server: Connect
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet

  == Client and server possibly exchange further authentication method packets ==

  Server -> Client: OK packet

  == Client and server enter Command Phase ==
  @enduml

  The packets the server sends in step 4 are a
  @ref page_protocol_connection_phase_packets_protocol_auth_more_data packet
  prefixed with 0x01 to distinguish them from @ref page_protocol_basic_err_packet
  and @ref page_protocol_basic_ok_packet

  @note Many authentication methods, including the mysql_native_password method
  consist of a single challenge-response exchange. In that case no extra packet are
  exchanged in step 4 and the server sends an @ref page_protocol_basic_ok_packet
  directly after receiving the
  @ref page_protocol_connection_phase_packets_protocol_handshake_response
  packet (provided the authentication was successful).

  @subsection sect_protocol_connection_phase_fast_path_fails Authentication Fails

  It goes exactly like @ref sect_protocol_connection_phase_fast_path_success,
  but if the server decides that it won't authenticate the user, it replies
  with an @ref page_protocol_basic_err_packet instead of
  @ref page_protocol_basic_ok_packet.

  1. The client connects to the server
  2. The server sends @ref page_protocol_connection_phase_packets_protocol_handshake
  3. The client responds with
  @ref page_protocol_connection_phase_packets_protocol_handshake_response
  4. Client and server possibly exchange further packets as required by the server
  authentication method for the user account the client is trying to authenticate
  against.
  5. The server responds with an @ref page_protocol_basic_err_packet

  @startuml
  Client -> Server: Connect
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet

  == Client and server possibly exchange further authentication method packets ==

  Server -> Client: ERR packet

  == Client and server close connection ==
  @enduml

  Again, the @ref page_protocol_connection_phase_packets_protocol_auth_more_data
  packets sent by the server during step 4 start with 0x01 byte and thus can
  never be confused with the @ref page_protocol_basic_err_packet.

  @section sect_protocol_connection_phase_auth_method_mismatch Authentication Method Mismatch

  Assume that client wants to log in as user U and that user account uses
  authentication method M. If:

  1. Server's default method used to generate authentication payload for
  @ref page_protocol_connection_phase_packets_protocol_handshake was different
  from M or
  2. Method used by the client to generate authentication reply in
  @ref page_protocol_connection_phase_packets_protocol_handshake_response
  was not compatible with M
  then there is an authentication method mismatch and authentication exchange
  must be restarted using the correct authentication method.

  @note
  1. The mismatch can happen even if client and server used compatible
  authentication methods in the initial handshake, but the method the server
  used was different from the method required by the user account.
  2. In the 4.1-5.7 server and client the default authentication method is
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication.
  3. In 8.0 server and client the default authentication method is
  @ref page_caching_sha2_authentication_exchanges.
  4. The client and the server can change their default authentication method via the
  `--default-auth` option.
  5. A sensibe thing to do for a client would be to see the server's default
  authentication method announced in the
  @ref page_protocol_connection_phase_packets_protocol_handshake packet and infer the
  authentication method from it instead of using the client default authentication
  method when producing
  @ref page_protocol_connection_phase_packets_protocol_handshake_response.
  But since there can be one to many server to client plugins and the clients
  generally do not know the mapping from server authentication methods to client
  authentication methods this is not implemented in the client mysql library.

  If authentication method mismatch happens, server sends to client the
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_request
  which contains the name of the client authentication method to be used and
  the first authentication payload generated by the new method. Client should
  switch to the requested authentication method and continue the exchange as
  dictated by that method.

  If the client does not know the requested method it should disconnect.

  @subsection sect_protocol_connection_phase_auth_method_mismatch_method_change Authentication Method Change

  1. The client connects to the server
  2. The server sends @ref page_protocol_connection_phase_packets_protocol_handshake
  3. The client responds with
  @ref page_protocol_connection_phase_packets_protocol_handshake_response
  4. The server sends the
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_request to tell
  the client that it needs to switch to a new authentication method.
  5. Client and server possibly exchange further packets as required by the server
  authentication method for the user account the client is trying to authenticate
  against.
  6. The server responds with an @ref page_protocol_basic_ok_packet or rejects
    with @ref page_protocol_basic_err_packet

  @startuml
  Client -> Server: Connect
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet
  Server -> Client: Authentication Switch Request Packet

  == Client and server possibly exchange further authentication method packets ==

  Server -> Client: ERR packet or OK packet
  @enduml

  @subsection sect_protocol_connection_phase_auth_method_mismatch_insuficcient_client Insufficient Client Capabilities

  Server will reject with @ref page_protocol_basic_err_packet if it discovers
  that client capabilities are not sufficient to complete authentication.
  This can happen in the following situations:

  <ul>
  <li>A client which does not support pluggable authentication
  (@ref CLIENT_PLUGIN_AUTH flag not set) connects to an account which uses
  authentication method different from
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  </li>
  <li>
  A client which does not support secure authentication (
  @ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION" flag not set) attempts
  to connect.
  </li>
  <li>Server's default authentication method used to generate authentication
  data in @ref page_protocol_connection_phase_packets_protocol_handshake is
  incompatible with
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  and client does not support pluggable authentication (@ref CLIENT_PLUGIN_AUTH
  flag is not set).
  </li>
  </ul>

  In either of these cases authentication phase will look as follows:

  1. The client connects to the server
  2. The server sends @ref page_protocol_connection_phase_packets_protocol_handshake
  3. The client response with
  @ref page_protocol_connection_phase_packets_protocol_handshake_response
  4. The server recognizes that the client does not have enough capabilities
  to handle the required authentication method, sends
  @ref page_protocol_basic_err_packet and closes the connection.

  @startuml
  Client -> Server: Connect
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet
  Server -> Client: Error Packet

  == server disconnects ==
  @enduml

  @subsection sect_protocol_connection_phase_auth_method_mismatch_unknown_auth_method New Authentication Method Not Known by Client

  Even if client supports external authentication (@ref CLIENT_PLUGIN_AUTH flag
  is set) the new authentication method indicated in
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_request might
  not be known to it. In that case the client simply disconnects.

  1. The client connects to the server
  2. The server sends @ref page_protocol_connection_phase_packets_protocol_handshake
  3. The client response with
  @ref page_protocol_connection_phase_packets_protocol_handshake_response
  4. The server sends the
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_request to tell
  the client that it needs to switch to a new authentication method.
  5. client discovers that it does not know the authentication method requested by
  the server - it disconnects.

  @startuml
  Client -> Server: Connect
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet
  Server -> Client: Authentication Switch Packet

  == client disconnects ==
  @enduml


  @subsection sect_protocol_connection_phase_auth_method_mismatch_non_client_plugin_auth Non-CLIENT_PLUGIN_AUTH Clients

  @note This can only happen on pre-8.0 servers. 8.0 has the
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  removed.

  The only situation where server will request authentication method change from
  a client which does not set @ref CLIENT_PLUGIN_AUTH flag is when the following
  conditions hold:

  1. The client uses
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  for the @ref page_protocol_connection_phase_packets_protocol_handshake_response
  packet.
  2. The client supports secure authentication
  (@ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION" is set)
  3. Server's default authentication method is
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication

  In this case server sends
  @ref page_protocol_connection_phase_packets_protocol_old_auth_switch_request.
  This packet does not contain a new authenticartion method name because it's
  implicitly assumed to be
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  and it does not contain authentication data.
  Client replies with @ref sect_protocol_connection_phase_packets_protocol_handshake_response320.
  To generate a password hash the client should re-use the random bytes sent by
  the server in the
  @ref page_protocol_connection_phase_packets_protocol_handshake.

  @startuml
  Client -> Server: Connect
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet
  Server -> Client: Old Switch Request Packet
  Client -> Server: Old Handshake Response
  Server -> Client: ERR packet or OK packet
  @enduml


  @section sect_protocol_connection_phase_com_change_user_auth Authentication After COM_CHANGE_USER Command

  During @ref page_protocol_command_phase a client can send a ::COM_CHANGE_USER
  command which will trigger authenticating into a new account via a full
  authentication handshake.

  Similarly to the @ref page_protocol_connection_phase the server may reply
  with a @ref page_protocol_basic_err_packet or
  @ref page_protocol_basic_ok_packet for the usual fast-path or with
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_request
  containing the authentication method to be used for the new account
  and the first authentication data payload to be consumed by the client.
  Further handshake continues as usual, as defined by the authentication
  method of the new account. Eventually the server will accept the new
  account with @ref page_protocol_basic_ok_packet or it will reject the change
  with an @ref page_protocol_basic_err_packet and disconnect.

  1. The client sends ::COM_CHANGE_USER packet
  2. The server responds with the
    @ref page_protocol_connection_phase_packets_protocol_auth_switch_request
    which initiates authentication handshake using the correct authentication
    method
  3. Client and server exchange further packets as required by the
    authentication method for the new account
  4. The server responds with @ref page_protocol_basic_ok_packet and returns
    to command phase or @ref page_protocol_basic_err_packet and closes
    the connection.

  @startuml
  Client -> Server: COM_CHANGE_USER
  Server -> Client: Auth Switch Request Packet
  == packets exchanged depending on the authentication method ==
  Server -> Client: ERR packet or OK packet
  @enduml

  @subsection sect_protocol_connection_phase_com_change_user_auth_non_plugin COM_CHANGE_USER and Non-CLIENT_PLUGIN_AUTH Clients

  Clients which do not support pluggable authentication can send
  ::COM_CHANGE_USER command for accounts which use
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  or
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication.
  In this case it is assumed that server has already sent the authentication
  challenge - the same which was sent when the client connected for the first
  time - and client's reply to that challenge, i.e. the hash of the new
  password, should be sent in the `auth_response` field of ::COM_CHANGE_USER.

  1. The client sends ::COM_CHANGE_USER packet with authentication response
  (hash of the password) for
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  (post 4.1 clients) or
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  (pre 4.1 clients) method.
  2. The server responds with an @ref page_protocol_basic_ok_packet and returns
  to @ref page_protocol_command_phase or with an
  @ref page_protocol_basic_err_packet and closes the connection.

  @startuml
  Client -> Server : COM_CHANGE_USER with a password hash
  Server -> Client : ERR packet or OK packet.
  @enduml

  As during normal connection, it is also possible that a post 4.1 client which
  does not support pluggable authentication connects to an account which uses
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  In that case server will send
  @ref page_protocol_connection_phase_packets_protocol_old_auth_switch_request
  and expect the client to reply with
  @ref sect_protocol_connection_phase_packets_protocol_handshake_response320

  1. The client sends ::COM_CHANGE_USER packet with response for
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  2. The server replies with
  @ref page_protocol_connection_phase_packets_protocol_old_auth_switch_request (0xFE byte)
  3. The client sends response again, this time in the form required by
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  4. The server responds with an @ref page_protocol_basic_ok_packet and returns
  to @ref page_protocol_command_phase or an @ref page_protocol_basic_err_packet
  and disconnects

  @startuml
  Client -> Server : COM_CHANGE_USER with a password hash
  Server -> Client : Old Switch Request Packet
  Client -> Server : Old Password Auth Response
  Server -> Client : ERR packet or OK packet
  @enduml

  @sa group_cs_capabilities_flags
  @sa unknown_accounts
  @subpage page_protocol_connection_phase_packets
  @subpage page_protocol_connection_phase_authentication_methods
  @subpage page_protocol_multi_factor_authentication_methods
*/


/**
  @page page_protocol_basic_expired_passwords Expired Password

  Since MySQL 5.6.7, a MySQL account can be expired.
  If a account is expired, the session is in a restricted mode which
  only permits SET PASSWORD = .. and similar SET commands.
  Other statements will fail with an error like this:
  ~~~~~~~~
  mysql> SELECT 1;
  ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
  ~~~~~~~~

  Not all clients can properly deal with that error.
  So on the protocol side exists a safeguard
  ::CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS
  @ref group_cs_capabilities_flags "capability flag" exists to prevent
  clients from entering this "sandbox" mode.
  Only clients that can handle this sandbox mode should report
  ::CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS on.
  Usually this means all interactive clients and all applications that got
  adjusted to handle the relevant SQL error.

  If a client is not setting that capability and it tries to login with an
  account that has an expired password, the server will return an
  @ref page_protocol_basic_err_packet for the
  @ref page_protocol_connection_phase or the ::COM_CHANGE_USER request.

  The idea is to block any activity until the password is reset.

  @sa ::MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, mysql_options,
  ACL_USER::password_expired, ACL_USER::password_lifetime,
  acl_authenticate
*/


/**
   @page page_protocol_connection_phase_authentication_methods Authentication Methods

   To authenticate a user against the server the client server protocol employs one of
   several authentication methods.

   As of MySQL 5.5 the authentication method to be used to authenticate
   connections to a particular MySQL account is indicated in the mysql.user table.
   For earlier servers it's always mysql native authentication or
   old password authentication depending on
   the @ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION" flag.

   Client and server negotiate what types of authentication they support as part of the
   @ref page_protocol_connection_phase and
   @ref sect_protocol_connection_phase_initial_handshake_auth_method.

   Each authentication method consists of
     * a client plugin name
     * a server plugin name
     * a specific exchange

   The exchanged input and output data may either be sent as part of the
   @ref page_protocol_connection_phase_packets_protocol_handshake and the
   @ref page_protocol_connection_phase_packets_protocol_handshake_response
   or as a part of the
   @ref page_protocol_connection_phase_packets_protocol_auth_switch_request
   and following packets. The structure is usually the same.

   @section page_protocol_connection_phase_authentication_methods_limitations Limitations

   While the overall exchange of data is free-form there are some limitations
   in the initial handshake of the amount of data that can be exchanged without
   causing an extra round trip:

   <ul>
   <li>
   The `auth_plugin_data` field in
   @ref page_protocol_connection_phase_packets_protocol_handshake packet can
   only carry 255 bytes max (see @ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION").
   </li><li>
   The `auth_reponse_data` field in
   @ref page_protocol_connection_phase_packets_protocol_handshake_response
   packet can only carry 255 bytes max too if
   @ref CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA is not set.
   </li><li>
   The client-side plugin may not receive its initial data in the initial handshake
   </li>
   </ul>

   @section page_protocol_connection_phase_authentication_methods_old_password_authentication Old Password Authentication

   Authentication::Old:

   <ul>
   <li>
   The server name is *mysql_old_password*
   </li>
   <li>
   The client name is *mysql_old_password*
   </li>
   <li>
   Client side requires an 8-byte random challenge from server
   </li>
   <li>
   Client side sends a 8 byte response packet based on a proprietary algorithm.
   </li>
   </ul>

   @note If the server announces
   @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
   in the
   @ref page_protocol_connection_phase_packets_protocol_handshake packet
   the client may use the first 8 bytes of its 20-byte auth_plugin_data as input.

   @startuml
   Client->Server: 8 byte random data
   Server->client: 8 byte scrambled password
   @enduml

   @warning The hashing algorithm used for this auth method is *broken* as
   shown in CVE-2000-0981.

   @subpage page_protocol_connection_phase_authentication_methods_native_password_authentication
   @subpage page_caching_sha2_authentication_exchanges
   @subpage page_protocol_connection_phase_authentication_methods_clear_text_password
   @subpage page_protocol_connection_phase_authentication_methods_authentication_windows
   @subpage page_fido_authentication_exchanges
*/

/**
  @page page_protocol_connection_phase_packets_protocol_handshake Protocol::Handshake

  Initial Handshake %Packet

  When the client connects to the server the server sends a handshake
  packet to the client. Depending on the server version and configuration
  options different variants of the initial packet are sent.

  To permit the server to add support for newer protocols, the first byte
  defines the protocol version.

  Since 3.21.0 the @ref page_protocol_connection_phase_packets_protocol_handshake_v10
  is sent.

  * @subpage page_protocol_connection_phase_packets_protocol_handshake_v9
  * @subpage page_protocol_connection_phase_packets_protocol_handshake_v10
*/

/**
  @page page_protocol_connection_phase_packets Connection Phase Packets

  @subpage page_protocol_connection_phase_packets_protocol_handshake
  @subpage page_protocol_connection_phase_packets_protocol_ssl_request
  @subpage page_protocol_connection_phase_packets_protocol_handshake_response
  @subpage page_protocol_connection_phase_packets_protocol_auth_switch_request
  @subpage page_protocol_connection_phase_packets_protocol_old_auth_switch_request
  @subpage page_protocol_connection_phase_packets_protocol_auth_switch_response
  @subpage page_protocol_connection_phase_packets_protocol_auth_more_data
  @subpage page_protocol_connection_phase_packets_protocol_auth_next_factor_request
*/

/**
  @page page_fido_authentication_exchanges authentication_fido information

  @section sect_fido_definition Definition
  <ul>
  <li>
  The server side plugin name is *authentication_fido*
  </li>
  <li>
  The client side plugin name is *authentication_fido_client*
  </li>
  <li>
  Account - user account (user-host combination)
  </li>
  <li>
  authentication_string - Transformation of Credential ID stored in mysql.user table
  </li>
  <li>
  relying party ID - Unique name assigned to server by authentication_fido plugin
  </li>
  <li>
  FIDO authenticator - A hardware token device
  </li>
  <li>
  Salt - 32 byte long random data
  </li>
  <li>
  Registration mode - Refers to state of connection where only ALTER USER is allowed
  to do registration steps.
  </li>
  </ul>

  @section sect_fido_info How authentication_fido works?

  Plugin authentication_fido works in two phases.
  <ul>
   <li>
    Registration of hardware token device
   </li>
   <li>
    Authentication process
   </li>
  </ul>

  Registration process:
  This is a 2 step process for a given user account.
  <ul>
   <li>
    Initiate registration step.
   </li>
   <li>
    Finish registration step.
   </li>
  </ul>

  Initiate registration:
  --fido-register-factor mysql client option initiates registration step.

  <ol>
   <li>
    Client executes ALTER USER user() nth FACTOR INITIATE REGISTRATION;
   </li>
   <li>
   Server sends a challenge comprising of 32 bytes random salt, user id, relying party ID
   Format of challenge is:
   |length encoded 32 bytes random salt|length encoded user id (user name + host name)|length encoded relying party ID|
   </li>
   <li>
   Client receives challenge and passes to authentication_fido_client plugin
   with option "registration_challenge" using mysql_plugin_options()
   </li>
   <li>
    FIDO authenticator prompts physical human user to perform gesture action.
    This message can be accessed via callback. Register a callback with option
    "fido_messages_callback" using mysql_plugin_options()
   </li>
   <li>
    Once physical human user gesture action (touching the token) is performed,
    FIDO authenticator generates a public/private key pair, a credential ID(
    X.509 certificate, signature) and authenticator data.
   </li>
   <li>
   Client extracts credential ID(aka challenge response) from authentication_fido_client
   plugin with option "registration_response" using mysql_plugin_get_option()
   Format of challenge response is:
   |length encoded authenticator data|length encoded credential ID|
   </li>
  </ol>

  Finish registration:
  <ol>
   <li>
    Client executes ALTER USER user() nth FACTOR FINISH REGISTRATION SET CHALLENGE_RESPONSE AS '?';
    parameter is binded to challenge response received during initiate registration step.
   </li>
   <li>
    authentication_fido plugin verifies the challenge response and responds with an
    @ref page_protocol_basic_ok_packet or rejects with @ref page_protocol_basic_err_packet
   </li>
  </ol>
       @startuml
         title Registration

         participant server as "MySQL server"
         participant client as "Client"
         participant authenticator as "FIDO authenticator"

         == Initiate registration ==

         client -> server : connect
         server -> client : OK packet. Connection is in registration mode where only ALTER USER command is allowed
         client -> server : ALTER USER USER() nth FACTOR INITIATE REGISTRATION
         server -> client : random challenge (32 byte random salt, user id, relying party ID)
         client -> authenticator : random challenge
         authenticator -> client : credential ID (X.509 certificate, signature), authenticator data

         == Finish registration ==

         client -> server : ALTER USER USER() nth FACTOR FINISH REGISTRATION SET CHALLENGE_RESPONSE = 'credential ID, authenticator data'
         server -> client : Ok packet upon successful verification of credential ID
       @enduml

  Authentication process:
  Once initial authentication methods defined for user account are successful,
  server initiates fido authentication process. This includes following steps:
   <ol>
    <li>
     Server sends a 32 byte random salt, relying party ID, credential ID to client.
    </li>
    <li>
     Client receives it and sends to FIDO authenticator.
    </li>
    <li>
     FIDO authenticator prompts physical human user to perform gesture action.
    </li>
    <li>
     FIDO authenticator extracts the private key based on relying party ID and
     signs the challenge.
    </li>
    <li>
     Client sends signed challenge to server.
    </li>
    <li>
     Server side fido authentication plugin verifies the signature with the
     public key and responds with an @ref page_protocol_basic_ok_packet or with
     @ref page_protocol_basic_err_packet
    </li>
   </ol>
       @startuml
         title Authentication

         participant server as "MySQL server"
         participant client as "Client"
         participant authenticator as "FIDO authenticator"

         == Authentication ==

         client -> server : connect
         server -> client : OK packet
         server -> client : send client side fido authentication plugin name in OK packet
         server -> client : sends 32 byte random salt, relying party ID, credential ID
         client -> authenticator : sends 32 byte random salt, relying party ID, credential ID
         authenticator -> client : signed challenge
         client -> server : signed challenge
         server -> client : verify signed challenge and send OK or ERR packet
       @enduml
*/
/* clang-format on */

const uint MAX_UNKNOWN_ACCOUNTS = 1000;
/**
  Hash to map unknown accounts to an authentication plugin.

  If unknown accounts always map to default authentication plugin,
  server's reply to switch authentication plugin would indicate that
  user in question is indeed a valid user.

  To counter this, one of the built-in authentication plugins is chosen
  at random. Thus, a request to switch authentication plugin is not and
  indicator of a valid user account.

  For same unknown account, if different plugin is chosen every time,
  that again is an indicator. To resolve this, a hashmap is  used to
  store information about unknown account => authentication plugin.
  This way, if same unknown account appears again, same authentication
  plugin is chosen again.

  However, size of such a hash has to be kept under control. Hence,
  once MAX_UNKNOWN_ACCOUNTS lim
*/
Map_with_rw_lock<Auth_id, uint> *unknown_accounts = nullptr;

inline const char *client_plugin_name(plugin_ref ref) {
  return ((st_mysql_auth *)(plugin_decl(ref)->info))->client_auth_plugin;
}

LEX_CSTRING validate_password_plugin_name = {
    STRING_WITH_LEN("validate_password")};

LEX_CSTRING default_auth_plugin_name;

const LEX_CSTRING Cached_authentication_plugins::cached_plugins_names[(
    uint)PLUGIN_LAST] = {{STRING_WITH_LEN("caching_sha2_password")},
                         {STRING_WITH_LEN("mysql_native_password")},
                         {STRING_WITH_LEN("sha256_password")}};

/**
  Use known pointers for cached plugins to improve comparison time

  @param  [in] plugin Name of the plugin
*/
void Cached_authentication_plugins::optimize_plugin_compare_by_pointer(
    LEX_CSTRING *plugin) {
  DBUG_TRACE;
  for (uint i = 0; i < (uint)PLUGIN_LAST; ++i) {
    if (my_strcasecmp(system_charset_info, cached_plugins_names[i].str,
                      plugin->str) == 0) {
      plugin->str = cached_plugins_names[i].str;
      plugin->length = cached_plugins_names[i].length;
      return;
    }
  }
}

/**
  Cached_authentication_plugins constructor

  Cache plugin_ref for each plugin in cached_plugins_names list
*/
Cached_authentication_plugins::Cached_authentication_plugins() {
  DBUG_TRACE;
  m_valid = true;
  for (uint i = 0; i < (uint)PLUGIN_LAST; ++i) {
    if (cached_plugins_names[i].str[0]) {
      cached_plugins[i] = my_plugin_lock_by_name(
          nullptr, cached_plugins_names[i], MYSQL_AUTHENTICATION_PLUGIN);
      if (!cached_plugins[i]) m_valid = false;
    } else
      cached_plugins[i] = nullptr;
  }
}

/**
  Cached_authentication_plugins destructor

  Releases all plugin_refs
*/
Cached_authentication_plugins::~Cached_authentication_plugins() {
  DBUG_TRACE;
  for (uint i = 0; i < (uint)PLUGIN_LAST; ++i) {
    if (cached_plugins[i]) plugin_unlock(nullptr, cached_plugins[i]);
  }
}

/**
  Get plugin_ref if plugin is cached

  @param [in] plugin Name of the plugin

  @returns cached plugin_ref if found, 0 otherwise.
*/
plugin_ref Cached_authentication_plugins::get_cached_plugin_ref(
    const LEX_CSTRING *plugin) {
  plugin_ref cached_plugin = nullptr;
  LEX_CSTRING plugin_cstring;
  DBUG_TRACE;
  if (!plugin || !plugin->str || !this->is_valid()) return cached_plugin;

  plugin_cstring.str = plugin->str;
  plugin_cstring.length = plugin->length;
  this->optimize_plugin_compare_by_pointer(&plugin_cstring);

  for (uint i = 0; i < (uint)PLUGIN_LAST; ++i) {
    if (plugin_cstring.str == cached_plugins_names[i].str) {
      cached_plugin = cached_plugins[i];
      return cached_plugin;
    }
  }
  return cached_plugin;
}

/*
  Fetch the SSL security level
*/
int security_level(void) {
  int current_sec_level = 2;
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  /*
    create a temporary SSL_CTX, we're going to use it to fetch
    the current OpenSSL security level. So that we can generate
    keys accordingly.
  */
  SSL_CTX *temp_ssl_ctx = SSL_CTX_new(TLS_server_method());

  /* get the current security level */
  current_sec_level = SSL_CTX_get_security_level(temp_ssl_ctx);

  assert(current_sec_level <= 5);

  /* current range for security level is [1,5] */
  if (current_sec_level > 5)
    current_sec_level = 5;
  else if (current_sec_level <= 1)
    current_sec_level = 2;

  /* get rid of temp_ssl_ctx, we're done with it */
  SSL_CTX_free(temp_ssl_ctx);
#endif
  DBUG_EXECUTE_IF("crypto_policy_3", current_sec_level = 3;);
  return current_sec_level;
}

Cached_authentication_plugins *g_cached_authentication_plugins = nullptr;

bool disconnect_on_expired_password = true;

extern bool initialized;

/** Size of the header fields of an authentication packet. */
#define AUTH_PACKET_HEADER_SIZE_PROTO_41 32
#define AUTH_PACKET_HEADER_SIZE_PROTO_40 5

#define MAX_CIPHER_LENGTH 1024
#define SHA256_PASSWORD_MAX_PASSWORD_LENGTH MAX_PLAINTEXT_LENGTH

#define DEFAULT_SSL_CLIENT_CERT "client-cert.pem"
#define DEFAULT_SSL_CLIENT_KEY "client-key.pem"

#define MAX_CN_NAME_LENGTH 64

bool opt_auto_generate_certs = true;

bool auth_rsa_auto_generate_rsa_keys = true;

static bool do_auto_rsa_keys_generation();

char *auth_rsa_private_key_path;
char *auth_rsa_public_key_path;
Rsa_authentication_keys *g_sha256_rsa_keys = nullptr;

bool Thd_charset_adapter::init_client_charset(uint cs_number) {
  if (thd_init_client_charset(thd, cs_number)) return true;
  thd->update_charset();
  return thd->is_error();
}

const CHARSET_INFO *Thd_charset_adapter::charset() { return thd->charset(); }

/**
  @brief Set key file path

  @param [in] key            Points to either m_private_key_path or
                             m_public_key_path.
  @param [out] key_file_path Stores value of actual key file path.

*/
void Rsa_authentication_keys::get_key_file_path(char *key,
                                                String *key_file_path) {
  /*
     If a fully qualified path is entered use that, else assume the keys are
     stored in the data directory.
   */
  if (strchr(key, FN_LIBCHAR) != nullptr
#ifdef _WIN32
      || strchr(key, FN_LIBCHAR2) != NULL
#endif
  )
    key_file_path->set_quick(key, strlen(key), system_charset_info);
  else {
    key_file_path->append(mysql_real_data_home, strlen(mysql_real_data_home));
    if ((*key_file_path)[key_file_path->length()] != FN_LIBCHAR)
      key_file_path->append(FN_LIBCHAR);
    key_file_path->append(key);
  }
}

/**
  @brief Read a key file and store its value in RSA structure

  @param [out] key_ptr         Address of pointer to RSA. This is set to
                               point to a non null value if key is correctly
                               read.
  @param [in] is_priv_key      Whether we are reading private key or public
                               key.
  @param [out] key_text_buffer To store key file content of public key.

  @return Error status
    @retval false              Success : Either both keys are read or none
                               are.
    @retval true               Failure : An appropriate error is raised.
*/
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
bool Rsa_authentication_keys::read_key_file(EVP_PKEY **key_ptr,
                                            bool is_priv_key,
                                            char **key_text_buffer) {
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
bool Rsa_authentication_keys::read_key_file(RSA **key_ptr, bool is_priv_key,
                                            char **key_text_buffer) {
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  String key_file_path;
  char *key;
  const char *key_type;
  FILE *key_file = nullptr;

  key = is_priv_key ? *m_private_key_path : *m_public_key_path;
  key_type = is_priv_key ? "private" : "public";
  *key_ptr = nullptr;

  get_key_file_path(key, &key_file_path);

  /*
     Check for existence of private key/public key file.
  */
  if ((key_file = fopen(key_file_path.c_ptr(), "rb")) == nullptr) {
    LogErr(WARNING_LEVEL, ER_AUTH_RSA_CANT_FIND, key_type,
           key_file_path.c_ptr());
  } else {
    *key_ptr = is_priv_key
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
                   ? PEM_read_PrivateKey(key_file, nullptr, nullptr, nullptr)
                   : PEM_read_PUBKEY(key_file, nullptr, nullptr, nullptr);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
                   ? PEM_read_RSAPrivateKey(key_file, nullptr, nullptr, nullptr)
                   : PEM_read_RSA_PUBKEY(key_file, nullptr, nullptr, nullptr);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

    if (!(*key_ptr)) {
      char error_buf[MYSQL_ERRMSG_SIZE];
      ERR_error_string_n(ERR_get_error(), error_buf, MYSQL_ERRMSG_SIZE);
      LogErr(ERROR_LEVEL, ER_AUTH_RSA_CANT_PARSE, key_type,
             key_file_path.c_ptr(), error_buf);

      /*
        Call ERR_clear_error() just in case there are more than 1 entry in the
        OpenSSL thread's error queue.
      */
      ERR_clear_error();
      fclose(key_file);
      return true;
    }

    /* For public key, read key file content into a char buffer. */
    bool read_error = false;
    if (!is_priv_key) {
      int filesize;
      fseek(key_file, 0, SEEK_END);
      filesize = ftell(key_file);
      fseek(key_file, 0, SEEK_SET);
      *key_text_buffer = new char[filesize + 1];
      int items_read = fread(*key_text_buffer, filesize, 1, key_file);
      read_error = items_read != 1;
      if (read_error) {
        char errbuf[MYSQL_ERRMSG_SIZE];
        LogErr(ERROR_LEVEL, ER_AUTH_RSA_CANT_READ,
               my_strerror(errbuf, MYSQL_ERRMSG_SIZE, my_errno()));
      }
      (*key_text_buffer)[filesize] = '\0';
    }
    fclose(key_file);
    return read_error;
  }
  return false;
}

void Rsa_authentication_keys::free_memory() {
  if (m_private_key)
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
    EVP_PKEY_free(m_private_key);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    RSA_free(m_private_key);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

  if (m_public_key) {
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
    EVP_PKEY_free(m_public_key);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    RSA_free(m_public_key);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    m_cipher_len = 0;
  }

  if (m_pem_public_key) delete[] m_pem_public_key;
}

void *Rsa_authentication_keys::allocate_pem_buffer(size_t buffer_len) {
  m_pem_public_key = new char[buffer_len];
  return m_pem_public_key;
}

int Rsa_authentication_keys::get_cipher_length() {
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  return (m_cipher_len = EVP_PKEY_get_size(m_public_key));
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  return (m_cipher_len = RSA_size(m_public_key));
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
}

/**
  @brief Read RSA private key and public key from file and store them
         in m_private_key and m_public_key. Also, read public key in
         text format and store it in m_pem_public_key.

  @return Error status
    @retval false        Success : Either both keys are read or none are.
    @retval true         Failure : An appropriate error is raised.
*/
bool Rsa_authentication_keys::read_rsa_keys() {
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  EVP_PKEY *rsa_private_key_ptr = nullptr;
  EVP_PKEY *rsa_public_key_ptr = nullptr;
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  RSA *rsa_private_key_ptr = nullptr;
  RSA *rsa_public_key_ptr = nullptr;
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  char *pub_key_buff = nullptr;

  if ((strlen(*m_private_key_path) == 0) && (strlen(*m_public_key_path) == 0)) {
    LogErr(INFORMATION_LEVEL, ER_AUTH_RSA_FILES_NOT_FOUND);
    return false;
  }

  /*
    Read private key in RSA format.
  */
  if (read_key_file(&rsa_private_key_ptr, true, nullptr)) return true;

  /*
    Read public key in RSA format.
  */
  if (read_key_file(&rsa_public_key_ptr, false, &pub_key_buff)) {
    if (rsa_private_key_ptr)
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
      EVP_PKEY_free(rsa_private_key_ptr);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
      RSA_free(rsa_private_key_ptr);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    return true;
  }

  /*
     If both key files are read successfully then assign values to following
     members of the class
     1. m_pem_public_key
     2. m_private_key
     3. m_public_key

     Else clean up.
   */
  if (rsa_private_key_ptr && rsa_public_key_ptr) {
    size_t buff_len = strlen(pub_key_buff);
    char *pem_file_buffer = (char *)allocate_pem_buffer(buff_len + 1);
    strncpy(pem_file_buffer, pub_key_buff, buff_len);
    pem_file_buffer[buff_len] = '\0';

    m_private_key = rsa_private_key_ptr;
    m_public_key = rsa_public_key_ptr;

    delete[] pub_key_buff;
  } else {
    if (rsa_private_key_ptr)
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
      EVP_PKEY_free(rsa_private_key_ptr);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
      RSA_free(rsa_private_key_ptr);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    if (rsa_public_key_ptr) {
      delete[] pub_key_buff;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
      EVP_PKEY_free(rsa_public_key_ptr);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
      RSA_free(rsa_public_key_ptr);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    }
  }
  return false;
}

void optimize_plugin_compare_by_pointer(LEX_CSTRING *plugin_name) {
  Cached_authentication_plugins::optimize_plugin_compare_by_pointer(
      plugin_name);
}

/**
 Initialize default authentication plugin based on command line options or
 configuration file settings.

 @param plugin_name Name of the plugin
 @param plugin_name_length Length of the string
*/

int set_default_auth_plugin(char *plugin_name, size_t plugin_name_length) {
  default_auth_plugin_name.str = plugin_name;
  default_auth_plugin_name.length = plugin_name_length;

  optimize_plugin_compare_by_pointer(&default_auth_plugin_name);

  if (!Cached_authentication_plugins::compare_plugin(
          PLUGIN_SHA256_PASSWORD, default_auth_plugin_name) &&
      !Cached_authentication_plugins::compare_plugin(
          PLUGIN_MYSQL_NATIVE_PASSWORD, default_auth_plugin_name) &&
      !Cached_authentication_plugins::compare_plugin(
          PLUGIN_CACHING_SHA2_PASSWORD, default_auth_plugin_name))
    return 1;

  if (!Cached_authentication_plugins::compare_plugin(
          PLUGIN_CACHING_SHA2_PASSWORD, default_auth_plugin_name))
    LogErr(WARNING_LEVEL, ER_DEPRECATE_MSG_WITH_REPLACEMENT,
           "default_authentication_plugin", "authentication_policy");
  return 0;
}
/**
  Return the default authentication plugin name

  @retval
    A string containing the default authentication plugin name
*/
std::string get_default_autnetication_plugin_name() {
  if (default_auth_plugin_name.length > 0)
    return default_auth_plugin_name.str;
  else
    return "";
}

bool auth_plugin_is_built_in(const char *plugin_name) {
  LEX_CSTRING plugin = {STRING_WITH_LEN(plugin_name)};
  return g_cached_authentication_plugins->auth_plugin_is_built_in(&plugin);
}

/**
  Only the plugins that are known to use the mysql.user table
  to store their passwords support password expiration atm.
  TODO: create a service and extend the plugin API to support
  password expiration for external plugins.

  @retval      false  expiration not supported
  @retval      true   expiration supported
*/
bool auth_plugin_supports_expiration(const char *plugin_name) {
  if (!plugin_name || !*plugin_name) return false;

  return auth_plugin_is_built_in(plugin_name);
}

/**
  a helper function to report an access denied error in all the proper places
*/
static void login_failed_error(THD *thd, MPVIO_EXT *mpvio, int passwd_used) {
  if (thd->is_error()) {
    LogEvent()
        .prio(INFORMATION_LEVEL)
        .errcode(ER_ABORTING_USER_CONNECTION)
        .subsys(LOG_SUBSYSTEM_TAG)
        .verbatim(thd->get_stmt_da()->message_text());
  }

  else if (passwd_used == 2) {
    my_error(ER_ACCESS_DENIED_NO_PASSWORD_ERROR, MYF(0),
             mpvio->auth_info.user_name, mpvio->auth_info.host_or_ip);
    query_logger.general_log_print(
        thd, COM_CONNECT, ER_DEFAULT(ER_ACCESS_DENIED_NO_PASSWORD_ERROR),
        mpvio->auth_info.user_name, mpvio->auth_info.host_or_ip);
    /*
      Log access denied messages to the error log when log_error_verbosity = 3
      so that the overhead of the general query log is not required to track
      failed connections.
    */
    LogErr(INFORMATION_LEVEL, ER_ACCESS_DENIED_ERROR_WITHOUT_PASSWORD,
           mpvio->auth_info.user_name, mpvio->auth_info.host_or_ip);
  } else {
    my_error(ER_ACCESS_DENIED_ERROR, MYF(0), mpvio->auth_info.user_name,
             mpvio->auth_info.host_or_ip,
             passwd_used ? ER_THD(thd, ER_YES) : ER_THD(thd, ER_NO));
    query_logger.general_log_print(
        thd, COM_CONNECT, ER_DEFAULT(ER_ACCESS_DENIED_ERROR),
        mpvio->auth_info.user_name, mpvio->auth_info.host_or_ip,
        passwd_used ? ER_DEFAULT(ER_YES) : ER_DEFAULT(ER_NO));
    /*
      Log access denied messages to the error log when log_error_verbosity = 3
      so that the overhead of the general query log is not required to track
      failed connections.
    */
    LogErr(INFORMATION_LEVEL, ER_ACCESS_DENIED_ERROR_WITH_PASSWORD,
           mpvio->auth_info.user_name, mpvio->auth_info.host_or_ip,
           passwd_used ? ER_DEFAULT(ER_YES) : ER_DEFAULT(ER_NO));
  }
}

/* clang-format off */
/**
  @page page_protocol_connection_phase_packets_protocol_handshake_v9 Protocol::HandshakeV9:

  Initial handshake packet for protocol version 9.

  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>protocol version</td>
    <td>Always 9</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string&lt;NUL&gt;"</td>
      <td>server version</td>
      <td>human readable status information</td></tr>
  <tr><td>@ref a_protocol_type_int4 "int&lt;4&gt;"</td>
    <td>thread id</td>
    <td>a.k.a. connection id</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string&lt;NUL&gt;"</td>
    <td>scramble</td>
    <td>Authentication plugin data for @ref page_protocol_connection_phase_authentication_methods_old_password_authentication</td></tr>
  </table>

  @returns @ref sect_protocol_connection_phase_packets_protocol_handshake_response320
*/



/**
  @page page_protocol_connection_phase_packets_protocol_handshake_v10 Protocol::HandshakeV10

  Initial handshake packet for protocol version 10.

  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>protocol version</td>
    <td>Always 10</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string&lt;NUL&gt;"</td>
      <td>server version</td>
      <td>human readable status information</td></tr>
  <tr><td>@ref a_protocol_type_int4 "int&lt;4&gt;"</td>
    <td>thread id</td>
    <td>a.k.a. connection id</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_fix "string[8]"</td>
    <td>auth-plugin-data-part-1</td>
    <td>first 8 bytes of the plugin provided data (scramble)</td></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>filler</td>
    <td>0x00 byte, terminating the first part of a scramble</td></tr>
  <tr><td>@ref a_protocol_type_int2 "int&lt;2&gt;"</td>
    <td>capability_flags_1</td>
    <td>The lower 2 bytes of the \ref group_cs_capabilities_flags</td></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>character_set</td>
    <td>default server \ref a_protocol_character_set, only the lower 8-bits</td></tr>
  <tr><td>@ref a_protocol_type_int2 "int&lt;2&gt;"</td>
    <td>status_flags</td>
    <td>\ref SERVER_STATUS_flags_enum</td></tr>
  <tr><td>@ref a_protocol_type_int2 "int&lt;2&gt;"</td>
    <td>capability_flags_2</td>
    <td>The upper 2 bytes of the \ref group_cs_capabilities_flags</td></tr>
  <tr><td colspan="3">if capabilities @& ::CLIENT_PLUGIN_AUTH {</td></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>auth_plugin_data_len</td>
    <td>length of the combined auth_plugin_data (scramble), if auth_plugin_data_len is &gt; 0</td></tr>
  <tr><td colspan="3">} else {</td></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>00</td>
    <td>constant 0x00</td></tr>
  <tr><td colspan="3">}</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_fix "string[10]"</td>
    <td>reserved</td>
    <td>reserved. All 0s.</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_le "$length"</td>
    <td>auth-plugin-data-part-2</td>
    <td>Rest of the plugin provided data (scramble), $len=MAX(13, length of auth-plugin-data - 8)</td></tr>
  <tr><td colspan="3">if capabilities @& ::CLIENT_PLUGIN_AUTH {</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "NULL"</td>
    <td>auth_plugin_name</td>
    <td>name of the auth_method that the auth_plugin_data belongs to</td></tr>
  <tr><td colspan="3">}</td></tr>
  </table>

  If the client supports SSL (\ref group_cs_capabilities_flags @& ::CLIENT_SSL
  is on and the \ref mysql_ssl_mode of the client is not ::SSL_MODE_DISABLED)
  a short package called
  @ref page_protocol_connection_phase_packets_protocol_ssl_request is sent,
  causing the server to establish an SSL layer and wait for the next package
  from the client.

  Client then returns
  @ref page_protocol_connection_phase_packets_protocol_handshake_response

  At any time, at any error, the client will just disconnect.

  @sa send_server_handshake_packet mysql_real_connect
*/
/* clang-format on */

/**
  Sends a server @ref
  page_protocol_connection_phase_packets_protocol_handshake_v10

  @retval 0 ok
  @retval 1 error
*/
static bool send_server_handshake_packet(MPVIO_EXT *mpvio, const char *data,
                                         uint data_len) {
  assert(mpvio->status == MPVIO_EXT::FAILURE);
  assert(data_len <= 255);
  Protocol_classic *protocol = mpvio->protocol;

  char *buff = (char *)my_alloca(1 + SERVER_VERSION_LENGTH + data_len + 64);
  char scramble_buf[SCRAMBLE_LENGTH];
  char *end = buff;

  DBUG_TRACE;
  *end++ = protocol_version;

  protocol->set_client_capabilities(CLIENT_BASIC_FLAGS);

  if (opt_using_transactions)
    protocol->add_client_capability(CLIENT_TRANSACTIONS);

  protocol->add_client_capability(CAN_CLIENT_COMPRESS);

  bool have_ssl = false;
  if (current_thd->is_admin_connection() && g_admin_ssl_configured == true) {
    Lock_and_access_ssl_acceptor_context context(mysql_admin);
    have_ssl = context.have_ssl();
  } else {
    Lock_and_access_ssl_acceptor_context context(mysql_main);
    have_ssl = context.have_ssl();
  }

  if (have_ssl) {
    protocol->add_client_capability(CLIENT_SSL);
    protocol->add_client_capability(CLIENT_SSL_VERIFY_SERVER_CERT);
  }

  if (opt_protocol_compression_algorithms &&
      opt_protocol_compression_algorithms[0] != 0) {
    /* turn off the capability flag as the global variable might have changed */
    protocol->remove_client_capability(CLIENT_COMPRESS);
    protocol->remove_client_capability(CLIENT_ZSTD_COMPRESSION_ALGORITHM);
    std::vector<std::string> list;
    parse_compression_algorithms_list(opt_protocol_compression_algorithms,
                                      list);
    auto it = list.begin();
    NET_SERVER *ext = static_cast<NET_SERVER *>(protocol->get_net()->extension);
    struct compression_attributes *compression = &(ext->compression);
    compression->compression_optional = false;
    while (it != list.end()) {
      std::string value = *it;
      switch (get_compression_algorithm(value)) {
        case enum_compression_algorithm::MYSQL_ZSTD:
          protocol->add_client_capability(CLIENT_ZSTD_COMPRESSION_ALGORITHM);
          break;
        case enum_compression_algorithm::MYSQL_ZLIB:
          protocol->add_client_capability(CLIENT_COMPRESS);
          break;
        case enum_compression_algorithm::MYSQL_UNCOMPRESSED:
          compression->compression_optional = true;
          break;
        case enum_compression_algorithm::MYSQL_INVALID:
          assert(false);
          break;
      }
      it++;
    }
  }

  if (data_len) {
    mpvio->cached_server_packet.pkt =
        (char *)memdup_root(mpvio->mem_root, data, data_len);
    mpvio->cached_server_packet.pkt_len = data_len;
  }

  if (data_len < SCRAMBLE_LENGTH) {
    if (data_len) {
      /*
        the first packet *must* have at least 20 bytes of a scramble.
        if a plugin provided less, we pad it to 20 with zeros
      */
      memcpy(scramble_buf, data, data_len);
      memset(scramble_buf + data_len, 0, SCRAMBLE_LENGTH - data_len);
      data = scramble_buf;
    } else {
      /*
        if the default plugin does not provide the data for the scramble at
        all, we generate a scramble internally anyway, just in case the
        user account (that will be known only later) uses a
        mysql_native_password plugin (which needs a scramble). If we don't send
        a scramble now - wasting 20 bytes in the packet - mysql_native_password
        plugin will have to send it in a separate packet, adding one more round
        trip.
      */
      generate_user_salt(mpvio->scramble, SCRAMBLE_LENGTH + 1);
      data = mpvio->scramble;
    }
    data_len = SCRAMBLE_LENGTH;
  }

  end = my_stpnmov(end, server_version, SERVER_VERSION_LENGTH) + 1;

  assert(sizeof(my_thread_id) == 4);
  int4store((uchar *)end, mpvio->thread_id);
  end += 4;

  /*
    Old clients does not understand long scrambles, but can ignore packet
    tail: that's why first part of the scramble is placed here, and second
    part at the end of packet.
  */
  end = (char *)memcpy(end, data, AUTH_PLUGIN_DATA_PART_1_LENGTH);
  end += AUTH_PLUGIN_DATA_PART_1_LENGTH;
  *end++ = 0;

  int2store(end, static_cast<uint16>(protocol->get_client_capabilities()));
  /* write server characteristics: up to 16 bytes allowed */
  end[2] = (char)default_charset_info->number;
  int2store(end + 3, mpvio->server_status[0]);
  int2store(end + 5, protocol->get_client_capabilities() >> 16);
  end[7] = data_len;
  DBUG_EXECUTE_IF("poison_srv_handshake_scramble_len", end[7] = -100;);
  DBUG_EXECUTE_IF("increase_srv_handshake_scramble_len", end[7] = 50;);
  memset(end + 8, 0, 10);
  end += 18;
  /* write scramble tail */
  end = (char *)memcpy(end, data + AUTH_PLUGIN_DATA_PART_1_LENGTH,
                       data_len - AUTH_PLUGIN_DATA_PART_1_LENGTH);
  end += data_len - AUTH_PLUGIN_DATA_PART_1_LENGTH;
  end = strmake(end, client_plugin_name(mpvio->plugin),
                strlen(client_plugin_name(mpvio->plugin)));

  int res = protocol->write((uchar *)buff, (size_t)(end - buff + 1)) ||
            protocol->flush();
  return res;
}

/* clang-format off */
/**
  @page page_protocol_connection_phase_packets_protocol_auth_switch_request Protocol::AuthSwitchRequest:

  Authentication method Switch Request Packet

  If both server and the client support @ref CLIENT_PLUGIN_AUTH capability,
  server can send this packet tp ask client to use another authentication method.

  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>0xFE (254)</td>
    <td>status tag</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string[NUL]"</td>
    <td>plugin name</td>
    <td>name of the client authentication plugin to switch to</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_eof "string[EOF]"</td>
    <td>plugin provided data</td>
    <td>Initial authentication data for that client plugin</td></tr>
  </table>

  @return @ref page_protocol_connection_phase_packets_protocol_auth_switch_response
  or closing the connection.

  @sa send_plugin_request_packet(), client_mpvio_read_packet()
*/


/**
  @page page_protocol_connection_phase_packets_protocol_old_auth_switch_request Protocol::OldAuthSwitchRequest:

  @warning *Deprecated*. Newer servers should never send this since they don't support
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  and they support @ref CLIENT_PLUGIN_AUTH.
  Newer clients should not support it since they should not support
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication.
  and they should support @ref CLIENT_PLUGIN_AUTH.

  Old Authentication Method Switch Request Packet consisting of
  a single 0xfe byte. It is sent by server to request client to switch to
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  if @ref CLIENT_PLUGIN_AUTH capability flag is not supported (by either the
  client or the server).

  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>0xFE (254)</td>
    <td>status tag</td></tr>
  </table>

  @return @ref page_protocol_connection_phase_packets_protocol_auth_switch_response
  with an
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication
  hash or closing the connection.

  @sa client_mpvio_read_packet()
 */

/**
  @page page_protocol_connection_phase_packets_protocol_auth_switch_response Protocol::AuthSwitchResponse:

  Authentication Method Switch Response Packet which contains response data
  generated by the authenticatication method requested in
  @ref page_protocol_connection_phase_packets_protocol_old_auth_switch_request
  packet. This data is opaque to the protocol.

  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_eof "string&lt;EOF&gt;"</td>
    <td>data</td>
    <td>authentication response data</td></tr>
  </table>

  @return @ref page_protocol_connection_phase_packets_protocol_auth_more_data,
    @ref page_protocol_basic_err_packet or @ref page_protocol_basic_ok_packet

  Example:

  If the client sends a @ref page_caching_sha2_authentication_exchanges and
  the server has a
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  for that user it will ask the client to switch to
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  and the client will reply from the
  @ref page_protocol_connection_phase_authentication_methods_native_password_authentication
  plugin:

  <table>
  <tr><td>
  ~~~~~~~~~~~~~~~~~~~~~
  14 00 00 03 f4 17 96 1f    79 f3 ac 10 0b da a6 b3
  ~~~~~~~~~~~~~~~~~~~~~
  </td><td>
  ~~~~~~~~~~~~~~~~~~~~~
  ........y.......
  ~~~~~~~~~~~~~~~~~~~~~
  </td></tr>
  <tr><td>
  ~~~~~~~~~~~~~~~~~~~~~
  b5 c2 0e ab 59 85 ff b8
  ~~~~~~~~~~~~~~~~~~~~~
  </td><td>
  ~~~~~~~~~~~~~~~~~~~~~
  ....Y...
  ~~~~~~~~~~~~~~~~~~~~~
  </td></tr>
  </table>

  @sa client_mpvio_write_packet, server_mpvio_read_packet
*/

/**
  @page page_protocol_connection_phase_packets_protocol_auth_more_data Protocol::AuthMoreData:


  We need to make sure that when sending plugin supplied data to the client they
  are not considered a special out-of-band command, like e.g.
  @ref page_protocol_basic_err_packet,
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_request
  or @ref page_protocol_basic_ok_packet.
  To avoid this the server will send all plugin data packets "wrapped"
  in a command \1.
  Note that the client will continue sending its replies unrwapped:
  @ref page_protocol_connection_phase_packets_protocol_auth_switch_response


  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
  <td>0x01</td>
  <td>status tag</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_eof "string&lt;EOF&gt;"</td>
    <td>authentication method data</td>
    <td>Extra authentication data beyond the initial challenge</td></tr>
  </table>

  @sa wrap_plguin_data_into_proper_command, server_mpvio_write_packet,
  client_mpvio_read_packet
*/

/**
  @page page_protocol_connection_phase_packets_protocol_auth_next_factor_request Protocol::AuthNextFactor:

  Next Authentication method Packet in @ref page_protocol_multi_factor_authentication_methods

  If both server and the client support @ref MULTI_FACTOR_AUTHENTICATION capability,
  server can send this packet to ask client to initiate next authentication method
  in @ref page_protocol_multi_factor_authentication_methods process.

  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
    <td>0x02 </td>
    <td>packet type</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string[NUL]"</td>
    <td>plugin name</td>
    <td>name of the client authentication plugin </td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_eof "string[EOF]"</td>
    <td>plugin provided data</td>
    <td>Initial authentication data for that client plugin</td></tr>
  </table>

  @return @ref page_protocol_connection_phase_packets_protocol_auth_next_factor_request
  or closing the connection.

  @sa send_auth_next_factor_packet(), client_mpvio_read_packet()
  @ref send_auth_next_factor_packet(), client_mpvio_read_packet()
*/

/* clang-format on */

/**
  Sends a @ref
  page_protocol_connection_phase_packets_protocol_auth_switch_request

  Used by the server to request that a client should restart authentication
  using a different authentication plugin.

  See @ref page_protocol_connection_phase_packets_protocol_auth_switch_request
  for more details.

  @retval false ok
  @retval true error
*/
static bool send_plugin_request_packet(MPVIO_EXT *mpvio, const uchar *data,
                                       uint data_len) {
  assert(mpvio->packets_written == 1);
  assert(mpvio->packets_read == 1);
  static uchar switch_plugin_request_buf[] = {254};

  DBUG_TRACE;

  /*
    In case of --skip-grant-tables, mpvio->status might already have set to
    SUCCESS, don't reset it to FAILURE now.
  */
  if (initialized)
    mpvio->status = MPVIO_EXT::FAILURE;  // the status is no longer RESTART

  /* Send the client side authentication plugin name */
  std::string client_auth_plugin(
      ((st_mysql_auth *)(plugin_decl(mpvio->plugin)->info))
          ->client_auth_plugin);

  assert(client_auth_plugin.c_str());

  DBUG_EXECUTE_IF("invalidate_client_auth_plugin", {
    client_auth_plugin.clear();
    client_auth_plugin = std::string("..") + std::string(FN_DIRSEP) +
                         std::string("..") + std::string(FN_DIRSEP) +
                         std::string("mysql_native_password");
  });
  /*
    If we're dealing with an older client we can't just send a change plugin
    packet to re-initiate the authentication handshake, because the client
    won't understand it. The good thing is that we don't need to : the old
    client expects us to just check the user credentials here, which we can do
    by just reading the cached data that are placed there by
    parse_com_change_user_packet() In this case we just do nothing and behave as
    if normal authentication should continue.
  */
  if (!(mpvio->protocol->has_client_capability(CLIENT_PLUGIN_AUTH))) {
    DBUG_PRINT("info", ("old client sent a COM_CHANGE_USER"));
    assert(mpvio->cached_client_reply.pkt);
    /* get the status back so the read can process the cached result */
    mpvio->status = MPVIO_EXT::RESTART;
    return false;
  }

  DBUG_PRINT("info", ("requesting client to use the %s plugin",
                      client_auth_plugin.c_str()));
  return net_write_command(
      mpvio->protocol->get_net(), switch_plugin_request_buf[0],
      pointer_cast<const uchar *>(client_auth_plugin.c_str()),
      client_auth_plugin.size() + 1, pointer_cast<const uchar *>(data),
      data_len);
}

/**
  Sends a @ref
  page_protocol_connection_phase_packets_protocol_auth_next_factor_request

  Used by the server to request that a client should initiate authentication
  for next authentication methods in the plugin chain of user definition.

  See @ref
  page_protocol_connection_phase_packets_protocol_auth_next_factor_request for
  more details.

  @param [in] mpvio      The communications channel
  @param [in] data       Client plugin data
  @param [in] data_len   Length of client plugin data

  @retval false ok
  @retval true error
*/
static bool send_auth_next_factor_packet(MPVIO_EXT *mpvio, const uchar *data,
                                         uint data_len) {
  static uchar auth_next_factor_request_buf[] = {2};
  DBUG_TRACE;

  /* Send the client side authentication plugin name */
  std::string client_auth_plugin(
      ((st_mysql_auth *)(plugin_decl(mpvio->plugin)->info))
          ->client_auth_plugin);
  assert(client_auth_plugin.c_str());
  if (!(mpvio->protocol->has_client_capability(MULTI_FACTOR_AUTHENTICATION)))
    return false;

  DBUG_PRINT("info",
             ("requesting client to initiate %s plugin's authentication",
              client_auth_plugin.c_str()));

  return net_write_command(
      mpvio->protocol->get_net(), auth_next_factor_request_buf[0],
      pointer_cast<const uchar *>(client_auth_plugin.c_str()),
      client_auth_plugin.size() + 1, pointer_cast<const uchar *>(data),
      data_len);
}

/* Return true if there is no users that can match the given host */

bool acl_check_host(THD *thd, const char *host, const char *ip) {
  Acl_cache_lock_guard acl_cache_lock(thd, Acl_cache_lock_mode::READ_MODE);
  if (!acl_cache_lock.lock(false)) return true;

  if (allow_all_hosts) return false;

  if ((host && acl_check_hosts->count(host) != 0) ||
      (ip && acl_check_hosts->count(ip) != 0))
    return false;  // Found host

  for (ACL_HOST_AND_IP *acl = acl_wild_hosts->begin();
       acl != acl_wild_hosts->end(); ++acl) {
    if (acl->compare_hostname(host, ip)) return false;  // Host ok
  }

  if (ip != nullptr) {
    /* Increment HOST_CACHE.COUNT_HOST_ACL_ERRORS. */
    Host_errors errors;
    errors.m_host_acl = 1;
    inc_host_errors(ip, &errors);
  }
  return true;  // Host is not allowed
}

/**
  When authentication is attempted using an unknown username a dummy user
  account with no authentication capabilities is assigned to the connection.
  When server is started with -skip-grant-tables, a dummy user account
  with authentication capabilities is assigned to the connection.
  Dummy user authenticates with the empty authentication string.
  This is done to decrease the cost of enumerating user accounts based on
  authentication protocol.

  @param [in] username       A dummy user to be created.
  @param [in] hostname       Host of the dummy user.
  @param [in] mem            Memory in which the dummy ACL user will be created.
  @param [in] rand           Seed value to generate random data
  @param [in] is_initialized State of ACL caches

  @retval A dummy ACL USER
*/
ACL_USER *decoy_user(const LEX_CSTRING &username, const LEX_CSTRING &hostname,
                     MEM_ROOT *mem, struct rand_struct *rand,
                     bool is_initialized) {
  ACL_USER *user = (ACL_USER *)mem->Alloc(sizeof(ACL_USER));
  user->can_authenticate = !initialized;
  user->user = strdup_root(mem, username.str);
  user->user[username.length] = '\0';
  user->host.update_hostname(strdup_root(mem, hostname.str));
  user->ssl_cipher = empty_c_string;
  user->x509_issuer = empty_c_string;
  user->x509_subject = empty_c_string;
  user->password_last_changed.time_type = MYSQL_TIMESTAMP_ERROR;
  user->password_lifetime = 0;
  user->use_default_password_lifetime = true;
  user->account_locked = false;
  user->use_default_password_reuse_interval = true;
  user->password_reuse_interval = 0;
  user->use_default_password_history = true;
  user->password_history_length = 0;
  user->password_require_current = Lex_acl_attrib_udyn::DEFAULT;
  user->password_locked_state.set_parameters(0, 0);
  user->m_mfa = nullptr;

  if (is_initialized) {
    Auth_id key(user);

    uint value;
    if (unknown_accounts->find(key, value)) {
      user->plugin = Cached_authentication_plugins::cached_plugins_names[value];
    } else {
      const int DECIMAL_SHIFT = 1000;
      const int random_number = static_cast<int>(my_rnd(rand) * DECIMAL_SHIFT);
      uint plugin_num = (uint)(random_number % ((uint)PLUGIN_LAST));
      user->plugin =
          Cached_authentication_plugins::cached_plugins_names[plugin_num];
      unknown_accounts->clear_if_greater(MAX_UNKNOWN_ACCOUNTS);

      /*
        If we fail to insert, someone already did it.
        So try to retrieve it. If we fail (e.g. map was cleared),
        just use the default and move on.
      */
      if (!unknown_accounts->insert(key, plugin_num)) {
        if (!unknown_accounts->find(key, plugin_num))
          user->plugin = default_auth_plugin_name;
        else
          user->plugin =
              Cached_authentication_plugins::cached_plugins_names[plugin_num];
      }
    }
  } else
    user->plugin = default_auth_plugin_name;

  for (int i = 0; i < NUM_CREDENTIALS; ++i) {
    memset(user->credentials[i].m_salt, 0, SCRAMBLE_LENGTH + 1);
    user->credentials[i].m_salt_len = 0;
    user->credentials[i].m_auth_string = EMPTY_CSTR;
  }
  return user;
}

/**
   Finds acl entry in user database for authentication purposes.

   Finds a user and copies it into mpvio. Reports an authentication
   failure if a user is not found.

   @note find_acl_user is not the same, because it doesn't take into
   account the case when user is not empty, but acl_user->user is empty

   @retval 0    found
   @retval 1    not found
*/
static bool find_mpvio_user(THD *thd, MPVIO_EXT *mpvio) {
  DBUG_TRACE;
  DBUG_PRINT("info", ("entry: %s", mpvio->auth_info.user_name));
  assert(mpvio->acl_user == nullptr);

  Acl_cache_lock_guard acl_cache_lock(thd, Acl_cache_lock_mode::READ_MODE);
  if (!acl_cache_lock.lock(false)) return true;

  Acl_user_ptr_list *list = nullptr;
  if (likely(acl_users)) {
    list = cached_acl_users_for_name(mpvio->auth_info.user_name);
  }
  if (list) {
    for (auto it = list->begin(); it != list->end(); ++it) {
      ACL_USER *acl_user_tmp = (*it);

      if ((!acl_user_tmp->user ||
           !strcmp(mpvio->auth_info.user_name, acl_user_tmp->user)) &&
          acl_user_tmp->host.compare_hostname(mpvio->host, mpvio->ip)) {
        mpvio->acl_user = acl_user_tmp->copy(mpvio->mem_root);
        *(mpvio->restrictions) =
            acl_restrictions->find_restrictions(mpvio->acl_user);

        /*
          When setting mpvio->acl_user_plugin we can save memory allocation if
          this is a built in plugin.
        */
        if (auth_plugin_is_built_in(acl_user_tmp->plugin.str))
          mpvio->acl_user_plugin = mpvio->acl_user->plugin;
        else
          lex_string_strmake(mpvio->mem_root, &mpvio->acl_user_plugin,
                             acl_user_tmp->plugin.str,
                             acl_user_tmp->plugin.length);
        break;
      }
    }
  }
  acl_cache_lock.unlock();

  if (!mpvio->acl_user) {
    /*
      Pretend the user exists; let the plugin decide how to handle
      bad credentials.
    */
    LEX_CSTRING usr = {mpvio->auth_info.user_name,
                       mpvio->auth_info.user_name_length};
    LEX_CSTRING hst = {mpvio->host ? mpvio->host : mpvio->ip,
                       mpvio->host ? strlen(mpvio->host) : strlen(mpvio->ip)};
    mpvio->acl_user =
        decoy_user(usr, hst, mpvio->mem_root, mpvio->rand, initialized);
    mpvio->acl_user_plugin = mpvio->acl_user->plugin;
  }

  if (!Cached_authentication_plugins::compare_plugin(
          PLUGIN_MYSQL_NATIVE_PASSWORD, mpvio->acl_user->plugin) &&
      !(mpvio->protocol->has_client_capability(CLIENT_PLUGIN_AUTH))) {
    /* user account requires non-default plugin and the client is too old */
    assert(!Cached_authentication_plugins::compare_plugin(
        PLUGIN_MYSQL_NATIVE_PASSWORD, mpvio->acl_user->plugin));
    my_error(ER_NOT_SUPPORTED_AUTH_MODE, MYF(0));
    query_logger.general_log_print(thd, COM_CONNECT, "%s",
                                   ER_DEFAULT(ER_NOT_SUPPORTED_AUTH_MODE));
    return true;
  }

  mpvio->auth_info.multi_factor_auth_info[0].auth_string =
      mpvio->acl_user->credentials[PRIMARY_CRED].m_auth_string.str;
  mpvio->auth_info.multi_factor_auth_info[0].auth_string_length =
      (unsigned long)mpvio->acl_user->credentials[PRIMARY_CRED]
          .m_auth_string.length;
  if (mpvio->acl_user->credentials[SECOND_CRED].m_auth_string.length) {
    mpvio->auth_info.additional_auth_string =
        mpvio->acl_user->credentials[SECOND_CRED].m_auth_string.str;
    mpvio->auth_info.additional_auth_string_length =
        (unsigned long)mpvio->acl_user->credentials[SECOND_CRED]
            .m_auth_string.length;
  } else {
    mpvio->auth_info.additional_auth_string = nullptr;
    mpvio->auth_info.additional_auth_string_length = 0;
  }
  strmake(mpvio->auth_info.authenticated_as,
          mpvio->acl_user->user ? mpvio->acl_user->user : "", USERNAME_LENGTH);

  /* auth_string references to 1st factor auth plugin credential details */
  mpvio->auth_info.auth_string =
      mpvio->auth_info.multi_factor_auth_info[0].auth_string;
  mpvio->auth_info.auth_string_length =
      mpvio->auth_info.multi_factor_auth_info[0].auth_string_length;

  DBUG_PRINT("info",
             ("exit: user=%s, auth_string=%s, authenticated as=%s"
              ", plugin=%s, authentication factor=%d",
              mpvio->auth_info.user_name, mpvio->auth_info.auth_string,
              mpvio->auth_info.authenticated_as, mpvio->acl_user->plugin.str,
              mpvio->auth_info.current_auth_factor));

  /* Copy 2nd and 3rd factor auth string and registration flag into mpvio */
  if (mpvio->acl_user->m_mfa) {
    Multi_factor_auth_list *auth_factor =
        mpvio->acl_user->m_mfa->get_multi_factor_auth_list();
    uint f = 1;
    for (auto m_it : auth_factor->get_mfa_list()) {
      Multi_factor_auth_info *af = m_it->get_multi_factor_auth_info();
      mpvio->auth_info.multi_factor_auth_info[f].auth_string =
          af->get_auth_str();
      mpvio->auth_info.multi_factor_auth_info[f].auth_string_length =
          af->get_auth_str_len();
      mpvio->auth_info.multi_factor_auth_info[f].is_registration_required =
          af->get_requires_registration();
      DBUG_PRINT(
          "info",
          ("exit: user=%s, auth_string=%s, plugin=%s, authentication factor=%d",
           mpvio->auth_info.user_name,
           mpvio->auth_info.multi_factor_auth_info[f].auth_string,
           af->get_plugin_str(), f));
      f++;
    }
  }

  return false;
}

static bool read_client_connect_attrs(THD *thd, char **ptr,
                                      size_t *max_bytes_available,
                                      MPVIO_EXT *mpvio [[maybe_unused]]) {
  size_t length, length_length;
  char *ptr_save;

  /* not enough bytes to hold the length */
  if (*max_bytes_available < 1) return true;

  /* read the length */
  ptr_save = *ptr;
  length = static_cast<size_t>(net_field_length_ll((uchar **)ptr));
  length_length = *ptr - ptr_save;
  if (*max_bytes_available < length_length) return true;

  *max_bytes_available -= length_length;

  /* length says there're more data than can fit into the packet */
  if (length > *max_bytes_available) return true;

  /* impose an artificial length limit of 64k */
  if (length > 65535) return true;

#ifdef HAVE_PSI_THREAD_INTERFACE
  MYSQL_SERVER_AUTH_INFO *auth_info = &mpvio->auth_info;
  int bytes_lost;
  if ((bytes_lost = PSI_THREAD_CALL(set_thread_connect_attrs)(
           *ptr, length, mpvio->charset_adapter->charset())))
    LogErr(WARNING_LEVEL, ER_CONN_ATTR_TRUNCATED, (unsigned long)length,
           (int)bytes_lost, (unsigned long long)mpvio->thread_id,
           (auth_info->user_name == NULL) ? "" : auth_info->user_name,
           auth_info->host_or_ip, auth_info->authenticated_as,
           mpvio->can_authenticate() ? "yes" : "no");
#endif /* HAVE_PSI_THREAD_INTERFACE */

  // assign the connection attributes in the current thread
  thd->m_connection_attributes = std::vector<char>(length);
  std::copy(*ptr, *ptr + length, thd->m_connection_attributes.begin());

  *max_bytes_available -= length;
  *ptr = *ptr + length;

  return false;
}

static bool acl_check_ssl(THD *thd, const ACL_USER *acl_user) {
  Vio *vio = thd->get_protocol_classic()->get_vio();
  SSL *ssl = (SSL *)vio->ssl_arg;
  X509 *cert;

  /*
    At this point we know that user is allowed to connect
    from given host by given username/password pair. Now
    we check if SSL is required, if user is using SSL and
    if X509 certificate attributes are OK
  */
  switch (acl_user->ssl_type) {
    case SSL_TYPE_NOT_SPECIFIED:  // Impossible
    case SSL_TYPE_NONE:           // SSL is not required
      return false;
    case SSL_TYPE_ANY:  // Any kind of SSL is ok
      return vio_type(vio) != VIO_TYPE_SSL;
    case SSL_TYPE_X509: /* Client should have any valid certificate. */
      /*
        Connections with non-valid certificates are dropped already
        in sslaccept() anyway, so we do not check validity here.

        We need to check for absence of SSL because without SSL
        we should reject connection.
      */
      if (vio_type(vio) == VIO_TYPE_SSL &&
          SSL_get_verify_result(ssl) == X509_V_OK &&
          (cert = SSL_get_peer_certificate(ssl))) {
        X509_free(cert);
        return false;
      }
      return true;
    case SSL_TYPE_SPECIFIED: /* Client should have specified attrib */
      /* If a cipher name is specified, we compare it to actual cipher in use.
       */
      if (vio_type(vio) != VIO_TYPE_SSL ||
          SSL_get_verify_result(ssl) != X509_V_OK)
        return true;
      if (acl_user->ssl_cipher) {
        DBUG_PRINT("info", ("comparing ciphers: '%s' and '%s'",
                            acl_user->ssl_cipher, SSL_get_cipher(ssl)));
        if (strcmp(acl_user->ssl_cipher, SSL_get_cipher(ssl))) {
          LogErr(INFORMATION_LEVEL, ER_X509_CIPHERS_MISMATCH,
                 acl_user->ssl_cipher, SSL_get_cipher(ssl));
          return true;
        }
      }
      /* Prepare certificate (if exists) */
      if (!(cert = SSL_get_peer_certificate(ssl))) return true;
      /* If X509 issuer is specified, we check it... */
      if (acl_user->x509_issuer) {
        char *ptr = X509_NAME_oneline(X509_get_issuer_name(cert), nullptr, 0);
        DBUG_PRINT("info", ("comparing issuers: '%s' and '%s'",
                            acl_user->x509_issuer, ptr));
        if (strcmp(acl_user->x509_issuer, ptr)) {
          LogErr(INFORMATION_LEVEL, ER_X509_ISSUER_MISMATCH,
                 acl_user->x509_issuer, ptr);
          OPENSSL_free(ptr);
          X509_free(cert);
          return true;
        }
        OPENSSL_free(ptr);
      }
      /* X509 subject is specified, we check it .. */
      if (acl_user->x509_subject) {
        char *ptr = X509_NAME_oneline(X509_get_subject_name(cert), nullptr, 0);
        DBUG_PRINT("info", ("comparing subjects: '%s' and '%s'",
                            acl_user->x509_subject, ptr));
        if (strcmp(acl_user->x509_subject, ptr)) {
          LogErr(INFORMATION_LEVEL, ER_X509_SUBJECT_MISMATCH,
                 acl_user->x509_subject, ptr);
          OPENSSL_free(ptr);
          X509_free(cert);
          return true;
        }
        OPENSSL_free(ptr);
      }
      X509_free(cert);
      return false;
  }
  return true;
}

/**

  Check if server has valid public key/private key
  pair for RSA communication.

  @retval false RSA support is available
  @retval true RSA support is not available
*/
bool sha256_rsa_auth_status() {
  return (!g_sha256_rsa_keys->get_private_key() ||
          !g_sha256_rsa_keys->get_public_key());
}

/* clang-format off */
/**
  @page page_protocol_com_change_user COM_CHANGE_USER

  @brief Changes the user of the current connection.

  Also and resets the following connection state:
  - user variables
  - temporary tables
  - prepared statements
  - ... and others

  It is going through the same states as the
  @ref sect_protocol_connection_phase_initial_handshake

  @return @ref page_protocol_connection_phase_packets_protocol_auth_switch_request
    or @ref page_protocol_basic_err_packet

  <table>
  <caption>Payload</caption>
  <tr><th>Type</th><th>Name</th><th>Description</th></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
      <td>command</td>
      <td>0x11: COM_CHANGE_USER</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string&lt;NUL&gt;"</td>
      <td>user</td>
      <td>user name</td></tr>
  <tr><td colspan="3">if capabilities @& @ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION" {</td></tr>
  <tr><td>@ref a_protocol_type_int1 "int&lt;1&gt;"</td>
      <td>auth_plugin_data_len</td>
      <td>length of auth_response</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_le "$length"</td>
      <td>auth_plugin_data</td>
      <td>authentication data</td></tr>
  <tr><td colspan="3">} else {</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string&lt;NUL&gt;"</td>
      <td>auth_plugin_data</td>
      <td>authentication data (9 bytes)</td></tr>
  <tr><td colspan="3">}</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string&lt;NUL&gt;"</td>
      <td>database</td>
      <td>schema name</td></tr>
  <tr><td colspan="3">if more data available {</td></tr>
  <tr><td colspan="3">if capabilities @& ::CLIENT_PROTOCOL_41 {</td></tr>
  <tr><td>@ref a_protocol_type_int2 "int&lt;2&gt;"</td>
      <td>character_set</td>
      <td>new connection character set. See @ref page_protocol_basic_character_set</td></tr>
  <tr><td colspan="3">} -- ::CLIENT_PROTOCOL_41</td></tr>
  <tr><td colspan="3">if capabilities @& ::CLIENT_PLUGIN_AUTH {</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_null "string&lt;NUL&gt;"</td>
      <td>auth_plugin_name</td>
      <td>client authentication plugin name used to generate auth_plugin_data</td></tr>
  <tr><td colspan="3">} -- ::CLIENT_PLUGIN_AUTH</td></tr>
  <tr><td colspan="3">if capabilities @& ::CLIENT_CONNECT_ATTRS {</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_int_le "int&lt;lenenc&gt;"</td>
      <td>connection_attributes_length</td>
      <td>length in bytes of the following block of key-value pairs</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_le "$length"</td>
      <td>key</td>
      <td>Key name</td></tr>
  <tr><td>@ref sect_protocol_basic_dt_string_le "$length"</td>
      <td>value</td>
      <td>value of key</td></tr>
  <tr><td colspan="3">more key/value pairs until connection_attributes_length is depleted</td></tr>
  <tr><td colspan="3">} -- ::CLIENT_CONNECT_ATTRS</td></tr>
  <tr><td colspan="3">} -- more data available</td></tr>
  </table>

  Please also read @ref sect_protocol_connection_phase_com_change_user_auth

  @sa mysql_change_user, send_change_user_packet, parse_com_change_user_packet,
  acl_authenticate, dispatch_command
*/
/* clang-format on */

/**
  @brief Parses a @ref page_protocol_com_change_user

  @param thd            current thread
  @param mpvio          the communications channel
  @param packet_length  length of the packet in mpvio's buffer

  @retval true error
  @retval false success
*/
static bool parse_com_change_user_packet(THD *thd, MPVIO_EXT *mpvio,
                                         size_t packet_length) {
  Protocol_classic *protocol = mpvio->protocol;
  char *user = (char *)protocol->get_net()->read_pos;
  char *end = user + packet_length;
  /* Safe because there is always a trailing \0 at the end of the packet */
  char *passwd = strend(user) + 1;
  size_t user_len = passwd - user - 1;
  char *db = passwd;
  char db_buff[NAME_LEN + 1];           // buffer to store db in utf8
  char user_buff[USERNAME_LENGTH + 1];  // buffer to store user in utf8
  uint dummy_errors;

  DBUG_TRACE;
  if (passwd >= end) {
    my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
    return true;
  }

  /*
    Clients send the size (1 byte) + string (not null-terminated).

    Cast *passwd to an unsigned char, so that it doesn't extend the sign for
    *passwd > 127 and become 2**32-127+ after casting to uint.
  */
  size_t passwd_len = (uchar)(*passwd++);

  db += passwd_len + 1;
  /*
    Database name is always NUL-terminated, so in case of empty database
    the packet must contain at least the trailing '\0'.
  */
  if (db >= end) {
    my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
    return true;
  }

  size_t db_len = strlen(db);

  char *ptr = db + db_len + 1;

  if (ptr + 1 < end) {
    if (mpvio->charset_adapter->init_client_charset(uint2korr(ptr)))
      return true;
    // skip over the charset's 2 bytes
    ptr += 2;
  }

  /* Convert database and user names to utf8 */
  db_len = copy_and_convert(db_buff, sizeof(db_buff) - 1, system_charset_info,
                            db, db_len, mpvio->charset_adapter->charset(),
                            &dummy_errors);
  db_buff[db_len] = 0;

  user_len = copy_and_convert(user_buff, sizeof(user_buff) - 1,
                              system_charset_info, user, user_len,
                              mpvio->charset_adapter->charset(), &dummy_errors);
  user_buff[user_len] = 0;

  /* we should not free mpvio->user here: it's saved by dispatch_command() */
  if (!(mpvio->auth_info.user_name = my_strndup(
            key_memory_MPVIO_EXT_auth_info, user_buff, user_len, MYF(MY_WME))))
    return true;
  mpvio->auth_info.user_name_length = user_len;

  if (lex_string_strmake(mpvio->mem_root, &mpvio->db, db_buff, db_len))
    return true; /* The error is set by make_lex_string(). */

  if (!initialized) {
    // if mysqld's been started with --skip-grant-tables option
    strmake(mpvio->auth_info.authenticated_as, mpvio->auth_info.user_name,
            USERNAME_LENGTH);

    mpvio->status = MPVIO_EXT::SUCCESS;
    return false;
  }

  if (find_mpvio_user(thd, mpvio)) {
    return true;
  }

  const char *client_plugin;
  if (protocol->has_client_capability(CLIENT_PLUGIN_AUTH)) {
    client_plugin = ptr;
    /*
      ptr needs to be updated to point to correct position so that
      connection attributes are read properly.
    */
    ptr = ptr + strlen(client_plugin) + 1;

    if (client_plugin >= end) {
      my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
      return true;
    }
  } else
    client_plugin = Cached_authentication_plugins::get_plugin_name(
        PLUGIN_MYSQL_NATIVE_PASSWORD);

  if (ptr > end) {
    my_error(ER_UNKNOWN_COM_ERROR, MYF(0));
    return true;
  }
  size_t bytes_remaining_in_packet = end - ptr;

  if (protocol->has_client_capability(CLIENT_CONNECT_ATTRS) &&
      read_client_connect_attrs(thd, &ptr, &bytes_remaining_in_packet, mpvio))
    return true;

  DBUG_PRINT("info", ("client_plugin=%s, restart", client_plugin));
  /*
    Remember the data part of the packet, to present it to plugin in
    read_packet()
  */
  mpvio->cached_client_reply.pkt = passwd;
  mpvio->cached_client_reply.pkt_len = passwd_len;
  mpvio->cached_client_reply.plugin = client_plugin;
  mpvio->status = MPVIO_EXT::RESTART;

  return false;
}

/** Get a string according to the protocol of the underlying buffer. */
typedef char *(*get_proto_string_func_t)(char **, size_t *, size_t *);

/**
  Get a string formatted according to the 4.1 version of the MySQL protocol.

  @param [in, out] buffer    Pointer to the user-supplied buffer to be scanned.
  @param [in, out] max_bytes_available  Limit the bytes to scan.
  @param [out] string_length The number of characters scanned not including
                            the null character.

  @note Strings are always null character terminated in this version of the
          protocol.

  @note The string_length does not include the terminating null character.
          However, after the call, the buffer is increased by string_length+1
          bytes, beyond the null character if there still available bytes to
          scan.

  @return pointer to beginning of the string scanned.
    @retval NULL The buffer content is malformed
*/

static char *get_41_protocol_string(char **buffer, size_t *max_bytes_available,
                                    size_t *string_length) {
  char *str = (char *)memchr(*buffer, '\0', *max_bytes_available);

  if (str == nullptr) return nullptr;

  *string_length = (size_t)(str - *buffer);
  *max_bytes_available -= *string_length + 1;
  str = *buffer;
  *buffer += *string_length + 1;

  return str;
}

/**
  Get a string formatted according to the 4.0 version of the MySQL protocol.

  @param [in, out] buffer    Pointer to the user-supplied buffer to be scanned.
  @param [in, out] max_bytes_available  Limit the bytes to scan.
  @param [out] string_length The number of characters scanned not including
                            the null character.

  @note If there are not enough bytes left after the current position of
          the buffer to satisfy the current string, the string is considered
          to be empty and a pointer to empty_c_string is returned.

  @note A string at the end of the packet is not null terminated.

  @return Pointer to beginning of the string scanned, or a pointer to a empty
          string.
*/
static char *get_40_protocol_string(char **buffer, size_t *max_bytes_available,
                                    size_t *string_length) {
  char *str;
  size_t len;

  /* No bytes to scan left, treat string as empty. */
  if ((*max_bytes_available) == 0) {
    *string_length = 0;
    return empty_c_string;
  }

  str = (char *)memchr(*buffer, '\0', *max_bytes_available);

  /*
    If the string was not null terminated by the client,
    the remainder of the packet is the string. Otherwise,
    advance the buffer past the end of the null terminated
    string.
  */
  if (str == nullptr)
    len = *string_length = *max_bytes_available;
  else
    len = (*string_length = (size_t)(str - *buffer)) + 1;

  str = *buffer;
  *buffer += len;
  *max_bytes_available -= len;

  return str;
}

/**
  Get a length encoded string from a user-supplied buffer.

  @param [in, out] buffer The buffer to scan; updates position after scan.
  @param [in, out] max_bytes_available Limit the number of bytes to scan
  @param [out] string_length Number of characters scanned

  @note In case the length is zero, then the total size of the string is
    considered to be 1 byte; the size byte.

  @return pointer to first byte after the header in buffer.
    @retval NULL The buffer content is malformed
*/

static char *get_56_lenc_string(char **buffer, size_t *max_bytes_available,
                                size_t *string_length) {
  static char empty_string[1] = {'\0'};
  char *begin = *buffer;
  uchar *pos = (uchar *)begin;

  if (*max_bytes_available == 0) return nullptr;

  /*
    If the length encoded string has the length 0
    the total size of the string is only one byte long (the size byte)
  */
  if (*begin == 0) {
    *string_length = 0;
    --*max_bytes_available;
    ++*buffer;
    /*
      Return a pointer to the \0 character so the return value will be
      an empty string.
    */
    return empty_string;
  }

  /* Make sure we have enough bytes available for net_field_length_ll */

  DBUG_EXECUTE_IF("buffer_too_short_3", *pos = 252; *max_bytes_available = 2;);
  DBUG_EXECUTE_IF("buffer_too_short_4", *pos = 253; *max_bytes_available = 3;);
  DBUG_EXECUTE_IF("buffer_too_short_9", *pos = 254; *max_bytes_available = 8;);

  size_t required_length = (size_t)net_field_length_size(pos);

  if (*max_bytes_available < required_length) return nullptr;

  *string_length = (size_t)net_field_length_ll((uchar **)buffer);

  DBUG_EXECUTE_IF("sha256_password_scramble_too_long",
                  *string_length = SIZE_T_MAX;);

  size_t len_len = (size_t)(*buffer - begin);

  assert((*max_bytes_available >= len_len) && (len_len == required_length));

  if (*string_length > *max_bytes_available - len_len) return nullptr;

  *max_bytes_available -= *string_length;
  *max_bytes_available -= len_len;
  *buffer += *string_length;
  return (char *)(begin + len_len);
}

/**
  Get a length encoded string from a user-supplied buffer.

  @param [in, out] buffer The buffer to scan; updates position after scan.
  @param [in, out] max_bytes_available Limit the number of bytes to scan
  @param [out] string_length Number of characters scanned

  @note In case the length is zero, then the total size of the string is
    considered to be 1 byte; the size byte.

  @note the maximum size of the string is 255 because the header is always
    1 byte.
  @return pointer to first byte after the header in buffer.
    @retval NULL The buffer content is malformed
*/

static char *get_41_lenc_string(char **buffer, size_t *max_bytes_available,
                                size_t *string_length) {
  if (*max_bytes_available == 0) return nullptr;

  /* Do double cast to prevent overflow from signed / unsigned conversion */
  size_t str_len = (size_t)(unsigned char)**buffer;

  /*
    If the length encoded string has the length 0
    the total size of the string is only one byte long (the size byte)
  */
  if (str_len == 0) {
    ++*buffer;
    *string_length = 0;
    /*
      Return a pointer to the 0 character so the return value will be
      an empty string.
    */
    return *buffer - 1;
  }

  if (str_len >= *max_bytes_available) return nullptr;

  char *str = *buffer + 1;
  *string_length = str_len;
  *max_bytes_available -= *string_length + 1;
  *buffer += *string_length + 1;
  return str;
}

/* the packet format is described in send_client_reply_packet() */
static size_t parse_client_handshake_packet(THD *thd, MPVIO_EXT *mpvio,
                                            uchar **buff, size_t pkt_len) {
  Protocol_classic *protocol = mpvio->protocol;
  char *end;
  bool packet_has_required_size = false;
  /* save server capabilities before setting client capabilities */
  bool is_server_supports_zlib =
      protocol->has_client_capability(CLIENT_COMPRESS);
  bool is_server_supports_zstd =
      protocol->has_client_capability(CLIENT_ZSTD_COMPRESSION_ALGORITHM);
  assert(mpvio->status == MPVIO_EXT::FAILURE);

  uint charset_code = 0;
  end = (char *)protocol->get_net()->read_pos;
  /*
    In order to safely scan a head for '\0' string terminators
    we must keep track of how many bytes remain in the allocated
    buffer or we might read past the end of the buffer.
  */
  size_t bytes_remaining_in_packet = pkt_len;

  /*
    Peek ahead on the client capability packet and determine which version of
    the protocol should be used.
  */
  if (bytes_remaining_in_packet < 2) return packet_error;

  protocol->set_client_capabilities(uint2korr(end));

  /*
    JConnector only sends server capabilities before starting SSL
    negotiation.  The below code is patch for this.
  */
  if (bytes_remaining_in_packet == 4 &&
      protocol->has_client_capability(CLIENT_SSL)) {
    protocol->set_client_capabilities(uint4korr(end));
    mpvio->max_client_packet_length = 0xfffff;
    charset_code = global_system_variables.character_set_client->number;
    goto skip_to_ssl;
  }

  if (protocol->has_client_capability(CLIENT_PROTOCOL_41))
    packet_has_required_size =
        bytes_remaining_in_packet >= AUTH_PACKET_HEADER_SIZE_PROTO_41;
  else
    packet_has_required_size =
        bytes_remaining_in_packet >= AUTH_PACKET_HEADER_SIZE_PROTO_40;

  if (!packet_has_required_size) return packet_error;

  if (protocol->has_client_capability(CLIENT_PROTOCOL_41)) {
    protocol->set_client_capabilities(uint4korr(end));
    mpvio->max_client_packet_length = uint4korr(end + 4);
    charset_code = (uint)(uchar) * (end + 8);
    /*
      Skip 23 remaining filler bytes which have no particular meaning.
    */
    end += AUTH_PACKET_HEADER_SIZE_PROTO_41;
    bytes_remaining_in_packet -= AUTH_PACKET_HEADER_SIZE_PROTO_41;
  } else {
    protocol->set_client_capabilities(uint2korr(end));
    mpvio->max_client_packet_length = uint3korr(end + 2);
    end += AUTH_PACKET_HEADER_SIZE_PROTO_40;
    bytes_remaining_in_packet -= AUTH_PACKET_HEADER_SIZE_PROTO_40;
    /**
      Old clients didn't have their own charset. Instead the assumption
      was that they used what ever the server used.
    */
    charset_code = global_system_variables.character_set_client->number;
  }

skip_to_ssl:
  DBUG_PRINT("info",
             ("client capabilities: %lu", protocol->get_client_capabilities()));

  /*
    If client requested SSL then we must stop parsing, try to switch to SSL,
    and wait for the client to send a new handshake packet.
    The client isn't expected to send any more bytes until SSL is initialized.
  */
  if (protocol->has_client_capability(CLIENT_SSL)) {
    unsigned long errptr;
    uint ssl_charset_code = 0;

    /*
      We need to make sure that reference count for
      SSL context is kept till the end of function
    */
    std::unique_ptr<Lock_and_access_ssl_acceptor_context> context;
    if (thd->is_admin_connection() && g_admin_ssl_configured == true)
      context =
          std::make_unique<Lock_and_access_ssl_acceptor_context>(mysql_admin);
    else
      context =
          std::make_unique<Lock_and_access_ssl_acceptor_context>(mysql_main);
    /* Do the SSL layering. */
    if (!context.get()->have_ssl()) return packet_error;
    DBUG_PRINT("info", ("IO layer change in progress..."));
    if (sslaccept(*(context.get()), protocol->get_vio(),
                  protocol->get_net()->read_timeout, &errptr)) {
      DBUG_PRINT("error", ("Failed to accept new SSL connection"));
      return packet_error;
    }

    DBUG_PRINT("info", ("Reading user information over SSL layer"));
    int rc = protocol->read_packet();
    pkt_len = protocol->get_packet_length();
    if (rc) {
      DBUG_PRINT("error", ("Failed to read user information (pkt_len= %lu)",
                           static_cast<ulong>(pkt_len)));
      return packet_error;
    }
    /* mark vio as encrypted */
    mpvio->vio_is_encrypted = 1;

    /*
      A new packet was read and the statistics reflecting the remaining bytes
      in the packet must be updated.
    */
    bytes_remaining_in_packet = pkt_len;

    /*
      After the SSL handshake is performed the client resends the handshake
      packet but because of legacy reasons we chose not to parse the packet
      fields a second time and instead only assert the length of the packet.
    */
    if (protocol->has_client_capability(CLIENT_PROTOCOL_41)) {
      packet_has_required_size =
          bytes_remaining_in_packet >= AUTH_PACKET_HEADER_SIZE_PROTO_41;
      ssl_charset_code =
          (uint)(uchar) * ((char *)protocol->get_net()->read_pos + 8);
      DBUG_PRINT("info", ("client_character_set: %u", ssl_charset_code));
      end = (char *)protocol->get_net()->read_pos +
            AUTH_PACKET_HEADER_SIZE_PROTO_41;
      bytes_remaining_in_packet -= AUTH_PACKET_HEADER_SIZE_PROTO_41;
    } else {
      packet_has_required_size =
          bytes_remaining_in_packet >= AUTH_PACKET_HEADER_SIZE_PROTO_40;
      end = (char *)protocol->get_net()->read_pos +
            AUTH_PACKET_HEADER_SIZE_PROTO_40;
      bytes_remaining_in_packet -= AUTH_PACKET_HEADER_SIZE_PROTO_40;
      /**
        Old clients didn't have their own charset. Instead the assumption
        was that they used what ever the server used.
      */
      ssl_charset_code = global_system_variables.character_set_client->number;
    }

    if (charset_code != ssl_charset_code || !packet_has_required_size)
      return packet_error;
  }

  DBUG_PRINT("info", ("client_character_set: %u", charset_code));
  if (mpvio->charset_adapter->init_client_charset(charset_code))
    return packet_error;

  if ((protocol->has_client_capability(CLIENT_TRANSACTIONS)) &&
      opt_using_transactions)
    protocol->get_net()->return_status = mpvio->server_status;

  /*
    The 4.0 and 4.1 versions of the protocol differ on how strings
    are terminated. In the 4.0 version, if a string is at the end
    of the packet, the string is not null terminated. Do not assume
    that the returned string is always null terminated.
  */
  get_proto_string_func_t get_string;

  if (protocol->has_client_capability(CLIENT_PROTOCOL_41))
    get_string = get_41_protocol_string;
  else
    get_string = get_40_protocol_string;

  /*
    When the ability to change default plugin require that the initial password
   field can be of arbitrary size. However, the 41 client-server protocol limits
   the length of the auth-data-field sent from client to server to 255 bytes
   (CLIENT_SECURE_CONNECTION). The solution is to change the type of the field
   to a true length encoded string and indicate the protocol change with a new
   client capability flag: CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA.
  */
  get_proto_string_func_t get_length_encoded_string;

  if (protocol->has_client_capability(CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA))
    get_length_encoded_string = get_56_lenc_string;
  else
    get_length_encoded_string = get_41_lenc_string;

  /*
    In order to safely scan a head for '\0' string terminators
    we must keep track of how many bytes remain in the allocated
    buffer or we might read past the end of the buffer.
  */
  bytes_remaining_in_packet =
      pkt_len - (end - (char *)protocol->get_net()->read_pos);

  size_t user_len;
  char *user = get_string(&end, &bytes_remaining_in_packet, &user_len);
  if (user == nullptr) return packet_error;

  /*
    Old clients send a null-terminated string as password; new clients send
    the size (1 byte) + string (not null-terminated). Hence in case of empty
    password both send '\0'.
  */
  size_t passwd_len = 0;
  char *passwd = nullptr;

  passwd =
      get_length_encoded_string(&end, &bytes_remaining_in_packet, &passwd_len);
  if (passwd == nullptr) return packet_error;

  size_t db_len = 0;
  char *db = nullptr;

  if (protocol->has_client_capability(CLIENT_CONNECT_WITH_DB)) {
    db = get_string(&end, &bytes_remaining_in_packet, &db_len);
    if (db == nullptr) return packet_error;
  }

  size_t client_plugin_len = 0;
  const char *client_plugin =
      get_string(&end, &bytes_remaining_in_packet, &client_plugin_len);
  if (client_plugin == nullptr) client_plugin = &empty_c_string[0];

  char db_buff[NAME_LEN + 1];           // buffer to store db in utf8
  char user_buff[USERNAME_LENGTH + 1];  // buffer to store user in utf8
  uint dummy_errors;

  /*
    Copy and convert the user and database names to the character set used
    by the server. Since 4.1 all database names are stored in UTF-8. Also,
    ensure that the names are properly null-terminated as this is relied
    upon later.
  */
  if (db) {
    db_len = copy_and_convert(db_buff, sizeof(db_buff) - 1, system_charset_info,
                              db, db_len, mpvio->charset_adapter->charset(),
                              &dummy_errors);
    db_buff[db_len] = '\0';
    db = db_buff;
  }

  user_len = copy_and_convert(user_buff, sizeof(user_buff) - 1,
                              system_charset_info, user, user_len,
                              mpvio->charset_adapter->charset(), &dummy_errors);
  user_buff[user_len] = '\0';
  user = user_buff;

  /* If username starts and ends in "'", chop them off */
  if (user_len > 1 && user[0] == '\'' && user[user_len - 1] == '\'') {
    user[user_len - 1] = 0;
    user++;
    user_len -= 2;
  }

  if (lex_string_strmake(mpvio->mem_root, &mpvio->db, db, db_len))
    return packet_error; /* The error is set by make_lex_string(). */
  if (mpvio->auth_info.user_name) my_free(mpvio->auth_info.user_name);
  if (!(mpvio->auth_info.user_name = my_strndup(key_memory_MPVIO_EXT_auth_info,
                                                user, user_len, MYF(MY_WME))))
    return packet_error; /* The error is set by my_strdup(). */
  mpvio->auth_info.user_name_length = user_len;

  if (find_mpvio_user(thd, mpvio)) return packet_error;

  if (!initialized) {
    // if mysqld's been started with --skip-grant-tables option
    mpvio->status = MPVIO_EXT::SUCCESS;
  }

  if (protocol->has_client_capability(CLIENT_CONNECT_ATTRS) &&
      read_client_connect_attrs(thd, &end, &bytes_remaining_in_packet, mpvio))
    return packet_error;

  NET_SERVER *ext = static_cast<NET_SERVER *>(protocol->get_net()->extension);
  struct compression_attributes *compression = &(ext->compression);
  bool is_client_supports_zlib =
      protocol->has_client_capability(CLIENT_COMPRESS);
  bool is_client_supports_zstd =
      protocol->has_client_capability(CLIENT_ZSTD_COMPRESSION_ALGORITHM);

  if (is_client_supports_zlib && is_server_supports_zlib) {
    strcpy(compression->compress_algorithm, COMPRESSION_ALGORITHM_ZLIB);
    /*
      for zlib compression method client does not send any compression level,
      set default compression level
    */
    compression->compress_level = 6;
  } else if (is_client_supports_zstd && is_server_supports_zstd) {
    strcpy(compression->compress_algorithm, COMPRESSION_ALGORITHM_ZSTD);
    compression->compress_level = (uint) * (end);
    end += 1;
    bytes_remaining_in_packet -= 1;
    if (!is_zstd_compression_level_valid(compression->compress_level)) {
      my_error(ER_WRONG_COMPRESSION_LEVEL_CLIENT, MYF(0),
               compression->compress_algorithm);
      mpvio->status = MPVIO_EXT::FAILURE;
      return CR_COMPRESSION_WRONGLY_CONFIGURED;
    }
  } else if (!compression->compression_optional) {
    /*
      if server is configured to only allow connections with compression enabled
      then check if client has enabled compression else report error
    */
    my_error(
        ER_WRONG_COMPRESSION_ALGORITHM_CLIENT, MYF(0),
        (compression->compress_algorithm[0] ? compression->compress_algorithm
                                            : "uncompressed"));

    mpvio->status = MPVIO_EXT::FAILURE;
    return CR_COMPRESSION_WRONGLY_CONFIGURED;
  }

  if (!(protocol->has_client_capability(CLIENT_PLUGIN_AUTH))) {
    /* An old client is connecting */
    client_plugin = Cached_authentication_plugins::get_plugin_name(
        PLUGIN_MYSQL_NATIVE_PASSWORD);
  }

  /*
    If the acl_user needs a different plugin to authenticate then
    the server default plugin we need to restart the authentication
    in the server.
    But perhaps the client has already used the correct plugin -
    in that case the authentication on the client may not need to be
    restarted and a server auth plugin will read the data that the client
    has just send. Cache them to return in the next server_mpvio_read_packet().
  */
  if (my_strcasecmp(system_charset_info, mpvio->acl_user_plugin.str,
                    plugin_name(mpvio->plugin)->str) != 0) {
    /* Server default plugin didn't match user plugin */
    mpvio->cached_client_reply.pkt = passwd;
    mpvio->cached_client_reply.pkt_len = passwd_len;
    mpvio->cached_client_reply.plugin = client_plugin;
    mpvio->status = MPVIO_EXT::RESTART;
    return packet_error;
  }

  /*
    ok, we don't need to restart the authentication on the server.
    but if the client used the wrong plugin, we need to restart
    the authentication on the client. Do it here, the server plugin
    doesn't need to know.
  */
  plugin_ref user_plugin =
      g_cached_authentication_plugins->get_cached_plugin_ref(
          &mpvio->acl_user_plugin);
  if (user_plugin == nullptr) return packet_error;
  auto user_client_plugin_name = client_plugin_name(user_plugin);
  if (my_strcasecmp(system_charset_info, client_plugin,
                    user_client_plugin_name)) {
    /*
      Client plugins don't match - send request to client to use a
      different plugin and restart authentication process.
    */
    mpvio->cached_client_reply.plugin = client_plugin;
    /*
      Inject error here for testing purpose.
      See auth_sec.server_send_client_plugin
    */
    DBUG_EXECUTE_IF("assert_authentication_roundtrips",
                    { return packet_error; });

    if (send_plugin_request_packet(mpvio,
                                   (uchar *)mpvio->cached_server_packet.pkt,
                                   mpvio->cached_server_packet.pkt_len))
      return packet_error;

    mpvio->protocol->read_packet();
    passwd_len = protocol->get_packet_length();
    passwd = (char *)protocol->get_net()->read_pos;
  }

  *buff = (uchar *)passwd;
  return passwd_len;
}

/**
  Wrap the extra auth data sent so that they can pass in the protocol.

  Check @ref page_protocol_connection_phase_packets_protocol_auth_more_data
  for the format description.

  @retval 0 ok
  @retval 1 error

  @param net         the network abstraction to use
  @param packet      data to transmit
  @param packet_len  length of packet

  @sa net_write_command, client_mpvio_write_packet

*/

static inline int wrap_plguin_data_into_proper_command(NET *net,
                                                       const uchar *packet,
                                                       int packet_len) {
  return net_write_command(net, 1, pointer_cast<const uchar *>(""), 0, packet,
                           packet_len);
}

/*
  Note: The following functions are declared inside extern "C" because
  they are used to initialize C structure MPVIO (see
  server_mpvio_initialize()).
*/

extern "C" {

/**
  vio->write_packet() callback method for server authentication plugins

  This function is called by a server authentication plugin, when it wants
  to send data to the client.

  It transparently wraps the data into a handshake packet,
  and handles plugin negotiation with the client. If necessary,
  it escapes the plugin data, if it starts with a mysql protocol packet byte.
*/
static int server_mpvio_write_packet(MYSQL_PLUGIN_VIO *param,
                                     const uchar *packet, int packet_len) {
  MPVIO_EXT *mpvio = (MPVIO_EXT *)param;
  int res;
  Protocol_classic *protocol = mpvio->protocol;

  DBUG_TRACE;
  /*
    Reset cached_client_reply if not an old client doing mysql_change_user,
    as this is where the password from COM_CHANGE_USER is stored.
  */
  if (!((!(protocol->has_client_capability(CLIENT_PLUGIN_AUTH))) &&
        mpvio->status == MPVIO_EXT::RESTART &&
        mpvio->cached_client_reply.plugin ==
            ((st_mysql_auth *)(plugin_decl(mpvio->plugin)->info))
                ->client_auth_plugin))
    mpvio->cached_client_reply.pkt = nullptr;
  /* for the 1st packet we wrap plugin data into the handshake packet */
  if (mpvio->packets_written == 0)
    res = send_server_handshake_packet(
        mpvio, pointer_cast<const char *>(packet), packet_len);
  else if (mpvio->status == MPVIO_EXT::RESTART) {
    /*
      Inject error here for testing purpose.
      See auth_sec.server_send_client_plugin
    */
    DBUG_EXECUTE_IF("assert_authentication_roundtrips", {
      return -1;  // Crash here.
    });
    res = send_plugin_request_packet(mpvio, packet, packet_len);
  } else if (mpvio->status == MPVIO_EXT::START_MFA) {
    res = send_auth_next_factor_packet(mpvio, packet, packet_len);
    /*
      reset the status to avoid sending AuthNextFactor again for the
      same factor authentication.
    */
    mpvio->status = MPVIO_EXT::FAILURE;
  } else
    res = wrap_plguin_data_into_proper_command(protocol->get_net(), packet,
                                               packet_len);
  mpvio->packets_written++;
  return res;
}

/**
  vio->read_packet() callback method for server authentication plugins

  This function is called by a server authentication plugin, when it wants
  to read data from the client.

  It transparently extracts the client plugin data, if embedded into
  a client authentication handshake packet, and handles plugin negotiation
  with the client, if necessary.

  RETURN
    -1          Protocol failure
    >= 0        Success and also the packet length
*/
static int server_mpvio_read_packet(MYSQL_PLUGIN_VIO *param, uchar **buf) {
  MPVIO_EXT *mpvio = (MPVIO_EXT *)param;
  Protocol_classic *protocol = mpvio->protocol;
  size_t pkt_len;

  DBUG_TRACE;
  if (mpvio->packets_written == 0) {
    /*
      plugin wants to read the data without sending anything first.
      send an empty packet to force a server handshake packet to be sent
    */
    if (mpvio->write_packet(mpvio, nullptr, 0))
      pkt_len = packet_error;
    else {
      protocol->read_packet();
      pkt_len = protocol->get_packet_length();
    }
  } else if (mpvio->cached_client_reply.pkt) {
    assert(mpvio->status == MPVIO_EXT::RESTART);
    assert(mpvio->packets_read > 0);
    /*
      If the data cached from the last server_mpvio_read_packet
      and a client has used the correct plugin, then we can return the
      cached data straight away and avoid one round trip.
    */

    auto client_auth_plugin_name = client_plugin_name(mpvio->plugin);
    if (client_auth_plugin_name == nullptr ||
        my_strcasecmp(system_charset_info, mpvio->cached_client_reply.plugin,
                      client_auth_plugin_name) == 0) {
      mpvio->status = MPVIO_EXT::FAILURE;
      *buf = const_cast<uchar *>(
          pointer_cast<const uchar *>(mpvio->cached_client_reply.pkt));
      mpvio->cached_client_reply.pkt = nullptr;
      mpvio->packets_read++;
      return (int)mpvio->cached_client_reply.pkt_len;
    }

    /* older clients don't support change of client plugin request */
    if (!(protocol->has_client_capability(CLIENT_PLUGIN_AUTH))) {
      mpvio->status = MPVIO_EXT::FAILURE;
      pkt_len = packet_error;
      goto err;
    }
    /*
      But if the client has used the wrong plugin, the cached data are
      useless. Furthermore, we have to send a "change plugin" request
      to the client.
    */
    if (mpvio->write_packet(mpvio, nullptr, 0))
      pkt_len = packet_error;
    else {
      protocol->read_packet();
      pkt_len = protocol->get_packet_length();
    }
  } else if (mpvio->status == MPVIO_EXT::START_MFA) {
    /* Send AuthNextFactor packet to client and change mpvio status */
    if (mpvio->write_packet(mpvio, nullptr, 0)) {
      pkt_len = packet_error;
    } else {
      protocol->read_packet();
      pkt_len = protocol->get_packet_length();
    }
  } else {
    protocol->read_packet();
    pkt_len = protocol->get_packet_length();
  }

  DBUG_EXECUTE_IF("simulate_packet_error", pkt_len = packet_error;);
  if (pkt_len == packet_error) goto err;

  mpvio->packets_read++;

  /*
    the 1st packet has the plugin data wrapped into the client authentication
    handshake packet
  */
  if (mpvio->packets_read == 1) {
    pkt_len = parse_client_handshake_packet(current_thd, mpvio, buf, pkt_len);
    if (pkt_len == packet_error) goto err;
  } else
    *buf = protocol->get_net()->read_pos;

  return (int)pkt_len;

err:
  if (mpvio->status == MPVIO_EXT::FAILURE) {
    my_error(ER_HANDSHAKE_ERROR, MYF(0));
  }
  return -1;
}

/**
  fills MYSQL_PLUGIN_VIO_INFO structure with the information about the
  connection
*/
static void server_mpvio_info(MYSQL_PLUGIN_VIO *vio,
                              MYSQL_PLUGIN_VIO_INFO *info) {
  MPVIO_EXT *mpvio = (MPVIO_EXT *)vio;
  mpvio_info(mpvio->protocol->get_net()->vio, info);
}

}  // extern "C"

static int do_auth_once(THD *thd, const LEX_CSTRING &auth_plugin_name,
                        MPVIO_EXT *mpvio) {
  DBUG_TRACE;
  int res = CR_OK, old_status = MPVIO_EXT::FAILURE;
  bool unlock_plugin = false;
  plugin_ref plugin =
      g_cached_authentication_plugins->get_cached_plugin_ref(&auth_plugin_name);

  if (!plugin) {
    if ((plugin = my_plugin_lock_by_name(thd, auth_plugin_name,
                                         MYSQL_AUTHENTICATION_PLUGIN)))
      unlock_plugin = true;
  }

  mpvio->plugin = plugin;
  old_status = mpvio->status;

  if (plugin) {
    st_mysql_auth *auth = (st_mysql_auth *)plugin_decl(plugin)->info;
    res = auth->authenticate_user(mpvio, &mpvio->auth_info);

    if (unlock_plugin) plugin_unlock(thd, plugin);
  } else {
    /* Server cannot load the required plugin. */
    Host_errors errors;
    errors.m_no_auth_plugin = 1;
    inc_host_errors(mpvio->ip, &errors);
    my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), auth_plugin_name.str);
    res = CR_ERROR;
  }

  /*
    If the status was MPVIO_EXT::RESTART before the authenticate_user() call
    it can never be MPVIO_EXT::RESTART after the call, because any call
    to write_packet() or read_packet() will reset the status.

    But (!) if a plugin never called a read_packet() or write_packet(), the
    status will stay unchanged. We'll fix it, by resetting the status here.
  */
  if (old_status == MPVIO_EXT::RESTART && mpvio->status == MPVIO_EXT::RESTART)
    mpvio->status = MPVIO_EXT::FAILURE;  // reset to the default

  return res;
}

/* clang-format off */
/**
  @page page_protocol_multi_factor_authentication_methods Multi Factor Authentication

  Assume the client wants to log in via user account U and that user account is defined
  with multiple authentication methods namely X,Y,Z. Assume default authentication method
  on both server and client is X.

  @section sect_protocol_multi_factor_authentication_phase Authentication

  A successful authentication path looks as follows:

  1. The client connects to the server
  2. The server sends @ref page_protocol_connection_phase_packets_protocol_handshake
  3. The client responds with
     @ref page_protocol_connection_phase_packets_protocol_handshake_response
  4. X authentication method packets are exchanged
  5. The server responds with an @ref page_protocol_connection_phase_packets_protocol_auth_next_factor_request
     containing client side plugin name and plugin data of plugin Y.
  6. Client reads plugin name and plugin data from AuthNextFactor packet and loads corresponding client side plugin.
  7. Y authentication method packets are exchanged
  8. The server responds with an @ref page_protocol_connection_phase_packets_protocol_auth_next_factor_request
     containing client side plugin name and plugin data of plugin Z.
  9. Client reads plugin name and plugin data from AuthNextFactor packet and loads corresponding client side plugin.
  10.Z authentication method packets are exchanged
  11.The server responds with an @ref page_protocol_basic_ok_packet

  @note At any point, if the Nth Authentication Method fails, the server can return ERR and disconnect.
    And the client can just disconnect.

  @startuml
  Client -> Server: Connect
  Server -> Client: Initial Handshake Packet
  Client -> Server: Handshake Response Packet

  == X authentication method packets are exchanged ==

  Server -> Client: AuthNextFactor packet containing plugin name/data of plugin Y

  == Y authentication method packets are exchanged ==

  Server -> Client: AuthNextFactor packet containing plugin name/data of plugin Z

  == Z authentication method packets are exchanged ==

  Server -> Client: OK packet

  == Client and server enter Command Phase ==
  @enduml
*/
/* clang-format on */

/**
  Perform 2nd and 3rd factor authentication.

  Once 1FA method succeeds, server checks if connecting user requires more
  authentication methods to do the authentication.

  Refer to @ref page_protocol_multi_factor_authentication_methods
  for server-client communication in various cases

  @param thd            thread handle
  @param mpvio          the communications channel

  @retval 0  success
  @retval 1  error
*/
static int do_multi_factor_auth(THD *thd, MPVIO_EXT *mpvio) {
  DBUG_TRACE;
  int res = CR_OK;
  /* user is not configured with Multi factor authentication */
  if (!mpvio->acl_user->m_mfa) return res;
  /*
    If an old client connects to server with user account created with Multi
    factor authentication methods, then return error.
  */
  if (!mpvio->protocol->has_client_capability(MULTI_FACTOR_AUTHENTICATION))
    return CR_AUTH_USER_CREDENTIALS;

  Multi_factor_auth_list *auth_factor =
      mpvio->acl_user->m_mfa->get_multi_factor_auth_list();
  for (auto m_it : auth_factor->get_mfa_list()) {
    Multi_factor_auth_info *af = m_it->get_multi_factor_auth_info();
    if (af->get_factor() == nthfactor::SECOND_FACTOR)
      mpvio->auth_info.current_auth_factor = 1;
    else if (af->get_factor() == nthfactor::THIRD_FACTOR)
      mpvio->auth_info.current_auth_factor = 2;
    /* reset cached_client_reply for 2nd and 3rd factors */
    mpvio->cached_client_reply.pkt = nullptr;
    plugin_ref plugin = my_plugin_lock_by_name(thd, af->plugin_name(),
                                               MYSQL_AUTHENTICATION_PLUGIN);
    if (plugin) {
      mpvio->plugin = plugin;
      /*
        Update auth_string, to refer to corresponding factors auth plugin
        credentials
      */
      mpvio->auth_info.auth_string =
          mpvio->auth_info
              .multi_factor_auth_info[mpvio->auth_info.current_auth_factor]
              .auth_string;
      mpvio->auth_info.auth_string_length =
          mpvio->auth_info
              .multi_factor_auth_info[mpvio->auth_info.current_auth_factor]
              .auth_string_length;
      mpvio->status = MPVIO_EXT::START_MFA;
      st_mysql_auth *auth = (st_mysql_auth *)plugin_decl(plugin)->info;
      res = auth->authenticate_user(mpvio, &mpvio->auth_info);
      if (res == CR_OK_AUTH_IN_SANDBOX_MODE) {
        /*
          Server allows user account to connect in case registration is
          required, and set server in sandbox mode.
        */
        if (af->get_requires_registration())
          thd->security_context()->set_registration_sandbox_mode(true);
        assert(af->get_requires_registration());
        plugin_unlock(thd, plugin);
        return CR_OK;
      }

      plugin_unlock(thd, plugin);
      if (res != CR_OK) {
        mpvio->status = MPVIO_EXT::FAILURE;
        break;
      } else {
        mpvio->status = MPVIO_EXT::SUCCESS;
      }
    } else {
      /* Server cannot load the required plugin. */
      Host_errors errors;
      errors.m_no_auth_plugin = 1;
      inc_host_errors(mpvio->ip, &errors);
      my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), af->get_plugin_str());
      res = CR_ERROR;
      break;
    }
  }
  return res;
}

static void server_mpvio_initialize(THD *thd, MPVIO_EXT *mpvio,
                                    Thd_charset_adapter *charset_adapter) {
  LEX_CSTRING sctx_host_or_ip = thd->security_context()->host_or_ip();

  memset(mpvio, 0, sizeof(MPVIO_EXT));
  mpvio->read_packet = server_mpvio_read_packet;
  mpvio->write_packet = server_mpvio_write_packet;
  mpvio->info = server_mpvio_info;
  mpvio->auth_info.user_name = nullptr;
  mpvio->auth_info.user_name_length = 0;
  mpvio->auth_info.host_or_ip = sctx_host_or_ip.str;
  mpvio->auth_info.host_or_ip_length = sctx_host_or_ip.length;
  mpvio->auth_info.password_used = PASSWORD_USED_NO;
  mpvio->auth_info.current_auth_factor = 0;

  Vio *vio = thd->get_protocol_classic()->get_vio();
  if (vio->ssl_arg)
    mpvio->vio_is_encrypted = 1;
  else
    mpvio->vio_is_encrypted = 0;
  mpvio->status = MPVIO_EXT::FAILURE;
  mpvio->mem_root = thd->mem_root;
  mpvio->scramble = thd->scramble;
  mpvio->rand = &thd->rand;
  mpvio->thread_id = thd->thread_id();
  mpvio->server_status = &thd->server_status;
  mpvio->protocol = thd->get_protocol_classic();
  mpvio->ip = thd->security_context()->ip().str;
  mpvio->host = thd->security_context()->host().str;
  mpvio->charset_adapter = charset_adapter;
  mpvio->restrictions = new (mpvio->mem_root) Restrictions();

  mpvio->auth_info.multi_factor_auth_info =
      new (mpvio->mem_root) auth_factor_desc[MAX_AUTH_FACTORS];
  memset(mpvio->auth_info.multi_factor_auth_info, 0, sizeof(auth_factor_desc));
}

static void server_mpvio_update_thd(THD *thd, MPVIO_EXT *mpvio) {
  thd->max_client_packet_length = mpvio->max_client_packet_length;
  if (mpvio->protocol->has_client_capability(CLIENT_INTERACTIVE))
    thd->variables.net_wait_timeout = thd->variables.net_interactive_timeout;
  thd->security_context()->assign_user(
      mpvio->auth_info.user_name,
      (mpvio->auth_info.user_name ? strlen(mpvio->auth_info.user_name) : 0));
  if (mpvio->acl_user) {
    thd->security_context()->lock_account(mpvio->acl_user->account_locked);
  }
  if (mpvio->auth_info.user_name) my_free(mpvio->auth_info.user_name);
  LEX_CSTRING sctx_user = thd->security_context()->user();
  mpvio->auth_info.user_name = const_cast<char *>(sctx_user.str);
  mpvio->auth_info.user_name_length = sctx_user.length;
  if (thd->get_protocol()->has_client_capability(CLIENT_IGNORE_SPACE))
    thd->variables.sql_mode |= MODE_IGNORE_SPACE;
}

/**
  Calculate the timestamp difference for password expiry

  @param thd			 thread handle
  @param acl_user		 ACL_USER handle

  @retval 0  password is valid
  @retval 1  password has expired
*/
static bool check_password_lifetime(THD *thd, const ACL_USER *acl_user) {
  bool password_time_expired = false;

  if (likely(acl_user != nullptr) && !acl_user->password_expired &&
      acl_user->password_last_changed.time_type != MYSQL_TIMESTAMP_ERROR &&
      auth_plugin_is_built_in(acl_user->plugin.str) &&
      (acl_user->use_default_password_lifetime ||
       acl_user->password_lifetime)) {
    MYSQL_TIME cur_time, password_change_by;
    Interval interval;

    thd->set_time();
    thd->variables.time_zone->gmt_sec_to_TIME(
        &cur_time, static_cast<my_time_t>(thd->query_start_in_secs()));
    password_change_by = acl_user->password_last_changed;
    memset(&interval, 0, sizeof(interval));

    if (!acl_user->use_default_password_lifetime)
      interval.day = acl_user->password_lifetime;
    else {
      MUTEX_LOCK(lock, &LOCK_default_password_lifetime);
      interval.day = default_password_lifetime;
    }
    if (interval.day) {
      if (!date_add_interval_with_warn(thd, &password_change_by, INTERVAL_DAY,
                                       interval))
        password_time_expired =
            my_time_compare(password_change_by, cur_time) >= 0 ? false : true;
      else {
        assert(false);
        /* Make the compiler happy. */
      }
    }
  }
  DBUG_EXECUTE_IF("force_password_interval_expire", {
    if (!acl_user->use_default_password_lifetime && acl_user->password_lifetime)
      password_time_expired = true;
  });
  DBUG_EXECUTE_IF("force_password_interval_expire_for_time_type", {
    if (acl_user->password_last_changed.time_type != MYSQL_TIMESTAMP_ERROR)
      password_time_expired = true;
  });
  return password_time_expired;
}

/**
Logging connection for the general query log, extracted from
acl_authenticate() as it's reused at different times based on
whether proxy users are checked.

@param user                    authentication user name
@param host                    authentication user host or IP address
@param auth_as                 privilege user name
@param db                      default database
@param thd                     thread handle
@param command                 type of command(connect or change user)
*/
void acl_log_connect(const char *user, const char *host, const char *auth_as,
                     const char *db, THD *thd,
                     enum enum_server_command command) {
  const char *vio_name_str = nullptr;
  int len = 0;
  get_vio_type_name(thd->get_vio_type(), &vio_name_str, &len);

  if (strcmp(auth_as, user) && (PROXY_FLAG != *auth_as)) {
    query_logger.general_log_print(thd, command, "%s@%s as %s on %s using %s",
                                   user, host, auth_as, db ? db : "",
                                   vio_name_str);
  } else {
    query_logger.general_log_print(thd, command, "%s@%s on %s using %s", user,
                                   host, db ? db : "", vio_name_str);
  }
}

/*
  Assign priv_user and priv_host fields of the Security_context.

  @param sctx Security context, which priv_user and priv_host fields are
              updated.
  @param user Authenticated user data.
*/
inline void assign_priv_user_host(Security_context *sctx, ACL_USER *user) {
  sctx->assign_priv_user(user->user, user->user ? strlen(user->user) : 0);
  sctx->assign_priv_host(user->host.get_host(), user->host.get_host_len());
}

/**
  Check that for command COM_CONNECT, either restriction on max number of
  concurrent connections  not violated or in case the connection is admin
  connection the user has required privilege.

  @param thd  Thread context

  @return Error status
    @retval false  success
    @retval true   error

  @note if connection is admin connection and a user doesn't have
  the privilege SERVICE_CONNECTION_ADMIN, the error
  ER_SPECIFIC_ACCESS_DENIED_ERROR is set in Diagnostics_area.

  @note if a user doesn't have any of the privileges SUPER_ACL,
  CONNECTION_ADMIN, SERVICE_CONNECTION_ADMIN and a number of concurrent
  connections exceeds the limit max_connections the error ER_CON_COUNT_ERROR
  is set in Diagnostics_area.
*/
static inline bool check_restrictions_for_com_connect_command(THD *thd) {
  if (thd->is_admin_connection() &&
      !thd->m_main_security_ctx
           .has_global_grant(STRING_WITH_LEN("SERVICE_CONNECTION_ADMIN"))
           .first) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SERVICE_CONNECTION_ADMIN");
    return true;
  }

  if (!(thd->m_main_security_ctx.check_access(SUPER_ACL) ||
        thd->m_main_security_ctx
            .has_global_grant(STRING_WITH_LEN("CONNECTION_ADMIN"))
            .first ||
        thd->m_main_security_ctx
            .has_global_grant(STRING_WITH_LEN("SERVICE_CONNECTION_ADMIN"))
            .first)) {
    if (!Connection_handler_manager::get_instance()
             ->valid_connection_count()) {  // too many connections
      my_error(ER_CON_COUNT_ERROR, MYF(0));
      return true;
    }
  }

  return false;
}

static void check_and_update_password_lock_state(MPVIO_EXT &mpvio, THD *thd,
                                                 int &res) {
  if (mpvio.acl_user && initialized &&
      mpvio.acl_user->password_locked_state.is_active()) {
    /* update user lock status and check if the account is locked */
    Acl_cache_lock_guard acl_cache_lock(thd, Acl_cache_lock_mode::READ_MODE);

    acl_cache_lock.lock();
    const ACL_USER *acl_user = mpvio.acl_user;

    ACL_USER *acl_user_ptr = find_acl_user(
        acl_user->host.get_host(), acl_user->user ? acl_user->user : "", true);
    long days_remaining = 0;
    assert(acl_user_ptr != nullptr);
    if (acl_user_ptr && acl_user_ptr->password_locked_state.update(
                            thd, res == CR_OK, &days_remaining)) {
      uint failed_logins =
          acl_user_ptr->password_locked_state.get_failed_login_attempts();
      int blocked_for_days =
          acl_user_ptr->password_locked_state.get_password_lock_time_days();
      acl_cache_lock.unlock();
      char str_blocked_for_days[30], str_days_remaining[30];
      if (blocked_for_days > 0)
        snprintf(str_blocked_for_days, sizeof(str_blocked_for_days), "%d",
                 blocked_for_days);
      else
        strncpy(str_blocked_for_days, "unlimited",
                sizeof(str_blocked_for_days));
      if (days_remaining > 0)
        snprintf(str_days_remaining, sizeof(str_days_remaining), "%ld",
                 days_remaining);
      else
        strncpy(str_days_remaining, "unlimited", sizeof(str_days_remaining));

      my_error(ER_USER_ACCESS_DENIED_FOR_USER_ACCOUNT_BLOCKED_BY_PASSWORD_LOCK,
               MYF(0), mpvio.acl_user->user ? mpvio.acl_user->user : "",
               mpvio.auth_info.host_or_ip ? mpvio.auth_info.host_or_ip : "",
               str_blocked_for_days, str_days_remaining, failed_logins);
      LogErr(INFORMATION_LEVEL,
             ER_ACCESS_DENIED_FOR_USER_ACCOUNT_BLOCKED_BY_PASSWORD_LOCK,
             mpvio.acl_user->user ? mpvio.acl_user->user : "",
             mpvio.auth_info.host_or_ip ? mpvio.auth_info.host_or_ip : "",
             str_blocked_for_days, str_days_remaining, failed_logins);
      res = CR_ERROR;
    } else
      acl_cache_lock.unlock();
  }
}

/**
  Perform the handshake, authorize the client and update thd sctx variables.

  @param thd                     thread handle
  @param command                 the command to be executed, it can be either a
                                 COM_CHANGE_USER or COM_CONNECT (if
                                 it's a new connection)

  @retval 0  success, thd is updated.
  @retval 1  error
*/
int acl_authenticate(THD *thd, enum_server_command command) {
  int res = CR_OK;
  int ret = 1;
  MPVIO_EXT mpvio;
  LEX_CSTRING auth_plugin_name = default_auth_plugin_name;
  Thd_charset_adapter charset_adapter(thd);

  DBUG_TRACE;
  static_assert(MYSQL_USERNAME_LENGTH == USERNAME_LENGTH, "");
  assert(command == COM_CONNECT || command == COM_CHANGE_USER);

  DBUG_EXECUTE_IF("acl_authenticate_begin", {
    const char act[] =
        "now SIGNAL conn2_in_acl_auth WAIT_FOR conn1_reached_kill";
    assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
  });

  server_mpvio_initialize(thd, &mpvio, &charset_adapter);
  /*
    Clear thd->db as it points to something, that will be freed when
    connection is closed. We don't want to accidentally free a wrong
    pointer if connect failed.
  */
  thd->reset_db(NULL_CSTR);

  auth_plugin_name = default_auth_plugin_name;
  /* acl_authenticate() takes the data from net->read_pos */
  thd->get_protocol_classic()->get_net()->read_pos =
      thd->get_protocol_classic()->get_raw_packet();
  DBUG_PRINT("info", ("com_change_user_pkt_len=%lu",
                      mpvio.protocol->get_packet_length()));

  if (command == COM_CHANGE_USER) {
    mpvio.packets_written++;  // pretend that a server handshake packet was sent
    mpvio.packets_read++;     // take COM_CHANGE_USER packet into account

    /* Clear variables that are allocated */
    thd->set_user_connect(nullptr);

    if (parse_com_change_user_packet(thd, &mpvio,
                                     mpvio.protocol->get_packet_length())) {
      login_failed_error(thd, &mpvio, mpvio.auth_info.password_used);
      server_mpvio_update_thd(thd, &mpvio);
      goto end;
    }

    assert(mpvio.status == MPVIO_EXT::RESTART ||
           mpvio.status == MPVIO_EXT::SUCCESS);
  } else {
    /* mark the thd as having no scramble yet */
    mpvio.scramble[SCRAMBLE_LENGTH] = 1;

    /*
     perform the first authentication attempt, with the default plugin.
     This sends the server handshake packet, reads the client reply
     with a user name, and performs the authentication if everyone has used
     the correct plugin.
    */

    res = do_auth_once(thd, auth_plugin_name, &mpvio);
  }

  /*
   retry the authentication, if - after receiving the user name -
   we found that we need to switch to a non-default plugin
  */
  if (mpvio.status == MPVIO_EXT::RESTART) {
    assert(mpvio.acl_user);
    assert(command == COM_CHANGE_USER ||
           my_strcasecmp(system_charset_info, auth_plugin_name.str,
                         mpvio.acl_user->plugin.str));
    auth_plugin_name = mpvio.acl_user->plugin;
    res = do_auth_once(thd, auth_plugin_name, &mpvio);
  }

  if (res == CR_OK) {
    res = do_multi_factor_auth(thd, &mpvio);
  }

  server_mpvio_update_thd(thd, &mpvio);

  check_and_update_password_lock_state(mpvio, thd, res);
#ifdef HAVE_PSI_THREAD_INTERFACE
  PSI_THREAD_CALL(set_connection_type)(thd->get_vio_type());
#endif /* HAVE_PSI_THREAD_INTERFACE */

  {
    Security_context *sctx = thd->security_context();
    const ACL_USER *acl_user = mpvio.acl_user;
    bool proxy_check = check_proxy_users && !*mpvio.auth_info.authenticated_as;

    DBUG_PRINT("info", ("proxy_check=%s", proxy_check ? "true" : "false"));

    thd->password =
        mpvio.auth_info.password_used;  // remember for error messages

    // reset authenticated_as because flag value received, but server
    // proxy mapping is disabled:
    if ((!check_proxy_users) && acl_user &&
        !*mpvio.auth_info.authenticated_as) {
      DBUG_PRINT("info",
                 ("setting authenticated_as to %s as check_proxy_user is OFF.",
                  mpvio.auth_info.user_name));
      strcpy(mpvio.auth_info.authenticated_as,
             acl_user->user ? acl_user->user : "");
    }
    /*
      Log the command here so that the user can check the log
      for the tried logins and also to detect break-in attempts.

      if sctx->user is unset it's protocol failure, bad packet.
    */
    if (mpvio.auth_info.user_name && !proxy_check) {
      acl_log_connect(mpvio.auth_info.user_name, mpvio.auth_info.host_or_ip,
                      mpvio.auth_info.authenticated_as, mpvio.db.str, thd,
                      command);
    }
    if (res == CR_OK && (!mpvio.can_authenticate() || thd->is_error())) {
      res = CR_ERROR;
    }

    /*
      Assign account user/host data to the current THD. This information is
      used when the authentication fails after this point and we call audit
      api notification event. Client user/host connects to the existing
      account is easily distinguished from other connects.
    */
    if (mpvio.can_authenticate())
      assign_priv_user_host(sctx, const_cast<ACL_USER *>(acl_user));

    if (res > CR_OK && mpvio.status != MPVIO_EXT::SUCCESS) {
      Host_errors errors;
      assert(mpvio.status == MPVIO_EXT::FAILURE);
      switch (res) {
        case CR_AUTH_PLUGIN_ERROR:
          errors.m_auth_plugin = 1;
          break;
        case CR_AUTH_HANDSHAKE:
          errors.m_handshake = 1;
          break;
        case CR_AUTH_USER_CREDENTIALS:
          errors.m_authentication = 1;
          break;
        case CR_ERROR:
        default:
          /* Unknown of unspecified auth plugin error. */
          errors.m_auth_plugin = 1;
          break;
      }
      inc_host_errors(mpvio.ip, &errors);
      if (mpvio.auth_info.user_name && proxy_check) {
        acl_log_connect(mpvio.auth_info.user_name, mpvio.auth_info.host_or_ip,
                        mpvio.auth_info.authenticated_as, mpvio.db.str, thd,
                        command);
      }
      login_failed_error(thd, &mpvio, mpvio.auth_info.password_used);
      goto end;
    }

    sctx->assign_proxy_user("", 0);

    if (initialized)  // if not --skip-grant-tables
    {
      bool is_proxy_user = false;
      bool password_time_expired = false;
      const char *auth_user = acl_user->user ? acl_user->user : "";
      ACL_PROXY_USER *proxy_user;
      /* check if the user is allowed to proxy as another user */
      Acl_cache_lock_guard acl_cache_lock(thd, Acl_cache_lock_mode::READ_MODE);
      if (!acl_cache_lock.lock()) return 1;

      proxy_user =
          acl_find_proxy_user(auth_user, sctx->host().str, sctx->ip().str,
                              mpvio.auth_info.authenticated_as, &is_proxy_user);
      acl_cache_lock.unlock();
      if (mpvio.auth_info.user_name && proxy_check) {
        acl_log_connect(mpvio.auth_info.user_name, mpvio.auth_info.host_or_ip,
                        mpvio.auth_info.authenticated_as, mpvio.db.str, thd,
                        command);
      }

      if (thd->is_error()) return 1;

      if (is_proxy_user) {
        ACL_USER *acl_proxy_user;
        char proxy_user_buf[USERNAME_LENGTH + HOSTNAME_LENGTH + 6];

        /* we need to find the proxy user, but there was none */
        if (!proxy_user) {
          Host_errors errors;
          errors.m_proxy_user = 1;
          inc_host_errors(mpvio.ip, &errors);
          login_failed_error(thd, &mpvio, mpvio.auth_info.password_used);
          goto end;
        }

        snprintf(proxy_user_buf, sizeof(proxy_user_buf) - 1, "'%s'@'%s'",
                 auth_user,
                 acl_user->host.get_host() ? acl_user->host.get_host() : "");
        sctx->assign_proxy_user(proxy_user_buf, strlen(proxy_user_buf));

        /* we're proxying : find the proxy user definition */
        if (!acl_cache_lock.lock()) return 1;
        acl_proxy_user = find_acl_user(proxy_user->get_proxied_host()
                                           ? proxy_user->get_proxied_host()
                                           : "",
                                       mpvio.auth_info.authenticated_as, true);
        if (!acl_proxy_user) {
          Host_errors errors;
          errors.m_proxy_user_acl = 1;
          inc_host_errors(mpvio.ip, &errors);
          login_failed_error(thd, &mpvio, mpvio.auth_info.password_used);
          goto end;
        }
        acl_user = acl_proxy_user->copy(thd->mem_root);
        *(mpvio.restrictions) = acl_restrictions->find_restrictions(acl_user);

        DBUG_PRINT("info", ("User %s is a PROXY and will assume a PROXIED"
                            " identity %s",
                            auth_user, acl_user->user));
        acl_cache_lock.unlock();
      }
      assert(mpvio.restrictions);
      sctx->set_master_access(acl_user->access, *(mpvio.restrictions));
      assign_priv_user_host(sctx, const_cast<ACL_USER *>(acl_user));
      /* Assign default role */
      {
        List_of_auth_id_refs default_roles;
        if (!acl_cache_lock.lock()) return 1;
        Auth_id_ref authid = create_authid_from(acl_user);
        if (opt_always_activate_granted_roles) {
          activate_all_granted_and_mandatory_roles(acl_user, sctx);
        } else {
          /* The server policy is to only activate default roles */
          get_default_roles(authid, default_roles);
          List_of_auth_id_refs::iterator it = default_roles.begin();
          for (; it != default_roles.end(); ++it) {
            if (sctx->activate_role(it->first, it->second, true)) {
              std::string roleidstr = create_authid_str_from(*it);
              std::string authidstr = create_authid_str_from(acl_user);
              LogErr(WARNING_LEVEL, ER_AUTH_CANT_ACTIVATE_ROLE,
                     roleidstr.c_str(), authidstr.c_str());
            }
          }
        }

        acl_cache_lock.unlock();
      }
      sctx->checkout_access_maps();

      if (!thd->is_error() &&
          !(sctx->check_access(SUPER_ACL) ||
            sctx->has_global_grant(STRING_WITH_LEN("CONNECTION_ADMIN"))
                .first)) {
        if (mysqld_offline_mode()) {
          my_error(ER_SERVER_OFFLINE_MODE, MYF(0));
          goto end;
        }
      }

      /*
        OK. Let's check the SSL. Historically it was checked after the
        password, as an additional layer, not instead of the password (in
        which case it would've been a plugin too).
      */
      if (acl_check_ssl(thd, acl_user)) {
        Host_errors errors;
        errors.m_ssl = 1;
        inc_host_errors(mpvio.ip, &errors);
        login_failed_error(thd, &mpvio, thd->password);
        goto end;
      }

      /*
        Check whether the account has been locked.
      */
      if (unlikely(mpvio.acl_user->account_locked)) {
        locked_account_connection_count++;

        my_error(ER_ACCOUNT_HAS_BEEN_LOCKED, MYF(0), mpvio.acl_user->user,
                 mpvio.auth_info.host_or_ip);
        LogErr(INFORMATION_LEVEL, ER_ACCESS_DENIED_FOR_USER_ACCOUNT_LOCKED,
               mpvio.acl_user->user, mpvio.auth_info.host_or_ip);
        goto end;
      }

      DBUG_EXECUTE_IF("before_secure_transport_check", {
        const char act[] = "now SIGNAL kill_now WAIT_FOR killed";
        assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
      });

      /*
        The assumption here is that thd->active_vio and thd->net.vio are both
        the same at this point. We should not use thd->active_vio at any cost,
        as a KILL command can shutdown the active_vio i.e., making it a nullptr
        which would cause issues. Instead we check the net.vio type.
      */
      if (opt_require_secure_transport && thd->get_net()->vio != nullptr &&
          !is_secure_transport(thd->get_net()->vio->type)) {
        my_error(ER_SECURE_TRANSPORT_REQUIRED, MYF(0));
        goto end;
      }

      /* checking password_time_expire for connecting user */
      password_time_expired = check_password_lifetime(thd, mpvio.acl_user);

      if (unlikely(
              mpvio.acl_user &&
              (mpvio.acl_user->password_expired || password_time_expired) &&
              !(mpvio.protocol->has_client_capability(
                  CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS)) &&
              disconnect_on_expired_password)) {
        /*
          Clients that don't signal password expiration support
          get a connect error.
        */
        Host_errors errors;

        my_error(ER_MUST_CHANGE_PASSWORD_LOGIN, MYF(0));
        query_logger.general_log_print(
            thd, COM_CONNECT, "%s", ER_DEFAULT(ER_MUST_CHANGE_PASSWORD_LOGIN));
        LogErr(INFORMATION_LEVEL, ER_ACCOUNT_WITH_EXPIRED_PASSWORD,
               mpvio.acl_user->user, mpvio.auth_info.host_or_ip);

        errors.m_authentication = 1;
        inc_host_errors(mpvio.ip, &errors);
        goto end;
      }

      /* Don't allow the user to connect if he has done too many queries */
      if ((acl_user->user_resource.questions ||
           acl_user->user_resource.updates ||
           acl_user->user_resource.conn_per_hour ||
           acl_user->user_resource.user_conn ||
           global_system_variables.max_user_connections) &&
          get_or_create_user_conn(
              thd,
              (opt_old_style_user_limits ? sctx->user().str
                                         : sctx->priv_user().str),
              (opt_old_style_user_limits ? sctx->host_or_ip().str
                                         : sctx->priv_host().str),
              &acl_user->user_resource))
        goto end;  // The error is set by get_or_create_user_conn()

      /*
        We are copying the connected user's password expired flag to the
        security context. This allows proxy user to execute queries even if
        proxied user password expires.
      */
      sctx->set_password_expired(mpvio.acl_user->password_expired ||
                                 password_time_expired);
    } else {
      sctx->skip_grants();
      /*
        In case of --skip-grant-tables, we already would have set the MPVIO
        as SUCCESS, it means we are not interested in any of the error set
        in the diagnostic area, clear them.
      */
      thd->get_stmt_da()->reset_diagnostics_area();
    }

    const USER_CONN *uc;
    if ((uc = thd->get_user_connect()) &&
        (uc->user_resources.conn_per_hour || uc->user_resources.user_conn ||
         global_system_variables.max_user_connections) &&
        check_for_max_user_connections(thd, uc)) {
      goto end;  // The error is set in check_for_max_user_connections()
    }

    DBUG_PRINT("info", ("Capabilities: %lu  packet_length: %ld  Host: '%s'  "
                        "Login user: '%s' Priv_user: '%s'  Using password: %s "
                        "Access: %" PRIu32 "  db: '%s'",
                        thd->get_protocol()->get_client_capabilities(),
                        thd->max_client_packet_length, sctx->host_or_ip().str,
                        sctx->user().str, sctx->priv_user().str,
                        thd->password ? "yes" : "no", sctx->master_access(),
                        mpvio.db.str));

    if (command == COM_CONNECT &&
        check_restrictions_for_com_connect_command(thd)) {
      release_user_connection(thd);
      goto end;
    }

    /*
      This is the default access rights for the current database.  It's
      set to 0 here because we don't have an active database yet (and we
      may not have an active database to set.
    */
    sctx->cache_current_db_access(0);

    /* Change a database if necessary */
    if (mpvio.db.length) {
      if (mysql_change_db(thd, to_lex_cstring(mpvio.db), false)) {
        /* mysql_change_db() has pushed the error message. */
        release_user_connection(thd);
        Host_errors errors;
        errors.m_default_database = 1;
        inc_host_errors(mpvio.ip, &errors);
        login_failed_error(thd, &mpvio, mpvio.auth_info.password_used);
        goto end;
      }
    }

    if (mpvio.auth_info.external_user[0])
      sctx->assign_external_user(mpvio.auth_info.external_user,
                                 strlen(mpvio.auth_info.external_user));

    if (res == CR_OK_HANDSHAKE_COMPLETE)
      thd->get_stmt_da()->disable_status();
    else
      my_ok(thd);
#ifdef HAVE_PSI_THREAD_INTERFACE
    LEX_CSTRING main_sctx_user = thd->m_main_security_ctx.user();
    LEX_CSTRING main_sctx_host_or_ip = thd->m_main_security_ctx.host_or_ip();
    PSI_THREAD_CALL(set_thread_account)
    (main_sctx_user.str, main_sctx_user.length, main_sctx_host_or_ip.str,
     main_sctx_host_or_ip.length);
#endif /* HAVE_PSI_THREAD_INTERFACE */

    /*
      Turn ON the flag in THD iff the user is granted SYSTEM_USER privilege.
      We must set the flag after all required roles are activated.
    */
    set_system_user_flag(thd);
    // Update the flag in THD based on if the user is granted CONNECTION_ADMIN
    // privilege
    set_connection_admin_flag(thd);
  }
  ret = 0;
end:
  if (mpvio.restrictions) mpvio.restrictions->~Restrictions();
  /* Ready to handle queries */
  return ret;
}

bool is_secure_transport(int vio_type) {
  switch (vio_type) {
    case VIO_TYPE_SSL:
    case VIO_TYPE_SHARED_MEMORY:
    case VIO_TYPE_SOCKET:
      return true;
  }
  return false;
}

static void native_password_authentication_deprecation_warning() {
  static std::atomic<bool> warning_logged = false;

  if (warning_logged) return;

  /*
    Deprecate message for mysql_native_password plugin.
  */
  LogPluginErr(WARNING_LEVEL, ER_SERVER_WARN_DEPRECATED,
               Cached_authentication_plugins::get_plugin_name(
                   PLUGIN_MYSQL_NATIVE_PASSWORD),
               Cached_authentication_plugins::get_plugin_name(
                   PLUGIN_CACHING_SHA2_PASSWORD));

  warning_logged = true;
}

static int generate_native_password(char *outbuf, unsigned int *buflen,
                                    const char *inbuf, unsigned int inbuflen) {
  THD *thd = current_thd;

  native_password_authentication_deprecation_warning();

  if (!thd->m_disable_password_validation) {
    if (my_validate_password_policy(inbuf, inbuflen)) return 1;
  }
  /* for empty passwords */
  if (inbuflen == 0) {
    *buflen = 0;
    return 0;
  }
  char *buffer = (char *)my_malloc(PSI_NOT_INSTRUMENTED,
                                   SCRAMBLED_PASSWORD_CHAR_LENGTH + 1, MYF(0));
  if (buffer == nullptr) return 1;
  my_make_scrambled_password_sha1(buffer, inbuf, inbuflen);
  /*
    if buffer specified by server is smaller than the buffer given
    by plugin then return error
  */
  if (*buflen < strlen(buffer)) {
    my_free(buffer);
    return 1;
  }
  *buflen = SCRAMBLED_PASSWORD_CHAR_LENGTH;
  memcpy(outbuf, buffer, *buflen);
  my_free(buffer);
  return 0;
}

static int validate_native_password_hash(char *const inbuf,
                                         unsigned int buflen) {
  /* empty password is also valid */
  if ((buflen && buflen == SCRAMBLED_PASSWORD_CHAR_LENGTH && inbuf[0] == '*') ||
      buflen == 0)
    return 0;
  return 1;
}

static int set_native_salt(const char *password, unsigned int password_len,
                           unsigned char *salt, unsigned char *salt_len) {
  /* for empty passwords salt_len is 0 */
  if (password_len == 0)
    *salt_len = 0;
  else {
    if (password_len == SCRAMBLED_PASSWORD_CHAR_LENGTH) {
      get_salt_from_password(salt, password);
      *salt_len = SCRAMBLE_LENGTH;
    }
  }
  return 0;
}

static int generate_sha256_password(char *outbuf, unsigned int *buflen,
                                    const char *inbuf, unsigned int inbuflen) {
  /*
   Deprecate message for SHA-256 authentication plugin.
  */
  LogPluginErr(
      WARNING_LEVEL, ER_SERVER_WARN_DEPRECATED,
      Cached_authentication_plugins::get_plugin_name(PLUGIN_SHA256_PASSWORD),
      Cached_authentication_plugins::get_plugin_name(
          PLUGIN_CACHING_SHA2_PASSWORD));
  if (inbuflen > SHA256_PASSWORD_MAX_PASSWORD_LENGTH) return 1;

  THD *thd = current_thd;
  if (!thd->m_disable_password_validation) {
    if (my_validate_password_policy(inbuf, inbuflen)) return 1;
  }
  if (inbuflen == 0) {
    *buflen = 0;
    return 0;
  }
  char *buffer = (char *)my_malloc(PSI_NOT_INSTRUMENTED,
                                   CRYPT_MAX_PASSWORD_SIZE + 1, MYF(0));
  if (buffer == nullptr) return 1;
  my_make_scrambled_password(buffer, inbuf, inbuflen);
  memcpy(outbuf, buffer, CRYPT_MAX_PASSWORD_SIZE);
  /*
    if buffer specified by server is smaller than the buffer given
    by plugin then return error
  */
  if (*buflen < strlen(buffer)) {
    my_free(buffer);
    return 1;
  }
  *buflen = strlen(buffer);
  my_free(buffer);
  return 0;
}

static int validate_sha256_password_hash(char *const inbuf,
                                         unsigned int buflen) {
  if ((inbuf && inbuf[0] == '$' && inbuf[1] == '5' && inbuf[2] == '$' &&
       buflen < CRYPT_MAX_PASSWORD_SIZE + 1) ||
      buflen == 0)
    return 0;
  return 1;
}

static int set_sha256_salt(const char *password [[maybe_unused]],
                           unsigned int password_len [[maybe_unused]],
                           unsigned char *salt [[maybe_unused]],
                           unsigned char *salt_len) {
  *salt_len = 0;
  return 0;
}

/**
  Compare a clear text password with a stored hash for
  the native password plugin

  If the password is non-empty it calculates a hash from
  the cleartext and compares it with the supplied hash.

  if the password is empty checks if the hash is empty too.

  @arg hash              pointer to the hashed data
  @arg hash_length       length of the hashed data
  @arg cleartext         pointer to the clear text password
  @arg cleartext_length  length of the cleat text password
  @arg[out] is_error     non-zero in case of error extracting the salt
  @retval 0              the hash was created with that password
  @retval non-zero       the hash was created with a different password
*/
static int compare_native_password_with_hash(const char *hash,
                                             unsigned long hash_length,
                                             const char *cleartext,
                                             unsigned long cleartext_length,
                                             int *is_error) {
  DBUG_TRACE;

  char buffer[SCRAMBLED_PASSWORD_CHAR_LENGTH + 1];

  /** empty password results in an empty hash */
  if (!hash_length && !cleartext_length) return 0;

  assert(hash_length <= SCRAMBLED_PASSWORD_CHAR_LENGTH);

  /* calculate the hash from the clear text */
  my_make_scrambled_password_sha1(buffer, cleartext, cleartext_length);

  *is_error = 0;
  int result = memcmp(hash, buffer, SCRAMBLED_PASSWORD_CHAR_LENGTH);

  return result;
}

/* clang-format off */
/**
  @page page_protocol_connection_phase_authentication_methods_native_password_authentication Native Authentication

  Authentication::Native41:

  <ul>
  <li>
  The server name is *mysql_native_password*
  </li>
  <li>
  The client name is *mysql_native_password*
  </li>
  <li>
  Client side requires an 20-byte random challenge from server
  </li>
  <li>
  Client side sends a 20-byte response packet based on the algorithm described
  later.
  </li>
  </ul>

  @par "Requires" @ref CLIENT_RESERVED2 "CLIENT_SECURE_CONNECTION"

  @startuml
  Client<-Server: 20 byte random data
  Client->Server: 20 byte scrambled password
  @enduml

  This method fixes a 2 short-comings of the
  @ref page_protocol_connection_phase_authentication_methods_old_password_authentication

  1. using a tested, crypto-graphic hashing function (SHA1)
  2. knowing the content of the hash in the mysql.user table isn't enough
     to authenticate against the MySQL Server.

  The network packet content for the password is calculated by:
  ~~~~~
  SHA1( password ) XOR SHA1( "20-bytes random data from server" <concat> SHA1( SHA1( password ) ) )
  ~~~~~

  The following is stored into mysql.user.authentication_string
  ~~~~~
  SHA1( SHA1( password ) )
  ~~~~~

  @sa native_password_authenticate, native_password_auth_client,
  native_password_client_plugin, native_password_handler,
  check_scramble_sha1, compute_two_stage_sha1_hash, make_password_from_salt
*/
/* clang-format on */

/**
  MySQL Server Password Authentication Plugin

  In the MySQL authentication protocol:
  1. the server sends the random scramble to the client
  2. client sends the encrypted password back to the server
  3. the server checks the password.
*/
static int native_password_authenticate(MYSQL_PLUGIN_VIO *vio,
                                        MYSQL_SERVER_AUTH_INFO *info) {
  uchar *pkt;
  int pkt_len;
  MPVIO_EXT *mpvio = (MPVIO_EXT *)vio;

  DBUG_TRACE;

  native_password_authentication_deprecation_warning();

  /* generate the scramble, or reuse the old one */
  if (mpvio->scramble[SCRAMBLE_LENGTH])
    generate_user_salt(mpvio->scramble, SCRAMBLE_LENGTH + 1);

  /* send it to the client */
  if (mpvio->write_packet(mpvio, (uchar *)mpvio->scramble, SCRAMBLE_LENGTH + 1))
    return CR_AUTH_HANDSHAKE;

  /* reply and authenticate */

  /*
    <digression>
      This is more complex than it looks.

      The plugin (we) may be called right after the client was connected -
      and will need to send a scramble, read reply, authenticate.

      Or the plugin may be called after another plugin has sent a scramble,
      and read the reply. If the client has used the correct client-plugin,
      we won't need to read anything here from the client, the client
      has already sent a reply with everything we need for authentication.

      Or the plugin may be called after another plugin has sent a scramble,
      and read the reply, but the client has used the wrong client-plugin.
      We'll need to sent a "switch to another plugin" packet to the
      client and read the reply. "Use the short scramble" packet is a special
      case of "switch to another plugin" packet.

      Or, perhaps, the plugin may be called after another plugin has
      done the handshake but did not send a useful scramble. We'll need
      to send a scramble (and perhaps a "switch to another plugin" packet)
      and read the reply.

      Besides, a client may be an old one, that doesn't understand plugins.
      Or doesn't even understand 4.0 scramble.

      And we want to keep the same protocol on the wire  unless non-native
      plugins are involved.

      Anyway, it still looks simple from a plugin point of view:
      "send the scramble, read the reply and authenticate"
      All the magic is transparently handled by the server.
    </digression>
  */

  /* read the reply with the encrypted password */
  if ((pkt_len = mpvio->read_packet(mpvio, &pkt)) < 0) return CR_AUTH_HANDSHAKE;
  DBUG_PRINT("info", ("reply read : pkt_len=%d", pkt_len));

  DBUG_EXECUTE_IF("native_password_bad_reply", {
    /* This should cause a HANDSHAKE ERROR */
    pkt_len = 12;
  });
  if (mysql_native_password_proxy_users) {
    *info->authenticated_as = PROXY_FLAG;
    DBUG_PRINT("info", ("mysql_native_authentication_proxy_users is enabled, "
                        "setting authenticated_as to NULL"));
  }
  if (pkt_len == 0) {
    info->password_used = PASSWORD_USED_NO;
    return mpvio->acl_user->credentials[PRIMARY_CRED].m_salt_len != 0
               ? CR_AUTH_USER_CREDENTIALS
               : CR_OK;
  } else
    info->password_used = PASSWORD_USED_YES;
  bool second = false;
  if (pkt_len == SCRAMBLE_LENGTH) {
    if (!mpvio->acl_user->credentials[PRIMARY_CRED].m_salt_len ||
        check_scramble(pkt, mpvio->scramble,
                       mpvio->acl_user->credentials[PRIMARY_CRED].m_salt)) {
      second = true;
      if (!mpvio->acl_user->credentials[SECOND_CRED].m_salt_len ||
          check_scramble(pkt, mpvio->scramble,
                         mpvio->acl_user->credentials[SECOND_CRED].m_salt)) {
        return CR_AUTH_USER_CREDENTIALS;
      } else {
        if (second) {
          MPVIO_EXT *mpvio_second = pointer_cast<MPVIO_EXT *>(vio);
          const char *username =
              *info->authenticated_as ? info->authenticated_as : "";
          const char *hostname = mpvio_second->acl_user->host.get_host();
          LogPluginErr(
              INFORMATION_LEVEL,
              ER_MYSQL_NATIVE_PASSWORD_SECOND_PASSWORD_USED_INFORMATION,
              username, hostname ? hostname : "");
        }
        return CR_OK;
      }
    } else {
      return CR_OK;
    }
  }

  my_error(ER_HANDSHAKE_ERROR, MYF(0));
  return CR_AUTH_HANDSHAKE;
}

/**
  Interface for querying the MYSQL_PUBLIC_VIO about encryption state.

*/

static int my_vio_is_encrypted(MYSQL_PLUGIN_VIO *vio) {
  MPVIO_EXT *mpvio = (MPVIO_EXT *)vio;
  return (mpvio->vio_is_encrypted);
}

/*
  The unused parameters must be here due to function pointer casting
  in sql_show.cc.
*/
int show_rsa_public_key(THD *, SHOW_VAR *var [[maybe_unused]], char *) {
  var->type = SHOW_CHAR;
  var->value = const_cast<char *>(g_sha256_rsa_keys->get_public_key_as_pem());
  return 0;
}

void deinit_rsa_keys(void) {
  if (g_sha256_rsa_keys) {
    g_sha256_rsa_keys->free_memory();
    delete g_sha256_rsa_keys;
    g_sha256_rsa_keys = nullptr;
  }
  if (g_caching_sha2_rsa_keys) {
    g_caching_sha2_rsa_keys->free_memory();
    delete g_caching_sha2_rsa_keys;
    g_caching_sha2_rsa_keys = nullptr;
  }
}

// Wraps a FILE handle, to ensure we always close it when returning.
class FileCloser {
  FILE *m_file;

 public:
  FileCloser(FILE *to_be_closed) : m_file(to_be_closed) {}
  ~FileCloser() {
    if (m_file != nullptr) fclose(m_file);
  }
};

/**
  Loads the RSA key pair from disk and store them in a global variable.

 @see init_ssl()

 @return Error code
   @retval false Success
   @retval true Error
*/

bool init_rsa_keys(void) {
  if ((strcmp(auth_rsa_private_key_path, AUTH_DEFAULT_RSA_PRIVATE_KEY) == 0 &&
       strcmp(auth_rsa_public_key_path, AUTH_DEFAULT_RSA_PUBLIC_KEY) == 0) ||
      (strcmp(caching_sha2_rsa_private_key_path,
              AUTH_DEFAULT_RSA_PRIVATE_KEY) == 0 &&
       strcmp(caching_sha2_rsa_public_key_path, AUTH_DEFAULT_RSA_PUBLIC_KEY) ==
           0)) {
    /**
      Presence of only a private key file and a public temp file implies that
      server crashed after creating the private key file and could not create a
      public key file. Hence removing the private key file.
    */
    if (access(AUTH_DEFAULT_RSA_PRIVATE_KEY, F_OK) == 0 &&
        access(AUTH_DEFAULT_RSA_PUBLIC_KEY, F_OK) == -1) {
      if (access((std::string{AUTH_DEFAULT_RSA_PUBLIC_KEY} + ".temp").c_str(),
                 F_OK) == 0 &&
          access((std::string{AUTH_DEFAULT_RSA_PRIVATE_KEY} + ".temp").c_str(),
                 F_OK) == -1)
        remove(AUTH_DEFAULT_RSA_PRIVATE_KEY);
    }
    // Removing temp files
    remove((std::string{AUTH_DEFAULT_RSA_PRIVATE_KEY} + ".temp").c_str());
    remove((std::string{AUTH_DEFAULT_RSA_PUBLIC_KEY} + ".temp").c_str());
  }

  if (!do_auto_rsa_keys_generation()) return true;

  if (!(g_sha256_rsa_keys = new Rsa_authentication_keys(
            &auth_rsa_private_key_path, &auth_rsa_public_key_path)))
    return true;
  if (!(g_caching_sha2_rsa_keys =
            new Rsa_authentication_keys(&caching_sha2_rsa_private_key_path,
                                        &caching_sha2_rsa_public_key_path))) {
    delete g_sha256_rsa_keys;
    g_sha256_rsa_keys = nullptr;
    return true;
  }

  return (g_sha256_rsa_keys->read_rsa_keys() ||
          g_caching_sha2_rsa_keys->read_rsa_keys());
}

static MYSQL_PLUGIN plugin_info_ptr;

static int init_sha256_password_handler(MYSQL_PLUGIN plugin_ref) {
  plugin_info_ptr = plugin_ref;
  return 0;
}

/**

 @param vio Virtual input-, output interface
 @param scramble - Scramble to be saved

 Save the scramble in mpvio for future re-use.
 It is useful when we need to pass the scramble to another plugin.
 Especially in case when old 5.1 client with no CLIENT_PLUGIN_AUTH capability
 tries to connect to server with default-authentication-plugin set to
 sha256_password

*/

void static inline auth_save_scramble(MYSQL_PLUGIN_VIO *vio,
                                      const char *scramble) {
  MPVIO_EXT *mpvio = (MPVIO_EXT *)vio;
  strncpy(mpvio->scramble, scramble, SCRAMBLE_LENGTH + 1);
}

/**
  Compare a clear text password with a stored hash

  Checks if a stored hash is produced using a clear text password.
  To do that first it extracts the scramble from the hash. Then
  calculates a new hash using the extracted scramble and the supplied
  password. And finally compares the two scrambles.

  @arg hash              pointer to the hashed data
  @arg hash_length       length of the hashed data
  @arg cleartext         pointer to the clear text password
  @arg cleartext_length  length of the cleat text password
  @arg[out] is_error     non-zero in case of error extracting the salt
  @retval 0              the hash was created with that password
  @retval non-zero       the hash was created with a different password
*/
static int compare_sha256_password_with_hash(const char *hash,
                                             unsigned long hash_length,
                                             const char *cleartext,
                                             unsigned long cleartext_length,
                                             int *is_error) {
  char stage2[CRYPT_MAX_PASSWORD_SIZE + 1];
  const char *user_salt_begin;
  const char *user_salt_end;

  DBUG_TRACE;
  assert(cleartext_length <= SHA256_PASSWORD_MAX_PASSWORD_LENGTH);

  if (cleartext_length > SHA256_PASSWORD_MAX_PASSWORD_LENGTH) return -1;

  /*
    Fetch user authentication_string and extract the password salt
  */
  user_salt_begin = hash;
  user_salt_end = hash + hash_length;
  if (extract_user_salt(&user_salt_begin, &user_salt_end) !=
      CRYPT_SALT_LENGTH) {
    *is_error = 1;
    return -1;
  }

  *is_error = 0;

  /* Create hash digest */
  my_crypt_genhash(stage2, CRYPT_MAX_PASSWORD_SIZE, cleartext, cleartext_length,
                   user_salt_begin, (const char **)nullptr);

  /* Compare the newly created hash digest with the password record */
  int result = memcmp(hash, stage2, hash_length);

  return result;
}

#undef LOG_COMPONENT_TAG
#define LOG_COMPONENT_TAG "sha256_password"

/**

 @param vio Virtual input-, output interface
 @param [out] info Connection information

 Authenticate the user by receiving a RSA or TLS encrypted password and
 calculate a hash digest which should correspond to the user record digest

 RSA keys are assumed to be pre-generated and supplied when server starts. If
 the client hasn't got a public key it can request one.

 TLS certificates and keys are assumed to be pre-generated and supplied when
 server starts.

*/

static int sha256_password_authenticate(MYSQL_PLUGIN_VIO *vio,
                                        MYSQL_SERVER_AUTH_INFO *info) {
  char scramble[SCRAMBLE_LENGTH + 1];
  uchar *pkt;
  int pkt_len;
  String scramble_response_packet;
  int cipher_length = 0;
  unsigned char plain_text[MAX_CIPHER_LENGTH + 1];
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  EVP_PKEY *private_key = nullptr;
  EVP_PKEY *public_key = nullptr;
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  RSA *private_key = nullptr;
  RSA *public_key = nullptr;
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

  DBUG_TRACE;

  /*
   Deprecate message for SHA-256 authentication plugin.
  */
  LogPluginErr(
      WARNING_LEVEL, ER_SERVER_WARN_DEPRECATED,
      Cached_authentication_plugins::get_plugin_name(PLUGIN_SHA256_PASSWORD),
      Cached_authentication_plugins::get_plugin_name(
          PLUGIN_CACHING_SHA2_PASSWORD));
  generate_user_salt(scramble, SCRAMBLE_LENGTH + 1);

  /*
    Note: The nonce is split into 8 + 12 bytes according to
http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeV10
    Native authentication sent 20 bytes + '\0' character = 21 bytes.
    This plugin must do the same to stay consistent with historical behavior
    if it is set to operate as a default plugin.
  */
  if (vio->write_packet(vio, (unsigned char *)scramble, SCRAMBLE_LENGTH + 1))
    return CR_ERROR;

  /*
    Save the scramble so it could be used by native plugin in case
    the authentication on the server side needs to be restarted
  */
  auth_save_scramble(vio, scramble);

  /*
    After the call to read_packet() the user name will appear in
    mpvio->acl_user and info will contain current data.
  */
  if ((pkt_len = vio->read_packet(vio, &pkt)) == -1) return CR_ERROR;

  /*
    If first packet is a 0 byte then the client isn't sending any password
    else the client will send a password.

    The original intention was that the password is a string[NUL] but this
    never got enforced properly so now we have to accept that an empty packet
    is a blank password, thus the check for pkt_len == 0 has to be made too.
  */
  if ((pkt_len == 0 || pkt_len == 1) && *pkt == 0) {
    info->password_used = PASSWORD_USED_NO;
    /*
      Send OK signal; the authentication might still be rejected based on
      host mask.
    */
    if (info->auth_string_length == 0) {
      if (sha256_password_proxy_users) {
        *info->authenticated_as = PROXY_FLAG;
        DBUG_PRINT("info", ("sha256_password_proxy_users is enabled \
                             , setting authenticated_as to NULL"));
      }
      return CR_OK;
    } else
      return CR_ERROR;
  } else
    info->password_used = PASSWORD_USED_YES;

  if (!my_vio_is_encrypted(vio)) {
    /*
      Since a password is being used it must be encrypted by RSA since no
      other encryption is being active.
    */
    private_key = g_sha256_rsa_keys->get_private_key();
    public_key = g_sha256_rsa_keys->get_public_key();

    /*
      Without the keys encryption isn't possible.
    */
    if (private_key == nullptr || public_key == nullptr) {
      LogPluginErr(ERROR_LEVEL, ER_SHA_PWD_AUTH_REQUIRES_RSA_OR_SSL);
      return CR_ERROR;
    }

    if ((cipher_length = g_sha256_rsa_keys->get_cipher_length()) >
        MAX_CIPHER_LENGTH) {
      LogPluginErr(ERROR_LEVEL, ER_SHA_PWD_RSA_KEY_TOO_LONG,
                   g_sha256_rsa_keys->get_cipher_length(), MAX_CIPHER_LENGTH);
      return CR_ERROR;
    }

    /*
      Client sent a "public key request"-packet ?
      If the first packet is 1 then the client will require a public key
      before encrypting the password.
    */
    if (pkt_len == 1 && *pkt == 1) {
      uint pem_length =
          static_cast<uint>(strlen(g_sha256_rsa_keys->get_public_key_as_pem()));
      if (vio->write_packet(vio,
                            pointer_cast<const uchar *>(
                                g_sha256_rsa_keys->get_public_key_as_pem()),
                            pem_length))
        return CR_ERROR;
      /* Get the encrypted response from the client */
      if ((pkt_len = vio->read_packet(vio, &pkt)) == -1) return CR_ERROR;
    }

    /*
      The packet will contain the cipher used. The length of the packet
      must correspond to the expected cipher length.
    */
    if (pkt_len != cipher_length) return CR_ERROR;

    /* Decrypt password */
    if (decrypt_RSA_private_key(pkt, cipher_length, plain_text,
                                sizeof(plain_text) - 1, private_key))
      return CR_ERROR;

    plain_text[cipher_length] = '\0';  // safety
    xor_string((char *)plain_text, cipher_length, (char *)scramble,
               SCRAMBLE_LENGTH);

    /*
      Set packet pointers and length for the hash digest function below
    */
    pkt = plain_text;
    pkt_len = strlen((char *)plain_text) + 1;  // include \0 intentionally.

    if (pkt_len == 1) return CR_ERROR;
  }  // if(!my_vio_is_encrypted())

  /* Don't process the password if it is longer than maximum limit */
  if (pkt_len > SHA256_PASSWORD_MAX_PASSWORD_LENGTH + 1) return CR_ERROR;

  /* A password was sent to an account without a password */
  if (info->auth_string_length == 0 && info->additional_auth_string_length == 0)
    return CR_ERROR;

  int is_error = 0;
  int result = compare_sha256_password_with_hash(
      info->auth_string, info->auth_string_length, (const char *)pkt,
      pkt_len - 1, &is_error);

  if (is_error) {
    /* User salt is not correct */
    LogPluginErr(ERROR_LEVEL, ER_SHA_PWD_SALT_FOR_USER_CORRUPT,
                 info->user_name);
    return CR_ERROR;
  }

  if (result && info->additional_auth_string_length) {
    result = compare_sha256_password_with_hash(
        info->additional_auth_string, info->additional_auth_string_length,
        (const char *)pkt, pkt_len - 1, &is_error);
    if (is_error) {
      /* User salt is not correct */
      LogPluginErr(ERROR_LEVEL, ER_SHA_PWD_SALT_FOR_USER_CORRUPT,
                   info->user_name);
      return CR_ERROR;
    }
    if (result == 0) {
      MPVIO_EXT *mpvio = (MPVIO_EXT *)vio;
      const char *username =
          *info->authenticated_as ? info->authenticated_as : "";
      const char *hostname = mpvio->acl_user->host.get_host();
      LogPluginErr(INFORMATION_LEVEL,
                   ER_SHA256_PASSWORD_SECOND_PASSWORD_USED_INFORMATION,
                   username, hostname ? hostname : "");
    }
  }

  if (result == 0) {
    if (sha256_password_proxy_users) {
      *info->authenticated_as = PROXY_FLAG;
      DBUG_PRINT("info", ("mysql_native_authentication_proxy_users is enabled \
						   , setting authenticated_as to NULL"));
    }
    return CR_OK;
  }

  return CR_ERROR;
}

static MYSQL_SYSVAR_STR(
    private_key_path, auth_rsa_private_key_path,
    PLUGIN_VAR_READONLY | PLUGIN_VAR_NOPERSIST,
    "A fully qualified path to the private RSA key used for authentication",
    nullptr, nullptr, AUTH_DEFAULT_RSA_PRIVATE_KEY);
static MYSQL_SYSVAR_STR(
    public_key_path, auth_rsa_public_key_path,
    PLUGIN_VAR_READONLY | PLUGIN_VAR_NOPERSIST,
    "A fully qualified path to the public RSA key used for authentication",
    nullptr, nullptr, AUTH_DEFAULT_RSA_PUBLIC_KEY);
static MYSQL_SYSVAR_BOOL(
    auto_generate_rsa_keys, auth_rsa_auto_generate_rsa_keys,
    PLUGIN_VAR_READONLY | PLUGIN_VAR_OPCMDARG | PLUGIN_VAR_NOPERSIST,
    "Auto generate RSA keys at server startup if corresponding "
    "system variables are not specified and key files are not present "
    "at the default location.",
    nullptr, nullptr, true);

static SYS_VAR *sha256_password_sysvars[] = {
    MYSQL_SYSVAR(private_key_path), MYSQL_SYSVAR(public_key_path),
    MYSQL_SYSVAR(auto_generate_rsa_keys), nullptr};

typedef std::string Sql_string_t;

/**
  Exception free resize

  @param [in,out] content string handle
  @param [in] size New size

  @retval false  Error
  @retval true  Successfully resized
*/
static bool resize_no_exception(Sql_string_t &content, size_t size) {
  try {
    content.resize(size);
  } catch (const std::length_error &) {
    return false;
  } catch (std::bad_alloc &) {
    return false;
  }
  return true;
}

/**

  FILE_IO : Wrapper around std::fstream
  1> Provides READ/WRITE handle to a file
  2> Records error on READ/WRITE operations
  3> Closes file before destruction

*/

class File_IO {
 public:
  File_IO(const File_IO &src)
      : m_file_name(src.file_name()),
        m_read(src.read_mode()),
        m_error_state(src.get_error()),
        m_file(-1) {
    file_open();
  }

  File_IO &operator=(const File_IO &src) {
    m_file_name = src.file_name();
    m_read = src.read_mode();
    file_open();
    return *this;
  }

  ~File_IO() { close(); }

  /*
    Close an already open file.
  */
  void close() {
    if (file_is_open()) {
      my_close(m_file, MYF(MY_WME));
      m_file = -1;
    }
  }

  /*
    Get name of the file. Used by copy constructor
  */
  const Sql_string_t &file_name() const { return m_file_name; }

  /*
    Get file IO mode. Used by copy constructor.
  */
  bool read_mode() const { return m_read; }

  /*
    Get READ/WRITE error status.
  */
  bool get_error() const { return m_error_state; }

  /*
    Set error. Used by >> and << functions.
  */
  void set_error() { m_error_state = true; }

  void reset_error() { m_error_state = false; }

  File_IO &operator>>(Sql_string_t &s);
  File_IO &operator<<(const Sql_string_t &output_string);

 protected:
  File_IO() = default;
  File_IO(const Sql_string_t filename, bool read)
      : m_file_name(filename), m_read(read), m_error_state(false), m_file(-1) {
    file_open();
  }

  /**
    A constructor to create the class with the right umask mode
    @param filename name of the file
    @param mode the create attributes to pass to my_create()
  */
  File_IO(const Sql_string_t filename, MY_MODE mode)
      : m_file_name(filename), m_read(false), m_error_state(false), m_file(-1) {
    m_file = my_create(m_file_name.c_str(), mode, O_WRONLY, MYF(MY_WME));
  }

  void file_open() {
    m_file =
        my_open(m_file_name.c_str(),
                m_read ? O_RDONLY : O_WRONLY | O_TRUNC | O_CREAT, MYF(MY_WME));
  }

  bool file_is_open() { return m_file >= 0; }

 private:
  Sql_string_t m_file_name;
  bool m_read;
  bool m_error_state;
  File m_file;
  /* Only File_creator can create File_IO */
  friend class File_creator;
};

/**
  Read an open file.

  @param [out] s String buffer

  Assumption : Caller will free string buffer

  @returns File_IO reference. Optionally sets error.
*/
File_IO &File_IO::operator>>(Sql_string_t &s) {
  assert(read_mode() && file_is_open());

  my_off_t off = my_seek(m_file, 0, SEEK_END, MYF(MY_WME));
  if (off == MY_FILEPOS_ERROR || resize_no_exception(s, off) == false)
    set_error();
  else {
    if (MY_FILEPOS_ERROR == my_seek(m_file, 0, SEEK_SET, MYF(MY_WME)) ||
        (size_t)-1 ==
            my_read(m_file, reinterpret_cast<uchar *>(&s[0]), s.size(), MYF(0)))
      set_error();
    close();
  }
  return *this;
}

/**
  Write into an open file

  @param [in] output_string Content to be written

  Assumption : string must be non-empty.

  @returns File_IO reference. Optionally sets error.
*/
File_IO &File_IO::operator<<(const Sql_string_t &output_string) {
  assert(!read_mode() && file_is_open());

  if (!output_string.size() ||
      MY_FILE_ERROR ==
          my_write(m_file,
                   reinterpret_cast<const uchar *>(output_string.data()),
                   output_string.length(), MYF(MY_NABP | MY_WME)))
    set_error();
  else
    my_sync(m_file, MYF(MY_WME));

  close();
  return *this;
}

/*
  Helper class to create a File_IO handle.
  Can be extended in future to set more file specific properties.
  Frees allocated memory in destructor.
*/
class File_creator {
 public:
  File_creator() = default;

  ~File_creator() {
    for (std::vector<File_IO *>::iterator it = m_file_vector.begin();
         it != m_file_vector.end(); ++it)
      delete (*it);
  }

  /*
    Note : Do not free memory.
  */
  File_IO *operator()(const Sql_string_t filename, bool read = false) {
    File_IO *f = new File_IO(filename, read);
    m_file_vector.push_back(f);
    return f;
  }

  /*
    Note : Do not free memory.
  */
  File_IO *operator()(const Sql_string_t filename, MY_MODE mode) {
    File_IO *f = new File_IO(filename, mode);
    m_file_vector.push_back(f);
    return f;
  }

 private:
  std::vector<File_IO *> m_file_vector;
};

/*
  This class encapsulates OpenSSL specific details of RSA key generation.
  It provides interfaces to:

  1> Get RSA structure
  2> Get EVP_PKEY structure
  3> Write Private/Public key into a string
  4> Free RSA/EVP_PKEY structures
*/
class RSA_gen {
 public:
  RSA_gen(uint32_t key_size, uint32_t exponent = RSA_F4)
      : m_key_size(key_size), m_exponent(exponent) {}

  ~RSA_gen() = default;

  /**
    Passing key type is a violation against the principle of generic
    programming when this operator is used in an algorithm
    but it at the same time increases usefulness of this class when used
    stand alone.
   */
  /* generate RSA keys */
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  EVP_PKEY *operator()(void) {
    EVP_PKEY *rsa = nullptr;
    BIGNUM *exponent = BN_new();
    if (!exponent) return nullptr;
    EVP_PKEY_CTX *rsa_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr);
    if (!rsa_ctx) {
      BN_free(exponent);
      return nullptr;
    }
    if (BN_set_word(exponent, m_exponent) != 1 ||
        EVP_PKEY_keygen_init(rsa_ctx) <= 0 ||
        EVP_PKEY_CTX_set_rsa_keygen_bits(rsa_ctx, m_key_size) <= 0 ||
        EVP_PKEY_CTX_set1_rsa_keygen_pubexp(rsa_ctx, exponent) <= 0 ||
        EVP_PKEY_keygen(rsa_ctx, &rsa) <= 0) {
      BN_free(exponent);
      EVP_PKEY_CTX_free(rsa_ctx);
      return nullptr;
    }
    BN_free(exponent);
    EVP_PKEY_CTX_free(rsa_ctx);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  RSA *operator()(void) {
    RSA *rsa = RSA_new();
    if (!rsa) return nullptr;
    BIGNUM *e = BN_new();
    if (!e) {
      RSA_free(rsa);
      return nullptr;
    }
    if (!BN_set_word(e, m_exponent) ||
        !RSA_generate_key_ex(rsa, m_key_size, e, nullptr)) {
      RSA_free(rsa);
      BN_free(e);
      return nullptr;
    }
    BN_free(e);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

    return rsa;  // pass ownership
  }

 private:
  uint32_t m_key_size;
  uint32_t m_exponent;
};

#if OPENSSL_VERSION_NUMBER < 0x30000000L
static EVP_PKEY *evp_pkey_generate(RSA *rsa) {
  if (rsa) {
    EVP_PKEY *pkey = EVP_PKEY_new();
    EVP_PKEY_assign_RSA(pkey, rsa);
    return pkey;
  }
  return nullptr;
}
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */

/**
  Write private key in a string buffer

  @param [in] rsa Handle to RSA structure where private key is stored

  @returns Sql_string_t object with private key stored in it.
*/
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static Sql_string_t rsa_priv_key_write(EVP_PKEY *rsa) {
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
static Sql_string_t rsa_priv_key_write(RSA *rsa) {
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  assert(rsa);
  BIO *buf = BIO_new(BIO_s_mem());
  Sql_string_t read_buffer;

#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  if (PEM_write_bio_PrivateKey(buf, rsa, nullptr, nullptr, 0, nullptr,
                               nullptr)) {
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  if (PEM_write_bio_RSAPrivateKey(buf, rsa, nullptr, nullptr, 0, nullptr,
                                  nullptr)) {
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    size_t len = BIO_pending(buf);
    if (resize_no_exception(read_buffer, len + 1) == true) {
      BIO_read(buf, const_cast<char *>(read_buffer.c_str()), len);
      read_buffer[len] = '\0';
    }
  }
  BIO_free(buf);
  return read_buffer;
}

/**
  Write public key in a string buffer

  @param [in] rsa Handle to RSA structure where public key is stored

  @returns Sql_string_t object with public key stored in it.
*/
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
static Sql_string_t rsa_pub_key_write(EVP_PKEY *rsa) {
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
static Sql_string_t rsa_pub_key_write(RSA *rsa) {
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  assert(rsa);
  BIO *buf = BIO_new(BIO_s_mem());
  Sql_string_t read_buffer;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  if (PEM_write_bio_PUBKEY(buf, rsa)) {
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  if (PEM_write_bio_RSA_PUBKEY(buf, rsa)) {
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    size_t len = BIO_pending(buf);
    if (resize_no_exception(read_buffer, len + 1) == true) {
      BIO_read(buf, const_cast<char *>(read_buffer.c_str()), len);
      read_buffer[len] = '\0';
    }
  }
  BIO_free(buf);
  return read_buffer;
}

/*
  This class encapsulates OpenSSL specific details of X509 certificate
  generation. It provides interfaces to:

  1> Generate X509 certificate
  2> Read/Write X509 certificate from/to a string
  3> Read/Write Private key from/to a string
  4> Free X509/EVP_PKEY structures
*/
class X509_gen {
 public:
  X509 *operator()(EVP_PKEY *pkey, const Sql_string_t cn, uint32_t serial,
                   uint32_t notbefore, uint32_t notafter, bool self_sign = true,
                   X509 *ca_x509 = nullptr, EVP_PKEY *ca_pkey = nullptr) {
    X509 *x509 = X509_new();
    X509_EXTENSION *ext = nullptr;
    X509V3_CTX v3ctx;
    X509_NAME *name = nullptr;

    assert(cn.length() <= MAX_CN_NAME_LENGTH);
    assert(serial != 0);
    assert(self_sign || (ca_x509 != nullptr && ca_pkey != nullptr));
    if (!x509) goto err;

    /** Set certificate version */
    if (!X509_set_version(x509, 2)) goto err;

    /** Set serial number */
    if (!ASN1_INTEGER_set(X509_get_serialNumber(x509), serial)) goto err;

    /** Set certificate validity */
    if (!X509_gmtime_adj(X509_get_notBefore(x509), notbefore) ||
        !X509_gmtime_adj(X509_get_notAfter(x509), notafter))
      goto err;

    /** Set public key */
    if (!X509_set_pubkey(x509, pkey)) goto err;

    /** Set CN value in subject */
    name = X509_get_subject_name(x509);
    if (!name) goto err;

    if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
                                    (const unsigned char *)cn.c_str(), -1, -1,
                                    0))
      goto err;

    /** Set Issuer */
    if (!X509_set_issuer_name(
            x509, self_sign ? name : X509_get_subject_name(ca_x509)))
      goto err;

    /** Add X509v3 extensions */
    X509V3_set_ctx(&v3ctx, self_sign ? x509 : ca_x509, x509, nullptr, nullptr,
                   0);

    /** Add CA:TRUE / CA:FALSE inforamation */
    if (!(ext = X509V3_EXT_conf_nid(
              nullptr, &v3ctx, NID_basic_constraints,
              self_sign ? const_cast<char *>("critical,CA:TRUE")
                        : const_cast<char *>("critical,CA:FALSE"))))
      goto err;
    X509_add_ext(x509, ext, -1);
    X509_EXTENSION_free(ext);

    /** Sign using SHA256 */
    if (!X509_sign(x509, self_sign ? pkey : ca_pkey, EVP_sha256())) goto err;

    return x509;
  err:
    if (x509) X509_free(x509);
    return nullptr;
  }
};

/**
  Read a X509 certificate into X509 format

  @param [in] input_string Content of X509 certificate file.

  @returns Handle to X509 structure.

  Assumption : Caller will free X509 object
*/
static X509 *x509_cert_read(const Sql_string_t &input_string) {
  X509 *x509 = nullptr;

  if (!input_string.size()) return x509;

  BIO *buf = BIO_new(BIO_s_mem());
  BIO_write(buf, input_string.c_str(), input_string.size());
  x509 = PEM_read_bio_X509(buf, nullptr, nullptr, nullptr);
  BIO_free(buf);
  return x509;
}

/**
  Write X509 certificate into a string

  @param [in] cert Certificate information in X509 format.

  @returns certificate information in string format.
*/
static Sql_string_t x509_cert_write(X509 *cert) {
  assert(cert);
  BIO *buf = BIO_new(BIO_s_mem());
  Sql_string_t read_buffer;
  if (PEM_write_bio_X509(buf, cert)) {
    size_t len = BIO_pending(buf);
    if (resize_no_exception(read_buffer, len + 1) == true) {
      BIO_read(buf, const_cast<char *>(read_buffer.c_str()), len);
      read_buffer[len] = '\0';
    }
  }
  BIO_free(buf);
  return read_buffer;
}

/**
  Read Private key into EVP_PKEY structure

  @param [in] input_string Content of private key file.

  @returns Handle to EVP_PKEY structure.

  Assumption : Caller will free EVP_PKEY object
*/
static EVP_PKEY *x509_key_read(const Sql_string_t &input_string) {
  EVP_PKEY *pkey = nullptr;

  if (!input_string.size()) return pkey;

  BIO *buf = BIO_new(BIO_s_mem());
  BIO_write(buf, input_string.c_str(), input_string.size());
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  pkey = PEM_read_bio_PrivateKey(buf, nullptr, nullptr, nullptr);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  RSA *rsa = PEM_read_bio_RSAPrivateKey(buf, nullptr, nullptr, nullptr);
  pkey = evp_pkey_generate(rsa);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  BIO_free(buf);
  return pkey;
}

/**
  Write X509 certificate into a string

  @param [in] pkey Private key information.

  @returns private key information in string format.
*/
static Sql_string_t x509_key_write(EVP_PKEY *pkey) {
  assert(pkey);
  BIO *buf = BIO_new(BIO_s_mem());
  Sql_string_t read_buffer;

#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  if (PEM_write_bio_PrivateKey(buf, pkey, nullptr, nullptr, 10, nullptr,
                               nullptr)) {
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  RSA *rsa = EVP_PKEY_get1_RSA(pkey);
  if (PEM_write_bio_RSAPrivateKey(buf, rsa, nullptr, nullptr, 10, nullptr,
                                  nullptr)) {
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    size_t len = BIO_pending(buf);
    if (resize_no_exception(read_buffer, len + 1) == true) {
      BIO_read(buf, const_cast<char *>(read_buffer.c_str()), len);
      read_buffer[len] = '\0';
    }
  }
  BIO_free(buf);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
  RSA_free(rsa);
#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
  return read_buffer;
}

/**
  Algorithm to create X509 certificate.
  Relies on:
  1> RSA key generator
  2> X509 certificate generator
  3> FILE reader/writer

  Overwrites key/certificate files if already present.

  @param [in] rsa_gen RSA generator
  @param [in] cn Common name field of X509 certificate.
  @param [in] serial Certificate serial number
  @param [in] cert_filename File name for X509 certificate
  @param [in] key_filename File name for private key
  @param [in] filecr File creator
  @param [in] ca_key_file CA private key file
  @param [in] ca_cert_file CA certificate file

  @returns generation status
    @retval false Error in key/certificate generation.
    @retval true key/certificate files are generated successfully.
*/

template <typename RSA_generator_func, typename File_creation_func>
bool create_x509_certificate(RSA_generator_func &rsa_gen, const Sql_string_t cn,
                             uint32_t serial, const Sql_string_t cert_filename,
                             const Sql_string_t key_filename,
                             File_creation_func &filecr,
                             const Sql_string_t ca_key_file = "",
                             const Sql_string_t ca_cert_file = "") {
  bool ret_val = true;
  bool self_sign = true;
  Sql_string_t ca_key_str;
  Sql_string_t ca_cert_str;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  EVP_PKEY *rsa = nullptr;
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  RSA *rsa = nullptr;
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  EVP_PKEY *pkey = nullptr;
  EVP_PKEY *ca_key = nullptr;
  X509 *x509 = nullptr;
  X509 *ca_x509 = nullptr;
  File_IO *x509_key_file_ostream = nullptr;
  File_IO *x509_cert_file_ostream = nullptr;
  File_IO *x509_ca_key_file_istream = nullptr;
  File_IO *x509_ca_cert_file_istream = nullptr;
  X509_gen x509_gen;
  MY_MODE file_creation_mode = get_file_perm(USER_READ | USER_WRITE);

  x509_key_file_ostream = filecr(key_filename, file_creation_mode);

  /* Generate private key for X509 certificate */
  rsa = rsa_gen();
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  DBUG_EXECUTE_IF("null_rsa_error", {
    EVP_PKEY_free(rsa);
    rsa = nullptr;
  });
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  DBUG_EXECUTE_IF("null_rsa_error", {
    RSA_free(rsa);
    rsa = nullptr;
  });
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

  if (!rsa) {
    LogErr(ERROR_LEVEL, ER_X509_NEEDS_RSA_PRIVKEY);
    ret_val = false;
    goto end;
  }

  /* Obtain EVP_PKEY */
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  pkey = rsa;
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  pkey = evp_pkey_generate(rsa);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

  /* Write private key information to file and set file permission */
  (*x509_key_file_ostream) << x509_key_write(pkey);
  DBUG_EXECUTE_IF("key_file_write_error",
                  { x509_key_file_ostream->set_error(); });
  if (x509_key_file_ostream->get_error()) {
    LogErr(ERROR_LEVEL, ER_X509_CANT_WRITE_KEY, key_filename.c_str());
    ret_val = false;
    goto end;
  }

  /*
    Read CA key/certificate files in PEM format.
  */
  if (ca_key_file.size() && ca_cert_file.size()) {
    x509_ca_key_file_istream = filecr(ca_key_file, true);
    x509_ca_cert_file_istream = filecr(ca_cert_file, true);
    (*x509_ca_key_file_istream) >> ca_key_str;
    ca_key = x509_key_read(ca_key_str);
    DBUG_EXECUTE_IF("ca_key_read_error", {
      EVP_PKEY_free(ca_key);
      ca_key = nullptr;
    });
    if (!ca_key) {
      LogErr(ERROR_LEVEL, ER_X509_CANT_READ_CA_KEY, ca_key_file.c_str());
      ret_val = false;
      goto end;
    }

    (*x509_ca_cert_file_istream) >> ca_cert_str;
    ca_x509 = x509_cert_read(ca_cert_str);
    DBUG_EXECUTE_IF("ca_cert_read_error", {
      X509_free(ca_x509);
      ca_x509 = nullptr;
    });
    if (!ca_x509) {
      LogErr(ERROR_LEVEL, ER_X509_CANT_READ_CA_CERT, ca_cert_file.c_str());
      ret_val = false;
      goto end;
    }

    self_sign = false;
  }

  /* Create X509 certificate with validity of 10 year */
  x509 = x509_gen(pkey, cn, serial, 0, 365L * 24 * 60 * 60 * 10, self_sign,
                  ca_x509, ca_key);
  DBUG_EXECUTE_IF("x509_cert_generation_error", {
    X509_free(x509);
    x509 = nullptr;
  });
  if (!x509) {
    LogErr(ERROR_LEVEL, ER_X509_CANT_CREATE_CERT);
    ret_val = false;
    goto end;
  }

  /* Write X509 certificate to file and set permission */
  x509_cert_file_ostream = filecr(cert_filename);
  (*x509_cert_file_ostream) << x509_cert_write(x509);
  DBUG_EXECUTE_IF("cert_pub_key_write_error",
                  { x509_cert_file_ostream->set_error(); });
  if (x509_cert_file_ostream->get_error()) {
    LogErr(ERROR_LEVEL, ER_X509_CANT_WRITE_CERT, cert_filename.c_str());
    ret_val = false;
    goto end;
  }

  if (my_chmod(cert_filename.c_str(),
               USER_READ | USER_WRITE | GROUP_READ | OTHERS_READ,
               MYF(MY_FAE + MY_WME))) {
    LogErr(ERROR_LEVEL, ER_X509_CANT_CHMOD_KEY, cert_filename.c_str());
    ret_val = false;
    goto end;
  }

end:

  if (pkey) EVP_PKEY_free(pkey); /* Frees rsa too */
  if (ca_key) EVP_PKEY_free(ca_key);
  if (x509) X509_free(x509);
  if (ca_x509) X509_free(ca_x509);

  return ret_val;
}

/**
  Algorithm to generate RSA key pair.
  Relies on:
  1> RSA generator
  2> File reader/writer

  Overwrites existing Private/Public key file if any.

  @param [in] rsa_gen RSA key pair generator
  @param [in] priv_key_filename File name of private key
  @param [in] pub_key_filename File name of public key
  @param [in] filecr File creator

  @returns status of RSA key pair generation.
    @retval false Error in RSA key pair generation.
    @retval true Private/Public keys are successfully generated.
*/
template <typename RSA_generator_func, typename File_creation_func>
bool create_RSA_key_pair(RSA_generator_func &rsa_gen,
                         const Sql_string_t priv_key_filename,
                         const Sql_string_t pub_key_filename,
                         File_creation_func &filecr) {
  std::string temp_priv_key_filename = priv_key_filename + ".temp";
  std::string temp_pub_key_filename = pub_key_filename + ".temp";
  bool ret_val = true;
  File_IO *priv_key_file_ostream = nullptr;
  File_IO *pub_key_file_ostream = nullptr;
  MY_MODE file_creation_mode = get_file_perm(USER_READ | USER_WRITE);
  MY_MODE saved_umask = umask(~(file_creation_mode));

  assert(temp_priv_key_filename.size() && temp_pub_key_filename.size());

#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  EVP_PKEY *rsa;
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  RSA *rsa;
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  rsa = rsa_gen();

#if OPENSSL_VERSION_NUMBER >= 0x30000000L
  DBUG_EXECUTE_IF("null_rsa_error", {
    EVP_PKEY_free(rsa);
    rsa = nullptr;
  });
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  DBUG_EXECUTE_IF("null_rsa_error", {
    RSA_free(rsa);
    rsa = nullptr;
  });
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

  if (!rsa) {
    LogErr(ERROR_LEVEL, ER_AUTH_CANT_CREATE_RSA_PAIR);
    ret_val = false;
    goto end;
  }
  DBUG_EXECUTE_IF("no_key_files", DBUG_SUICIDE(););

  priv_key_file_ostream = filecr(temp_priv_key_filename, file_creation_mode);
  DBUG_EXECUTE_IF("empty_priv_key_temp_file", DBUG_SUICIDE(););

  (*priv_key_file_ostream) << rsa_priv_key_write(rsa);
  DBUG_EXECUTE_IF("key_file_write_error",
                  { priv_key_file_ostream->set_error(); });
  if (priv_key_file_ostream->get_error()) {
    LogErr(ERROR_LEVEL, ER_AUTH_CANT_WRITE_PRIVKEY, priv_key_filename.c_str());
    ret_val = false;
    goto end;
  }
  if (my_chmod(temp_priv_key_filename.c_str(), USER_READ | USER_WRITE,
               MYF(MY_FAE + MY_WME))) {
    LogErr(ERROR_LEVEL, ER_X509_CANT_CHMOD_KEY, priv_key_filename.c_str());
    ret_val = false;
    goto end;
  }
  DBUG_EXECUTE_IF("valid_priv_key_temp_file", DBUG_SUICIDE(););

  pub_key_file_ostream = filecr(temp_pub_key_filename);
  DBUG_EXECUTE_IF("valid_priv_key_temp_file_empty_pub_key_temp_file",
                  DBUG_SUICIDE(););

  (*pub_key_file_ostream) << rsa_pub_key_write(rsa);

  DBUG_EXECUTE_IF("cert_pub_key_write_error",
                  { pub_key_file_ostream->set_error(); });

  if (pub_key_file_ostream->get_error()) {
    LogErr(ERROR_LEVEL, ER_AUTH_CANT_WRITE_PUBKEY, pub_key_filename.c_str());
    ret_val = false;
    goto end;
  }
  if (my_chmod(temp_pub_key_filename.c_str(),
               USER_READ | USER_WRITE | GROUP_READ | OTHERS_READ,
               MYF(MY_FAE + MY_WME))) {
    LogErr(ERROR_LEVEL, ER_X509_CANT_CHMOD_KEY, pub_key_filename.c_str());
    ret_val = false;
    goto end;
  }
  DBUG_EXECUTE_IF("valid_key_temp_files", DBUG_SUICIDE(););

  rename(temp_priv_key_filename.c_str(), priv_key_filename.c_str());
  DBUG_EXECUTE_IF("valid_pub_key_temp_file_valid_priv_key_file",
                  DBUG_SUICIDE(););
  rename(temp_pub_key_filename.c_str(), pub_key_filename.c_str());
  DBUG_EXECUTE_IF("valid_key_files", DBUG_SUICIDE(););

end:
  if (rsa)
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
    EVP_PKEY_free(rsa);
#else  /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
    RSA_free(rsa);
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

  umask(saved_umask);
  return ret_val;
}

/**
  Check auto_generate_certs option and generate
  SSL certificates if required.

  SSL Certificates are generated iff following conditions are met.
  1> auto_generate_certs is set to ON.
  2> None of the SSL system variables are specified.
  3> Following files are not present in data directory.
     a> ca.pem
     b> server_cert.pem
     c> server_key.pem

  If above mentioned conditions are satisfied, following action will be taken:

  1> 6 File are generated and placed data directory:
     a> ca.pem
     b> ca_key.pem
     c> server_cert.pem
     d> server_key.pem
     e> client_cert.pem
     f> client_key.pem

     ca.pem is self signed auto generated CA certificate. server_cert.pem
     and client_cert.pem are signed using auto generated CA.

     ca_key.pem, client_cert.pem and client_key.pem are overwritten if
     they are present in data directory.

  Path of following system variables are set if certificates are either
  generated or already present in data directory.
  a> ssl-ca
  b> ssl-cert
  c> ssl-key

  Assumption : auto_detect_ssl() is called before control reaches to
  do_auto_cert_generation().

  @param [in] auto_detection_status Status of SSL artifacts detection process
  @param [out] ssl_ca  pointer to the generated CA certificate file
  @param [out] ssl_key pointer to the generated key file
  @param [out] ssl_cert pointer to the generated certificate file.

  @retval true i Generation is successful or skipped
  @retval false Generation failed.
*/
bool do_auto_cert_generation(ssl_artifacts_status auto_detection_status,
                             const char **ssl_ca, const char **ssl_key,
                             const char **ssl_cert) {
  if (opt_auto_generate_certs == true) {
    /*
      Do not generate SSL certificates/RSA keys,
      If any of the SSL option was specified.
    */

    int sec_level = security_level();

    if (auto_detection_status == SSL_ARTIFACTS_VIA_OPTIONS) {
      LogErr(INFORMATION_LEVEL, ER_AUTH_SSL_CONF_PREVENTS_CERT_GENERATION);
      return true;
    } else if (auto_detection_status == SSL_ARTIFACTS_AUTO_DETECTED ||
               auto_detection_status == SSL_ARTIFACT_TRACES_FOUND) {
      LogErr(INFORMATION_LEVEL, ER_AUTH_USING_EXISTING_CERTS);
      return true;
    } else {
      assert(auto_detection_status == SSL_ARTIFACTS_NOT_FOUND);

      /* Initialize the key pair generator. It can also be used stand alone */
      RSA_gen rsa_gen(rsa_key_sizes[sec_level]);

      /*
         Initialize the file creator.
       */
      File_creator fcr;
      Sql_string_t ca_name = "MySQL_Server_";
      Sql_string_t server_name = "MySQL_Server_";
      Sql_string_t client_name = "MySQL_Server_";

      ca_name.append(MYSQL_SERVER_VERSION);
      ca_name.append("_Auto_Generated_CA_Certificate");
      server_name.append(MYSQL_SERVER_VERSION);
      server_name.append("_Auto_Generated_Server_Certificate");
      client_name.append(MYSQL_SERVER_VERSION);
      client_name.append("_Auto_Generated_Client_Certificate");

      /*
        Maximum length of X509 certificate subject is 64.
        Make sure that constructed strings are within valid
        bounds or change them to minimal default strings
      */
      if (ca_name.length() > MAX_CN_NAME_LENGTH ||
          server_name.length() > MAX_CN_NAME_LENGTH ||
          client_name.length() > MAX_CN_NAME_LENGTH) {
        ca_name.clear();
        ca_name.append("MySQL_Server_Auto_Generated_CA_Certificate");
        server_name.clear();
        server_name.append("MySQL_Server_Auto_Generated_Server_Certificate");
        client_name.clear();
        client_name.append("MySQL_Server_Auto_Generated_Client_Certificate");
      }

      /* Create and write the certa and keys on disk */
      if ((create_x509_certificate(rsa_gen, ca_name, 1, DEFAULT_SSL_CA_CERT,
                                   DEFAULT_SSL_CA_KEY, fcr) == false) ||
          (create_x509_certificate(
               rsa_gen, server_name, 2, DEFAULT_SSL_SERVER_CERT,
               DEFAULT_SSL_SERVER_KEY, fcr, DEFAULT_SSL_CA_KEY,
               DEFAULT_SSL_CA_CERT) == false) ||
          (create_x509_certificate(
               rsa_gen, client_name, 3, DEFAULT_SSL_CLIENT_CERT,
               DEFAULT_SSL_CLIENT_KEY, fcr, DEFAULT_SSL_CA_KEY,
               DEFAULT_SSL_CA_CERT) == false)) {
        return false;
      }
      *ssl_ca = DEFAULT_SSL_CA_CERT;
      *ssl_cert = DEFAULT_SSL_SERVER_CERT;
      *ssl_key = DEFAULT_SSL_SERVER_KEY;
      LogErr(INFORMATION_LEVEL, ER_AUTH_CERTS_SAVED_TO_DATADIR);
    }
    return true;
  } else {
    LogErr(INFORMATION_LEVEL, ER_AUTH_CERT_GENERATION_DISABLED);
    return true;
  }
}

/*
 Generate RSA keys

 @param [in] auto_generate Variable to control key generation
 @param [in] priv_key_path Path to store/check private key
 @param [in] pub_key_path  Path to store/check public key
 @param [in] message       Message part to be logged

 @returns status of key generation
   @retval true  Success
   @retval false Error generating keys
*/

static bool generate_rsa_keys(bool auto_generate, const char *priv_key_path,
                              const char *pub_key_path, const char *message) {
  DBUG_TRACE;
  if (auto_generate) {
    MY_STAT priv_stat, pub_stat;
    int sec_level = security_level();
    if (strcmp(priv_key_path, AUTH_DEFAULT_RSA_PRIVATE_KEY) ||
        strcmp(pub_key_path, AUTH_DEFAULT_RSA_PUBLIC_KEY)) {
      LogErr(INFORMATION_LEVEL, ER_AUTH_RSA_CONF_PREVENTS_KEY_GENERATION,
             message);
      return true;
    } else if (my_stat(AUTH_DEFAULT_RSA_PRIVATE_KEY, &priv_stat, MYF(0)) ||
               my_stat(AUTH_DEFAULT_RSA_PUBLIC_KEY, &pub_stat, MYF(0))) {
      LogErr(INFORMATION_LEVEL, ER_AUTH_KEY_GENERATION_SKIPPED_PAIR_PRESENT,
             message);
      return true;
    } else {
      /* Initialize the key pair generator. */
      RSA_gen rsa_gen(rsa_key_sizes[sec_level]);

      /* Initialize the file creator. */
      File_creator fcr;

      if (create_RSA_key_pair(rsa_gen, "private_key.pem", "public_key.pem",
                              fcr) == false)
        return false;

      LogErr(INFORMATION_LEVEL, ER_AUTH_KEYS_SAVED_TO_DATADIR, message);
      return true;
    }
  } else {
    LogErr(INFORMATION_LEVEL, ER_AUTH_KEY_GENERATION_DISABLED, message);
    return true;
  }
}

/*
  Generate RSA key pair.

  @returns Status of key generation
    @retval true Success
    @retval false Failure

  Check sha256_password_auto_generate_rsa_keys/
  caching_sha2_password_auto_generate_rsa_keys
  option and generate RSA key pair if required.

  RSA key pair is generated iff following conditions are met.
  1> sha256_password_auto_generate_rsa_keys/
     caching_sha2_password_auto_generate_rsa_keys is set to ON.
  2> sha256_password_private_key_path/caching_sha2_rsa_private_key_path
     or sha256_password_public_key_path/caching_sha2_rsa_public_key_path
     are pointing to non-default locations.
  3> Following files are not present in data directory.
     a> private_key.pem
     b> public_key.pem

  If above mentioned conditions are satisfied private_key.pem and
  public_key.pem files are generated and placed in data directory.
*/
static bool do_auto_rsa_keys_generation() {
  return (generate_rsa_keys(auth_rsa_auto_generate_rsa_keys,
                            auth_rsa_private_key_path, auth_rsa_public_key_path,
                            "--sha256_password_auto_generate_rsa_keys") &&
          generate_rsa_keys(caching_sha2_auto_generate_rsa_keys,
                            caching_sha2_rsa_private_key_path,
                            caching_sha2_rsa_public_key_path,
                            "--caching_sha2_password_auto_generate_rsa_keys"));
}

bool MPVIO_EXT::can_authenticate() {
  return (acl_user && acl_user->can_authenticate);
}

static struct st_mysql_auth native_password_handler = {
    MYSQL_AUTHENTICATION_INTERFACE_VERSION,
    Cached_authentication_plugins::get_plugin_name(
        PLUGIN_MYSQL_NATIVE_PASSWORD),
    native_password_authenticate,
    generate_native_password,
    validate_native_password_hash,
    set_native_salt,
    AUTH_FLAG_USES_INTERNAL_STORAGE,
    compare_native_password_with_hash,
};

static struct st_mysql_auth sha256_password_handler = {
    MYSQL_AUTHENTICATION_INTERFACE_VERSION,
    Cached_authentication_plugins::get_plugin_name(PLUGIN_SHA256_PASSWORD),
    sha256_password_authenticate,
    generate_sha256_password,
    validate_sha256_password_hash,
    set_sha256_salt,
    AUTH_FLAG_USES_INTERNAL_STORAGE,
    compare_sha256_password_with_hash,
};

mysql_declare_plugin(mysql_password){
    MYSQL_AUTHENTICATION_PLUGIN, /* type constant    */
    &native_password_handler,    /* type descriptor  */
    Cached_authentication_plugins::get_plugin_name(
        PLUGIN_MYSQL_NATIVE_PASSWORD), /* Name           */
    PLUGIN_AUTHOR_ORACLE,              /* Author           */
    "Native MySQL authentication",     /* Description      */
    PLUGIN_LICENSE_GPL,                /* License          */
    nullptr,                           /* Init function    */
    nullptr,                           /* Check uninstall  */
    nullptr,                           /* Deinit function  */
    0x0101,                            /* Version (1.0)    */
    nullptr,                           /* status variables */
    nullptr,                           /* system variables */
    nullptr,                           /* config options   */
    0,                                 /* flags            */
},
    {
        MYSQL_AUTHENTICATION_PLUGIN, /* type constant    */
        &sha256_password_handler,    /* type descriptor  */
        Cached_authentication_plugins::get_plugin_name(
            PLUGIN_SHA256_PASSWORD),      /* Name             */
        PLUGIN_AUTHOR_ORACLE,             /* Author           */
        "SHA256 password authentication", /* Description      */
        PLUGIN_LICENSE_GPL,               /* License          */
        &init_sha256_password_handler,    /* Init function    */
        nullptr,                          /* Check uninstall  */
        nullptr,                          /* Deinit function  */
        0x0101,                           /* Version (1.0)    */
        nullptr,                          /* status variables */
        sha256_password_sysvars,          /* system variables */
        nullptr,                          /* config options   */
        0                                 /* flags            */
    } mysql_declare_plugin_end;