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