| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | // Package extension implements the extension values in the ClientHello/ServerHello |
| 5 | package extensionscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/ |
| 6 | |
| 7 | import ( |
| 8 | "github.com/pion/dtls/v3/pkg/crypto/hashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/hash`/" |
| 9 | "github.com/pion/dtls/v3/pkg/crypto/signaturescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signature`/" |
| 10 | "github.com/pion/dtls/v3/pkg/crypto/signaturehashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/" |
| 11 | "golang.org/x/crypto/cryptobytescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/" |
| 12 | ) |
| 13 | |
| 14 | // marshalGenericSignatureHashAlgorithm encodes the extension. |
| 15 | // This supports hybrid encoding: TLS 1.3 PSS schemes are encoded as full uint16, |
| 16 | // while TLS 1.2 schemes use hash (high byte) + signature (low byte) encoding. |
| 17 | func marshalGenericSignatureHashAlgorithmscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/marshalGenericSignatureHashAlgorithm().(typeValue TypeValuescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/TypeValue#, sigHashAlgs []signaturehashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/.Algorithmscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#) ([]byte, error) { |
| 18 | var builder cryptobytescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/.Builderscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder# |
| 19 | builder.AddUint16scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#AddUint16().(uint16(typeValue)) |
| 20 | builder.AddUint16LengthPrefixedscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#AddUint16LengthPrefixed().(func(extBuilder *cryptobytescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/.Builderscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#) { |
| 21 | extBuilder.AddUint16LengthPrefixedscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#AddUint16LengthPrefixed().(func(algBuilder *cryptobytescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/.Builderscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#) { |
| 22 | for _, v := range sigHashAlgs { |
| 23 | // For PSS schemes, write the full uint16 SignatureScheme value |
| 24 | // For other schemes, write hash (high byte) + signature (low byte) in TLS 1.2 style |
| 25 | if v.Signaturescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#Signature..IsPSSscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signature`/Algorithm#IsPSS().() { |
| 26 | // TLS 1.3 PSS: full uint16 is the signature scheme |
| 27 | algBuilder.AddUint16scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#AddUint16().(uint16(v.Signaturescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#Signature.)) |
| 28 | } else { |
| 29 | // TLS 1.2 style: hash byte + signature byte |
| 30 | algBuilder.AddUint8scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#AddUint8().(byte(v.Hashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#Hash.)) |
| 31 | algBuilder.AddUint8scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#AddUint8().(byte(v.Signaturescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#Signature.)) |
| 32 | } |
| 33 | } |
| 34 | }) |
| 35 | }) |
| 36 | |
| 37 | return builder.Bytesscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/Builder#Bytes().() |
| 38 | } |
| 39 | |
| 40 | // unmarshalGenericSignatureAlgorithm populates the extension from encoded data. |
| 41 | // This supports hybrid encoding: detects TLS 1.3 PSS schemes |
| 42 | // and handles them as full uint16, while TLS 1.2 schemes use byte-split encoding. |
| 43 | func unmarshalGenericSignatureHashAlgorithmscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/unmarshalGenericSignatureHashAlgorithm().(typeValue TypeValuescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/TypeValue#, data []byte, dst *[]signaturehashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/.Algorithmscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#) error { |
| 44 | val := cryptobytescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/.Stringscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String#(data) |
| 45 | var extension uint16 |
| 46 | if !val.ReadUint16scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String#ReadUint16().(&extension) || TypeValuescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/TypeValue#(extension) != typeValue { |
| 47 | return errInvalidExtensionTypescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/errInvalidExtensionType. |
| 48 | } |
| 49 | |
| 50 | var extData cryptobytescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/.Stringscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String# |
| 51 | if !val.ReadUint16LengthPrefixedscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String#ReadUint16LengthPrefixed().(&extData) { |
| 52 | return errBufferTooSmallscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/errBufferTooSmall. |
| 53 | } |
| 54 | |
| 55 | var algData cryptobytescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/.Stringscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String# |
| 56 | if !extData.ReadUint16LengthPrefixedscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String#ReadUint16LengthPrefixed().(&algData) { |
| 57 | return errLengthMismatchscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/errLengthMismatch. |
| 58 | } |
| 59 | |
| 60 | for !algData.Emptyscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String#Empty().() { |
| 61 | var scheme uint16 |
| 62 | if !algData.ReadUint16scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/cryptobyte`/String#ReadUint16().(&scheme) { |
| 63 | return errLengthMismatchscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/errLengthMismatch. |
| 64 | } |
| 65 | |
| 66 | // Parse the signature scheme (handles both TLS 1.2 and TLS 1.3 PSS encoding) |
| 67 | supportedHashAlgorithm, supportedSignatureAlgorithm := parseSignatureSchemescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/protocol/extension`/parseSignatureScheme().(scheme) |
| 68 | |
| 69 | // Validate both hash and signature algorithms |
| 70 | if _, ok := hashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/hash`/.Algorithmsscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/hash`/Algorithms().()[supportedHashAlgorithm]; ok { |
| 71 | if _, ok := signaturescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signature`/.Algorithmsscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signature`/Algorithms().()[supportedSignatureAlgorithm]; ok { |
| 72 | *dst = append(*dst, signaturehashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/.Algorithmscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#{ |
| 73 | Hashscip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#Hash.: supportedHashAlgorithm, |
| 74 | Signaturescip-go gomod github.com/pion/dtls/v3 . `github.com/pion/dtls/v3/pkg/crypto/signaturehash`/Algorithm#Signature.: supportedSignatureAlgorithm, |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return nil |
| 81 | } |