| 1 |
package mint |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"net" |
| 7 |
) |
| 8 |
|
| 9 |
const ( |
| 10 |
handshakeHeaderLenTLS = 4 |
| 11 |
handshakeHeaderLenDTLS = 12 |
| 12 |
maxHandshakeMessageLen = 1 << 24 |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
type HandshakeMessage struct { |
| 32 |
msgType HandshakeType |
| 33 |
seq uint32 |
| 34 |
body []byte |
| 35 |
datagram bool |
| 36 |
offset uint32 |
| 37 |
length uint32 |
| 38 |
cipher *cipherState |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func (hm *HandshakeMessage) Marshal() []byte { |
| 46 |
if hm == nil { |
| 47 |
return []byte{} |
| 48 |
} |
| 49 |
|
| 50 |
fragLen := len(hm.body) |
| 51 |
var data []byte |
| 52 |
|
| 53 |
if hm.datagram { |
| 54 |
data = make([]byte, handshakeHeaderLenDTLS+fragLen) |
| 55 |
} else { |
| 56 |
data = make([]byte, handshakeHeaderLenTLS+fragLen) |
| 57 |
} |
| 58 |
tmp := data |
| 59 |
tmp = encodeUint(uint64(hm.msgType), 1, tmp) |
| 60 |
tmp = encodeUint(uint64(hm.length), 3, tmp) |
| 61 |
if hm.datagram { |
| 62 |
tmp = encodeUint(uint64(hm.seq), 2, tmp) |
| 63 |
tmp = encodeUint(uint64(hm.offset), 3, tmp) |
| 64 |
tmp = encodeUint(uint64(fragLen), 3, tmp) |
| 65 |
} |
| 66 |
copy(tmp, hm.body) |
| 67 |
return data |
| 68 |
} |
| 69 |
|
| 70 |
func (hm HandshakeMessage) ToBody() (HandshakeMessageBody, error) { |
| 71 |
logf(logTypeHandshake, "HandshakeMessage.toBody [%d] [%x]", hm.msgType, hm.body) |
| 72 |
|
| 73 |
var body HandshakeMessageBody |
| 74 |
switch hm.msgType { |
| 75 |
case HandshakeTypeClientHello: |
| 76 |
body = new(ClientHelloBody) |
| 77 |
case HandshakeTypeServerHello: |
| 78 |
body = new(ServerHelloBody) |
| 79 |
case HandshakeTypeEncryptedExtensions: |
| 80 |
body = new(EncryptedExtensionsBody) |
| 81 |
case HandshakeTypeCertificate: |
| 82 |
body = new(CertificateBody) |
| 83 |
case HandshakeTypeCertificateRequest: |
| 84 |
body = new(CertificateRequestBody) |
| 85 |
case HandshakeTypeCertificateVerify: |
| 86 |
body = new(CertificateVerifyBody) |
| 87 |
case HandshakeTypeFinished: |
| 88 |
body = &FinishedBody{VerifyDataLen: len(hm.body)} |
| 89 |
case HandshakeTypeNewSessionTicket: |
| 90 |
body = new(NewSessionTicketBody) |
| 91 |
case HandshakeTypeKeyUpdate: |
| 92 |
body = new(KeyUpdateBody) |
| 93 |
case HandshakeTypeEndOfEarlyData: |
| 94 |
body = new(EndOfEarlyDataBody) |
| 95 |
default: |
| 96 |
return body, fmt.Errorf("tls.handshakemessage: Unsupported body type") |
| 97 |
} |
| 98 |
|
| 99 |
err := safeUnmarshal(body, hm.body) |
| 100 |
return body, err |
| 101 |
} |
| 102 |
|
| 103 |
func (h *HandshakeLayer) HandshakeMessageFromBody(body HandshakeMessageBody) (*HandshakeMessage, error) { |
| 104 |
data, err := body.Marshal() |
| 105 |
if err != nil { |
| 106 |
return nil, err |
| 107 |
} |
| 108 |
|
| 109 |
m := &HandshakeMessage{ |
| 110 |
msgType: body.Type(), |
| 111 |
body: data, |
| 112 |
seq: h.msgSeq, |
| 113 |
datagram: h.datagram, |
| 114 |
length: uint32(len(data)), |
| 115 |
} |
| 116 |
h.msgSeq++ |
| 117 |
return m, nil |
| 118 |
} |
| 119 |
|
| 120 |
type HandshakeLayer struct { |
| 121 |
ctx *HandshakeContext |
| 122 |
nonblocking bool |
| 123 |
conn RecordLayer |
| 124 |
frame *frameReader |
| 125 |
datagram bool |
| 126 |
msgSeq uint32 |
| 127 |
queued []*HandshakeMessage |
| 128 |
sent []*HandshakeMessage |
| 129 |
recvdRecords []uint64 |
| 130 |
maxFragmentLen int |
| 131 |
} |
| 132 |
|
| 133 |
type handshakeLayerFrameDetails struct { |
| 134 |
datagram bool |
| 135 |
} |
| 136 |
|
| 137 |
func (d handshakeLayerFrameDetails) headerLen() int { |
| 138 |
if d.datagram { |
| 139 |
return handshakeHeaderLenDTLS |
| 140 |
} |
| 141 |
return handshakeHeaderLenTLS |
| 142 |
} |
| 143 |
|
| 144 |
func (d handshakeLayerFrameDetails) defaultReadLen() int { |
| 145 |
return d.headerLen() + maxFragmentLen |
| 146 |
} |
| 147 |
|
| 148 |
func (d handshakeLayerFrameDetails) frameLen(hdr []byte) (int, error) { |
| 149 |
logf(logTypeIO, "Header=%x", hdr) |
| 150 |
|
| 151 |
|
| 152 |
val, _ := decodeUint(hdr[len(hdr)-3:], 3) |
| 153 |
return int(val), nil |
| 154 |
} |
| 155 |
|
| 156 |
func NewHandshakeLayerTLS(c *HandshakeContext, r RecordLayer) *HandshakeLayer { |
| 157 |
h := HandshakeLayer{} |
| 158 |
h.ctx = c |
| 159 |
h.conn = r |
| 160 |
h.datagram = false |
| 161 |
h.frame = newFrameReader(&handshakeLayerFrameDetails{false}) |
| 162 |
h.maxFragmentLen = maxFragmentLen |
| 163 |
return &h |
| 164 |
} |
| 165 |
|
| 166 |
func NewHandshakeLayerDTLS(c *HandshakeContext, r RecordLayer) *HandshakeLayer { |
| 167 |
h := HandshakeLayer{} |
| 168 |
h.ctx = c |
| 169 |
h.conn = r |
| 170 |
h.datagram = true |
| 171 |
h.frame = newFrameReader(&handshakeLayerFrameDetails{true}) |
| 172 |
h.maxFragmentLen = initialMtu |
| 173 |
return &h |
| 174 |
} |
| 175 |
|
| 176 |
func (h *HandshakeLayer) readRecord() error { |
| 177 |
var pt *TLSPlaintext |
| 178 |
var err error |
| 179 |
|
| 180 |
if h.datagram { |
| 181 |
logf(logTypeVerbose, "Trying to read record") |
| 182 |
pt, err = h.conn.(*DefaultRecordLayer).ReadRecordAnyEpoch() |
| 183 |
} else { |
| 184 |
pt, err = h.conn.ReadRecord() |
| 185 |
} |
| 186 |
if err != nil { |
| 187 |
return err |
| 188 |
} |
| 189 |
|
| 190 |
switch pt.contentType { |
| 191 |
case RecordTypeHandshake, RecordTypeAlert, RecordTypeAck: |
| 192 |
default: |
| 193 |
return fmt.Errorf("tls.handshakelayer: Unexpected record type %d", pt.contentType) |
| 194 |
} |
| 195 |
|
| 196 |
if pt.contentType == RecordTypeAck { |
| 197 |
if !h.datagram { |
| 198 |
return fmt.Errorf("tls.handshakelayer: can't have ACK with TLS") |
| 199 |
} |
| 200 |
logf(logTypeIO, "read ACK") |
| 201 |
return h.ctx.processAck(pt.fragment) |
| 202 |
} |
| 203 |
|
| 204 |
if pt.contentType == RecordTypeAlert { |
| 205 |
logf(logTypeIO, "read alert %v", pt.fragment[1]) |
| 206 |
if len(pt.fragment) < 2 { |
| 207 |
h.sendAlert(AlertUnexpectedMessage) |
| 208 |
return io.EOF |
| 209 |
} |
| 210 |
return Alert(pt.fragment[1]) |
| 211 |
} |
| 212 |
|
| 213 |
assert(h.ctx.hIn.conn != nil) |
| 214 |
if pt.epoch != h.ctx.hIn.conn.Epoch() { |
| 215 |
|
| 216 |
|
| 217 |
if pt.epoch == EpochClear || pt.epoch == EpochHandshakeData { |
| 218 |
return nil |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
return AlertIllegalParameter |
| 223 |
} |
| 224 |
|
| 225 |
h.recvdRecords = append(h.recvdRecords, pt.seq) |
| 226 |
h.frame.addChunk(pt.fragment) |
| 227 |
|
| 228 |
return nil |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
func (h *HandshakeLayer) sendAlert(err Alert) error { |
| 233 |
tmp := make([]byte, 2) |
| 234 |
tmp[0] = AlertLevelError |
| 235 |
tmp[1] = byte(err) |
| 236 |
h.conn.WriteRecord(&TLSPlaintext{ |
| 237 |
contentType: RecordTypeAlert, |
| 238 |
fragment: tmp}, |
| 239 |
) |
| 240 |
|
| 241 |
|
| 242 |
if err != AlertCloseNotify { |
| 243 |
return &net.OpError{Op: "local error", Err: err} |
| 244 |
} |
| 245 |
return nil |
| 246 |
} |
| 247 |
|
| 248 |
func (h *HandshakeLayer) noteMessageDelivered(seq uint32) { |
| 249 |
h.msgSeq = seq + 1 |
| 250 |
var i int |
| 251 |
var m *HandshakeMessage |
| 252 |
for i, m = range h.queued { |
| 253 |
if m.seq > seq { |
| 254 |
break |
| 255 |
} |
| 256 |
} |
| 257 |
h.queued = h.queued[i:] |
| 258 |
} |
| 259 |
|
| 260 |
func (h *HandshakeLayer) newFragmentReceived(hm *HandshakeMessage) (*HandshakeMessage, error) { |
| 261 |
if hm.seq < h.msgSeq { |
| 262 |
return nil, nil |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
h.ctx.receivedHandshakeMessage() |
| 268 |
|
| 269 |
if hm.seq == h.msgSeq && hm.offset == 0 && hm.length == uint32(len(hm.body)) { |
| 270 |
|
| 271 |
|
| 272 |
h.noteMessageDelivered(hm.seq) |
| 273 |
return hm, nil |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
var i int |
| 278 |
for i = 0; i < len(h.queued); i++ { |
| 279 |
f := h.queued[i] |
| 280 |
if hm.seq < f.seq { |
| 281 |
break |
| 282 |
} |
| 283 |
if hm.offset < f.offset { |
| 284 |
break |
| 285 |
} |
| 286 |
} |
| 287 |
tmp := make([]*HandshakeMessage, 0, len(h.queued)+1) |
| 288 |
tmp = append(tmp, h.queued[:i]...) |
| 289 |
tmp = append(tmp, hm) |
| 290 |
tmp = append(tmp, h.queued[i:]...) |
| 291 |
h.queued = tmp |
| 292 |
|
| 293 |
return h.checkMessageAvailable() |
| 294 |
} |
| 295 |
|
| 296 |
func (h *HandshakeLayer) checkMessageAvailable() (*HandshakeMessage, error) { |
| 297 |
if len(h.queued) == 0 { |
| 298 |
return nil, nil |
| 299 |
} |
| 300 |
|
| 301 |
hm := h.queued[0] |
| 302 |
if hm.seq != h.msgSeq { |
| 303 |
return nil, nil |
| 304 |
} |
| 305 |
|
| 306 |
if hm.seq == h.msgSeq && hm.offset == 0 && hm.length == uint32(len(hm.body)) { |
| 307 |
|
| 308 |
|
| 309 |
h.noteMessageDelivered(hm.seq) |
| 310 |
return hm, nil |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
end := uint32(0) |
| 315 |
buf := make([]byte, hm.length) |
| 316 |
|
| 317 |
for _, f := range h.queued { |
| 318 |
|
| 319 |
if f.seq > hm.seq { |
| 320 |
break |
| 321 |
} |
| 322 |
|
| 323 |
if f.length != uint32(len(buf)) { |
| 324 |
return nil, fmt.Errorf("Mismatched DTLS length") |
| 325 |
} |
| 326 |
|
| 327 |
if f.offset > end { |
| 328 |
break |
| 329 |
} |
| 330 |
|
| 331 |
if f.offset+uint32(len(f.body)) > end { |
| 332 |
|
| 333 |
copy(buf[f.offset:], f.body) |
| 334 |
end = f.offset + uint32(len(f.body)) |
| 335 |
if end == hm.length { |
| 336 |
h2 := *hm |
| 337 |
h2.offset = 0 |
| 338 |
h2.body = buf |
| 339 |
h.noteMessageDelivered(hm.seq) |
| 340 |
return &h2, nil |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
return nil, nil |
| 347 |
} |
| 348 |
|
| 349 |
func (h *HandshakeLayer) ReadMessage() (*HandshakeMessage, error) { |
| 350 |
var hdr, body []byte |
| 351 |
var err error |
| 352 |
|
| 353 |
hm, err := h.checkMessageAvailable() |
| 354 |
if err != nil { |
| 355 |
return nil, err |
| 356 |
} |
| 357 |
if hm != nil { |
| 358 |
return hm, nil |
| 359 |
} |
| 360 |
for { |
| 361 |
logf(logTypeVerbose, "ReadMessage() buffered=%v", len(h.frame.remainder)) |
| 362 |
if h.frame.needed() > 0 { |
| 363 |
logf(logTypeVerbose, "Trying to read a new record") |
| 364 |
err = h.readRecord() |
| 365 |
|
| 366 |
if err != nil && (h.nonblocking || err != AlertWouldBlock) { |
| 367 |
return nil, err |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
hdr, body, err = h.frame.process() |
| 372 |
if err == nil { |
| 373 |
break |
| 374 |
} |
| 375 |
if err != nil && (h.nonblocking || err != AlertWouldBlock) { |
| 376 |
return nil, err |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
logf(logTypeHandshake, "read handshake message") |
| 381 |
|
| 382 |
hm = &HandshakeMessage{} |
| 383 |
hm.msgType = HandshakeType(hdr[0]) |
| 384 |
hm.datagram = h.datagram |
| 385 |
hm.body = make([]byte, len(body)) |
| 386 |
copy(hm.body, body) |
| 387 |
logf(logTypeHandshake, "Read message with type: %v", hm.msgType) |
| 388 |
if h.datagram { |
| 389 |
tmp, hdr := decodeUint(hdr[1:], 3) |
| 390 |
hm.length = uint32(tmp) |
| 391 |
tmp, hdr = decodeUint(hdr, 2) |
| 392 |
hm.seq = uint32(tmp) |
| 393 |
tmp, hdr = decodeUint(hdr, 3) |
| 394 |
hm.offset = uint32(tmp) |
| 395 |
|
| 396 |
return h.newFragmentReceived(hm) |
| 397 |
} |
| 398 |
|
| 399 |
hm.length = uint32(len(body)) |
| 400 |
return hm, nil |
| 401 |
} |
| 402 |
|
| 403 |
func (h *HandshakeLayer) QueueMessage(hm *HandshakeMessage) error { |
| 404 |
if h.datagram { |
| 405 |
hm.cipher = h.conn.(*DefaultRecordLayer).cipher |
| 406 |
h.queued = append(h.queued, hm) |
| 407 |
return nil |
| 408 |
} |
| 409 |
_, err := h.WriteMessages([]*HandshakeMessage{hm}) |
| 410 |
return err |
| 411 |
} |
| 412 |
|
| 413 |
func (h *HandshakeLayer) SendQueuedMessages() (int, error) { |
| 414 |
logf(logTypeHandshake, "Sending outgoing messages") |
| 415 |
count, err := h.WriteMessages(h.queued) |
| 416 |
if !h.datagram { |
| 417 |
h.ClearQueuedMessages() |
| 418 |
} |
| 419 |
return count, err |
| 420 |
} |
| 421 |
|
| 422 |
func (h *HandshakeLayer) ClearQueuedMessages() { |
| 423 |
logf(logTypeHandshake, "Clearing outgoing hs message queue") |
| 424 |
h.queued = nil |
| 425 |
} |
| 426 |
|
| 427 |
func (h *HandshakeLayer) writeFragment(hm *HandshakeMessage, start int, room int) (bool, int, error) { |
| 428 |
var buf []byte |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
hdrlen := 0 |
| 433 |
if hm.datagram { |
| 434 |
hdrlen = handshakeHeaderLenDTLS |
| 435 |
} else if start == 0 { |
| 436 |
hdrlen = handshakeHeaderLenTLS |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
room -= hdrlen |
| 441 |
if room == 0 { |
| 442 |
|
| 443 |
|
| 444 |
panic("Too short max fragment len") |
| 445 |
} |
| 446 |
bodylen := len(hm.body) - start |
| 447 |
if bodylen > room { |
| 448 |
bodylen = room |
| 449 |
} |
| 450 |
body := hm.body[start : start+bodylen] |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
if h.ctx.fragmentAcked(hm.seq, start, bodylen) { |
| 455 |
logf(logTypeHandshake, "Fragment %v %v(%v) already acked. Skipping", hm.seq, start, bodylen) |
| 456 |
return false, start + bodylen, nil |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
if hdrlen > 0 { |
| 461 |
hm2 := *hm |
| 462 |
hm2.offset = uint32(start) |
| 463 |
hm2.body = body |
| 464 |
buf = hm2.Marshal() |
| 465 |
hm = &hm2 |
| 466 |
} else { |
| 467 |
buf = body |
| 468 |
} |
| 469 |
|
| 470 |
var err error |
| 471 |
if h.datagram { |
| 472 |
|
| 473 |
h.ctx.sentFragments = append(h.ctx.sentFragments, &SentHandshakeFragment{ |
| 474 |
hm.seq, |
| 475 |
start, |
| 476 |
len(body), |
| 477 |
h.conn.(*DefaultRecordLayer).cipher.combineSeq(true), |
| 478 |
false, |
| 479 |
}) |
| 480 |
err = h.conn.(*DefaultRecordLayer).writeRecordWithPadding( |
| 481 |
&TLSPlaintext{ |
| 482 |
contentType: RecordTypeHandshake, |
| 483 |
fragment: buf, |
| 484 |
}, |
| 485 |
hm.cipher, 0) |
| 486 |
} else { |
| 487 |
err = h.conn.WriteRecord( |
| 488 |
&TLSPlaintext{ |
| 489 |
contentType: RecordTypeHandshake, |
| 490 |
fragment: buf, |
| 491 |
}) |
| 492 |
} |
| 493 |
return true, start + bodylen, err |
| 494 |
} |
| 495 |
|
| 496 |
func (h *HandshakeLayer) WriteMessage(hm *HandshakeMessage) (int, error) { |
| 497 |
start := int(0) |
| 498 |
|
| 499 |
if len(hm.body) > maxHandshakeMessageLen { |
| 500 |
return 0, fmt.Errorf("Tried to write a handshake message that's too long") |
| 501 |
} |
| 502 |
|
| 503 |
written := 0 |
| 504 |
wrote := false |
| 505 |
|
| 506 |
|
| 507 |
for { |
| 508 |
var err error |
| 509 |
wrote, start, err = h.writeFragment(hm, start, h.maxFragmentLen) |
| 510 |
if err != nil { |
| 511 |
return 0, err |
| 512 |
} |
| 513 |
if wrote { |
| 514 |
written++ |
| 515 |
} |
| 516 |
if start >= len(hm.body) { |
| 517 |
break |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
return written, nil |
| 522 |
} |
| 523 |
|
| 524 |
func (h *HandshakeLayer) WriteMessages(hms []*HandshakeMessage) (int, error) { |
| 525 |
written := 0 |
| 526 |
for _, hm := range hms { |
| 527 |
logf(logTypeHandshake, "WriteMessage [%d] %x", hm.msgType, hm.body) |
| 528 |
|
| 529 |
wrote, err := h.WriteMessage(hm) |
| 530 |
if err != nil { |
| 531 |
return 0, err |
| 532 |
} |
| 533 |
written += wrote |
| 534 |
} |
| 535 |
return written, nil |
| 536 |
} |
| 537 |
|
| 538 |
func encodeUint(v uint64, size int, out []byte) []byte { |
| 539 |
for i := size - 1; i >= 0; i-- { |
| 540 |
out[i] = byte(v & 0xff) |
| 541 |
v >>= 8 |
| 542 |
} |
| 543 |
return out[size:] |
| 544 |
} |
| 545 |
|
| 546 |
func decodeUint(in []byte, size int) (uint64, []byte) { |
| 547 |
val := uint64(0) |
| 548 |
|
| 549 |
for i := 0; i < size; i++ { |
| 550 |
val <<= 8 |
| 551 |
val += uint64(in[i]) |
| 552 |
} |
| 553 |
return val, in[size:] |
| 554 |
} |
| 555 |
|
| 556 |
type marshalledPDU interface { |
| 557 |
Marshal() ([]byte, error) |
| 558 |
Unmarshal(data []byte) (int, error) |
| 559 |
} |
| 560 |
|
| 561 |
func safeUnmarshal(pdu marshalledPDU, data []byte) error { |
| 562 |
read, err := pdu.Unmarshal(data) |
| 563 |
if err != nil { |
| 564 |
return err |
| 565 |
} |
| 566 |
if len(data) != read { |
| 567 |
return fmt.Errorf("Invalid encoding: Extra data not consumed") |
| 568 |
} |
| 569 |
return nil |
| 570 |
} |