• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018, 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 /**
30  * @file
31  *   This file implements the OpenThread CoAP Secure API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
37 
38 #include "instance/instance.hpp"
39 
40 using namespace ot;
41 
otCoapSecureStart(otInstance * aInstance,uint16_t aPort)42 otError otCoapSecureStart(otInstance *aInstance, uint16_t aPort)
43 {
44     otError error;
45 
46     SuccessOrExit(error = AsCoreType(aInstance).GetApplicationCoapSecure().Open());
47     error = AsCoreType(aInstance).GetApplicationCoapSecure().Bind(aPort);
48 
49 exit:
50     return error;
51 }
52 
otCoapSecureStartWithMaxConnAttempts(otInstance * aInstance,uint16_t aPort,uint16_t aMaxAttempts,otCoapSecureAutoStopCallback aCallback,void * aContext)53 otError otCoapSecureStartWithMaxConnAttempts(otInstance                  *aInstance,
54                                              uint16_t                     aPort,
55                                              uint16_t                     aMaxAttempts,
56                                              otCoapSecureAutoStopCallback aCallback,
57                                              void                        *aContext)
58 {
59     Error error = kErrorAlready;
60 
61     SuccessOrExit(
62         AsCoreType(aInstance).GetApplicationCoapSecure().SetMaxConnectionAttempts(aMaxAttempts, aCallback, aContext));
63     error = otCoapSecureStart(aInstance, aPort);
64 
65 exit:
66     return error;
67 }
68 
69 #ifdef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
otCoapSecureSetCertificate(otInstance * aInstance,const uint8_t * aX509Cert,uint32_t aX509Length,const uint8_t * aPrivateKey,uint32_t aPrivateKeyLength)70 void otCoapSecureSetCertificate(otInstance    *aInstance,
71                                 const uint8_t *aX509Cert,
72                                 uint32_t       aX509Length,
73                                 const uint8_t *aPrivateKey,
74                                 uint32_t       aPrivateKeyLength)
75 {
76     AsCoreType(aInstance).GetApplicationCoapSecure().SetCertificate(aX509Cert, aX509Length, aPrivateKey,
77                                                                     aPrivateKeyLength);
78 }
79 
otCoapSecureSetCaCertificateChain(otInstance * aInstance,const uint8_t * aX509CaCertificateChain,uint32_t aX509CaCertChainLength)80 void otCoapSecureSetCaCertificateChain(otInstance    *aInstance,
81                                        const uint8_t *aX509CaCertificateChain,
82                                        uint32_t       aX509CaCertChainLength)
83 {
84     AsCoreType(aInstance).GetApplicationCoapSecure().SetCaCertificateChain(aX509CaCertificateChain,
85                                                                            aX509CaCertChainLength);
86 }
87 #endif // MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
88 
89 #ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
otCoapSecureSetPsk(otInstance * aInstance,const uint8_t * aPsk,uint16_t aPskLength,const uint8_t * aPskIdentity,uint16_t aPskIdLength)90 void otCoapSecureSetPsk(otInstance    *aInstance,
91                         const uint8_t *aPsk,
92                         uint16_t       aPskLength,
93                         const uint8_t *aPskIdentity,
94                         uint16_t       aPskIdLength)
95 {
96     AssertPointerIsNotNull(aPsk);
97     AssertPointerIsNotNull(aPskIdentity);
98 
99     AsCoreType(aInstance).GetApplicationCoapSecure().SetPreSharedKey(aPsk, aPskLength, aPskIdentity, aPskIdLength);
100 }
101 #endif // MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
102 
103 #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
otCoapSecureGetPeerCertificateBase64(otInstance * aInstance,unsigned char * aPeerCert,size_t * aCertLength,size_t aCertBufferSize)104 otError otCoapSecureGetPeerCertificateBase64(otInstance    *aInstance,
105                                              unsigned char *aPeerCert,
106                                              size_t        *aCertLength,
107                                              size_t         aCertBufferSize)
108 {
109     AssertPointerIsNotNull(aPeerCert);
110 
111     return AsCoreType(aInstance).GetApplicationCoapSecure().GetPeerCertificateBase64(aPeerCert, aCertLength,
112                                                                                      aCertBufferSize);
113 }
114 #endif // defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
115 
otCoapSecureSetSslAuthMode(otInstance * aInstance,bool aVerifyPeerCertificate)116 void otCoapSecureSetSslAuthMode(otInstance *aInstance, bool aVerifyPeerCertificate)
117 {
118     AsCoreType(aInstance).GetApplicationCoapSecure().SetSslAuthMode(aVerifyPeerCertificate);
119 }
120 
otCoapSecureConnect(otInstance * aInstance,const otSockAddr * aSockAddr,otHandleCoapSecureClientConnect aHandler,void * aContext)121 otError otCoapSecureConnect(otInstance                     *aInstance,
122                             const otSockAddr               *aSockAddr,
123                             otHandleCoapSecureClientConnect aHandler,
124                             void                           *aContext)
125 {
126     AsCoreType(aInstance).GetApplicationCoapSecure().SetConnectCallback(aHandler, aContext);
127 
128     return AsCoreType(aInstance).GetApplicationCoapSecure().Connect(AsCoreType(aSockAddr));
129 }
130 
otCoapSecureDisconnect(otInstance * aInstance)131 void otCoapSecureDisconnect(otInstance *aInstance) { AsCoreType(aInstance).GetApplicationCoapSecure().Disconnect(); }
132 
otCoapSecureIsConnected(otInstance * aInstance)133 bool otCoapSecureIsConnected(otInstance *aInstance)
134 {
135     return AsCoreType(aInstance).GetApplicationCoapSecure().IsConnected();
136 }
137 
otCoapSecureIsConnectionActive(otInstance * aInstance)138 bool otCoapSecureIsConnectionActive(otInstance *aInstance)
139 {
140     return AsCoreType(aInstance).GetApplicationCoapSecure().IsConnectionActive();
141 }
142 
otCoapSecureIsClosed(otInstance * aInstance)143 bool otCoapSecureIsClosed(otInstance *aInstance) { return AsCoreType(aInstance).GetApplicationCoapSecure().IsClosed(); }
144 
otCoapSecureStop(otInstance * aInstance)145 void otCoapSecureStop(otInstance *aInstance) { AsCoreType(aInstance).GetApplicationCoapSecure().Close(); }
146 
147 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSecureSendRequestBlockWise(otInstance * aInstance,otMessage * aMessage,otCoapResponseHandler aHandler,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook,otCoapBlockwiseReceiveHook aReceiveHook)148 otError otCoapSecureSendRequestBlockWise(otInstance                 *aInstance,
149                                          otMessage                  *aMessage,
150                                          otCoapResponseHandler       aHandler,
151                                          void                       *aContext,
152                                          otCoapBlockwiseTransmitHook aTransmitHook,
153                                          otCoapBlockwiseReceiveHook  aReceiveHook)
154 {
155     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(AsCoapMessage(aMessage), aHandler, aContext,
156                                                                         aTransmitHook, aReceiveHook);
157 }
158 #endif
159 
otCoapSecureSendRequest(otInstance * aInstance,otMessage * aMessage,otCoapResponseHandler aHandler,void * aContext)160 otError otCoapSecureSendRequest(otInstance           *aInstance,
161                                 otMessage            *aMessage,
162                                 otCoapResponseHandler aHandler,
163                                 void                 *aContext)
164 {
165     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(AsCoapMessage(aMessage), aHandler, aContext);
166 }
167 
168 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSecureAddBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)169 void otCoapSecureAddBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
170 {
171     AsCoreType(aInstance).GetApplicationCoapSecure().AddBlockWiseResource(AsCoreType(aResource));
172 }
173 
otCoapSecureRemoveBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)174 void otCoapSecureRemoveBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
175 {
176     AsCoreType(aInstance).GetApplicationCoapSecure().RemoveBlockWiseResource(AsCoreType(aResource));
177 }
178 #endif
179 
otCoapSecureAddResource(otInstance * aInstance,otCoapResource * aResource)180 void otCoapSecureAddResource(otInstance *aInstance, otCoapResource *aResource)
181 {
182     AsCoreType(aInstance).GetApplicationCoapSecure().AddResource(AsCoreType(aResource));
183 }
184 
otCoapSecureRemoveResource(otInstance * aInstance,otCoapResource * aResource)185 void otCoapSecureRemoveResource(otInstance *aInstance, otCoapResource *aResource)
186 {
187     AsCoreType(aInstance).GetApplicationCoapSecure().RemoveResource(AsCoreType(aResource));
188 }
189 
otCoapSecureSetClientConnectEventCallback(otInstance * aInstance,otHandleCoapSecureClientConnect aHandler,void * aContext)190 void otCoapSecureSetClientConnectEventCallback(otInstance                     *aInstance,
191                                                otHandleCoapSecureClientConnect aHandler,
192                                                void                           *aContext)
193 {
194     AsCoreType(aInstance).GetApplicationCoapSecure().SetConnectCallback(aHandler, aContext);
195 }
196 
otCoapSecureSetDefaultHandler(otInstance * aInstance,otCoapRequestHandler aHandler,void * aContext)197 void otCoapSecureSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
198 {
199     AsCoreType(aInstance).GetApplicationCoapSecure().SetDefaultHandler(aHandler, aContext);
200 }
201 
202 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSecureSendResponseBlockWise(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook)203 otError otCoapSecureSendResponseBlockWise(otInstance                 *aInstance,
204                                           otMessage                  *aMessage,
205                                           const otMessageInfo        *aMessageInfo,
206                                           void                       *aContext,
207                                           otCoapBlockwiseTransmitHook aTransmitHook)
208 {
209     OT_UNUSED_VARIABLE(aMessageInfo);
210 
211     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(AsCoapMessage(aMessage), nullptr, aContext,
212                                                                         aTransmitHook);
213 }
214 #endif
215 
otCoapSecureSendResponse(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo)216 otError otCoapSecureSendResponse(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo)
217 {
218     OT_UNUSED_VARIABLE(aMessageInfo);
219 
220     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(AsCoapMessage(aMessage));
221 }
222 
223 #endif // OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
224