Skip to content

Commit

Permalink
Add libctx to x931 keygen.
Browse files Browse the repository at this point in the history
Added coverage test that failed without the change.

Reviewed-by: Paul Dale <[email protected]>
Reviewed-by: Todd Short <[email protected]>
Reviewed-by: Hugo Landau <[email protected]>
(Merged from openssl#19677)
  • Loading branch information
slontis authored and hlandau committed May 5, 2023
1 parent f612673 commit bcd94b6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
7 changes: 3 additions & 4 deletions crypto/rsa/rsa_x931g.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,
BN_CTX *ctx = NULL, *ctx2 = NULL;
int ret = 0;

if (!rsa)
if (rsa == NULL)
goto err;

ctx = BN_CTX_new();
ctx = BN_CTX_new_ex(rsa->libctx);
if (ctx == NULL)
goto err;
BN_CTX_start(ctx);
Expand Down Expand Up @@ -145,7 +145,6 @@ int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,
BN_CTX_free(ctx2);

return ret;

}

int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
Expand All @@ -155,7 +154,7 @@ int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
BIGNUM *Xp = NULL, *Xq = NULL;
BN_CTX *ctx = NULL;

ctx = BN_CTX_new();
ctx = BN_CTX_new_ex(rsa->libctx);
if (ctx == NULL)
goto error;

Expand Down
7 changes: 7 additions & 0 deletions test/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,13 @@ IF[{- !$disabled{tests} -}]
INCLUDE[rsa_sp800_56b_test]=.. ../include ../crypto/rsa ../apps/include
DEPEND[rsa_sp800_56b_test]=../libcrypto.a libtestutil.a

IF[{- !$disabled{'deprecated-3.0'} -}]
PROGRAMS{noinst}=rsa_x931_test
SOURCE[rsa_x931_test]=rsa_x931_test.c
INCLUDE[rsa_x931_test]=.. ../include ../apps/include
DEPEND[rsa_x931_test]=../libcrypto.a libtestutil.a
ENDIF

SOURCE[bn_internal_test]=bn_internal_test.c
INCLUDE[bn_internal_test]=.. ../include ../crypto/bn ../apps/include
DEPEND[bn_internal_test]=../libcrypto.a libtestutil.a
Expand Down
9 changes: 7 additions & 2 deletions test/recipes/15-test_rsa.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env perl
# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
# Copyright 2015-2022 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
Expand All @@ -16,7 +16,7 @@ use OpenSSL::Test::Utils;

setup("test_rsa");

plan tests => 12;
plan tests => 14;

require_ok(srctop_file('test', 'recipes', 'tconversion.pl'));

Expand All @@ -32,6 +32,11 @@ sub run_rsa_tests {
ok(run(app([ 'openssl', $cmd, '-check', '-in', srctop_file('test', 'testrsa.pem'), '-noout'])),
"$cmd -check" );

SKIP: {
skip "Skipping Deprecated rsa_x931_test", 1 if disabled("deprecated-3.0");
ok(run(test(['rsa_x931_test'])), "RSA X931 test");
};

SKIP: {
skip "Skipping $cmd conversion test", 3
if disabled("rsa");
Expand Down
48 changes: 48 additions & 0 deletions test/rsa_x931_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2022 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
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include "internal/deprecated.h"

#include <openssl/rsa.h>
#include <openssl/bn.h>
#include "crypto/rsa.h"
#include "testutil.h"

static OSSL_PROVIDER *prov_null = NULL;
static OSSL_LIB_CTX *libctx = NULL;

static int test_rsa_x931_keygen(void)
{
int ret = 0;
BIGNUM *e = NULL;
RSA *rsa = NULL;

ret = TEST_ptr(rsa = ossl_rsa_new_with_ctx(libctx))
&& TEST_ptr(e = BN_new())
&& TEST_int_eq(BN_set_word(e, RSA_F4), 1)
&& TEST_int_eq(RSA_X931_generate_key_ex(rsa, 1024, e, NULL), 1);
BN_free(e);
RSA_free(rsa);
return ret;
}

int setup_tests(void)
{
if (!test_get_libctx(&libctx, &prov_null, NULL, NULL, NULL))
return 0;

ADD_TEST(test_rsa_x931_keygen);
return 1;
}

void cleanup_tests(void)
{
OSSL_PROVIDER_unload(prov_null);
OSSL_LIB_CTX_free(libctx);
}

0 comments on commit bcd94b6

Please sign in to comment.