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 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "ssl_stack.h" 16 #include "ssl_dbg.h" 17 #include "ssl_port.h" 18 19 #ifndef CONFIG_MIN_NODES 20 #define MIN_NODES 4 21 #else 22 #define MIN_NODES CONFIG_MIN_NODES 23 #endif 24 25 /** 26 * @brief create a openssl stack object 27 */ OPENSSL_sk_new(OPENSSL_sk_compfunc c)28OPENSSL_STACK* OPENSSL_sk_new(OPENSSL_sk_compfunc c) 29 { 30 OPENSSL_STACK *stack; 31 char **data; 32 33 stack = ssl_mem_zalloc(sizeof(OPENSSL_STACK)); 34 if (!stack) { 35 SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (stack)"); 36 goto no_mem1; 37 } 38 39 data = ssl_mem_zalloc(sizeof(*data) * MIN_NODES); 40 if (!data) { 41 SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (data)"); 42 goto no_mem2; 43 } 44 45 stack->data = data; 46 stack->num_alloc = MIN_NODES; 47 stack->c = c; 48 49 return stack; 50 51 no_mem2: 52 ssl_mem_free(stack); 53 no_mem1: 54 return NULL; 55 } 56 57 /** 58 * @brief create a NULL function openssl stack object 59 */ OPENSSL_sk_new_null(void)60OPENSSL_STACK *OPENSSL_sk_new_null(void) 61 { 62 return OPENSSL_sk_new((OPENSSL_sk_compfunc)NULL); 63 } 64 65 /** 66 * @brief free openssl stack object 67 */ OPENSSL_sk_free(OPENSSL_STACK * stack)68void OPENSSL_sk_free(OPENSSL_STACK *stack) 69 { 70 SSL_ASSERT3(stack); 71 72 ssl_mem_free(stack->data); 73 ssl_mem_free(stack); 74 } 75