context.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "internal/cryptlib.h"
  10. #include "internal/thread_once.h"
  11. struct openssl_ctx_st {
  12. CRYPTO_RWLOCK *lock;
  13. CRYPTO_EX_DATA data;
  14. };
  15. static OPENSSL_CTX default_context;
  16. static int context_init(OPENSSL_CTX *ctx)
  17. {
  18. return (ctx->lock = CRYPTO_THREAD_lock_new()) != NULL
  19. && CRYPTO_new_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
  20. &ctx->data);
  21. }
  22. static int context_deinit(OPENSSL_CTX *ctx)
  23. {
  24. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
  25. CRYPTO_THREAD_lock_free(ctx->lock);
  26. return 1;
  27. }
  28. static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
  29. static void do_default_context_deinit(void)
  30. {
  31. context_deinit(&default_context);
  32. }
  33. DEFINE_RUN_ONCE_STATIC(do_default_context_init)
  34. {
  35. return OPENSSL_init_crypto(0, NULL)
  36. && context_init(&default_context)
  37. && OPENSSL_atexit(do_default_context_deinit);
  38. }
  39. OPENSSL_CTX *OPENSSL_CTX_new(void)
  40. {
  41. OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  42. if (ctx != NULL && !context_init(ctx)) {
  43. OPENSSL_CTX_free(ctx);
  44. ctx = NULL;
  45. }
  46. return ctx;
  47. }
  48. void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
  49. {
  50. if (ctx != NULL)
  51. context_deinit(ctx);
  52. OPENSSL_free(ctx);
  53. }
  54. static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
  55. CRYPTO_EX_DATA *ad, int index,
  56. long argl_ign, void *argp)
  57. {
  58. const OPENSSL_CTX_METHOD *meth = argp;
  59. void *ptr = meth->new_func();
  60. if (ptr != NULL)
  61. CRYPTO_set_ex_data(ad, index, ptr);
  62. }
  63. static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
  64. CRYPTO_EX_DATA *ad, int index,
  65. long argl_ign, void *argp)
  66. {
  67. const OPENSSL_CTX_METHOD *meth = argp;
  68. meth->free_func(ptr);
  69. }
  70. int openssl_ctx_new_index(const OPENSSL_CTX_METHOD *meth)
  71. {
  72. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_OPENSSL_CTX, 0, (void *)meth,
  73. openssl_ctx_generic_new, NULL,
  74. openssl_ctx_generic_free);
  75. }
  76. void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index)
  77. {
  78. void *data = NULL;
  79. if (ctx == NULL) {
  80. if (!RUN_ONCE(&default_context_init, do_default_context_init))
  81. return 0;
  82. ctx = &default_context;
  83. }
  84. CRYPTO_THREAD_read_lock(ctx->lock);
  85. /* The alloc call ensures there's a value there */
  86. if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
  87. &ctx->data, index))
  88. data = CRYPTO_get_ex_data(&ctx->data, index);
  89. CRYPTO_THREAD_unlock(ctx->lock);
  90. return data;
  91. }