Exemplo n.º 1
0
        /// <summary>
        /// Sends the Client Key Exchange message flight.
        /// </summary>
        /// <param name="Endpoint">Endpoint.</param>
        /// <param name="State">Endpoint state.</param>
        public override void SendClientKeyExchange(DtlsEndpoint Endpoint, EndpointState State)
        {
            this.CalcMasterSecret(State);

            // Sends the Client Key Exchange message for Pre-shared key ciphers,
            // as defined in §2 of RFC 4279: https://tools.ietf.org/html/rfc4279

            if (State.credentials is PresharedKey Psk)
            {
                ushort N = (ushort)Psk.Identity.Length;
                byte[] ClientKeyExchange = new byte[2 + N];

                ClientKeyExchange[0] = (byte)(N >> 8);
                ClientKeyExchange[1] = (byte)N;

                Array.Copy(Psk.Identity, 0, ClientKeyExchange, 2, N);

                Endpoint.SendHandshake(HandshakeType.client_key_exchange, ClientKeyExchange, true, true, State);

                // RFC 5246, §7.1, Change Cipher Spec Protocol:

                Endpoint.SendRecord(ContentType.change_cipher_spec, new byte[] { 1 }, true, true, State);
                Endpoint.ChangeCipherSpec(State, true);

                this.SendFinished(Endpoint, State, true);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finishes the handshake.
        /// </summary>
        /// <param name="Endpoint">Endpoint.</param>
        /// <param name="State">Endpoint state.</param>
        /// <param name="Resendable">If flight of records is resendable.</param>
        public virtual void SendFinished(DtlsEndpoint Endpoint, EndpointState State, bool Resendable)
        {
            if (State.masterSecret == null)
            {
                Endpoint.SendAlert(AlertLevel.fatal, AlertDescription.handshake_failure, State);
            }
            else
            {
                string Label;
                byte[] HandshakeHash;
                byte[] VerifyData;

                if (State.isClient)
                {
                    State.clientFinished = true;
                    Label = "client finished";
                    State.CalcClientHandshakeHash();
                    HandshakeHash = State.clientHandshakeHash;
                }
                else
                {
                    State.serverFinished = true;
                    Label = "server finished";
                    State.CalcServerHandshakeHash();
                    HandshakeHash = State.serverHandshakeHash;
                }

                VerifyData = this.PRF(State.masterSecret, Label, HandshakeHash, 12);

                Endpoint.SendHandshake(HandshakeType.finished, VerifyData, false, Resendable, State);

                if (State.clientFinished && State.serverFinished)
                {
                    Endpoint.HandshakeSuccess(State);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Sends the Server Key Exchange message flight.
 /// </summary>
 /// <param name="Endpoint">Endpoint.</param>
 /// <param name="State">Endpoint state.</param>
 public override void SendServerKeyExchange(DtlsEndpoint Endpoint, EndpointState State)
 {
     Endpoint.SendHandshake(HandshakeType.server_hello_done, new byte[0], false, true, State);
 }