{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/comments/50421025","id":50421025,"diff_hunk":"@@ -0,0 +1,822 @@\n+// Copyright (c) Microsoft. All rights reserved.\n+// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n+\n+using System.Diagnostics;\n+using System.IO;\n+using System.Security;\n+using System.Security.Principal;\n+using System.Threading;\n+using System.ComponentModel;\n+using System.Security.Authentication;\n+using System.Security.Authentication.ExtendedProtection;\n+\n+namespace System.Net.Security\n+{\n+    //\n+    // The class maintains the state of the authentication process and the security context.\n+    // It encapsulates security context and does the real work in authentication and\n+    // user data encryption\n+    //\n+    internal partial class NegoState\n+    {\n+        private const int ERROR_TRUST_FAILURE = 1790;   // Used to serialize protectionLevel or impersonationLevel mismatch error to the remote side.\n+\n+        private static readonly byte[] s_emptyMessage = new byte[0];\n+        private static readonly AsyncCallback s_readCallback = new AsyncCallback(ReadCallback);\n+        private static readonly AsyncCallback s_writeCallback = new AsyncCallback(WriteCallback);\n+\n+        private Stream _innerStream;\n+        private bool _leaveStreamOpen;\n+\n+        private Exception _exception;\n+\n+        private StreamFramer _framer;\n+        private NTAuthentication _context;\n+\n+        private int _nestedAuth;\n+\n+        internal const int MaxReadFrameSize = 64 * 1024;\n+        internal const int MaxWriteDataSize = 63 * 1024; // 1k for the framing and trailer that is always less as per SSPI.\n+\n+        private bool _canRetryAuthentication;\n+        private ProtectionLevel _expectedProtectionLevel;\n+        private TokenImpersonationLevel _expectedImpersonationLevel;\n+        private uint _writeSequenceNumber;\n+        private uint _readSequenceNumber;\n+\n+        private ExtendedProtectionPolicy _extendedProtectionPolicy;\n+\n+        // SSPI does not send a server ack on successful auth.\n+        // This is a state variable used to gracefully handle auth confirmation.\n+        private bool _remoteOk = false;\n+\n+        internal NegoState(Stream innerStream, bool leaveStreamOpen)\n+        {\n+            if (innerStream == null)\n+            {\n+                throw new ArgumentNullException(\"stream\");\n+            }\n+\n+            _innerStream = innerStream;\n+            _leaveStreamOpen = leaveStreamOpen;\n+        }\n+\n+        internal static string DefaultPackage\n+        {\n+            get\n+            {\n+                return NegotiationInfoClass.Negotiate;\n+            }\n+        }\n+\n+        internal void ValidateCreateContext(\n+            string package,\n+            NetworkCredential credential,\n+            string servicePrincipalName,\n+            ExtendedProtectionPolicy policy,\n+            ProtectionLevel protectionLevel,\n+            TokenImpersonationLevel impersonationLevel)\n+        {\n+            if (policy != null)\n+            {\n+                // One of these must be set if EP is turned on\n+                if (policy.CustomChannelBinding == null && policy.CustomServiceNames == null)\n+                {\n+                    throw new ArgumentException(SR.net_auth_must_specify_extended_protection_scheme, \"policy\");\n+                }\n+\n+                _extendedProtectionPolicy = policy;\n+            }\n+            else\n+            {\n+                _extendedProtectionPolicy = new ExtendedProtectionPolicy(PolicyEnforcement.Never);\n+            }\n+\n+            ValidateCreateContext(package, true, credential, servicePrincipalName, _extendedProtectionPolicy.CustomChannelBinding, protectionLevel, impersonationLevel);\n+        }\n+\n+        internal void ValidateCreateContext(\n+            string package,\n+            bool isServer,\n+            NetworkCredential credential,\n+            string servicePrincipalName,\n+            ChannelBinding channelBinding,\n+            ProtectionLevel protectionLevel,\n+            TokenImpersonationLevel impersonationLevel)\n+        {\n+            if (_exception != null && !_canRetryAuthentication)\n+            {\n+                throw _exception;\n+            }\n+\n+            if (_context != null && _context.IsValidContext)\n+            {\n+                throw new InvalidOperationException(SR.net_auth_reauth);\n+            }\n+\n+            if (credential == null)\n+            {\n+                throw new ArgumentNullException(\"credential\");\n+            }\n+\n+            if (servicePrincipalName == null)\n+            {\n+                throw new ArgumentNullException(\"servicePrincipalName\");\n+            }\n+\n+            ValidateImpersonationLevel(impersonationLevel);\n+\n+            if (_context != null && IsServer != isServer)\n+            {\n+                throw new InvalidOperationException(SR.net_auth_client_server);\n+            }\n+\n+            _exception = null;\n+            _remoteOk = false;\n+            _framer = new StreamFramer(_innerStream);\n+            _framer.WriteHeader.MessageId = FrameHeader.HandshakeId;\n+\n+            _expectedProtectionLevel = protectionLevel;\n+            _expectedImpersonationLevel = isServer ? impersonationLevel : TokenImpersonationLevel.None;\n+            _writeSequenceNumber = 0;\n+            _readSequenceNumber = 0;\n+\n+            ContextFlagsPal flags = ContextFlagsPal.Connection;\n+\n+            // A workaround for the client when talking to Win9x on the server side.\n+            if (protectionLevel == ProtectionLevel.None && !isServer)\n+            {\n+                package = NegotiationInfoClass.NTLM;\n+            }\n+\n+            else if (protectionLevel == ProtectionLevel.EncryptAndSign)\n+            {\n+                flags |= ContextFlagsPal.Confidentiality;\n+            }\n+            else if (protectionLevel == ProtectionLevel.Sign)\n+            {\n+                // Assuming user expects NT4 SP4 and above.\n+                flags |= (ContextFlagsPal.ReplayDetect | ContextFlagsPal.SequenceDetect | ContextFlagsPal.InitIntegrity);\n+            }\n+\n+            if (isServer)\n+            {\n+                if (_extendedProtectionPolicy.PolicyEnforcement == PolicyEnforcement.WhenSupported)\n+                {\n+                    flags |= ContextFlagsPal.AllowMissingBindings;\n+                }\n+\n+                if (_extendedProtectionPolicy.PolicyEnforcement != PolicyEnforcement.Never &&\n+                    _extendedProtectionPolicy.ProtectionScenario == ProtectionScenario.TrustedProxy)\n+                {\n+                    flags |= ContextFlagsPal.ProxyBindings;\n+                }\n+            }\n+            else\n+            {\n+                // Server side should not request any of these flags.","path":"src/System.Net.Security/src/System/Net/SecureProtocols/InternalNegoState.cs","position":177,"original_position":177,"commit_id":"6d62ab9dc967fae379569a1c64eebe90a2342bdf","original_commit_id":"6d62ab9dc967fae379569a1c64eebe90a2342bdf","user":{"login":"bartonjs","id":10642668,"avatar_url":"https://avatars.githubusercontent.com/u/10642668?v=3","gravatar_id":"","url":"https://api.github.com/users/bartonjs","html_url":"https://github.com/bartonjs","followers_url":"https://api.github.com/users/bartonjs/followers","following_url":"https://api.github.com/users/bartonjs/following{/other_user}","gists_url":"https://api.github.com/users/bartonjs/gists{/gist_id}","starred_url":"https://api.github.com/users/bartonjs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartonjs/subscriptions","organizations_url":"https://api.github.com/users/bartonjs/orgs","repos_url":"https://api.github.com/users/bartonjs/repos","events_url":"https://api.github.com/users/bartonjs/events{/privacy}","received_events_url":"https://api.github.com/users/bartonjs/received_events","type":"User","site_admin":false},"body":"(I think that's me saying \"maybe we should add some asserts in the if\" :smile:)","created_at":"2016-01-21T16:08:57Z","updated_at":"2016-01-21T16:08:57Z","html_url":"https://github.com/dotnet/corefx/pull/5581#discussion_r50421025","pull_request_url":"https://api.github.com/repos/dotnet/corefx/pulls/5581","_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments/50421025"},"html":{"href":"https://github.com/dotnet/corefx/pull/5581#discussion_r50421025"},"pull_request":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/5581"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/corefx/pulls/5581","id":56719185,"html_url":"https://github.com/dotnet/corefx/pull/5581","diff_url":"https://github.com/dotnet/corefx/pull/5581.diff","patch_url":"https://github.com/dotnet/corefx/pull/5581.patch","issue_url":"https://api.github.com/repos/dotnet/corefx/issues/5581","number":5581,"state":"open","locked":false,"title":"Refactoring the Windows specific code in NegotiateStream","user":{"login":"shrutigarg","id":13743289,"avatar_url":"https://avatars.githubusercontent.com/u/13743289?v=3","gravatar_id":"","url":"https://api.github.com/users/shrutigarg","html_url":"https://github.com/shrutigarg","followers_url":"https://api.github.com/users/shrutigarg/followers","following_url":"https://api.github.com/users/shrutigarg/following{/other_user}","gists_url":"https://api.github.com/users/shrutigarg/gists{/gist_id}","starred_url":"https://api.github.com/users/shrutigarg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shrutigarg/subscriptions","organizations_url":"https://api.github.com/users/shrutigarg/orgs","repos_url":"https://api.github.com/users/shrutigarg/repos","events_url":"https://api.github.com/users/shrutigarg/events{/privacy}","received_events_url":"https://api.github.com/users/shrutigarg/received_events","type":"User","site_admin":false},"body":"summary \r\n\r\nRenaming NegoState.Windows.cs to InternalNegoState.cs as it had much common code applicable for Unix. (commit1)\r\nSeparating out the common code and windows specific code in InternalNegoState and NegoState.Windows.cs respectiviely.\r\nAdding NegotiateInfoClass.cs to be used by Unix impl.\r\n\r\nTested locally both AuthenticateAsServer/Client on Windows.\r\n\r\n/cc: @SidharthNabar @stephentoub  @davidsh @CIPop @vijaykota \r\n","created_at":"2016-01-21T08:43:45Z","updated_at":"2016-01-21T16:08:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"443ef11418b14a8bcfe8a193c28baf78e655076e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/corefx/pulls/5581/commits","review_comments_url":"https://api.github.com/repos/dotnet/corefx/pulls/5581/comments","review_comment_url":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/corefx/issues/5581/comments","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/6d62ab9dc967fae379569a1c64eebe90a2342bdf","head":{"label":"shrutigarg:Nego","ref":"Nego","sha":"6d62ab9dc967fae379569a1c64eebe90a2342bdf","user":{"login":"shrutigarg","id":13743289,"avatar_url":"https://avatars.githubusercontent.com/u/13743289?v=3","gravatar_id":"","url":"https://api.github.com/users/shrutigarg","html_url":"https://github.com/shrutigarg","followers_url":"https://api.github.com/users/shrutigarg/followers","following_url":"https://api.github.com/users/shrutigarg/following{/other_user}","gists_url":"https://api.github.com/users/shrutigarg/gists{/gist_id}","starred_url":"https://api.github.com/users/shrutigarg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shrutigarg/subscriptions","organizations_url":"https://api.github.com/users/shrutigarg/orgs","repos_url":"https://api.github.com/users/shrutigarg/repos","events_url":"https://api.github.com/users/shrutigarg/events{/privacy}","received_events_url":"https://api.github.com/users/shrutigarg/received_events","type":"User","site_admin":false},"repo":{"id":40525181,"name":"corefx","full_name":"shrutigarg/corefx","owner":{"login":"shrutigarg","id":13743289,"avatar_url":"https://avatars.githubusercontent.com/u/13743289?v=3","gravatar_id":"","url":"https://api.github.com/users/shrutigarg","html_url":"https://github.com/shrutigarg","followers_url":"https://api.github.com/users/shrutigarg/followers","following_url":"https://api.github.com/users/shrutigarg/following{/other_user}","gists_url":"https://api.github.com/users/shrutigarg/gists{/gist_id}","starred_url":"https://api.github.com/users/shrutigarg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shrutigarg/subscriptions","organizations_url":"https://api.github.com/users/shrutigarg/orgs","repos_url":"https://api.github.com/users/shrutigarg/repos","events_url":"https://api.github.com/users/shrutigarg/events{/privacy}","received_events_url":"https://api.github.com/users/shrutigarg/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shrutigarg/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":true,"url":"https://api.github.com/repos/shrutigarg/corefx","forks_url":"https://api.github.com/repos/shrutigarg/corefx/forks","keys_url":"https://api.github.com/repos/shrutigarg/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shrutigarg/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shrutigarg/corefx/teams","hooks_url":"https://api.github.com/repos/shrutigarg/corefx/hooks","issue_events_url":"https://api.github.com/repos/shrutigarg/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/shrutigarg/corefx/events","assignees_url":"https://api.github.com/repos/shrutigarg/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/shrutigarg/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/shrutigarg/corefx/tags","blobs_url":"https://api.github.com/repos/shrutigarg/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shrutigarg/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shrutigarg/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/shrutigarg/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shrutigarg/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/shrutigarg/corefx/languages","stargazers_url":"https://api.github.com/repos/shrutigarg/corefx/stargazers","contributors_url":"https://api.github.com/repos/shrutigarg/corefx/contributors","subscribers_url":"https://api.github.com/repos/shrutigarg/corefx/subscribers","subscription_url":"https://api.github.com/repos/shrutigarg/corefx/subscription","commits_url":"https://api.github.com/repos/shrutigarg/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/shrutigarg/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/shrutigarg/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/shrutigarg/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/shrutigarg/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/shrutigarg/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shrutigarg/corefx/merges","archive_url":"https://api.github.com/repos/shrutigarg/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shrutigarg/corefx/downloads","issues_url":"https://api.github.com/repos/shrutigarg/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/shrutigarg/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/shrutigarg/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/shrutigarg/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shrutigarg/corefx/labels{/name}","releases_url":"https://api.github.com/repos/shrutigarg/corefx/releases{/id}","created_at":"2015-08-11T06:22:30Z","updated_at":"2015-08-11T06:22:48Z","pushed_at":"2016-01-21T08:30:31Z","git_url":"git://github.com/shrutigarg/corefx.git","ssh_url":"git@github.com:shrutigarg/corefx.git","clone_url":"https://github.com/shrutigarg/corefx.git","svn_url":"https://github.com/shrutigarg/corefx","homepage":"http://dotnet.github.io/core/","size":46135,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"a58f490274e286d844d277f2658f2c074a4d5350","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":26295345,"name":"corefx","full_name":"dotnet/corefx","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/corefx","description":"This repo contains the .NET Core foundational libraries, called CoreFX. It includes classes for collections, file systems, console, XML, async and many others.","fork":false,"url":"https://api.github.com/repos/dotnet/corefx","forks_url":"https://api.github.com/repos/dotnet/corefx/forks","keys_url":"https://api.github.com/repos/dotnet/corefx/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/corefx/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/corefx/teams","hooks_url":"https://api.github.com/repos/dotnet/corefx/hooks","issue_events_url":"https://api.github.com/repos/dotnet/corefx/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/corefx/events","assignees_url":"https://api.github.com/repos/dotnet/corefx/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/corefx/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/corefx/tags","blobs_url":"https://api.github.com/repos/dotnet/corefx/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/corefx/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/corefx/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/corefx/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/corefx/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/corefx/languages","stargazers_url":"https://api.github.com/repos/dotnet/corefx/stargazers","contributors_url":"https://api.github.com/repos/dotnet/corefx/contributors","subscribers_url":"https://api.github.com/repos/dotnet/corefx/subscribers","subscription_url":"https://api.github.com/repos/dotnet/corefx/subscription","commits_url":"https://api.github.com/repos/dotnet/corefx/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/corefx/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/corefx/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/corefx/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/corefx/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/corefx/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/corefx/merges","archive_url":"https://api.github.com/repos/dotnet/corefx/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/corefx/downloads","issues_url":"https://api.github.com/repos/dotnet/corefx/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/corefx/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/corefx/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/corefx/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/corefx/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/corefx/releases{/id}","created_at":"2014-11-06T23:42:48Z","updated_at":"2016-01-21T14:21:01Z","pushed_at":"2016-01-21T16:02:30Z","git_url":"git://github.com/dotnet/corefx.git","ssh_url":"git@github.com:dotnet/corefx.git","clone_url":"https://github.com/dotnet/corefx.git","svn_url":"https://github.com/dotnet/corefx","homepage":"http://dotnet.github.io/","size":55987,"stargazers_count":8973,"watchers_count":8973,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2063,"mirror_url":null,"open_issues_count":824,"forks":2063,"open_issues":824,"watchers":8973,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/5581"},"html":{"href":"https://github.com/dotnet/corefx/pull/5581"},"issue":{"href":"https://api.github.com/repos/dotnet/corefx/issues/5581"},"comments":{"href":"https://api.github.com/repos/dotnet/corefx/issues/5581/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/5581/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/corefx/pulls/5581/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/corefx/statuses/6d62ab9dc967fae379569a1c64eebe90a2342bdf"}}}}