1 
2 /*
3  *  Copyright (c) 2017, The OpenThread Authors.
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions are met:
8  *  1. Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *  3. Neither the name of the copyright holder nor the
14  *     names of its contributors may be used to endorse or promote products
15  *     derived from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /**
31  * @file
32  *   This file implements common MeshCoP utility functions.
33  */
34 
35 #include "meshcop.hpp"
36 
37 #include "common/crc16.hpp"
38 #include "instance/instance.hpp"
39 
40 namespace ot {
41 
42 RegisterLogModule("MeshCoP");
43 
44 namespace MeshCoP {
45 
SetFrom(const char * aPskdString)46 Error JoinerPskd::SetFrom(const char *aPskdString)
47 {
48     Error error = kErrorNone;
49 
50     VerifyOrExit(IsPskdValid(aPskdString), error = kErrorInvalidArgs);
51 
52     Clear();
53     memcpy(m8, aPskdString, StringLength(aPskdString, sizeof(m8)));
54 
55 exit:
56     return error;
57 }
58 
operator ==(const JoinerPskd & aOther) const59 bool JoinerPskd::operator==(const JoinerPskd &aOther) const
60 {
61     bool isEqual = true;
62 
63     for (size_t i = 0; i < sizeof(m8); i++)
64     {
65         if (m8[i] != aOther.m8[i])
66         {
67             isEqual = false;
68             ExitNow();
69         }
70 
71         if (m8[i] == '\0')
72         {
73             break;
74         }
75     }
76 
77 exit:
78     return isEqual;
79 }
80 
IsPskdValid(const char * aPskdString)81 bool JoinerPskd::IsPskdValid(const char *aPskdString)
82 {
83     bool     valid      = false;
84     uint16_t pskdLength = StringLength(aPskdString, kMaxLength + 1);
85 
86     VerifyOrExit(pskdLength >= kMinLength && pskdLength <= kMaxLength);
87 
88     for (uint16_t i = 0; i < pskdLength; i++)
89     {
90         char c = aPskdString[i];
91 
92         VerifyOrExit(IsDigit(c) || IsUppercase(c));
93         VerifyOrExit(c != 'I' && c != 'O' && c != 'Q' && c != 'Z');
94     }
95 
96     valid = true;
97 
98 exit:
99     return valid;
100 }
101 
GenerateJoinerId(Mac::ExtAddress & aJoinerId) const102 void JoinerDiscerner::GenerateJoinerId(Mac::ExtAddress &aJoinerId) const
103 {
104     aJoinerId.GenerateRandom();
105     CopyTo(aJoinerId);
106     aJoinerId.SetLocal(true);
107 }
108 
Matches(const Mac::ExtAddress & aJoinerId) const109 bool JoinerDiscerner::Matches(const Mac::ExtAddress &aJoinerId) const
110 {
111     uint64_t mask;
112 
113     OT_ASSERT(IsValid());
114 
115     mask = GetMask();
116 
117     return (BigEndian::ReadUint64(aJoinerId.m8) & mask) == (mValue & mask);
118 }
119 
CopyTo(Mac::ExtAddress & aExtAddress) const120 void JoinerDiscerner::CopyTo(Mac::ExtAddress &aExtAddress) const
121 {
122     // Copies the discerner value up to its bit length to `aExtAddress`
123     // array, assuming big-endian encoding (i.e., the discerner lowest bits
124     // are copied at end of `aExtAddress.m8[]` array). Any initial/remaining
125     // bits of `aExtAddress` array remain unchanged.
126 
127     uint8_t *cur       = &aExtAddress.m8[sizeof(Mac::ExtAddress) - 1];
128     uint8_t  remaining = mLength;
129     uint64_t value     = mValue;
130 
131     OT_ASSERT(IsValid());
132 
133     // Write full bytes
134     while (remaining >= kBitsPerByte)
135     {
136         *cur = static_cast<uint8_t>(value & 0xff);
137         value >>= kBitsPerByte;
138         cur--;
139         remaining -= kBitsPerByte;
140     }
141 
142     // Write any remaining bits (not a full byte)
143     if (remaining != 0)
144     {
145         uint8_t mask = static_cast<uint8_t>((1U << remaining) - 1);
146 
147         // `mask` has it lower (lsb) `remaining` bits as `1` and rest as `0`.
148         // Example with `remaining = 3` -> (1 << 3) - 1 = 0b1000 - 1 = 0b0111.
149 
150         *cur &= ~mask;
151         *cur |= static_cast<uint8_t>(value & mask);
152     }
153 }
154 
operator ==(const JoinerDiscerner & aOther) const155 bool JoinerDiscerner::operator==(const JoinerDiscerner &aOther) const
156 {
157     uint64_t mask = GetMask();
158 
159     return IsValid() && (mLength == aOther.mLength) && ((mValue & mask) == (aOther.mValue & mask));
160 }
161 
ToString(void) const162 JoinerDiscerner::InfoString JoinerDiscerner::ToString(void) const
163 {
164     InfoString string;
165 
166     if (mLength <= BitSizeOf(uint16_t))
167     {
168         string.Append("0x%04x", static_cast<uint16_t>(mValue));
169     }
170     else if (mLength <= BitSizeOf(uint32_t))
171     {
172         string.Append("0x%08lx", ToUlong(static_cast<uint32_t>(mValue)));
173     }
174     else
175     {
176         string.Append("0x%lx-%08lx", ToUlong(static_cast<uint32_t>(mValue >> 32)),
177                       ToUlong(static_cast<uint32_t>(mValue)));
178     }
179 
180     string.Append("/len:%d", mLength);
181 
182     return string;
183 }
184 
Init(uint8_t aLength)185 void SteeringData::Init(uint8_t aLength)
186 {
187     OT_ASSERT(aLength <= kMaxLength);
188     mLength = aLength;
189     ClearAllBytes(m8);
190 }
191 
SetToPermitAllJoiners(void)192 void SteeringData::SetToPermitAllJoiners(void)
193 {
194     Init(1);
195     m8[0] = kPermitAll;
196 }
197 
UpdateBloomFilter(const Mac::ExtAddress & aJoinerId)198 void SteeringData::UpdateBloomFilter(const Mac::ExtAddress &aJoinerId)
199 {
200     HashBitIndexes indexes;
201 
202     CalculateHashBitIndexes(aJoinerId, indexes);
203     UpdateBloomFilter(indexes);
204 }
205 
UpdateBloomFilter(const JoinerDiscerner & aDiscerner)206 void SteeringData::UpdateBloomFilter(const JoinerDiscerner &aDiscerner)
207 {
208     HashBitIndexes indexes;
209 
210     CalculateHashBitIndexes(aDiscerner, indexes);
211     UpdateBloomFilter(indexes);
212 }
213 
UpdateBloomFilter(const HashBitIndexes & aIndexes)214 void SteeringData::UpdateBloomFilter(const HashBitIndexes &aIndexes)
215 {
216     OT_ASSERT((mLength > 0) && (mLength <= kMaxLength));
217 
218     SetBit(aIndexes.mIndex[0] % GetNumBits());
219     SetBit(aIndexes.mIndex[1] % GetNumBits());
220 }
221 
Contains(const Mac::ExtAddress & aJoinerId) const222 bool SteeringData::Contains(const Mac::ExtAddress &aJoinerId) const
223 {
224     HashBitIndexes indexes;
225 
226     CalculateHashBitIndexes(aJoinerId, indexes);
227 
228     return Contains(indexes);
229 }
230 
Contains(const JoinerDiscerner & aDiscerner) const231 bool SteeringData::Contains(const JoinerDiscerner &aDiscerner) const
232 {
233     HashBitIndexes indexes;
234 
235     CalculateHashBitIndexes(aDiscerner, indexes);
236 
237     return Contains(indexes);
238 }
239 
Contains(const HashBitIndexes & aIndexes) const240 bool SteeringData::Contains(const HashBitIndexes &aIndexes) const
241 {
242     return (mLength > 0) && GetBit(aIndexes.mIndex[0] % GetNumBits()) && GetBit(aIndexes.mIndex[1] % GetNumBits());
243 }
244 
CalculateHashBitIndexes(const Mac::ExtAddress & aJoinerId,HashBitIndexes & aIndexes)245 void SteeringData::CalculateHashBitIndexes(const Mac::ExtAddress &aJoinerId, HashBitIndexes &aIndexes)
246 {
247     Crc16 ccitt(Crc16::kCcitt);
248     Crc16 ansi(Crc16::kAnsi);
249 
250     for (uint8_t b : aJoinerId.m8)
251     {
252         ccitt.Update(b);
253         ansi.Update(b);
254     }
255 
256     aIndexes.mIndex[0] = ccitt.Get();
257     aIndexes.mIndex[1] = ansi.Get();
258 }
259 
CalculateHashBitIndexes(const JoinerDiscerner & aDiscerner,HashBitIndexes & aIndexes)260 void SteeringData::CalculateHashBitIndexes(const JoinerDiscerner &aDiscerner, HashBitIndexes &aIndexes)
261 {
262     Mac::ExtAddress address;
263 
264     address.Clear();
265     aDiscerner.CopyTo(address);
266 
267     CalculateHashBitIndexes(address, aIndexes);
268 }
269 
DoesAllMatch(uint8_t aMatch) const270 bool SteeringData::DoesAllMatch(uint8_t aMatch) const
271 {
272     bool matches = true;
273 
274     for (uint8_t i = 0; i < mLength; i++)
275     {
276         if (m8[i] != aMatch)
277         {
278             matches = false;
279             break;
280         }
281     }
282 
283     return matches;
284 }
285 
ComputeJoinerId(const Mac::ExtAddress & aEui64,Mac::ExtAddress & aJoinerId)286 void ComputeJoinerId(const Mac::ExtAddress &aEui64, Mac::ExtAddress &aJoinerId)
287 {
288     Crypto::Sha256       sha256;
289     Crypto::Sha256::Hash hash;
290 
291     sha256.Start();
292     sha256.Update(aEui64);
293     sha256.Finish(hash);
294 
295     memcpy(&aJoinerId, hash.GetBytes(), sizeof(aJoinerId));
296     aJoinerId.SetLocal(true);
297 }
298 
299 #if OPENTHREAD_FTD
GeneratePskc(const char * aPassPhrase,const NetworkName & aNetworkName,const ExtendedPanId & aExtPanId,Pskc & aPskc)300 Error GeneratePskc(const char          *aPassPhrase,
301                    const NetworkName   &aNetworkName,
302                    const ExtendedPanId &aExtPanId,
303                    Pskc                &aPskc)
304 {
305     Error      error        = kErrorNone;
306     const char saltPrefix[] = "Thread";
307     uint8_t    salt[OT_CRYPTO_PBDKF2_MAX_SALT_SIZE];
308     uint16_t   saltLen = 0;
309     uint16_t   passphraseLen;
310     uint8_t    networkNameLen;
311 
312     VerifyOrExit(IsValidUtf8String(aPassPhrase), error = kErrorInvalidArgs);
313 
314     passphraseLen  = static_cast<uint16_t>(StringLength(aPassPhrase, OT_COMMISSIONING_PASSPHRASE_MAX_SIZE + 1));
315     networkNameLen = static_cast<uint8_t>(StringLength(aNetworkName.GetAsCString(), OT_NETWORK_NAME_MAX_SIZE + 1));
316 
317     VerifyOrExit((passphraseLen >= OT_COMMISSIONING_PASSPHRASE_MIN_SIZE) &&
318                      (passphraseLen <= OT_COMMISSIONING_PASSPHRASE_MAX_SIZE) &&
319                      (networkNameLen <= OT_NETWORK_NAME_MAX_SIZE),
320                  error = kErrorInvalidArgs);
321 
322     ClearAllBytes(salt);
323     memcpy(salt, saltPrefix, sizeof(saltPrefix) - 1);
324     saltLen += static_cast<uint16_t>(sizeof(saltPrefix) - 1);
325 
326     memcpy(salt + saltLen, aExtPanId.m8, sizeof(aExtPanId));
327     saltLen += OT_EXT_PAN_ID_SIZE;
328 
329     memcpy(salt + saltLen, aNetworkName.GetAsCString(), networkNameLen);
330     saltLen += networkNameLen;
331 
332     error = otPlatCryptoPbkdf2GenerateKey(reinterpret_cast<const uint8_t *>(aPassPhrase), passphraseLen, salt, saltLen,
333                                           16384, OT_PSKC_MAX_SIZE, aPskc.m8);
334 
335 exit:
336     return error;
337 }
338 #endif // OPENTHREAD_FTD
339 
340 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
341 
LogCertMessage(const char * aText,const Coap::Message & aMessage)342 void LogCertMessage(const char *aText, const Coap::Message &aMessage)
343 {
344     OT_UNUSED_VARIABLE(aText);
345 
346     uint8_t  buf[kBufferSize];
347     uint16_t length = aMessage.GetLength() - aMessage.GetOffset();
348 
349     VerifyOrExit(length <= sizeof(buf));
350     aMessage.ReadBytes(aMessage.GetOffset(), buf, length);
351 
352     DumpCert(aText, buf, length);
353 
354 exit:
355     return;
356 }
357 
358 #endif
359 
360 } // namespace MeshCoP
361 } // namespace ot
362