File: StreamSecurityUpgradeInitiatorAsyncResult.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (113 lines) | stat: -rw-r--r-- 4,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
    using System.IO;
    using System.Runtime;
    using System.Security.Authentication;
    using System.ServiceModel.Security;

    abstract class StreamSecurityUpgradeInitiatorAsyncResult : AsyncResult
    {
        Stream originalStream;
        SecurityMessageProperty remoteSecurity;
        Stream upgradedStream;

        static AsyncCallback onAuthenticateAsClient = Fx.ThunkCallback(new AsyncCallback(OnAuthenticateAsClient));

        public StreamSecurityUpgradeInitiatorAsyncResult(AsyncCallback callback, object state)
            : base(callback, state)
        {
            // empty
        }

        public void Begin(Stream stream)
        {
            this.originalStream = stream;
            IAsyncResult result;

            try
            {
                result = this.OnBeginAuthenticateAsClient(this.originalStream, onAuthenticateAsClient);
            }
            catch (AuthenticationException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
                    exception));
            }
            catch (IOException ioException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
                    SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
            }

            if (!result.CompletedSynchronously)
            {
                return;
            }

            CompleteAuthenticateAsClient(result);
            base.Complete(true);
        }

        void CompleteAuthenticateAsClient(IAsyncResult result)
        {
            try
            {
                this.upgradedStream = this.OnCompleteAuthenticateAsClient(result);
            }
            catch (AuthenticationException exception)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(exception.Message,
                    exception));
            }
            catch (IOException ioException)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityNegotiationException(
                    SR.GetString(SR.NegotiationFailedIO, ioException.Message), ioException));
            }

            this.remoteSecurity = this.ValidateCreateSecurity();
        }

        public static Stream End(IAsyncResult result, out SecurityMessageProperty remoteSecurity)
        {
            StreamSecurityUpgradeInitiatorAsyncResult thisPtr = AsyncResult.End<StreamSecurityUpgradeInitiatorAsyncResult>(result);
            remoteSecurity = thisPtr.remoteSecurity;
            return thisPtr.upgradedStream;
        }

        static void OnAuthenticateAsClient(IAsyncResult result)
        {
            if (result.CompletedSynchronously)
            {
                return;
            }

            StreamSecurityUpgradeInitiatorAsyncResult thisPtr =
                (StreamSecurityUpgradeInitiatorAsyncResult)result.AsyncState;

            Exception completionException = null;
            try
            {
                thisPtr.CompleteAuthenticateAsClient(result);
            }
#pragma warning suppress 56500 // [....], transferring exception to another thread
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                completionException = e;
            }
            thisPtr.Complete(false, completionException);
        }

        protected abstract IAsyncResult OnBeginAuthenticateAsClient(Stream stream, AsyncCallback callback);
        protected abstract Stream OnCompleteAuthenticateAsClient(IAsyncResult result);
        protected abstract SecurityMessageProperty ValidateCreateSecurity();
    }
}