-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandshake_message_certificate.go
47 lines (37 loc) · 1 KB
/
handshake_message_certificate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package dtls
import (
"crypto/x509"
)
type handshakeMessageCertificate struct {
certificate *x509.Certificate
}
func (h handshakeMessageCertificate) handshakeType() handshakeType {
return handshakeTypeCertificate
}
func (h *handshakeMessageCertificate) Marshal() ([]byte, error) {
if h.certificate == nil {
return nil, errCertificateUnset
}
out := make([]byte, 6)
putBigEndianUint24(out, uint32(len(h.certificate.Raw))+3)
putBigEndianUint24(out[3:], uint32(len(h.certificate.Raw)))
return append(out, h.certificate.Raw...), nil
}
func (h *handshakeMessageCertificate) Unmarshal(data []byte) error {
if len(data) < 6 {
return errBufferTooSmall
}
certificateBodyLen := int(bigEndianUint24(data))
certificateLen := int(bigEndianUint24(data[3:]))
if certificateBodyLen+3 != len(data) {
return errLengthMismatch
} else if certificateLen+6 != len(data) {
return errLengthMismatch
}
cert, err := x509.ParseCertificate(data[6:])
if err != nil {
return err
}
h.certificate = cert
return nil
}