p_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /*
  10. * This is a very simple provider that does absolutely nothing except respond
  11. * to provider global parameter requests. It does this by simply echoing back
  12. * a parameter request it makes to the loading library.
  13. */
  14. #include <string.h>
  15. #include <stdio.h>
  16. /*
  17. * When built as an object file to link the application with, we get the
  18. * init function name through the macro PROVIDER_INIT_FUNCTION_NAME. If
  19. * not defined, we use the standard init function name for the shared
  20. * object form.
  21. */
  22. #ifdef PROVIDER_INIT_FUNCTION_NAME
  23. # define OSSL_provider_init PROVIDER_INIT_FUNCTION_NAME
  24. #endif
  25. #include <openssl/core.h>
  26. #include <openssl/core_dispatch.h>
  27. static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
  28. static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
  29. /* Tell the core what params we provide and what type they are */
  30. static const OSSL_PARAM p_param_types[] = {
  31. { "greeting", OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
  32. { NULL, 0, NULL, 0, 0 }
  33. };
  34. /* This is a trick to ensure we define the provider functions correctly */
  35. static OSSL_FUNC_provider_gettable_params_fn p_gettable_params;
  36. static OSSL_FUNC_provider_get_params_fn p_get_params;
  37. static OSSL_FUNC_provider_get_reason_strings_fn p_get_reason_strings;
  38. static const OSSL_PARAM *p_gettable_params(void *_)
  39. {
  40. return p_param_types;
  41. }
  42. static int p_get_params(void *vhand, OSSL_PARAM params[])
  43. {
  44. const OSSL_CORE_HANDLE *hand = vhand;
  45. OSSL_PARAM *p = params;
  46. int ok = 1;
  47. for (; ok && p->key != NULL; p++) {
  48. if (strcmp(p->key, "greeting") == 0) {
  49. static char *opensslv;
  50. static char *provname;
  51. static char *greeting;
  52. static OSSL_PARAM counter_request[] = {
  53. /* Known libcrypto provided parameters */
  54. { "openssl-version", OSSL_PARAM_UTF8_PTR,
  55. &opensslv, sizeof(&opensslv), 0 },
  56. { "provider-name", OSSL_PARAM_UTF8_PTR,
  57. &provname, sizeof(&provname), 0},
  58. /* This might be present, if there's such a configuration */
  59. { "greeting", OSSL_PARAM_UTF8_PTR,
  60. &greeting, sizeof(&greeting), 0 },
  61. { NULL, 0, NULL, 0, 0 }
  62. };
  63. char buf[256];
  64. size_t buf_l;
  65. opensslv = provname = greeting = NULL;
  66. if (c_get_params(hand, counter_request)) {
  67. if (greeting) {
  68. strcpy(buf, greeting);
  69. } else {
  70. const char *versionp = *(void **)counter_request[0].data;
  71. const char *namep = *(void **)counter_request[1].data;
  72. sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
  73. versionp, namep);
  74. }
  75. } else {
  76. sprintf(buf, "Howdy stranger...");
  77. }
  78. p->return_size = buf_l = strlen(buf) + 1;
  79. if (p->data_size >= buf_l)
  80. strcpy(p->data, buf);
  81. else
  82. ok = 0;
  83. }
  84. }
  85. return ok;
  86. }
  87. static const OSSL_ITEM *p_get_reason_strings(void *_)
  88. {
  89. static const OSSL_ITEM reason_strings[] = {
  90. {1, "dummy reason string"},
  91. {0, NULL}
  92. };
  93. return reason_strings;
  94. }
  95. static const OSSL_DISPATCH p_test_table[] = {
  96. { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))p_gettable_params },
  97. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
  98. { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS,
  99. (void (*)(void))p_get_reason_strings},
  100. { 0, NULL }
  101. };
  102. int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
  103. const OSSL_DISPATCH *in,
  104. const OSSL_DISPATCH **out,
  105. void **provctx)
  106. {
  107. for (; in->function_id != 0; in++) {
  108. switch (in->function_id) {
  109. case OSSL_FUNC_CORE_GETTABLE_PARAMS:
  110. c_gettable_params = OSSL_FUNC_core_gettable_params(in);
  111. break;
  112. case OSSL_FUNC_CORE_GET_PARAMS:
  113. c_get_params = OSSL_FUNC_core_get_params(in);
  114. break;
  115. default:
  116. /* Just ignore anything we don't understand */
  117. break;
  118. }
  119. }
  120. /* Because we use this in get_params, we need to pass it back */
  121. *provctx = (void *)handle;
  122. *out = p_test_table;
  123. return 1;
  124. }