evp_fetch.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include <stddef.h>
  10. #include <openssl/ossl_typ.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/core.h>
  13. #include "internal/cryptlib.h"
  14. #include "internal/thread_once.h"
  15. #include "internal/asn1_int.h"
  16. #include "internal/property.h"
  17. #include "internal/core.h"
  18. #include "internal/evp_int.h" /* evp_locl.h needs it */
  19. #include "evp_locl.h"
  20. static void default_method_store_free(void *vstore)
  21. {
  22. ossl_method_store_free(vstore);
  23. }
  24. static void *default_method_store_new(OPENSSL_CTX *ctx)
  25. {
  26. return ossl_method_store_new(ctx);
  27. }
  28. static const OPENSSL_CTX_METHOD default_method_store_method = {
  29. default_method_store_new,
  30. default_method_store_free,
  31. };
  32. /* Data to be passed through ossl_method_construct() */
  33. struct method_data_st {
  34. const char *name;
  35. int nid;
  36. OSSL_METHOD_CONSTRUCT_METHOD *mcm;
  37. void *(*method_from_dispatch)(int nid, const OSSL_DISPATCH *,
  38. OSSL_PROVIDER *);
  39. int (*refcnt_up_method)(void *method);
  40. void (*destruct_method)(void *method);
  41. int (*nid_method)(void *method);
  42. };
  43. /*
  44. * Generic routines to fetch / create EVP methods with ossl_method_construct()
  45. */
  46. static void *alloc_tmp_method_store(OPENSSL_CTX *ctx)
  47. {
  48. return ossl_method_store_new(ctx);
  49. }
  50. static void dealloc_tmp_method_store(void *store)
  51. {
  52. if (store != NULL)
  53. ossl_method_store_free(store);
  54. }
  55. static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
  56. {
  57. return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX,
  58. &default_method_store_method);
  59. }
  60. static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
  61. const char *propquery, void *data)
  62. {
  63. struct method_data_st *methdata = data;
  64. void *method = NULL;
  65. if (store == NULL
  66. && (store = get_default_method_store(libctx)) == NULL)
  67. return NULL;
  68. (void)ossl_method_store_fetch(store, methdata->nid, propquery, &method);
  69. if (method != NULL
  70. && !methdata->refcnt_up_method(method)) {
  71. method = NULL;
  72. }
  73. return method;
  74. }
  75. static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
  76. const char *propdef,
  77. void *method, void *data)
  78. {
  79. struct method_data_st *methdata = data;
  80. int nid = methdata->nid_method(method);
  81. if (nid == NID_undef)
  82. return 0;
  83. if (store == NULL
  84. && (store = get_default_method_store(libctx)) == NULL)
  85. return 0;
  86. if (methdata->refcnt_up_method(method)
  87. && ossl_method_store_add(store, nid, propdef, method,
  88. methdata->destruct_method))
  89. return 1;
  90. return 0;
  91. }
  92. static void *construct_method(const char *algorithm_name,
  93. const OSSL_DISPATCH *fns, OSSL_PROVIDER *prov,
  94. void *data)
  95. {
  96. struct method_data_st *methdata = data;
  97. void *method = NULL;
  98. int nid = OBJ_sn2nid(algorithm_name);
  99. if (nid == NID_undef) {
  100. /* Create a new NID for that name on the fly */
  101. ASN1_OBJECT tmpobj;
  102. /* This is the same as OBJ_create() but without requiring a OID */
  103. tmpobj.nid = OBJ_new_nid(1);
  104. tmpobj.sn = tmpobj.ln = methdata->name;
  105. tmpobj.flags = ASN1_OBJECT_FLAG_DYNAMIC;
  106. tmpobj.length = 0;
  107. tmpobj.data = NULL;
  108. nid = OBJ_add_object(&tmpobj);
  109. }
  110. if (nid == NID_undef)
  111. return NULL;
  112. method = methdata->method_from_dispatch(nid, fns, prov);
  113. if (method == NULL)
  114. return NULL;
  115. return method;
  116. }
  117. static void destruct_method(void *method, void *data)
  118. {
  119. struct method_data_st *methdata = data;
  120. methdata->destruct_method(method);
  121. }
  122. void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
  123. const char *algorithm, const char *properties,
  124. void *(*new_method)(int nid, const OSSL_DISPATCH *fns,
  125. OSSL_PROVIDER *prov),
  126. int (*upref_method)(void *),
  127. void (*free_method)(void *),
  128. int (*nid_method)(void *))
  129. {
  130. OSSL_METHOD_STORE *store = get_default_method_store(libctx);
  131. int nid = OBJ_sn2nid(algorithm);
  132. void *method = NULL;
  133. if (store == NULL)
  134. return NULL;
  135. if (nid == NID_undef
  136. || !ossl_method_store_cache_get(store, nid, properties, &method)) {
  137. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  138. alloc_tmp_method_store,
  139. dealloc_tmp_method_store,
  140. get_method_from_store,
  141. put_method_in_store,
  142. construct_method,
  143. destruct_method
  144. };
  145. struct method_data_st mcmdata;
  146. mcmdata.nid = nid;
  147. mcmdata.mcm = &mcm;
  148. mcmdata.method_from_dispatch = new_method;
  149. mcmdata.destruct_method = free_method;
  150. mcmdata.refcnt_up_method = upref_method;
  151. mcmdata.destruct_method = free_method;
  152. mcmdata.nid_method = nid_method;
  153. method = ossl_method_construct(libctx, operation_id, algorithm,
  154. properties, 0 /* !force_cache */,
  155. &mcm, &mcmdata);
  156. ossl_method_store_cache_set(store, nid, properties, method);
  157. } else {
  158. upref_method(method);
  159. }
  160. return method;
  161. }
  162. int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
  163. {
  164. OSSL_METHOD_STORE *store = get_default_method_store(libctx);
  165. if (store != NULL)
  166. return ossl_method_store_set_global_properties(store, propq);
  167. EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
  168. return 0;
  169. }