public TlsContext(TlsConfiguration configuration, bool isServer) { this.configuration = configuration; this.isServer = isServer; #if INSTRUMENTATION SetupInstrumentation(); #endif session = new Session(configuration); Session.RandomNumberGenerator = RandomNumberGenerator.Create(); if (IsServer) { negotiationHandler = CreateNegotiationHandler(NegotiationState.InitialServerConnection); } else { negotiationHandler = CreateNegotiationHandler(NegotiationState.InitialClientConnection); } if (Configuration.UserSettings != null && Configuration.UserSettings.EnableDebugging) { EnableDebugging = true; } }
public TlsContext(TlsConfiguration configuration, bool isServer, IMonoTlsEventSink eventSink) { this.configuration = configuration; this.isServer = isServer; this.eventSink = eventSink; #if INSTRUMENTATION var instrumentation = configuration.UserSettings.Instrumentation; if (instrumentation != null) { if (instrumentation.HasSignatureInstrument) { signatureProvider = instrumentation.SignatureInstrument; } if (instrumentation.HasSettingsInstrument) { settingsProvider = instrumentation.SettingsInstrument; } handshakeInstruments = instrumentation.HandshakeInstruments; instrumentationEventSink = instrumentation.EventSink; } #endif if (signatureProvider == null) { signatureProvider = new SignatureProvider(); } if (settingsProvider == null) { settingsProvider = new SettingsProvider(configuration.UserSettings); } session = new Session(configuration); Session.RandomNumberGenerator = RandomNumberGenerator.Create(); if (IsServer) { negotiationHandler = CreateNegotiationHandler(NegotiationState.InitialServerConnection); } else { negotiationHandler = CreateNegotiationHandler(NegotiationState.InitialClientConnection); } if (settingsProvider.EnableDebugging) { EnableDebugging = true; } settingsProvider.Initialize(this); }
public void Clear() { negotiationHandler = null; if (handshakeParameters != null) { handshakeParameters.Dispose(); handshakeParameters = null; } if (session != null) { session.Dispose(); session = null; } }
public TlsContext (TlsConfiguration configuration, bool isServer, IMonoTlsEventSink eventSink) { this.configuration = configuration; this.isServer = isServer; this.eventSink = eventSink; #if INSTRUMENTATION if (configuration.HasInstrumentation) { if (configuration.Instrumentation.HasSignatureInstrument) signatureProvider = configuration.Instrumentation.SignatureInstrument; if (configuration.Instrumentation.HasSettingsInstrument) settingsProvider = configuration.Instrumentation.SettingsInstrument; handshakeInstruments = configuration.Instrumentation.HandshakeInstruments; } #endif if (signatureProvider == null) signatureProvider = new SignatureProvider (); if (settingsProvider == null) settingsProvider = new SettingsProvider (configuration.TlsSettings.UserSettings); session = new Session (configuration); Session.RandomNumberGenerator = RandomNumberGenerator.Create (); if (IsServer) negotiationHandler = CreateNegotiationHandler (NegotiationState.InitialServerConnection); else negotiationHandler = CreateNegotiationHandler (NegotiationState.InitialClientConnection); if (Configuration.TlsSettings != null && Configuration.TlsSettings.EnableDebugging) EnableDebugging = true; else if (settingsProvider.EnableDebugging ?? false) EnableDebugging = true; settingsProvider.Initialize (this); }
SecurityStatus _GenerateNextToken (TlsBuffer incoming, TlsMultiBuffer outgoing) { #if DEBUG_FULL if (EnableDebugging) { DebugHelper.WriteLine ("GenerateNextToken: {0}", negotiationHandler); if (incoming != null) DebugHelper.WriteRemaining (" incoming", incoming); } #endif if (incoming == null) { negotiationHandler = negotiationHandler.GenerateReply (outgoing); return SecurityStatus.ContinueNeeded; } var contentType = (ContentType)incoming.ReadByte (); #if DEBUG_FULL if (EnableDebugging) DebugHelper.WriteLine (" received message type {0}", contentType); #endif if (skipToOffset >= 0 && contentType != ContentType.Handshake) throw new TlsException (AlertDescription.InternalError); if (contentType == ContentType.Alert) return ProcessAlert (incoming); bool decrypted = false; if (cachedFragment != null) { if (contentType != ContentType.Handshake) throw new TlsException (AlertDescription.DecodeError); decrypted = ReadStandardBuffer (ContentType.Handshake, ref incoming); cachedFragment.Write (incoming.Buffer, incoming.Position, incoming.Remaining); if (cachedFragment.Remaining > 0) return SecurityStatus.ContinueNeeded; incoming.Dispose (); incoming = cachedFragment; cachedFragment = null; incoming.Position = 0; } else { decrypted = ReadStandardBuffer (contentType, ref incoming); } if (Session.Read != null && Session.Read.Cipher != null && !decrypted) throw new TlsException (AlertDescription.DecryptError, "Expected encrypted message."); try { if (contentType == ContentType.ChangeCipherSpec) return negotiationHandler.ProcessMessage (new TlsChangeCipherSpec ()); else if (contentType == ContentType.ApplicationData) { if (session.Read == null || session.Read.Cipher == null || !session.SecureRenegotiation) throw new TlsException (AlertDescription.DecodeError); // FIXME throw new NotImplementedException (); } else if (contentType != ContentType.Handshake) { throw new TlsException (AlertDescription.UnexpectedMessage); } if (skipToOffset >= 0) { incoming.Position = skipToOffset; skipToOffset = -1; } SecurityStatus result; bool finished; while (true) { var startOffset = incoming.Position; finished = ProcessHandshakeMessage (incoming, out result); if (result == SecurityStatus.CredentialsNeeded) { // Caller will call us again with the same input. skipToOffset = startOffset; if (decrypted) Session.Read.ReadSequenceNumber--; return result; } if (incoming.Remaining == 0) break; if (finished || result != SecurityStatus.ContinueNeeded) throw new TlsException (AlertDescription.UnexpectedMessage); } if (finished) negotiationHandler = negotiationHandler.GenerateReply (outgoing); return result; } finally { if (decrypted) incoming.Dispose (); } }
public void Clear () { negotiationHandler = null; if (handshakeParameters != null) { handshakeParameters.Dispose (); handshakeParameters = null; } if (session != null) { session.Dispose (); session = null; } }
SecurityStatus _GenerateNextToken(TlsBuffer incoming, TlsMultiBuffer outgoing) { #if DEBUG_FULL if (EnableDebugging) { DebugHelper.WriteLine("GenerateNextToken: {0}", negotiationHandler); if (incoming != null) { DebugHelper.WriteRemaining(" incoming", incoming); } } #endif if (incoming == null) { negotiationHandler = negotiationHandler.GenerateReply(outgoing); return(SecurityStatus.ContinueNeeded); } var contentType = (ContentType)incoming.ReadByte(); #if DEBUG_FULL if (EnableDebugging) { DebugHelper.WriteLine(" received message type {0}", contentType); } #endif if (skipToOffset >= 0 && contentType != ContentType.Handshake) { throw new TlsException(AlertDescription.InternalError); } if (contentType == ContentType.Alert) { return(ProcessAlert(incoming)); } bool decrypted = false; if (cachedFragment != null) { if (contentType != ContentType.Handshake) { throw new TlsException(AlertDescription.DecodeError); } decrypted = ReadStandardBuffer(ContentType.Handshake, ref incoming); cachedFragment.Write(incoming.Buffer, incoming.Position, incoming.Position + incoming.Remaining); if (cachedFragment.Remaining > 0) { return(SecurityStatus.ContinueNeeded); } incoming.Dispose(); incoming = cachedFragment; cachedFragment = null; incoming.Position = 0; } else { decrypted = ReadStandardBuffer(contentType, ref incoming); } if (Session.Read != null && Session.Read.Cipher != null && !decrypted) { throw new TlsException(AlertDescription.DecryptError, "Expected encrypted message."); } try { if (contentType == ContentType.ChangeCipherSpec) { return(negotiationHandler.ProcessMessage(new TlsChangeCipherSpec())); } else if (contentType == ContentType.ApplicationData) { if (session.Read == null || session.Read.Cipher == null || !session.SecureRenegotiation) { throw new TlsException(AlertDescription.DecodeError); } // FIXME throw new NotImplementedException(); } else if (contentType != ContentType.Handshake) { throw new TlsException(AlertDescription.UnexpectedMessage); } if (skipToOffset >= 0) { incoming.Position = skipToOffset; skipToOffset = -1; } SecurityStatus result; bool finished; while (true) { var startOffset = incoming.Position; finished = ProcessHandshakeMessage(incoming, out result); if (result == SecurityStatus.CredentialsNeeded) { // Caller will call us again with the same input. skipToOffset = startOffset; if (decrypted) { Session.Read.ReadSequenceNumber--; } return(result); } if (incoming.Remaining == 0) { break; } if (finished || result != SecurityStatus.ContinueNeeded) { throw new TlsException(AlertDescription.UnexpectedMessage); } } if (finished) { negotiationHandler = negotiationHandler.GenerateReply(outgoing); } return(result); } finally { if (decrypted) { incoming.Dispose(); } } }