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