p_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /*
  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_numbers.h>
  27. static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
  28. static OSSL_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_ITEM p_param_types[] = {
  31. { OSSL_PARAM_UTF8_STRING, "greeting" },
  32. { 0, NULL }
  33. };
  34. /* This is a trick to ensure we define the provider functions correctly */
  35. static OSSL_provider_get_param_types_fn p_get_param_types;
  36. static OSSL_provider_get_params_fn p_get_params;
  37. static const OSSL_ITEM *p_get_param_types(void *_)
  38. {
  39. return p_param_types;
  40. }
  41. static int p_get_params(void *vprov, OSSL_PARAM params[])
  42. {
  43. const OSSL_PROVIDER *prov = vprov;
  44. OSSL_PARAM *p = params;
  45. int ok = 1;
  46. for (; ok && p->key != NULL; p++) {
  47. if (strcmp(p->key, "greeting") == 0) {
  48. static char *opensslv;
  49. static char *provname;
  50. static char *greeting;
  51. static OSSL_PARAM counter_request[] = {
  52. /* Known libcrypto provided parameters */
  53. { "openssl-version", OSSL_PARAM_UTF8_PTR,
  54. &opensslv, sizeof(&opensslv), 0 },
  55. { "provider-name", OSSL_PARAM_UTF8_PTR,
  56. &provname, sizeof(&provname), 0},
  57. /* This might be present, if there's such a configuration */
  58. { "greeting", OSSL_PARAM_UTF8_PTR,
  59. &greeting, sizeof(&greeting), 0 },
  60. { NULL, 0, NULL, 0, 0 }
  61. };
  62. char buf[256];
  63. size_t buf_l;
  64. opensslv = provname = greeting = NULL;
  65. if (c_get_params(prov, counter_request)) {
  66. if (greeting) {
  67. strcpy(buf, greeting);
  68. } else {
  69. const char *versionp = *(void **)counter_request[0].data;
  70. const char *namep = *(void **)counter_request[1].data;
  71. sprintf(buf, "Hello OpenSSL %.20s, greetings from %s!",
  72. versionp, namep);
  73. }
  74. } else {
  75. sprintf(buf, "Howdy stranger...");
  76. }
  77. p->return_size = buf_l = strlen(buf) + 1;
  78. if (p->data_size >= buf_l)
  79. strncpy(p->data, buf, buf_l);
  80. else
  81. ok = 0;
  82. }
  83. }
  84. return ok;
  85. }
  86. static const OSSL_DISPATCH p_test_table[] = {
  87. { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))p_get_param_types },
  88. { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))p_get_params },
  89. { 0, NULL }
  90. };
  91. int OSSL_provider_init(const OSSL_PROVIDER *provider,
  92. const OSSL_DISPATCH *in,
  93. const OSSL_DISPATCH **out,
  94. void **provctx)
  95. {
  96. for (; in->function_id != 0; in++) {
  97. switch (in->function_id) {
  98. case OSSL_FUNC_CORE_GET_PARAM_TYPES:
  99. c_get_param_types = OSSL_get_core_get_param_types(in);
  100. break;
  101. case OSSL_FUNC_CORE_GET_PARAMS:
  102. c_get_params = OSSL_get_core_get_params(in);
  103. break;
  104. default:
  105. /* Just ignore anything we don't understand */
  106. break;
  107. }
  108. }
  109. /* Because we use this in get_params, we need to pass it back */
  110. *provctx = (void *)provider;
  111. *out = p_test_table;
  112. return 1;
  113. }