app_params.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "apps.h"
  10. #include "app_params.h"
  11. static int describe_param_type(char *buf, size_t bufsz, const OSSL_PARAM *param)
  12. {
  13. const char *type_mod = "";
  14. const char *type = NULL;
  15. int show_type_number = 0;
  16. int printed_len;
  17. switch (param->data_type) {
  18. case OSSL_PARAM_UNSIGNED_INTEGER:
  19. type_mod = "unsigned ";
  20. /* FALLTHRU */
  21. case OSSL_PARAM_INTEGER:
  22. type = "integer";
  23. break;
  24. case OSSL_PARAM_UTF8_PTR:
  25. type_mod = "pointer to a ";
  26. /* FALLTHRU */
  27. case OSSL_PARAM_UTF8_STRING:
  28. type = "UTF8 encoded string";
  29. break;
  30. case OSSL_PARAM_OCTET_PTR:
  31. type_mod = "pointer to an ";
  32. /* FALLTHRU */
  33. case OSSL_PARAM_OCTET_STRING:
  34. type = "octet string";
  35. break;
  36. default:
  37. type = "unknown type";
  38. show_type_number = 1;
  39. break;
  40. }
  41. printed_len = BIO_snprintf(buf, bufsz, "%s: ", param->key);
  42. if (printed_len > 0) {
  43. buf += printed_len;
  44. bufsz -= printed_len;
  45. }
  46. printed_len = BIO_snprintf(buf, bufsz, "%s%s", type_mod, type);
  47. if (printed_len > 0) {
  48. buf += printed_len;
  49. bufsz -= printed_len;
  50. }
  51. if (show_type_number) {
  52. printed_len = BIO_snprintf(buf, bufsz, " [%d]", param->data_type);
  53. if (printed_len > 0) {
  54. buf += printed_len;
  55. bufsz -= printed_len;
  56. }
  57. }
  58. if (param->data_size == 0)
  59. printed_len = BIO_snprintf(buf, bufsz, " (arbitrary size)");
  60. else
  61. printed_len = BIO_snprintf(buf, bufsz, " (max %zu bytes large)",
  62. param->data_size);
  63. if (printed_len > 0) {
  64. buf += printed_len;
  65. bufsz -= printed_len;
  66. }
  67. *buf = '\0';
  68. return 1;
  69. }
  70. int print_param_types(const char *thing, const OSSL_PARAM *pdefs, int indent)
  71. {
  72. if (pdefs == NULL) {
  73. return 1;
  74. } else if (pdefs->key == NULL) {
  75. /*
  76. * An empty list? This shouldn't happen, but let's just make sure to
  77. * say something if there's a badly written provider...
  78. */
  79. BIO_printf(bio_out, "%*sEmpty list of %s (!!!)\n", indent, "", thing);
  80. } else {
  81. BIO_printf(bio_out, "%*s%s:\n", indent, "", thing);
  82. for (; pdefs->key != NULL; pdefs++) {
  83. char buf[200]; /* This should be ample space */
  84. describe_param_type(buf, sizeof(buf), pdefs);
  85. BIO_printf(bio_out, "%*s %s\n", indent, "", buf);
  86. }
  87. }
  88. return 1;
  89. }