mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-19 03:35:12 +00:00
feat(crypto): FIPS 140-3 compliant build and runtime checks (#12233)
# Which Problems Are Solved Enable FIPS 140-3 compliant build. # How the Problems Are Solved - Add runtime config validation, if the FIPS flag is enabled fail the application when a non-compliant hasher is used, or throw a warning when a legacy verifier is used - Add a build matrix for FIPS certified build: - Go binary is built with `GOFIPS140=certified` - Login container uses a separate base: [ubi9](https://catalog.redhat.com/en/software/containers/ubi9/ubi/615bcf606feffc5384e8452e) from redhat which provides a FIPS certified OpenSSL (used by NodeJS TLS stack) - Non-FIPS images where already pushed to both Github Container Registry and Google Artifact Repository (GAR). Fips images are only pushed to the GAR. - Tag versions are suffixed `-fips`. So on release the following images will be additionally available: ``` europe-docker.pkg.dev/zitadel-common/zitadel-repo/zitadel-login:vX.Y.Z-fips europe-docker.pkg.dev/zitadel-common/zitadel-repo/zitadel:vX.Y.Z-fips-debug europe-docker.pkg.dev/zitadel-common/zitadel-repo/zitadel:vX.Y.Z-fips ``` # Other changes - Bumb Go toolchain. At least v1.25.10 is required for a GOFIPS140=certified setting. # Additional Context - Closes https://github.com/zitadel/zitadel/issues/4335 - Build [test run](https://github.com/zitadel/zitadel/actions/runs/27253916052) pushing FIPS and non-FIPS images
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/fips140"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/v3/instrumentation/logging"
|
||||
)
|
||||
|
||||
// fipsPBKDF2MinIterations is the minimum PBKDF2 iteration count per NIST SP 800-132 §5.2.
|
||||
const fipsPBKDF2MinIterations uint32 = 1000
|
||||
|
||||
// fips140Mode reports whether FIPS 140-3 application policies are enforced.
|
||||
// Defaults to crypto/fips140.Enabled; tests in this package may replace it.
|
||||
var fips140Mode = fips140.Enabled
|
||||
|
||||
func fips140Enabled() bool {
|
||||
return fips140Mode()
|
||||
}
|
||||
|
||||
// IsFIPSCompliant reports whether the algorithm is approved for password
|
||||
// hashing/verification under FIPS 140-3 (NIST SP 800-132).
|
||||
func (n HashName) IsFIPSCompliant() bool {
|
||||
return n == HashNamePBKDF2
|
||||
}
|
||||
|
||||
// IsFIPSCompliant reports whether the hash mode is an approved PBKDF2 PRF
|
||||
// under FIPS 140-3 (SHA-2 family; SHA-1/SHA-224 rejected for new use).
|
||||
func (m HashMode) IsFIPSCompliant() bool {
|
||||
return m == HashModeSHA256 || m == HashModeSHA384 || m == HashModeSHA512
|
||||
}
|
||||
|
||||
func nonFIPSVerifiersConfigured(verifiers []HashName) []HashName {
|
||||
var found []HashName
|
||||
for _, v := range verifiers {
|
||||
if !v.IsFIPSCompliant() {
|
||||
found = append(found, v)
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
func validateFIPSPBKDF2Hasher(c HasherConfig) error {
|
||||
p, hashMode, err := c.pbkdf2Params()
|
||||
if err != nil {
|
||||
return fmt.Errorf("decode pbkdf2 hasher for FIPS validation: %w", err)
|
||||
}
|
||||
if !hashMode.IsFIPSCompliant() {
|
||||
return fmt.Errorf(
|
||||
"application cannot start in uncertified cryptographic state: pbkdf2 hash mode %q is not FIPS 140-3 compliant while FIPS mode is enabled",
|
||||
hashMode,
|
||||
)
|
||||
}
|
||||
if p.Rounds < fipsPBKDF2MinIterations {
|
||||
return fmt.Errorf(
|
||||
"application cannot start in uncertified cryptographic state: pbkdf2 iteration count %d is below the FIPS minimum of %d while FIPS mode is enabled",
|
||||
p.Rounds, fipsPBKDF2MinIterations,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *HashConfig) validateFIPS140() error {
|
||||
if !fips140Enabled() {
|
||||
return nil
|
||||
}
|
||||
|
||||
alg := c.Hasher.Algorithm
|
||||
if !alg.IsFIPSCompliant() {
|
||||
return fmt.Errorf(
|
||||
"application cannot start in uncertified cryptographic state: password hasher algorithm %q is not FIPS 140-3 compliant while FIPS mode is enabled",
|
||||
alg,
|
||||
)
|
||||
}
|
||||
|
||||
if alg == HashNamePBKDF2 {
|
||||
if err := validateFIPSPBKDF2Hasher(c.Hasher); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if legacy := nonFIPSVerifiersConfigured(c.Verifiers); len(legacy) > 0 {
|
||||
logging.New(logging.StreamRuntime).Warn(
|
||||
"Non-FIPS compliant password verifiers are active for migration. This instance is temporarily non-compliant until these verifiers are disabled",
|
||||
slog.Any("verifiers", legacy),
|
||||
)
|
||||
}
|
||||
|
||||
if c.Limits.PBKDF2.MinRounds < fipsPBKDF2MinIterations {
|
||||
logging.New(logging.StreamRuntime).Warn(
|
||||
"PBKDF2 MinRounds is below the FIPS 140-3 minimum iteration count; imported hashes may use non-compliant cost parameters until limits are raised",
|
||||
slog.Uint64("min_rounds", uint64(c.Limits.PBKDF2.MinRounds)),
|
||||
slog.Uint64("fips_minimum", uint64(fipsPBKDF2MinIterations)),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func withFIPS140Enabled(t *testing.T) {
|
||||
t.Helper()
|
||||
prev := fips140Mode
|
||||
fips140Mode = func() bool { return true }
|
||||
t.Cleanup(func() { fips140Mode = prev })
|
||||
}
|
||||
|
||||
func TestHashName_IsFIPSCompliant(t *testing.T) {
|
||||
tests := []struct {
|
||||
name HashName
|
||||
want bool
|
||||
}{
|
||||
{HashNamePBKDF2, true},
|
||||
{HashNameArgon2, false},
|
||||
{HashNameArgon2i, false},
|
||||
{HashNameArgon2id, false},
|
||||
{HashNameBcrypt, false},
|
||||
{HashNameMd5, false},
|
||||
{HashNameMd5Plain, false},
|
||||
{HashNameMd5Salted, false},
|
||||
{HashNamePHPass, false},
|
||||
{HashNameSha2, false},
|
||||
{HashNameScrypt, false},
|
||||
{HashNameDrupal7, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(string(tt.name), func(t *testing.T) {
|
||||
assert.Equal(t, tt.want, tt.name.IsFIPSCompliant())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashMode_IsFIPSCompliant(t *testing.T) {
|
||||
tests := []struct {
|
||||
mode HashMode
|
||||
want bool
|
||||
}{
|
||||
{HashModeSHA256, true},
|
||||
{HashModeSHA384, true},
|
||||
{HashModeSHA512, true},
|
||||
{HashModeSHA1, false},
|
||||
{HashModeSHA224, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(string(tt.mode), func(t *testing.T) {
|
||||
assert.Equal(t, tt.want, tt.mode.IsFIPSCompliant())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonFIPSVerifiersConfigured(t *testing.T) {
|
||||
got := nonFIPSVerifiersConfigured([]HashName{HashNamePBKDF2, HashNameBcrypt, HashNameMd5})
|
||||
assert.Equal(t, []HashName{HashNameBcrypt, HashNameMd5}, got)
|
||||
assert.Equal(t, []HashName{HashNameSha2}, nonFIPSVerifiersConfigured([]HashName{HashNamePBKDF2, HashNameSha2}))
|
||||
assert.Nil(t, nonFIPSVerifiersConfigured([]HashName{HashNamePBKDF2}))
|
||||
}
|
||||
|
||||
func TestValidateFIPSPBKDF2Hasher(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
hasher HasherConfig
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "rounds below minimum",
|
||||
hasher: HasherConfig{
|
||||
Algorithm: HashNamePBKDF2,
|
||||
Params: map[string]any{
|
||||
"Rounds": 999,
|
||||
"Hash": HashModeSHA256,
|
||||
},
|
||||
},
|
||||
wantErr: "iteration count 999 is below the FIPS minimum",
|
||||
},
|
||||
{
|
||||
name: "rounds at minimum",
|
||||
hasher: HasherConfig{
|
||||
Algorithm: HashNamePBKDF2,
|
||||
Params: map[string]any{
|
||||
"Rounds": 1000,
|
||||
"Hash": HashModeSHA256,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sha1 hash mode",
|
||||
hasher: HasherConfig{
|
||||
Algorithm: HashNamePBKDF2,
|
||||
Params: map[string]any{
|
||||
"Rounds": 10000,
|
||||
"Hash": HashModeSHA1,
|
||||
},
|
||||
},
|
||||
wantErr: "hash mode \"sha1\" is not FIPS 140-3 compliant",
|
||||
},
|
||||
{
|
||||
name: "sha224 hash mode",
|
||||
hasher: HasherConfig{
|
||||
Algorithm: HashNamePBKDF2,
|
||||
Params: map[string]any{
|
||||
"Rounds": 10000,
|
||||
"Hash": HashModeSHA224,
|
||||
},
|
||||
},
|
||||
wantErr: "hash mode \"sha224\" is not FIPS 140-3 compliant",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateFIPSPBKDF2Hasher(tt.hasher)
|
||||
if tt.wantErr == "" {
|
||||
require.NoError(t, err)
|
||||
return
|
||||
}
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.wantErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashConfig_validateFIPS140(t *testing.T) {
|
||||
withFIPS140Enabled(t)
|
||||
|
||||
t.Run("bcrypt hasher fails", func(t *testing.T) {
|
||||
cfg := &HashConfig{
|
||||
Hasher: HasherConfig{
|
||||
Algorithm: HashNameBcrypt,
|
||||
Params: map[string]any{"Cost": 12},
|
||||
},
|
||||
Limits: HashLimitsConfig{
|
||||
Bcrypt: BcryptLimitsConfig{MinCost: 10, MaxCost: 16},
|
||||
},
|
||||
}
|
||||
err := cfg.validateFIPS140()
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "uncertified cryptographic state")
|
||||
assert.Contains(t, err.Error(), "bcrypt")
|
||||
})
|
||||
|
||||
t.Run("pbkdf2 compliant passes", func(t *testing.T) {
|
||||
cfg := &HashConfig{
|
||||
Hasher: HasherConfig{
|
||||
Algorithm: HashNamePBKDF2,
|
||||
Params: map[string]any{
|
||||
"Rounds": 290000,
|
||||
"Hash": HashModeSHA256,
|
||||
},
|
||||
},
|
||||
Limits: HashLimitsConfig{
|
||||
PBKDF2: PBKDF2LimitsConfig{MinRounds: 1000, MaxRounds: 10000000},
|
||||
},
|
||||
}
|
||||
require.NoError(t, cfg.validateFIPS140())
|
||||
})
|
||||
}
|
||||
|
||||
func TestHashConfig_NewHasher_FIPSBcryptFails(t *testing.T) {
|
||||
withFIPS140Enabled(t)
|
||||
|
||||
cfg := &HashConfig{
|
||||
Hasher: HasherConfig{
|
||||
Algorithm: HashNameBcrypt,
|
||||
Params: map[string]any{"Cost": 12},
|
||||
},
|
||||
Limits: HashLimitsConfig{
|
||||
Bcrypt: BcryptLimitsConfig{MinCost: 10, MaxCost: 16},
|
||||
},
|
||||
}
|
||||
_, err := cfg.NewHasher()
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "uncertified cryptographic state")
|
||||
}
|
||||
@@ -217,6 +217,9 @@ func (l Drupal7LimitsConfig) validationOpts() *drupal7.ValidationOpts {
|
||||
}
|
||||
|
||||
func (c *HashConfig) NewHasher() (*Hasher, error) {
|
||||
if err := c.validateFIPS140(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
verifiers, vPrefixes, err := c.buildVerifiers()
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInvalidArgument(err, "CRYPT-sahW9", "password hash config invalid")
|
||||
|
||||
Reference in New Issue
Block a user