ctx.c
Go to the documentation of this file.
1 /* Written by Ulf Moeller for the OpenSSL project. */
2 /* ====================================================================
3  * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  * software must display the following acknowledgment:
19  * "This product includes software developed by the OpenSSL Project
20  * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  * endorse or promote products derived from this software without
24  * prior written permission. For written permission, please contact
25  * openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  * nor may "OpenSSL" appear in their names without prior written
29  * permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  * acknowledgment:
33  * "This product includes software developed by the OpenSSL Project
34  * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com). This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com). */
53 
54 
55 #include <openssl/bn.h>
56 
57 #include <assert.h>
58 #include <string.h>
59 
60 #include <openssl/err.h>
61 #include <openssl/mem.h>
62 
63 #include "../../internal.h"
64 
65 
66 // The stack frame info is resizing, set a first-time expansion size;
67 #define BN_CTX_START_FRAMES 32
68 
69 
70 // BN_STACK
71 
72 // A |BN_STACK| is a stack of |size_t| values.
73 typedef struct {
74  // Array of indexes into |ctx->bignums|.
75  size_t *indexes;
76  // Number of stack frames, and the size of the allocated array
77  size_t depth, size;
78 } BN_STACK;
79 
80 static void BN_STACK_init(BN_STACK *);
81 static void BN_STACK_cleanup(BN_STACK *);
82 static int BN_STACK_push(BN_STACK *, size_t idx);
83 static size_t BN_STACK_pop(BN_STACK *);
84 
85 
86 // BN_CTX
87 
89 
90 // The opaque BN_CTX type
92  // bignums is the stack of |BIGNUM|s managed by this |BN_CTX|.
93  STACK_OF(BIGNUM) *bignums;
94  // stack is the stack of |BN_CTX_start| frames. It is the value of |used| at
95  // the time |BN_CTX_start| was called.
97  // used is the number of |BIGNUM|s from |bignums| that have been used.
98  size_t used;
99  // error is one if any operation on this |BN_CTX| failed. All subsequent
100  // operations will fail.
101  char error;
102  // defer_error is one if an operation on this |BN_CTX| has failed, but no
103  // error has been pushed to the queue yet. This is used to defer errors from
104  // |BN_CTX_start| to |BN_CTX_get|.
105  char defer_error;
106 };
107 
109  BN_CTX *ret = OPENSSL_malloc(sizeof(BN_CTX));
110  if (!ret) {
112  return NULL;
113  }
114 
115  // Initialise the structure
116  ret->bignums = NULL;
117  BN_STACK_init(&ret->stack);
118  ret->used = 0;
119  ret->error = 0;
120  ret->defer_error = 0;
121  return ret;
122 }
123 
125  if (ctx == NULL) {
126  return;
127  }
128 
129  // All |BN_CTX_start| calls must be matched with |BN_CTX_end|, otherwise the
130  // function may use more memory than expected, potentially without bound if
131  // done in a loop. Assert that all |BIGNUM|s have been released.
132  assert(ctx->used == 0 || ctx->error);
133  sk_BIGNUM_pop_free(ctx->bignums, BN_free);
134  BN_STACK_cleanup(&ctx->stack);
135  OPENSSL_free(ctx);
136 }
137 
139  if (ctx->error) {
140  // Once an operation has failed, |ctx->stack| no longer matches the number
141  // of |BN_CTX_end| calls to come. Do nothing.
142  return;
143  }
144 
145  if (!BN_STACK_push(&ctx->stack, ctx->used)) {
146  ctx->error = 1;
147  // |BN_CTX_start| cannot fail, so defer the error to |BN_CTX_get|.
148  ctx->defer_error = 1;
149  }
150 }
151 
153  // Once any operation has failed, they all do.
154  if (ctx->error) {
155  if (ctx->defer_error) {
157  ctx->defer_error = 0;
158  }
159  return NULL;
160  }
161 
162  if (ctx->bignums == NULL) {
163  ctx->bignums = sk_BIGNUM_new_null();
164  if (ctx->bignums == NULL) {
166  ctx->error = 1;
167  return NULL;
168  }
169  }
170 
171  if (ctx->used == sk_BIGNUM_num(ctx->bignums)) {
172  BIGNUM *bn = BN_new();
173  if (bn == NULL || !sk_BIGNUM_push(ctx->bignums, bn)) {
175  BN_free(bn);
176  ctx->error = 1;
177  return NULL;
178  }
179  }
180 
181  BIGNUM *ret = sk_BIGNUM_value(ctx->bignums, ctx->used);
182  BN_zero(ret);
183  // This is bounded by |sk_BIGNUM_num|, so it cannot overflow.
184  ctx->used++;
185  return ret;
186 }
187 
189  if (ctx->error) {
190  // Once an operation has failed, |ctx->stack| no longer matches the number
191  // of |BN_CTX_end| calls to come. Do nothing.
192  return;
193  }
194 
195  ctx->used = BN_STACK_pop(&ctx->stack);
196 }
197 
198 
199 // BN_STACK
200 
201 static void BN_STACK_init(BN_STACK *st) {
202  st->indexes = NULL;
203  st->depth = st->size = 0;
204 }
205 
206 static void BN_STACK_cleanup(BN_STACK *st) {
207  OPENSSL_free(st->indexes);
208 }
209 
210 static int BN_STACK_push(BN_STACK *st, size_t idx) {
211  if (st->depth == st->size) {
212  // This function intentionally does not push to the error queue on error.
213  // Error-reporting is deferred to |BN_CTX_get|.
214  size_t new_size = st->size != 0 ? st->size * 3 / 2 : BN_CTX_START_FRAMES;
215  if (new_size <= st->size || new_size > ((size_t)-1) / sizeof(size_t)) {
216  return 0;
217  }
218  size_t *new_indexes =
219  OPENSSL_realloc(st->indexes, new_size * sizeof(size_t));
220  if (new_indexes == NULL) {
221  return 0;
222  }
223  st->indexes = new_indexes;
224  st->size = new_size;
225  }
226 
227  st->indexes[st->depth] = idx;
228  st->depth++;
229  return 1;
230 }
231 
232 static size_t BN_STACK_pop(BN_STACK *st) {
233  assert(st->depth > 0);
234  st->depth--;
235  return st->indexes[st->depth];
236 }
bn.h
BN_STACK::size
size_t size
Definition: ctx.c:77
ctx
Definition: benchmark-async.c:30
BN_STACK
Definition: ctx.c:73
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
OPENSSL_realloc
#define OPENSSL_realloc
Definition: boringssl_prefix_symbols.h:1889
BN_CTX_free
void BN_CTX_free(BN_CTX *ctx)
Definition: ctx.c:124
error
grpc_error_handle error
Definition: retry_filter.cc:499
binary_size.new_size
def new_size
Definition: binary_size.py:124
BN_STACK_cleanup
static void BN_STACK_cleanup(BN_STACK *)
Definition: ctx.c:206
BN_free
#define BN_free
Definition: boringssl_prefix_symbols.h:923
bignum_ctx
Definition: ctx.c:91
BN_CTX_new
BN_CTX * BN_CTX_new(void)
Definition: ctx.c:108
gen_build_yaml.struct
def struct(**kwargs)
Definition: test/core/end2end/gen_build_yaml.py:30
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
BN_CTX_start
void BN_CTX_start(BN_CTX *ctx)
Definition: ctx.c:138
STACK_OF
#define STACK_OF(type)
Definition: stack.h:125
BN_R_TOO_MANY_TEMPORARY_VARIABLES
#define BN_R_TOO_MANY_TEMPORARY_VARIABLES
Definition: bn.h:1052
err.h
stack
NodeStack stack
Definition: cord_rep_btree.cc:356
DEFINE_STACK_OF
#define DEFINE_STACK_OF(type)
Definition: stack.h:409
setup.idx
idx
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:197
BN_CTX_get
BIGNUM * BN_CTX_get(BN_CTX *ctx)
Definition: ctx.c:152
BN_zero
#define BN_zero
Definition: boringssl_prefix_symbols.h:1004
BN_STACK::indexes
size_t * indexes
Definition: ctx.c:75
BN_STACK_push
static int BN_STACK_push(BN_STACK *, size_t idx)
Definition: ctx.c:210
BN_CTX_START_FRAMES
#define BN_CTX_START_FRAMES
Definition: ctx.c:67
bignum_st
Definition: bn.h:957
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
BN_STACK_init
static void BN_STACK_init(BN_STACK *)
Definition: ctx.c:201
mem.h
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
BN_STACK::depth
size_t depth
Definition: ctx.c:77
mkowners.depth
depth
Definition: mkowners.py:114
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
BN_STACK_pop
static size_t BN_STACK_pop(BN_STACK *)
Definition: ctx.c:232
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971
BN_CTX_end
void BN_CTX_end(BN_CTX *ctx)
Definition: ctx.c:188
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:02