Coverage Report

Created: 2024-06-09 08:57

/src/openssl/crypto/property/property.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <string.h>
12
#include <stdio.h>
13
#include <stdarg.h>
14
#include <openssl/crypto.h>
15
#include "internal/property.h"
16
#include "crypto/ctype.h"
17
#include <openssl/lhash.h>
18
#include <openssl/rand.h>
19
#include "internal/thread_once.h"
20
#include "crypto/lhash.h"
21
#include "crypto/sparse_array.h"
22
#include "property_local.h"
23
24
/*
25
 * The number of elements in the query cache before we initiate a flush.
26
 * If reducing this, also ensure the stochastic test in test/property_test.c
27
 * isn't likely to fail.
28
 */
29
0
#define IMPL_CACHE_FLUSH_THRESHOLD  500
30
31
typedef struct {
32
    void *method;
33
    int (*up_ref)(void *);
34
    void (*free)(void *);
35
} METHOD;
36
37
typedef struct {
38
    const OSSL_PROVIDER *provider;
39
    OSSL_PROPERTY_LIST *properties;
40
    METHOD method;
41
} IMPLEMENTATION;
42
43
DEFINE_STACK_OF(IMPLEMENTATION)
44
45
typedef struct {
46
    const char *query;
47
    METHOD method;
48
    char body[1];
49
} QUERY;
50
51
DEFINE_LHASH_OF(QUERY);
52
53
typedef struct {
54
    int nid;
55
    STACK_OF(IMPLEMENTATION) *impls;
56
    LHASH_OF(QUERY) *cache;
57
} ALGORITHM;
58
59
struct ossl_method_store_st {
60
    OPENSSL_CTX *ctx;
61
    size_t nelem;
62
    SPARSE_ARRAY_OF(ALGORITHM) *algs;
63
    OSSL_PROPERTY_LIST *global_properties;
64
    int need_flush;
65
    CRYPTO_RWLOCK *lock;
66
};
67
68
typedef struct {
69
    LHASH_OF(QUERY) *cache;
70
    size_t nelem;
71
    uint32_t seed;
72
} IMPL_CACHE_FLUSH;
73
74
DEFINE_SPARSE_ARRAY_OF(ALGORITHM);
75
76
static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid);
77
static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *c);
78
79
static int ossl_method_up_ref(METHOD *method)
80
0
{
81
0
    return (*method->up_ref)(method->method);
82
0
}
83
84
static void ossl_method_free(METHOD *method)
85
0
{
86
0
    (*method->free)(method->method);
87
0
}
88
89
int ossl_property_read_lock(OSSL_METHOD_STORE *p)
90
0
{
91
0
    return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0;
92
0
}
93
94
int ossl_property_write_lock(OSSL_METHOD_STORE *p)
95
0
{
96
0
    return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0;
97
0
}
98
99
int ossl_property_unlock(OSSL_METHOD_STORE *p)
100
0
{
101
0
    return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0;
102
0
}
103
104
static unsigned long query_hash(const QUERY *a)
105
0
{
106
0
    return OPENSSL_LH_strhash(a->query);
107
0
}
108
109
static int query_cmp(const QUERY *a, const QUERY *b)
110
0
{
111
0
    return strcmp(a->query, b->query);
112
0
}
113
114
static void impl_free(IMPLEMENTATION *impl)
115
0
{
116
0
    if (impl != NULL) {
117
0
        ossl_method_free(&impl->method);
118
0
        OPENSSL_free(impl);
119
0
    }
120
0
}
121
122
static void impl_cache_free(QUERY *elem)
123
0
{
124
0
    if (elem != NULL) {
125
0
        ossl_method_free(&elem->method);
126
0
        OPENSSL_free(elem);
127
0
    }
128
0
}
129
130
static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a)
131
0
{
132
0
    if (a != NULL) {
133
0
        sk_IMPLEMENTATION_pop_free(a->impls, &impl_free);
134
0
        lh_QUERY_doall(a->cache, &impl_cache_free);
135
0
        lh_QUERY_free(a->cache);
136
0
        OPENSSL_free(a);
137
0
    }
138
0
}
139
140
/*
141
 * The OPENSSL_CTX param here allows access to underlying property data needed
142
 * for computation
143
 */
144
OSSL_METHOD_STORE *ossl_method_store_new(OPENSSL_CTX *ctx)
145
0
{
146
0
    OSSL_METHOD_STORE *res;
147
148
0
    res = OPENSSL_zalloc(sizeof(*res));
149
0
    if (res != NULL) {
150
0
        res->ctx = ctx;
151
0
        if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL) {
152
0
            OPENSSL_free(res);
153
0
            return NULL;
154
0
        }
155
0
        if ((res->lock = CRYPTO_THREAD_lock_new()) == NULL) {
156
0
            ossl_sa_ALGORITHM_free(res->algs);
157
0
            OPENSSL_free(res);
158
0
            return NULL;
159
0
        }
160
0
    }
161
0
    return res;
162
0
}
163
164
void ossl_method_store_free(OSSL_METHOD_STORE *store)
165
0
{
166
0
    if (store != NULL) {
167
0
        ossl_sa_ALGORITHM_doall(store->algs, &alg_cleanup);
168
0
        ossl_sa_ALGORITHM_free(store->algs);
169
0
        ossl_property_free(store->global_properties);
170
0
        CRYPTO_THREAD_lock_free(store->lock);
171
0
        OPENSSL_free(store);
172
0
    }
173
0
}
174
175
static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid)
176
0
{
177
0
    return ossl_sa_ALGORITHM_get(store->algs, nid);
178
0
}
179
180
static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg)
181
0
{
182
0
        return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg);
183
0
}
184
185
int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov,
186
                          int nid, const char *properties, void *method,
187
                          int (*method_up_ref)(void *),
188
                          void (*method_destruct)(void *))
189
0
{
190
0
    ALGORITHM *alg = NULL;
191
0
    IMPLEMENTATION *impl;
192
0
    int ret = 0;
193
0
    int i;
194
195
0
    if (nid <= 0 || method == NULL || store == NULL)
196
0
        return 0;
197
0
    if (properties == NULL)
198
0
        properties = "";
199
200
    /* Create new entry */
201
0
    impl = OPENSSL_malloc(sizeof(*impl));
202
0
    if (impl == NULL)
203
0
        return 0;
204
0
    impl->method.method = method;
205
0
    impl->method.up_ref = method_up_ref;
206
0
    impl->method.free = method_destruct;
207
0
    if (!ossl_method_up_ref(&impl->method)) {
208
0
        OPENSSL_free(impl);
209
0
        return 0;
210
0
    }
211
0
    impl->provider = prov;
212
213
    /*
214
     * Insert into the hash table if required.
215
     *
216
     * A write lock is used unconditionally because we wend our way down to the
217
     * property string code which isn't locking friendly.
218
     */
219
0
    ossl_property_write_lock(store);
220
0
    ossl_method_cache_flush(store, nid);
221
0
    if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) {
222
0
        impl->properties = ossl_parse_property(store->ctx, properties);
223
0
        if (impl->properties == NULL)
224
0
            goto err;
225
0
        ossl_prop_defn_set(store->ctx, properties, impl->properties);
226
0
    }
227
228
0
    alg = ossl_method_store_retrieve(store, nid);
229
0
    if (alg == NULL) {
230
0
        if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL
231
0
                || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL
232
0
                || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL)
233
0
            goto err;
234
0
        alg->nid = nid;
235
0
        if (!ossl_method_store_insert(store, alg))
236
0
            goto err;
237
0
    }
238
239
    /* Push onto stack if there isn't one there already */
240
0
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
241
0
        const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i);
242
243
0
        if (tmpimpl->provider == impl->provider
244
0
            && tmpimpl->properties == impl->properties)
245
0
            break;
246
0
    }
247
0
    if (i == sk_IMPLEMENTATION_num(alg->impls)
248
0
        && sk_IMPLEMENTATION_push(alg->impls, impl))
249
0
        ret = 1;
250
0
    ossl_property_unlock(store);
251
0
    if (ret == 0)
252
0
        impl_free(impl);
253
0
    return ret;
254
255
0
err:
256
0
    ossl_property_unlock(store);
257
0
    alg_cleanup(0, alg);
258
0
    impl_free(impl);
259
0
    return 0;
260
0
}
261
262
int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid,
263
                             const void *method)
264
0
{
265
0
    ALGORITHM *alg = NULL;
266
0
    int i;
267
268
0
    if (nid <= 0 || method == NULL || store == NULL)
269
0
        return 0;
270
271
0
    ossl_property_write_lock(store);
272
0
    ossl_method_cache_flush(store, nid);
273
0
    alg = ossl_method_store_retrieve(store, nid);
274
0
    if (alg == NULL) {
275
0
        ossl_property_unlock(store);
276
0
        return 0;
277
0
    }
278
279
    /*
280
     * A sorting find then a delete could be faster but these stacks should be
281
     * relatively small, so we avoid the overhead.  Sorting could also surprise
282
     * users when result orderings change (even though they are not guaranteed).
283
     */
284
0
    for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) {
285
0
        IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i);
286
287
0
        if (impl->method.method == method) {
288
0
            impl_free(impl);
289
0
            sk_IMPLEMENTATION_delete(alg->impls, i);
290
0
            ossl_property_unlock(store);
291
0
            return 1;
292
0
        }
293
0
    }
294
0
    ossl_property_unlock(store);
295
0
    return 0;
296
0
}
297
298
int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid,
299
                            const char *prop_query, void **method)
300
0
{
301
0
    ALGORITHM *alg;
302
0
    IMPLEMENTATION *impl;
303
0
    OSSL_PROPERTY_LIST *pq = NULL, *p2;
304
0
    METHOD *best_method = NULL;
305
0
    int ret = 0;
306
0
    int j, best = -1, score, optional;
307
308
0
#ifndef FIPS_MODE
309
0
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
310
0
#endif
311
312
0
    if (nid <= 0 || method == NULL || store == NULL)
313
0
        return 0;
314
315
    /*
316
     * This only needs to be a read lock, because queries never create property
317
     * names or value and thus don't modify any of the property string layer.
318
     */
319
0
    ossl_property_read_lock(store);
320
0
    alg = ossl_method_store_retrieve(store, nid);
321
0
    if (alg == NULL) {
322
0
        ossl_property_unlock(store);
323
0
        return 0;
324
0
    }
325
326
0
    if (prop_query == NULL) {
327
0
        if ((impl = sk_IMPLEMENTATION_value(alg->impls, 0)) != NULL) {
328
0
            best_method = &impl->method;
329
0
            ret = 1;
330
0
        }
331
0
        goto fin;
332
0
    }
333
0
    pq = ossl_parse_query(store->ctx, prop_query);
334
0
    if (pq == NULL)
335
0
        goto fin;
336
0
    if (store->global_properties != NULL) {
337
0
        p2 = ossl_property_merge(pq, store->global_properties);
338
0
        if (p2 == NULL)
339
0
            goto fin;
340
0
        ossl_property_free(pq);
341
0
        pq = p2;
342
0
    }
343
0
    optional = ossl_property_has_optional(pq);
344
0
    for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) {
345
0
        impl = sk_IMPLEMENTATION_value(alg->impls, j);
346
0
        score = ossl_property_match_count(pq, impl->properties);
347
0
        if (score > best) {
348
0
            best_method = &impl->method;
349
0
            best = score;
350
0
            ret = 1;
351
0
            if (!optional)
352
0
                goto fin;
353
0
        }
354
0
    }
355
0
fin:
356
0
    if (ret && ossl_method_up_ref(best_method))
357
0
        *method = best_method->method;
358
0
    else
359
0
        ret = 0;
360
0
    ossl_property_unlock(store);
361
0
    ossl_property_free(pq);
362
0
    return ret;
363
0
}
364
365
int ossl_method_store_set_global_properties(OSSL_METHOD_STORE *store,
366
0
                                            const char *prop_query) {
367
0
    int ret = 0;
368
369
0
    if (store == NULL)
370
0
        return 1;
371
372
0
    ossl_property_write_lock(store);
373
0
    ossl_method_cache_flush_all(store);
374
0
    if (prop_query == NULL) {
375
0
        ossl_property_free(store->global_properties);
376
0
        store->global_properties = NULL;
377
0
        ossl_property_unlock(store);
378
0
        return 1;
379
0
    }
380
0
    store->global_properties = ossl_parse_query(store->ctx, prop_query);
381
0
    ret = store->global_properties != NULL;
382
0
    ossl_property_unlock(store);
383
0
    return ret;
384
0
}
385
386
static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg)
387
0
{
388
0
    lh_QUERY_doall(alg->cache, &impl_cache_free);
389
0
    lh_QUERY_flush(alg->cache);
390
0
}
391
392
static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid)
393
0
{
394
0
    ALGORITHM *alg = ossl_method_store_retrieve(store, nid);
395
396
0
    if (alg != NULL) {
397
0
        store->nelem -= lh_QUERY_num_items(alg->cache);
398
0
        impl_cache_flush_alg(0, alg);
399
0
    }
400
0
}
401
402
static void ossl_method_cache_flush_all(OSSL_METHOD_STORE *store)
403
0
{
404
0
    ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg);
405
0
    store->nelem = 0;
406
0
}
407
408
IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH);
409
410
/*
411
 * Flush an element from the query cache (perhaps).
412
 *
413
 * In order to avoid taking a write lock or using atomic operations
414
 * to keep accurate least recently used (LRU) or least frequently used
415
 * (LFU) information, the procedure used here is to stochastically
416
 * flush approximately half the cache.
417
 *
418
 * This procedure isn't ideal, LRU or LFU would be better.  However,
419
 * in normal operation, reaching a full cache would be unexpected.
420
 * It means that no steady state of algorithm queries has been reached.
421
 * That is, it is most likely an attack of some form.  A suboptimal clearance
422
 * strategy that doesn't degrade performance of the normal case is
423
 * preferable to a more refined approach that imposes a performance
424
 * impact.
425
 */
426
static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state)
427
0
{
428
0
    uint32_t n;
429
430
    /*
431
     * Implement the 32 bit xorshift as suggested by George Marsaglia in:
432
     *      https://doi.org/10.18637/jss.v008.i14
433
     *
434
     * This is a very fast PRNG so there is no need to extract bits one at a
435
     * time and use the entire value each time.
436
     */
437
0
    n = state->seed;
438
0
    n ^= n << 13;
439
0
    n ^= n >> 17;
440
0
    n ^= n << 5;
441
0
    state->seed = n;
442
443
0
    if ((n & 1) != 0)
444
0
        impl_cache_free(lh_QUERY_delete(state->cache, c));
445
0
    else
446
0
        state->nelem++;
447
0
}
448
449
static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg,
450
                                     void *v)
451
0
{
452
0
    IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v;
453
454
0
    state->cache = alg->cache;
455
0
    lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache,
456
0
                                    state);
457
0
}
458
459
static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store)
460
0
{
461
0
    IMPL_CACHE_FLUSH state;
462
463
0
    state.nelem = 0;
464
0
    if ((state.seed = OPENSSL_rdtsc()) == 0)
465
0
        state.seed = 1;
466
0
    store->need_flush = 0;
467
0
    ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state);
468
0
    store->nelem = state.nelem;
469
0
}
470
471
int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, int nid,
472
                                const char *prop_query, void **method)
473
0
{
474
0
    ALGORITHM *alg;
475
0
    QUERY elem, *r;
476
0
    int res = 0;
477
478
0
    if (nid <= 0 || store == NULL)
479
0
        return 0;
480
481
0
    ossl_property_read_lock(store);
482
0
    alg = ossl_method_store_retrieve(store, nid);
483
0
    if (alg == NULL)
484
0
        goto err;
485
486
0
    elem.query = prop_query != NULL ? prop_query : "";
487
0
    r = lh_QUERY_retrieve(alg->cache, &elem);
488
0
    if (r == NULL)
489
0
        goto err;
490
0
    if (ossl_method_up_ref(&r->method)) {
491
0
        *method = r->method.method;
492
0
        res = 1;
493
0
    }
494
0
err:
495
0
    ossl_property_unlock(store);
496
0
    return res;
497
0
}
498
499
int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, int nid,
500
                                const char *prop_query, void *method,
501
                                int (*method_up_ref)(void *),
502
                                void (*method_destruct)(void *))
503
0
{
504
0
    QUERY elem, *old, *p = NULL;
505
0
    ALGORITHM *alg;
506
0
    size_t len;
507
0
    int res = 1;
508
509
0
    if (nid <= 0 || store == NULL)
510
0
        return 0;
511
0
    if (prop_query == NULL)
512
0
        return 1;
513
514
0
    ossl_property_write_lock(store);
515
0
    if (store->need_flush)
516
0
        ossl_method_cache_flush_some(store);
517
0
    alg = ossl_method_store_retrieve(store, nid);
518
0
    if (alg == NULL)
519
0
        goto err;
520
521
0
    if (method == NULL) {
522
0
        elem.query = prop_query;
523
0
        if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) {
524
0
            impl_cache_free(old);
525
0
            store->nelem--;
526
0
        }
527
0
        goto end;
528
0
    }
529
0
    p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query)));
530
0
    if (p != NULL) {
531
0
        p->query = p->body;
532
0
        p->method.method = method;
533
0
        p->method.up_ref = method_up_ref;
534
0
        p->method.free = method_destruct;
535
0
        if (!ossl_method_up_ref(&p->method))
536
0
            goto err;
537
0
        memcpy((char *)p->query, prop_query, len + 1);
538
0
        if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) {
539
0
            impl_cache_free(old);
540
0
            goto end;
541
0
        }
542
0
        if (!lh_QUERY_error(alg->cache)) {
543
0
            if (++store->nelem >= IMPL_CACHE_FLUSH_THRESHOLD)
544
0
                store->need_flush = 1;
545
0
            goto end;
546
0
        }
547
0
        ossl_method_free(&p->method);
548
0
    }
549
0
err:
550
0
    res = 0;
551
0
    OPENSSL_free(p);
552
0
end:
553
0
    ossl_property_unlock(store);
554
0
    return res;
555
0
}