]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_ssl/lib/sslMemory.c
Security-59754.60.13.tar.gz
[apple/security.git] / OSX / libsecurity_ssl / lib / sslMemory.c
1 /*
2 * Copyright (c) 1999-2001,2005-2007,2010-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * sslMemory.c - Memory allocator implementation
26 */
27
28 /* THIS FILE CONTAINS KERNEL CODE */
29
30 #include "sslMemory.h"
31 #include "sslDebug.h"
32
33 #include <string.h> /* memset */
34 #include <AssertMacros.h>
35
36 // MARK: -
37 // MARK: Basic low-level malloc/free
38
39 /*
40 * For now, all allocs/frees go thru here.
41 */
42
43 #ifdef KERNEL
44
45 /* BSD Malloc */
46 #include <sys/malloc.h>
47 #include <IOKit/IOLib.h>
48 #include <libkern/libkern.h>
49
50 /* Define this for debugging sslMalloc and sslFree */
51 //#define SSL_CANARIS
52
53 void *
54 sslMalloc(size_t length)
55 {
56 void *p;
57
58 #ifdef SSL_CANARIS
59 length+=8;
60 #endif
61
62 p = _MALLOC(length, M_TEMP, M_WAITOK);
63 check(p);
64
65 if(p==NULL)
66 return p;
67
68 #ifdef SSL_CANARIS
69 *(uint32_t *)p=(uint32_t)length-8;
70 printf("sslMalloc @%p of 0x%08lx bytes\n", p, length-8);
71 *(uint32_t *)(p+length-4)=0xdeadbeed;
72 p+=4;
73 #endif
74
75 return p;
76 }
77
78 void
79 sslFree(void *p)
80 {
81 if(p != NULL) {
82
83 #ifdef SSL_CANARIS
84 p=p-4;
85 uint32_t len=*(uint32_t *)p;
86 uint32_t marker=*(uint32_t *)(p+4+len);
87 printf("sslFree @%p len=0x%08x\n", p, len);
88 if(marker!=0xdeadbeef)
89 panic("Buffer overflow in SSL!\n");
90 #endif
91
92 _FREE(p, M_TEMP);
93 }
94 }
95
96 #else
97
98 #include <stdlib.h>
99
100 void *
101 sslMalloc(size_t length)
102 {
103 return malloc(length);
104 }
105
106 void
107 sslFree(void *p)
108 {
109 if(p != NULL) {
110 free(p);
111 }
112 }
113
114 #endif
115
116 // MARK: -
117 // MARK: SSLBuffer-level alloc/free
118
119 int SSLAllocBuffer(
120 SSLBuffer *buf,
121 size_t length)
122 {
123 buf->data = (uint8_t *)sslMalloc(length);
124 if(buf->data == NULL) {
125 sslErrorLog("SSLAllocBuffer: NULL buf!\n");
126 check(0);
127 buf->length = 0;
128 return -1;
129 }
130 buf->length = length;
131 return 0;
132 }
133
134 int
135 SSLFreeBuffer(SSLBuffer *buf)
136 {
137 if(buf == NULL) {
138 sslErrorLog("SSLFreeBuffer: NULL buf!\n");
139 check(0);
140 return -1;
141 }
142 sslFree(buf->data);
143 buf->data = NULL;
144 buf->length = 0;
145 return 0;
146 }
147
148 uint8_t *sslAllocCopy(
149 const uint8_t *src,
150 size_t len)
151 {
152 uint8_t *dst;
153
154 dst = (uint8_t *)sslMalloc(len);
155 if(dst == NULL) {
156 return NULL;
157 }
158 memmove(dst, src, len);
159 return dst;
160 }
161
162 int SSLCopyBufferFromData(
163 const void *src,
164 size_t len,
165 SSLBuffer *dst) // data mallocd and returned
166 {
167 dst->data = sslAllocCopy((const uint8_t *)src, len);
168 if(dst->data == NULL) {
169 sslErrorLog("SSLCopyBufferFromData: NULL buf!\n");
170 check(0);
171 return -1;
172 }
173 dst->length = len;
174 return 0;
175 }
176
177 int SSLCopyBuffer(
178 const SSLBuffer *src,
179 SSLBuffer *dst) // data mallocd and returned
180 {
181 return SSLCopyBufferFromData(src->data, src->length, dst);
182 }
183