Error This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms

Blog นี้เรื่องของเรื่องเลย อยู่ๆ Lib VB6-DOTNET Connector เมื่อ 8-9 ปีก่อน ที่เคยทำไว้ก็พอลูกค้าเอาไปติดตั้งที่ Site ใหม่ มันก็แจ้ง Error ตามด้านล่างเลยครับ

System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.

แล้วที่นี้พอมาหาสาเหตุ ตอนแรกคิดว่ามันจาก VB6 หรือ ป่าว สรุปมันมาจาก dotnet เจอ Exception แล้วส่ง Message ให้ทาง VB6 เอาไปแสดงผลและครับ

Error This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms คือ อะไร

FIPS (Federal Information Processing Standard) คือ ข้อกำหนดโดยรัฐบาลสหรัฐอเมริกา ซึ่งเกี่ยวข้องกับการเข้ารหัสลับและมาตรการความปลอดภัยอื่นๆ ที่หน่วยงานรัฐของสหรัฐอเมริกาใช้งาน ตอนนี้น่าจะใช้ Version FIPS 140-2 นั้นเอง

โดยปกติแล้ว OS มันมีกันหมดแต่จะเปิด หรือ ปิดขึ้นกับข้อตกลงตอน Hardening ระบบ อย่างตัว สำหรับใน Blog นี้จะเป็น Windows แต่ตัว Linux อย่าง RedHat ก็มีเหมือนกัน

ทำไมอยู่ๆ Code dotnet มันแจ้งหละ ?

เงื่อนไขมี 2 ส่วน ที่ต้องเป็นจริง

📌 ตัว OS มีการเปิด FIPS Mode ทั้งจาก

  • Local Security Policy > System cryptography: Use FIPS compliant algorithms for encryption, hashing, and signing (Enabled)
  • หรือ ใน registry HKLM\System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy\Enabled มีค่าเป็น 1

📌 และ ใน Code มีเรียกใช้ Algorithm ที่ไม่ผ่านข้อกำหนดของ FIPS อาทิ เช่น

using System.Security.Cryptography;

public class Example
{
    public static void Main()
    {
        var md5 = new MD5CryptoServiceProvider(); // Not FIPS-compliant
        var sha1 = new SHA1Managed(); // Not FIPS-compliant
        var sha256 = new SHA256Managed(); // Not FIPS-compliant
        var rijndael = new RijndaelManaged(); // Not FIPS-compliant
        var aes = new AesManaged(); // Not FIPS-compliant
    }
}

Solution

- แก้ที่ ENV
  1. Control Panel > Administrative Tools > Local Security Policy > Local Policies > Security Options
  2. จากนั้นแก้ไขค่า System cryptography: Use FIPS compliant algorithms for encryption, hashing, and signing เปลี่ยนค่าเป็น Disable

ปกติวิธีนี้ไม่ต้อง restart เครื่องนะ แต่ต้องตรวจสอบ local และ group policy

แต่ถ้าลองแล้วมันไม่ Work ต้องไปแก้ที่ Registry Key ตามนี้

HKLM\System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy\Enabled

จากนั้นก็กำหนดค่าเป็น 0 แล้ว restart เครื่องนะ

ก่อนทำ อย่าลืมสำรวจ Policy ก่อนจะ ว่า Policy เค้าสั่งให้เปิดเป็น Default หรือป่าว แล้วถ้าจะเอาออก อาจจะต้องมี Process ภายในของลูกค้าเอง

- แก้ที่ Code

ต้องไปดูก่อน ว่า Algorithm เดิมที่เราใช้ มันรองรับผ่านข้อกำหนดของ FIPS ไหม

  • ถ้าไม่ผ่าน อย่าง md5 ต้องไปใช้ Algorithm ที่ดีกว่า อย่างเช่น อันนี้ md5 ไม่เข้าช่าย ย้ายไปใช้ SHA ส่วนลูกค้าเดิมยัง md5 แบบว่ายังมีเงื่อนไขบางอย่างทีที่ยังบังคับขยับไม่ได้
using System;
using System.Security.Cryptography;

public static class FipsAwareHasher
{
    public static byte[] ComputeHash(byte[] data)
    {
        if (CryptoConfig.AllowOnlyFipsAlgorithms)  //<< dotnet property to check env is FIPS
        {
            // FIPS mode enforced: use FIPS-approved SHA256
            using (SHA256 sha = SHA256.Create())
            {
                return sha.ComputeHash(data);
            }
        }
        else
        {
            // FIPS mode NOT enforced: non-FIPS logic (example: MD5, or any custom logic)
            using (MD5 md5 = MD5.Create())
            {
                return md5.ComputeHash(data);
            }
        }
    }
}
//ถ้าไม่อยากใช้ CryptoConfig.AllowOnlyFipsAlgorithms เขียน Helper อ่าน Registry แบบดิบๆได้
public bool IsFipsPolicyEnabled()
{
   RegistryKey uac = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy", true);
   if (uac == null)
   {
      return false;
   }
   return (bool)uac.GetValue("Enabled");
}
  • หรือ ถ้า Algorithm เดิม ผ่านข้อกำหนดของ FIPS ต้องสร้าง Class ขึ้นมาให้ไปเรียกใช้จาก Factory Method Create() แทน ให้มันไปดู ENV แล้วดึง Class ที่ใช่มาให้ อาทิ เช่น
using (SHA256 sha = SHA256.Create())
{
    // In FIPS mode: uses OS-provided (FIPS-validated) implementation
    // Not in FIPS mode: may use non-FIPS .NET managed implementation
}

Error นี้มันไม่ได้เกิดกับ dotnet เพียงอย่างเดียว อย่างที่ผมทำงานมาเจอฝั่ง JAVA ด้วย [PODMAN] java.security.KeyManagementException: FIPS mode: only SunJSSE TrustManagers may be used

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.