github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/sshutils/checker_test.go (about)

     1  /*
     2  Copyright 2019-2021 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sshutils
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"crypto/elliptic"
    22  	"crypto/rand"
    23  	"crypto/rsa"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  	"golang.org/x/crypto/ssh"
    28  
    29  	"github.com/gravitational/teleport/api/constants"
    30  )
    31  
    32  // TestCheckerValidate checks what algorithm are supported in regular (non-FIPS) mode.
    33  func TestCheckerValidate(t *testing.T) {
    34  	checker := CertChecker{}
    35  
    36  	rsaKey, err := rsa.GenerateKey(rand.Reader, constants.RSAKeySize)
    37  	require.NoError(t, err)
    38  	smallRSAKey, err := rsa.GenerateKey(rand.Reader, 1024)
    39  	require.NoError(t, err)
    40  	ellipticKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    41  	require.NoError(t, err)
    42  
    43  	// 2048-bit RSA keys are valid.
    44  	cryptoKey := rsaKey.Public()
    45  	sshKey, err := ssh.NewPublicKey(cryptoKey)
    46  	require.NoError(t, err)
    47  	err = checker.validateFIPS(sshKey)
    48  	require.NoError(t, err)
    49  
    50  	// 1024-bit RSA keys are valid.
    51  	cryptoKey = smallRSAKey.Public()
    52  	sshKey, err = ssh.NewPublicKey(cryptoKey)
    53  	require.NoError(t, err)
    54  	err = checker.validateFIPS(sshKey)
    55  	require.NoError(t, err)
    56  
    57  	// ECDSA keys are valid.
    58  	cryptoKey = ellipticKey.Public()
    59  	sshKey, err = ssh.NewPublicKey(cryptoKey)
    60  	require.NoError(t, err)
    61  	err = checker.validateFIPS(sshKey)
    62  	require.NoError(t, err)
    63  }
    64  
    65  // TestCheckerValidateFIPS makes sure the public key is a valid algorithm
    66  // that Teleport supports while in FIPS mode.
    67  func TestCheckerValidateFIPS(t *testing.T) {
    68  	checker := CertChecker{
    69  		FIPS: true,
    70  	}
    71  
    72  	rsaKey, err := rsa.GenerateKey(rand.Reader, constants.RSAKeySize)
    73  	require.NoError(t, err)
    74  	smallRSAKey, err := rsa.GenerateKey(rand.Reader, 1024)
    75  	require.NoError(t, err)
    76  	ellipticKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    77  	require.NoError(t, err)
    78  
    79  	// 2048-bit RSA keys are valid.
    80  	cryptoKey := rsaKey.Public()
    81  	sshKey, err := ssh.NewPublicKey(cryptoKey)
    82  	require.NoError(t, err)
    83  	err = checker.validateFIPS(sshKey)
    84  	require.NoError(t, err)
    85  
    86  	// 1024-bit RSA keys are not valid.
    87  	cryptoKey = smallRSAKey.Public()
    88  	sshKey, err = ssh.NewPublicKey(cryptoKey)
    89  	require.NoError(t, err)
    90  	err = checker.validateFIPS(sshKey)
    91  	require.Error(t, err)
    92  
    93  	// ECDSA keys are not valid.
    94  	cryptoKey = ellipticKey.Public()
    95  	sshKey, err = ssh.NewPublicKey(cryptoKey)
    96  	require.NoError(t, err)
    97  	err = checker.validateFIPS(sshKey)
    98  	require.Error(t, err)
    99  }