ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

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-2016 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 <stddef.h>
00022 #include <inttypes.h>
00023 #include <stdbool.h>
00024 
00025 #ifdef NS_USE_EXTERNAL_MBED_TLS
00026 #include "mbedtls/ssl.h"
00027 #ifdef MBEDTLS_SSL_TLS_C
00028 #define COAP_SECURITY_AVAILABLE
00029 #endif
00030 #endif
00031 
00032 #define COOKIE_SIMPLE_LEN 8
00033 typedef struct simple_cookie {
00034     unsigned char value[COOKIE_SIMPLE_LEN];
00035     size_t        len;
00036 } simple_cookie_t;
00037 
00038 #define KEY_BLOCK_LEN 40
00039 typedef struct key_block {
00040     unsigned char value[KEY_BLOCK_LEN];
00041 } key_block_t;
00042 
00043 typedef int send_cb(int8_t socket_id, void *handle, const void *buf, size_t);
00044 typedef int receive_cb(int8_t socket_id, unsigned char *, size_t);
00045 typedef void start_timer_cb(int8_t timer_id, uint32_t min, uint32_t fin);
00046 typedef int timer_status_cb(int8_t timer_id);
00047 
00048 #define DTLS_HANDSHAKE_TIMEOUT_MIN 25000
00049 #define DTLS_HANDSHAKE_TIMEOUT_MAX 201000
00050 
00051 typedef enum {
00052     DTLS = 0,
00053     TLS = 1
00054 }SecureSocketMode;
00055 
00056 typedef enum {
00057     Certificate,
00058     PSK,
00059     ECJPAKE
00060 }SecureConnectionMode;
00061 
00062 typedef struct {
00063     unsigned char *_server_cert;
00064     uint8_t _server_cert_len;
00065     unsigned char *_pub_cert_or_identifier;
00066     uint8_t _pub_len;
00067     unsigned char *_priv;
00068     uint8_t _priv_len;
00069 } coap_security_keys_t;
00070 
00071 typedef struct coap_security_s coap_security_t;
00072 
00073 #ifdef COAP_SECURITY_AVAILABLE
00074 
00075 coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, void *handle,
00076                                           SecureConnectionMode mode,
00077                                           send_cb *send_cb,
00078                                           receive_cb *receive_cb,
00079                                           start_timer_cb *start_timer_cb,
00080                                           timer_status_cb *timer_status_cb);
00081 
00082 void coap_security_destroy(coap_security_t *sec);
00083 
00084 int coap_security_handler_connect(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys);
00085 
00086 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);
00087 
00088 int coap_security_handler_continue_connecting(coap_security_t *sec);
00089 
00090 int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len);
00091 
00092 int coap_security_send_close_alert(coap_security_t *sec);
00093 
00094 int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len);
00095 
00096 bool coap_security_handler_is_started(const coap_security_t *sec);
00097 
00098 const void *coap_security_handler_keyblock(const coap_security_t *sec);
00099 
00100 #else
00101 
00102 /* Dummy definitions, including needed error codes */
00103 #define MBEDTLS_ERR_SSL_TIMEOUT (-1)
00104 #define MBEDTLS_ERR_SSL_WANT_READ (-2)
00105 #define MBEDTLS_ERR_SSL_WANT_WRITE (-3)
00106 #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE (-4)
00107 
00108 #define coap_security_create(socket_id, timer_id, handle, \
00109                              mode, send_cb, receive_cb, start_timer_cb, timer_status_cb) ((coap_security_t *) 0)
00110 #define coap_security_destroy(sec) ((void) 0)
00111 #define coap_security_handler_connect(sec, is_server, sock_mode, keys) (-1)
00112 #define coap_security_handler_connect_non_blocking(sec, is_server, sock_mode, keys, timeout_min, timeout_max) (-1)
00113 #define coap_security_handler_continue_connecting(sec) (-1)
00114 #define coap_security_handler_send_message(sec, message, len) (-1)
00115 #define coap_security_send_close_alert(sec) (-1)
00116 #define coap_security_handler_read(sec, buffer, len) (-1)
00117 #define coap_security_handler_is_started(sec) false
00118 #define coap_security_handler_keyblock(sec) ((void *) 0)
00119 
00120 #endif /* COAP_SECURITY_AVAILABLE */
00121 
00122 #endif