}
}
+int
+get_num_elements(const char *string, char delimiter)
+{
+ int string_len = strlen(string);
+
+ ASSERT(0 != string_len);
+
+ int element_count = 1;
+ /* Get number of ciphers */
+ for (int i = 0; i < string_len; i++)
+ {
+ if (string[i] == delimiter)
+ {
+ element_count++;
+ }
+ }
+
+ return element_count;
+}
#endif /* P2MP_SERVER */
#endif /* P2MP_SERVER */
+/**
+ * Returns the occurrences of 'delimiter' in a string +1
+ * This is typically used to find out the number elements in a
+ * cipher string or similar that is separated by : like
+ *
+ * X25519:secp256r1:X448:secp512r1:secp384r1:brainpoolP384r1
+ *
+ * @param string the string to work on
+ * @param delimiter the delimiter to count, typically ':'
+ * @return occrrences of delimiter + 1
+ */
+int
+get_num_elements(const char *string, char delimiter);
+
#endif /* ifndef MISC_H */
tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
{
char *tmp_ciphers, *tmp_ciphers_orig, *token;
- int i, cipher_count;
- int ciphers_len;
if (NULL == ciphers)
{
return; /* Nothing to do */
-
}
- ciphers_len = strlen(ciphers);
ASSERT(NULL != ctx);
- ASSERT(0 != ciphers_len);
/* Get number of ciphers */
- for (i = 0, cipher_count = 1; i < ciphers_len; i++)
- {
- if (ciphers[i] == ':')
- {
- cipher_count++;
- }
- }
+ int cipher_count = get_num_elements(ciphers, ':');
/* Allocate an array for them */
ALLOC_ARRAY_CLEAR(ctx->allowed_ciphers, int, cipher_count+1)
/* Parse allowed ciphers, getting IDs */
- i = 0;
+ int i = 0;
tmp_ciphers_orig = tmp_ciphers = string_alloc(ciphers, NULL);
token = strtok(tmp_ciphers, ":");