mirror of
https://github.com/gravitational/teleport.git
synced 2026-09-24 16:17:11 +08:00
100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
/*
|
|
Copyright 2019-2021 Gravitational, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package sshutils
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"testing"
|
|
|
|
"github.com/gravitational/teleport/api/constants"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// TestCheckerValidate checks what algorithm are supported in regular (non-FIPS) mode.
|
|
func TestCheckerValidate(t *testing.T) {
|
|
checker := CertChecker{}
|
|
|
|
rsaKey, err := rsa.GenerateKey(rand.Reader, constants.RSAKeySize)
|
|
require.NoError(t, err)
|
|
smallRSAKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
require.NoError(t, err)
|
|
ellipticKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
require.NoError(t, err)
|
|
|
|
// 2048-bit RSA keys are valid.
|
|
cryptoKey := rsaKey.Public()
|
|
sshKey, err := ssh.NewPublicKey(cryptoKey)
|
|
require.NoError(t, err)
|
|
err = checker.validateFIPS(sshKey)
|
|
require.NoError(t, err)
|
|
|
|
// 1024-bit RSA keys are valid.
|
|
cryptoKey = smallRSAKey.Public()
|
|
sshKey, err = ssh.NewPublicKey(cryptoKey)
|
|
require.NoError(t, err)
|
|
err = checker.validateFIPS(sshKey)
|
|
require.NoError(t, err)
|
|
|
|
// ECDSA keys are valid.
|
|
cryptoKey = ellipticKey.Public()
|
|
sshKey, err = ssh.NewPublicKey(cryptoKey)
|
|
require.NoError(t, err)
|
|
err = checker.validateFIPS(sshKey)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// TestCheckerValidateFIPS makes sure the public key is a valid algorithm
|
|
// that Teleport supports while in FIPS mode.
|
|
func TestCheckerValidateFIPS(t *testing.T) {
|
|
checker := CertChecker{
|
|
FIPS: true,
|
|
}
|
|
|
|
rsaKey, err := rsa.GenerateKey(rand.Reader, constants.RSAKeySize)
|
|
require.NoError(t, err)
|
|
smallRSAKey, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
require.NoError(t, err)
|
|
ellipticKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
require.NoError(t, err)
|
|
|
|
// 2048-bit RSA keys are valid.
|
|
cryptoKey := rsaKey.Public()
|
|
sshKey, err := ssh.NewPublicKey(cryptoKey)
|
|
require.NoError(t, err)
|
|
err = checker.validateFIPS(sshKey)
|
|
require.NoError(t, err)
|
|
|
|
// 1024-bit RSA keys are not valid.
|
|
cryptoKey = smallRSAKey.Public()
|
|
sshKey, err = ssh.NewPublicKey(cryptoKey)
|
|
require.NoError(t, err)
|
|
err = checker.validateFIPS(sshKey)
|
|
require.Error(t, err)
|
|
|
|
// ECDSA keys are not valid.
|
|
cryptoKey = ellipticKey.Public()
|
|
sshKey, err = ssh.NewPublicKey(cryptoKey)
|
|
require.NoError(t, err)
|
|
err = checker.validateFIPS(sshKey)
|
|
require.Error(t, err)
|
|
}
|