Example #1
0
        private HandshakeMessage createClientHandshakeMessage(
            HandshakeType type, byte[] buffer)
        {
            switch (type)
            {
            case HandshakeType.ClientHello:
                return(new TlsClientHello(this.context, buffer));

            case HandshakeType.Certificate:
                return(new TlsClientCertificate(this.context, buffer));

            case HandshakeType.ClientKeyExchange:
                return(new TlsClientKeyExchange(this.context, buffer));

            case HandshakeType.CertificateVerify:
                return(new TlsClientCertificateVerify(this.context, buffer));

            case HandshakeType.Finished:
                return(new TlsClientFinished(this.context, buffer));

            default:
                throw new TlsException(
                          AlertDescription.UnexpectedMessage,
                          String.Format(CultureInfo.CurrentUICulture,
                                        "Unknown server handshake message received ({0})",
                                        type.ToString()));
            }
        }
        private HandshakeMessage createClientHandshakeMessage(
            HandshakeType type, byte[] buffer)
        {
            var last = context.LastHandshakeMsg;

            switch (type)
            {
            case HandshakeType.ClientHello:
                return(new TlsClientHello(context, buffer));

            case HandshakeType.Certificate:
                if (last != HandshakeType.ClientHello)
                {
                    break;
                }
                cert = new TlsClientCertificate(context, buffer);
                return(cert);

            case HandshakeType.ClientKeyExchange:
                if (last == HandshakeType.ClientHello || last == HandshakeType.Certificate)
                {
                    return(new TlsClientKeyExchange(context, buffer));
                }
                break;

            case HandshakeType.CertificateVerify:
                if (last == HandshakeType.ClientKeyExchange && cert != null)
                {
                    return(new TlsClientCertificateVerify(context, buffer));
                }
                break;

            case HandshakeType.Finished:
                // Certificates are optional, but if provided, they should send a CertificateVerify
                var hasCert = cert != null && cert.HasCertificate;
                var check   = hasCert
                        ? last == HandshakeType.CertificateVerify
                        : last == HandshakeType.ClientKeyExchange;
                // ChangeCipherSpecDone is not an handshake message (it's a content type) but still needs to be happens before finished
                if (check && context.ChangeCipherSpecDone)
                {
                    context.ChangeCipherSpecDone = false;
                    return(new TlsClientFinished(context, buffer));
                }

                break;

            default:
                throw new TlsException(AlertDescription.UnexpectedMessage, string.Format(
                                           CultureInfo.CurrentUICulture,
                                           "Unknown server handshake message received ({0})",
                                           type.ToString()));
            }

            throw new TlsException(AlertDescription.HandshakeFailiure,
                                   string.Format("Protocol error, unexpected protocol transition from {0} to {1}", last, type));
        }
Example #3
0
        private HandshakeMessage createServerHandshakeMessage(
            HandshakeType type, byte[] buffer)
        {
            ClientContext context = (ClientContext)this.context;

            switch (type)
            {
            case HandshakeType.HelloRequest:
                if (context.HandshakeState != HandshakeState.Started)
                {
                    context.HandshakeState = HandshakeState.None;
                    // re-negotiation will occur at next read/write
                    // (i.e. not during an existing encode/decode op)
                }
                else
                {
                    this.SendAlert(
                        AlertLevel.Warning,
                        AlertDescription.NoRenegotiation);
                }
                return(null);

            case HandshakeType.ServerHello:
                return(new TlsServerHello(this.context, buffer));

            case HandshakeType.Certificate:
                return(new TlsServerCertificate(this.context, buffer));

            case HandshakeType.ServerKeyExchange:
                return(new TlsServerKeyExchange(this.context, buffer));

            case HandshakeType.CertificateRequest:
                return(new TlsServerCertificateRequest(this.context, buffer));

            case HandshakeType.ServerHelloDone:
                return(new TlsServerHelloDone(this.context, buffer));

            case HandshakeType.Finished:
                return(new TlsServerFinished(this.context, buffer));

            default:
                throw new TlsException(
                          AlertDescription.UnexpectedMessage,
                          String.Format(CultureInfo.CurrentUICulture,
                                        "Unknown server handshake message received ({0})",
                                        type.ToString()));
            }
        }
Example #4
0
        private HandshakeMessage createServerHandshakeMessage(HandshakeType type, byte[] buffer)
        {
            ClientContext clientContext = (ClientContext)this.context;

            switch (type)
            {
            case HandshakeType.Certificate:
                return(new TlsServerCertificate(this.context, buffer));

            case HandshakeType.ServerKeyExchange:
                return(new TlsServerKeyExchange(this.context, buffer));

            case HandshakeType.CertificateRequest:
                return(new TlsServerCertificateRequest(this.context, buffer));

            case HandshakeType.ServerHelloDone:
                return(new TlsServerHelloDone(this.context, buffer));

            default:
                switch (type)
                {
                case HandshakeType.HelloRequest:
                    if (clientContext.HandshakeState != HandshakeState.Started)
                    {
                        clientContext.HandshakeState = HandshakeState.None;
                    }
                    else
                    {
                        base.SendAlert(AlertLevel.Warning, AlertDescription.NoRenegotiation);
                    }
                    return(null);

                case HandshakeType.ServerHello:
                    return(new TlsServerHello(this.context, buffer));
                }
                throw new TlsException(AlertDescription.UnexpectedMessage, string.Format(CultureInfo.CurrentUICulture, "Unknown server handshake message received ({0})", new object[]
                {
                    type.ToString()
                }));

            case HandshakeType.Finished:
                return(new TlsServerFinished(this.context, buffer));
            }
        }
    /// <summary>
    /// Handshake with the Audioscrobbler service
    /// </summary>
    /// <returns>True if the connection was successful, false otherwise</returns>
    private static void DoHandshake(bool forceNow_, HandshakeType ReasonForHandshake)
    {
      if (_useDebugLog)
      {
        Log.Debug("AudioscrobblerBase: Attempting {0} handshake", ReasonForHandshake.ToString());
      }

      // Handle uninitialized username/password.
      if (username.Length < 1 || password.Length < 1)
      {
        Log.Error("AudioscrobblerBase: {0}", "user or password not defined");
        workerFailed(ReasonForHandshake, DateTime.MinValue, new Exception("Account details insufficent"));
        return;
      }

      if (!forceNow_ || ReasonForHandshake != HandshakeType.Recover)
      {
        // Check whether we had a *successful* handshake recently.
        if (DateTime.Now < lastHandshake.Add(handshakeInterval))
        {
          string nexthandshake = lastHandshake.Add(handshakeInterval).ToString();
          string logmessage = "Next handshake due at " + nexthandshake;
          if (_useDebugLog)
          {
            Log.Debug("AudioscrobblerBase: {0}", logmessage);
          }
          workerSuccess(ReasonForHandshake, lastHandshake);
          return;
        }
      }

      if (ReasonForHandshake != HandshakeType.Init)
      {
        if (ReasonForHandshake == HandshakeType.PreRadio)
        {
          Log.Warn("AudioscrobblerBase: Disconnected - nevertheless trying radio handshake to listen without submits");
          AttemptRadioHandshake();
          return;
        }
        //else
        //{
        //  Log.Warn("AudioscrobblerBase: Disconnected - not attempting {0} handshake", ReasonForHandshake.ToString());
        //  workerFailed(ReasonForHandshake, DateTime.MinValue, new Exception("Disconnected!"));
        //  return;
        //}
      }

      BackgroundWorker worker = new BackgroundWorker();
      worker.DoWork += new DoWorkEventHandler(Worker_TryHandshake);
      worker.RunWorkerAsync(ReasonForHandshake);
    }
Example #6
0
        private HandshakeMessage createServerHandshakeMessage(
            HandshakeType type)
        {
            switch (type)
            {
            case HandshakeType.HelloRequest:
                this.SendRecord(HandshakeType.ClientHello);
                return(null);

            case HandshakeType.ServerHello:
                return(new TlsServerHello(this.context));

            case HandshakeType.Certificate:
                return(new TlsServerCertificate(this.context));

            case HandshakeType.ServerKeyExchange:
                return(new TlsServerKeyExchange(this.context));

            case HandshakeType.CertificateRequest:
                return(new TlsServerCertificateRequest(this.context));

            case HandshakeType.ServerHelloDone:
                return(new TlsServerHelloDone(this.context));

            case HandshakeType.Finished:
                return(new TlsServerFinished(this.context));

            default:
                throw new InvalidOperationException("Unknown server handshake message type: " + type.ToString());
            }
        }
Example #7
0
        private HandshakeMessage createClientHandshakeMessage(
            HandshakeType type,
            byte[] buffer)
        {
            HandshakeType handshakeType = type;

            switch (handshakeType)
            {
            case HandshakeType.CertificateVerify:
                return((HandshakeMessage) new TlsClientCertificateVerify(this.context, buffer));

            case HandshakeType.ClientKeyExchange:
                return((HandshakeMessage) new TlsClientKeyExchange(this.context, buffer));

            case HandshakeType.Finished:
                return((HandshakeMessage) new TlsClientFinished(this.context, buffer));

            default:
                if (handshakeType == HandshakeType.ClientHello)
                {
                    return((HandshakeMessage) new TlsClientHello(this.context, buffer));
                }
                if (handshakeType == HandshakeType.Certificate)
                {
                    return((HandshakeMessage) new TlsClientCertificate(this.context, buffer));
                }
                throw new TlsException(AlertDescription.UnexpectedMessage, string.Format((IFormatProvider)CultureInfo.CurrentUICulture, "Unknown server handshake message received ({0})", (object)type.ToString()));
            }
        }
		private HandshakeMessage createServerHandshakeMessage(
			HandshakeType type, byte[] buffer)
		{
			ClientContext context = (ClientContext)this.context;

			switch (type)
			{
				case HandshakeType.HelloRequest:
					if (context.HandshakeState != HandshakeState.Started)
					{
						context.HandshakeState = HandshakeState.None;
						// re-negotiation will occur at next read/write
						// (i.e. not during an existing encode/decode op)
					}
					else
					{
						this.SendAlert(
							AlertLevel.Warning,
							AlertDescription.NoRenegotiation);
					}
					return null;

				case HandshakeType.ServerHello:
					return new TlsServerHello(this.context, buffer);

				case HandshakeType.Certificate:
					return new TlsServerCertificate(this.context, buffer);

				case HandshakeType.ServerKeyExchange:
					return new TlsServerKeyExchange(this.context, buffer);

				case HandshakeType.CertificateRequest:
					return new TlsServerCertificateRequest(this.context, buffer);

				case HandshakeType.ServerHelloDone:
					return new TlsServerHelloDone(this.context, buffer);

				case HandshakeType.Finished:
					return new TlsServerFinished(this.context, buffer);

				default:
					throw new TlsException(
						AlertDescription.UnexpectedMessage,
						String.Format(CultureInfo.CurrentUICulture,
							"Unknown server handshake message received ({0})", 
							type.ToString()));
			}
		}
		private HandshakeMessage createClientHandshakeMessage(HandshakeType type)
		{
			switch (type)
			{
				case HandshakeType.ClientHello:
					return new TlsClientHello(this.context);

				case HandshakeType.Certificate:
					return new TlsClientCertificate(this.context);

				case HandshakeType.ClientKeyExchange:
					return new TlsClientKeyExchange(this.context);

				case HandshakeType.CertificateVerify:
					return new TlsClientCertificateVerify(this.context);

				case HandshakeType.Finished:
					return new TlsClientFinished(this.context);

				default:
					throw new InvalidOperationException("Unknown client handshake message type: " + type.ToString() );
			}
		}
		private HandshakeMessage createClientHandshakeMessage(
			HandshakeType type, byte[] buffer)
		{
			var last = context.LastHandshakeMsg;
			switch (type)
			{
				case HandshakeType.ClientHello:
					return new TlsClientHello(this.context, buffer);

				case HandshakeType.Certificate:
					if (last != HandshakeType.ClientHello)
						break;
					cert = new TlsClientCertificate(this.context, buffer);
					return cert;

				case HandshakeType.ClientKeyExchange:
					if (last == HandshakeType.ClientHello || last == HandshakeType.Certificate)
						return new TlsClientKeyExchange(this.context, buffer);
					break;

				case HandshakeType.CertificateVerify:
					if (last == HandshakeType.ClientKeyExchange && cert != null)
						return new TlsClientCertificateVerify(this.context, buffer);
					break;

				case HandshakeType.Finished:
					// Certificates are optional, but if provided, they should send a CertificateVerify
				bool check = (cert == null) ? (last == HandshakeType.ClientKeyExchange || last == HandshakeType.ClientHello) : (last == HandshakeType.CertificateVerify);
					// ChangeCipherSpecDone is not an handshake message (it's a content type) but still needs to be happens before finished
					if (check && context.ChangeCipherSpecDone) {
						context.ChangeCipherSpecDone = false;
						return new TlsClientFinished(this.context, buffer);
					}
					break;
					
				default:
					throw new TlsException(AlertDescription.UnexpectedMessage, String.Format(CultureInfo.CurrentUICulture,
														 "Unknown server handshake message received ({0})", 
														 type.ToString()));
					break;
			}
			throw new TlsException (AlertDescription.HandshakeFailiure, String.Format ("Protocol error, unexpected protocol transition from {0} to {1}", last, type));
		}
		private HandshakeMessage createServerHandshakeMessage(
			HandshakeType type)
		{
			switch (type)
			{
				case HandshakeType.HelloRequest:
					this.SendRecord(HandshakeType.ClientHello);
					return null;

				case HandshakeType.ServerHello:
					return new TlsServerHello(this.context);

				case HandshakeType.Certificate:
					return new TlsServerCertificate(this.context);

				case HandshakeType.ServerKeyExchange:
					return new TlsServerKeyExchange(this.context);

				case HandshakeType.CertificateRequest:
					return new TlsServerCertificateRequest(this.context);

				case HandshakeType.ServerHelloDone:
					return new TlsServerHelloDone(this.context);

				case HandshakeType.Finished:
					return new TlsServerFinished(this.context);

				default:
					throw new InvalidOperationException("Unknown server handshake message type: " + type.ToString() );					
			}
		}
Example #12
0
        private HandshakeMessage createServerHandshakeMessage(
            HandshakeType type, byte[] buffer)
        {
            ClientContext context = (ClientContext)this.context;
            var           last    = context.LastHandshakeMsg;

            switch (type)
            {
            case HandshakeType.HelloRequest:
                if (context.HandshakeState != HandshakeState.Started)
                {
                    context.HandshakeState = HandshakeState.None;
                    // re-negotiation will occur at next read/write
                    // (i.e. not during an existing encode/decode op)
                }
                else
                {
                    this.SendAlert(
                        AlertLevel.Warning,
                        AlertDescription.NoRenegotiation);
                }
                return(null);

            case HandshakeType.ServerHello:
                if (last != HandshakeType.HelloRequest)
                {
                    break;
                }
                return(new TlsServerHello(this.context, buffer));

            // Optional
            case HandshakeType.Certificate:
                if (last != HandshakeType.ServerHello)
                {
                    break;
                }
                return(new TlsServerCertificate(this.context, buffer));

            // Optional
            case HandshakeType.CertificateRequest:
                if (last == HandshakeType.ServerKeyExchange || last == HandshakeType.Certificate)
                {
                    return(new TlsServerCertificateRequest(this.context, buffer));
                }
                break;

            case HandshakeType.ServerHelloDone:
                if (last == HandshakeType.CertificateRequest || last == HandshakeType.Certificate || last == HandshakeType.ServerHello)
                {
                    return(new TlsServerHelloDone(this.context, buffer));
                }
                break;

            case HandshakeType.Finished:
                // depends if a full (ServerHelloDone) or an abbreviated handshake (ServerHello) is being done
                bool check = context.AbbreviatedHandshake ? (last == HandshakeType.ServerHello) : (last == HandshakeType.ServerHelloDone);
                // ChangeCipherSpecDone is not an handshake message (it's a content type) but still needs to be happens before finished
                if (check && context.ChangeCipherSpecDone)
                {
                    context.ChangeCipherSpecDone = false;
                    return(new TlsServerFinished(this.context, buffer));
                }
                break;

            default:
                throw new TlsException(
                          AlertDescription.UnexpectedMessage,
                          String.Format(CultureInfo.CurrentUICulture,
                                        "Unknown server handshake message received ({0})",
                                        type.ToString()));
            }
            throw new TlsException(AlertDescription.HandshakeFailiure, String.Format("Protocol error, unexpected protocol transition from {0} to {1}", last, type));
        }
Example #13
0
        private HandshakeMessage createClientHandshakeMessage(HandshakeType type)
        {
            switch (type)
            {
            case HandshakeType.ClientHello:
                return(new TlsClientHello(this.context));

            case HandshakeType.Certificate:
                return(new TlsClientCertificate(this.context));

            case HandshakeType.ClientKeyExchange:
                return(new TlsClientKeyExchange(this.context));

            case HandshakeType.CertificateVerify:
                return(new TlsClientCertificateVerify(this.context));

            case HandshakeType.Finished:
                return(new TlsClientFinished(this.context));

            default:
                throw new InvalidOperationException("Unknown client handshake message type: " + type.ToString());
            }
        }
		private HandshakeMessage createClientHandshakeMessage(
			HandshakeType type, byte[] buffer)
		{
			switch (type)
			{
				case HandshakeType.ClientHello:
					return new TlsClientHello(this.context, buffer);

				case HandshakeType.Certificate:
					return new TlsClientCertificate(this.context, buffer);

				case HandshakeType.ClientKeyExchange:
					return new TlsClientKeyExchange(this.context, buffer);

				case HandshakeType.CertificateVerify:
					return new TlsClientCertificateVerify(this.context, buffer);

				case HandshakeType.Finished:
					return new TlsClientFinished(this.context, buffer);

				default:
					throw new TlsException(
						AlertDescription.UnexpectedMessage,
						String.Format(CultureInfo.CurrentUICulture,
							"Unknown server handshake message received ({0})", 
							type.ToString()));
			}
		}
Example #15
0
		private HandshakeMessage createServerHandshakeMessage(
			HandshakeType type, byte[] buffer)
		{
			ClientContext context = (ClientContext)this.context;
			var last = context.LastHandshakeMsg;

			switch (type)
			{
				case HandshakeType.HelloRequest:
					if (context.HandshakeState != HandshakeState.Started)
					{
						context.HandshakeState = HandshakeState.None;
						// re-negotiation will occur at next read/write
						// (i.e. not during an existing encode/decode op)
					}
					else
					{
						this.SendAlert(
							AlertLevel.Warning,
							AlertDescription.NoRenegotiation);
					}
					return null;

				case HandshakeType.ServerHello:
					if (last != HandshakeType.HelloRequest)
						break;
					return new TlsServerHello(this.context, buffer);

					// Optional
				case HandshakeType.Certificate:
					if (last != HandshakeType.ServerHello)
						break;
					return new TlsServerCertificate(this.context, buffer);

					// Optional
				case HandshakeType.CertificateRequest:
					if (last == HandshakeType.ServerKeyExchange || last == HandshakeType.Certificate)
						return new TlsServerCertificateRequest(this.context, buffer);
					break;

				case HandshakeType.ServerHelloDone:
					if (last == HandshakeType.CertificateRequest || last == HandshakeType.Certificate || last == HandshakeType.ServerHello)
						return new TlsServerHelloDone(this.context, buffer);
					break;

				case HandshakeType.Finished:
					// depends if a full (ServerHelloDone) or an abbreviated handshake (ServerHello) is being done
					bool check = context.AbbreviatedHandshake ? (last == HandshakeType.ServerHello) : (last == HandshakeType.ServerHelloDone);
					// ChangeCipherSpecDone is not an handshake message (it's a content type) but still needs to be happens before finished
					if (check && context.ChangeCipherSpecDone) {
						context.ChangeCipherSpecDone = false;
						return new TlsServerFinished (this.context, buffer);
					}
					break;
					
				default:
					throw new TlsException(
						AlertDescription.UnexpectedMessage,
						String.Format(CultureInfo.CurrentUICulture,
							"Unknown server handshake message received ({0})", 
							type.ToString()));
			}
			throw new TlsException (AlertDescription.HandshakeFailiure, String.Format ("Protocol error, unexpected protocol transition from {0} to {1}", last, type));
		}