A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Committer:
Vanger
Date:
Mon Jan 19 21:45:42 2015 +0000
Revision:
0:b86d15c6ba29
Updated CyaSSL Library to 3.3.0. Changed Settings and functions to be implemented for mbed platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vanger 0:b86d15c6ba29 1 /* memory.c
Vanger 0:b86d15c6ba29 2 *
Vanger 0:b86d15c6ba29 3 * Copyright (C) 2006-2014 wolfSSL Inc.
Vanger 0:b86d15c6ba29 4 *
Vanger 0:b86d15c6ba29 5 * This file is part of CyaSSL.
Vanger 0:b86d15c6ba29 6 *
Vanger 0:b86d15c6ba29 7 * CyaSSL is free software; you can redistribute it and/or modify
Vanger 0:b86d15c6ba29 8 * it under the terms of the GNU General Public License as published by
Vanger 0:b86d15c6ba29 9 * the Free Software Foundation; either version 2 of the License, or
Vanger 0:b86d15c6ba29 10 * (at your option) any later version.
Vanger 0:b86d15c6ba29 11 *
Vanger 0:b86d15c6ba29 12 * CyaSSL is distributed in the hope that it will be useful,
Vanger 0:b86d15c6ba29 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Vanger 0:b86d15c6ba29 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Vanger 0:b86d15c6ba29 15 * GNU General Public License for more details.
Vanger 0:b86d15c6ba29 16 *
Vanger 0:b86d15c6ba29 17 * You should have received a copy of the GNU General Public License
Vanger 0:b86d15c6ba29 18 * along with this program; if not, write to the Free Software
Vanger 0:b86d15c6ba29 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Vanger 0:b86d15c6ba29 20 */
Vanger 0:b86d15c6ba29 21
Vanger 0:b86d15c6ba29 22 #ifdef HAVE_CONFIG_H
Vanger 0:b86d15c6ba29 23 #include <config.h>
Vanger 0:b86d15c6ba29 24 #endif
Vanger 0:b86d15c6ba29 25
Vanger 0:b86d15c6ba29 26 #include <cyassl/ctaocrypt/settings.h>
Vanger 0:b86d15c6ba29 27
Vanger 0:b86d15c6ba29 28 #ifdef USE_CYASSL_MEMORY
Vanger 0:b86d15c6ba29 29
Vanger 0:b86d15c6ba29 30 #include <cyassl/ctaocrypt/memory.h>
Vanger 0:b86d15c6ba29 31 #include <cyassl/ctaocrypt/error-crypt.h>
Vanger 0:b86d15c6ba29 32
Vanger 0:b86d15c6ba29 33 #ifdef CYASSL_MALLOC_CHECK
Vanger 0:b86d15c6ba29 34 #include <stdio.h>
Vanger 0:b86d15c6ba29 35 #endif
Vanger 0:b86d15c6ba29 36
Vanger 0:b86d15c6ba29 37 /* Set these to default values initially. */
Vanger 0:b86d15c6ba29 38 static CyaSSL_Malloc_cb malloc_function = 0;
Vanger 0:b86d15c6ba29 39 static CyaSSL_Free_cb free_function = 0;
Vanger 0:b86d15c6ba29 40 static CyaSSL_Realloc_cb realloc_function = 0;
Vanger 0:b86d15c6ba29 41
Vanger 0:b86d15c6ba29 42 int CyaSSL_SetAllocators(CyaSSL_Malloc_cb mf,
Vanger 0:b86d15c6ba29 43 CyaSSL_Free_cb ff,
Vanger 0:b86d15c6ba29 44 CyaSSL_Realloc_cb rf)
Vanger 0:b86d15c6ba29 45 {
Vanger 0:b86d15c6ba29 46 int res = 0;
Vanger 0:b86d15c6ba29 47
Vanger 0:b86d15c6ba29 48 if (mf)
Vanger 0:b86d15c6ba29 49 malloc_function = mf;
Vanger 0:b86d15c6ba29 50 else
Vanger 0:b86d15c6ba29 51 res = BAD_FUNC_ARG;
Vanger 0:b86d15c6ba29 52
Vanger 0:b86d15c6ba29 53 if (ff)
Vanger 0:b86d15c6ba29 54 free_function = ff;
Vanger 0:b86d15c6ba29 55 else
Vanger 0:b86d15c6ba29 56 res = BAD_FUNC_ARG;
Vanger 0:b86d15c6ba29 57
Vanger 0:b86d15c6ba29 58 if (rf)
Vanger 0:b86d15c6ba29 59 realloc_function = rf;
Vanger 0:b86d15c6ba29 60 else
Vanger 0:b86d15c6ba29 61 res = BAD_FUNC_ARG;
Vanger 0:b86d15c6ba29 62
Vanger 0:b86d15c6ba29 63 return res;
Vanger 0:b86d15c6ba29 64 }
Vanger 0:b86d15c6ba29 65
Vanger 0:b86d15c6ba29 66
Vanger 0:b86d15c6ba29 67 void* CyaSSL_Malloc(size_t size)
Vanger 0:b86d15c6ba29 68 {
Vanger 0:b86d15c6ba29 69 void* res = 0;
Vanger 0:b86d15c6ba29 70
Vanger 0:b86d15c6ba29 71 if (malloc_function)
Vanger 0:b86d15c6ba29 72 res = malloc_function(size);
Vanger 0:b86d15c6ba29 73 else
Vanger 0:b86d15c6ba29 74 res = malloc(size);
Vanger 0:b86d15c6ba29 75
Vanger 0:b86d15c6ba29 76 #ifdef CYASSL_MALLOC_CHECK
Vanger 0:b86d15c6ba29 77 if (res == NULL)
Vanger 0:b86d15c6ba29 78 puts("CyaSSL_malloc failed");
Vanger 0:b86d15c6ba29 79 #endif
Vanger 0:b86d15c6ba29 80
Vanger 0:b86d15c6ba29 81 return res;
Vanger 0:b86d15c6ba29 82 }
Vanger 0:b86d15c6ba29 83
Vanger 0:b86d15c6ba29 84 void CyaSSL_Free(void *ptr)
Vanger 0:b86d15c6ba29 85 {
Vanger 0:b86d15c6ba29 86 if (free_function)
Vanger 0:b86d15c6ba29 87 free_function(ptr);
Vanger 0:b86d15c6ba29 88 else
Vanger 0:b86d15c6ba29 89 free(ptr);
Vanger 0:b86d15c6ba29 90 }
Vanger 0:b86d15c6ba29 91
Vanger 0:b86d15c6ba29 92 void* CyaSSL_Realloc(void *ptr, size_t size)
Vanger 0:b86d15c6ba29 93 {
Vanger 0:b86d15c6ba29 94 void* res = 0;
Vanger 0:b86d15c6ba29 95
Vanger 0:b86d15c6ba29 96 if (realloc_function)
Vanger 0:b86d15c6ba29 97 res = realloc_function(ptr, size);
Vanger 0:b86d15c6ba29 98 else
Vanger 0:b86d15c6ba29 99 res = realloc(ptr, size);
Vanger 0:b86d15c6ba29 100
Vanger 0:b86d15c6ba29 101 return res;
Vanger 0:b86d15c6ba29 102 }
Vanger 0:b86d15c6ba29 103
Vanger 0:b86d15c6ba29 104 #endif /* USE_CYASSL_MEMORY */
Vanger 0:b86d15c6ba29 105
Vanger 0:b86d15c6ba29 106
Vanger 0:b86d15c6ba29 107 #ifdef HAVE_IO_POOL
Vanger 0:b86d15c6ba29 108
Vanger 0:b86d15c6ba29 109 /* Example for user io pool, shared build may need definitions in lib proper */
Vanger 0:b86d15c6ba29 110
Vanger 0:b86d15c6ba29 111 #include <cyassl/ctaocrypt/types.h>
Vanger 0:b86d15c6ba29 112 #include <stdlib.h>
Vanger 0:b86d15c6ba29 113
Vanger 0:b86d15c6ba29 114 #ifndef HAVE_THREAD_LS
Vanger 0:b86d15c6ba29 115 #error "Oops, simple I/O pool example needs thread local storage"
Vanger 0:b86d15c6ba29 116 #endif
Vanger 0:b86d15c6ba29 117
Vanger 0:b86d15c6ba29 118
Vanger 0:b86d15c6ba29 119 /* allow simple per thread in and out pools */
Vanger 0:b86d15c6ba29 120 /* use 17k size sense max record size is 16k plus overhead */
Vanger 0:b86d15c6ba29 121 static THREAD_LS_T byte pool_in[17*1024];
Vanger 0:b86d15c6ba29 122 static THREAD_LS_T byte pool_out[17*1024];
Vanger 0:b86d15c6ba29 123
Vanger 0:b86d15c6ba29 124
Vanger 0:b86d15c6ba29 125 void* XMALLOC(size_t n, void* heap, int type)
Vanger 0:b86d15c6ba29 126 {
Vanger 0:b86d15c6ba29 127 (void)heap;
Vanger 0:b86d15c6ba29 128
Vanger 0:b86d15c6ba29 129 if (type == DYNAMIC_TYPE_IN_BUFFER) {
Vanger 0:b86d15c6ba29 130 if (n < sizeof(pool_in))
Vanger 0:b86d15c6ba29 131 return pool_in;
Vanger 0:b86d15c6ba29 132 else
Vanger 0:b86d15c6ba29 133 return NULL;
Vanger 0:b86d15c6ba29 134 }
Vanger 0:b86d15c6ba29 135
Vanger 0:b86d15c6ba29 136 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
Vanger 0:b86d15c6ba29 137 if (n < sizeof(pool_out))
Vanger 0:b86d15c6ba29 138 return pool_out;
Vanger 0:b86d15c6ba29 139 else
Vanger 0:b86d15c6ba29 140 return NULL;
Vanger 0:b86d15c6ba29 141 }
Vanger 0:b86d15c6ba29 142
Vanger 0:b86d15c6ba29 143 return malloc(n);
Vanger 0:b86d15c6ba29 144 }
Vanger 0:b86d15c6ba29 145
Vanger 0:b86d15c6ba29 146 void* XREALLOC(void *p, size_t n, void* heap, int type)
Vanger 0:b86d15c6ba29 147 {
Vanger 0:b86d15c6ba29 148 (void)heap;
Vanger 0:b86d15c6ba29 149
Vanger 0:b86d15c6ba29 150 if (type == DYNAMIC_TYPE_IN_BUFFER) {
Vanger 0:b86d15c6ba29 151 if (n < sizeof(pool_in))
Vanger 0:b86d15c6ba29 152 return pool_in;
Vanger 0:b86d15c6ba29 153 else
Vanger 0:b86d15c6ba29 154 return NULL;
Vanger 0:b86d15c6ba29 155 }
Vanger 0:b86d15c6ba29 156
Vanger 0:b86d15c6ba29 157 if (type == DYNAMIC_TYPE_OUT_BUFFER) {
Vanger 0:b86d15c6ba29 158 if (n < sizeof(pool_out))
Vanger 0:b86d15c6ba29 159 return pool_out;
Vanger 0:b86d15c6ba29 160 else
Vanger 0:b86d15c6ba29 161 return NULL;
Vanger 0:b86d15c6ba29 162 }
Vanger 0:b86d15c6ba29 163
Vanger 0:b86d15c6ba29 164 return realloc(p, n);
Vanger 0:b86d15c6ba29 165 }
Vanger 0:b86d15c6ba29 166
Vanger 0:b86d15c6ba29 167
Vanger 0:b86d15c6ba29 168 /* unit api calls, let's make sure visisble with CYASSL_API */
Vanger 0:b86d15c6ba29 169 CYASSL_API void XFREE(void *p, void* heap, int type)
Vanger 0:b86d15c6ba29 170 {
Vanger 0:b86d15c6ba29 171 (void)heap;
Vanger 0:b86d15c6ba29 172
Vanger 0:b86d15c6ba29 173 if (type == DYNAMIC_TYPE_IN_BUFFER)
Vanger 0:b86d15c6ba29 174 return; /* do nothing, static pool */
Vanger 0:b86d15c6ba29 175
Vanger 0:b86d15c6ba29 176 if (type == DYNAMIC_TYPE_OUT_BUFFER)
Vanger 0:b86d15c6ba29 177 return; /* do nothing, static pool */
Vanger 0:b86d15c6ba29 178
Vanger 0:b86d15c6ba29 179 free(p);
Vanger 0:b86d15c6ba29 180 }
Vanger 0:b86d15c6ba29 181
Vanger 0:b86d15c6ba29 182 #endif /* HAVE_IO_POOL */
Vanger 0:b86d15c6ba29 183