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