11/* *
22 * This work is STL free and based on Earle F. Philhower ESP8266 WiFiClientSecure helper functions.
3- *
3+ *
44 * SPDX-FileCopyrightText: 2025 Suwatchai K. <suwatchai@outlook.com>
55 *
66 * SPDX-License-Identifier: MIT
7- *
7+ *
88 * Copyright (c) 2018 Earle F. Philhower, III
99 */
1010#ifndef BSSL_HELPER_H
@@ -89,13 +89,13 @@ namespace key_bssl
8989 };
9090
9191 // Forward definitions
92- void free_ta_contents (br_x509_trust_anchor *ta);
93- void free_public_key (public_key *pk);
94- void free_private_key (private_key *sk);
95- bool looks_like_DER (const unsigned char *buf, size_t len);
96- pem_object *decode_pem (const void *src, size_t len, size_t *num);
97- void free_pem_object_contents (pem_object *po);
98- char *strdupImpl (const char *s);
92+ static void free_ta_contents (br_x509_trust_anchor *ta);
93+ static void free_public_key (public_key *pk);
94+ static void free_private_key (private_key *sk);
95+ static bool looks_like_DER (const unsigned char *buf, size_t len);
96+ static pem_object *decode_pem (const void *src, size_t len, size_t *num);
97+ static void free_pem_object_contents (pem_object *po);
98+ static char *strdupImpl (const char *s);
9999
100100 // Used as callback multiple places to append a string to a vector
101101 static void byte_vector_append (void *ctx, const void *buff, size_t len)
@@ -134,13 +134,13 @@ namespace key_bssl
134134 static bool certificate_to_trust_anchor_inner (br_x509_trust_anchor *ta, const br_x509_certificate *xc)
135135 {
136136
137- br_x509_decoder_context *dc =reinterpret_cast <br_x509_decoder_context *>(esp_sslclient_malloc (sizeof (br_x509_decoder_context)));
137+ br_x509_decoder_context *dc = reinterpret_cast <br_x509_decoder_context *>(esp_sslclient_malloc (sizeof (br_x509_decoder_context)));
138138
139139 if (!dc)
140140 return false ; // OOM check on context allocation
141141
142142 DynBuffer vdn_buffer; // Temporary buffer where the DN data is collected
143- br_x509_pkey *pk = nullptr ;
143+ br_x509_pkey *pk = nullptr ;
144144 bool success = false ; // Status flag
145145
146146 // Ensure Trust Anchor is clean before starting
@@ -161,7 +161,7 @@ namespace key_bssl
161161 if (pk == nullptr )
162162 {
163163 // No key present
164- goto cleanup;
164+ goto cleanup;
165165 }
166166
167167 // Copy Parsed DN Data (Matching original STL code's logic)
@@ -172,13 +172,15 @@ namespace key_bssl
172172 if (!ta->dn .data )
173173 {
174174 // OOM on DN copy
175- goto cleanup;
175+ goto cleanup;
176176 }
177177 memcpy (ta->dn .data , vdn_buffer.data , vdn_buffer.len );
178178 ta->dn .len = vdn_buffer.len ;
179- } else {
180- ta->dn .data = nullptr ; // Ensure null if DN is empty
181- ta->dn .len = 0 ;
179+ }
180+ else
181+ {
182+ ta->dn .data = nullptr ; // Ensure null if DN is empty
183+ ta->dn .len = 0 ;
182184 }
183185
184186 ta->flags = 0 ;
@@ -197,7 +199,7 @@ namespace key_bssl
197199 if ((ta->pkey .key .rsa .n == nullptr ) || (ta->pkey .key .rsa .e == nullptr ))
198200 {
199201 // OOM, so clean up DN data allocated above
200- free_ta_contents (ta);
202+ free_ta_contents (ta);
201203 goto cleanup;
202204 }
203205 memcpy (ta->pkey .key .rsa .n , pk->key .rsa .n , pk->key .rsa .nlen );
@@ -215,7 +217,7 @@ namespace key_bssl
215217 if (ta->pkey .key .ec .q == nullptr )
216218 {
217219 // OOM, so clean up DN data allocated above
218- free_ta_contents (ta);
220+ free_ta_contents (ta);
219221 goto cleanup;
220222 }
221223 memcpy (ta->pkey .key .ec .q , pk->key .ec .q , pk->key .ec .qlen );
@@ -231,12 +233,12 @@ namespace key_bssl
231233 cleanup:
232234 // Explicit cleanup of the decoder context and the temporary buffer
233235 esp_sslclient_free (&dc);
234- free_dyn_buffer (&vdn_buffer);
235-
236+ free_dyn_buffer (&vdn_buffer);
237+
236238 return success;
237239 }
238240
239- br_x509_trust_anchor *certificate_to_trust_anchor (const br_x509_certificate *xc)
241+ static br_x509_trust_anchor *certificate_to_trust_anchor (const br_x509_certificate *xc)
240242 {
241243 br_x509_trust_anchor *ta = reinterpret_cast <br_x509_trust_anchor *>(esp_sslclient_malloc (sizeof (br_x509_trust_anchor)));
242244 if (!ta)
@@ -250,7 +252,7 @@ namespace key_bssl
250252 return ta;
251253 }
252254
253- void free_ta_contents (br_x509_trust_anchor *ta)
255+ static void free_ta_contents (br_x509_trust_anchor *ta)
254256 {
255257 if (ta)
256258 {
@@ -271,7 +273,7 @@ namespace key_bssl
271273 // Basically tries to verify the length of all included segments
272274 // matches the length of the input buffer. Does not actually
273275 // validate any contents.
274- bool looks_like_DER (const unsigned char *buff, size_t len)
276+ static bool looks_like_DER (const unsigned char *buff, size_t len)
275277 {
276278 if (len < 2 )
277279 return false ;
@@ -303,7 +305,7 @@ namespace key_bssl
303305 }
304306 }
305307
306- void free_pem_object_contents (pem_object *po)
308+ static void free_pem_object_contents (pem_object *po)
307309 {
308310 if (po)
309311 {
@@ -312,7 +314,7 @@ namespace key_bssl
312314 }
313315 }
314316
315- char *strdupImpl (const char *s)
317+ static char *strdupImpl (const char *s)
316318 {
317319 size_t slen = strlen (s);
318320 char *result = reinterpret_cast <char *>(esp_sslclient_malloc (slen + 1 ));
@@ -351,7 +353,7 @@ namespace key_bssl
351353 // Converts a PEM (~=base64) source into a set of DER-encoded binary blobs.
352354 // Each blob is named by the ---- BEGIN xxx ---- field, and multiple
353355 // blobs may be returned.
354- key_bssl::pem_object *decode_pem (const void *src, size_t len, size_t *num)
356+ static key_bssl::pem_object *decode_pem (const void *src, size_t len, size_t *num)
355357 {
356358 // Replace std::unique_ptr with manual pointer
357359 br_pem_decoder_context *pc = nullptr ;
@@ -500,7 +502,7 @@ namespace key_bssl
500502 return nullptr ;
501503 }
502504
503- void free_certificates (br_x509_certificate *certs, size_t num)
505+ static void free_certificates (br_x509_certificate *certs, size_t num)
504506 {
505507 if (certs)
506508 {
@@ -512,7 +514,7 @@ namespace key_bssl
512514 }
513515 }
514516
515- void free_pem_object (pem_object *pos)
517+ static void free_pem_object (pem_object *pos)
516518 {
517519 if (pos != nullptr )
518520 {
@@ -529,7 +531,7 @@ namespace key_bssl
529531 // Assuming pem_object, free_pem_object, key_bssl::free_certificates, and key_bssl::read_certificates
530532 // are defined (or used) elsewhere.
531533
532- br_x509_certificate *read_certificates (const char *buff, size_t len, size_t *num)
534+ static br_x509_certificate *read_certificates (const char *buff, size_t len, size_t *num)
533535 {
534536 br_x509_certificate *cert_array = nullptr ;
535537 size_t cert_count = 0 ;
@@ -740,7 +742,7 @@ namespace key_bssl
740742 return nullptr ;
741743 }
742744
743- void free_public_key (public_key *pk)
745+ static void free_public_key (public_key *pk)
744746 {
745747 if (pk)
746748 {
@@ -865,7 +867,7 @@ namespace key_bssl
865867 }
866868 return nullptr ;
867869 }
868- void free_private_key (private_key *sk)
870+ static void free_private_key (private_key *sk)
869871 {
870872 if (sk)
871873 {
@@ -889,7 +891,7 @@ namespace key_bssl
889891 }
890892 }
891893
892- private_key *read_private_key (const char *buff, size_t len)
894+ static private_key *read_private_key (const char *buff, size_t len)
893895 {
894896 private_key *sk = nullptr ;
895897 pem_object *pos = nullptr ;
@@ -920,7 +922,7 @@ namespace key_bssl
920922 return nullptr ;
921923 }
922924
923- public_key *read_public_key (const char *buff, size_t len)
925+ static public_key *read_public_key (const char *buff, size_t len)
924926 {
925927 public_key *pk = nullptr ;
926928 pem_object *pos = nullptr ;
0 commit comments