Make EVP_MD_CTX_[gettable|settable]_params() take an EVP_MD_CTX
[openssl.git] / apps / list.c
index 555ce7e815ab7a1ac32b9f75fbdb88c8cbd36405..5348cc68dd3f487ed5643a9bd2c7e2d49bdcfb4a 100644 (file)
 #include <openssl/err.h>
 #include <openssl/provider.h>
 #include <openssl/safestack.h>
+#include <openssl/kdf.h>
 #include "apps.h"
+#include "app_params.h"
 #include "progs.h"
 #include "opt.h"
 
 static int verbose = 0;
 
-static int describe_param_type(char *buf, size_t bufsz, const OSSL_PARAM *param)
-{
-    const char *type_mod = "";
-    const char *type = NULL;
-    int show_type_number = 0;
-    int printed_len;
-
-    switch (param->data_type) {
-    case OSSL_PARAM_UNSIGNED_INTEGER:
-        type_mod = "unsigned ";
-        /* FALLTHRU */
-    case OSSL_PARAM_INTEGER:
-        type = "integer";
-        break;
-    case OSSL_PARAM_UTF8_PTR:
-        type_mod = "pointer to a ";
-        /* FALLTHRU */
-    case OSSL_PARAM_UTF8_STRING:
-        type = "UTF8 encoded string";
-        break;
-    case OSSL_PARAM_OCTET_PTR:
-        type_mod = "pointer to an ";
-        /* FALLTHRU */
-    case OSSL_PARAM_OCTET_STRING:
-        type = "octet string";
-        break;
-    default:
-        type = "unknown type";
-        show_type_number = 1;
-        break;
-    }
-
-    printed_len = BIO_snprintf(buf, bufsz, "%s: ", param->key);
-    if (printed_len > 0) {
-        buf += printed_len;
-        bufsz -= printed_len;
-    }
-    printed_len = BIO_snprintf(buf, bufsz, "%s%s", type_mod, type);
-    if (printed_len > 0) {
-        buf += printed_len;
-        bufsz -= printed_len;
-    }
-    if (show_type_number) {
-        printed_len = BIO_snprintf(buf, bufsz, " [%d]", param->data_type);
-        if (printed_len > 0) {
-            buf += printed_len;
-            bufsz -= printed_len;
-        }
-    }
-    if (param->data_size == 0)
-        printed_len = BIO_snprintf(buf, bufsz, " (arbitrary size)");
-    else
-        printed_len = BIO_snprintf(buf, bufsz, " (max %zu bytes large)",
-                                   param->data_size);
-    if (printed_len > 0) {
-        buf += printed_len;
-        bufsz -= printed_len;
-    }
-    *buf = '\0';
-    return 1;
-}
-
-static int print_param_types(const char *thing, const OSSL_PARAM *pdefs)
-{
-    if (pdefs == NULL) {
-        BIO_printf(bio_out, "    No declared %s\n", thing);
-    } else if (pdefs->key == NULL) {
-        /*
-         * An empty list?  This shouldn't happen, but let's just make sure to
-         * say something if there's a badly written provider...
-         */
-        BIO_printf(bio_out, "    Empty list of %s (!!!)\n", thing);
-    } else {
-        BIO_printf(bio_out, "    %s:\n", thing);
-        for (; pdefs->key != NULL; pdefs++) {
-            char buf[200];       /* This should be ample space */
-
-            describe_param_type(buf, sizeof(buf), pdefs);
-            BIO_printf(bio_out, "      %s\n", buf);
-        }
-    }
-    return 1;
-}
-
 static void legacy_cipher_fn(const EVP_CIPHER *c,
                              const char *from, const char *to, void *arg)
 {
@@ -133,8 +51,8 @@ static void collect_ciphers(EVP_CIPHER *cipher, void *stack)
 {
     STACK_OF(EVP_CIPHER) *cipher_stack = stack;
 
-    sk_EVP_CIPHER_push(cipher_stack, cipher);
-    EVP_CIPHER_up_ref(cipher);
+    if (sk_EVP_CIPHER_push(cipher_stack, cipher) > 0)
+        EVP_CIPHER_up_ref(cipher);
 }
 
 static void list_ciphers(void)
@@ -156,14 +74,14 @@ static void list_ciphers(void)
                    OSSL_PROVIDER_name(EVP_CIPHER_provider(c)));
         if (verbose) {
             print_param_types("retrievable algorithm parameters",
-                              EVP_CIPHER_gettable_params(c));
+                              EVP_CIPHER_gettable_params(c), 4);
             print_param_types("retrievable operation parameters",
-                              EVP_CIPHER_CTX_gettable_params(c));
+                              EVP_CIPHER_CTX_gettable_params(c), 4);
             print_param_types("settable operation parameters",
-                              EVP_CIPHER_CTX_settable_params(c));
+                              EVP_CIPHER_CTX_settable_params(c), 4);
         }
     }
-    sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_meth_free);
+    sk_EVP_CIPHER_pop_free(ciphers, EVP_CIPHER_free);
 }
 
 static void list_md_fn(const EVP_MD *m,
@@ -196,8 +114,8 @@ static void collect_digests(EVP_MD *md, void *stack)
 {
     STACK_OF(EVP_MD) *digest_stack = stack;
 
-    sk_EVP_MD_push(digest_stack, md);
-    EVP_MD_up_ref(md);
+    if (sk_EVP_MD_push(digest_stack, md) > 0)
+        EVP_MD_up_ref(md);
 }
 
 static void list_digests(void)
@@ -219,31 +137,112 @@ static void list_digests(void)
                    OSSL_PROVIDER_name(EVP_MD_provider(m)));
         if (verbose) {
             print_param_types("retrievable algorithm parameters",
-                              EVP_MD_gettable_params(m));
+                              EVP_MD_gettable_params(m), 4);
             print_param_types("retrievable operation parameters",
-                              EVP_MD_CTX_gettable_params(m));
+                              EVP_MD_gettable_ctx_params(m), 4);
             print_param_types("settable operation parameters",
-                              EVP_MD_CTX_settable_params(m));
+                              EVP_MD_settable_ctx_params(m), 4);
         }
     }
-    sk_EVP_MD_pop_free(digests, EVP_MD_meth_free);
+    sk_EVP_MD_pop_free(digests, EVP_MD_free);
 }
 
-#if 0                            /* Temporarly disabled */
-static void list_mac_fn(const EVP_MAC *m,
-                        const char *from, const char *to, void *arg)
+DEFINE_STACK_OF(EVP_MAC)
+static int mac_cmp(const EVP_MAC * const *a, const EVP_MAC * const *b)
 {
-    if (m != NULL) {
-        BIO_printf(arg, "%s\n", EVP_MAC_name(m));
-    } else {
-        if (from == NULL)
-            from = "<undefined>";
-        if (to == NULL)
-            to = "<undefined>";
-        BIO_printf(arg, "%s => %s\n", from, to);
+    int ret = strcasecmp(EVP_MAC_name(*a), EVP_MAC_name(*b));
+
+    if (ret == 0)
+        ret = strcmp(OSSL_PROVIDER_name(EVP_MAC_provider(*a)),
+                     OSSL_PROVIDER_name(EVP_MAC_provider(*b)));
+
+    return ret;
+}
+
+static void collect_macs(EVP_MAC *mac, void *stack)
+{
+    STACK_OF(EVP_MAC) *mac_stack = stack;
+
+    if (sk_EVP_MAC_push(mac_stack, mac) > 0)
+        EVP_MAC_up_ref(mac);
+}
+
+static void list_macs(void)
+{
+    STACK_OF(EVP_MAC) *macs = sk_EVP_MAC_new(mac_cmp);
+    int i;
+
+    BIO_printf(bio_out, "Provided MACs:\n");
+    EVP_MAC_do_all_ex(NULL, collect_macs, macs);
+    sk_EVP_MAC_sort(macs);
+    for (i = 0; i < sk_EVP_MAC_num(macs); i++) {
+        const EVP_MAC *m = sk_EVP_MAC_value(macs, i);
+
+        BIO_printf(bio_out, "  %s", EVP_MAC_name(m));
+        BIO_printf(bio_out, " @ %s\n",
+                   OSSL_PROVIDER_name(EVP_MAC_provider(m)));
+
+        if (verbose) {
+            print_param_types("retrievable algorithm parameters",
+                              EVP_MAC_gettable_params(m), 4);
+            print_param_types("retrievable operation parameters",
+                              EVP_MAC_CTX_gettable_params(m), 4);
+            print_param_types("settable operation parameters",
+                              EVP_MAC_CTX_settable_params(m), 4);
+        }
     }
+    sk_EVP_MAC_pop_free(macs, EVP_MAC_free);
+}
+
+/*
+ * KDFs and PRFs
+ */
+DEFINE_STACK_OF(EVP_KDF)
+static int kdf_cmp(const EVP_KDF * const *a, const EVP_KDF * const *b)
+{
+    int ret = strcasecmp(EVP_KDF_name(*a), EVP_KDF_name(*b));
+
+    if (ret == 0)
+        ret = strcmp(OSSL_PROVIDER_name(EVP_KDF_provider(*a)),
+                     OSSL_PROVIDER_name(EVP_KDF_provider(*b)));
+
+    return ret;
+}
+
+static void collect_kdfs(EVP_KDF *kdf, void *stack)
+{
+    STACK_OF(EVP_KDF) *kdf_stack = stack;
+
+    sk_EVP_KDF_push(kdf_stack, kdf);
+    EVP_KDF_up_ref(kdf);
+}
+
+static void list_kdfs(void)
+{
+    STACK_OF(EVP_KDF) *kdfs = sk_EVP_KDF_new(kdf_cmp);
+    int i;
+
+    BIO_printf(bio_out, "Provided KDFs and PDFs:\n");
+    EVP_KDF_do_all_ex(NULL, collect_kdfs, kdfs);
+    sk_EVP_KDF_sort(kdfs);
+    for (i = 0; i < sk_EVP_KDF_num(kdfs); i++) {
+        const EVP_KDF *m = sk_EVP_KDF_value(kdfs, i);
+
+        BIO_printf(bio_out, "  %s", EVP_KDF_name(m));
+        BIO_printf(bio_out, " @ %s\n",
+                   OSSL_PROVIDER_name(EVP_KDF_provider(m)));
+
+        if (verbose) {
+            print_param_types("retrievable algorithm parameters",
+                              EVP_KDF_gettable_params(m), 4);
+            print_param_types("retrievable operation parameters",
+                              EVP_KDF_CTX_gettable_params(m), 4);
+            print_param_types("settable operation parameters",
+                              EVP_KDF_CTX_settable_params(m), 4);
+        }
+    }
+    sk_EVP_KDF_pop_free(kdfs, EVP_KDF_free);
 }
-#endif
 
 static void list_missing_help(void)
 {
@@ -579,7 +578,7 @@ typedef enum HELPLIST_CHOICE {
     OPT_COMMANDS, OPT_DIGEST_COMMANDS, OPT_MAC_ALGORITHMS, OPT_OPTIONS,
     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
     OPT_PK_ALGORITHMS, OPT_PK_METHOD, OPT_ENGINES, OPT_DISABLED,
-    OPT_MISSING_HELP, OPT_OBJECTS
+    OPT_KDF_ALGORITHMS, OPT_MISSING_HELP, OPT_OBJECTS
 } HELPLIST_CHOICE;
 
 const OPTIONS list_options[] = {
@@ -591,6 +590,8 @@ const OPTIONS list_options[] = {
      "List of message digest commands"},
     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
      "List of message digest algorithms"},
+    {"kdf-algorithms", OPT_KDF_ALGORITHMS, '-',
+     "List of key derivation and pseudo random function algorithms"},
     {"mac-algorithms", OPT_MAC_ALGORITHMS, '-',
      "List of message authentication code algorithms"},
     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
@@ -622,6 +623,7 @@ int list_main(int argc, char **argv)
         unsigned int commands:1;
         unsigned int digest_commands:1;
         unsigned int digest_algorithms:1;
+        unsigned int kdf_algorithms:1;
         unsigned int mac_algorithms:1;
         unsigned int cipher_commands:1;
         unsigned int cipher_algorithms:1;
@@ -659,6 +661,9 @@ opthelp:
         case OPT_DIGEST_ALGORITHMS:
             todo.digest_algorithms = 1;
             break;
+        case OPT_KDF_ALGORITHMS:
+            todo.kdf_algorithms = 1;
+            break;
         case OPT_MAC_ALGORITHMS:
             todo.mac_algorithms = 1;
             break;
@@ -706,10 +711,10 @@ opthelp:
         list_type(FT_md, one);
     if (todo.digest_algorithms)
         list_digests();
-#if 0                            /* Temporarly disabled */
+    if (todo.kdf_algorithms)
+        list_kdfs();
     if (todo.mac_algorithms)
-        EVP_MAC_do_all_sorted(list_mac_fn, bio_out);
-#endif
+        list_macs();
     if (todo.cipher_commands)
         list_type(FT_cipher, one);
     if (todo.cipher_algorithms)