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