|
| 1 | +#if defined(ESP32) |
| 2 | +// NOTE: This class is only available for ESP32 because it depends on mbedtls, which is provided by the ESP32 Arduino core. |
| 3 | +// DtlsUdp.cpp |
| 4 | +// mbedTLS DTLS wrapper class skeleton implementation for Arduino |
| 5 | +#include "DtlsUdp.h" |
| 6 | + |
| 7 | +DtlsUdp::DtlsUdp() : connected(false) { |
| 8 | + mbedtls_net_init(&net_ctx); |
| 9 | + mbedtls_ssl_init(&ssl); |
| 10 | + mbedtls_ssl_config_init(&conf); |
| 11 | + mbedtls_entropy_init(&entropy); |
| 12 | + mbedtls_ctr_drbg_init(&ctr_drbg); |
| 13 | + mbedtls_x509_crt_init(&ca_cert); |
| 14 | + mbedtls_x509_crt_init(&client_cert); |
| 15 | + mbedtls_pk_init(&client_key); |
| 16 | +} |
| 17 | + |
| 18 | +DtlsUdp::~DtlsUdp() { |
| 19 | + end(); |
| 20 | + mbedtls_ssl_free(&ssl); |
| 21 | + mbedtls_ssl_config_free(&conf); |
| 22 | + mbedtls_ctr_drbg_free(&ctr_drbg); |
| 23 | + mbedtls_entropy_free(&entropy); |
| 24 | + mbedtls_net_free(&net_ctx); |
| 25 | + mbedtls_x509_crt_free(&ca_cert); |
| 26 | + mbedtls_x509_crt_free(&client_cert); |
| 27 | + mbedtls_pk_free(&client_key); |
| 28 | +} |
| 29 | + |
| 30 | +uint8_t DtlsUdp::begin(uint16_t port) { |
| 31 | + // For DTLS: No need to initialize UDP socket |
| 32 | + return 1; |
| 33 | +} |
| 34 | + |
| 35 | +bool DtlsUdp::connect(IPAddress ip, int port) { |
| 36 | + char ipstr[16]; |
| 37 | + sprintf(ipstr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); |
| 38 | + char portstr[8]; |
| 39 | + snprintf(portstr, sizeof(portstr), "%d", port); |
| 40 | + if (mbedtls_net_connect(&net_ctx, ipstr, portstr, MBEDTLS_NET_PROTO_UDP) != 0) return false; |
| 41 | + if (mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) return false; |
| 42 | + mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_NONE); |
| 43 | + mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 44 | + if (mbedtls_ssl_setup(&ssl, &conf) != 0) return false; |
| 45 | + mbedtls_ssl_set_bio(&ssl, &net_ctx, mbedtls_net_send, mbedtls_net_recv, NULL); |
| 46 | + // DTLS handshake |
| 47 | + int ret; |
| 48 | + do { |
| 49 | + ret = mbedtls_ssl_handshake(&ssl); |
| 50 | + } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 51 | + connected = (ret == 0); |
| 52 | + return connected; |
| 53 | +} |
| 54 | + |
| 55 | +int DtlsUdp::beginPacket(IPAddress ip, uint16_t port) { |
| 56 | + _remoteIP = ip; |
| 57 | + _remotePort = port; |
| 58 | + return 1; |
| 59 | +} |
| 60 | + |
| 61 | +int DtlsUdp::beginPacket(const char *host, uint16_t port) { |
| 62 | + // Hostname resolution not supported (implement if needed) |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +int DtlsUdp::endPacket() { |
| 67 | + // For DTLS: send is done directly in write |
| 68 | + return 1; |
| 69 | +} |
| 70 | + |
| 71 | +size_t DtlsUdp::write(const uint8_t *buf, size_t size) { |
| 72 | + if (!connected) return 0; |
| 73 | + return mbedtls_ssl_write(&ssl, buf, size); |
| 74 | +} |
| 75 | + |
| 76 | +size_t DtlsUdp::write(uint8_t data) { |
| 77 | + return write(&data, 1); |
| 78 | +} |
| 79 | + |
| 80 | +int DtlsUdp::parsePacket() { |
| 81 | + // DTLS is connection-oriented, always treat as one packet |
| 82 | + return 1; |
| 83 | +} |
| 84 | + |
| 85 | +int DtlsUdp::available() { |
| 86 | + // Check if receive buffer has data (simple implementation) |
| 87 | + return 1; |
| 88 | +} |
| 89 | + |
| 90 | +int DtlsUdp::read(unsigned char* buffer, size_t len) { |
| 91 | + if (!connected) return 0; |
| 92 | + return mbedtls_ssl_read(&ssl, buffer, len); |
| 93 | +} |
| 94 | + |
| 95 | +int DtlsUdp::read(char* buffer, size_t len) { |
| 96 | + if (!connected) return 0; |
| 97 | + return mbedtls_ssl_read(&ssl, (unsigned char*)buffer, len); |
| 98 | +} |
| 99 | + |
| 100 | +int DtlsUdp::read() { |
| 101 | + unsigned char b; |
| 102 | + return read(&b, 1) == 1 ? b : -1; |
| 103 | +} |
| 104 | + |
| 105 | +int DtlsUdp::peek() { return -1; } |
| 106 | + |
| 107 | +void DtlsUdp::flush() {} |
| 108 | + |
| 109 | +IPAddress DtlsUdp::remoteIP() { return _remoteIP; } |
| 110 | + |
| 111 | +uint16_t DtlsUdp::remotePort() { return _remotePort; } |
| 112 | + |
| 113 | +void DtlsUdp::end() { |
| 114 | + if (connected) { |
| 115 | + mbedtls_ssl_close_notify(&ssl); |
| 116 | + connected = false; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void DtlsUdp::stop() { |
| 121 | + // For DTLS: nothing to do |
| 122 | +} |
| 123 | + |
| 124 | +bool DtlsUdp::setRootCA(const char* ca_pem) { |
| 125 | + mbedtls_x509_crt_free(&ca_cert); |
| 126 | + mbedtls_x509_crt_init(&ca_cert); |
| 127 | + int ret = mbedtls_x509_crt_parse(&ca_cert, (const unsigned char*)ca_pem, strlen(ca_pem)+1); |
| 128 | + if (ret != 0) return false; |
| 129 | + mbedtls_ssl_conf_ca_chain(&conf, &ca_cert, NULL); |
| 130 | + mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED); |
| 131 | + return true; |
| 132 | +} |
| 133 | + |
| 134 | +bool DtlsUdp::setClientCert(const char* cert_pem, const char* key_pem) { |
| 135 | + mbedtls_x509_crt_free(&client_cert); |
| 136 | + mbedtls_x509_crt_init(&client_cert); |
| 137 | + mbedtls_pk_free(&client_key); |
| 138 | + mbedtls_pk_init(&client_key); |
| 139 | + int ret1 = mbedtls_x509_crt_parse(&client_cert, (const unsigned char*)cert_pem, strlen(cert_pem)+1); |
| 140 | + int ret2 = mbedtls_pk_parse_key(&client_key, (const unsigned char*)key_pem, strlen(key_pem)+1, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 141 | + if (ret1 != 0 || ret2 != 0) return false; |
| 142 | + mbedtls_ssl_conf_own_cert(&conf, &client_cert, &client_key); |
| 143 | + return true; |
| 144 | +} |
| 145 | +#endif // ARDUINO_ARCH_ESP32 |
0 commit comments