pki_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package nebula
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/slackhq/nebula/cert"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestNewCipherSuite(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. curve cert.Curve
  13. cipher string
  14. fips140Enforced bool
  15. wantErr string
  16. // wantName is the full expected CipherSuite name (<DH>_<Cipher>_<Hash>),
  17. // only checked when wantErr is empty. Asserting the whole name makes both
  18. // the curve and cipher selection load-bearing.
  19. wantName string
  20. }{
  21. {
  22. name: "curve25519 aesgcm, not enforced",
  23. curve: cert.Curve_CURVE25519,
  24. cipher: "aesgcm",
  25. wantName: "25519_AESGCM_SHA256",
  26. },
  27. {
  28. name: "curve25519 chachapoly, not enforced",
  29. curve: cert.Curve_CURVE25519,
  30. cipher: "chachapoly",
  31. wantName: "25519_ChaChaPoly_SHA256",
  32. },
  33. {
  34. name: "p256 aesgcm, not enforced",
  35. curve: cert.Curve_P256,
  36. cipher: "aesgcm",
  37. wantName: "P256_AESGCM_SHA256",
  38. },
  39. {
  40. name: "p256 aesgcm, enforced is allowed",
  41. curve: cert.Curve_P256,
  42. cipher: "aesgcm",
  43. fips140Enforced: true,
  44. wantName: "P256_AESGCM_SHA256",
  45. },
  46. {
  47. name: "curve25519 rejected when enforced",
  48. curve: cert.Curve_CURVE25519,
  49. cipher: "aesgcm",
  50. fips140Enforced: true,
  51. wantErr: "pki: use of Curve25519 is not allowed in FIPS 140-only mode",
  52. },
  53. {
  54. name: "chachapoly rejected when enforced",
  55. curve: cert.Curve_P256,
  56. cipher: "chachapoly",
  57. fips140Enforced: true,
  58. wantErr: "pki: use of ChaChaPoly is not allowed in FIPS 140-only mode",
  59. },
  60. {
  61. // Curve is checked before cipher, so a Curve25519+ChaChaPoly
  62. // request reports the Curve25519 rejection.
  63. name: "curve25519 chachapoly rejected on curve when enforced",
  64. curve: cert.Curve_CURVE25519,
  65. cipher: "chachapoly",
  66. fips140Enforced: true,
  67. wantErr: "pki: use of Curve25519 is not allowed in FIPS 140-only mode",
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. cs, err := newCipherSuite(tt.curve, false, tt.cipher, tt.fips140Enforced)
  73. if tt.wantErr != "" {
  74. require.EqualError(t, err, tt.wantErr)
  75. assert.Nil(t, cs)
  76. return
  77. }
  78. require.NoError(t, err)
  79. require.NotNil(t, cs)
  80. assert.Equal(t, tt.wantName, string(cs.Name()))
  81. })
  82. }
  83. }
  84. func TestNewCipherSuiteUnsupportedCurve(t *testing.T) {
  85. cs, err := newCipherSuite(cert.Curve(99), false, "aesgcm", false)
  86. require.Error(t, err)
  87. assert.True(t, strings.HasPrefix(err.Error(), "unsupported curve:"), "got: %v", err)
  88. assert.Nil(t, cs)
  89. }