ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
tls_policy.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file tls_policy.cpp
6 * @brief TLS version negotiation + pinned cipher-suite policy (see tls_policy.h).
7 */
8
10
11#if PC_ENABLE_TLS_POLICY
12
13uint16_t pc_tls_negotiate_version(uint16_t client_max, uint16_t server_min, uint16_t server_max)
14{
15 if (server_min > server_max)
16 {
17 return 0;
18 }
19 if (client_max < server_min)
20 {
21 return 0; // the client cannot go as high as we require
22 }
23 uint16_t chosen = client_max < server_max ? client_max : server_max;
24 return chosen;
25}
26
27const char *pc_tls_version_name(uint16_t version)
28{
29 switch (version)
30 {
31 case TLS_VERSION_1_2:
32 return "TLS 1.2";
33 case TLS_VERSION_1_3:
34 return "TLS 1.3";
35 default:
36 return "unknown";
37 }
38}
39
40uint16_t pc_tls_select_cipher(const uint16_t *client_offered, size_t n_client, const uint16_t *server_pinned,
41 size_t n_server)
42{
43 if (!client_offered || !server_pinned)
44 {
45 return 0;
46 }
47 // Server preference: walk the pinned list in order, take the first the client also offered.
48 for (size_t i = 0; i < n_server; i++)
49 {
50 for (size_t j = 0; j < n_client; j++)
51 {
52 if (server_pinned[i] == client_offered[j])
53 {
54 return server_pinned[i];
55 }
56 }
57 }
58 return 0;
59}
60
61bool pc_tls_is_aead(uint16_t suite)
62{
63 switch (suite)
64 {
65 // TLS 1.3 AEAD suites.
66 case 0x1301: // TLS_AES_128_GCM_SHA256
67 case 0x1302: // TLS_AES_256_GCM_SHA384
68 case 0x1303: // TLS_CHACHA20_POLY1305_SHA256
69 // TLS 1.2 ECDHE AEAD suites.
70 case 0xC02B: // ECDHE_ECDSA_AES_128_GCM_SHA256
71 case 0xC02C: // ECDHE_ECDSA_AES_256_GCM_SHA384
72 case 0xC02F: // ECDHE_RSA_AES_128_GCM_SHA256
73 case 0xC030: // ECDHE_RSA_AES_256_GCM_SHA384
74 case 0xCCA8: // ECDHE_RSA_CHACHA20_POLY1305
75 case 0xCCA9: // ECDHE_ECDSA_CHACHA20_POLY1305
76 return true;
77 default:
78 return false;
79 }
80}
81
82#endif // PC_ENABLE_TLS_POLICY
TLS version negotiation + pinned cipher-suite policy (PC_ENABLE_TLS_POLICY).