| 1 |
// Written by Ulf Moeller for the OpenSSL project. |
| 2 |
// Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/bn.h> |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
|
| 13 |
#include "../../internal.h" |
| 14 |
|
| 15 |
|
| 16 |
// The stack frame info is resizing, set a first-time expansion size; |
| 17 |
#define BN_CTX_START_FRAMES 32 |
| 18 |
|
| 19 |
|
| 20 |
// BN_STACK |
| 21 |
|
| 22 |
// A |BN_STACK| is a stack of |size_t| values. |
| 23 |
typedef struct { |
| 24 |
// Array of indexes into |ctx->bignums|. |
| 25 |
size_t *indexes; |
| 26 |
// Number of stack frames, and the size of the allocated array |
| 27 |
size_t depth, size; |
| 28 |
} BN_STACK; |
| 29 |
|
| 30 |
static void BN_STACK_init(BN_STACK *); |
| 31 |
static void BN_STACK_cleanup(BN_STACK *); |
| 32 |
static int BN_STACK_push(BN_STACK *, size_t idx); |
| 33 |
static size_t BN_STACK_pop(BN_STACK *); |
| 34 |
|
| 35 |
|
| 36 |
// BN_CTX |
| 37 |
|
| 38 |
DEFINE_STACK_OF(BIGNUM) |
| 39 |
|
| 40 |
// The opaque BN_CTX type |
| 41 |
struct bignum_ctx { |
| 42 |
// bignums is the stack of |BIGNUM|s managed by this |BN_CTX|. |
| 43 |
STACK_OF(BIGNUM) *bignums; |
| 44 |
// stack is the stack of |BN_CTX_start| frames. It is the value of |used| at |
| 45 |
// the time |BN_CTX_start| was called. |
| 46 |
BN_STACK stack; |
| 47 |
// used is the number of |BIGNUM|s from |bignums| that have been used. |
| 48 |
size_t used; |
| 49 |
// error is one if any operation on this |BN_CTX| failed. All subsequent |
| 50 |
// operations will fail. |
| 51 |
char error; |
| 52 |
// defer_error is one if an operation on this |BN_CTX| has failed, but no |
| 53 |
// error has been pushed to the queue yet. This is used to defer errors from |
| 54 |
// |BN_CTX_start| to |BN_CTX_get|. |
| 55 |
char defer_error; |
| 56 |
}; |
| 57 |
|
| 58 |
BN_CTX *BN_CTX_new(void) { |
| 59 |
BN_CTX *ret = OPENSSL_zalloc(sizeof(BN_CTX)); |
| 60 |
if (!ret) { |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
|
| 64 |
// Initialise the structure |
| 65 |
BN_STACK_init(&ret->stack); |
| 66 |
return ret; |
| 67 |
} |
| 68 |
|
| 69 |
BN_CTX *BN_CTX_secure_new(void) { return BN_CTX_new(); } |
| 70 |
|
| 71 |
void BN_CTX_free(BN_CTX *ctx) { |
| 72 |
if (ctx == NULL) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// All |BN_CTX_start| calls must be matched with |BN_CTX_end|, otherwise the |
| 77 |
// function may use more memory than expected, potentially without bound if |
| 78 |
// done in a loop. Assert that all |BIGNUM|s have been released. |
| 79 |
assert(ctx->used == 0 || ctx->error); |
| 80 |
sk_BIGNUM_pop_free(ctx->bignums, BN_free); |
| 81 |
BN_STACK_cleanup(&ctx->stack); |
| 82 |
OPENSSL_free(ctx); |
| 83 |
} |
| 84 |
|
| 85 |
void BN_CTX_start(BN_CTX *ctx) { |
| 86 |
if (ctx->error) { |
| 87 |
// Once an operation has failed, |ctx->stack| no longer matches the number |
| 88 |
// of |BN_CTX_end| calls to come. Do nothing. |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if (!BN_STACK_push(&ctx->stack, ctx->used)) { |
| 93 |
ctx->error = 1; |
| 94 |
// |BN_CTX_start| cannot fail, so defer the error to |BN_CTX_get|. |
| 95 |
ctx->defer_error = 1; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
BIGNUM *BN_CTX_get(BN_CTX *ctx) { |
| 100 |
// Once any operation has failed, they all do. |
| 101 |
if (ctx->error) { |
| 102 |
if (ctx->defer_error) { |
| 103 |
OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); |
| 104 |
ctx->defer_error = 0; |
| 105 |
} |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
if (ctx->bignums == NULL) { |
| 110 |
ctx->bignums = sk_BIGNUM_new_null(); |
| 111 |
if (ctx->bignums == NULL) { |
| 112 |
ctx->error = 1; |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if (ctx->used == sk_BIGNUM_num(ctx->bignums)) { |
| 118 |
BIGNUM *bn = BN_new(); |
| 119 |
if (bn == NULL || !sk_BIGNUM_push(ctx->bignums, bn)) { |
| 120 |
OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); |
| 121 |
BN_free(bn); |
| 122 |
ctx->error = 1; |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
BIGNUM *ret = sk_BIGNUM_value(ctx->bignums, ctx->used); |
| 128 |
BN_zero(ret); |
| 129 |
// This is bounded by |sk_BIGNUM_num|, so it cannot overflow. |
| 130 |
ctx->used++; |
| 131 |
return ret; |
| 132 |
} |
| 133 |
|
| 134 |
void BN_CTX_end(BN_CTX *ctx) { |
| 135 |
if (ctx->error) { |
| 136 |
// Once an operation has failed, |ctx->stack| no longer matches the number |
| 137 |
// of |BN_CTX_end| calls to come. Do nothing. |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
ctx->used = BN_STACK_pop(&ctx->stack); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
// BN_STACK |
| 146 |
|
| 147 |
static void BN_STACK_init(BN_STACK *st) { |
| 148 |
st->indexes = NULL; |
| 149 |
st->depth = st->size = 0; |
| 150 |
} |
| 151 |
|
| 152 |
static void BN_STACK_cleanup(BN_STACK *st) { |
| 153 |
OPENSSL_free(st->indexes); |
| 154 |
} |
| 155 |
|
| 156 |
static int BN_STACK_push(BN_STACK *st, size_t idx) { |
| 157 |
if (st->depth == st->size) { |
| 158 |
// This function intentionally does not push to the error queue on error. |
| 159 |
// Error-reporting is deferred to |BN_CTX_get|. |
| 160 |
size_t new_size = st->size != 0 ? st->size * 3 / 2 : BN_CTX_START_FRAMES; |
| 161 |
if (new_size <= st->size || new_size > SIZE_MAX / sizeof(size_t)) { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
size_t *new_indexes = |
| 165 |
OPENSSL_realloc(st->indexes, new_size * sizeof(size_t)); |
| 166 |
if (new_indexes == NULL) { |
| 167 |
return 0; |
| 168 |
} |
| 169 |
st->indexes = new_indexes; |
| 170 |
st->size = new_size; |
| 171 |
} |
| 172 |
|
| 173 |
st->indexes[st->depth] = idx; |
| 174 |
st->depth++; |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
static size_t BN_STACK_pop(BN_STACK *st) { |
| 179 |
assert(st->depth > 0); |
| 180 |
st->depth--; |
| 181 |
return st->indexes[st->depth]; |
| 182 |
} |