• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2016, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/pool.h>
16 
17 #include <assert.h>
18 #include <string.h>
19 
20 #include <openssl/bytestring.h>
21 #include <openssl/mem.h>
22 #include <openssl/thread.h>
23 
24 #include "../internal.h"
25 #include "internal.h"
26 
27 
DEFINE_LHASH_OF(CRYPTO_BUFFER)28 DEFINE_LHASH_OF(CRYPTO_BUFFER)
29 
30 static uint32_t CRYPTO_BUFFER_hash(const CRYPTO_BUFFER *buf) {
31   return OPENSSL_hash32(buf->data, buf->len);
32 }
33 
CRYPTO_BUFFER_cmp(const CRYPTO_BUFFER * a,const CRYPTO_BUFFER * b)34 static int CRYPTO_BUFFER_cmp(const CRYPTO_BUFFER *a, const CRYPTO_BUFFER *b) {
35   if (a->len != b->len) {
36     return 1;
37   }
38   return OPENSSL_memcmp(a->data, b->data, a->len);
39 }
40 
CRYPTO_BUFFER_POOL_new(void)41 CRYPTO_BUFFER_POOL* CRYPTO_BUFFER_POOL_new(void) {
42   CRYPTO_BUFFER_POOL *pool = OPENSSL_malloc(sizeof(CRYPTO_BUFFER_POOL));
43   if (pool == NULL) {
44     return NULL;
45   }
46 
47   OPENSSL_memset(pool, 0, sizeof(CRYPTO_BUFFER_POOL));
48   pool->bufs = lh_CRYPTO_BUFFER_new(CRYPTO_BUFFER_hash, CRYPTO_BUFFER_cmp);
49   if (pool->bufs == NULL) {
50     OPENSSL_free(pool);
51     return NULL;
52   }
53 
54   CRYPTO_MUTEX_init(&pool->lock);
55 
56   return pool;
57 }
58 
CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL * pool)59 void CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL *pool) {
60   if (pool == NULL) {
61     return;
62   }
63 
64 #if !defined(NDEBUG)
65   CRYPTO_MUTEX_lock_write(&pool->lock);
66   assert(lh_CRYPTO_BUFFER_num_items(pool->bufs) == 0);
67   CRYPTO_MUTEX_unlock_write(&pool->lock);
68 #endif
69 
70   lh_CRYPTO_BUFFER_free(pool->bufs);
71   CRYPTO_MUTEX_cleanup(&pool->lock);
72   OPENSSL_free(pool);
73 }
74 
CRYPTO_BUFFER_new(const uint8_t * data,size_t len,CRYPTO_BUFFER_POOL * pool)75 CRYPTO_BUFFER *CRYPTO_BUFFER_new(const uint8_t *data, size_t len,
76                                  CRYPTO_BUFFER_POOL *pool) {
77   if (pool != NULL) {
78     CRYPTO_BUFFER tmp;
79     tmp.data = (uint8_t *) data;
80     tmp.len = len;
81 
82     CRYPTO_MUTEX_lock_read(&pool->lock);
83     CRYPTO_BUFFER *const duplicate =
84         lh_CRYPTO_BUFFER_retrieve(pool->bufs, &tmp);
85     if (duplicate != NULL) {
86       CRYPTO_refcount_inc(&duplicate->references);
87     }
88     CRYPTO_MUTEX_unlock_read(&pool->lock);
89 
90     if (duplicate != NULL) {
91       return duplicate;
92     }
93   }
94 
95   CRYPTO_BUFFER *const buf = OPENSSL_malloc(sizeof(CRYPTO_BUFFER));
96   if (buf == NULL) {
97     return NULL;
98   }
99   OPENSSL_memset(buf, 0, sizeof(CRYPTO_BUFFER));
100 
101   buf->data = OPENSSL_memdup(data, len);
102   if (len != 0 && buf->data == NULL) {
103     OPENSSL_free(buf);
104     return NULL;
105   }
106 
107   buf->len = len;
108   buf->references = 1;
109 
110   if (pool == NULL) {
111     return buf;
112   }
113 
114   buf->pool = pool;
115 
116   CRYPTO_MUTEX_lock_write(&pool->lock);
117   CRYPTO_BUFFER *duplicate = lh_CRYPTO_BUFFER_retrieve(pool->bufs, buf);
118   int inserted = 0;
119   if (duplicate == NULL) {
120     CRYPTO_BUFFER *old = NULL;
121     inserted = lh_CRYPTO_BUFFER_insert(pool->bufs, &old, buf);
122     assert(old == NULL);
123   } else {
124     CRYPTO_refcount_inc(&duplicate->references);
125   }
126   CRYPTO_MUTEX_unlock_write(&pool->lock);
127 
128   if (!inserted) {
129     // We raced to insert |buf| into the pool and lost, or else there was an
130     // error inserting.
131     OPENSSL_free(buf->data);
132     OPENSSL_free(buf);
133     return duplicate;
134   }
135 
136   return buf;
137 }
138 
CRYPTO_BUFFER_alloc(uint8_t ** out_data,size_t len)139 CRYPTO_BUFFER *CRYPTO_BUFFER_alloc(uint8_t **out_data, size_t len) {
140   CRYPTO_BUFFER *const buf = OPENSSL_malloc(sizeof(CRYPTO_BUFFER));
141   if (buf == NULL) {
142     return NULL;
143   }
144   OPENSSL_memset(buf, 0, sizeof(CRYPTO_BUFFER));
145 
146   buf->data = OPENSSL_malloc(len);
147   if (len != 0 && buf->data == NULL) {
148     OPENSSL_free(buf);
149     return NULL;
150   }
151   buf->len = len;
152   buf->references = 1;
153 
154   *out_data = buf->data;
155   return buf;
156 }
157 
CRYPTO_BUFFER_new_from_CBS(CBS * cbs,CRYPTO_BUFFER_POOL * pool)158 CRYPTO_BUFFER* CRYPTO_BUFFER_new_from_CBS(CBS *cbs, CRYPTO_BUFFER_POOL *pool) {
159   return CRYPTO_BUFFER_new(CBS_data(cbs), CBS_len(cbs), pool);
160 }
161 
CRYPTO_BUFFER_free(CRYPTO_BUFFER * buf)162 void CRYPTO_BUFFER_free(CRYPTO_BUFFER *buf) {
163   if (buf == NULL) {
164     return;
165   }
166 
167   CRYPTO_BUFFER_POOL *const pool = buf->pool;
168   if (pool == NULL) {
169     if (CRYPTO_refcount_dec_and_test_zero(&buf->references)) {
170       // If a reference count of zero is observed, there cannot be a reference
171       // from any pool to this buffer and thus we are able to free this
172       // buffer.
173       OPENSSL_free(buf->data);
174       OPENSSL_free(buf);
175     }
176 
177     return;
178   }
179 
180   CRYPTO_MUTEX_lock_write(&pool->lock);
181   if (!CRYPTO_refcount_dec_and_test_zero(&buf->references)) {
182     CRYPTO_MUTEX_unlock_write(&buf->pool->lock);
183     return;
184   }
185 
186   // We have an exclusive lock on the pool, therefore no concurrent lookups can
187   // find this buffer and increment the reference count. Thus, if the count is
188   // zero there are and can never be any more references and thus we can free
189   // this buffer.
190   void *found = lh_CRYPTO_BUFFER_delete(pool->bufs, buf);
191   assert(found != NULL);
192   assert(found == buf);
193   (void)found;
194   CRYPTO_MUTEX_unlock_write(&buf->pool->lock);
195   OPENSSL_free(buf->data);
196   OPENSSL_free(buf);
197 }
198 
CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER * buf)199 int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf) {
200   // This is safe in the case that |buf->pool| is NULL because it's just
201   // standard reference counting in that case.
202   //
203   // This is also safe if |buf->pool| is non-NULL because, if it were racing
204   // with |CRYPTO_BUFFER_free| then the two callers must have independent
205   // references already and so the reference count will never hit zero.
206   CRYPTO_refcount_inc(&buf->references);
207   return 1;
208 }
209 
CRYPTO_BUFFER_data(const CRYPTO_BUFFER * buf)210 const uint8_t *CRYPTO_BUFFER_data(const CRYPTO_BUFFER *buf) {
211   return buf->data;
212 }
213 
CRYPTO_BUFFER_len(const CRYPTO_BUFFER * buf)214 size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER *buf) {
215   return buf->len;
216 }
217 
CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER * buf,CBS * out)218 void CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER *buf, CBS *out) {
219   CBS_init(out, buf->data, buf->len);
220 }
221