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 */ 28 typedef struct stack_st_tag { 29 size_t num; 30 void **data; 31 } _STACK; 32 33 sk_GENERAL_NAME_value(const struct stack_st_GENERAL_NAME * sk,size_t i)34GENERAL_NAME *sk_GENERAL_NAME_value(const struct stack_st_GENERAL_NAME *sk, size_t i) 35 { 36 if (!sk || i >= ((_STACK*)sk)->num) { 37 return NULL; 38 } 39 return ((_STACK*)sk)->data[i]; 40 } 41 42 sk_GENERAL_NAME_num(const struct stack_st_GENERAL_NAME * sk)43size_t sk_GENERAL_NAME_num(const struct stack_st_GENERAL_NAME *sk) 44 { 45 if (sk == NULL) { 46 return 0; 47 } 48 return ((_STACK*)sk)->num; 49 } 50 OPENSSL_sk_new(OPENSSL_sk_compfunc c)51OPENSSL_STACK* OPENSSL_sk_new(OPENSSL_sk_compfunc c) 52 { 53 OPENSSL_STACK *stack; 54 char **data; 55 56 stack = ssl_mem_zalloc(sizeof(OPENSSL_STACK)); 57 if (!stack) { 58 SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (stack)"); 59 goto no_mem1; 60 } 61 62 data = ssl_mem_zalloc(sizeof(*data) * MIN_NODES); 63 if (!data) { 64 SSL_DEBUG(SSL_STACK_ERROR_LEVEL, "no enough memory > (data)"); 65 goto no_mem2; 66 } 67 68 stack->data = data; 69 stack->num_alloc = MIN_NODES; 70 stack->c = c; 71 72 return stack; 73 74 no_mem2: 75 ssl_mem_free(stack); 76 no_mem1: 77 return NULL; 78 } 79 80 /** 81 * @brief create a NULL function openssl stack object 82 */ OPENSSL_sk_new_null(void)83OPENSSL_STACK *OPENSSL_sk_new_null(void) 84 { 85 return OPENSSL_sk_new((OPENSSL_sk_compfunc)NULL); 86 } 87 88 /** 89 * @brief free openssl stack object 90 */ OPENSSL_sk_free(OPENSSL_STACK * stack)91void OPENSSL_sk_free(OPENSSL_STACK *stack) 92 { 93 SSL_ASSERT3(stack); 94 95 ssl_mem_free(stack->data); 96 ssl_mem_free(stack); 97 } 98