arduino-audio-tools
Loading...
Searching...
No Matches
WiFiClientSecureZephyr.h
Go to the documentation of this file.
1#pragma once
2#include "WiFiClientZephyr.h"
3#include <zephyr/net/tls_credentials.h> // sec_tag_t, tls_credential_type, tls_credential_add, TLS_CREDENTIAL_*
4#include <zephyr/net/socket.h> // SOL_TLS, TLS_PEER_VERIFY, TLS_HOSTNAME, TLS_SEC_TAG_LIST, IPPROTO_TLS_1_2
5
6namespace audio_tools {
7
25 public:
27 : WiFiClientZephyr(), // base initialises _sock, _timeout_ms, etc.
28 _insecure(false),
29 _ca_tag(TLS_TAG_NONE),
30 _cert_tag(TLS_TAG_NONE),
31 _key_tag(TLS_TAG_NONE),
32 _next_tag(TAG_BASE) {}
33
34 ~WiFiClientSecureZephyr() { stop(); } // calls base stop()
35
36 // --- credential API (unchanged from before) ---
37 bool setCACert(const uint8_t* pem, size_t len) { return _addCredential(TLS_CREDENTIAL_CA_CERTIFICATE, pem, len, &_ca_tag); }
38 bool setCACert(const char* pem) { return pem && setCACert((const uint8_t*)pem, strlen(pem)+1); }
39 bool setCertificate(const uint8_t* pem, size_t len) { return _addCredential(TLS_CREDENTIAL_PUBLIC_CERTIFICATE, pem, len, &_cert_tag); }
40 bool setCertificate(const char* pem) { return pem && setCertificate((const uint8_t*)pem, strlen(pem)+1); }
41 bool setPrivateKey(const uint8_t* pem, size_t len) { return _addCredential(TLS_CREDENTIAL_PRIVATE_KEY, pem, len, &_key_tag); }
42 bool setPrivateKey(const char* pem) { return pem && setPrivateKey((const uint8_t*)pem, strlen(pem)+1); }
43 void setSecure(bool secure) { _insecure = secure; }
44 void setInsecure() { _insecure = true; }
45
46 // --- only connect() needs overriding ---
47 int connect(const char* host, uint16_t port) override {
48 stop(); // inherited
49
50 struct sockaddr_storage addr = {};
51 socklen_t addrlen = sizeof(addr);
52
53 if (_resolve(host, port, (struct sockaddr*)&addr, &addrlen) < 0) // inherited
54 return 0;
55
57 if (_sock < 0) return 0;
58
59 if (!_configureTLS(host)) {
60 zsock_close(_sock); _sock = -1; return 0;
61 }
62
63 _applyTimeout(); // inherited
64
65 if (zsock_connect(_sock, (struct sockaddr*)&addr, addrlen) < 0) {
66 zsock_close(_sock); _sock = -1; return 0;
67 }
68 return 1;
69 }
70
71 int connect(uint32_t ip, uint16_t port) override {
72 char host[INET_ADDRSTRLEN];
73 struct in_addr addr_in;
74 addr_in.s_addr = ip;
75 zsock_inet_ntop(AF_INET, &addr_in, host, sizeof(host));
76 return connect(host, port); // calls the overridden string version above
77 }
78
79 private:
80 bool _insecure;
81 sec_tag_t _ca_tag, _cert_tag, _key_tag, _next_tag;
82
83 static constexpr sec_tag_t TAG_BASE = 100;
84 static constexpr sec_tag_t TLS_TAG_NONE = 0;
85
86 bool _addCredential(enum tls_credential_type type,
87 const uint8_t* data, size_t len, sec_tag_t* tag_out) {
88 if (!data || len == 0) return false;
89 sec_tag_t tag = _next_tag++;
90 if (tls_credential_add(tag, type, data, len) < 0) { --_next_tag; return false; }
91 *tag_out = tag;
92 return true;
93 }
94
95 bool _configureTLS(const char* host) {
98 return false;
99
100 if (host && !_insecure)
101 if (zsock_setsockopt(_sock, SOL_TLS, TLS_HOSTNAME, host, strlen(host)) < 0)
102 return false;
103
104 sec_tag_t tags[3]; int n = 0;
105 if (_ca_tag != TLS_TAG_NONE) tags[n++] = _ca_tag;
106 if (_cert_tag != TLS_TAG_NONE) tags[n++] = _cert_tag;
107 if (_key_tag != TLS_TAG_NONE) tags[n++] = _key_tag;
108
109 if (n > 0)
111 return false;
112
113 return true;
114 }
115};
116
118
119} // namespace audio_tools
Definition WiFiClientSecureZephyr.h:24
int connect(const char *host, uint16_t port) override
Definition WiFiClientSecureZephyr.h:47
WiFiClientSecureZephyr()
Definition WiFiClientSecureZephyr.h:26
~WiFiClientSecureZephyr()
Definition WiFiClientSecureZephyr.h:34
void setSecure(bool secure)
Definition WiFiClientSecureZephyr.h:43
bool setCertificate(const uint8_t *pem, size_t len)
Definition WiFiClientSecureZephyr.h:39
bool setCertificate(const char *pem)
Definition WiFiClientSecureZephyr.h:40
void setInsecure()
Definition WiFiClientSecureZephyr.h:44
bool setPrivateKey(const char *pem)
Definition WiFiClientSecureZephyr.h:42
bool setPrivateKey(const uint8_t *pem, size_t len)
Definition WiFiClientSecureZephyr.h:41
bool setCACert(const uint8_t *pem, size_t len)
Definition WiFiClientSecureZephyr.h:37
bool setCACert(const char *pem)
Definition WiFiClientSecureZephyr.h:38
int connect(uint32_t ip, uint16_t port) override
Definition WiFiClientSecureZephyr.h:71
Definition WiFiClientZephyr.h:44
int _sock
Definition WiFiClientZephyr.h:278
int _resolve(const char *host, uint16_t port, struct sockaddr *out, socklen_t *outlen)
Definition WiFiClientZephyr.h:291
void stop()
Definition WiFiClientZephyr.h:104
void _applyTimeout()
Definition WiFiClientZephyr.h:283
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508