0.01.00
dtls.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, The OpenThread Authors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
34 #ifndef DTLS_HPP_
35 #define DTLS_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <openthread/types.h>
40 
41 #include <mbedtls/ssl.h>
42 #include <mbedtls/entropy.h>
43 #include <mbedtls/ctr_drbg.h>
44 #include <mbedtls/error.h>
45 #include <mbedtls/certs.h>
46 #include <mbedtls/ssl_cookie.h>
47 
48 #include "common/locator.hpp"
49 #include "common/message.hpp"
50 #include "common/timer.hpp"
51 #include "crypto/sha256.hpp"
52 #include "meshcop/meshcop_tlvs.hpp"
53 
54 namespace ot {
55 
56 class ThreadNetif;
57 
58 namespace MeshCoP {
59 
60 class Dtls: public InstanceLocator
61 {
62 public:
63  enum
64  {
65  kPskMaxLength = 32,
66  kApplicationDataMaxLength = 128,
67  };
68 
75  Dtls(otInstance &aInstance);
76 
84  typedef void (*ConnectedHandler)(void *aContext, bool aConnected);
85 
94  typedef void (*ReceiveHandler)(void *aContext, uint8_t *aBuf, uint16_t aLength);
95 
105  typedef otError(*SendHandler)(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType);
106 
119  otError Start(bool aClient, ConnectedHandler aConnectedHandler, ReceiveHandler aReceiveHandler,
120  SendHandler aSendHandler, void *aContext);
121 
128  otError Stop(void);
129 
136  bool IsStarted(void);
137 
147  otError SetPsk(const uint8_t *aPsk, uint8_t aPskLength);
148 
158  otError SetClientId(const uint8_t *aClientId, uint8_t aLength);
159 
167  bool IsConnected(void);
168 
179  otError Send(Message &aMessage, uint16_t aLength);
180 
191  otError Receive(Message &aMessage, uint16_t aOffset, uint16_t aLength);
192 
200  void SetDefaultMessageSubType(uint8_t aMessageSubType) { mMessageDefaultSubType = aMessageSubType; }
201 
207 
208 private:
209  static otError MapError(int rval);
210 
211  static void HandleMbedtlsDebug(void *ctx, int level, const char *file, int line, const char *str);
212 
213  static int HandleMbedtlsGetTimer(void *aContext);
214  int HandleMbedtlsGetTimer(void);
215 
216  static void HandleMbedtlsSetTimer(void *aContext, uint32_t aIntermediate, uint32_t aFinish);
217  void HandleMbedtlsSetTimer(uint32_t aIntermediate, uint32_t aFinish);
218 
219  static int HandleMbedtlsReceive(void *aContext, unsigned char *aBuf, size_t aLength);
220  int HandleMbedtlsReceive(unsigned char *aBuf, size_t aLength);
221 
222  static int HandleMbedtlsTransmit(void *aContext, const unsigned char *aBuf, size_t aLength);
223  int HandleMbedtlsTransmit(const unsigned char *aBuf, size_t aLength);
224 
225  static int HandleMbedtlsExportKeys(void *aContext, const unsigned char *aMasterSecret,
226  const unsigned char *aKeyBlock,
227  size_t aMacLength, size_t aKeyLength, size_t aIvLength);
228  int HandleMbedtlsExportKeys(const unsigned char *aMasterSecret, const unsigned char *aKeyBlock,
229  size_t aMacLength, size_t aKeyLength, size_t aIvLength);
230 
231  static void HandleTimer(Timer &aTimer);
232  void HandleTimer(void);
233 
234  void Close(void);
235  void Process(void);
236 
237  static Dtls &GetOwner(const Context &aContext);
238 
239  uint8_t mPsk[kPskMaxLength];
240  uint8_t mPskLength;
241 
242  mbedtls_entropy_context mEntropy;
243  mbedtls_ctr_drbg_context mCtrDrbg;
244  mbedtls_ssl_context mSsl;
245  mbedtls_ssl_config mConf;
246  mbedtls_ssl_cookie_ctx mCookieCtx;
247  bool mStarted;
248 
249  TimerMilli mTimer;
250  uint32_t mTimerIntermediate;
251  bool mTimerSet;
252 
253  Message *mReceiveMessage;
254  uint16_t mReceiveOffset;
255  uint16_t mReceiveLength;
256 
257  ConnectedHandler mConnectedHandler;
258  ReceiveHandler mReceiveHandler;
259  SendHandler mSendHandler;
260  void *mContext;
261  bool mClient;
262 
263  uint8_t mMessageSubType;
264  uint8_t mMessageDefaultSubType;
265 };
266 
267 } // namespace MeshCoP
268 } // namespace ot
269 
270 #endif // DTLS_HPP_
ProvisioningUrlTlv mProvisioningUrl
The provisioning URL is placed here so that both the Commissioner and Joiner can share the same objec...
Definition: dtls.hpp:206
This type represents all the static / global variables used by OpenThread allocated in one place...
Definition: openthread-instance.h:59
void(* ReceiveHandler)(void *aContext, uint8_t *aBuf, uint16_t aLength)
This function pointer is called when data is received from the DTLS session.
Definition: dtls.hpp:94
Definition: cli.cpp:90
This file defines the types and structures used in the OpenThread library API.
otError Send(Message &aMessage, uint16_t aLength)
This method sends data within the DTLS session.
void SetDefaultMessageSubType(uint8_t aMessageSubType)
This method sets the default message sub-type that will be used for all messages without defined sub-...
Definition: dtls.hpp:200
This file includes definitions for locator class for OpenThread objects.
otError Receive(Message &aMessage, uint16_t aOffset, uint16_t aLength)
This method provides a received DTLS message to the DTLS object.
otError(* SendHandler)(void *aContext, const uint8_t *aBuf, uint16_t aLength, uint8_t aMessageSubType)
This function pointer is called when data is ready to transmit for the DTLS session.
Definition: dtls.hpp:105
Definition: dtls.hpp:60
This class implements a timer.
Definition: timer.hpp:69
This class implements the millisecond timer.
Definition: timer.hpp:145
This file includes definitions for performing SHA-256 computations.
This file includes definitions for generating and processing MeshCoP TLVs.
otError SetClientId(const uint8_t *aClientId, uint8_t aLength)
This method sets the Client ID used for generating the Hello Cookie.
void(* ConnectedHandler)(void *aContext, bool aConnected)
This function pointer is called when a connection is established or torn down.
Definition: dtls.hpp:84
This class implements definitions for maintaining a pointer to arbitrary context information.
Definition: context.hpp:61
bool IsConnected(void)
This method indicates whether or not the DTLS session is connected.
This class represents a message.
Definition: message.hpp:195
otError SetPsk(const uint8_t *aPsk, uint8_t aPskLength)
This method sets the PSK.
otError Stop(void)
This method stops the DTLS service.
This file includes definitions for the message buffer pool and message buffers.
bool IsStarted(void)
This method indicates whether or not the DTLS service is active.
This class implements locator for otInstance object.
Definition: locator.hpp:63
This file includes definitions for the multiplexed timer service.
Dtls(otInstance &aInstance)
This constructor initializes the DTLS object.
otError Start(bool aClient, ConnectedHandler aConnectedHandler, ReceiveHandler aReceiveHandler, SendHandler aSendHandler, void *aContext)
This method starts the DTLS service.
otError
This enumeration represents error codes used throughout OpenThread.
Definition: types.h:107
This file includes compile-time configuration constants for OpenThread.
This class implements Provisioning URL TLV generation and parsing.
Definition: meshcop_tlvs.hpp:1501