| 1 |
package mint |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
) |
| 7 |
|
| 8 |
const ( |
| 9 |
tls13Version uint16 = 0x0304 |
| 10 |
tls12Version uint16 = 0x0303 |
| 11 |
tls10Version uint16 = 0x0301 |
| 12 |
dtls12WireVersion uint16 = 0xfefd |
| 13 |
) |
| 14 |
|
| 15 |
var ( |
| 16 |
|
| 17 |
allowWrongVersionNumber = true |
| 18 |
allowPKCS1 = true |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
type RecordType byte |
| 23 |
|
| 24 |
const ( |
| 25 |
RecordTypeAlert RecordType = 21 |
| 26 |
RecordTypeHandshake RecordType = 22 |
| 27 |
RecordTypeApplicationData RecordType = 23 |
| 28 |
RecordTypeAck RecordType = 25 |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
type HandshakeType byte |
| 33 |
|
| 34 |
const ( |
| 35 |
|
| 36 |
HandshakeTypeClientHello HandshakeType = 1 |
| 37 |
HandshakeTypeServerHello HandshakeType = 2 |
| 38 |
HandshakeTypeNewSessionTicket HandshakeType = 4 |
| 39 |
HandshakeTypeEndOfEarlyData HandshakeType = 5 |
| 40 |
HandshakeTypeHelloRetryRequest HandshakeType = 6 |
| 41 |
HandshakeTypeEncryptedExtensions HandshakeType = 8 |
| 42 |
HandshakeTypeCertificate HandshakeType = 11 |
| 43 |
HandshakeTypeCertificateRequest HandshakeType = 13 |
| 44 |
HandshakeTypeCertificateVerify HandshakeType = 15 |
| 45 |
HandshakeTypeServerConfiguration HandshakeType = 17 |
| 46 |
HandshakeTypeFinished HandshakeType = 20 |
| 47 |
HandshakeTypeKeyUpdate HandshakeType = 24 |
| 48 |
HandshakeTypeMessageHash HandshakeType = 254 |
| 49 |
) |
| 50 |
|
| 51 |
var hrrRandomSentinel = [32]byte{ |
| 52 |
0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, |
| 53 |
0xbe, 0x1d, 0x8c, 0x02, 0x1e, 0x65, 0xb8, 0x91, |
| 54 |
0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, |
| 55 |
0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
type CipherSuite uint16 |
| 60 |
|
| 61 |
const ( |
| 62 |
|
| 63 |
|
| 64 |
CIPHER_SUITE_UNKNOWN CipherSuite = 0x0000 |
| 65 |
TLS_AES_128_GCM_SHA256 CipherSuite = 0x1301 |
| 66 |
TLS_AES_256_GCM_SHA384 CipherSuite = 0x1302 |
| 67 |
TLS_CHACHA20_POLY1305_SHA256 CipherSuite = 0x1303 |
| 68 |
TLS_AES_128_CCM_SHA256 CipherSuite = 0x1304 |
| 69 |
TLS_AES_256_CCM_8_SHA256 CipherSuite = 0x1305 |
| 70 |
) |
| 71 |
|
| 72 |
func (c CipherSuite) String() string { |
| 73 |
switch c { |
| 74 |
case CIPHER_SUITE_UNKNOWN: |
| 75 |
return "unknown" |
| 76 |
case TLS_AES_128_GCM_SHA256: |
| 77 |
return "TLS_AES_128_GCM_SHA256" |
| 78 |
case TLS_AES_256_GCM_SHA384: |
| 79 |
return "TLS_AES_256_GCM_SHA384" |
| 80 |
case TLS_CHACHA20_POLY1305_SHA256: |
| 81 |
return "TLS_CHACHA20_POLY1305_SHA256" |
| 82 |
case TLS_AES_128_CCM_SHA256: |
| 83 |
return "TLS_AES_128_CCM_SHA256" |
| 84 |
case TLS_AES_256_CCM_8_SHA256: |
| 85 |
return "TLS_AES_256_CCM_8_SHA256" |
| 86 |
} |
| 87 |
|
| 88 |
return fmt.Sprintf("invalid CipherSuite value: 0x%s", strconv.FormatUint(uint64(c), 16)) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
type SignatureScheme uint16 |
| 93 |
|
| 94 |
const ( |
| 95 |
|
| 96 |
RSA_PKCS1_SHA1 SignatureScheme = 0x0201 |
| 97 |
RSA_PKCS1_SHA256 SignatureScheme = 0x0401 |
| 98 |
RSA_PKCS1_SHA384 SignatureScheme = 0x0501 |
| 99 |
RSA_PKCS1_SHA512 SignatureScheme = 0x0601 |
| 100 |
|
| 101 |
ECDSA_P256_SHA256 SignatureScheme = 0x0403 |
| 102 |
ECDSA_P384_SHA384 SignatureScheme = 0x0503 |
| 103 |
ECDSA_P521_SHA512 SignatureScheme = 0x0603 |
| 104 |
|
| 105 |
RSA_PSS_SHA256 SignatureScheme = 0x0804 |
| 106 |
RSA_PSS_SHA384 SignatureScheme = 0x0805 |
| 107 |
RSA_PSS_SHA512 SignatureScheme = 0x0806 |
| 108 |
|
| 109 |
Ed25519 SignatureScheme = 0x0807 |
| 110 |
Ed448 SignatureScheme = 0x0808 |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
type ExtensionType uint16 |
| 115 |
|
| 116 |
const ( |
| 117 |
ExtensionTypeServerName ExtensionType = 0 |
| 118 |
ExtensionTypeSupportedGroups ExtensionType = 10 |
| 119 |
ExtensionTypeSignatureAlgorithms ExtensionType = 13 |
| 120 |
ExtensionTypeALPN ExtensionType = 16 |
| 121 |
ExtensionTypeKeyShare ExtensionType = 51 |
| 122 |
ExtensionTypePreSharedKey ExtensionType = 41 |
| 123 |
ExtensionTypeEarlyData ExtensionType = 42 |
| 124 |
ExtensionTypeSupportedVersions ExtensionType = 43 |
| 125 |
ExtensionTypeCookie ExtensionType = 44 |
| 126 |
ExtensionTypePSKKeyExchangeModes ExtensionType = 45 |
| 127 |
ExtensionTypeTicketEarlyDataInfo ExtensionType = 46 |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
type NamedGroup uint16 |
| 132 |
|
| 133 |
const ( |
| 134 |
|
| 135 |
P256 NamedGroup = 23 |
| 136 |
P384 NamedGroup = 24 |
| 137 |
P521 NamedGroup = 25 |
| 138 |
|
| 139 |
X25519 NamedGroup = 29 |
| 140 |
X448 NamedGroup = 30 |
| 141 |
|
| 142 |
FFDHE2048 NamedGroup = 256 |
| 143 |
FFDHE3072 NamedGroup = 257 |
| 144 |
FFDHE4096 NamedGroup = 258 |
| 145 |
FFDHE6144 NamedGroup = 259 |
| 146 |
FFDHE8192 NamedGroup = 260 |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
type PSKKeyExchangeMode uint8 |
| 151 |
|
| 152 |
const ( |
| 153 |
PSKModeKE PSKKeyExchangeMode = 0 |
| 154 |
PSKModeDHEKE PSKKeyExchangeMode = 1 |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
type KeyUpdateRequest uint8 |
| 161 |
|
| 162 |
const ( |
| 163 |
KeyUpdateNotRequested KeyUpdateRequest = 0 |
| 164 |
KeyUpdateRequested KeyUpdateRequest = 1 |
| 165 |
) |
| 166 |
|
| 167 |
type State uint8 |
| 168 |
|
| 169 |
const ( |
| 170 |
StateInit = 0 |
| 171 |
|
| 172 |
|
| 173 |
StateClientStart State = iota |
| 174 |
StateClientWaitSH |
| 175 |
StateClientWaitEE |
| 176 |
StateClientWaitCert |
| 177 |
StateClientWaitCV |
| 178 |
StateClientWaitFinished |
| 179 |
StateClientWaitCertCR |
| 180 |
StateClientConnected |
| 181 |
|
| 182 |
StateServerStart State = iota |
| 183 |
StateServerRecvdCH |
| 184 |
StateServerNegotiated |
| 185 |
StateServerReadPastEarlyData |
| 186 |
StateServerWaitEOED |
| 187 |
StateServerWaitFlight2 |
| 188 |
StateServerWaitCert |
| 189 |
StateServerWaitCV |
| 190 |
StateServerWaitFinished |
| 191 |
StateServerConnected |
| 192 |
) |
| 193 |
|
| 194 |
func (s State) String() string { |
| 195 |
switch s { |
| 196 |
case StateClientStart: |
| 197 |
return "Client START" |
| 198 |
case StateClientWaitSH: |
| 199 |
return "Client WAIT_SH" |
| 200 |
case StateClientWaitEE: |
| 201 |
return "Client WAIT_EE" |
| 202 |
case StateClientWaitCert: |
| 203 |
return "Client WAIT_CERT" |
| 204 |
case StateClientWaitCV: |
| 205 |
return "Client WAIT_CV" |
| 206 |
case StateClientWaitFinished: |
| 207 |
return "Client WAIT_FINISHED" |
| 208 |
case StateClientWaitCertCR: |
| 209 |
return "Client WAIT_CERT_CR" |
| 210 |
case StateClientConnected: |
| 211 |
return "Client CONNECTED" |
| 212 |
case StateServerStart: |
| 213 |
return "Server START" |
| 214 |
case StateServerRecvdCH: |
| 215 |
return "Server RECVD_CH" |
| 216 |
case StateServerNegotiated: |
| 217 |
return "Server NEGOTIATED" |
| 218 |
case StateServerReadPastEarlyData: |
| 219 |
return "Server READ_PAST_EARLY_DATA" |
| 220 |
case StateServerWaitEOED: |
| 221 |
return "Server WAIT_EOED" |
| 222 |
case StateServerWaitFlight2: |
| 223 |
return "Server WAIT_FLIGHT2" |
| 224 |
case StateServerWaitCert: |
| 225 |
return "Server WAIT_CERT" |
| 226 |
case StateServerWaitCV: |
| 227 |
return "Server WAIT_CV" |
| 228 |
case StateServerWaitFinished: |
| 229 |
return "Server WAIT_FINISHED" |
| 230 |
case StateServerConnected: |
| 231 |
return "Server CONNECTED" |
| 232 |
default: |
| 233 |
return fmt.Sprintf("unknown state: %d", s) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
type Epoch uint16 |
| 239 |
|
| 240 |
const ( |
| 241 |
EpochClear Epoch = 0 |
| 242 |
EpochEarlyData Epoch = 1 |
| 243 |
EpochHandshakeData Epoch = 2 |
| 244 |
EpochApplicationData Epoch = 3 |
| 245 |
EpochUpdate Epoch = 4 |
| 246 |
) |
| 247 |
|
| 248 |
func (e Epoch) label() string { |
| 249 |
switch e { |
| 250 |
case EpochClear: |
| 251 |
return "clear" |
| 252 |
case EpochEarlyData: |
| 253 |
return "early data" |
| 254 |
case EpochHandshakeData: |
| 255 |
return "handshake" |
| 256 |
case EpochApplicationData: |
| 257 |
return "application data" |
| 258 |
} |
| 259 |
return "Application data (updated)" |
| 260 |
} |
| 261 |
|
| 262 |
func assert(b bool) { |
| 263 |
if !b { |
| 264 |
panic("Assertion failed") |
| 265 |
} |
| 266 |
} |