user_property_test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2021 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 <openssl/core.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/provider.h>
  13. #include <openssl/crypto.h>
  14. #include <openssl/evp.h>
  15. #include "testutil.h"
  16. #define MYPROPERTIES "foo.bar=yes"
  17. static OSSL_FUNC_provider_query_operation_fn testprov_query;
  18. static OSSL_FUNC_digest_get_params_fn tmpmd_get_params;
  19. static OSSL_FUNC_digest_digest_fn tmpmd_digest;
  20. static int tmpmd_get_params(OSSL_PARAM params[])
  21. {
  22. OSSL_PARAM *p = NULL;
  23. p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE);
  24. if (p != NULL && !OSSL_PARAM_set_size_t(p, 1))
  25. return 0;
  26. p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);
  27. if (p != NULL && !OSSL_PARAM_set_size_t(p, 1))
  28. return 0;
  29. return 1;
  30. }
  31. static int tmpmd_digest(void *provctx, const unsigned char *in, size_t inl,
  32. unsigned char *out, size_t *outl, size_t outsz)
  33. {
  34. return 0;
  35. }
  36. static const OSSL_DISPATCH testprovmd_functions[] = {
  37. { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))tmpmd_get_params },
  38. { OSSL_FUNC_DIGEST_DIGEST, (void (*)(void))tmpmd_digest },
  39. { 0, NULL }
  40. };
  41. static const OSSL_ALGORITHM testprov_digests[] = {
  42. { "testprovmd", MYPROPERTIES, testprovmd_functions },
  43. { NULL, NULL, NULL }
  44. };
  45. static const OSSL_ALGORITHM *testprov_query(void *provctx,
  46. int operation_id,
  47. int *no_cache)
  48. {
  49. *no_cache = 0;
  50. return operation_id == OSSL_OP_DIGEST ? testprov_digests : NULL;
  51. }
  52. static const OSSL_DISPATCH testprov_dispatch_table[] = {
  53. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))testprov_query },
  54. { 0, NULL }
  55. };
  56. static int testprov_provider_init(const OSSL_CORE_HANDLE *handle,
  57. const OSSL_DISPATCH *in,
  58. const OSSL_DISPATCH **out,
  59. void **provctx)
  60. {
  61. *provctx = (void *)handle;
  62. *out = testprov_dispatch_table;
  63. return 1;
  64. }
  65. enum {
  66. DEFAULT_PROPS_FIRST = 0,
  67. DEFAULT_PROPS_AFTER_LOAD,
  68. DEFAULT_PROPS_AFTER_FETCH,
  69. DEFAULT_PROPS_FINAL
  70. };
  71. static int test_default_props_and_providers(int propsorder)
  72. {
  73. OSSL_LIB_CTX *libctx;
  74. OSSL_PROVIDER *testprov = NULL;
  75. EVP_MD *testprovmd = NULL;
  76. int res = 0;
  77. if (!TEST_ptr(libctx = OSSL_LIB_CTX_new())
  78. || !TEST_true(OSSL_PROVIDER_add_builtin(libctx, "testprov",
  79. testprov_provider_init)))
  80. goto err;
  81. if (propsorder == DEFAULT_PROPS_FIRST
  82. && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES)))
  83. goto err;
  84. if (!TEST_ptr(testprov = OSSL_PROVIDER_load(libctx, "testprov")))
  85. goto err;
  86. if (propsorder == DEFAULT_PROPS_AFTER_LOAD
  87. && !TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES)))
  88. goto err;
  89. if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL)))
  90. goto err;
  91. if (propsorder == DEFAULT_PROPS_AFTER_FETCH) {
  92. if (!TEST_true(EVP_set_default_properties(libctx, MYPROPERTIES)))
  93. goto err;
  94. EVP_MD_free(testprovmd);
  95. if (!TEST_ptr(testprovmd = EVP_MD_fetch(libctx, "testprovmd", NULL)))
  96. goto err;
  97. }
  98. res = 1;
  99. err:
  100. EVP_MD_free(testprovmd);
  101. OSSL_PROVIDER_unload(testprov);
  102. OSSL_LIB_CTX_free(libctx);
  103. return res;
  104. }
  105. int setup_tests(void)
  106. {
  107. ADD_ALL_TESTS(test_default_props_and_providers, DEFAULT_PROPS_FINAL);
  108. return 1;
  109. }