1
0

dtls_al.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. /**
  35. * DATE AUTHOR INSTRUCTION
  36. * 2019-10-17 19:50 zhangqianfu The first version
  37. *
  38. */
  39. #ifndef LITEOS_LAB_IOT_LINK_NETWORK_DTLS_DTLS_AL_DTLS_AL_H_
  40. #define LITEOS_LAB_IOT_LINK_NETWORK_DTLS_DTLS_AL_DTLS_AL_H_
  41. #include <stdint.h>
  42. #include <stddef.h>
  43. typedef enum
  44. {
  45. EN_DTLS_AL_ERR_OK = 0,
  46. EN_DTLS_AL_ERR_PARA,
  47. EN_DTLS_AL_ERR_SYS,
  48. EN_DTLS_AL_ERR_SYSMEM,
  49. EN_DTLS_AL_ERR_NOCONFIG,
  50. EN_DTLS_AL_ERR_NETWORK,
  51. EN_DTLS_AL_ERR_SERVERCERTPARSE,
  52. EN_DTLS_AL_ERR_CLIENTCERTPARSE,
  53. EN_DTLS_AL_ERR_CLIENTPKPARSE,
  54. }en_dtls_al_err_t;
  55. /** @brief this enum all the transport encode we support now*/
  56. typedef enum
  57. {
  58. EN_DTLS_AL_SECURITY_TYPE_NONE = 0, ///< no encode
  59. EN_DTLS_AL_SECURITY_TYPE_PSK, ///< use the psk mode in transport layer
  60. EN_DTLS_AL_SECURITY_TYPE_CERT, ///< use the ca mode in transport layer,only check the server
  61. }en_dtls_al_security_type_t;
  62. /** @brief this data defines for the psk mode*/
  63. typedef struct
  64. {
  65. uint8_t *psk_id; ///< the psk id
  66. uint8_t *psk_key; ///< the psk key
  67. int psk_id_len; ///< the psk id length
  68. int psk_key_len; ///< the psk key length
  69. }dtls_al_security_psk_t;
  70. /** @brief this data defines for the cas mode:only check the server */
  71. typedef struct
  72. {
  73. uint8_t *server_ca;
  74. uint8_t *client_ca;
  75. uint8_t *client_pk;
  76. uint8_t *client_pk_pwd;
  77. int server_ca_len;
  78. int client_ca_len;
  79. int client_pk_len;
  80. int client_pk_pwd_len;
  81. char *server_name;
  82. }dtls_al_security_cert_t;
  83. /** @brief this data defines for the encode parameter for the connect */
  84. typedef struct
  85. {
  86. en_dtls_al_security_type_t type; ///< which security type of the data
  87. union
  88. {
  89. dtls_al_security_psk_t psk; ///< psk data if the type is EN_DTSL_SECURITY_TYPE_PSK
  90. dtls_al_security_cert_t cert; ///< cert data if the type is EN_DTSL_SECURITY_TYPE_CERT
  91. }u;
  92. }dtls_al_security_t;
  93. typedef struct
  94. {
  95. int istcp;
  96. int isclient;
  97. dtls_al_security_t security;
  98. }dtls_al_para_t;
  99. en_dtls_al_err_t dtls_al_new(dtls_al_para_t *para,void **handle);
  100. int dtls_al_connect(void *handle,const char *ip, const char *port, int timeout );
  101. int dtls_al_write(void *handle, uint8_t *msg, size_t len, int timeout );
  102. int dtls_al_read(void *handle,uint8_t *buf, size_t len,int timeout );
  103. en_dtls_al_err_t dtls_al_destroy(void *handle);
  104. typedef en_dtls_al_err_t (*fn_dtls_al_new)(dtls_al_para_t *para,void **handle);
  105. typedef int (*fn_dtls_al_connect)(void *handle,const char *server_ip, const char *server_port,int timeout);
  106. typedef int (*fn_dtls_al_write)(void *handle,uint8_t *msg, size_t len, int timeout);
  107. typedef int (*fn_dtls_al_read)(void *handle, uint8_t *buf, size_t len, int timeout);
  108. typedef en_dtls_al_err_t (*fn_dtls_al_destroy)(void *handle);
  109. typedef struct
  110. {
  111. fn_dtls_al_new io_new;
  112. fn_dtls_al_connect io_connect;
  113. fn_dtls_al_write io_write;
  114. fn_dtls_al_read io_read;
  115. fn_dtls_al_destroy io_destroy;
  116. }dtls_al_io_t;
  117. typedef struct
  118. {
  119. const char *name;
  120. dtls_al_io_t io;
  121. }dtls_al_t;
  122. int dtls_al_install(const dtls_al_t *dtls);
  123. int dtls_al_uninstall(const char*name);
  124. ///< this function should implemented by the developer of the tls
  125. int dtls_imp_init(void);
  126. int dtls_al_init(void) ; ///< this function will call dtls_imp_init()
  127. #endif /* LITEOS_LAB_IOT_LINK_NETWORK_DTLS_DTLS_AL_DTLS_AL_H_ */