]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Create BN_CTX_new_ex() and BN_CTX_secure_new_ex()
authorMatt Caswell <matt@openssl.org>
Wed, 29 May 2019 16:03:53 +0000 (17:03 +0100)
committerMatt Caswell <matt@openssl.org>
Wed, 12 Jun 2019 08:16:43 +0000 (09:16 +0100)
These variants of BN_CTX_new() and BN_CTX_secure_new() enable passing
an OPENSSL_CTX so that we can access this where needed throughout the
BIGNUM sub library.

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9130)

crypto/bn/bn_ctx.c
crypto/bn/bn_err.c
crypto/err/openssl.txt
doc/man3/BN_CTX_new.pod
include/openssl/bn.h
include/openssl/bnerr.h
util/libcrypto.num

index 62e29b5dcc2dff736c51cc21f8b9790d8955058e..4857661d2db32748ef9f6c84aa74b047e342c092 100644 (file)
@@ -86,6 +86,8 @@ struct bignum_ctx {
     int too_many;
     /* Flags. */
     int flags;
+    /* The library context */
+    OPENSSL_CTX *libctx;
 };
 
 /* Debugging functionality */
@@ -121,30 +123,40 @@ static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx)
         ctxdbg(trc_out, str, ctx);  \
     } OSSL_TRACE_END(BN_CTX)
 
-
-BN_CTX *BN_CTX_new(void)
+BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx)
 {
     BN_CTX *ret;
 
     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
-        BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);
+        BNerr(BN_F_BN_CTX_NEW_EX, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
     /* Initialise the structure */
     BN_POOL_init(&ret->pool);
     BN_STACK_init(&ret->stack);
+    ret->libctx = ctx;
     return ret;
 }
 
-BN_CTX *BN_CTX_secure_new(void)
+BN_CTX *BN_CTX_new(void)
 {
-    BN_CTX *ret = BN_CTX_new();
+    return BN_CTX_new_ex(NULL);
+}
+
+BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx)
+{
+    BN_CTX *ret = BN_CTX_new_ex(ctx);
 
     if (ret != NULL)
         ret->flags = BN_FLG_SECURE;
     return ret;
 }
 
+BN_CTX *BN_CTX_secure_new(void)
+{
+    return BN_CTX_secure_new_ex(NULL);
+}
+
 void BN_CTX_free(BN_CTX *ctx)
 {
     if (ctx == NULL)
index b988646f225715bc5eed25304b5dfa873413f92b..5ca6a1f34f60cea17abbffce8fd50a46ca78a95a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Generated by util/mkerr.pl DO NOT EDIT
- * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -29,6 +29,7 @@ static const ERR_STRING_DATA BN_str_functs[] = {
     {ERR_PACK(ERR_LIB_BN, BN_F_BN_COMPUTE_WNAF, 0), "bn_compute_wNAF"},
     {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_GET, 0), "BN_CTX_get"},
     {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_NEW, 0), "BN_CTX_new"},
+    {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_NEW_EX, 0), "BN_CTX_new_ex"},
     {ERR_PACK(ERR_LIB_BN, BN_F_BN_CTX_START, 0), "BN_CTX_start"},
     {ERR_PACK(ERR_LIB_BN, BN_F_BN_DIV, 0), "BN_div"},
     {ERR_PACK(ERR_LIB_BN, BN_F_BN_DIV_RECP, 0), "BN_div_recp"},
index 58e1b940cd985f7fdc9d7143800328856856fd5d..a8a632f025b0e403ac2ff50bb7340817ccb39641 100644 (file)
@@ -196,6 +196,7 @@ BN_F_BN_BN2HEX:105:BN_bn2hex
 BN_F_BN_COMPUTE_WNAF:142:bn_compute_wNAF
 BN_F_BN_CTX_GET:116:BN_CTX_get
 BN_F_BN_CTX_NEW:106:BN_CTX_new
+BN_F_BN_CTX_NEW_EX:151:BN_CTX_new_ex
 BN_F_BN_CTX_START:129:BN_CTX_start
 BN_F_BN_DIV:107:BN_div
 BN_F_BN_DIV_RECP:130:BN_div_recp
index eb8899b773436aa12a8872c623514546a946b5d7..17c53ec79d6ced60524ba6339d506327c50b15c4 100644 (file)
@@ -2,14 +2,17 @@
 
 =head1 NAME
 
-BN_CTX_new, BN_CTX_secure_new, BN_CTX_free - allocate and free BN_CTX structures
+BN_CTX_new_ex, BN_CTX_new, BN_CTX_secure_new_ex, BN_CTX_secure_new, BN_CTX_free
+- allocate and free BN_CTX structures
 
 =head1 SYNOPSIS
 
  #include <openssl/bn.h>
 
+ BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx);
  BN_CTX *BN_CTX_new(void);
 
+ BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx);
  BN_CTX *BN_CTX_secure_new(void);
 
  void BN_CTX_free(BN_CTX *c);
@@ -21,10 +24,17 @@ library functions. Since dynamic memory allocation to create B<BIGNUM>s
 is rather expensive when used in conjunction with repeated subroutine
 calls, the B<BN_CTX> structure is used.
 
-BN_CTX_new() allocates and initializes a B<BN_CTX> structure.
-BN_CTX_secure_new() allocates and initializes a B<BN_CTX> structure
+BN_CTX_new_ex() allocates and initializes a B<BN_CTX> structure for the given
+library context B<ctx>. The <ctx> value may be NULL in which case the default
+library context will be used. BN_CTX_new() is the same as BN_CTX_new_ex() except
+that the default library context is always used.
+
+BN_CTX_secure_new_ex() allocates and initializes a B<BN_CTX> structure
 but uses the secure heap (see L<CRYPTO_secure_malloc(3)>) to hold the
-B<BIGNUM>s.
+B<BIGNUM>s for the given library context B<ctx>. The <ctx> value may be NULL in
+which case the default library context will be used. BN_CTX_secure_new() is the
+same as BN_CTX_secure_new_ex() except that the default library context is always
+used.
 
 BN_CTX_free() frees the components of the B<BN_CTX> and the structure itself.
 Since BN_CTX_start() is required in order to obtain B<BIGNUM>s from the
index 57d2ddd18d191113089ef47ea9a208b7df13eaac..377016062deea39e2e9abf1f201fa77a75cae858 100644 (file)
@@ -198,7 +198,9 @@ void BN_zero_ex(BIGNUM *a);
 
 const BIGNUM *BN_value_one(void);
 char *BN_options(void);
+BN_CTX *BN_CTX_new_ex(OPENSSL_CTX *ctx);
 BN_CTX *BN_CTX_new(void);
+BN_CTX *BN_CTX_secure_new_ex(OPENSSL_CTX *ctx);
 BN_CTX *BN_CTX_secure_new(void);
 void BN_CTX_free(BN_CTX *c);
 void BN_CTX_start(BN_CTX *ctx);
index bcf4f8f2eb52a66096ed7e30cbf62a951b38fc50..6bdea5bb0e75d568d8595c40d5ca9d83087f0d98 100644 (file)
@@ -35,6 +35,7 @@ int ERR_load_BN_strings(void);
 # define BN_F_BN_COMPUTE_WNAF                             142
 # define BN_F_BN_CTX_GET                                  116
 # define BN_F_BN_CTX_NEW                                  106
+# define BN_F_BN_CTX_NEW_EX                               151
 # define BN_F_BN_CTX_START                                129
 # define BN_F_BN_DIV                                      107
 # define BN_F_BN_DIV_RECP                                 130
index 0c2a8f5da7a4e97f5209a66b459849650126cc05..7280649920324ad3953a16bd312832f2a4bc23ab 100644 (file)
@@ -4829,3 +4829,5 @@ RAND_DRBG_secure_new_ex                 4773      3_0_0   EXIST::FUNCTION:
 OPENSSL_CTX_get0_master_drbg            4774   3_0_0   EXIST::FUNCTION:
 OPENSSL_CTX_get0_public_drbg            4775   3_0_0   EXIST::FUNCTION:
 OPENSSL_CTX_get0_private_drbg           4776   3_0_0   EXIST::FUNCTION:
+BN_CTX_new_ex                           4777   3_0_0   EXIST::FUNCTION:
+BN_CTX_secure_new_ex                    4778   3_0_0   EXIST::FUNCTION: