message_certificate.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package handshake
  4. import (
  5. "github.com/pion/dtls/v2/internal/util"
  6. )
  7. // MessageCertificate is a DTLS Handshake Message
  8. // it can contain either a Client or Server Certificate
  9. //
  10. // https://tools.ietf.org/html/rfc5246#section-7.4.2
  11. type MessageCertificate struct {
  12. Certificate [][]byte
  13. }
  14. // Type returns the Handshake Type
  15. func (m MessageCertificate) Type() Type {
  16. return TypeCertificate
  17. }
  18. const (
  19. handshakeMessageCertificateLengthFieldSize = 3
  20. )
  21. // Marshal encodes the Handshake
  22. func (m *MessageCertificate) Marshal() ([]byte, error) {
  23. out := make([]byte, handshakeMessageCertificateLengthFieldSize)
  24. for _, r := range m.Certificate {
  25. // Certificate Length
  26. out = append(out, make([]byte, handshakeMessageCertificateLengthFieldSize)...)
  27. util.PutBigEndianUint24(out[len(out)-handshakeMessageCertificateLengthFieldSize:], uint32(len(r)))
  28. // Certificate body
  29. out = append(out, append([]byte{}, r...)...)
  30. }
  31. // Total Payload Size
  32. util.PutBigEndianUint24(out[0:], uint32(len(out[handshakeMessageCertificateLengthFieldSize:])))
  33. return out, nil
  34. }
  35. // Unmarshal populates the message from encoded data
  36. func (m *MessageCertificate) Unmarshal(data []byte) error {
  37. if len(data) < handshakeMessageCertificateLengthFieldSize {
  38. return errBufferTooSmall
  39. }
  40. if certificateBodyLen := int(util.BigEndianUint24(data)); certificateBodyLen+handshakeMessageCertificateLengthFieldSize != len(data) {
  41. return errLengthMismatch
  42. }
  43. offset := handshakeMessageCertificateLengthFieldSize
  44. for offset < len(data) {
  45. certificateLen := int(util.BigEndianUint24(data[offset:]))
  46. offset += handshakeMessageCertificateLengthFieldSize
  47. if offset+certificateLen > len(data) {
  48. return errLengthMismatch
  49. }
  50. m.Certificate = append(m.Certificate, append([]byte{}, data[offset:offset+certificateLen]...))
  51. offset += certificateLen
  52. }
  53. return nil
  54. }