• 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 <openthread/coap_secure.h>
39 #include <openthread/ip6.h>
40 
41 #include "coap/coap_message.hpp"
42 #include "coap/coap_secure.hpp"
43 #include "common/as_core_type.hpp"
44 #include "common/locator_getters.hpp"
45 
46 using namespace ot;
47 
otCoapSecureStart(otInstance * aInstance,uint16_t aPort)48 otError otCoapSecureStart(otInstance *aInstance, uint16_t aPort)
49 {
50     return AsCoreType(aInstance).GetApplicationCoapSecure().Start(aPort);
51 }
52 
53 #ifdef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
otCoapSecureSetCertificate(otInstance * aInstance,const uint8_t * aX509Cert,uint32_t aX509Length,const uint8_t * aPrivateKey,uint32_t aPrivateKeyLength)54 void otCoapSecureSetCertificate(otInstance *   aInstance,
55                                 const uint8_t *aX509Cert,
56                                 uint32_t       aX509Length,
57                                 const uint8_t *aPrivateKey,
58                                 uint32_t       aPrivateKeyLength)
59 {
60     OT_ASSERT(aX509Cert != nullptr && aX509Length != 0 && aPrivateKey != nullptr && aPrivateKeyLength != 0);
61 
62     AsCoreType(aInstance).GetApplicationCoapSecure().SetCertificate(aX509Cert, aX509Length, aPrivateKey,
63                                                                     aPrivateKeyLength);
64 }
65 
otCoapSecureSetCaCertificateChain(otInstance * aInstance,const uint8_t * aX509CaCertificateChain,uint32_t aX509CaCertChainLength)66 void otCoapSecureSetCaCertificateChain(otInstance *   aInstance,
67                                        const uint8_t *aX509CaCertificateChain,
68                                        uint32_t       aX509CaCertChainLength)
69 {
70     OT_ASSERT(aX509CaCertificateChain != nullptr && aX509CaCertChainLength != 0);
71 
72     AsCoreType(aInstance).GetApplicationCoapSecure().SetCaCertificateChain(aX509CaCertificateChain,
73                                                                            aX509CaCertChainLength);
74 }
75 #endif // MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
76 
77 #ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
otCoapSecureSetPsk(otInstance * aInstance,const uint8_t * aPsk,uint16_t aPskLength,const uint8_t * aPskIdentity,uint16_t aPskIdLength)78 void otCoapSecureSetPsk(otInstance *   aInstance,
79                         const uint8_t *aPsk,
80                         uint16_t       aPskLength,
81                         const uint8_t *aPskIdentity,
82                         uint16_t       aPskIdLength)
83 {
84     OT_ASSERT(aPsk != nullptr && aPskLength != 0 && aPskIdentity != nullptr && aPskIdLength != 0);
85 
86     AsCoreType(aInstance).GetApplicationCoapSecure().SetPreSharedKey(aPsk, aPskLength, aPskIdentity, aPskIdLength);
87 }
88 #endif // MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
89 
90 #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
otCoapSecureGetPeerCertificateBase64(otInstance * aInstance,unsigned char * aPeerCert,size_t * aCertLength,size_t aCertBufferSize)91 otError otCoapSecureGetPeerCertificateBase64(otInstance *   aInstance,
92                                              unsigned char *aPeerCert,
93                                              size_t *       aCertLength,
94                                              size_t         aCertBufferSize)
95 {
96     return AsCoreType(aInstance).GetApplicationCoapSecure().GetPeerCertificateBase64(aPeerCert, aCertLength,
97                                                                                      aCertBufferSize);
98 }
99 #endif // defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
100 
otCoapSecureSetSslAuthMode(otInstance * aInstance,bool aVerifyPeerCertificate)101 void otCoapSecureSetSslAuthMode(otInstance *aInstance, bool aVerifyPeerCertificate)
102 {
103     AsCoreType(aInstance).GetApplicationCoapSecure().SetSslAuthMode(aVerifyPeerCertificate);
104 }
105 
otCoapSecureConnect(otInstance * aInstance,const otSockAddr * aSockAddr,otHandleCoapSecureClientConnect aHandler,void * aContext)106 otError otCoapSecureConnect(otInstance *                    aInstance,
107                             const otSockAddr *              aSockAddr,
108                             otHandleCoapSecureClientConnect aHandler,
109                             void *                          aContext)
110 {
111     return AsCoreType(aInstance).GetApplicationCoapSecure().Connect(AsCoreType(aSockAddr), aHandler, aContext);
112 }
113 
otCoapSecureDisconnect(otInstance * aInstance)114 void otCoapSecureDisconnect(otInstance *aInstance)
115 {
116     AsCoreType(aInstance).GetApplicationCoapSecure().Disconnect();
117 }
118 
otCoapSecureIsConnected(otInstance * aInstance)119 bool otCoapSecureIsConnected(otInstance *aInstance)
120 {
121     return AsCoreType(aInstance).GetApplicationCoapSecure().IsConnected();
122 }
123 
otCoapSecureIsConnectionActive(otInstance * aInstance)124 bool otCoapSecureIsConnectionActive(otInstance *aInstance)
125 {
126     return AsCoreType(aInstance).GetApplicationCoapSecure().IsConnectionActive();
127 }
128 
otCoapSecureStop(otInstance * aInstance)129 void otCoapSecureStop(otInstance *aInstance)
130 {
131     AsCoreType(aInstance).GetApplicationCoapSecure().Stop();
132 }
133 
134 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSecureSendRequestBlockWise(otInstance * aInstance,otMessage * aMessage,otCoapResponseHandler aHandler,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook,otCoapBlockwiseReceiveHook aReceiveHook)135 otError otCoapSecureSendRequestBlockWise(otInstance *                aInstance,
136                                          otMessage *                 aMessage,
137                                          otCoapResponseHandler       aHandler,
138                                          void *                      aContext,
139                                          otCoapBlockwiseTransmitHook aTransmitHook,
140                                          otCoapBlockwiseReceiveHook  aReceiveHook)
141 {
142     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(AsCoapMessage(aMessage), aHandler, aContext,
143                                                                         aTransmitHook, aReceiveHook);
144 }
145 #endif
146 
otCoapSecureSendRequest(otInstance * aInstance,otMessage * aMessage,otCoapResponseHandler aHandler,void * aContext)147 otError otCoapSecureSendRequest(otInstance *          aInstance,
148                                 otMessage *           aMessage,
149                                 otCoapResponseHandler aHandler,
150                                 void *                aContext)
151 {
152     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(AsCoapMessage(aMessage), aHandler, aContext);
153 }
154 
155 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSecureAddBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)156 void otCoapSecureAddBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
157 {
158     AsCoreType(aInstance).GetApplicationCoapSecure().AddBlockWiseResource(AsCoreType(aResource));
159 }
160 
otCoapSecureRemoveBlockWiseResource(otInstance * aInstance,otCoapBlockwiseResource * aResource)161 void otCoapSecureRemoveBlockWiseResource(otInstance *aInstance, otCoapBlockwiseResource *aResource)
162 {
163     AsCoreType(aInstance).GetApplicationCoapSecure().RemoveBlockWiseResource(AsCoreType(aResource));
164 }
165 #endif
166 
otCoapSecureAddResource(otInstance * aInstance,otCoapResource * aResource)167 void otCoapSecureAddResource(otInstance *aInstance, otCoapResource *aResource)
168 {
169     AsCoreType(aInstance).GetApplicationCoapSecure().AddResource(AsCoreType(aResource));
170 }
171 
otCoapSecureRemoveResource(otInstance * aInstance,otCoapResource * aResource)172 void otCoapSecureRemoveResource(otInstance *aInstance, otCoapResource *aResource)
173 {
174     AsCoreType(aInstance).GetApplicationCoapSecure().RemoveResource(AsCoreType(aResource));
175 }
176 
otCoapSecureSetClientConnectedCallback(otInstance * aInstance,otHandleCoapSecureClientConnect aHandler,void * aContext)177 void otCoapSecureSetClientConnectedCallback(otInstance *                    aInstance,
178                                             otHandleCoapSecureClientConnect aHandler,
179                                             void *                          aContext)
180 {
181     AsCoreType(aInstance).GetApplicationCoapSecure().SetClientConnectedCallback(aHandler, aContext);
182 }
183 
otCoapSecureSetDefaultHandler(otInstance * aInstance,otCoapRequestHandler aHandler,void * aContext)184 void otCoapSecureSetDefaultHandler(otInstance *aInstance, otCoapRequestHandler aHandler, void *aContext)
185 {
186     AsCoreType(aInstance).GetApplicationCoapSecure().SetDefaultHandler(aHandler, aContext);
187 }
188 
189 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
otCoapSecureSendResponseBlockWise(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo,void * aContext,otCoapBlockwiseTransmitHook aTransmitHook)190 otError otCoapSecureSendResponseBlockWise(otInstance *                aInstance,
191                                           otMessage *                 aMessage,
192                                           const otMessageInfo *       aMessageInfo,
193                                           void *                      aContext,
194                                           otCoapBlockwiseTransmitHook aTransmitHook)
195 {
196     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(
197         AsCoapMessage(aMessage), AsCoreType(aMessageInfo), nullptr, aContext, aTransmitHook);
198 }
199 #endif
200 
otCoapSecureSendResponse(otInstance * aInstance,otMessage * aMessage,const otMessageInfo * aMessageInfo)201 otError otCoapSecureSendResponse(otInstance *aInstance, otMessage *aMessage, const otMessageInfo *aMessageInfo)
202 {
203     return AsCoreType(aInstance).GetApplicationCoapSecure().SendMessage(AsCoapMessage(aMessage),
204                                                                         AsCoreType(aMessageInfo));
205 }
206 
207 #endif // OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
208