summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/sys/windows/SslAsynchIO.h
blob: 3cdf2c8f0872683045ceee96041232f9f7b660a8 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef _sys_windows_SslAsynchIO
#define _sys_windows_SslAsynchIO

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "qpid/sys/AsynchIO.h"
#include "qpid/sys/IntegerTypes.h"
#include "qpid/sys/Poller.h"
#include "qpid/CommonImportExport.h"
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <windows.h>
// security.h needs to see this to distinguish from kernel use.
#define SECURITY_WIN32
#include <security.h>
#include <Schnlsp.h>
#undef SECURITY_WIN32

namespace qpid {
namespace sys {
namespace windows {
    
class Socket;
class Poller;

/*
 * SSL/Schannel shim between the frame-handling and AsynchIO layers.
 * SslAsynchIO creates a regular AsynchIO object to handle I/O and this class
 * gets involved for SSL negotiations and encrypt/decrypt. The details of
 * how this all works are invisible to the layers on either side. The only
 * change from normal AsynchIO usage is that there's an extra callback
 * from SslAsynchIO to indicate that the initial session negotiation is
 * complete.
 *
 * The details of session negotiation are different for client and server
 * SSL roles. These differences are handled by deriving separate client
 * and server role classes.
 */
class SslAsynchIO : public qpid::sys::AsynchIO {
public:
    typedef boost::function1<void, SECURITY_STATUS> NegotiateDoneCallback;

    SslAsynchIO(const qpid::sys::Socket& s,
                CredHandle hCred,
                ReadCallback rCb,
                EofCallback eofCb,
                DisconnectCallback disCb,
                ClosedCallback cCb = 0,
                BuffersEmptyCallback eCb = 0,
                IdleCallback iCb = 0,
                NegotiateDoneCallback nCb = 0);
    ~SslAsynchIO();

    virtual void queueForDeletion();

    virtual void start(qpid::sys::Poller::shared_ptr poller);
    virtual void queueReadBuffer(BufferBase* buff);
    virtual void unread(BufferBase* buff);
    virtual void queueWrite(BufferBase* buff);
    virtual void notifyPendingWrite();
    virtual void queueWriteClose();
    virtual bool writeQueueEmpty();
    virtual void startReading();
    virtual void stopReading();
    virtual void requestCallback(RequestCallback);
    virtual BufferBase* getQueuedBuffer();

    QPID_COMMON_EXTERN unsigned int getSslKeySize();

protected:
    CredHandle credHandle;

    // AsynchIO layer below that's actually doing the I/O
    qpid::sys::AsynchIO *aio;

    // Track what the state of the SSL session is. Have to know when it's
    // time to notify the upper layer that the session is up, and also to
    // know when it's not legit to pass data through to either side.
    enum { Negotiating, Running, Redo, ShuttingDown } state;
    bool sessionUp;
    CtxtHandle ctxtHandle;
    TimeStamp credExpiry;

    // Client- and server-side SSL subclasses implement these to do the
    // proper negotiation steps. negotiateStep() is called with a buffer
    // just received from the peer.
    virtual void startNegotiate() = 0;
    virtual void negotiateStep(BufferBase *buff) = 0;

    // The negotiating steps call one of these when it's finalized:
    void negotiationDone();
    void negotiationFailed(SECURITY_STATUS status);

private:
    // These are callbacks from AsynchIO to here.
    void sslDataIn(qpid::sys::AsynchIO& a, BufferBase *buff);
    void idle(qpid::sys::AsynchIO&);

    // These callbacks are to the layer above.
    ReadCallback readCallback;
    IdleCallback idleCallback;
    NegotiateDoneCallback negotiateDoneCallback;
    volatile unsigned int callbacksInProgress;   // >0 if w/in callbacks
    volatile bool queuedDelete;

    // Address of peer, in case it's needed for logging.
    std::string peerAddress;

    // Partial buffer of decrypted plaintext given back by the layer above.
    AsynchIO::BufferBase *leftoverPlaintext;

    SecPkgContext_StreamSizes schSizes;
};

/*
 * SSL/Schannel client-side shim between the frame-handling and AsynchIO
 * layers.
 */
class ClientSslAsynchIO : public SslAsynchIO {
public:
    // Args same as for SslIoShim, with the addition of brokerHost which is
    // the expected SSL name of the server.
    QPID_COMMON_EXTERN ClientSslAsynchIO(const std::string& brokerHost,
                                         const qpid::sys::Socket& s,
                                         CredHandle hCred,
                                         ReadCallback rCb,
                                         EofCallback eofCb,
                                         DisconnectCallback disCb,
                                         ClosedCallback cCb = 0,
                                         BuffersEmptyCallback eCb = 0,
                                         IdleCallback iCb = 0,
                                         NegotiateDoneCallback nCb = 0);

private:
    std::string serverHost;

    // Client- and server-side SSL subclasses implement these to do the
    // proper negotiation steps. negotiateStep() is called with a buffer
    // just received from the peer.
    void startNegotiate();
    void negotiateStep(BufferBase *buff);
};
/*
 * SSL/Schannel server-side shim between the frame-handling and AsynchIO
 * layers.
 */
class ServerSslAsynchIO : public SslAsynchIO {
public:
    QPID_COMMON_EXTERN ServerSslAsynchIO(bool clientMustAuthenticate,
                                         const qpid::sys::Socket& s,
                                         CredHandle hCred,
                                         ReadCallback rCb,
                                         EofCallback eofCb,
                                         DisconnectCallback disCb,
                                         ClosedCallback cCb = 0,
                                         BuffersEmptyCallback eCb = 0,
                                         IdleCallback iCb = 0,
                                         NegotiateDoneCallback nCb = 0);

private:
    bool clientAuth;

    // Client- and server-side SSL subclasses implement these to do the
    // proper negotiation steps. negotiateStep() is called with a buffer
    // just received from the peer.
    void startNegotiate();
    void negotiateStep(BufferBase *buff);
};

}}}   // namespace qpid::sys::windows

#endif // _sys_windows_SslAsynchIO