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
29 #include "coap_secure.hpp"
30
31 #if OPENTHREAD_CONFIG_DTLS_ENABLE
32
33 #include "common/instance.hpp"
34 #include "common/locator_getters.hpp"
35 #include "common/log.hpp"
36 #include "common/new.hpp"
37 #include "meshcop/dtls.hpp"
38 #include "thread/thread_netif.hpp"
39
40 /**
41 * @file
42 * This file implements the secure CoAP agent.
43 */
44
45 namespace ot {
46 namespace Coap {
47
48 RegisterLogModule("CoapSecure");
49
CoapSecure(Instance & aInstance,bool aLayerTwoSecurity)50 CoapSecure::CoapSecure(Instance &aInstance, bool aLayerTwoSecurity)
51 : CoapBase(aInstance, &CoapSecure::Send)
52 , mDtls(aInstance, aLayerTwoSecurity)
53 , mConnectedCallback(nullptr)
54 , mConnectedContext(nullptr)
55 , mTransmitTask(aInstance, CoapSecure::HandleTransmit, this)
56 {
57 }
58
Start(uint16_t aPort)59 Error CoapSecure::Start(uint16_t aPort)
60 {
61 Error error = kErrorNone;
62
63 mConnectedCallback = nullptr;
64 mConnectedContext = nullptr;
65
66 SuccessOrExit(error = mDtls.Open(&CoapSecure::HandleDtlsReceive, &CoapSecure::HandleDtlsConnected, this));
67 SuccessOrExit(error = mDtls.Bind(aPort));
68
69 exit:
70 return error;
71 }
72
Start(MeshCoP::Dtls::TransportCallback aCallback,void * aContext)73 Error CoapSecure::Start(MeshCoP::Dtls::TransportCallback aCallback, void *aContext)
74 {
75 Error error = kErrorNone;
76
77 mConnectedCallback = nullptr;
78 mConnectedContext = nullptr;
79
80 SuccessOrExit(error = mDtls.Open(&CoapSecure::HandleDtlsReceive, &CoapSecure::HandleDtlsConnected, this));
81 SuccessOrExit(error = mDtls.Bind(aCallback, aContext));
82
83 exit:
84 return error;
85 }
86
Stop(void)87 void CoapSecure::Stop(void)
88 {
89 mDtls.Close();
90
91 mTransmitQueue.DequeueAndFreeAll();
92 ClearRequestsAndResponses();
93 }
94
Connect(const Ip6::SockAddr & aSockAddr,ConnectedCallback aCallback,void * aContext)95 Error CoapSecure::Connect(const Ip6::SockAddr &aSockAddr, ConnectedCallback aCallback, void *aContext)
96 {
97 mConnectedCallback = aCallback;
98 mConnectedContext = aContext;
99
100 return mDtls.Connect(aSockAddr);
101 }
102
SetPsk(const MeshCoP::JoinerPskd & aPskd)103 void CoapSecure::SetPsk(const MeshCoP::JoinerPskd &aPskd)
104 {
105 static_assert(static_cast<uint16_t>(MeshCoP::JoinerPskd::kMaxLength) <=
106 static_cast<uint16_t>(MeshCoP::Dtls::kPskMaxLength),
107 "The maximum length of DTLS PSK is smaller than joiner PSKd");
108
109 SuccessOrAssert(mDtls.SetPsk(reinterpret_cast<const uint8_t *>(aPskd.GetAsCString()), aPskd.GetLength()));
110 }
111
112 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
SendMessage(Message & aMessage,ResponseHandler aHandler,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook,otCoapBlockwiseReceiveHook aReceiveHook)113 Error CoapSecure::SendMessage(Message & aMessage,
114 ResponseHandler aHandler,
115 void * aContext,
116 otCoapBlockwiseTransmitHook aTransmitHook,
117 otCoapBlockwiseReceiveHook aReceiveHook)
118 {
119 Error error = kErrorNone;
120
121 VerifyOrExit(IsConnected(), error = kErrorInvalidState);
122
123 error = CoapBase::SendMessage(aMessage, mDtls.GetMessageInfo(), TxParameters::GetDefault(), aHandler, aContext,
124 aTransmitHook, aReceiveHook);
125
126 exit:
127 return error;
128 }
129
SendMessage(Message & aMessage,const Ip6::MessageInfo & aMessageInfo,ResponseHandler aHandler,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook,otCoapBlockwiseReceiveHook aReceiveHook)130 Error CoapSecure::SendMessage(Message & aMessage,
131 const Ip6::MessageInfo & aMessageInfo,
132 ResponseHandler aHandler,
133 void * aContext,
134 otCoapBlockwiseTransmitHook aTransmitHook,
135 otCoapBlockwiseReceiveHook aReceiveHook)
136 {
137 return CoapBase::SendMessage(aMessage, aMessageInfo, TxParameters::GetDefault(), aHandler, aContext, aTransmitHook,
138 aReceiveHook);
139 }
140 #else // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
SendMessage(Message & aMessage,ResponseHandler aHandler,void * aContext)141 Error CoapSecure::SendMessage(Message &aMessage, ResponseHandler aHandler, void *aContext)
142 {
143 Error error = kErrorNone;
144
145 VerifyOrExit(IsConnected(), error = kErrorInvalidState);
146
147 error = CoapBase::SendMessage(aMessage, mDtls.GetMessageInfo(), aHandler, aContext);
148
149 exit:
150 return error;
151 }
152
SendMessage(Message & aMessage,const Ip6::MessageInfo & aMessageInfo,ResponseHandler aHandler,void * aContext)153 Error CoapSecure::SendMessage(Message & aMessage,
154 const Ip6::MessageInfo &aMessageInfo,
155 ResponseHandler aHandler,
156 void * aContext)
157 {
158 return CoapBase::SendMessage(aMessage, aMessageInfo, aHandler, aContext);
159 }
160 #endif // OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
161
Send(ot::Message & aMessage,const Ip6::MessageInfo & aMessageInfo)162 Error CoapSecure::Send(ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
163 {
164 OT_UNUSED_VARIABLE(aMessageInfo);
165
166 mTransmitQueue.Enqueue(aMessage);
167 mTransmitTask.Post();
168
169 return kErrorNone;
170 }
171
HandleDtlsConnected(void * aContext,bool aConnected)172 void CoapSecure::HandleDtlsConnected(void *aContext, bool aConnected)
173 {
174 return static_cast<CoapSecure *>(aContext)->HandleDtlsConnected(aConnected);
175 }
176
HandleDtlsConnected(bool aConnected)177 void CoapSecure::HandleDtlsConnected(bool aConnected)
178 {
179 if (mConnectedCallback != nullptr)
180 {
181 mConnectedCallback(aConnected, mConnectedContext);
182 }
183 }
184
HandleDtlsReceive(void * aContext,uint8_t * aBuf,uint16_t aLength)185 void CoapSecure::HandleDtlsReceive(void *aContext, uint8_t *aBuf, uint16_t aLength)
186 {
187 return static_cast<CoapSecure *>(aContext)->HandleDtlsReceive(aBuf, aLength);
188 }
189
HandleDtlsReceive(uint8_t * aBuf,uint16_t aLength)190 void CoapSecure::HandleDtlsReceive(uint8_t *aBuf, uint16_t aLength)
191 {
192 ot::Message *message = nullptr;
193
194 VerifyOrExit((message = Get<MessagePool>().Allocate(Message::kTypeIp6, Message::GetHelpDataReserved())) != nullptr);
195 SuccessOrExit(message->AppendBytes(aBuf, aLength));
196
197 CoapBase::Receive(*message, mDtls.GetMessageInfo());
198
199 exit:
200 FreeMessage(message);
201 }
202
HandleTransmit(Tasklet & aTasklet)203 void CoapSecure::HandleTransmit(Tasklet &aTasklet)
204 {
205 static_cast<CoapSecure *>(static_cast<TaskletContext &>(aTasklet).GetContext())->HandleTransmit();
206 }
207
HandleTransmit(void)208 void CoapSecure::HandleTransmit(void)
209 {
210 Error error = kErrorNone;
211 ot::Message *message = mTransmitQueue.GetHead();
212
213 VerifyOrExit(message != nullptr);
214 mTransmitQueue.Dequeue(*message);
215
216 if (mTransmitQueue.GetHead() != nullptr)
217 {
218 mTransmitTask.Post();
219 }
220
221 SuccessOrExit(error = mDtls.Send(*message, message->GetLength()));
222
223 exit:
224 if (error != kErrorNone)
225 {
226 LogNote("Transmit: %s", ErrorToString(error));
227 message->Free();
228 }
229 else
230 {
231 LogDebg("Transmit: %s", ErrorToString(error));
232 }
233 }
234
235 } // namespace Coap
236 } // namespace ot
237
238 #endif // OPENTHREAD_CONFIG_DTLS_ENABLE
239