From 95cd0ad4c75a3bd4763836542f6e2742d8461281 Mon Sep 17 00:00:00 2001 From: Egok4wryu <15806957625@163.com> Date: Wed, 19 Oct 2022 15:46:42 +0800 Subject: [PATCH 1/5] add the annotation of the encryption type of new SM3 --- src/gausskernel/optimizer/commands/user.cpp | 1 + 1 file changed, 1 insertion(+) mode change 100755 => 100644 src/gausskernel/optimizer/commands/user.cpp diff --git a/src/gausskernel/optimizer/commands/user.cpp b/src/gausskernel/optimizer/commands/user.cpp old mode 100755 new mode 100644 index 634d0976d..60ae641ff --- a/src/gausskernel/optimizer/commands/user.cpp +++ b/src/gausskernel/optimizer/commands/user.cpp @@ -6068,6 +6068,7 @@ Datum calculate_encrypted_password(bool is_encrypted, const char* password, cons * if Password_encryption_type is 0, the encrypted password is md5. * if Password_encryption_type is 1, the encrypted password is sha256 + md5. * if Password_encryption_type is 2, the encrypted password is sha256. + * if Password_encryption_type is 3, the encrypted password is SM3. */ if (u_sess->attr.attr_security.Password_encryption_type == 0) { if (!pg_md5_encrypt(password, rolname, strlen(rolname), encrypted_md5_password)) { From 200c9b14a7e6a6bcba8ff9439e56dec1b5887873 Mon Sep 17 00:00:00 2001 From: Egok4wryu <15806957625@163.com> Date: Thu, 20 Oct 2022 11:08:02 +0800 Subject: [PATCH 2/5] add the annotation of the function of calculate_encrypted_password --- src/gausskernel/optimizer/commands/user.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gausskernel/optimizer/commands/user.cpp b/src/gausskernel/optimizer/commands/user.cpp index 60ae641ff..7115054fc 100644 --- a/src/gausskernel/optimizer/commands/user.cpp +++ b/src/gausskernel/optimizer/commands/user.cpp @@ -6052,6 +6052,7 @@ static Datum gs_calculate_encrypted_sm3_password(const char* password, const cha Datum calculate_encrypted_password(bool is_encrypted, const char* password, const char* rolname, const char* salt_string) { + /* If the password is '\0' or not exist */ if (password == NULL || password[0] == '\0') { ereport(ERROR, (errcode(ERRCODE_INVALID_PASSWORD), errmsg("The password could not be NULL."))); } @@ -6059,6 +6060,7 @@ Datum calculate_encrypted_password(bool is_encrypted, const char* password, cons char encrypted_md5_password[MD5_PASSWD_LEN + 1] = {0}; Datum datum_value; + /* If the password has encrypted */ if (!is_encrypted || isPWDENCRYPTED(password)) { return CStringGetTextDatum(password); } From 95b7221f15f8e338d5603dfa4db134817594c54a Mon Sep 17 00:00:00 2001 From: Egok4wryu <15806957625@163.com> Date: Thu, 20 Oct 2022 11:13:26 +0800 Subject: [PATCH 3/5] add the annotation in the function of pg_md5_encrypt --- src/common/backend/libpq/md5.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/backend/libpq/md5.cpp b/src/common/backend/libpq/md5.cpp index 9b8072e0f..f5af86993 100644 --- a/src/common/backend/libpq/md5.cpp +++ b/src/common/backend/libpq/md5.cpp @@ -309,6 +309,7 @@ bool pg_md5_encrypt(const char* passwd, const char* salt, size_t salt_len, char* { size_t passwd_len = strlen(passwd); errno_t rc = EOK; + /* the length of salt and password is <= SIZE_MAX */ #ifndef WIN32 if (unlikely(passwd_len >= SIZE_MAX - salt_len)) { return false; @@ -322,6 +323,7 @@ bool pg_md5_encrypt(const char* passwd, const char* salt, size_t salt_len, char* char* crypt_buf = (char*)malloc(passwd_len + salt_len + 1); bool ret = false; + /* the buffer is not exist */ if (crypt_buf == NULL) return false; From 5eb65aa4ca0673184b23e139f5d4774750f35ed1 Mon Sep 17 00:00:00 2001 From: Egok4wryu <15806957625@163.com> Date: Thu, 20 Oct 2022 11:19:11 +0800 Subject: [PATCH 4/5] add the annotation in the function of calcultate_encrypted_combined_password --- src/gausskernel/optimizer/commands/user.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gausskernel/optimizer/commands/user.cpp b/src/gausskernel/optimizer/commands/user.cpp index 7115054fc..ae352ad88 100644 --- a/src/gausskernel/optimizer/commands/user.cpp +++ b/src/gausskernel/optimizer/commands/user.cpp @@ -5911,6 +5911,7 @@ Datum calculate_encrypted_combined_password(const char* password, const char* ro errno_t rc = EOK; /* For PG ecological compatibility, we stored both sha256 and md5 password. */ + /* the encrypted method of sha256 */ if (!pg_sha256_encrypt(password, salt_string, strlen(salt_string), @@ -5921,7 +5922,7 @@ Datum calculate_encrypted_combined_password(const char* password, const char* ro securec_check(rc, "\0", "\0"); ereport(ERROR, (errcode(ERRCODE_INVALID_PASSWORD), errmsg("first stage encryption password failed"))); } - + /* the encrypted method of md5 */ if (!pg_md5_encrypt(password, rolname, strlen(rolname), encrypted_md5_password)) { rc = memset_s(encrypted_md5_password, MD5_PASSWD_LEN + 1, 0, MD5_PASSWD_LEN + 1); securec_check(rc, "\0", "\0"); From 01163f2e40b1a5d87e1c4953ad0dcdea26caa407 Mon Sep 17 00:00:00 2001 From: Egok4wryu <15806957625@163.com> Date: Thu, 20 Oct 2022 11:31:29 +0800 Subject: [PATCH 5/5] add the annotation in the function of GsSm3Encrypt --- src/common/backend/libpq/sha2.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/common/backend/libpq/sha2.cpp b/src/common/backend/libpq/sha2.cpp index 3b996ffe2..484b55c18 100644 --- a/src/common/backend/libpq/sha2.cpp +++ b/src/common/backend/libpq/sha2.cpp @@ -772,6 +772,15 @@ bool pg_sha256_encrypt_for_md5(const char* password, const char* salt, size_t sa return true; } +/* + * @Description: calculate the encrypted password for GsSm3. + * @const char* password : the password need be encrypted. + * @const char* salt_s : the content fo the slat. + * @size_t salt_len : the length fo the slat. + * @char* buf : the buffer to store the encrypted key with GsSm3. + * @char* client_key_buf : the buffer to store the key of client. + * @int iteration_count : to record the number of the iteration. + */ bool GsSm3Encrypt( const char* password, const char* salt_s, size_t salt_len, char* buf, char* client_key_buf, int iteration_count) { @@ -799,6 +808,7 @@ bool GsSm3Encrypt( } password_len = strlen(password); + /* Tranform string(64Bytes) to binary(32Bytes) */ sha_hex_to_bytes32(salt, (char*)salt_s); /* calculate k */ pkcs_ret = PKCS5_PBKDF2_HMAC((char*)password,