cipher_suite.go 861 B

1234567891011121314151617181920212223242526272829303132
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package handshake
  4. import "encoding/binary"
  5. func decodeCipherSuiteIDs(buf []byte) ([]uint16, error) {
  6. if len(buf) < 2 {
  7. return nil, errBufferTooSmall
  8. }
  9. cipherSuitesCount := int(binary.BigEndian.Uint16(buf[0:])) / 2
  10. rtrn := make([]uint16, cipherSuitesCount)
  11. for i := 0; i < cipherSuitesCount; i++ {
  12. if len(buf) < (i*2 + 4) {
  13. return nil, errBufferTooSmall
  14. }
  15. rtrn[i] = binary.BigEndian.Uint16(buf[(i*2)+2:])
  16. }
  17. return rtrn, nil
  18. }
  19. func encodeCipherSuiteIDs(cipherSuiteIDs []uint16) []byte {
  20. out := []byte{0x00, 0x00}
  21. binary.BigEndian.PutUint16(out[len(out)-2:], uint16(len(cipherSuiteIDs)*2))
  22. for _, id := range cipherSuiteIDs {
  23. out = append(out, []byte{0x00, 0x00}...)
  24. binary.BigEndian.PutUint16(out[len(out)-2:], id)
  25. }
  26. return out
  27. }