forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSecBufferDesc.h
More file actions
169 lines (139 loc) · 3.31 KB
/
AutoSecBufferDesc.h
File metadata and controls
169 lines (139 loc) · 3.31 KB
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
//
// AutoSecBufferDesc.h
//
// $Id$
//
// Library: NetSSL_Win
// Package: SSLCore
// Module: AutoSecBufferDesc
//
// Definition of the AutoSecBufferDesc class.
//
// Copyright (c) 2006-2014, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef NetSSL_AutoSecBufferDesc_INCLUDED
#define NetSSL_AutoSecBufferDesc_INCLUDED
#include "Poco/Net/NetSSL.h"
#include <windows.h>
#include <wincrypt.h>
#ifndef SECURITY_WIN32
#define SECURITY_WIN32
#endif
#include <sspi.h>
namespace Poco {
namespace Net {
template <int numBufs>
class AutoSecBufferDesc: public SecBufferDesc
/// AutoSecBufferDesc is a helper class for automatic initialization and release of SecBuffer objects.
{
public:
AutoSecBufferDesc(SecurityFunctionTableW* pSec, bool autoRelease):
/// Creates a AutoSecBufferDesc. If autoRelease is true, the buffers will be released with the provided pSec function.
_pSec(pSec),
_autoRelease(autoRelease)
{
poco_check_ptr (_pSec);
poco_static_assert (numBufs > 0);
initBuffers();
cBuffers = numBufs;
pBuffers = _buffers;
ulVersion = SECBUFFER_VERSION;
}
~AutoSecBufferDesc()
/// Destroys the AutoSecBufferDesc
{
release();
}
void reset(bool autoRelease)
{
release();
_autoRelease = autoRelease;
initBuffers();
cBuffers = numBufs;
pBuffers = _buffers;
ulVersion = SECBUFFER_VERSION;
}
void release()
{
if (_autoRelease)
{
for (int i = 0; i < numBufs; ++i)
{
if (_buffers[i].pvBuffer)
{
_pSec->FreeContextBuffer(_buffers[i].pvBuffer);
}
}
_autoRelease = false;
}
}
SecBuffer& operator [] (Poco::UInt32 idx)
{
return _buffers[idx];
}
const SecBuffer& operator [] (Poco::UInt32 idx) const
{
return _buffers[idx];
}
void release(int idx)
/// Will release the buffer if necessary
{
release(idx, _autoRelease);
}
void setSecBufferEmpty(int idx)
{
release(idx, _autoRelease);
}
void setSecBufferData(int idx, void* pData, int len)
{
setContent(idx, pData, len, SECBUFFER_DATA);
}
void setSecBufferToken(int idx, void* pData, int len)
{
setContent(idx, pData, len, SECBUFFER_TOKEN);
}
void setSecBufferStreamHeader(int idx, void* pData, int len)
{
setContent(idx, pData, len, SECBUFFER_STREAM_HEADER);
}
void setSecBufferStreamTrailer(int idx, void* pData, int len)
{
setContent(idx, pData, len, SECBUFFER_STREAM_TRAILER);
}
private:
AutoSecBufferDesc(const AutoSecBufferDesc& desc);
AutoSecBufferDesc& operator = (const AutoSecBufferDesc& desc);
void release(int idx, bool force)
{
if (force && _buffers[idx].pvBuffer)
_pSec->FreeContextBuffer(_buffers[idx].pvBuffer);
_buffers[idx].pvBuffer = 0;
_buffers[idx].cbBuffer = 0;
_buffers[idx].BufferType = SECBUFFER_EMPTY;
}
void initBuffers()
{
for (int i = 0; i < numBufs; ++i)
{
_buffers[i].pvBuffer = 0;
_buffers[i].cbBuffer = 0;
_buffers[i].BufferType = SECBUFFER_EMPTY;
}
}
void setContent(int idx, void* pData, int len, unsigned long type)
{
release(idx, _autoRelease);
_buffers[idx].pvBuffer = pData;
_buffers[idx].cbBuffer = len;
_buffers[idx].BufferType = type;
}
private:
SecurityFunctionTableW* _pSec;
bool _autoRelease;
SecBuffer _buffers[numBufs];
};
} } // namespace Poco::Net
#endif // NetSSL_AutoSecBufferDesc_INCLUDED