]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
vtls: convert pubkey_pem_to_der to use dynbuf
authorDaniel Stenberg <daniel@haxx.se>
Wed, 2 Oct 2024 12:14:18 +0000 (14:14 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 3 Oct 2024 06:33:53 +0000 (08:33 +0200)
... instead of malloc and "manual" buffer stuffing

Closes #15126

lib/vtls/vtls.c

index a5cb7b0ffadebe2b3226e6278c2d87a09f615a67..13c46b8c7a6ccf32f8127e4e4a4761a4e9d2e2c3 100644 (file)
@@ -941,14 +941,17 @@ CURLcode Curl_ssl_random(struct Curl_easy *data,
 static CURLcode pubkey_pem_to_der(const char *pem,
                                   unsigned char **der, size_t *der_len)
 {
-  char *stripped_pem, *begin_pos, *end_pos;
-  size_t pem_count, stripped_pem_count = 0, pem_len;
+  char *begin_pos, *end_pos;
+  size_t pem_count, pem_len;
   CURLcode result;
+  struct dynbuf pbuf;
 
   /* if no pem, exit. */
   if(!pem)
     return CURLE_BAD_CONTENT_ENCODING;
 
+  Curl_dyn_init(&pbuf, MAX_PINNED_PUBKEY_SIZE);
+
   begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
   if(!begin_pos)
     return CURLE_BAD_CONTENT_ENCODING;
@@ -968,26 +971,23 @@ static CURLcode pubkey_pem_to_der(const char *pem,
 
   pem_len = end_pos - pem;
 
-  stripped_pem = malloc(pem_len - pem_count + 1);
-  if(!stripped_pem)
-    return CURLE_OUT_OF_MEMORY;
-
   /*
    * Here we loop through the pem array one character at a time between the
    * correct indices, and place each character that is not '\n' or '\r'
    * into the stripped_pem array, which should represent the raw base64 string
    */
   while(pem_count < pem_len) {
-    if('\n' != pem[pem_count] && '\r' != pem[pem_count])
-      stripped_pem[stripped_pem_count++] = pem[pem_count];
+    if('\n' != pem[pem_count] && '\r' != pem[pem_count]) {
+      result = Curl_dyn_addn(&pbuf, &pem[pem_count], 1);
+      if(result)
+        return result;
+    }
     ++pem_count;
   }
-  /* Place the null terminator in the correct place */
-  stripped_pem[stripped_pem_count] = '\0';
 
-  result = Curl_base64_decode(stripped_pem, der, der_len);
+  result = Curl_base64_decode(Curl_dyn_ptr(&pbuf), der, der_len);
 
-  Curl_safefree(stripped_pem);
+  Curl_dyn_free(&pbuf);
 
   return result;
 }