defn_cache.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2019 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 "property_lcl.h"
  16. /*
  17. * Implement a property definition cache.
  18. * These functions assume that they are called under a write lock.
  19. * No attempt is made to clean out the cache, except when it is shut down.
  20. */
  21. typedef struct {
  22. const char *prop;
  23. OSSL_PROPERTY_LIST *defn;
  24. char body[1];
  25. } PROPERTY_DEFN_ELEM;
  26. DEFINE_LHASH_OF(PROPERTY_DEFN_ELEM);
  27. static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
  28. {
  29. return OPENSSL_LH_strhash(a->prop);
  30. }
  31. static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
  32. const PROPERTY_DEFN_ELEM *b)
  33. {
  34. return strcmp(a->prop, b->prop);
  35. }
  36. static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
  37. {
  38. ossl_property_free(elem->defn);
  39. OPENSSL_free(elem);
  40. }
  41. static void property_defns_free(void *vproperty_defns)
  42. {
  43. LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
  44. if (property_defns != NULL) {
  45. lh_PROPERTY_DEFN_ELEM_doall(property_defns,
  46. &property_defn_free);
  47. lh_PROPERTY_DEFN_ELEM_free(property_defns);
  48. }
  49. }
  50. static void *property_defns_new(OPENSSL_CTX *ctx) {
  51. return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
  52. }
  53. static const OPENSSL_CTX_METHOD property_defns_method = {
  54. property_defns_new,
  55. property_defns_free,
  56. };
  57. OSSL_PROPERTY_LIST *ossl_prop_defn_get(OPENSSL_CTX *ctx, const char *prop)
  58. {
  59. PROPERTY_DEFN_ELEM elem, *r;
  60. LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
  61. property_defns = openssl_ctx_get_data(ctx, OPENSSL_CTX_PROPERTY_DEFN_INDEX,
  62. &property_defns_method);
  63. if (property_defns == NULL)
  64. return NULL;
  65. elem.prop = prop;
  66. r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
  67. return r != NULL ? r->defn : NULL;
  68. }
  69. int ossl_prop_defn_set(OPENSSL_CTX *ctx, const char *prop,
  70. OSSL_PROPERTY_LIST *pl)
  71. {
  72. PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
  73. size_t len;
  74. LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
  75. property_defns = openssl_ctx_get_data(ctx, OPENSSL_CTX_PROPERTY_DEFN_INDEX,
  76. &property_defns_method);
  77. if (property_defns == NULL)
  78. return 0;
  79. if (prop == NULL)
  80. return 1;
  81. if (pl == NULL) {
  82. elem.prop = prop;
  83. lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
  84. return 1;
  85. }
  86. len = strlen(prop);
  87. p = OPENSSL_malloc(sizeof(*p) + len);
  88. if (p != NULL) {
  89. p->prop = p->body;
  90. p->defn = pl;
  91. memcpy(p->body, prop, len + 1);
  92. old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
  93. if (old != NULL) {
  94. property_defn_free(old);
  95. return 1;
  96. }
  97. if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
  98. return 1;
  99. }
  100. OPENSSL_free(p);
  101. return 0;
  102. }