defn_cache.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 (!ossl_assert(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. if (r == NULL || !ossl_assert(r->defn != NULL))
  67. return NULL;
  68. return r->defn;
  69. }
  70. /*
  71. * Cache the property list for a given property string *pl.
  72. * If an entry already exists in the cache *pl is freed and
  73. * overwritten with the existing entry from the cache.
  74. */
  75. int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
  76. OSSL_PROPERTY_LIST **pl)
  77. {
  78. PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
  79. size_t len;
  80. LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
  81. int res = 1;
  82. property_defns = ossl_lib_ctx_get_data(ctx,
  83. OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
  84. if (property_defns == NULL)
  85. return 0;
  86. if (prop == NULL)
  87. return 1;
  88. if (!ossl_lib_ctx_write_lock(ctx))
  89. return 0;
  90. elem.prop = prop;
  91. if (pl == NULL) {
  92. lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
  93. goto end;
  94. }
  95. /* check if property definition is in the cache already */
  96. if ((p = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem)) != NULL) {
  97. ossl_property_free(*pl);
  98. *pl = p->defn;
  99. goto end;
  100. }
  101. len = strlen(prop);
  102. p = OPENSSL_malloc(sizeof(*p) + len);
  103. if (p != NULL) {
  104. p->prop = p->body;
  105. p->defn = *pl;
  106. memcpy(p->body, prop, len + 1);
  107. old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
  108. if (!ossl_assert(old == NULL))
  109. /* This should not happen. An existing entry is handled above. */
  110. goto end;
  111. if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
  112. goto end;
  113. }
  114. OPENSSL_free(p);
  115. res = 0;
  116. end:
  117. ossl_lib_ctx_unlock(ctx);
  118. return res;
  119. }