github.com/pion/dtls/v2@v2.2.12/pkg/protocol/handshake/message_server_key_exchange_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package handshake
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/pion/dtls/v2/internal/ciphersuite/types"
    11  	"github.com/pion/dtls/v2/pkg/crypto/elliptic"
    12  	"github.com/pion/dtls/v2/pkg/crypto/hash"
    13  	"github.com/pion/dtls/v2/pkg/crypto/signature"
    14  )
    15  
    16  func TestHandshakeMessageServerKeyExchange(t *testing.T) {
    17  	test := func(rawServerKeyExchange []byte, parsedServerKeyExchange *MessageServerKeyExchange) {
    18  		c := &MessageServerKeyExchange{
    19  			KeyExchangeAlgorithm: types.KeyExchangeAlgorithmEcdhe,
    20  		}
    21  		if err := c.Unmarshal(rawServerKeyExchange); err != nil {
    22  			t.Error(err)
    23  		} else if !reflect.DeepEqual(c, parsedServerKeyExchange) {
    24  			t.Errorf("handshakeMessageServerKeyExchange unmarshal: got %#v, want %#v", c, parsedServerKeyExchange)
    25  		}
    26  
    27  		raw, err := c.Marshal()
    28  		if err != nil {
    29  			t.Error(err)
    30  		} else if !reflect.DeepEqual(raw, rawServerKeyExchange) {
    31  			t.Errorf("handshakeMessageServerKeyExchange marshal: got %#v, want %#v", raw, rawServerKeyExchange)
    32  		}
    33  	}
    34  
    35  	t.Run("Hash+Signature", func(t *testing.T) {
    36  		rawServerKeyExchange := []byte{
    37  			0x03, 0x00, 0x1d, 0x41, 0x04, 0x0c, 0xb9, 0xa3, 0xb9, 0x90, 0x71, 0x35, 0x4a, 0x08, 0x66, 0xaf,
    38  			0xd6, 0x88, 0x58, 0x29, 0x69, 0x98, 0xf1, 0x87, 0x0f, 0xb5, 0xa8, 0xcd, 0x92, 0xf6, 0x2b, 0x08,
    39  			0x0c, 0xd4, 0x16, 0x5b, 0xcc, 0x81, 0xf2, 0x58, 0x91, 0x8e, 0x62, 0xdf, 0xc1, 0xec, 0x72, 0xe8,
    40  			0x47, 0x24, 0x42, 0x96, 0xb8, 0x7b, 0xee, 0xe7, 0x0d, 0xdc, 0x44, 0xec, 0xf3, 0x97, 0x6b, 0x1b,
    41  			0x45, 0x28, 0xac, 0x3f, 0x35, 0x02, 0x03, 0x00, 0x47, 0x30, 0x45, 0x02, 0x21, 0x00, 0xb2, 0x0b,
    42  			0x22, 0x95, 0x3d, 0x56, 0x57, 0x6a, 0x3f, 0x85, 0x30, 0x6f, 0x55, 0xc3, 0xf4, 0x24, 0x1b, 0x21,
    43  			0x07, 0xe5, 0xdf, 0xba, 0x24, 0x02, 0x68, 0x95, 0x1f, 0x6e, 0x13, 0xbd, 0x9f, 0xaa, 0x02, 0x20,
    44  			0x49, 0x9c, 0x9d, 0xdf, 0x84, 0x60, 0x33, 0x27, 0x96, 0x9e, 0x58, 0x6d, 0x72, 0x13, 0xe7, 0x3a,
    45  			0xe8, 0xdf, 0x43, 0x75, 0xc7, 0xb9, 0x37, 0x6e, 0x90, 0xe5, 0x3b, 0x81, 0xd4, 0xda, 0x68, 0xcd,
    46  		}
    47  		parsedServerKeyExchange := &MessageServerKeyExchange{
    48  			EllipticCurveType:    elliptic.CurveTypeNamedCurve,
    49  			NamedCurve:           elliptic.X25519,
    50  			PublicKey:            rawServerKeyExchange[4:69],
    51  			HashAlgorithm:        hash.SHA1,
    52  			SignatureAlgorithm:   signature.ECDSA,
    53  			Signature:            rawServerKeyExchange[73:144],
    54  			KeyExchangeAlgorithm: types.KeyExchangeAlgorithmEcdhe,
    55  		}
    56  
    57  		test(rawServerKeyExchange, parsedServerKeyExchange)
    58  	})
    59  
    60  	t.Run("Anonymous", func(t *testing.T) {
    61  		rawServerKeyExchange := []byte{
    62  			0x03, 0x00, 0x1d, 0x41, 0x04, 0x0c, 0xb9, 0xa3, 0xb9, 0x90, 0x71, 0x35, 0x4a, 0x08, 0x66, 0xaf,
    63  			0xd6, 0x88, 0x58, 0x29, 0x69, 0x98, 0xf1, 0x87, 0x0f, 0xb5, 0xa8, 0xcd, 0x92, 0xf6, 0x2b, 0x08,
    64  			0x0c, 0xd4, 0x16, 0x5b, 0xcc, 0x81, 0xf2, 0x58, 0x91, 0x8e, 0x62, 0xdf, 0xc1, 0xec, 0x72, 0xe8,
    65  			0x47, 0x24, 0x42, 0x96, 0xb8, 0x7b, 0xee, 0xe7, 0x0d, 0xdc, 0x44, 0xec, 0xf3, 0x97, 0x6b, 0x1b,
    66  			0x45, 0x28, 0xac, 0x3f, 0x35,
    67  		}
    68  		parsedServerKeyExchange := &MessageServerKeyExchange{
    69  			EllipticCurveType:    elliptic.CurveTypeNamedCurve,
    70  			NamedCurve:           elliptic.X25519,
    71  			PublicKey:            rawServerKeyExchange[4:69],
    72  			HashAlgorithm:        hash.None,
    73  			SignatureAlgorithm:   signature.Anonymous,
    74  			KeyExchangeAlgorithm: types.KeyExchangeAlgorithmEcdhe,
    75  		}
    76  
    77  		test(rawServerKeyExchange, parsedServerKeyExchange)
    78  	})
    79  }