ssl_stack.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "ssl_stack.h"
  14. #include "ssl_dbg.h"
  15. #include "ssl_port.h"
  16. #ifndef CONFIG_MIN_NODES
  17. #define MIN_NODES 4
  18. #else
  19. #define MIN_NODES CONFIG_MIN_NODES
  20. #endif
  21. /**
  22. * @brief create a openssl stack object
  23. */
  24. OPENSSL_STACK* OPENSSL_sk_new(OPENSSL_sk_compfunc c)
  25. {
  26. OPENSSL_STACK *stack;
  27. char **data;
  28. stack = ssl_mem_zalloc(sizeof(OPENSSL_STACK));
  29. if (!stack) {
  30. SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (stack)");
  31. goto no_mem1;
  32. }
  33. data = ssl_mem_zalloc(sizeof(*data) * MIN_NODES);
  34. if (!data) {
  35. SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (data)");
  36. goto no_mem2;
  37. }
  38. stack->data = data;
  39. stack->num_alloc = MIN_NODES;
  40. stack->c = c;
  41. return stack;
  42. no_mem2:
  43. ssl_mem_free(stack);
  44. no_mem1:
  45. return NULL;
  46. }
  47. /**
  48. * @brief create a NULL function openssl stack object
  49. */
  50. OPENSSL_STACK *OPENSSL_sk_new_null(void)
  51. {
  52. return OPENSSL_sk_new((OPENSSL_sk_compfunc)NULL);
  53. }
  54. /**
  55. * @brief free openssl stack object
  56. */
  57. void OPENSSL_sk_free(OPENSSL_STACK *stack)
  58. {
  59. SSL_ASSERT3(stack);
  60. ssl_mem_free(stack->data);
  61. ssl_mem_free(stack);
  62. }