OPENSSL_secure_malloc.pod 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. =pod
  2. =head1 NAME
  3. CRYPTO_secure_malloc_init, CRYPTO_secure_malloc_initialized,
  4. CRYPTO_secure_malloc_done, OPENSSL_secure_malloc, CRYPTO_secure_malloc,
  5. OPENSSL_secure_zalloc, CRYPTO_secure_zalloc, OPENSSL_secure_free,
  6. CRYPTO_secure_free, OPENSSL_secure_clear_free,
  7. CRYPTO_secure_clear_free, OPENSSL_secure_actual_size,
  8. CRYPTO_secure_allocated,
  9. CRYPTO_secure_used - secure heap storage
  10. =head1 SYNOPSIS
  11. #include <openssl/crypto.h>
  12. int CRYPTO_secure_malloc_init(size_t size, size_t minsize);
  13. int CRYPTO_secure_malloc_initialized();
  14. int CRYPTO_secure_malloc_done();
  15. void *OPENSSL_secure_malloc(size_t num);
  16. void *CRYPTO_secure_malloc(size_t num, const char *file, int line);
  17. void *OPENSSL_secure_zalloc(size_t num);
  18. void *CRYPTO_secure_zalloc(size_t num, const char *file, int line);
  19. void OPENSSL_secure_free(void* ptr);
  20. void CRYPTO_secure_free(void *ptr, const char *, int);
  21. void OPENSSL_secure_clear_free(void* ptr, size_t num);
  22. void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *, int);
  23. size_t OPENSSL_secure_actual_size(const void *ptr);
  24. int CRYPTO_secure_allocated(const void *ptr);
  25. size_t CRYPTO_secure_used();
  26. =head1 DESCRIPTION
  27. In order to help protect applications (particularly long-running servers)
  28. from pointer overruns or underruns that could return arbitrary data from
  29. the program's dynamic memory area, where keys and other sensitive
  30. information might be stored, OpenSSL supports the concept of a "secure heap."
  31. The level and type of security guarantees depend on the operating system.
  32. It is a good idea to review the code and see if it addresses your
  33. threat model and concerns. It should be noted that the secure heap
  34. uses a single read/write lock, and therefore any operations
  35. that involve allocation or freeing of secure heap memory are serialised,
  36. blocking other threads. With that in mind, highly concurrent applications
  37. should enable the secure heap with caution and be aware of the performance
  38. implications for multi-threaded code.
  39. If a secure heap is used, then private key B<BIGNUM> values are stored there.
  40. This protects long-term storage of private keys, but will not necessarily
  41. put all intermediate values and computations there.
  42. CRYPTO_secure_malloc_init() creates the secure heap, with the specified
  43. C<size> in bytes. The C<minsize> parameter is the minimum size to
  44. allocate from the heap or zero to use a reasonable default value.
  45. Both C<size> and, if specified, C<minsize> must be a power of two and
  46. C<minsize> should generally be small, for example 16 or 32.
  47. C<minsize> must be less than a quarter of C<size> in any case.
  48. CRYPTO_secure_malloc_initialized() indicates whether or not the secure
  49. heap as been initialized and is available.
  50. CRYPTO_secure_malloc_done() releases the heap and makes the memory unavailable
  51. to the process if all secure memory has been freed.
  52. It can take noticeably long to complete.
  53. OPENSSL_secure_malloc() allocates C<num> bytes from the heap.
  54. If CRYPTO_secure_malloc_init() is not called, this is equivalent to
  55. calling OPENSSL_malloc().
  56. It is a macro that expands to
  57. CRYPTO_secure_malloc() and adds the C<__FILE__> and C<__LINE__> parameters.
  58. OPENSSL_secure_zalloc() and CRYPTO_secure_zalloc() are like
  59. OPENSSL_secure_malloc() and CRYPTO_secure_malloc(), respectively,
  60. except that they call memset() to zero the memory before returning.
  61. OPENSSL_secure_free() releases the memory at C<ptr> back to the heap.
  62. It must be called with a value previously obtained from
  63. OPENSSL_secure_malloc().
  64. If CRYPTO_secure_malloc_init() is not called, this is equivalent to
  65. calling OPENSSL_free().
  66. It exists for consistency with OPENSSL_secure_malloc() , and
  67. is a macro that expands to CRYPTO_secure_free() and adds the C<__FILE__>
  68. and C<__LINE__> parameters.. If the argument to OPENSSL_secure_free()
  69. is NULL, nothing is done.
  70. OPENSSL_secure_clear_free() is similar to OPENSSL_secure_free() except
  71. that it has an additional C<num> parameter which is used to clear
  72. the memory if it was not allocated from the secure heap.
  73. If CRYPTO_secure_malloc_init() is not called, this is equivalent to
  74. calling OPENSSL_clear_free(). If the argument to OPENSSL_secure_clear_free()
  75. is NULL, nothing is done.
  76. OPENSSL_secure_actual_size() tells the actual size allocated to the
  77. pointer; implementations may allocate more space than initially
  78. requested, in order to "round up" and reduce secure heap fragmentation.
  79. OPENSSL_secure_allocated() tells if a pointer is allocated in the secure heap.
  80. CRYPTO_secure_used() returns the number of bytes allocated in the
  81. secure heap.
  82. =head1 RETURN VALUES
  83. CRYPTO_secure_malloc_init() returns 0 on failure, 1 if successful,
  84. and 2 if successful but the heap could not be protected by memory
  85. mapping.
  86. CRYPTO_secure_malloc_initialized() returns 1 if the secure heap is
  87. available (that is, if CRYPTO_secure_malloc_init() has been called,
  88. but CRYPTO_secure_malloc_done() has not been called or failed) or 0 if not.
  89. OPENSSL_secure_malloc() and OPENSSL_secure_zalloc() return a pointer into
  90. the secure heap of the requested size, or C<NULL> if memory could not be
  91. allocated.
  92. CRYPTO_secure_allocated() returns 1 if the pointer is in the secure heap, or 0 if not.
  93. CRYPTO_secure_malloc_done() returns 1 if the secure memory area is released, or 0 if not.
  94. OPENSSL_secure_free() and OPENSSL_secure_clear_free() return no values.
  95. =head1 SEE ALSO
  96. L<OPENSSL_malloc(3)>,
  97. L<BN_new(3)>
  98. =head1 HISTORY
  99. The OPENSSL_secure_clear_free() function was added in OpenSSL 1.1.0g.
  100. The second argument to CRYPTO_secure_malloc_init() was changed from an B<int> to
  101. a B<size_t> in OpenSSL 3.0.
  102. =head1 COPYRIGHT
  103. Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
  104. Licensed under the Apache License 2.0 (the "License"). You may not use
  105. this file except in compliance with the License. You can obtain a copy
  106. in the file LICENSE in the source distribution or at
  107. L<https://www.openssl.org/source/license.html>.
  108. =cut