defn_cache.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. #include <openssl/lhash.h>
  13. #include "internal/propertyerr.h"
  14. #include "internal/property.h"
  15. #include "internal/core.h"
  16. #include "property_local.h"
  17. #include "crypto/context.h"
  18. /*
  19. * Implement a property definition cache.
  20. * These functions assume that they are called under a write lock.
  21. * No attempt is made to clean out the cache, except when it is shut down.
  22. */
  23. typedef struct {
  24. const char *prop;
  25. OSSL_PROPERTY_LIST *defn;
  26. char body[1];
  27. } PROPERTY_DEFN_ELEM;
  28. DEFINE_LHASH_OF_EX(PROPERTY_DEFN_ELEM);
  29. static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
  30. {
  31. return OPENSSL_LH_strhash(a->prop);
  32. }
  33. static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
  34. const PROPERTY_DEFN_ELEM *b)
  35. {
  36. return strcmp(a->prop, b->prop);
  37. }
  38. static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
  39. {
  40. ossl_property_free(elem->defn);
  41. OPENSSL_free(elem);
  42. }
  43. void ossl_property_defns_free(void *vproperty_defns)
  44. {
  45. LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
  46. if (property_defns != NULL) {
  47. lh_PROPERTY_DEFN_ELEM_doall(property_defns,
  48. &property_defn_free);
  49. lh_PROPERTY_DEFN_ELEM_free(property_defns);
  50. }
  51. }
  52. void *ossl_property_defns_new(OSSL_LIB_CTX *ctx) {
  53. return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
  54. }
  55. OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop)
  56. {
  57. PROPERTY_DEFN_ELEM elem, *r;
  58. LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
  59. property_defns = ossl_lib_ctx_get_data(ctx,
  60. OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
  61. if (property_defns == NULL || !ossl_lib_ctx_read_lock(ctx))
  62. return NULL;
  63. elem.prop = prop;
  64. r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
  65. ossl_lib_ctx_unlock(ctx);
  66. return r != NULL ? r->defn : NULL;
  67. }
  68. /*
  69. * Cache the property list for a given property string. Callers of this function
  70. * should call ossl_prop_defn_get first to ensure that there is no existing
  71. * cache entry for this property string.
  72. */
  73. int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
  74. OSSL_PROPERTY_LIST *pl)
  75. {
  76. PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
  77. size_t len;
  78. LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
  79. int res = 1;
  80. property_defns = ossl_lib_ctx_get_data(ctx,
  81. OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
  82. if (property_defns == NULL)
  83. return 0;
  84. if (prop == NULL)
  85. return 1;
  86. if (!ossl_lib_ctx_write_lock(ctx))
  87. return 0;
  88. if (pl == NULL) {
  89. elem.prop = prop;
  90. lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
  91. goto end;
  92. }
  93. len = strlen(prop);
  94. p = OPENSSL_malloc(sizeof(*p) + len);
  95. if (p != NULL) {
  96. p->prop = p->body;
  97. p->defn = pl;
  98. memcpy(p->body, prop, len + 1);
  99. old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
  100. if (!ossl_assert(old == NULL)) {
  101. /*
  102. * This should not happen. Any caller of ossl_prop_defn_set should
  103. * have called ossl_prop_defn_get first - so we should know that
  104. * there is no existing entry. If we get here we have a bug. We
  105. * deliberately leak the |old| reference in order to avoid a crash
  106. * if there are any existing users of it.
  107. */
  108. goto end;
  109. }
  110. if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
  111. goto end;
  112. }
  113. OPENSSL_free(p);
  114. res = 0;
  115. end:
  116. ossl_lib_ctx_unlock(ctx);
  117. return res;
  118. }