Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers coap_security_handler.h Source File

coap_security_handler.h

00001 /*
00002  * Copyright (c) 2015-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef __COAP_SECURITY_HANDLER_H__
00018 #define __COAP_SECURITY_HANDLER_H__
00019 
00020 #include "ns_types.h"
00021 
00022 #ifdef NS_USE_EXTERNAL_MBED_TLS
00023 #if !defined(MBEDTLS_CONFIG_FILE)
00024 #include "mbedtls/config.h"
00025 #else
00026 #include MBEDTLS_CONFIG_FILE
00027 #endif
00028 
00029 #if defined(MBEDTLS_SSL_TLS_C)
00030 #include "mbedtls/ssl.h"
00031 #define COAP_SECURITY_AVAILABLE
00032 #endif
00033 
00034 #endif /* NS_USE_EXTERNAL_MBED_TLS */
00035 
00036 #define COOKIE_SIMPLE_LEN 8
00037 typedef struct simple_cookie {
00038     unsigned char value[COOKIE_SIMPLE_LEN];
00039     size_t        len;
00040 } simple_cookie_t;
00041 
00042 #define KEY_BLOCK_LEN 40
00043 typedef struct key_block {
00044     unsigned char value[KEY_BLOCK_LEN];
00045 } key_block_t;
00046 
00047 typedef int send_cb(int8_t socket_id, void *handle, const void *buf, size_t);
00048 typedef int receive_cb(int8_t socket_id, unsigned char *, size_t);
00049 typedef void start_timer_cb(int8_t timer_id, uint32_t min, uint32_t fin);
00050 typedef int timer_status_cb(int8_t timer_id);
00051 
00052 #define DTLS_HANDSHAKE_TIMEOUT_MIN 25000
00053 #define DTLS_HANDSHAKE_TIMEOUT_MAX 201000
00054 
00055 typedef enum {
00056     DTLS = 0,
00057     TLS = 1
00058 }SecureSocketMode;
00059 
00060 typedef enum {
00061     CERTIFICATE,
00062     PSK,
00063     ECJPAKE
00064 }SecureConnectionMode;
00065 
00066 typedef struct {
00067     SecureConnectionMode mode;
00068     /* Certificate pointers, not owned */
00069     const unsigned char *_cert;
00070     uint16_t _cert_len;
00071     const unsigned char *_priv_key;
00072     uint8_t _priv_key_len;
00073     /* Secure key pointer, owned */
00074     unsigned char *_key;
00075     uint8_t _key_len;
00076 } coap_security_keys_t;
00077 
00078 typedef struct coap_security_s coap_security_t;
00079 
00080 #ifdef COAP_SECURITY_AVAILABLE
00081 
00082 coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle,
00083                                           SecureConnectionMode mode,
00084                                           send_cb *send_cb,
00085                                           receive_cb *receive_cb,
00086                                           start_timer_cb *start_timer_cb,
00087                                           timer_status_cb *timer_status_cb);
00088 
00089 void coap_security_destroy(coap_security_t *sec);
00090 
00091 int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys, uint32_t timeout_min, uint32_t timeout_max);
00092 
00093 int coap_security_handler_continue_connecting(coap_security_t *sec);
00094 
00095 int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len);
00096 
00097 int coap_security_send_close_alert(coap_security_t *sec);
00098 
00099 int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len);
00100 
00101 bool coap_security_handler_is_started(const coap_security_t *sec);
00102 
00103 const void *coap_security_handler_keyblock(const coap_security_t *sec);
00104 
00105 #else
00106 
00107 NS_DUMMY_DEFINITIONS_OK
00108 
00109 /* Dummy definitions, including needed error codes */
00110 #ifndef MBEDTLS_ERR_SSL_TIMEOUT
00111 #define MBEDTLS_ERR_SSL_TIMEOUT (-1)
00112 #endif
00113 
00114 #ifndef MBEDTLS_ERR_SSL_WANT_READ
00115 #define MBEDTLS_ERR_SSL_WANT_READ (-2)
00116 #endif
00117 
00118 #ifndef MBEDTLS_ERR_SSL_WANT_WRITE
00119 #define MBEDTLS_ERR_SSL_WANT_WRITE (-3)
00120 #endif
00121 
00122 #ifndef MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE
00123 #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE (-4)
00124 #endif
00125 
00126 #define coap_security_create(socket_id, timer_id, handle, \
00127                              mode, send_cb, receive_cb, start_timer_cb, timer_status_cb) ((coap_security_t *) 0)
00128 #define coap_security_destroy(sec) ((void) 0)
00129 #define coap_security_handler_connect_non_blocking(sec, is_server, sock_mode, keys, timeout_min, timeout_max) (-1)
00130 #define coap_security_handler_continue_connecting(sec) (-1)
00131 #define coap_security_handler_send_message(sec, message, len) (-1)
00132 #define coap_security_send_close_alert(sec) (-1)
00133 #define coap_security_handler_read(sec, buffer, len) (-1)
00134 #define coap_security_handler_is_started(sec) false
00135 #define coap_security_handler_keyblock(sec) ((void *) 0)
00136 
00137 #endif /* COAP_SECURITY_AVAILABLE */
00138 
00139 #endif