summaryrefslogtreecommitdiffstats
path: root/src/knx/netip/qknxnetipsessionresponse.cpp
blob: 7c01050be7a3685cdae266152b957c6ec200abda (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/******************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtKnx module.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
******************************************************************************/

#include "qknxnetipsessionresponse.h"

QT_BEGIN_NAMESPACE

/*!
    \class QKnxNetIpSessionResponseProxy

    \since 5.12
    \inmodule QtKnx
    \ingroup qtknx-netip

    \brief The QKnxNetIpSessionResponseProxy class provides the means to
    introspect session response data inside the generic \l QKnxNetIpFrame
    class and to create a KNXnet/IP session response frame from provided
    data.

    This class is part of the Qt KNX module and currently available as a
    Technology Preview, and therefore the API and functionality provided
    by the class may be subject to change at any time without prior notice.

    This frame will be sent by the KNXnet/IP secure server to the KNXnet/IP
    secure client control endpoint in response to a received secure session
    request frame.

    \note When using QKnxNetIpSessionResponseProxy, care must be taken to
    ensure that the referenced KNXnet/IP frame outlives the proxy on all code
    paths, lest the proxy ends up referencing deleted data.

    The following code sample illustrates how to read the session response
    information:

    \code
        auto netIpFrame = QKnxNetIpFrame::fromBytes(...);

        const QKnxNetIpSessionResponseProxy proxy(netIpFrame);
        if (!proxy.isValid())
            return;

        auto endPoint = proxy.controlEndpoint();
        auto publicKey = proxy.publicKey();
        auto authenticationCode = proxy.messageAuthenticationCode();
    \endcode

    \sa builder(), QKnxNetIpSessionRequestProxy, {Qt KNXnet/IP Connection Classes}
*/

/*!
    \fn QKnxNetIpSessionResponseProxy::QKnxNetIpSessionResponseProxy()
    \internal
*/

/*!
    \fn QKnxNetIpSessionResponseProxy::~QKnxNetIpSessionResponseProxy()
    \internal
*/

/*!
    \fn QKnxNetIpSessionResponseProxy::QKnxNetIpSessionResponseProxy(const QKnxNetIpFrame &&)
    \internal
*/

/*!
    Constructs a proxy object to read the session response information
    carried by the specified KNXnet/IP frame \a frame.
*/
QKnxNetIpSessionResponseProxy::QKnxNetIpSessionResponseProxy(const QKnxNetIpFrame &frame)
    : m_frame(frame)
{}

/*!
    Returns \c true if the frame contains initialized values and is in itself
    valid, otherwise returns \c false. A valid KNXnet/IP frame consists of
    at least a valid header and a size in bytes corresponding to the total size
    of the KNXnet/IP frame header.

    \note KNXnet/IP session response frames currently have a fixed size of
    \c 56 bytes.

    \sa QKnxNetIpFrameHeader::totalSize()
*/
bool QKnxNetIpSessionResponseProxy::isValid() const
{
    return m_frame.isValid() && m_frame.serviceType() == QKnxNetIp::ServiceType::SessionResponse
        && m_frame.size() == 56;
}

/*!
    Returns the secure session ID from the generic KNXnet/IP session response
    frame if the object that was passed during construction was valid; otherwise
    returns a \l {default-constructed value} which can be \c 0.
*/
quint16 QKnxNetIpSessionResponseProxy::secureSessionId() const
{
    return QKnxUtils::QUint16::fromBytes(m_frame.constData());
}

/*!
    Returns the public key from the generic KNXnet/IP session response frame.
    The public key needs to be generated using the Curve25519 algorithm and
    has a fixed size of \c 32 bytes.
*/
QKnxByteArray QKnxNetIpSessionResponseProxy::publicKey() const
{
    return m_frame.constData().mid(sizeof(quint16), 32);
}

/*!
    Returns the AES128 CCM message authentication code from the generic
    KNXnet/IP session response frame with a fixed size of \c 16 bytes.
*/
QKnxByteArray QKnxNetIpSessionResponseProxy::messageAuthenticationCode() const
{
    return m_frame.constData().mid(sizeof(quint16) + 32);
}

/*!
    Returns a builder object to create a KNXnet/IP session response frame.
*/
QKnxNetIpSessionResponseProxy::Builder QKnxNetIpSessionResponseProxy::builder()
{
    return QKnxNetIpSessionResponseProxy::Builder();
}


/*!
    \class QKnxNetIpSessionResponseProxy::Builder

    \inmodule QtKnx
    \inheaderfile QKnxNetIpSessionResponseProxy

    \brief The QKnxNetIpSessionResponseProxy::Builder class provides the
    means to create a KNXnet/IP session response frame.

    This class is part of the Qt KNX module and currently available as a
    Technology Preview, and therefore the API and functionality provided
    by the class may be subject to change at any time without prior notice.

    This frame will be sent by the KNXnet/IP secure server to the KNXnet/IP
    secure client control endpoint in response to a received secure session
    request frame.

    The common way to create a session response frame is:

    \code
        auto publicKey = ... // create the public key
        auto auth = ... // create the full 128 bit CCM-MAC

        auto netIpFrame = QKnxNetIpSessionResponseProxy::builder()
            .setSecureSessionId(0x1976)
            .setPublicKey(publicKey)
            .setMessageAuthenticationCode(auth)
            .create();
    \endcode

    \sa QKnxCryptographicEngine
*/

/*!
    Sets the secure session ID of the KNXnet/IP session response frame to
    \a sessionId and returns a reference to the builder.
*/
QKnxNetIpSessionResponseProxy::Builder &
    QKnxNetIpSessionResponseProxy::Builder::setSecureSessionId(quint16 sessionId)
{
    m_id = sessionId;
    return *this;
}

/*!
    Sets the public key of the KNXnet/IP session response frame to \a publicKey
    and returns a reference to the builder. The public key needs to be generated
    using the Curve25519 algorithm and has a fixed size of \c 32 bytes.
*/
QKnxNetIpSessionResponseProxy::Builder &
    QKnxNetIpSessionResponseProxy::Builder::setPublicKey(const QKnxByteArray &publicKey)
{
    m_publicKey = publicKey;
    return *this;
}

/*!
    Sets the AES128 CCM message authentication code of the generic KNXnet/IP
    session response frame to \a data and returns a reference to builder. The
    message authentication code has a fixed size of \c 16 bytes.
*/
QKnxNetIpSessionResponseProxy::Builder &
    QKnxNetIpSessionResponseProxy::Builder::setMessageAuthenticationCode(const QKnxByteArray &data)
{
    m_authCode = data;
    return *this;
}

/*!
    Creates and returns a KNXnet/IP session response frame.

    \note The returned frame may be invalid depending on the values used during
    setup.

    \sa isValid()
*/
QKnxNetIpFrame QKnxNetIpSessionResponseProxy::Builder::create() const
{
    if (m_id < 0 || m_publicKey.size() != 32 || m_authCode.size() != 16)
        return { QKnxNetIp::ServiceType::SessionResponse };
    return { QKnxNetIp::ServiceType::SessionResponse, QKnxUtils::QUint16::bytes(m_id) + m_publicKey
        + m_authCode };
}

QT_END_NAMESPACE