aboutsummaryrefslogtreecommitdiffstats
path: root/src/coap/qcoapprotocol.cpp
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2019-05-22 16:10:39 +0200
committerSona Kurazyan <sona.kurazyan@qt.io>2019-05-22 16:10:39 +0200
commit430a041c2ce117282a5ef660b65d0a81f61c00b7 (patch)
treeeb030b29d0b1017e671f1633fbcd5a9c4b8d5a9f /src/coap/qcoapprotocol.cpp
parentd2744ade327fe6b63bb80d6acd9c1cd7ae6e317f (diff)
Add a method for setting the minimum token size
For security reasons it is recommended to use tokens with a length of at least 4 bytes. Added a method for setting the minimum token size and changed it to be 4 by default. Change-Id: Ib589b338df8e59ccaf24dceab6691f92d92f861c Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/coap/qcoapprotocol.cpp')
-rw-r--r--src/coap/qcoapprotocol.cpp24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/coap/qcoapprotocol.cpp b/src/coap/qcoapprotocol.cpp
index 46f420c..33ca9d7 100644
--- a/src/coap/qcoapprotocol.cpp
+++ b/src/coap/qcoapprotocol.cpp
@@ -690,9 +690,7 @@ QCoapToken QCoapProtocolPrivate::generateUniqueToken() const
// TODO: Store used token for the period specified by CoAP spec
QCoapToken token;
while (isTokenRegistered(token)) {
- // TODO: Allow setting minimum token size as a security setting
- quint8 length = static_cast<quint8>(QtCoap::randomGenerator().bounded(1, 8));
-
+ quint8 length = static_cast<quint8>(QtCoap::randomGenerator().bounded(minimumTokenSize, 9));
token.resize(length);
quint8 *tokenData = reinterpret_cast<quint8 *>(token.data());
for (int i = 0; i < token.size(); ++i)
@@ -1151,4 +1149,24 @@ void QCoapProtocol::setMaximumServerResponseDelay(uint responseDelay)
d->maximumServerResponseDelay = responseDelay;
}
+/*!
+ \internal
+
+ Sets the minimum token size to \a tokenSize in bytes. For security reasons it is
+ recommended to use tokens with a length of at least 4 bytes. The default value for
+ this parameter is 4 bytes.
+*/
+void QCoapProtocol::setMinumumTokenSize(int tokenSize)
+{
+ Q_D(QCoapProtocol);
+
+ if (tokenSize > 0 && tokenSize <= 8) {
+ d->minimumTokenSize = tokenSize;
+ } else {
+ qCWarning(lcCoapProtocol,
+ "Failed to set the minimum token size,"
+ "it should not be more than 8 bytes and cannot be 0.");
+ }
+}
+
QT_END_NAMESPACE