config_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package dtls
  4. import (
  5. "crypto/dsa" //nolint:staticcheck
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/tls"
  9. "errors"
  10. "testing"
  11. "github.com/pion/dtls/v2/pkg/crypto/selfsign"
  12. )
  13. func TestValidateConfig(t *testing.T) {
  14. cert, err := selfsign.GenerateSelfSigned()
  15. if err != nil {
  16. t.Fatalf("TestValidateConfig: Config validation error(%v), self signed certificate not generated", err)
  17. return
  18. }
  19. dsaPrivateKey := &dsa.PrivateKey{}
  20. err = dsa.GenerateParameters(&dsaPrivateKey.Parameters, rand.Reader, dsa.L1024N160)
  21. if err != nil {
  22. t.Fatalf("TestValidateConfig: Config validation error(%v), DSA parameters not generated", err)
  23. return
  24. }
  25. err = dsa.GenerateKey(dsaPrivateKey, rand.Reader)
  26. if err != nil {
  27. t.Fatalf("TestValidateConfig: Config validation error(%v), DSA private key not generated", err)
  28. return
  29. }
  30. rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  31. if err != nil {
  32. t.Fatalf("TestValidateConfig: Config validation error(%v), RSA private key not generated", err)
  33. return
  34. }
  35. cases := map[string]struct {
  36. config *Config
  37. wantAnyErr bool
  38. expErr error
  39. }{
  40. "Empty config": {
  41. expErr: errNoConfigProvided,
  42. },
  43. "PSK and Certificate, valid cipher suites": {
  44. config: &Config{
  45. CipherSuites: []CipherSuiteID{TLS_PSK_WITH_AES_128_CCM_8, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  46. PSK: func(hint []byte) ([]byte, error) {
  47. return nil, nil
  48. },
  49. Certificates: []tls.Certificate{cert},
  50. },
  51. },
  52. "PSK and Certificate, no PSK cipher suite": {
  53. config: &Config{
  54. CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  55. PSK: func(hint []byte) ([]byte, error) {
  56. return nil, nil
  57. },
  58. Certificates: []tls.Certificate{cert},
  59. },
  60. expErr: errNoAvailablePSKCipherSuite,
  61. },
  62. "PSK and Certificate, no non-PSK cipher suite": {
  63. config: &Config{
  64. CipherSuites: []CipherSuiteID{TLS_PSK_WITH_AES_128_CCM_8},
  65. PSK: func(hint []byte) ([]byte, error) {
  66. return nil, nil
  67. },
  68. Certificates: []tls.Certificate{cert},
  69. },
  70. expErr: errNoAvailableCertificateCipherSuite,
  71. },
  72. "PSK identity hint with not PSK": {
  73. config: &Config{
  74. CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  75. PSK: nil,
  76. PSKIdentityHint: []byte{},
  77. },
  78. expErr: errIdentityNoPSK,
  79. },
  80. "Invalid private key": {
  81. config: &Config{
  82. CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  83. Certificates: []tls.Certificate{{Certificate: cert.Certificate, PrivateKey: dsaPrivateKey}},
  84. },
  85. expErr: errInvalidPrivateKey,
  86. },
  87. "PrivateKey without Certificate": {
  88. config: &Config{
  89. CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  90. Certificates: []tls.Certificate{{PrivateKey: cert.PrivateKey}},
  91. },
  92. expErr: errInvalidCertificate,
  93. },
  94. "Invalid cipher suites": {
  95. config: &Config{CipherSuites: []CipherSuiteID{0x0000}},
  96. wantAnyErr: true,
  97. },
  98. "Valid config": {
  99. config: &Config{
  100. CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  101. Certificates: []tls.Certificate{cert, {Certificate: cert.Certificate, PrivateKey: rsaPrivateKey}},
  102. },
  103. },
  104. "Valid config with get certificate": {
  105. config: &Config{
  106. CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  107. GetCertificate: func(chi *ClientHelloInfo) (*tls.Certificate, error) {
  108. return &tls.Certificate{Certificate: cert.Certificate, PrivateKey: rsaPrivateKey}, nil
  109. },
  110. },
  111. },
  112. "Valid config with get client certificate": {
  113. config: &Config{
  114. CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
  115. GetClientCertificate: func(cri *CertificateRequestInfo) (*tls.Certificate, error) {
  116. return &tls.Certificate{Certificate: cert.Certificate, PrivateKey: rsaPrivateKey}, nil
  117. },
  118. },
  119. },
  120. }
  121. for name, testCase := range cases {
  122. testCase := testCase
  123. t.Run(name, func(t *testing.T) {
  124. err := validateConfig(testCase.config)
  125. if testCase.expErr != nil || testCase.wantAnyErr {
  126. if testCase.expErr != nil && !errors.Is(err, testCase.expErr) {
  127. t.Fatalf("TestValidateConfig: Config validation error exp(%v) failed(%v)", testCase.expErr, err)
  128. }
  129. if err == nil {
  130. t.Fatalf("TestValidateConfig: Config validation expected an error")
  131. }
  132. }
  133. })
  134. }
  135. }