app_params.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2019-2020 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. }
  90. void print_param_value(const OSSL_PARAM *p, int indent)
  91. {
  92. int64_t i;
  93. uint64_t u;
  94. printf("%*s%s: ", indent, "", p->key);
  95. switch (p->data_type) {
  96. case OSSL_PARAM_UNSIGNED_INTEGER:
  97. if (OSSL_PARAM_get_uint64(p, &u))
  98. BIO_printf(bio_out, "%llu\n", (unsigned long long int)u);
  99. else
  100. BIO_printf(bio_out, "error getting value\n");
  101. break;
  102. case OSSL_PARAM_INTEGER:
  103. if (OSSL_PARAM_get_int64(p, &i))
  104. BIO_printf(bio_out, "%lld\n", (long long int)i);
  105. else
  106. BIO_printf(bio_out, "error getting value\n");
  107. break;
  108. case OSSL_PARAM_UTF8_PTR:
  109. BIO_printf(bio_out, "'%s'\n", *(char **)(p->data));
  110. break;
  111. case OSSL_PARAM_UTF8_STRING:
  112. BIO_printf(bio_out, "'%s'\n", (char *)p->data);
  113. break;
  114. case OSSL_PARAM_OCTET_PTR:
  115. case OSSL_PARAM_OCTET_STRING:
  116. BIO_printf(bio_out, "<%zu bytes>\n", p->data_size);
  117. break;
  118. default:
  119. BIO_printf(bio_out, "unknown type (%u) of %zu bytes\n",
  120. p->data_type, p->data_size);
  121. break;
  122. }
  123. }