Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoder key export fixes #21519

Closed
wants to merge 5 commits into from
Closed

Conversation

t8m
Copy link
Member

@t8m t8m commented Jul 21, 2023

This corrects two serious problems - one in decoders where we try to export/import things with 0 selection which is not meaningful and the second in the actual keymgmt export functions which instead of failing on such export call, they produce bogus (empty) keys.

Fixes #21493

This is already correct in the rsa_kmgmt.c but other
implementations are wrong.
@t8m t8m added branch: master Merge to master branch triaged: bug The issue/pr is/fixes a bug branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 labels Jul 21, 2023
@t8m
Copy link
Member Author

t8m commented Jul 21, 2023

I'll add testcases next.

@github-actions github-actions bot added the severity: fips change The pull request changes FIPS provider sources label Jul 21, 2023
t8m added 2 commits July 21, 2023 17:40
When decoding 0 as the selection means to decode anything
you get.

However when exporting and then importing the key data 0 as
selection is not meaningful.
So we set it to OSSL_KEYMGMT_SELECT_ALL to make the export/import
function export/import everything that we have decoded.

Fixes openssl#21493
@paulidale paulidale removed the approval: otc review pending This pull request needs review by an OTC member label Jul 24, 2023
@t8m t8m requested review from hlandau and mattcaswell July 24, 2023 08:58
@junaruga
Copy link

junaruga commented Jul 24, 2023

master branch

I tested this PR with my reproducer, the x25519 public key PEM file, and the OpenSSL on the current latest master branch 06a0d40 plus the commits in this PR. And I can see the encoded and decoded text is same with the original one!

master branch plus this PR's commits

The encoded and decode text is same with the original one. It's okay.

$ cat x25519_pub.pem
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VuAyEA3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08=
-----END PUBLIC KEY-----

$ gcc \
  -I /home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322-plus-pr-21519/include \
  -L /home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322-plus-pr-21519/lib \
  -o reproducer_pr_21519 reproducer.c -lcrypto

$ OPENSSL_CONF=$(pwd)/openssl_fips.cnf \
  OPENSSL_CONF_INCLUDE=/home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322-plus-pr-21519/ssl \ 
  OPENSSL_MODULES=/home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322-plus-pr-21519/lib/ossl-modules \
  LD_LIBRARY_PATH=/home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322-plus-pr-21519/lib \
  ./reproducer_pr_21519 x25519_pub.pem
[DEBUG] Loaded providers:
  fips
  base
[DEBUG] FIPS mode enabled: 1
[DEBUG] data_size: 113
[DEBUG] OSSL_DECODER_from_bio PEM 2 failed.
[DEBUG] Got a pkey! 0xbe9e40
[DEBUG] It's held by the provider fips
[DEBUG] ossl_membio2str buf->data:
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VuAyEA3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08=
-----END PUBLIC KEY-----

master branch (without this PR's commits)

And the encoded and decoded text is different from the original one on the current master branch 06a0d40 without this PR's commits. This is the expected behavior. I checked it just in case.

$ gcc \
  -I /home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322/include \
  -L /home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322/lib \
  -o reproducer reproducer.c -lcrypto

$ OPENSSL_CONF=$(pwd)/openssl_fips.cnf \
  OPENSSL_CONF_INCLUDE=/home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322/ssl \
  OPENSSL_MODULES=/home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322/lib/ossl-modules \
  LD_LIBRARY_PATH=/home/jaruga/.local/openssl-3.2.0.dev-fips-debug-06a0d40322/lib \
  ./reproducer x25519_pub.pem
[DEBUG] Loaded providers:
  fips
  base
[DEBUG] FIPS mode enabled: 1
[DEBUG] data_size: 113
[DEBUG] OSSL_DECODER_from_bio PEM 2 failed.
[DEBUG] Got a pkey! 0x83adf0
[DEBUG] It's held by the provider fips
[DEBUG] ossl_membio2str buf->data:
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VuAyEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
-----END PUBLIC KEY-----

@junaruga
Copy link

I tested this PR on the lastet openssl-3.1 and openssl-3.1 branches too. It looks good to me!

openssl-3.1 branch plus this PR's commits

$ gcc \
  -I /home/jaruga/.local/openssl-3.1.2.dev-fips-debug-cead7e5adf-plus-pr-21519/include \
  -L /home/jaruga/.local/openssl-3.1.2.dev-fips-debug-cead7e5adf-plus-pr-21519/lib \
  -o reproducer_pr_21519_312 reproducer.c -lcrypto
$ OPENSSL_CONF=$(pwd)/openssl_fips.cnf \
  OPENSSL_CONF_INCLUDE=/home/jaruga/.local/openssl-3.1.2.dev-fips-debug-cead7e5adf-plus-pr-21519/ssl \
  OPENSSL_MODULES=/home/jaruga/.local/openssl-3.1.2.dev-fips-debug-cead7e5adf-plus-pr-21519/lib/ossl-modules \
  LD_LIBRARY_PATH=/home/jaruga/.local/openssl-3.1.2.dev-fips-debug-cead7e5adf-plus-pr-21519/lib \
  ./reproducer x25519_pub.pem
[DEBUG] Loaded providers:
  fips
  base
[DEBUG] FIPS mode enabled: 1
[DEBUG] data_size: 113
[DEBUG] OSSL_DECODER_from_bio PEM 2 failed.
[DEBUG] Got a pkey! 0x1c6fe40
[DEBUG] It's held by the provider fips
[DEBUG] ossl_membio2str buf->data:
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VuAyEA3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08=
-----END PUBLIC KEY-----

openssl-3.0 branch plus this PR's commits

I tested both x25519 and ed25519 cases.

$ gcc \
  -I /home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/include \
  -L /home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/lib \
  -o reproducer_pr_21519_3010 reproducer.c -lcrypto

crypto: x25519

$ OPENSSL_CONF=$(pwd)/openssl_fips.cnf \
  OPENSSL_CONF_INCLUDE=/home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/ssl \
  OPENSSL_MODULES=/home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/lib/ossl-modules \
  LD_LIBRARY_PATH=/home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/lib \
  ./reproducer_pr_21519_3010 x25519_pub.pem
[DEBUG] Loaded providers:
  fips
  base
[DEBUG] FIPS mode enabled: 1
[DEBUG] data_size: 113
[DEBUG] OSSL_DECODER_from_bio PEM 2 failed.
[DEBUG] Got a pkey! 0x218c550
[DEBUG] It's held by the provider fips
[DEBUG] ossl_membio2str buf->data:
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VuAyEA3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08=
-----END PUBLIC KEY-----

crypto: ed25519

$ cat ed25519_pub.pem
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAPUAXw+hDiVqStwqnTRt+vJyYLM8uxJaMwM1V8Sr0Zgw=
-----END PUBLIC KEY-----
$ OPENSSL_CONF=$(pwd)/openssl_fips.cnf \
  OPENSSL_CONF_INCLUDE=/home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/ssl \
  OPENSSL_MODULES=/home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/lib/ossl-modules \
  LD_LIBRARY_PATH=/home/jaruga/.local/openssl-3.0.10.dev-fips-debug-6bcb6d297e-plus-pr-21519/lib \
  ./reproducer_pr_21519_3010 ed25519_pub.pem
[DEBUG] Loaded providers:
  fips
  base
[DEBUG] FIPS mode enabled: 1
[DEBUG] data_size: 113
[DEBUG] OSSL_DECODER_from_bio PEM 2 failed.
[DEBUG] Got a pkey! 0xf7c550
[DEBUG] It's held by the provider fips
[DEBUG] ossl_membio2str buf->data:
-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAPUAXw+hDiVqStwqnTRt+vJyYLM8uxJaMwM1V8Sr0Zgw=
-----END PUBLIC KEY-----

@t8m
Copy link
Member Author

t8m commented Jul 25, 2023

@paulidale please reconfirm
@mattcaswell fixup pushed

@mattcaswell
Copy link
Member

@paulidale - please reconfirm

@paulidale paulidale added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Jul 30, 2023
@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Aug 1, 2023
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

@tmshort tmshort self-assigned this Aug 4, 2023
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
This is already correct in the rsa_kmgmt.c but other
implementations are wrong.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
When decoding 0 as the selection means to decode anything
you get.

However when exporting and then importing the key data 0 as
selection is not meaningful.
So we set it to OSSL_KEYMGMT_SELECT_ALL to make the export/import
function export/import everything that we have decoded.

Fixes #21493

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
This is already correct in the rsa_kmgmt.c but other
implementations are wrong.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)

(cherry picked from commit 1ae4678)
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
When decoding 0 as the selection means to decode anything
you get.

However when exporting and then importing the key data 0 as
selection is not meaningful.
So we set it to OSSL_KEYMGMT_SELECT_ALL to make the export/import
function export/import everything that we have decoded.

Fixes #21493

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)

(cherry picked from commit 2acb0d3)
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)

(cherry picked from commit 4c50610)
@tmshort
Copy link
Contributor

tmshort commented Aug 4, 2023

Merged to master, openssl-3.1 and openssl-3.0. Thank you!

@tmshort tmshort closed this Aug 4, 2023
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
This is already correct in the rsa_kmgmt.c but other
implementations are wrong.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)

(cherry picked from commit 1ae4678)
(cherry picked from commit 8865d7c)
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
When decoding 0 as the selection means to decode anything
you get.

However when exporting and then importing the key data 0 as
selection is not meaningful.
So we set it to OSSL_KEYMGMT_SELECT_ALL to make the export/import
function export/import everything that we have decoded.

Fixes #21493

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)

(cherry picked from commit 2acb0d3)
(cherry picked from commit 137ba05)
openssl-machine pushed a commit that referenced this pull request Aug 4, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from #21519)

(cherry picked from commit 4c50610)
(cherry picked from commit 42f32b4)
@junaruga
Copy link

junaruga commented Aug 4, 2023

Thank you!

@junaruga
Copy link

junaruga commented Aug 17, 2023

Merged to master, openssl-3.1 and openssl-3.0. Thank you!

I just put the note that this PR's commits were merged as 3 commits to master, openssl-3.1 and openssl-3.0 branches for someone who is interested in it.

@levitte
Copy link
Member

levitte commented Aug 17, 2023

... with 0 selection which is not meaningful

Actually, from man OSSL_DECODER_CTX_new_for_pkey:

The search of decoder implementations can also be limited with keytype and selection, which specifies the expected resulting keytype and contents. NULL and zero are valid and signify that the decoder implementations will find out the keytype and key contents on their own from the input they get.

So, the selection 0 means "get me whatever you can", and leaves it to the application to discover (through probing) what it actually got.

@t8m
Copy link
Member Author

t8m commented Aug 17, 2023

... with 0 selection which is not meaningful

Actually, from man OSSL_DECODER_CTX_new_for_pkey:

The search of decoder implementations can also be limited with keytype and selection, which specifies the expected resulting keytype and contents. NULL and zero are valid and signify that the decoder implementations will find out the keytype and key contents on their own from the input they get.

So, the selection 0 means "get me whatever you can", and leaves it to the application to discover (through probing) what it actually got.

Yes, it is meaningful for OSSL_DECODER_CTX_new_for_pkey() but not for (at least in the current implementation of providers) for keymgmt import and export functions.

xl32 pushed a commit to xl32/openssl that referenced this pull request Sep 29, 2023
This is already correct in the rsa_kmgmt.c but other
implementations are wrong.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from openssl#21519)

(cherry picked from commit 1ae4678)
(cherry picked from commit 8865d7c)
xl32 pushed a commit to xl32/openssl that referenced this pull request Sep 29, 2023
When decoding 0 as the selection means to decode anything
you get.

However when exporting and then importing the key data 0 as
selection is not meaningful.
So we set it to OSSL_KEYMGMT_SELECT_ALL to make the export/import
function export/import everything that we have decoded.

Fixes openssl#21493

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from openssl#21519)

(cherry picked from commit 2acb0d3)
(cherry picked from commit 137ba05)
xl32 pushed a commit to xl32/openssl that referenced this pull request Sep 29, 2023
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Todd Short <todd.short@me.com>
(Merged from openssl#21519)

(cherry picked from commit 4c50610)
(cherry picked from commit 42f32b4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 severity: fips change The pull request changes FIPS provider sources tests: present The PR has suitable tests present triaged: bug The issue/pr is/fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenSSL 3: x25519 a decode from and then encode to a pem file corrupts the key if fips+base provider is used
7 participants