-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
sql_authentication.cc
6068 lines (5178 loc) · 216 KB
/
sql_authentication.cc
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
/* Copyright (c) 2000, 2023, 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 also distributed 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 included with MySQL.
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 = {