| 1 |
// Copyright (c) 2016, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/pool.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/bytestring.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
#include <openssl/rand.h> |
| 12 |
#include <openssl/siphash.h> |
| 13 |
#include <openssl/thread.h> |
| 14 |
|
| 15 |
#include "../internal.h" |
| 16 |
#include "internal.h" |
| 17 |
|
| 18 |
|
| 19 |
static uint32_t CRYPTO_BUFFER_hash(const CRYPTO_BUFFER *buf) { |
| 20 |
return (uint32_t)SIPHASH_24(buf->pool->hash_key, buf->data, buf->len); |
| 21 |
} |
| 22 |
|
| 23 |
static int CRYPTO_BUFFER_cmp(const CRYPTO_BUFFER *a, const CRYPTO_BUFFER *b) { |
| 24 |
// Only |CRYPTO_BUFFER|s from the same pool have compatible hashes. |
| 25 |
assert(a->pool != NULL); |
| 26 |
assert(a->pool == b->pool); |
| 27 |
if (a->len != b->len) { |
| 28 |
return 1; |
| 29 |
} |
| 30 |
return OPENSSL_memcmp(a->data, b->data, a->len); |
| 31 |
} |
| 32 |
|
| 33 |
CRYPTO_BUFFER_POOL* CRYPTO_BUFFER_POOL_new(void) { |
| 34 |
CRYPTO_BUFFER_POOL *pool = OPENSSL_zalloc(sizeof(CRYPTO_BUFFER_POOL)); |
| 35 |
if (pool == NULL) { |
| 36 |
return NULL; |
| 37 |
} |
| 38 |
|
| 39 |
pool->bufs = lh_CRYPTO_BUFFER_new(CRYPTO_BUFFER_hash, CRYPTO_BUFFER_cmp); |
| 40 |
if (pool->bufs == NULL) { |
| 41 |
OPENSSL_free(pool); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
|
| 45 |
CRYPTO_MUTEX_init(&pool->lock); |
| 46 |
AWSLC_ABORT_IF_NOT_ONE(RAND_bytes((uint8_t *)&pool->hash_key, sizeof(pool->hash_key))); |
| 47 |
|
| 48 |
return pool; |
| 49 |
} |
| 50 |
|
| 51 |
void CRYPTO_BUFFER_POOL_free(CRYPTO_BUFFER_POOL *pool) { |
| 52 |
if (pool == NULL) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
#if !defined(NDEBUG) |
| 57 |
CRYPTO_MUTEX_lock_write(&pool->lock); |
| 58 |
assert(lh_CRYPTO_BUFFER_num_items(pool->bufs) == 0); |
| 59 |
CRYPTO_MUTEX_unlock_write(&pool->lock); |
| 60 |
#endif |
| 61 |
|
| 62 |
lh_CRYPTO_BUFFER_free(pool->bufs); |
| 63 |
CRYPTO_MUTEX_cleanup(&pool->lock); |
| 64 |
OPENSSL_free(pool); |
| 65 |
} |
| 66 |
|
| 67 |
static void crypto_buffer_free_object(CRYPTO_BUFFER *buf) { |
| 68 |
if (!buf->data_is_static) { |
| 69 |
OPENSSL_free(buf->data); |
| 70 |
} |
| 71 |
OPENSSL_free(buf); |
| 72 |
} |
| 73 |
|
| 74 |
static CRYPTO_BUFFER *crypto_buffer_new(const uint8_t *data, size_t len, |
| 75 |
int data_is_static, |
| 76 |
CRYPTO_BUFFER_POOL *pool) { |
| 77 |
if (pool != NULL) { |
| 78 |
CRYPTO_BUFFER tmp; |
| 79 |
tmp.data = (uint8_t *) data; |
| 80 |
tmp.len = len; |
| 81 |
tmp.pool = pool; |
| 82 |
|
| 83 |
CRYPTO_MUTEX_lock_read(&pool->lock); |
| 84 |
CRYPTO_BUFFER *duplicate = lh_CRYPTO_BUFFER_retrieve(pool->bufs, &tmp); |
| 85 |
if (data_is_static && duplicate != NULL && !duplicate->data_is_static) { |
| 86 |
// If the new |CRYPTO_BUFFER| would have static data, but the duplicate |
| 87 |
// does not, we replace the old one with the new static version. |
| 88 |
duplicate = NULL; |
| 89 |
} |
| 90 |
if (duplicate != NULL) { |
| 91 |
CRYPTO_refcount_inc(&duplicate->references); |
| 92 |
} |
| 93 |
CRYPTO_MUTEX_unlock_read(&pool->lock); |
| 94 |
|
| 95 |
if (duplicate != NULL) { |
| 96 |
return duplicate; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
CRYPTO_BUFFER *const buf = OPENSSL_zalloc(sizeof(CRYPTO_BUFFER)); |
| 101 |
if (buf == NULL) { |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
if (data_is_static) { |
| 106 |
buf->data = (uint8_t *)data; |
| 107 |
buf->data_is_static = 1; |
| 108 |
} else { |
| 109 |
buf->data = OPENSSL_memdup(data, len); |
| 110 |
if (len != 0 && buf->data == NULL) { |
| 111 |
OPENSSL_free(buf); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
buf->len = len; |
| 117 |
buf->references = 1; |
| 118 |
|
| 119 |
if (pool == NULL) { |
| 120 |
return buf; |
| 121 |
} |
| 122 |
|
| 123 |
buf->pool = pool; |
| 124 |
|
| 125 |
CRYPTO_MUTEX_lock_write(&pool->lock); |
| 126 |
CRYPTO_BUFFER *duplicate = lh_CRYPTO_BUFFER_retrieve(pool->bufs, buf); |
| 127 |
if (data_is_static && duplicate != NULL && !duplicate->data_is_static) { |
| 128 |
// If the new |CRYPTO_BUFFER| would have static data, but the duplicate does |
| 129 |
// not, we replace the old one with the new static version. |
| 130 |
duplicate = NULL; |
| 131 |
} |
| 132 |
int inserted = 0; |
| 133 |
if (duplicate == NULL) { |
| 134 |
CRYPTO_BUFFER *old = NULL; |
| 135 |
inserted = lh_CRYPTO_BUFFER_insert(pool->bufs, &old, buf); |
| 136 |
// |old| may be non-NULL if a match was found but ignored. |pool->bufs| does |
| 137 |
// not increment refcounts, so there is no need to clean up after the |
| 138 |
// replacement. |
| 139 |
} else { |
| 140 |
CRYPTO_refcount_inc(&duplicate->references); |
| 141 |
} |
| 142 |
CRYPTO_MUTEX_unlock_write(&pool->lock); |
| 143 |
|
| 144 |
if (!inserted) { |
| 145 |
// We raced to insert |buf| into the pool and lost, or else there was an |
| 146 |
// error inserting. |
| 147 |
crypto_buffer_free_object(buf); |
| 148 |
return duplicate; |
| 149 |
} |
| 150 |
|
| 151 |
return buf; |
| 152 |
} |
| 153 |
|
| 154 |
CRYPTO_BUFFER *CRYPTO_BUFFER_new(const uint8_t *data, size_t len, |
| 155 |
CRYPTO_BUFFER_POOL *pool) { |
| 156 |
return crypto_buffer_new(data, len, /*data_is_static=*/0, pool); |
| 157 |
} |
| 158 |
|
| 159 |
CRYPTO_BUFFER *CRYPTO_BUFFER_alloc(uint8_t **out_data, size_t len) { |
| 160 |
CRYPTO_BUFFER *const buf = OPENSSL_zalloc(sizeof(CRYPTO_BUFFER)); |
| 161 |
if (buf == NULL) { |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
buf->data = OPENSSL_malloc(len); |
| 166 |
if (len != 0 && buf->data == NULL) { |
| 167 |
OPENSSL_free(buf); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
buf->len = len; |
| 171 |
buf->references = 1; |
| 172 |
|
| 173 |
*out_data = buf->data; |
| 174 |
return buf; |
| 175 |
} |
| 176 |
|
| 177 |
CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_CBS(const CBS *cbs, |
| 178 |
CRYPTO_BUFFER_POOL *pool) { |
| 179 |
return CRYPTO_BUFFER_new(CBS_data(cbs), CBS_len(cbs), pool); |
| 180 |
} |
| 181 |
|
| 182 |
CRYPTO_BUFFER *CRYPTO_BUFFER_new_from_static_data_unsafe( |
| 183 |
const uint8_t *data, size_t len, CRYPTO_BUFFER_POOL *pool) { |
| 184 |
return crypto_buffer_new(data, len, /*data_is_static=*/1, pool); |
| 185 |
} |
| 186 |
|
| 187 |
void CRYPTO_BUFFER_free(CRYPTO_BUFFER *buf) { |
| 188 |
if (buf == NULL) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
CRYPTO_BUFFER_POOL *const pool = buf->pool; |
| 193 |
if (pool == NULL) { |
| 194 |
if (CRYPTO_refcount_dec_and_test_zero(&buf->references)) { |
| 195 |
// If a reference count of zero is observed, there cannot be a reference |
| 196 |
// from any pool to this buffer and thus we are able to free this |
| 197 |
// buffer. |
| 198 |
crypto_buffer_free_object(buf); |
| 199 |
} |
| 200 |
|
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
CRYPTO_MUTEX_lock_write(&pool->lock); |
| 205 |
if (!CRYPTO_refcount_dec_and_test_zero(&buf->references)) { |
| 206 |
CRYPTO_MUTEX_unlock_write(&buf->pool->lock); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
// We have an exclusive lock on the pool, therefore no concurrent lookups can |
| 211 |
// find this buffer and increment the reference count. Thus, if the count is |
| 212 |
// zero there are and can never be any more references and thus we can free |
| 213 |
// this buffer. |
| 214 |
// |
| 215 |
// Note it is possible |buf| is no longer in the pool, if it was replaced by a |
| 216 |
// static version. If that static version was since removed, it is even |
| 217 |
// possible for |found| to be NULL. |
| 218 |
CRYPTO_BUFFER *found = lh_CRYPTO_BUFFER_retrieve(pool->bufs, buf); |
| 219 |
if (found == buf) { |
| 220 |
found = lh_CRYPTO_BUFFER_delete(pool->bufs, buf); |
| 221 |
assert(found == buf); |
| 222 |
(void)found; |
| 223 |
} |
| 224 |
|
| 225 |
CRYPTO_MUTEX_unlock_write(&buf->pool->lock); |
| 226 |
crypto_buffer_free_object(buf); |
| 227 |
} |
| 228 |
|
| 229 |
int CRYPTO_BUFFER_up_ref(CRYPTO_BUFFER *buf) { |
| 230 |
// This is safe in the case that |buf->pool| is NULL because it's just |
| 231 |
// standard reference counting in that case. |
| 232 |
// |
| 233 |
// This is also safe if |buf->pool| is non-NULL because, if it were racing |
| 234 |
// with |CRYPTO_BUFFER_free| then the two callers must have independent |
| 235 |
// references already and so the reference count will never hit zero. |
| 236 |
CRYPTO_refcount_inc(&buf->references); |
| 237 |
return 1; |
| 238 |
} |
| 239 |
|
| 240 |
const uint8_t *CRYPTO_BUFFER_data(const CRYPTO_BUFFER *buf) { |
| 241 |
return buf->data; |
| 242 |
} |
| 243 |
|
| 244 |
size_t CRYPTO_BUFFER_len(const CRYPTO_BUFFER *buf) { |
| 245 |
return buf->len; |
| 246 |
} |
| 247 |
|
| 248 |
void CRYPTO_BUFFER_init_CBS(const CRYPTO_BUFFER *buf, CBS *out) { |
| 249 |
CBS_init(out, buf->data, buf->len); |
| 250 |
} |