Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Support for tidb_sm3_password authentication #36193

Merged
merged 43 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
21d6d9a
*: Support for sm3_password authentication
CbcWestwolf Jul 13, 2022
77ec1f7
Fix
CbcWestwolf Jul 13, 2022
3676087
Fix
CbcWestwolf Jul 13, 2022
2699659
Merge branch 'master' of github.com:pingcap/tidb into support_sm3
CbcWestwolf Jul 13, 2022
d1b0c83
Merge branch 'master' into support_sm3
CbcWestwolf Jul 13, 2022
3972d00
Merge branch 'master' of github.com:pingcap/tidb into support_sm3
CbcWestwolf Jul 14, 2022
d080a88
implement sm3 like caching_sha2
CbcWestwolf Jul 14, 2022
3aba797
Update parser/auth/sm3.go
CbcWestwolf Jul 15, 2022
711736b
Update
CbcWestwolf Jul 15, 2022
4b68cb6
Merge branch 'master' of github.com:pingcap/tidb into support_sm3
CbcWestwolf Jul 15, 2022
0b79e73
Update
CbcWestwolf Jul 15, 2022
45340e3
Update bazel
CbcWestwolf Jul 15, 2022
f9badbb
Merge branch 'master' into support_sm3
CbcWestwolf Jul 18, 2022
e64e902
Fix
CbcWestwolf Jul 18, 2022
edd2ae6
Fix
CbcWestwolf Jul 18, 2022
59cc76e
Fix
CbcWestwolf Jul 18, 2022
6b5cb33
Support bulitin function SM3(str)
CbcWestwolf Jul 18, 2022
a98c4e2
Add license from Suzhou Tongji Fintech Research Institute
CbcWestwolf Jul 18, 2022
69d779a
Fix
CbcWestwolf Jul 18, 2022
cf565a8
Add test for builtin SM3()
CbcWestwolf Jul 18, 2022
92f2dc5
Merge branch 'master' of github.com:pingcap/tidb into support_sm3
CbcWestwolf Jul 19, 2022
e11e90e
Add test for SM3
CbcWestwolf Jul 19, 2022
d893f3d
Fix
CbcWestwolf Jul 19, 2022
9da7846
Fix
CbcWestwolf Jul 19, 2022
cbd467e
Fix UT
CbcWestwolf Jul 19, 2022
64516b5
Fix
CbcWestwolf Jul 19, 2022
661f720
Fix
CbcWestwolf Jul 19, 2022
1ab815b
Fix
CbcWestwolf Jul 21, 2022
1c54ff0
Merge branch 'master' into support_sm3
CbcWestwolf Jul 22, 2022
da28c9e
Merge branch 'master' into support_sm3
CbcWestwolf Jul 22, 2022
2a496f5
Merge branch 'master' of github.com:pingcap/tidb into support_sm3
CbcWestwolf Aug 27, 2022
9db5b4d
Fix
CbcWestwolf Aug 28, 2022
8e09189
Update parser/auth/sm3.go
CbcWestwolf Sep 2, 2022
a888612
Update
CbcWestwolf Sep 5, 2022
2c472fc
Merge branch 'support_sm3' of github.com:CbcWestwolf/tidb into suppor…
CbcWestwolf Sep 5, 2022
511afe8
Merge branch 'master' into support_sm3
CbcWestwolf Sep 6, 2022
9a08797
Update
CbcWestwolf Sep 6, 2022
ffa4176
Fix
CbcWestwolf Sep 6, 2022
eda8c41
Improve compatibility
CbcWestwolf Sep 7, 2022
0012cc4
Fix UT
CbcWestwolf Sep 7, 2022
9ed13ae
Merge branch 'master' into support_sm3
CbcWestwolf Sep 8, 2022
e53ad3e
Merge branch 'master' into support_sm3
ti-chi-bot Sep 8, 2022
d2e2858
Merge branch 'master' into support_sm3
ti-chi-bot Sep 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2502,8 +2502,8 @@ def go_deps():
name = "com_github_pingcap_kvproto",
build_file_proto_mode = "disable_global",
importpath = "github.com/pingcap/kvproto",
sum = "h1:nP2wmyw9JTRsk5rm+tZtfAso6c/1FvuaFNbXTaYz3FE=",
version = "v0.0.0-20220705053936-aa9c2d20cd2a",
sum = "h1:VKMmvYhtG28j1sCCBdq4s+V9UOYqNgQ6CQviQwOgTeg=",
version = "v0.0.0-20220705090230-a5d4ffd2ba33",
)
go_repository(
name = "com_github_pingcap_log",
Expand Down
6 changes: 4 additions & 2 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ func (e *SimpleExec) executeCreateUser(ctx context.Context, s *ast.CreateUserStm
}

switch authPlugin {
case mysql.AuthNativePassword, mysql.AuthCachingSha2Password, mysql.AuthSocket:
case mysql.AuthNativePassword, mysql.AuthCachingSha2Password, mysql.AuthSM3Password, mysql.AuthSocket:
default:
return ErrPluginIsNotLoaded.GenWithStackByArgs(spec.AuthOpt.AuthPlugin)
}
Expand Down Expand Up @@ -982,7 +982,7 @@ func (e *SimpleExec) executeAlterUser(ctx context.Context, s *ast.AlterUserStmt)
spec.AuthOpt.AuthPlugin = authplugin
}
switch spec.AuthOpt.AuthPlugin {
case mysql.AuthNativePassword, mysql.AuthCachingSha2Password, mysql.AuthSocket, "":
case mysql.AuthNativePassword, mysql.AuthCachingSha2Password, mysql.AuthSM3Password, mysql.AuthSocket, "":
default:
return ErrPluginIsNotLoaded.GenWithStackByArgs(spec.AuthOpt.AuthPlugin)
}
Expand Down Expand Up @@ -1463,6 +1463,8 @@ func (e *SimpleExec) executeSetPwd(ctx context.Context, s *ast.SetPwdStmt) error
switch authplugin {
case mysql.AuthCachingSha2Password:
pwd = auth.NewSha2Password(s.Password)
case mysql.AuthSM3Password:
pwd = auth.NewSM3Password(s.Password)
case mysql.AuthSocket:
e.ctx.GetSessionVars().StmtCtx.AppendNote(ErrSetPasswordAuthPlugin.GenWithStackByArgs(u, h))
pwd = ""
Expand Down
6 changes: 6 additions & 0 deletions parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,8 @@ func (n *UserSpec) EncodedPassword() (string, bool) {
switch opt.AuthPlugin {
case mysql.AuthCachingSha2Password:
return auth.NewSha2Password(opt.AuthString), true
case mysql.AuthSM3Password:
return auth.NewSM3Password(opt.AuthString), true
case mysql.AuthSocket:
return "", true
default:
Expand All @@ -1340,6 +1342,10 @@ func (n *UserSpec) EncodedPassword() (string, bool) {
if len(opt.HashString) != mysql.SHAPWDHashLen {
return "", false
}
case mysql.AuthSM3Password:
if len(opt.HashString) != mysql.SM3PWDHashLen {
return "", false
}
case "", mysql.AuthNativePassword:
if len(opt.HashString) != (mysql.PWDHashLen+1) || !strings.HasPrefix(opt.HashString, "*") {
return "", false
Expand Down
2 changes: 2 additions & 0 deletions parser/auth/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
"auth.go",
"caching_sha2.go",
"mysql_native_password.go",
"sm3.go",
],
importpath = "github.com/pingcap/tidb/parser/auth",
visibility = ["//visibility:public"],
Expand All @@ -21,6 +22,7 @@ go_test(
srcs = [
"caching_sha2_test.go",
"mysql_native_password_test.go",
"sm3_test.go",
],
embed = [":auth"],
deps = ["@com_github_stretchr_testify//require"],
Expand Down
14 changes: 7 additions & 7 deletions parser/auth/caching_sha2.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,24 +177,24 @@ func sha256crypt(plaintext string, salt []byte, iterations int) string {
return buf.String()
}

// Checks if a MySQL style caching_sha2 authentication string matches a password
// CheckShaPassword checks if a MySQL style caching_sha2 authentication string matches a password
func CheckShaPassword(pwhash []byte, password string) (bool, error) {
pwhash_parts := bytes.Split(pwhash, []byte("$"))
if len(pwhash_parts) != 4 {
pwhashParts := bytes.Split(pwhash, []byte("$"))
if len(pwhashParts) != 4 {
return false, errors.New("failed to decode hash parts")
}

hash_type := string(pwhash_parts[1])
if hash_type != "A" {
hashType := string(pwhashParts[1])
if hashType != "A" {
return false, errors.New("digest type is incompatible")
}

iterations, err := strconv.Atoi(string(pwhash_parts[2]))
iterations, err := strconv.Atoi(string(pwhashParts[2]))
if err != nil {
return false, errors.New("failed to decode iterations")
}
iterations = iterations * ITERATION_MULTIPLIER
salt := pwhash_parts[3][:SALT_LENGTH]
salt := pwhashParts[3][:SALT_LENGTH]

newHash := sha256crypt(password, salt, iterations)

Expand Down
10 changes: 5 additions & 5 deletions parser/auth/caching_sha2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"github.com/stretchr/testify/require"
)

var foobarPwdHash, _ = hex.DecodeString("24412430303524031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
var foobarPwdSHA2Hash, _ = hex.DecodeString("24412430303524031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")

func TestCheckShaPasswordGood(t *testing.T) {
pwd := "foobar"
r, err := CheckShaPassword(foobarPwdHash, pwd)
r, err := CheckShaPassword(foobarPwdSHA2Hash, pwd)
require.NoError(t, err)
require.True(t, r)
}
Expand All @@ -44,7 +44,7 @@ func TestCheckShaPasswordShort(t *testing.T) {
require.Error(t, err)
}

func TestCheckShaPasswordDigetTypeIncompatible(t *testing.T) {
func TestCheckShaPasswordDigestTypeIncompatible(t *testing.T) {
pwd := "not_foobar"
pwhash, _ := hex.DecodeString("24422430303524031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
_, err := CheckShaPassword(pwhash, pwd)
Expand All @@ -58,7 +58,7 @@ func TestCheckShaPasswordIterationsInvalid(t *testing.T) {
require.Error(t, err)
}

// The output from NewSha2Password is not stable as the hash is based on the genrated salt.
// The output from NewSha2Password is not stable as the hash is based on the generated salt.
// This is why CheckShaPassword is used here.
func TestNewSha2Password(t *testing.T) {
pwd := "testpwd"
Expand All @@ -76,7 +76,7 @@ func TestNewSha2Password(t *testing.T) {

func BenchmarkShaPassword(b *testing.B) {
for i := 0; i < b.N; i++ {
m, err := CheckShaPassword(foobarPwdHash, "foobar")
m, err := CheckShaPassword(foobarPwdSHA2Hash, "foobar")
require.Nil(b, err)
require.True(b, m)
}
Expand Down
Loading