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 contains definitions for a simple CLI CoAP Secure server and client. 32 */ 33 34 #ifndef CLI_COAP_SECURE_HPP_ 35 #define CLI_COAP_SECURE_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE 40 41 #include <mbedtls/ssl.h> 42 43 #include <openthread/coap_secure.h> 44 45 #include "cli/cli_utils.hpp" 46 47 #ifndef CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER 48 #define CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER 0 49 #endif 50 51 namespace ot { 52 namespace Cli { 53 54 /** 55 * Implements the CLI CoAP Secure server and client. 56 */ 57 class CoapSecure : private Utils 58 { 59 public: 60 /** 61 * Constructor 62 * 63 * @param[in] aInstance The OpenThread Instance. 64 * @param[in] aOutputImplementer An `OutputImplementer`. 65 */ 66 CoapSecure(otInstance *aInstance, OutputImplementer &aOutputImplementer); 67 68 /** 69 * Processes a CLI sub-command. 70 * 71 * @param[in] aArgs An array of command line arguments. 72 * 73 * @retval OT_ERROR_NONE Successfully executed the CLI command. 74 * @retval OT_ERROR_PENDING The CLI command was successfully started but final result is pending. 75 * @retval OT_ERROR_INVALID_COMMAND Invalid or unknown CLI command. 76 * @retval OT_ERROR_INVALID_ARGS Invalid arguments. 77 * @retval ... Error during execution of the CLI command. 78 */ 79 otError Process(Arg aArgs[]); 80 81 private: 82 static constexpr uint16_t kMaxUriLength = 32; 83 static constexpr uint16_t kMaxBufferSize = 16; 84 static constexpr uint8_t kPskMaxLength = 32; 85 static constexpr uint8_t kPskIdMaxLength = 32; 86 87 using Command = CommandEntry<CoapSecure>; 88 89 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE 90 enum BlockType : uint8_t{ 91 kBlockType1, 92 kBlockType2, 93 }; 94 #endif 95 96 void PrintPayload(otMessage *aMessage); 97 98 template <CommandId kCommandId> otError Process(Arg aArgs[]); 99 100 otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode); 101 otError ProcessIsRequest(Arg aArgs[], bool (*IsChecker)(otInstance *)); 102 103 void Stop(void); 104 105 static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); 106 void HandleRequest(otMessage *aMessage, const otMessageInfo *aMessageInfo); 107 108 static void HandleResponse(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError); 109 void HandleResponse(otMessage *aMessage, const otMessageInfo *aMessageInfo, otError aError); 110 111 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE 112 113 static otError BlockwiseReceiveHook(void *aContext, 114 const uint8_t *aBlock, 115 uint32_t aPosition, 116 uint16_t aBlockLength, 117 bool aMore, 118 uint32_t aTotalLength); 119 otError BlockwiseReceiveHook(const uint8_t *aBlock, 120 uint32_t aPosition, 121 uint16_t aBlockLength, 122 bool aMore, 123 uint32_t aTotalLength); 124 static otError BlockwiseTransmitHook(void *aContext, 125 uint8_t *aBlock, 126 uint32_t aPosition, 127 uint16_t *aBlockLength, 128 bool *aMore); 129 otError BlockwiseTransmitHook(uint8_t *aBlock, uint32_t aPosition, uint16_t *aBlockLength, bool *aMore); 130 #endif 131 132 #if CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER 133 static void DefaultHandler(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); 134 void DefaultHandler(otMessage *aMessage, const otMessageInfo *aMessageInfo); 135 #endif // CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER 136 137 static void HandleConnectEvent(otCoapSecureConnectEvent aEvent, void *aContext); 138 void HandleConnectEvent(otCoapSecureConnectEvent aEvent); 139 140 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE 141 otCoapBlockwiseResource mResource; 142 #else 143 otCoapResource mResource; 144 #endif 145 char mUriPath[kMaxUriLength]; 146 char mResourceContent[kMaxBufferSize]; 147 148 bool mShutdownFlag; 149 bool mUseCertificate; 150 uint8_t mPsk[kPskMaxLength]; 151 uint8_t mPskLength; 152 uint8_t mPskId[kPskIdMaxLength]; 153 uint8_t mPskIdLength; 154 #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE 155 uint32_t mBlockCount; 156 #endif 157 }; 158 159 } // namespace Cli 160 } // namespace ot 161 162 #endif // OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE 163 164 #endif // CLI_COAP_SECURE_HPP_ 165