oweals/openssl.git
5 years agoDeprecate the public definition of ERR_STATE
Richard Levitte [Thu, 25 Jul 2019 19:57:48 +0000 (21:57 +0200)]
Deprecate the public definition of ERR_STATE

The intention is to make it opaque later on.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9462)

5 years agoconfdata.pm.in: New template for configdata.pm
Richard Levitte [Sun, 25 Aug 2019 08:46:22 +0000 (10:46 +0200)]
confdata.pm.in: New template for configdata.pm

To have the configdata.pm text embedded in Configure was kind of ugly,
and becomes clearer if put into a template file, configdata.pm.in.  We
can then use OpenSSL::Template to generate it.

We also modify configdata.pm to be the build file generator, and run
it from Configure.  The benefit with that is that developers who
tinker and play with the build file can do a "factory reset" without
having to go through the configuration process, i.e. they can re-use
the config data the already have.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9693)

5 years agoutil/dofile.pl, util/perl/OpenSSL/Template.pm: move parts of dofile.pl
Richard Levitte [Sun, 25 Aug 2019 08:44:41 +0000 (10:44 +0200)]
util/dofile.pl, util/perl/OpenSSL/Template.pm: move parts of dofile.pl

We make a module OpenSSL::Template from the central parts of
util/dofile.pl, and also reduce the amount of ugly code with more
proper use of Text::Template.  OpenSSL::Template is a simply subclass
of Text::Template.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9693)

5 years agoAdd a CHANGES entry for the recent ERR changes
Richard Levitte [Thu, 5 Sep 2019 07:14:03 +0000 (09:14 +0200)]
Add a CHANGES entry for the recent ERR changes

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9756)

5 years agoDocument the deprecation of ERR_func_error_string()
Richard Levitte [Thu, 5 Sep 2019 06:51:12 +0000 (08:51 +0200)]
Document the deprecation of ERR_func_error_string()

Also, correct the output template for ERR_error_string() and
ERR_error_string_n().

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9756)

5 years agoModernise ERR_print_errors_cb()
Richard Levitte [Wed, 4 Sep 2019 20:14:21 +0000 (22:14 +0200)]
Modernise ERR_print_errors_cb()

ERR_print_errors_cb() used functionality that isn't suitable any more,
as that functionality couldn't integrate the error record function
name strings.  We therefore refactor it a bit to use better adapted
methods.

Fixes #9756

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9756)

5 years agoModernise the ERR functionality further (new functions and deprecations)
Richard Levitte [Wed, 4 Sep 2019 20:04:08 +0000 (22:04 +0200)]
Modernise the ERR functionality further (new functions and deprecations)

ERR_func_error_string() essentially returns NULL, and since all
function codes are now removed for all intents and purposes, this
function has fallen out of use and cannot be modified to suit the
data, since its only function is to interpret an error code.

To compensate for the loss of error code, we instead provide new
functions that extracts the function name strings from an error
record:

- ERR_get_error_func()
- ERR_peek_error_func()
- ERR_peek_last_error_func()

Similarly, the once all encompasing functions
ERR_peek_last_error_line_data(), ERR_peek_error_line_data() and
ERR_get_error_line_data() lack the capability of getting the function
name string, so we deprecate those and add these functions to replace
them:

- ERR_get_error_all()
- ERR_peek_error_all()
- ERR_peek_last_error_all()

Finally, we adjust a few lines of code that used the now deprecated
functions.

Fixes #9756

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9756)

5 years agoinclude/openssl/err.h: Depend on OPENSSL_NO_FILENAMES, not OPENSSL_NO_ERR
Richard Levitte [Wed, 4 Sep 2019 19:55:58 +0000 (21:55 +0200)]
include/openssl/err.h: Depend on OPENSSL_NO_FILENAMES, not OPENSSL_NO_ERR

The configuration option 'no-err' is documented to be used to avoid
loading error related string tables.  For some reason, it was also
used to define if ERR_PUT_error() would pass the source file name and
line information or not.

The configuration option 'no-filenames' is documented to be used to
avoid passing the source file name and line anywhere.  So, the
definition of ERR_PUT_error() should depend on OPENSSL_NO_FILENAMES
rather than OPENSSL_NO_ERR.

Furthermore, the definition of OPENSSL_FILE and OPENSSL_LINE depends
on if OPENSSL_NO_FILENAMES is defined or not, so there was never any
need to do extra macro gymnastics in include/openssl/err.h, so we
simply remove it and use OPENSSL_FILE and OPENSSL_LINE directly.

Finally, the macro OPENSSL_FUNC is unaffected by all these
configuration options, so it should be used in all macros that call
ERR_set_debug().

Fixes #9756

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9756)

5 years agoinclude/openssl/macros.h: Remove the PEDANTIC OPENSSL_FUNC definition
Richard Levitte [Wed, 4 Sep 2019 19:51:17 +0000 (21:51 +0200)]
include/openssl/macros.h: Remove the PEDANTIC OPENSSL_FUNC definition

There was a section to define OPENSSL_FUNC that depended on PEDANTIC
being defined.  That is an internal build macro that should never
appear in a public header.  The solution was simple, replace it with
a check of __STRICT_ANSI__.

Fixes #9756

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9756)

5 years agoutil/mkerr.pl: make it not depend on the function code
Richard Levitte [Wed, 4 Sep 2019 19:45:56 +0000 (21:45 +0200)]
util/mkerr.pl: make it not depend on the function code

The output C code was made to use ERR_func_error_string() to see if a
string table was already loaded or not.  Since this function returns
NULL always, this check became useless.

Change it to use ERR_reason_error_string() instead, as there's no
reason to believe we will get rid of reason strings, ever.

To top it off, we rebuild all affected C sources.

Fixes #9756

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9756)

5 years agoBIO_f_zlib: Properly handle BIO_CTRL_PENDING and BIO_CTRL_WPENDING calls.
Tomas Mraz [Thu, 12 Sep 2019 10:27:36 +0000 (12:27 +0200)]
BIO_f_zlib: Properly handle BIO_CTRL_PENDING and BIO_CTRL_WPENDING calls.

There can be data to write in output buffer and data to read that were
not yet read in the input stream.

Fixes #9866

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9877)

5 years agoTravis and Appveyor: use HARNESS_VERBOSE_FAILURE rather than HARNESS_VERBOSE
Richard Levitte [Wed, 11 Sep 2019 09:16:53 +0000 (11:16 +0200)]
Travis and Appveyor: use HARNESS_VERBOSE_FAILURE rather than HARNESS_VERBOSE

This allows for shorter logs, and also logs that only show the details
for tests that fail.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9862)

5 years agoRework test/run_tests.pl to support selective verbosity and TAP copy
Richard Levitte [Wed, 11 Sep 2019 09:05:11 +0000 (11:05 +0200)]
Rework test/run_tests.pl to support selective verbosity and TAP copy

This includes a complete rework of how we use TAP::Harness, by adding
a TAP::Parser subclass that allows additional callbacks to be passed
to perform what we need.  The TAP::Parser callbacks we add are:

    ALL         to print all the TAP output to a file (conditionally)
                to collect all the TAP output to an array (conditionally)
    EOF         to print all the collected TAP output (if there is any)
                if any subtest failed

To get TAP output to file, the environment variable HARNESS_TAP_COPY
must be defined, with a file name as value.  That file will be
overwritten unconditionally.

To get TAP output displayed on failure, the make variable VERBOSE_FAILURE
or VF must be defined with a non-emoty value.

Additionally, the output of test recipe names has been changed to only
display its basename.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9862)

5 years agoutil/perl/OpenSSL/Test.pm: Disable stdout/stderr redirection on non-verbosity
Richard Levitte [Wed, 11 Sep 2019 09:02:24 +0000 (11:02 +0200)]
util/perl/OpenSSL/Test.pm: Disable stdout/stderr redirection on non-verbosity

... except on VMS, where output from executed programs doesn't seem to be
captured properly by Test::Harness or TAP::Harness.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9862)

5 years agodoc/man3/OSSL_PARAM.pod: add details about multiple elements with same key
Richard Levitte [Sat, 31 Aug 2019 07:30:43 +0000 (09:30 +0200)]
doc/man3/OSSL_PARAM.pod: add details about multiple elements with same key

Usually, each element in an OSSL_PARAM array will have a unique key.
However, there may be some rare cases when a responder will handle
multiple elements with the same key.  This adds a short passage
explaining this case.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9741)

5 years agodoc/man3/OSSL_PARAM.pod: remove a comment that isn't true any more
Richard Levitte [Sat, 31 Aug 2019 07:30:15 +0000 (09:30 +0200)]
doc/man3/OSSL_PARAM.pod: remove a comment that isn't true any more

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9741)

5 years agodoc/man3/OSSL_PARAM.pod: conform to man-page standards
Richard Levitte [Sat, 31 Aug 2019 07:29:33 +0000 (09:29 +0200)]
doc/man3/OSSL_PARAM.pod: conform to man-page standards

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9741)

5 years agotest/evp_test.c: try fetching algorithms
Richard Levitte [Fri, 7 Jun 2019 08:52:25 +0000 (10:52 +0200)]
test/evp_test.c: try fetching algorithms

Instead of relying on implicit fetches, try explicit fetches when available.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9121)

5 years agoUpdate the bundled external perl module Text-Template to version 1.56
Richard Levitte [Mon, 9 Sep 2019 10:49:15 +0000 (12:49 +0200)]
Update the bundled external perl module Text-Template to version 1.56

Fixes #9287

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9828)

5 years agoClarify the status of bundled external perl modules
Richard Levitte [Mon, 9 Sep 2019 10:13:37 +0000 (12:13 +0200)]
Clarify the status of bundled external perl modules

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9826)

5 years agoRework the perl fallback functionality
Richard Levitte [Mon, 9 Sep 2019 09:51:01 +0000 (11:51 +0200)]
Rework the perl fallback functionality

The module with_fallback.pm was kind of clunky and required a transfer
module.  This change replaces if with a much more generic pragma type
module, which simply appends given directories to @INC (as opposed to
the 'lib' pragma, which prepends the directories to @INC).

This also supports having a file MODULES.txt with sub-directories to
modules.  This ensures that we don't have to spray individual module
paths throughout our perl code, but can have them collected in one
place.

(do note that there is a 'fallback' module on CPAN.  However, it isn't
part of the core perl, and it has no support the any MODULES.txt kind
of construct)

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9826)

5 years agoAvoid passing NULL to memcpy
Matt Caswell [Wed, 11 Sep 2019 14:03:39 +0000 (15:03 +0100)]
Avoid passing NULL to memcpy

It is undefined behaviour to send NULL as either the src, or dest params
in memcpy.

In pkey_kdf.c we had a check to ensure that the src address is non-NULL.
However in some situations it is possible that the dest address could also
be NULL. Specifically in the case where the datalen is 0 and we are using
a newly allocated BUF_MEM.

We add a check of datalen to avoid the undefined behaviour.

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9868)

5 years agoPass the correct ctx to provider KDF functions
Matt Caswell [Wed, 11 Sep 2019 10:52:47 +0000 (11:52 +0100)]
Pass the correct ctx to provider KDF functions

Make sure we pass the provider side ctx and not the libcrypto side ctx.

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9865)

5 years agoFix no-engine
Matt Caswell [Wed, 11 Sep 2019 09:17:00 +0000 (10:17 +0100)]
Fix no-engine

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9863)

5 years agoFix no-dsa
Matt Caswell [Wed, 11 Sep 2019 09:04:23 +0000 (10:04 +0100)]
Fix no-dsa

Also fixes no-dh

Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9861)

5 years agoFix test error if scrypt is disabled
Paul Yang [Thu, 12 Sep 2019 08:57:23 +0000 (16:57 +0800)]
Fix test error if scrypt is disabled

Fix no-scrypt.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9840)

5 years agocrypto/threads_win.c: fix preprocessor indentation
Dr. Matthias St. Pierre [Wed, 11 Sep 2019 08:40:18 +0000 (10:40 +0200)]
crypto/threads_win.c: fix preprocessor indentation

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9832)

5 years agodrbg: fix issue where DRBG_CTR fails if NO_DF is used (2nd attempt)
Dr. Matthias St. Pierre [Thu, 30 May 2019 16:37:29 +0000 (18:37 +0200)]
drbg: fix issue where DRBG_CTR fails if NO_DF is used (2nd attempt)

Since commit 7c226dfc434d a chained DRBG does not add additional
data anymore when reseeding from its parent. The reason is that
the size of the additional data exceeded the allowed size when
no derivation function was used.

This commit provides an alternative fix: instead of adding the
entire DRBG's complete state, we just add the DRBG's address
in memory, thereby providing some distinction between the different
DRBG instances.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9832)

5 years agodrbg: add fork id to additional data on UNIX systems
Dr. Matthias St. Pierre [Thu, 30 May 2019 16:52:39 +0000 (18:52 +0200)]
drbg: add fork id to additional data on UNIX systems

Provides a little extra fork-safety on UNIX systems, adding to the
fact that all DRBGs reseed automatically when the fork_id changes.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9832)

5 years agodrbg: ensure fork-safety without using a pthread_atfork handler
Dr. Matthias St. Pierre [Mon, 27 May 2019 19:03:09 +0000 (21:03 +0200)]
drbg: ensure fork-safety without using a pthread_atfork handler

When the new OpenSSL CSPRNG was introduced in version 1.1.1,
it was announced in the release notes that it would be fork-safe,
which the old CSPRNG hadn't been.

The fork-safety was implemented using a fork count, which was
incremented by a pthread_atfork handler. Initially, this handler
was enabled by default. Unfortunately, the default behaviour
had to be changed for other reasons in commit b5319bdbd095, so
the new OpenSSL CSPRNG failed to keep its promise.

This commit restores the fork-safety using a different approach.
It replaces the fork count by a fork id, which coincides with
the process id on UNIX-like operating systems and is zero on other
operating systems. It is used to detect when an automatic reseed
after a fork is necessary.

To prevent a future regression, it also adds a test to verify that
the child reseeds after fork.

CVE-2019-1549

Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9832)

5 years agoAdd EVP_CIPHER_CTX_tag_length()
Shane Lontis [Wed, 11 Sep 2019 07:52:30 +0000 (17:52 +1000)]
Add EVP_CIPHER_CTX_tag_length()

There is no deprecated CTRL support for this new field.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9698)

5 years agoUsages of KDFs converted to use the name macros
Pauli [Mon, 9 Sep 2019 01:42:22 +0000 (11:42 +1000)]
Usages of KDFs converted to use the name macros

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9814)

5 years agoRegister KDF's using their name macros rather than strings
Pauli [Mon, 9 Sep 2019 01:42:00 +0000 (11:42 +1000)]
Register KDF's using their name macros rather than strings

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9814)

5 years agoMake FIPS provider use KDF name for PBKDF2
Pauli [Mon, 9 Sep 2019 01:39:10 +0000 (11:39 +1000)]
Make FIPS provider use KDF name for PBKDF2

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9814)

5 years agoUpdate tests to (mostly) use KDF names
Pauli [Mon, 9 Sep 2019 01:37:11 +0000 (11:37 +1000)]
Update tests to (mostly) use KDF names

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9814)

5 years agoConvert SSL functions to use KDF names not SN_ strings
Pauli [Mon, 9 Sep 2019 01:36:48 +0000 (11:36 +1000)]
Convert SSL functions to use KDF names not SN_ strings

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9814)

5 years agoAdd macros for the KDF algorithm names.
Pauli [Mon, 9 Sep 2019 01:35:25 +0000 (11:35 +1000)]
Add macros for the KDF algorithm names.

This avoids the problems with PBKDF2 and SCRYPT not being of the same form
as the rest.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9814)

5 years agoCoverity 1453629 and 1453638: Error handling issues (NEGATIVE_RETURNS)
Pauli [Mon, 9 Sep 2019 00:14:32 +0000 (10:14 +1000)]
Coverity 1453629 and 1453638: Error handling issues (NEGATIVE_RETURNS)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoCoverity 1453634: Resource leaks (RESOURCE_LEAK)
Pauli [Sun, 8 Sep 2019 08:39:11 +0000 (18:39 +1000)]
Coverity 1453634: Resource leaks (RESOURCE_LEAK)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoCoverity 1453633: Error handling issues (CHECKED_RETURN)
Pauli [Sun, 8 Sep 2019 22:04:45 +0000 (08:04 +1000)]
Coverity 1453633: Error handling issues (CHECKED_RETURN)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoCoverity 1453632 & 1453635: Null pointer dereferences (FORWARD_NULL)
Pauli [Sun, 8 Sep 2019 08:37:13 +0000 (18:37 +1000)]
Coverity 1453632 & 1453635: Null pointer dereferences (FORWARD_NULL)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoCoverity 1453630: Null pointer dereferences (FORWARD_NULL)
Pauli [Sun, 8 Sep 2019 08:33:12 +0000 (18:33 +1000)]
Coverity 1453630: Null pointer dereferences (FORWARD_NULL)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoCoverity 1453628: Null pointer dereferences (REVERSE_INULL)
Pauli [Sun, 8 Sep 2019 08:29:58 +0000 (18:29 +1000)]
Coverity 1453628: Null pointer dereferences (REVERSE_INULL)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoCoverity 1453627: Null pointer dereferences (REVERSE_INULL)
Pauli [Sun, 8 Sep 2019 08:28:56 +0000 (18:28 +1000)]
Coverity 1453627: Null pointer dereferences (REVERSE_INULL)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoCoverity 1414465: Resource leaks (RESOURCE_LEAK)
Pauli [Sun, 8 Sep 2019 08:25:34 +0000 (18:25 +1000)]
Coverity 1414465: Resource leaks  (RESOURCE_LEAK)

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9805)

5 years agoFix a padding oracle in PKCS7_dataDecode and CMS_decrypt_set1_pkey
Bernd Edlinger [Sat, 31 Aug 2019 22:16:28 +0000 (00:16 +0200)]
Fix a padding oracle in PKCS7_dataDecode and CMS_decrypt_set1_pkey

An attack is simple, if the first CMS_recipientInfo is valid but the
second CMS_recipientInfo is chosen ciphertext. If the second
recipientInfo decodes to PKCS #1 v1.5 form plaintext, the correct
encryption key will be replaced by garbage, and the message cannot be
decoded, but if the RSA decryption fails, the correct encryption key is
used and the recipient will not notice the attack.

As a work around for this potential attack the length of the decrypted
key must be equal to the cipher default key length, in case the
certifiate is not given and all recipientInfo are tried out.

The old behaviour can be re-enabled in the CMS code by setting the
CMS_DEBUG_DECRYPT flag.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9777)

5 years agoFix a potential crash in rand_unix.c
Bernd Edlinger [Fri, 6 Sep 2019 19:54:13 +0000 (21:54 +0200)]
Fix a potential crash in rand_unix.c

Due to the dynamic allocation that was added to rand_pool_add_begin
this function could now return a null pointer where it was previously
guaranteed to succeed. But the return value of this function does
not need to be checked by design.

Move rand_pool_grow from rand_pool_add_begin to rand_pool_bytes_needed.
Make an allocation error persistent to avoid falling back to less secure
or blocking entropy sources.

Fixes: a6a66e4511ee ("Make rand_pool buffers more dynamic in their sizing.")

Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9687)

5 years agoFix a strict warnings error in rand_pool_acquire_entropy
Bernd Edlinger [Sat, 24 Aug 2019 09:38:32 +0000 (11:38 +0200)]
Fix a strict warnings error in rand_pool_acquire_entropy

There was a warning about unused variables in this config:
./config --strict-warnings --with-rand-seed=rdcpu

Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9687)

5 years ago[test] ECC: check the bounds for auto computing cofactor
Billy Brumley [Mon, 9 Sep 2019 08:29:09 +0000 (11:29 +0300)]
[test] ECC: check the bounds for auto computing cofactor

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/9821)

5 years agoFix build with VS2008
Bernd Edlinger [Mon, 9 Sep 2019 09:42:56 +0000 (11:42 +0200)]
Fix build with VS2008

crypto/rand/rand_win.c(70) : error C2065: 'BCRYPT_USE_SYSTEM_PREFERRED_RNG' : undeclared identifier

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9827)

5 years agoUse simple names in core_names.h
Matt Caswell [Thu, 5 Sep 2019 13:57:06 +0000 (14:57 +0100)]
Use simple names in core_names.h

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoMove EVP_PKEY algorithm implementations into a union
Matt Caswell [Wed, 4 Sep 2019 22:13:25 +0000 (23:13 +0100)]
Move EVP_PKEY algorithm implementations into a union

An EVP_PKEY can be used for multiple different algorithm operations.
Only one can be used at a time, so we move those into a union.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoRevise EVP_PKEY param handling
Matt Caswell [Wed, 4 Sep 2019 11:46:02 +0000 (12:46 +0100)]
Revise EVP_PKEY param handling

We add new functions for getting parameters and discovering the gettable
and settable parameters. We also make EVP_PKEY_CTX_get_signature_md() a
function and implement it in terms of the new functions.

This enables applications to discover the set of parameters that are
supported for a given algorithm implementation.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoEnable DH "keys" which only contain domain parameters
Matt Caswell [Wed, 4 Sep 2019 09:58:59 +0000 (10:58 +0100)]
Enable DH "keys" which only contain domain parameters

It is valid for a pub_key and priv_key to be missing from a DH "key".

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoAdd docs for the provider interface for signature operations
Matt Caswell [Tue, 3 Sep 2019 16:05:52 +0000 (17:05 +0100)]
Add docs for the provider interface for signature operations

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoAdd public API docs for newly added EVP_SIGNATURE related functions
Matt Caswell [Tue, 3 Sep 2019 15:15:21 +0000 (16:15 +0100)]
Add public API docs for newly added EVP_SIGNATURE related functions

Documentation for EVP_SIGNATURE_*() as well as EVP_PKEY_sign_init_ex(),
EVP_PKEY_verify_init_ex() and EVP_PKEY_verify_recover_init_ex().

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoAdd support for verify/verify_recover functions to EVP_SIGNATURE
Matt Caswell [Mon, 2 Sep 2019 15:48:26 +0000 (16:48 +0100)]
Add support for verify/verify_recover functions to EVP_SIGNATURE

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoImplement DSA in the default provider
Matt Caswell [Fri, 30 Aug 2019 12:33:37 +0000 (13:33 +0100)]
Implement DSA in the default provider

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoAdd the ability to perform signatures in a provider
Matt Caswell [Fri, 30 Aug 2019 12:33:10 +0000 (13:33 +0100)]
Add the ability to perform signatures in a provider

This makes EVP_PKEY_sign and EVP_PKEY_sign_init provider aware. It
also introduces the new type EVP_SIGNATURE to represent signature
algorithms. This also automatically makes the EVP_Sign* APIs provider
aware because they use EVP_Digest* (which is already provider aware)
and EVP_PKEY_sign(_init) under the covers.

At this stage there are no signature algorithms in any providers. That
will come in the following commits.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9753)

5 years agoDH_check_pub_key_ex was accidentally calling DH_check,
Bernd Edlinger [Fri, 6 Sep 2019 22:58:31 +0000 (00:58 +0200)]
DH_check_pub_key_ex was accidentally calling DH_check,
so results were undefined.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9796)

5 years agoUse BN_clear_free in DH_set0_key
Bernd Edlinger [Fri, 6 Sep 2019 22:53:24 +0000 (00:53 +0200)]
Use BN_clear_free in DH_set0_key

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9796)

5 years agoCheck the DH modulus bit length
Bernd Edlinger [Fri, 6 Sep 2019 21:38:49 +0000 (23:38 +0200)]
Check the DH modulus bit length

The check was missing in DH_check and DH_check_params.

[extended tests]

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9796)

5 years agoUniform TEST_*() check usage in test/ectest.c
Nicola Tuveri [Mon, 9 Sep 2019 01:00:37 +0000 (04:00 +0300)]
Uniform TEST_*() check usage in test/ectest.c

- Replace a `TEST_true()` with `!TEST_false()` to avoid reporting
  confusing errors
- We tend to use `if (!TEST_foo() || !TEST_bar())` and it's a bit
  confusing to switch to `if(!(TEST_foo() && TEST_bar()))`: replace it
  with the more common style

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9813)

5 years agoFix spacing nit in test/ectest.c
Nicola Tuveri [Mon, 9 Sep 2019 00:52:00 +0000 (03:52 +0300)]
Fix spacing nit in test/ectest.c

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9813)

5 years agoFix typos in the OSSL_METHOD_STORE doc
Jakub Zelenka [Sun, 8 Sep 2019 16:32:07 +0000 (17:32 +0100)]
Fix typos in the OSSL_METHOD_STORE doc

Reviewed-by: Paul Yang <kaishen.yy@antfin.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9825)

5 years ago[ec] Match built-in curves on EC_GROUP_new_from_ecparameters
Nicola Tuveri [Sat, 7 Sep 2019 15:05:31 +0000 (18:05 +0300)]
[ec] Match built-in curves on EC_GROUP_new_from_ecparameters

Description
-----------

Upon `EC_GROUP_new_from_ecparameters()` check if the parameters match any
of the built-in curves. If that is the case, return a new
`EC_GROUP_new_by_curve_name()` object instead of the explicit parameters
`EC_GROUP`.

This affects all users of `EC_GROUP_new_from_ecparameters()`:
- direct calls to `EC_GROUP_new_from_ecparameters()`
- direct calls to `EC_GROUP_new_from_ecpkparameters()` with an explicit
  parameters argument
- ASN.1 parsing of explicit parameters keys (as it eventually
  ends up calling `EC_GROUP_new_from_ecpkparameters()`)

A parsed explicit parameter key will still be marked with the
`OPENSSL_EC_EXPLICIT_CURVE` ASN.1 flag on load, so, unless
programmatically forced otherwise, if the key is eventually serialized
the output will still be encoded with explicit parameters, even if
internally it is treated as a named curve `EC_GROUP`.

Before this change, creating any `EC_GROUP` object using
`EC_GROUP_new_from_ecparameters()`, yielded an object associated with
the default generic `EC_METHOD`, but this was never guaranteed in the
documentation.
After this commit, users of the library that intentionally want to
create an `EC_GROUP` object using a specific `EC_METHOD` can still
explicitly call `EC_GROUP_new(foo_method)` and then manually set the
curve parameters using `EC_GROUP_set_*()`.

Motivation
----------

This has obvious performance benefits for the built-in curves with
specialized `EC_METHOD`s and subtle but important security benefits:
- the specialized methods have better security hardening than the
  generic implementations
- optional fields in the parameter encoding, like the `cofactor`, cannot
  be leveraged by an attacker to force execution of the less secure
  code-paths for single point scalar multiplication
- in general, this leads to reducing the attack surface

Check the manuscript at https://arxiv.org/abs/1909.01785 for an in depth
analysis of the issues related to this commit.

It should be noted that `libssl` does not allow to negotiate explicit
parameters (as per RFC 8422), so it is not directly affected by the
consequences of using explicit parameters that this commit fixes.
On the other hand, we detected external applications and users in the
wild that use explicit parameters by default (and sometimes using 0 as
the cofactor value, which is technically not a valid value per the
specification, but is tolerated by parsers for wider compatibility given
that the field is optional).
These external users of `libcrypto` are exposed to these vulnerabilities
and their security will benefit from this commit.

Related commits
---------------

While this commit is beneficial for users using built-in curves and
explicit parameters encoding for serialized keys, commit
b783beeadf6b80bc431e6f3230b5d5585c87ef87 (and its equivalents for the
1.0.2, 1.1.0 and 1.1.1 stable branches) fixes the consequences of the
invalid cofactor values more in general also for other curves
(CVE-2019-1547).

The following list covers commits in `master` that are related to the
vulnerabilities presented in the manuscript motivating this commit:

d2baf88c43 [crypto/rsa] Set the constant-time flag in multi-prime RSA too
311e903d84 [crypto/asn1] Fix multiple SCA vulnerabilities during RSA key validation.
b783beeadf [crypto/ec] for ECC parameters with NULL or zero cofactor, compute it
724339ff44 Fix SCA vulnerability when using PVK and MSBLOB key formats

Note that the PRs that contributed the listed commits also include other
commits providing related testing and documentation, in addition to
links to PRs and commits backporting the fixes to the 1.0.2, 1.1.0 and
1.1.1 branches.

Responsible Disclosure
----------------------

This and the other issues presented in https://arxiv.org/abs/1909.01785
were reported by Cesar Pereida GarcĂ­a, Sohaib ul Hassan, Nicola Tuveri,
Iaroslav Gridin, Alejandro Cabrera Aldaya and Billy Bob Brumley from the
NISEC group at Tampere University, FINLAND.

The OpenSSL Security Team evaluated the security risk for this
vulnerability as low, and encouraged to propose fixes using public Pull
Requests.

_______________________________________________________________________________

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9808)

5 years agoDo no mention private headers in public headers
Richard Levitte [Mon, 9 Sep 2019 00:41:26 +0000 (02:41 +0200)]
Do no mention private headers in public headers

Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
(Merged from https://github.com/openssl/openssl/pull/9812)

5 years agoConfigure: clang: move -Wno-unknown-warning-option to the front
Dr. Matthias St. Pierre [Tue, 23 Jul 2019 18:54:03 +0000 (20:54 +0200)]
Configure: clang: move -Wno-unknown-warning-option to the front

While gcc ignores unknown options of the type '-Wno-xxx', clang by default issues
a warning [-Wunknown-warning-option] (see [3]), which together with '-Werror'
causes the build to fail. This turned out to be a problem on the 1.0.2 stable branch
in the case of the '-Wextended-offsetof' option, which was removed in version 6.0.0,
but needs to be kept here in order to support older clang versions, too (see #9446).

Incidentally, master and 1.1.1 branch already contained the -Wno-unknown-warning-option
option. Due to its special role and its importance, this commit adds an explaining
commit message and moves the option to the front.

[extended tests]

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9447)

5 years agoCHANGES entry: for ECC parameters with NULL or zero cofactor, compute it
Billy Brumley [Fri, 6 Sep 2019 14:26:40 +0000 (17:26 +0300)]
CHANGES entry: for ECC parameters with NULL or zero cofactor, compute it

This is a forward port from https://github.com/openssl/openssl/pull/9781
of the CHANGES entry for the functionality added in
https://github.com/openssl/openssl/pull/9727

(cherry picked from commit 4b965086cb56c24cb5d2197fc04869b95f209a11)

Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9797)

5 years ago[test] computing ECC cofactors: regression test
Billy Brumley [Fri, 6 Sep 2019 14:26:08 +0000 (17:26 +0300)]
[test] computing ECC cofactors: regression test

This is a forward port from
    https://github.com/openssl/openssl/pull/9781
for the test logic introduced by
    https://github.com/openssl/openssl/pull/9727

As @mattcaswell commented
(https://github.com/openssl/openssl/pull/9781#discussion_r321621541):

> These `TEST_true` calls should be `!TEST_false` because we are
> *expecting* a failure.
> The difference is that the test framework will print error details if
> the test doesn't give the expected answer.
> So by using `TEST_true` instead of `!TEST_false` we'll get error
> details printed, but the test will succeed anyway.

(cherry picked from commit e8aafc891d9bd7fa1cce0401d858ef842f09b49e)

Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9797)

5 years agoUse common digest getter for X942 KDF
Pauli [Sat, 7 Sep 2019 00:50:46 +0000 (10:50 +1000)]
Use common digest getter for X942 KDF

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoUse common digest getter for SSH KDF
Pauli [Sat, 7 Sep 2019 00:50:14 +0000 (10:50 +1000)]
Use common digest getter for SSH KDF

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoUse common digest getter for TLS1 PRF
Pauli [Sat, 7 Sep 2019 00:49:53 +0000 (10:49 +1000)]
Use common digest getter for TLS1 PRF

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoUse common digest getter for single step KDF
Pauli [Sat, 7 Sep 2019 00:49:36 +0000 (10:49 +1000)]
Use common digest getter for single step KDF

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoUse common digest getter for PBKDF2
Pauli [Sat, 7 Sep 2019 00:49:18 +0000 (10:49 +1000)]
Use common digest getter for PBKDF2

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoUse common digest getter for HKDF
Pauli [Sat, 7 Sep 2019 00:48:56 +0000 (10:48 +1000)]
Use common digest getter for HKDF

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoUse common define for properties, engine, cipher and digest params
Pauli [Sat, 7 Sep 2019 00:48:07 +0000 (10:48 +1000)]
Use common define for properties, engine, cipher and digest params

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoAdd 'engine' param to KDFs
Pauli [Sat, 7 Sep 2019 00:47:37 +0000 (10:47 +1000)]
Add 'engine' param to KDFs

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoKMAC using common digest get code
Pauli [Thu, 5 Sep 2019 03:55:04 +0000 (13:55 +1000)]
KMAC using common digest get code

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoHMAC using common digest get code
Pauli [Thu, 5 Sep 2019 03:54:53 +0000 (13:54 +1000)]
HMAC using common digest get code

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoGMAC using common cipher get code
Pauli [Thu, 5 Sep 2019 04:24:44 +0000 (14:24 +1000)]
GMAC using common cipher get code

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoCMAC using common cipher get code
Pauli [Thu, 5 Sep 2019 04:15:02 +0000 (14:15 +1000)]
CMAC using common cipher get code

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years agoUnify the digest getting code inside providers.
Pauli [Thu, 5 Sep 2019 03:53:20 +0000 (13:53 +1000)]
Unify the digest getting code inside providers.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9770)

5 years ago[ec/ecp_nistp*.c] restyle: use {} around `else` too
Nicola Tuveri [Fri, 6 Sep 2019 11:05:26 +0000 (14:05 +0300)]
[ec/ecp_nistp*.c] restyle: use {} around `else` too

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9511)

5 years ago[ec/ecp_nistp*.c] remove flip_endian()
Nicola Tuveri [Thu, 5 Sep 2019 22:31:45 +0000 (01:31 +0300)]
[ec/ecp_nistp*.c] remove flip_endian()

Replace flip_endian() by using the little endian specific
BN_bn2lebinpad() and BN_lebin2bn().

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9511)

5 years agoUniform BN_bn2binpad() and BN_bn2lebinpad() implementations
Nicola Tuveri [Thu, 5 Sep 2019 21:18:36 +0000 (00:18 +0300)]
Uniform BN_bn2binpad() and BN_bn2lebinpad() implementations

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9511)

5 years agoMake BN_num_bits() consttime upon BN_FLG_CONSTTIME
Nicola Tuveri [Thu, 1 Aug 2019 23:08:34 +0000 (02:08 +0300)]
Make BN_num_bits() consttime upon BN_FLG_CONSTTIME

This issue was partially addressed by commit
972c87dfc7e765bd28a4964519c362f0d3a58ca4, which hardened its callee
BN_num_bits_word() to avoid leaking the most-significant word of its
argument via branching and memory access pattern.
The commit message also reported:
> There are a few places where BN_num_bits is called on an input where
> the bit length is also secret. This does *not* fully resolve those
> cases as we still only look at the top word.

BN_num_bits() is called directly or indirectly (e.g., through
BN_num_bytes() or BN_bn2binpad() ) in various parts of the `crypto/ec`
code, notably in all the currently supported implementations of scalar
multiplication (in the generic path through ec_scalar_mul_ladder() as
well as in dedicated methods like ecp_nistp{224,256,521}.c and
ecp_nistz256.c).

Under the right conditions, a motivated SCA attacker could retrieve the
secret bitlength of a secret nonce through this vulnerability,
potentially leading, ultimately, to recover a long-term secret key.

With this commit, exclusively for BIGNUMs that are flagged with
BN_FLG_CONSTTIME, instead of accessing only bn->top, all the limbs of
the BIGNUM are accessed up to bn->dmax and bitwise masking is used to
avoid branching.

Memory access pattern still leaks bn->dmax, the size of the lazily
allocated buffer for representing the BIGNUM, which is inevitable with
the current BIGNUM architecture: reading past bn->dmax would be an
out-of-bound read.
As such, it's the caller responsibility to ensure that bn->dmax does not
leak secret information, by explicitly expanding the internal BIGNUM
buffer to a public value sufficient to avoid any lazy reallocation
while manipulating it: this should be already done at the top level
alongside setting the BN_FLG_CONSTTIME.

Thanks to David Schrammel and Samuel Weiser for reporting this issue
through responsible disclosure.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9511)

5 years agoFix a SCA leak using BN_bn2bin()
Nicola Tuveri [Thu, 1 Aug 2019 22:33:05 +0000 (01:33 +0300)]
Fix a SCA leak using BN_bn2bin()

BN_bn2bin() is not constant-time and leaks the number of bits in the
processed BIGNUM.

The specialized methods in ecp_nistp224.c, ecp_nistp256.c and
ecp_nistp521.c internally used BN_bn2bin() to convert scalars into the
internal fixed length representation.

This can leak during ECDSA/ECDH key generation or handling the nonce
while generating an ECDSA signature, when using these implementations.
The amount and risk of leaked information useful for a SCA attack
varies for each of the three curves, as it depends mainly on the
ratio between the bitlength of the curve subgroup order (governing the
size of the secret nonce/key) and the limb size for the internal BIGNUM
representation (which depends on the compilation target architecture).

To fix this, we replace BN_bn2bin() with BN_bn2binpad(), bounding the
output length to the width of the internal representation buffer: this
length is public.

Internally the final implementation of both BN_bn2binpad() and
BN_bn2bin() already has masking in place to avoid leaking bn->top
through memory access patterns.
Memory access pattern still leaks bn->dmax, the size of the lazily
allocated buffer for representing the BIGNUM, which is inevitable with
the current BIGNUM architecture: reading past bn->dmax would be an
out-of-bound read.
As such, it's the caller responsibility to ensure that bn->dmax does not
leak secret information, by explicitly expanding the internal BIGNUM
buffer to a public value sufficient to avoid any lazy reallocation
while manipulating it: this is already done at the top level alongside
setting the BN_FLG_CONSTTIME.

Finally, the internal implementation of BN_bn2binpad() indirectly calls
BN_num_bits() via BN_num_bytes(): the current implementation of
BN_num_bits() can leak information to a SCA attacker, and is addressed
in the next commit.

Thanks to David Schrammel and Samuel Weiser for reporting this issue
through responsible disclosure.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
(Merged from https://github.com/openssl/openssl/pull/9511)

5 years agoFix a SCA leak in BN_generate_dsa_nonce
Bernd Edlinger [Fri, 6 Sep 2019 06:46:46 +0000 (08:46 +0200)]
Fix a SCA leak in BN_generate_dsa_nonce

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
(Merged from https://github.com/openssl/openssl/pull/9782)

5 years ago[crypto/rsa] Set the constant-time flag in multi-prime RSA too
Cesar Pereida Garcia [Fri, 6 Sep 2019 07:48:00 +0000 (10:48 +0300)]
[crypto/rsa] Set the constant-time flag in multi-prime RSA too

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9779)

5 years ago[crypto/asn1] Fix multiple SCA vulnerabilities during RSA key validation.
Cesar Pereida Garcia [Thu, 5 Sep 2019 09:13:11 +0000 (12:13 +0300)]
[crypto/asn1] Fix multiple SCA vulnerabilities during RSA key validation.

This commit addresses multiple side-channel vulnerabilities present
during RSA key validation.
Private key parameters are re-computed using variable-time functions.

This issue was discovered and reported by the NISEC group at TAU Finland.

Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9779)

5 years agolibcrypto.num entries for KDFs
Pauli [Wed, 4 Sep 2019 22:11:48 +0000 (08:11 +1000)]
libcrypto.num entries for KDFs

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoMove OSSL_OP_KDF into its rightful place amongst the other OSSL_OP_ definitions
Pauli [Tue, 3 Sep 2019 04:30:53 +0000 (14:30 +1000)]
Move OSSL_OP_KDF into its rightful place amongst the other OSSL_OP_ definitions

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoRemove reference to legacy aliases for MAC and KDF
Pauli [Tue, 3 Sep 2019 04:29:46 +0000 (14:29 +1000)]
Remove reference to legacy aliases for MAC and KDF

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoClear collected_seed after freeing it
Pauli [Tue, 3 Sep 2019 04:28:47 +0000 (14:28 +1000)]
Clear collected_seed after freeing it

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoLowercase command line 'N' argument since params have lower case names
Pauli [Tue, 3 Sep 2019 04:26:19 +0000 (14:26 +1000)]
Lowercase command line 'N' argument since params have lower case names

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoUpdate KDF documentation (section 3)
Pauli [Mon, 2 Sep 2019 04:23:50 +0000 (14:23 +1000)]
Update KDF documentation (section 3)

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoUpdate KDF documentation (section 1)
Pauli [Mon, 2 Sep 2019 03:58:42 +0000 (13:58 +1000)]
Update KDF documentation (section 1)

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoUpdate KDF documentation (section 7)
Pauli [Mon, 2 Sep 2019 03:58:22 +0000 (13:58 +1000)]
Update KDF documentation (section 7)

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9662)

5 years agoDeal with BUF_MEM_grow ambiguity
Richard Levitte [Fri, 30 Aug 2019 14:54:47 +0000 (16:54 +0200)]
Deal with BUF_MEM_grow ambiguity

BUF_MEM_grow() returns the passed length, but also zero on error.  If
the passed length was zero, an extra check to see if a returned zero
was an error or not is needed.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9662)