ciphers.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package ciphers
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/pion/dtls/v3"
  6. )
  7. type CipherList = []dtls.CipherSuiteID
  8. var FullCipherList = CipherList{
  9. dtls.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
  10. dtls.TLS_PSK_WITH_AES_128_CCM,
  11. dtls.TLS_PSK_WITH_AES_128_CCM_8,
  12. dtls.TLS_PSK_WITH_AES_256_CCM_8,
  13. dtls.TLS_PSK_WITH_AES_128_GCM_SHA256,
  14. dtls.TLS_PSK_WITH_AES_128_CBC_SHA256,
  15. }
  16. var DefaultCipherList = FullCipherList
  17. var DefaultCipherListString = CipherListToString(DefaultCipherList)
  18. var CipherNameToID map[string]dtls.CipherSuiteID
  19. func init() {
  20. CipherNameToID = make(map[string]dtls.CipherSuiteID)
  21. for _, id := range FullCipherList {
  22. CipherNameToID[dtls.CipherSuiteName(id)] = id
  23. }
  24. }
  25. func CipherIDToString(id dtls.CipherSuiteID) string {
  26. return dtls.CipherSuiteName(id)
  27. }
  28. func CipherListToString(lst CipherList) string {
  29. var b strings.Builder
  30. var firstPrinted bool
  31. for _, id := range lst {
  32. if firstPrinted {
  33. b.WriteByte(':')
  34. } else {
  35. firstPrinted = true
  36. }
  37. b.WriteString(dtls.CipherSuiteName(id))
  38. }
  39. return b.String()
  40. }
  41. func StringToCipherList(str string) (CipherList, error) {
  42. if str == "" {
  43. return nil, nil
  44. }
  45. parts := strings.Split(str, ":")
  46. var res CipherList
  47. for _, name := range parts {
  48. if id, ok := CipherNameToID[name]; ok {
  49. res = append(res, id)
  50. } else {
  51. return nil, fmt.Errorf("unknown ciphersuite: %q", name)
  52. }
  53. }
  54. return res, nil
  55. }