tls.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Embedded Linux library
  3. *
  4. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef __ELL_TLS_H
  21. #define __ELL_TLS_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. enum l_tls_version {
  26. L_TLS_V10 = ((3 << 8) | 1),
  27. L_TLS_V11 = ((3 << 8) | 2),
  28. L_TLS_V12 = ((3 << 8) | 3),
  29. L_TLS_V13 = ((3 << 8) | 4), /* Not supported */
  30. };
  31. struct l_tls;
  32. struct l_key;
  33. struct l_certchain;
  34. struct l_queue;
  35. enum l_tls_alert_desc {
  36. TLS_ALERT_CLOSE_NOTIFY = 0,
  37. TLS_ALERT_UNEXPECTED_MESSAGE = 10,
  38. TLS_ALERT_BAD_RECORD_MAC = 20,
  39. TLS_ALERT_DECRYPT_FAIL_RESERVED = 21,
  40. TLS_ALERT_RECORD_OVERFLOW = 22,
  41. TLS_ALERT_DECOMPRESS_FAIL = 30,
  42. TLS_ALERT_HANDSHAKE_FAIL = 40,
  43. TLS_ALERT_NO_CERT_RESERVED = 41,
  44. TLS_ALERT_BAD_CERT = 42,
  45. TLS_ALERT_UNSUPPORTED_CERT = 43,
  46. TLS_ALERT_CERT_REVOKED = 44,
  47. TLS_ALERT_CERT_EXPIRED = 45,
  48. TLS_ALERT_CERT_UNKNOWN = 46,
  49. TLS_ALERT_ILLEGAL_PARAM = 47,
  50. TLS_ALERT_UNKNOWN_CA = 48,
  51. TLS_ALERT_ACCESS_DENIED = 49,
  52. TLS_ALERT_DECODE_ERROR = 50,
  53. TLS_ALERT_DECRYPT_ERROR = 51,
  54. TLS_ALERT_EXPORT_RES_RESERVED = 60,
  55. TLS_ALERT_PROTOCOL_VERSION = 70,
  56. TLS_ALERT_INSUFFICIENT_SECURITY = 71,
  57. TLS_ALERT_INTERNAL_ERROR = 80,
  58. TLS_ALERT_USER_CANCELED = 90,
  59. TLS_ALERT_NO_RENEGOTIATION = 100,
  60. TLS_ALERT_UNSUPPORTED_EXTENSION = 110,
  61. };
  62. typedef void (*l_tls_write_cb_t)(const uint8_t *data, size_t len,
  63. void *user_data);
  64. typedef void (*l_tls_ready_cb_t)(const char *peer_identity, void *user_data);
  65. typedef void (*l_tls_disconnect_cb_t)(enum l_tls_alert_desc reason,
  66. bool remote, void *user_data);
  67. typedef void (*l_tls_debug_cb_t)(const char *str, void *user_data);
  68. typedef void (*l_tls_destroy_cb_t)(void *user_data);
  69. /*
  70. * app_data_handler gets called with newly received decrypted data.
  71. * tx_handler gets called to send TLS payloads off to remote end.
  72. * ready_handler gets called when l_tls_write calls are first accepted.
  73. */
  74. struct l_tls *l_tls_new(bool server, l_tls_write_cb_t app_data_handler,
  75. l_tls_write_cb_t tx_handler,
  76. l_tls_ready_cb_t ready_handler,
  77. l_tls_disconnect_cb_t disconnect_handler,
  78. void *user_data);
  79. void l_tls_free(struct l_tls *tls);
  80. /* Begin sending connection setup messages to the server */
  81. bool l_tls_start(struct l_tls *tls);
  82. /* Properly disconnect a connected session */
  83. void l_tls_close(struct l_tls *tls);
  84. /* Submit plaintext data to be encrypted and transmitted */
  85. void l_tls_write(struct l_tls *tls, const uint8_t *data, size_t len);
  86. /* Submit TLS payload from underlying transport to be decrypted */
  87. void l_tls_handle_rx(struct l_tls *tls, const uint8_t *data, size_t len);
  88. /*
  89. * If peer is to be authenticated, supply the CA certificates. On success
  90. * the l_tls object takes ownership of the queue and the individual l_cert
  91. * objects and they should not be freed by the caller afterwards.
  92. */
  93. bool l_tls_set_cacert(struct l_tls *tls, struct l_queue *ca_certs);
  94. /*
  95. * If we are to be authenticated, supply our certificate and private key.
  96. * On the client this is optional. On success, the l_tls object takes
  97. * ownership of the certchain and the key objects and they should not be
  98. * freed by the caller afterwards.
  99. * TODO: it may also be useful for the caller to be able to supply one
  100. * certificate of each type so they can be used depending on which is compatible
  101. * with the negotiated parameters.
  102. */
  103. bool l_tls_set_auth_data(struct l_tls *tls,
  104. struct l_certchain *certchain,
  105. struct l_key *priv_key);
  106. void l_tls_set_version_range(struct l_tls *tls,
  107. enum l_tls_version min_version,
  108. enum l_tls_version max_version);
  109. void l_tls_set_domain_mask(struct l_tls *tls, char **mask);
  110. const char *l_tls_alert_to_str(enum l_tls_alert_desc desc);
  111. enum l_checksum_type;
  112. bool l_tls_prf_get_bytes(struct l_tls *tls, bool use_master_secret,
  113. const char *label, uint8_t *buf, size_t len);
  114. bool l_tls_set_debug(struct l_tls *tls, l_tls_debug_cb_t function,
  115. void *user_data, l_tls_destroy_cb_t destroy);
  116. bool l_tls_set_cert_dump_path(struct l_tls *tls, const char *path);
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* __ELL_TLS_H */