joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

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 #include "mbedtls/platform.h"
00025 #include "mbedtls/ssl.h"
00026 #include "mbedtls/sha256.h"
00027 #include "mbedtls/entropy.h"
00028 #include "mbedtls/ctr_drbg.h"
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, uint8_t *address_ptr, uint16_t port, const unsigned char *, 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 {
00070     mbedtls_ssl_config          _conf;
00071     mbedtls_ssl_context         _ssl;
00072 
00073     mbedtls_ctr_drbg_context    _ctr_drbg;
00074     mbedtls_entropy_context     _entropy;
00075     bool                        _is_started;
00076     simple_cookie_t             _cookie;
00077     key_block_t                 _keyblk;
00078 
00079     SecureConnectionMode        _conn_mode;
00080 #if defined(MBEDTLS_X509_CRT_PARSE_C)
00081     mbedtls_x509_crt            _cacert;
00082     mbedtls_x509_crt            _owncert;
00083 #endif
00084     mbedtls_pk_context          _pkey;
00085 
00086     uint8_t                     _remote_address[16];
00087     uint16_t                    _remote_port;
00088 
00089     uint8_t                     _pw[64];
00090     uint8_t                     _pw_len;
00091 
00092     bool                        _is_blocking;
00093     int8_t                      _socket_id;
00094     int8_t                      _timer_id;
00095     send_cb                     *_send_cb;
00096     receive_cb                  *_receive_cb;
00097     start_timer_cb              *_start_timer_cb;
00098     timer_status_cb             *_timer_status_cb;
00099 
00100 } coap_security_t;
00101 
00102 coap_security_t *coap_security_create(int8_t socket_id, int8_t timer_id, uint8_t *address_ptr, uint16_t port,
00103                                           SecureConnectionMode mode,
00104                                           send_cb *send_cb,
00105                                           receive_cb *receive_cb,
00106                                           start_timer_cb *start_timer_cb,
00107                                           timer_status_cb *timer_status_cb);
00108 
00109 void coap_security_destroy(coap_security_t *sec);
00110 
00111 int coap_security_handler_connect(coap_security_t *sec, bool is_server, SecureSocketMode sock_mode, coap_security_keys_t keys);
00112 
00113 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);
00114 
00115 int coap_security_handler_continue_connecting(coap_security_t *sec);
00116 
00117 int coap_security_handler_send_message(coap_security_t *sec, unsigned char *message, size_t len);
00118 
00119 int coap_security_send_close_alert(coap_security_t *sec);
00120 
00121 int coap_security_handler_read(coap_security_t *sec, unsigned char* buffer, size_t len);
00122 
00123 #endif