]> Cypherpunks.ru repositories - gostls13.git/commitdiff
crypto/tls: add VersionName
authorFilippo Valsorda <filippo@golang.org>
Wed, 24 May 2023 00:29:15 +0000 (02:29 +0200)
committerGopher Robot <gobot@golang.org>
Thu, 25 May 2023 00:13:32 +0000 (00:13 +0000)
Fixes #46308

Change-Id: I5162b26cbce61ae5df5d2e093cf8a28406d15863
Reviewed-on: https://go-review.googlesource.com/c/go/+/497377
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Marten Seemann <martenseemann@gmail.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Samuli Silvius <samuli.silvius@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Filippo Valsorda <filippo@golang.org>

api/next/46308.txt [new file with mode: 0644]
src/crypto/tls/common.go
src/crypto/tls/tls_test.go

diff --git a/api/next/46308.txt b/api/next/46308.txt
new file mode 100644 (file)
index 0000000..ea6b605
--- /dev/null
@@ -0,0 +1 @@
+pkg crypto/tls, func VersionName(uint16) string #46308
index f49cc017602b89df7876a02568292c902aacc8bb..829db2316e88f93f544532a130d982a2e95ae8b4 100644 (file)
@@ -36,6 +36,26 @@ const (
        VersionSSL30 = 0x0300
 )
 
+// VersionName returns the name for the provided TLS version number
+// (e.g. "TLS 1.3"), or a fallback representation of the value if the
+// version is not implemented by this package.
+func VersionName(version uint16) string {
+       switch version {
+       case VersionSSL30:
+               return "SSLv3"
+       case VersionTLS10:
+               return "TLS 1.0"
+       case VersionTLS11:
+               return "TLS 1.1"
+       case VersionTLS12:
+               return "TLS 1.2"
+       case VersionTLS13:
+               return "TLS 1.3"
+       default:
+               return fmt.Sprintf("0x%04X", version)
+       }
+}
+
 const (
        maxPlaintext       = 16384        // maximum plaintext payload length
        maxCiphertext      = 16384 + 2048 // maximum ciphertext payload length
index 610cffa094e5c0a93b1e691551b3293e0e42f51a..4e4d6a9d215b146122b26de0a8e3af0973585529 100644 (file)
@@ -1590,6 +1590,15 @@ func TestCipherSuites(t *testing.T) {
        }
 }
 
+func TestVersionName(t *testing.T) {
+       if got, exp := VersionName(VersionTLS13), "TLS 1.3"; got != exp {
+               t.Errorf("unexpected VersionName: got %q, expected %q", got, exp)
+       }
+       if got, exp := VersionName(0x12a), "0x012A"; got != exp {
+               t.Errorf("unexpected fallback VersionName: got %q, expected %q", got, exp)
+       }
+}
+
 // http2isBadCipher is copied from net/http.
 // TODO: if it ends up exposed somewhere, use that instead.
 func http2isBadCipher(cipher uint16) bool {