package extension
import (
"encoding/binary"
"github.com/pion/dtls/v3/pkg/crypto/hash"
"github.com/pion/dtls/v3/pkg/crypto/signature"
"github.com/pion/dtls/v3/pkg/crypto/signaturehash"
)
const (
supportedSignatureAlgorithmsHeaderSize = 6
)
type SupportedSignatureAlgorithms struct {
SignatureHashAlgorithms []signaturehash .Algorithm
}
func (s SupportedSignatureAlgorithms ) TypeValue () TypeValue {
return SupportedSignatureAlgorithmsTypeValue
}
func (s *SupportedSignatureAlgorithms ) Marshal () ([]byte , error ) {
out := make ([]byte , supportedSignatureAlgorithmsHeaderSize )
binary .BigEndian .PutUint16 (out , uint16 (s .TypeValue ()))
binary .BigEndian .PutUint16 (out [2 :], uint16 (2 +(len (s .SignatureHashAlgorithms )*2 )))
binary .BigEndian .PutUint16 (out [4 :], uint16 (len (s .SignatureHashAlgorithms )*2 ))
for _ , v := range s .SignatureHashAlgorithms {
out = append (out , []byte {0x00 , 0x00 }...)
out [len (out )-2 ] = byte (v .Hash )
out [len (out )-1 ] = byte (v .Signature )
}
return out , nil
}
func (s *SupportedSignatureAlgorithms ) Unmarshal (data []byte ) error {
if len (data ) <= supportedSignatureAlgorithmsHeaderSize {
return errBufferTooSmall
} else if TypeValue (binary .BigEndian .Uint16 (data )) != s .TypeValue () {
return errInvalidExtensionType
}
algorithmCount := int (binary .BigEndian .Uint16 (data [4 :]) / 2 )
if supportedSignatureAlgorithmsHeaderSize +(algorithmCount *2 ) > len (data ) {
return errLengthMismatch
}
for i := 0 ; i < algorithmCount ; i ++ {
supportedHashAlgorithm := hash .Algorithm (data [supportedSignatureAlgorithmsHeaderSize +(i *2 )])
supportedSignatureAlgorithm := signature .Algorithm (data [supportedSignatureAlgorithmsHeaderSize +(i *2 )+1 ])
if _ , ok := hash .Algorithms ()[supportedHashAlgorithm ]; ok {
if _ , ok := signature .Algorithms ()[supportedSignatureAlgorithm ]; ok {
s .SignatureHashAlgorithms = append (s .SignatureHashAlgorithms , signaturehash .Algorithm {
Hash : supportedHashAlgorithm ,
Signature : supportedSignatureAlgorithm ,
})
}
}
}
return nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .