]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
cli, serv: allow multiple --compress-cert options
authorDaiki Ueno <ueno@gnu.org>
Mon, 14 Mar 2022 15:03:07 +0000 (16:03 +0100)
committerDaiki Ueno <ueno@gnu.org>
Tue, 15 Mar 2022 15:28:31 +0000 (16:28 +0100)
This eliminates the need of parsing the comma separated list manually.

Signed-off-by: Daiki Ueno <ueno@gnu.org>
src/cli.c
src/common.c
src/common.h
src/gnutls-cli-options.json
src/gnutls-serv-options.json
src/serv.c

index 3b28a7b839624980b902aa034c42387fdb5c6c83..fb7f957b48bee163d753986a1fc1ae2739d4b8fd 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -821,7 +821,9 @@ gnutls_session_t init_tls_session(const char *host)
        }
 
        if (HAVE_OPT(COMPRESS_CERT) && disable_extensions == 0) {
-               ret = compress_cert_set_methods(session, OPT_ARG(COMPRESS_CERT));
+               ret = compress_cert_set_methods(session,
+                                               OPTS_ARRAY(COMPRESS_CERT),
+                                               OPTS_COUNT(COMPRESS_CERT));
                if (ret < 0)
                        exit(1);
        }
index 719106c032d3280fd36396b784fb10b07f8a3d1c..d850b852f4cca088cfa0e02b1627a48a19a30a26 100644 (file)
@@ -292,44 +292,38 @@ int cert_verify(gnutls_session_t session, const char *hostname, const char *purp
 }
 
 /* Parse input string and set certificate compression methods */
-int compress_cert_set_methods(gnutls_session_t session, const char *string)
+int compress_cert_set_methods(gnutls_session_t session,
+                             const char **strings,
+                             size_t n_strings)
 {
-       int ret = 0, i = 0;
-       char *s = NULL, *t = NULL, *str = NULL;
-       size_t methods_len = 0;
-       gnutls_compression_method_t *methods = NULL;
+       int ret = 0;
+       gnutls_compression_method_t *methods;
 
-       if (!string || !*string)
+       if (n_strings == 0) {
                return 0;
-
-       str = strdup(string);
-       if (!str) {
-               ret = GNUTLS_E_MEMORY_ERROR;
-               fprintf(stderr, "Could not set certificate compression methods: %s\n",
-                       gnutls_strerror(ret));
-               goto cleanup;
        }
 
-       methods_len = 1;
-       for (s = str; *s; ++s)
-               if (*s == ',')
-                       ++methods_len;
-       
-       methods = gnutls_malloc(methods_len * sizeof(gnutls_compression_method_t));
+/* GCC analyzer in 11.2 mishandles reallocarray/free */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wanalyzer-mismatching-deallocation"
+
+       methods = reallocarray(NULL, n_strings, sizeof(*methods));
        if (!methods) {
-               ret = GNUTLS_E_MEMORY_ERROR;
                fprintf(stderr, "Could not set certificate compression methods: %s\n",
                        gnutls_strerror(ret));
-               goto cleanup;
+               return GNUTLS_E_MEMORY_ERROR;
        }
 
-       for (s = str, i = 0; (t = strchr(s, ',')); s = t + 1, ++i) {
-               *t = '\0';
-               methods[i] = gnutls_compression_get_id(s);
+       for (size_t i = 0; i < n_strings; ++i) {
+               methods[i] = gnutls_compression_get_id(strings[i]);
+               if (methods[i] == GNUTLS_COMP_UNKNOWN) {
+                       fprintf(stderr, "Unknown compression method: %s\n",
+                               strings[i]);
+                       goto cleanup;
+               }
        }
-       methods[i] = gnutls_compression_get_id(s);
 
-       ret = gnutls_compress_certificate_set_methods(session, methods, methods_len);
+       ret = gnutls_compress_certificate_set_methods(session, methods, n_strings);
        if (ret < 0) {
                fprintf(stderr, "Could not set certificate compression methods: %s\n",
                        gnutls_strerror(ret));
@@ -337,9 +331,10 @@ int compress_cert_set_methods(gnutls_session_t session, const char *string)
        }
 
 cleanup:
-       free(str);
        free(methods);
 
+#pragma GCC diagnostic pop
+
        return ret;
 }
 
index f93187cfee4f9eb0d0f6353ae46a7ab6b9b4f2af..507d0d83378a5930bd99ce9d475070e946a6362d 100644 (file)
@@ -71,7 +71,8 @@ void print_cert_info2(gnutls_session_t, int flag, FILE *fp, int print_cert);
 void print_list(const char *priorities, int verbose);
 int cert_verify(gnutls_session_t session, const char *hostname, const char *purpose);
 
-int compress_cert_set_methods(gnutls_session_t session, const char *string);
+int compress_cert_set_methods(gnutls_session_t session, const char **strings,
+                             size_t n_strings);
 
 const char *raw_to_string(const unsigned char *raw, size_t raw_size);
 const char *raw_to_hex(const unsigned char *raw, size_t raw_size);
index 6a6eea1651ef963b50b057e7747a3b722e1475ca..2d712ce434f976b51d4679436417c6c0492dd42d 100644 (file)
         {
           "long-option": "compress-cert",
           "description": "Compress certificate",
-          "detail": "This option sets a list of supported compression methods for certificate compression. Use comma delimited list of compression methods such as \"zlib,brotli,zstd\".",
-          "argument-type": "string"
+          "detail": "This option sets a supported compression method for certificate compression.",
+          "argument-type": "string",
+          "multiple": true
         },
         {
           "long-option": "heartbeat",
       ]
     }
   ]
-}
\ No newline at end of file
+}
index 342b8a8cdc34e5d0dcd76cfe4f0f81c53e628e2a..015a70d3c28573675a797f895510bafb24c1e5ba 100644 (file)
         {
           "long-option": "compress-cert",
           "description": "Compress certificate",
-          "detail": "This option sets a list of supported compression methods for certificate compression. Use comma delimited list of compression methods such as \"zlib,brotli,zstd\".",
-          "argument-type": "string"
+          "detail": "This option sets a supported compression method for certificate compression.",
+          "argument-type": "string",
+          "multiple": true
         },
         {
           "long-option": "heartbeat",
       ]
     }
   ]
-}
\ No newline at end of file
+}
index 555406b434cc76b859a7d112bcce8731c98302e0..3ff335d1b17df35857382025859b13636f4aa0e0 100644 (file)
@@ -518,7 +518,9 @@ gnutls_session_t initialize_session(int dtls)
        }
 
        if (HAVE_OPT(COMPRESS_CERT)) {
-               ret = compress_cert_set_methods(session, OPT_ARG(COMPRESS_CERT));
+               ret = compress_cert_set_methods(session,
+                                               OPTS_ARRAY(COMPRESS_CERT),
+                                               OPTS_COUNT(COMPRESS_CERT));
                if (ret < 0)
                        exit(1);
        }