glue.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. Copyright 2018-2023 Bruce A Henderson
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "mbedtls/net_sockets.h"
  14. #include "mbedtls/x509.h"
  15. #include "mbedtls/ctr_drbg.h"
  16. #include "mbedtls/entropy.h"
  17. #include "brl.mod/blitz.mod/blitz.h"
  18. mbedtls_net_context * bmx_mbedtls_net_init() {
  19. mbedtls_net_context * context = (mbedtls_net_context *)malloc(sizeof(mbedtls_net_context));
  20. mbedtls_net_init(context);
  21. return context;
  22. }
  23. void bmx_mbedtls_net_free(mbedtls_net_context * context) {
  24. mbedtls_net_free(context);
  25. }
  26. void bmx_mbedtls_net_delete(mbedtls_net_context * context) {
  27. free(context);
  28. }
  29. int bmx_mbedtls_net_bind(mbedtls_net_context * context, BBString * bindIp, BBString * port, int proto) {
  30. char * b = NULL;
  31. if (bindIp != &bbEmptyString) {
  32. b = bbStringToUTF8String(bindIp);
  33. }
  34. char * p = NULL;
  35. if (port != &bbEmptyString) {
  36. p = bbStringToUTF8String(port);
  37. }
  38. int res = mbedtls_net_bind(context, b, p, proto);
  39. bbMemFree(p);
  40. bbMemFree(b);
  41. return res;
  42. }
  43. int bmx_mbedtls_net_connect(mbedtls_net_context * context, BBString * host, BBString * port, int proto) {
  44. char * h = NULL;
  45. if (host != &bbEmptyString) {
  46. h = bbStringToUTF8String(host);
  47. }
  48. char * p = NULL;
  49. if (port != &bbEmptyString) {
  50. p = bbStringToUTF8String(port);
  51. }
  52. int res = mbedtls_net_connect(context, h, p, proto);
  53. bbMemFree(p);
  54. bbMemFree(h);
  55. return res;
  56. }
  57. #ifdef BMX_NG
  58. int bmx_mbedtls_net_recv(mbedtls_net_context * context, char * buf, size_t length) {
  59. #else
  60. int bmx_mbedtls_net_recv(mbedtls_net_context * context, char * buf, int length) {
  61. #endif
  62. return mbedtls_net_recv(context, buf, length);
  63. }
  64. #ifdef BMX_NG
  65. int bmx_mbedtls_net_recv_timeout(mbedtls_net_context * context, char * buf, size_t length, uint32_t timeout) {
  66. #else
  67. int bmx_mbedtls_net_recv_timeout(mbedtls_net_context * context, char * buf, int length, int timeout) {
  68. #endif
  69. return mbedtls_net_recv_timeout(context, buf, length, timeout);
  70. }
  71. #ifdef BMX_NG
  72. int bmx_mbedtls_net_send(mbedtls_net_context * context, char * buf, size_t length) {
  73. #else
  74. int bmx_mbedtls_net_send(mbedtls_net_context * context, char * buf, int length) {
  75. #endif
  76. return mbedtls_net_send(context, buf, length);
  77. }
  78. #ifdef BMX_NG
  79. void bmx_mbedtls_net_usleep(uint32_t usec) {
  80. #else
  81. void bmx_mbedtls_net_usleep(int usec) {
  82. #endif
  83. mbedtls_net_usleep(usec);
  84. }
  85. int bmx_mbedtls_net_poll(mbedtls_net_context * context, int rw, int timeout) {
  86. return mbedtls_net_poll(context, rw, timeout);
  87. }
  88. int bmx_mbedtls_net_cbsend(mbedtls_ssl_send_t * send, void * context, char * buf, size_t length) {
  89. return send(context, buf, length);
  90. }
  91. int bmx_mbedtls_net_cbrecv(mbedtls_ssl_recv_t * recv, void * context, char * buf, size_t length) {
  92. return recv(context, buf, length);
  93. }
  94. int bmx_mbedtls_net_cbtimeout(mbedtls_ssl_recv_timeout_t * recvtimeout, void * context, char * buf, size_t length, int timeout) {
  95. return recvtimeout(context, buf, length, timeout);
  96. }
  97. // --------------------------------------------------------
  98. mbedtls_ssl_context * bmx_mbedtls_ssl_init() {
  99. mbedtls_ssl_context * context = (mbedtls_ssl_context *)malloc(sizeof(mbedtls_ssl_context));
  100. mbedtls_ssl_init(context);
  101. return context;
  102. }
  103. void bmx_mbedtls_ssl_free(mbedtls_ssl_context * context) {
  104. mbedtls_ssl_free(context);
  105. free(context);
  106. }
  107. #ifdef BMX_NG
  108. int bmx_mbedtls_ssl_read(mbedtls_ssl_context * context, char * buf, size_t length) {
  109. #else
  110. int bmx_mbedtls_ssl_read(mbedtls_ssl_context * context, char * buf, int length) {
  111. #endif
  112. return mbedtls_ssl_read(context, buf, length);
  113. }
  114. #ifdef BMX_NG
  115. int bmx_mbedtls_ssl_write(mbedtls_ssl_context * context, char * buf, size_t length) {
  116. #else
  117. int bmx_mbedtls_ssl_write(mbedtls_ssl_context * context, char * buf, int length) {
  118. #endif
  119. return mbedtls_ssl_write(context, buf, length);
  120. }
  121. // --------------------------------------------------------
  122. mbedtls_ssl_config * bmx_mbedtls_ssl_config_init() {
  123. mbedtls_ssl_config * config = (mbedtls_ssl_config *)malloc(sizeof(mbedtls_ssl_config));
  124. mbedtls_ssl_config_init(config);
  125. return config;
  126. }
  127. void bmx_mbedtls_ssl_config_free(mbedtls_ssl_config * config) {
  128. mbedtls_ssl_config_free(config);
  129. free(config);
  130. }
  131. void bmx_mbedtls_ssl_conf_rng(mbedtls_ssl_config * config, int (*f_rng)(void *, unsigned char *, size_t), void *rng) {
  132. mbedtls_ssl_conf_rng(config, f_rng, rng);
  133. }
  134. // --------------------------------------------------------
  135. mbedtls_x509_crt * bmx_mbedtls_x509_crt_init() {
  136. mbedtls_x509_crt * cert = (mbedtls_x509_crt *)malloc(sizeof(mbedtls_x509_crt));
  137. mbedtls_x509_crt_init(cert);
  138. return cert;
  139. }
  140. void bmx_mbedtls_x509_crt_free(mbedtls_x509_crt * cert) {
  141. mbedtls_x509_crt_free(cert);
  142. free(cert);
  143. }
  144. int bmx_mbedtls_x509_crt_parse(mbedtls_x509_crt * cert, char * buf, int buflen) {
  145. return mbedtls_x509_crt_parse(cert, buf, buflen);
  146. }
  147. // --------------------------------------------------------
  148. mbedtls_pk_context * bmx_mbedtls_pk_init() {
  149. mbedtls_pk_context * context = (mbedtls_pk_context *)malloc(sizeof(mbedtls_pk_context));
  150. mbedtls_pk_init(context);
  151. return context;
  152. }
  153. void bmx_mbedtls_pk_free(mbedtls_pk_context * context) {
  154. mbedtls_pk_free(context);
  155. free(context);
  156. }
  157. int bmx_mbedtls_pk_parse_key(mbedtls_pk_context * context, char * key, int keylen, char * pwd, int pwdlen, int (*f_rng)(void *, unsigned char *, size_t), void *rng) {
  158. return mbedtls_pk_parse_key(context, key, keylen, pwd, pwdlen, f_rng, rng);
  159. }
  160. int bmx_mbedtls_pk_parse_key_string(mbedtls_pk_context * context, BBString * key, BBString * pwd, int (*f_rng)(void *, unsigned char *, size_t), void *rng) {
  161. char * k = bbStringToCString(key);
  162. char * p = NULL;
  163. // mbedtls takes length including the null terminator
  164. size_t k_len = strlen(k) + 1;
  165. size_t p_len = 0;
  166. if (pwd != &bbEmptyString) {
  167. p = bbStringToCString(pwd);
  168. p_len = strlen(p) + 1;
  169. }
  170. int res = mbedtls_pk_parse_key(context, k, k_len, p, p_len, f_rng, rng);
  171. bbMemFree(p);
  172. if (pwd != &bbEmptyString) {
  173. bbMemFree(k);
  174. }
  175. return res;
  176. }
  177. // --------------------------------------------------------
  178. mbedtls_ctr_drbg_context * bmx_mbedtls_ctr_drbg_init() {
  179. mbedtls_ctr_drbg_context * context = (mbedtls_ctr_drbg_context *)malloc(sizeof(mbedtls_ctr_drbg_context));
  180. mbedtls_ctr_drbg_init(context);
  181. return context;
  182. }
  183. void bmx_mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context * context) {
  184. mbedtls_ctr_drbg_free(context);
  185. free(context);
  186. }
  187. int bmx_mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context * context, int (*f_entropy)(void *, unsigned char *, size_t), void *entropy, const unsigned char *custom, size_t len) {
  188. return mbedtls_ctr_drbg_seed(context, f_entropy, entropy, custom, len);
  189. }
  190. // --------------------------------------------------------
  191. mbedtls_entropy_context * bmx_mbedtls_entropy_init() {
  192. mbedtls_entropy_context * context = (mbedtls_entropy_context *)malloc(sizeof(mbedtls_entropy_context));
  193. mbedtls_entropy_init(context);
  194. return context;
  195. }
  196. void bmx_mbedtls_entropy_free(mbedtls_entropy_context * context) {
  197. mbedtls_entropy_free(context);
  198. free(context);
  199. }