1 package tls
2 3 // Printable names for diagnostics.
4 //
5 // fmt's variadics are Stringer, so an interface value or a slice cannot be
6 // passed to Errorf: stage4 rendered '?' or accepted it lazily, legacy rejects
7 // the package. These helpers give the same information as Go's %T, %x and %q
8 // without relying on a formatter that Moxie does not have.
9 10 import (
11 "crypto"
12 "crypto/ecdsa"
13 "crypto/ed25519"
14 "crypto/rsa"
15 "strconv"
16 )
17 18 // publicKeyName names a public key for a diagnostic.
19 func publicKeyName(k crypto.PublicKey) (n string) {
20 switch k.(type) {
21 case *rsa.PublicKey:
22 return "*rsa.PublicKey"
23 case *ecdsa.PublicKey:
24 return "*ecdsa.PublicKey"
25 case ed25519.PublicKey:
26 return "ed25519.PublicKey"
27 }
28 return "unknown"
29 }
30 31 // privateKeyName names a private key for a diagnostic.
32 func privateKeyName(k crypto.PrivateKey) (n string) {
33 switch k.(type) {
34 case *rsa.PrivateKey:
35 return "*rsa.PrivateKey"
36 case *ecdsa.PrivateKey:
37 return "*ecdsa.PrivateKey"
38 case ed25519.PrivateKey:
39 return "ed25519.PrivateKey"
40 }
41 return "unknown"
42 }
43 44 // handshakeMessageName names a handshake message's concrete type.
45 func handshakeMessageName(m handshakeMessage) (n string) {
46 switch m.(type) {
47 case *clientHelloMsg:
48 return "clientHelloMsg"
49 case *serverHelloMsg:
50 return "serverHelloMsg"
51 case *encryptedExtensionsMsg:
52 return "encryptedExtensionsMsg"
53 case *endOfEarlyDataMsg:
54 return "endOfEarlyDataMsg"
55 case *keyUpdateMsg:
56 return "keyUpdateMsg"
57 case *newSessionTicketMsgTLS13:
58 return "newSessionTicketMsgTLS13"
59 case *certificateRequestMsgTLS13:
60 return "certificateRequestMsgTLS13"
61 case *certificateMsg:
62 return "certificateMsg"
63 case *certificateMsgTLS13:
64 return "certificateMsgTLS13"
65 case *serverKeyExchangeMsg:
66 return "serverKeyExchangeMsg"
67 case *certificateStatusMsg:
68 return "certificateStatusMsg"
69 case *serverHelloDoneMsg:
70 return "serverHelloDoneMsg"
71 case *clientKeyExchangeMsg:
72 return "clientKeyExchangeMsg"
73 case *finishedMsg:
74 return "finishedMsg"
75 case *certificateRequestMsg:
76 return "certificateRequestMsg"
77 case *certificateVerifyMsg:
78 return "certificateVerifyMsg"
79 case *newSessionTicketMsg:
80 return "newSessionTicketMsg"
81 case *helloRequestMsg:
82 return "helloRequestMsg"
83 }
84 return "unknown"
85 }
86 87 // uint16sText renders a list of 16-bit values in hex, space separated.
88 func uint16sText(v []uint16) (s string) {
89 for i, x := range v {
90 if i > 0 {
91 s = s | " "
92 }
93 s = s | strconv.FormatUint(uint64(x), 16)
94 }
95 return s
96 }
97 98 // byteSlicesText renders a list of byte strings, quoted, space separated.
99 func byteSlicesText(v [][]byte) (s string) {
100 for i, x := range v {
101 if i > 0 {
102 s = s | " "
103 }
104 s = s | "\"" | string(x) | "\""
105 }
106 return s
107 }
108