version.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package fp
  2. import "fmt"
  3. // Version represents a TLS Version
  4. type Version uint16
  5. // NewVersion parses a version from a string, returning VersionEmpty if not recognized
  6. func NewVersion(s string) (Version, error) {
  7. var a Version
  8. err := a.Parse(s)
  9. return a, err
  10. }
  11. // Parse initializes a version from a string
  12. func (a *Version) Parse(s string) error {
  13. switch s {
  14. case "":
  15. *a = VersionEmpty
  16. case "2.0":
  17. *a = VersionSSL2
  18. case "3.0":
  19. *a = VersionSSL3
  20. case "3.1":
  21. *a = VersionTLS10
  22. case "3.2":
  23. *a = VersionTLS11
  24. case "3.3":
  25. *a = VersionTLS12
  26. case "3.4":
  27. *a = VersionTLS13
  28. default:
  29. return fmt.Errorf("invalid version: '%s'", s)
  30. }
  31. return nil
  32. }
  33. // String returns a string representation of the version
  34. func (a Version) String() string {
  35. switch a {
  36. case VersionEmpty:
  37. return ""
  38. case VersionSSL2:
  39. return "2.0"
  40. case VersionSSL3:
  41. return "3.0"
  42. case VersionTLS10:
  43. return "3.1"
  44. case VersionTLS11:
  45. return "3.2"
  46. case VersionTLS12:
  47. return "3.3"
  48. case VersionTLS13:
  49. return "3.4"
  50. default:
  51. return fmt.Sprintf("Version(%d)", a)
  52. }
  53. }
  54. // Grade returns a security grade for the version
  55. func (a Version) Grade() Grade {
  56. switch a {
  57. case VersionEmpty:
  58. return GradeEmpty
  59. case VersionTLS13, VersionTLS12:
  60. return GradeA
  61. case VersionTLS11, VersionTLS10:
  62. return GradeB
  63. case VersionSSL3:
  64. return GradeC
  65. default:
  66. return GradeF
  67. }
  68. }
  69. // Source:
  70. // - SSL0.2: https://www-archive.mozilla.org/projects/security/pki/nss/ssl/draft02.html
  71. // - SSL3.0: https://tools.ietf.org/html/draft-ietf-tls-ssl-version3-00#appendix-A.1.1
  72. // - TLS1.0: https://tools.ietf.org/html/draft-ietf-tls-protocol-01#appendix-A.2
  73. // - TLS1.1: https://www.ietf.org/rfc/rfc4346.txt
  74. // - TLS1.2: https://www.ietf.org/rfc/rfc5246.txt
  75. // - TLS1.3: https://tools.ietf.org/html/draft-ietf-tls-tls13-28#section-4.2.1
  76. const (
  77. VersionEmpty Version = 0
  78. VersionSSL2 Version = 0x0002
  79. VersionSSL3 Version = 0x0300
  80. VersionTLS10 Version = 0x0301
  81. VersionTLS11 Version = 0x0302
  82. VersionTLS12 Version = 0x0303
  83. VersionTLS13 Version = 0x0304
  84. )