evp_utils.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2019-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. /* Internal EVP utility functions */
  10. #include <openssl/core.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/err.h>
  13. #include <openssl/asn1.h> /* evp_local.h needs it */
  14. #include <openssl/safestack.h> /* evp_local.h needs it */
  15. #include "crypto/evp.h" /* evp_local.h needs it */
  16. #include "evp_local.h"
  17. /*
  18. * EVP_CTRL_RET_UNSUPPORTED = -1 is the returned value from any ctrl function
  19. * where the control command isn't supported, and an alternative code path
  20. * may be chosen.
  21. * Since these functions are used to implement ctrl functionality, we
  22. * use the same value, and other callers will have to compensate.
  23. */
  24. #define PARAM_CHECK(obj, func, errfunc) \
  25. if (obj == NULL) \
  26. return 0; \
  27. if (obj->prov == NULL) \
  28. return EVP_CTRL_RET_UNSUPPORTED; \
  29. if (obj->func == NULL) { \
  30. errfunc(); \
  31. return 0; \
  32. }
  33. #define PARAM_FUNC(name, func, type, err) \
  34. int name (const type *obj, OSSL_PARAM params[]) \
  35. { \
  36. PARAM_CHECK(obj, func, err) \
  37. return obj->func(params); \
  38. }
  39. #define PARAM_CTX_FUNC(name, func, type, err) \
  40. int name (const type *obj, void *algctx, OSSL_PARAM params[]) \
  41. { \
  42. PARAM_CHECK(obj, func, err) \
  43. return obj->func(algctx, params); \
  44. }
  45. #define PARAM_FUNCTIONS(type, \
  46. getname, getfunc, \
  47. getctxname, getctxfunc, \
  48. setctxname, setctxfunc) \
  49. PARAM_FUNC(getname, getfunc, type, geterr) \
  50. PARAM_CTX_FUNC(getctxname, getctxfunc, type, geterr) \
  51. PARAM_CTX_FUNC(setctxname, setctxfunc, type, seterr)
  52. /*
  53. * These error functions are a workaround for the error scripts, which
  54. * currently require that XXXerr method appears inside a function (not a macro).
  55. */
  56. static void geterr(void)
  57. {
  58. ERR_raise(ERR_LIB_EVP, EVP_R_CANNOT_GET_PARAMETERS);
  59. }
  60. static void seterr(void)
  61. {
  62. ERR_raise(ERR_LIB_EVP, EVP_R_CANNOT_SET_PARAMETERS);
  63. }
  64. PARAM_FUNCTIONS(EVP_CIPHER,
  65. evp_do_ciph_getparams, get_params,
  66. evp_do_ciph_ctx_getparams, get_ctx_params,
  67. evp_do_ciph_ctx_setparams, set_ctx_params)
  68. PARAM_FUNCTIONS(EVP_MD,
  69. evp_do_md_getparams, get_params,
  70. evp_do_md_ctx_getparams, get_ctx_params,
  71. evp_do_md_ctx_setparams, set_ctx_params)