p_legacy.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 1995-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. /*
  10. * Legacy EVP_PKEY assign/set/get APIs are deprecated for public use, but
  11. * still ok for internal use, particularly in providers.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/types.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/err.h>
  17. #include <openssl/rsa.h>
  18. #include <openssl/ec.h>
  19. #include "crypto/types.h"
  20. #include "crypto/evp.h"
  21. #include "evp_local.h"
  22. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  23. {
  24. int ret = EVP_PKEY_assign_RSA(pkey, key);
  25. if (ret)
  26. RSA_up_ref(key);
  27. return ret;
  28. }
  29. RSA *evp_pkey_get0_RSA_int(const EVP_PKEY *pkey)
  30. {
  31. if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
  32. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_RSA_KEY);
  33. return NULL;
  34. }
  35. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  36. }
  37. const RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
  38. {
  39. return evp_pkey_get0_RSA_int(pkey);
  40. }
  41. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  42. {
  43. RSA *ret = evp_pkey_get0_RSA_int(pkey);
  44. if (ret != NULL)
  45. RSA_up_ref(ret);
  46. return ret;
  47. }
  48. #ifndef OPENSSL_NO_EC
  49. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  50. {
  51. if (!EC_KEY_up_ref(key))
  52. return 0;
  53. if (!EVP_PKEY_assign_EC_KEY(pkey, key)) {
  54. EC_KEY_free(key);
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. EC_KEY *evp_pkey_get0_EC_KEY_int(const EVP_PKEY *pkey)
  60. {
  61. if (EVP_PKEY_get_base_id(pkey) != EVP_PKEY_EC) {
  62. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_EC_KEY);
  63. return NULL;
  64. }
  65. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  66. }
  67. const EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
  68. {
  69. return evp_pkey_get0_EC_KEY_int(pkey);
  70. }
  71. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  72. {
  73. EC_KEY *ret = evp_pkey_get0_EC_KEY_int(pkey);
  74. if (ret != NULL && !EC_KEY_up_ref(ret))
  75. ret = NULL;
  76. return ret;
  77. }
  78. #endif /* OPENSSL_NO_EC */