/src/connectedhomeip/src/setup_payload/QRCodeSetupPayloadGenerator.cpp
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2020 Project CHIP Authors |
4 | | * |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | /** |
19 | | * @file |
20 | | * This file implements a QRCode Setup Payload generator in accordance |
21 | | * with the CHIP specification. |
22 | | * |
23 | | */ |
24 | | |
25 | | #include "QRCodeSetupPayloadGenerator.h" |
26 | | #include "Base38Encode.h" |
27 | | |
28 | | #include <lib/core/CHIPCore.h> |
29 | | #include <lib/core/TLV.h> |
30 | | #include <lib/core/TLVData.h> |
31 | | #include <lib/core/TLVDebug.h> |
32 | | #include <lib/core/TLVUtilities.h> |
33 | | #include <lib/support/CodeUtils.h> |
34 | | #include <lib/support/SafeInt.h> |
35 | | #include <lib/support/ScopedMemoryBuffer.h> |
36 | | #include <protocols/Protocols.h> |
37 | | |
38 | | #include <stdlib.h> |
39 | | #include <string.h> |
40 | | #include <string> |
41 | | |
42 | | namespace chip { |
43 | | |
44 | | // Populates numberOfBits starting from LSB of input into bits, which is assumed to be zero-initialized |
45 | | static CHIP_ERROR populateBits(uint8_t * bits, size_t & offset, uint64_t input, size_t numberOfBits, |
46 | | size_t totalPayloadDataSizeInBits) |
47 | 0 | { |
48 | 0 | VerifyOrReturnError(offset + numberOfBits <= totalPayloadDataSizeInBits, CHIP_ERROR_INVALID_ARGUMENT); |
49 | 0 | VerifyOrReturnError(input < 1u << numberOfBits, CHIP_ERROR_INVALID_ARGUMENT); |
50 | | |
51 | 0 | size_t index = offset; |
52 | 0 | offset += numberOfBits; |
53 | 0 | while (input != 0) |
54 | 0 | { |
55 | 0 | if (input & 1) |
56 | 0 | { |
57 | 0 | const uint8_t mask = static_cast<uint8_t>(1 << index % 8); |
58 | 0 | bits[index / 8] = static_cast<uint8_t>(bits[index / 8] | mask); |
59 | 0 | } |
60 | 0 | index++; |
61 | 0 | input >>= 1; |
62 | 0 | } |
63 | 0 | return CHIP_NO_ERROR; |
64 | 0 | } |
65 | | |
66 | | static CHIP_ERROR populateTLVBits(uint8_t * bits, size_t & offset, const uint8_t * tlvBuf, size_t tlvBufSizeInBytes, |
67 | | size_t totalPayloadDataSizeInBits) |
68 | 0 | { |
69 | 0 | for (size_t i = 0; i < tlvBufSizeInBytes; i++) |
70 | 0 | { |
71 | 0 | const uint8_t value = tlvBuf[i]; |
72 | 0 | ReturnErrorOnFailure(populateBits(bits, offset, value, 8, totalPayloadDataSizeInBits)); |
73 | 0 | } |
74 | 0 | return CHIP_NO_ERROR; |
75 | 0 | } |
76 | | |
77 | | CHIP_ERROR writeTag(TLV::TLVWriter & writer, TLV::Tag tag, OptionalQRCodeInfo & info) |
78 | 0 | { |
79 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
80 | |
|
81 | 0 | if (info.type == optionalQRCodeInfoTypeString) |
82 | 0 | { |
83 | 0 | err = writer.PutString(tag, info.data.c_str()); |
84 | 0 | } |
85 | 0 | else if (info.type == optionalQRCodeInfoTypeInt32) |
86 | 0 | { |
87 | 0 | err = writer.Put(tag, info.int32); |
88 | 0 | } |
89 | 0 | else |
90 | 0 | { |
91 | 0 | err = CHIP_ERROR_INVALID_ARGUMENT; |
92 | 0 | } |
93 | |
|
94 | 0 | return err; |
95 | 0 | } |
96 | | |
97 | | CHIP_ERROR writeTag(TLV::TLVWriter & writer, TLV::Tag tag, OptionalQRCodeInfoExtension & info) |
98 | 0 | { |
99 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
100 | |
|
101 | 0 | if (info.type == optionalQRCodeInfoTypeString || info.type == optionalQRCodeInfoTypeInt32) |
102 | 0 | { |
103 | 0 | err = writeTag(writer, tag, static_cast<OptionalQRCodeInfo &>(info)); |
104 | 0 | } |
105 | 0 | else if (info.type == optionalQRCodeInfoTypeInt64) |
106 | 0 | { |
107 | 0 | err = writer.Put(tag, info.int64); |
108 | 0 | } |
109 | 0 | else if (info.type == optionalQRCodeInfoTypeUInt32) |
110 | 0 | { |
111 | 0 | err = writer.Put(tag, info.uint32); |
112 | 0 | } |
113 | 0 | else if (info.type == optionalQRCodeInfoTypeUInt64) |
114 | 0 | { |
115 | 0 | err = writer.Put(tag, info.uint64); |
116 | 0 | } |
117 | 0 | else |
118 | 0 | { |
119 | 0 | err = CHIP_ERROR_INVALID_ARGUMENT; |
120 | 0 | } |
121 | |
|
122 | 0 | return err; |
123 | 0 | } |
124 | | |
125 | | CHIP_ERROR QRCodeSetupPayloadGenerator::generateTLVFromOptionalData(SetupPayload & outPayload, uint8_t * tlvDataStart, |
126 | | uint32_t maxLen, size_t & tlvDataLengthInBytes) |
127 | 0 | { |
128 | 0 | std::vector<OptionalQRCodeInfo> optionalData = outPayload.getAllOptionalVendorData(); |
129 | 0 | std::vector<OptionalQRCodeInfoExtension> optionalExtensionData = outPayload.getAllOptionalExtensionData(); |
130 | 0 | VerifyOrReturnError(!optionalData.empty() || !optionalExtensionData.empty(), CHIP_NO_ERROR); |
131 | | |
132 | 0 | TLV::TLVWriter rootWriter; |
133 | 0 | rootWriter.Init(tlvDataStart, maxLen); |
134 | |
|
135 | 0 | TLV::TLVWriter innerStructureWriter; |
136 | |
|
137 | 0 | ReturnErrorOnFailure(rootWriter.OpenContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, innerStructureWriter)); |
138 | | |
139 | 0 | for (OptionalQRCodeInfo info : optionalData) |
140 | 0 | { |
141 | 0 | ReturnErrorOnFailure(writeTag(innerStructureWriter, TLV::ContextTag(info.tag), info)); |
142 | 0 | } |
143 | | |
144 | 0 | for (OptionalQRCodeInfoExtension info : optionalExtensionData) |
145 | 0 | { |
146 | 0 | ReturnErrorOnFailure(writeTag(innerStructureWriter, TLV::ContextTag(info.tag), info)); |
147 | 0 | } |
148 | | |
149 | 0 | ReturnErrorOnFailure(rootWriter.CloseContainer(innerStructureWriter)); |
150 | | |
151 | 0 | ReturnErrorOnFailure(rootWriter.Finalize()); |
152 | | |
153 | 0 | tlvDataLengthInBytes = rootWriter.GetLengthWritten(); |
154 | |
|
155 | 0 | return CHIP_NO_ERROR; |
156 | 0 | } |
157 | | |
158 | | static CHIP_ERROR generateBitSet(PayloadContents & payload, MutableByteSpan & bits, uint8_t * tlvDataStart, |
159 | | size_t tlvDataLengthInBytes) |
160 | 0 | { |
161 | 0 | size_t offset = 0; |
162 | 0 | size_t totalPayloadSizeInBits = kTotalPayloadDataSizeInBits + (tlvDataLengthInBytes * 8); |
163 | 0 | VerifyOrReturnError(bits.size() * 8 >= totalPayloadSizeInBits, CHIP_ERROR_BUFFER_TOO_SMALL); |
164 | | |
165 | | // isValidQRCodePayload() has already performed all relevant checks (including that we have a |
166 | | // long discriminator and rendevouz information). But if AllowInvalidPayload is set these |
167 | | // requirements might be violated; in that case simply encode 0 for the relevant fields. |
168 | | // Encoding an invalid (or partially valid) payload is useful for clients that need to be able |
169 | | // to serialize and deserialize partially populated or invalid payloads. |
170 | 0 | ReturnErrorOnFailure( |
171 | 0 | populateBits(bits.data(), offset, payload.version, kVersionFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
172 | 0 | ReturnErrorOnFailure( |
173 | 0 | populateBits(bits.data(), offset, payload.vendorID, kVendorIDFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
174 | 0 | ReturnErrorOnFailure( |
175 | 0 | populateBits(bits.data(), offset, payload.productID, kProductIDFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
176 | 0 | ReturnErrorOnFailure(populateBits(bits.data(), offset, static_cast<uint64_t>(payload.commissioningFlow), |
177 | 0 | kCommissioningFlowFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
178 | 0 | ReturnErrorOnFailure(populateBits(bits.data(), offset, |
179 | 0 | payload.rendezvousInformation.ValueOr(RendezvousInformationFlag::kNone).Raw(), |
180 | 0 | kRendezvousInfoFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
181 | 0 | auto const & pd = payload.discriminator; |
182 | 0 | ReturnErrorOnFailure(populateBits(bits.data(), offset, (!pd.IsShortDiscriminator() ? pd.GetLongValue() : 0), |
183 | 0 | kPayloadDiscriminatorFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
184 | 0 | ReturnErrorOnFailure( |
185 | 0 | populateBits(bits.data(), offset, payload.setUpPINCode, kSetupPINCodeFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
186 | 0 | ReturnErrorOnFailure(populateBits(bits.data(), offset, 0, kPaddingFieldLengthInBits, kTotalPayloadDataSizeInBits)); |
187 | 0 | ReturnErrorOnFailure(populateTLVBits(bits.data(), offset, tlvDataStart, tlvDataLengthInBytes, totalPayloadSizeInBits)); |
188 | | |
189 | 0 | return CHIP_NO_ERROR; |
190 | 0 | } |
191 | | |
192 | | static CHIP_ERROR payloadBase38RepresentationWithTLV(PayloadContents & payload, MutableCharSpan & outBuffer, MutableByteSpan bits, |
193 | | uint8_t * tlvDataStart, size_t tlvDataLengthInBytes, |
194 | | const char * prefix = kQRCodePrefix) |
195 | 0 | { |
196 | 0 | memset(bits.data(), 0, bits.size()); |
197 | 0 | ReturnErrorOnFailure(generateBitSet(payload, bits, tlvDataStart, tlvDataLengthInBytes)); |
198 | | |
199 | 0 | CHIP_ERROR err = CHIP_NO_ERROR; |
200 | 0 | size_t prefixLen = strlen(prefix); |
201 | |
|
202 | 0 | if (outBuffer.size() < prefixLen + 1) |
203 | 0 | { |
204 | 0 | err = CHIP_ERROR_BUFFER_TOO_SMALL; |
205 | 0 | } |
206 | 0 | else |
207 | 0 | { |
208 | 0 | MutableCharSpan subSpan = outBuffer.SubSpan(prefixLen, outBuffer.size() - prefixLen); |
209 | 0 | memcpy(outBuffer.data(), prefix, prefixLen); |
210 | 0 | err = base38Encode(bits, subSpan); |
211 | | // Reduce output span size to be the size of written data |
212 | 0 | outBuffer.reduce_size(subSpan.size() + prefixLen); |
213 | 0 | } |
214 | |
|
215 | 0 | return err; |
216 | 0 | } |
217 | | |
218 | | CHIP_ERROR QRCodeSetupPayloadGenerator::payloadBase38Representation(std::string & base38Representation) |
219 | 0 | { |
220 | | // 6.1.2.2. Table: Packed Binary Data Structure |
221 | | // The TLV Data should be 0 length if TLV is not included. |
222 | 0 | return payloadBase38Representation(base38Representation, nullptr, 0); |
223 | 0 | } |
224 | | |
225 | | CHIP_ERROR QRCodeSetupPayloadGenerator::payloadBase38RepresentationWithAutoTLVBuffer(std::string & base38Representation) |
226 | 0 | { |
227 | | // Estimate the size of the needed buffer. |
228 | 0 | size_t estimate = 0; |
229 | |
|
230 | 0 | auto dataItemSizeEstimate = [](const OptionalQRCodeInfo & item) { |
231 | | // Each data item needs a control byte and a context tag. |
232 | 0 | size_t size = 2; |
233 | |
|
234 | 0 | if (item.type == optionalQRCodeInfoTypeString) |
235 | 0 | { |
236 | | // We'll need to encode the string length and then the string data. |
237 | | // Length is at most 8 bytes. |
238 | 0 | size += 8; |
239 | 0 | size += item.data.size(); |
240 | 0 | } |
241 | 0 | else |
242 | 0 | { |
243 | | // Integer. Assume it might need up to 8 bytes, for simplicity. |
244 | 0 | size += 8; |
245 | 0 | } |
246 | 0 | return size; |
247 | 0 | }; |
248 | |
|
249 | 0 | auto vendorData = mPayload.getAllOptionalVendorData(); |
250 | 0 | for (auto & data : vendorData) |
251 | 0 | { |
252 | 0 | estimate += dataItemSizeEstimate(data); |
253 | 0 | } |
254 | |
|
255 | 0 | auto extensionData = mPayload.getAllOptionalExtensionData(); |
256 | 0 | for (auto & data : extensionData) |
257 | 0 | { |
258 | 0 | estimate += dataItemSizeEstimate(data); |
259 | 0 | } |
260 | |
|
261 | 0 | estimate = TLV::EstimateStructOverhead(estimate); |
262 | |
|
263 | 0 | VerifyOrReturnError(CanCastTo<uint32_t>(estimate), CHIP_ERROR_NO_MEMORY); |
264 | | |
265 | 0 | Platform::ScopedMemoryBuffer<uint8_t> buf; |
266 | 0 | VerifyOrReturnError(buf.Alloc(estimate), CHIP_ERROR_NO_MEMORY); |
267 | | |
268 | 0 | return payloadBase38Representation(base38Representation, buf.Get(), static_cast<uint32_t>(estimate)); |
269 | 0 | } |
270 | | |
271 | | CHIP_ERROR QRCodeSetupPayloadGenerator::payloadBase38Representation(std::string & base38Representation, uint8_t * tlvDataStart, |
272 | | uint32_t tlvDataStartSize) |
273 | 0 | { |
274 | 0 | size_t tlvDataLengthInBytes = 0; |
275 | |
|
276 | 0 | VerifyOrReturnError(mAllowInvalidPayload || mPayload.isValidQRCodePayload(), CHIP_ERROR_INVALID_ARGUMENT); |
277 | 0 | ReturnErrorOnFailure(generateTLVFromOptionalData(mPayload, tlvDataStart, tlvDataStartSize, tlvDataLengthInBytes)); |
278 | | |
279 | 0 | std::vector<uint8_t> bits(kTotalPayloadDataSizeInBytes + tlvDataLengthInBytes); |
280 | 0 | MutableByteSpan bitsSpan(bits.data(), bits.size()); |
281 | 0 | std::vector<char> buffer(base38EncodedLength(bits.size()) + strlen(kQRCodePrefix)); |
282 | 0 | MutableCharSpan bufferSpan(buffer.data(), buffer.size()); |
283 | |
|
284 | 0 | static constexpr char kDelimiterString[]{ kPayloadDelimiter, 0 }; |
285 | 0 | ReturnErrorOnFailure(payloadBase38RepresentationWithTLV(mPayload, bufferSpan, bitsSpan, tlvDataStart, tlvDataLengthInBytes, |
286 | 0 | (mAsConcatenation ? kDelimiterString : kQRCodePrefix))); |
287 | | |
288 | 0 | base38Representation.assign(bufferSpan.data()); |
289 | 0 | return CHIP_NO_ERROR; |
290 | 0 | } |
291 | | |
292 | | CHIP_ERROR QRCodeBasicSetupPayloadGenerator::payloadBase38Representation(MutableCharSpan & outBuffer) |
293 | 0 | { |
294 | 0 | uint8_t bits[kTotalPayloadDataSizeInBytes]; |
295 | 0 | VerifyOrReturnError(mPayload.isValidQRCodePayload(), CHIP_ERROR_INVALID_ARGUMENT); |
296 | | |
297 | 0 | return payloadBase38RepresentationWithTLV(mPayload, outBuffer, MutableByteSpan(bits), nullptr, 0); |
298 | 0 | } |
299 | | |
300 | | } // namespace chip |