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