Skip to content

Decoding PKCS#8: separate decoding of encrypted and unencrypted PKCS#8 - #15498

Closed
levitte wants to merge 9 commits into
openssl:masterfrom
levitte:pkcs8-decoding
Closed

levitte wants to merge 9 commits into
openssl:masterfrom
levitte:pkcs8-decoding

Conversation

@levitte

@levitte levitte commented May 27, 2021 •

Copy link
Copy Markdown
Member

This has us switch from the 'structure' "pkcs8" to "PrivateKeyInfo",
which is sensible considering we already have "SubjectPublicKeyInfo".
We also add "EncryptedPrivateKeyInfo", and use it for a special decoder
that detects and decrypts an EncryptedPrivateKeyInfo structured DER
blob into a PrivateKeyInfo structured DER blob and passes that on to
the next decoder implementation.

The result of this change is that PKCS#8 decryption should only happen
once per decoding instead of once for every expected key type.
Furthermore, this new decoder implementation sets the data type to the
OID of the algorithmIdentifier field, thus reducing how many decoder
implementations are tentativaly run further down the call chain.

@levitte levitte added branch: master Applies to master branch approval: review pending This pull request needs review by a committer labels May 27, 2021
@levitte

levitte commented May 27, 2021

Copy link
Copy Markdown
Member Author

Note, I haven't made any before and after performance measurements yet. Feel free to do so.

@t8m

t8m commented May 27, 2021

Copy link
Copy Markdown
Member

Unfortunately the gost test failure is relevant.

@levitte

levitte commented May 27, 2021

Copy link
Copy Markdown
Member Author

Ah, I think I know why.

@levitte

levitte commented May 27, 2021

Copy link
Copy Markdown
Member Author

The gost-engine test is much happier now 😉

@levitte

levitte commented May 27, 2021

Copy link
Copy Markdown
Member Author

Hmmmm... this wasn't a performance boost :-/

Running the program from #15199 on master:

$ time ./util/wrap.pl ./15199

real	0m0,439s
user	0m0,439s
sys	0m0,000s

on this PR:

$ time ./util/wrap.pl ./15199

real	0m1,661s
user	0m1,660s
sys	0m0,000s

@github-actions github-actions Bot added the severity: fips change The pull request changes FIPS provider sources label May 27, 2021
@levitte

levitte commented May 28, 2021

Copy link
Copy Markdown
Member Author

A profile build showed me one difference that I believe is crucial:

master:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  us/call  us/call  name    
...
  0.00      0.30     0.00    37037     0.00     0.26  ossl_decoder_from_algorithm

this PR:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
...
  4.12      0.42     0.04  1786000     0.00     0.00  ossl_decoder_from_algorithm

This is due to OSSL_DECODER_do_all_provided() re-creating every decoder method, every time it's called, i.e. it doesn't do the "normal" pre-fetching. There are technical reasons behind this, but I strongly believe that if we make that (oh, all XXX_do_all_provided()!) do the "normal" pre-fetching of methods, only the first run should take any noticable time.
A separate issue should be raised about this.

@levitte

levitte commented May 28, 2021

Copy link
Copy Markdown
Member Author

Side note: the non-caching failure is not relevant here, and is fixed by #15516

@t8m

t8m commented Jun 7, 2021

Copy link
Copy Markdown
Member

Please rebase.

levitte added 3 commits June 7, 2021 12:39
This has us switch from the 'structure' "pkcs8" to "PrivateKeyInfo",
which is sensible considering we already have "SubjectPublicKeyInfo".
We also add "EncryptedPrivateKeyInfo", and use it for a special decoder
that detects and decrypts an EncryptedPrivateKeyInfo structured DER
blob into a PrivateKeyInfo structured DER blob and passes that on to
the next decoder implementation.

The result of this change is that PKCS#8 decryption should only happen
once per decoding instead of once for every expected key type.
Furthermore, this new decoder implementation sets the data type to the
OID of the algorithmIdentifier field, thus reducing how many decoder
implementations are tentativaly run further down the call chain.

Fixes openssl#15199
The new PKCS#8 decoder implementation decodes from DER to DER.
OSSL_DECODER_CTX_add_extra() wasn't suited for this case; we had to
modify it to walk through all existing decoder implementations, and
filter out those that aren't suitable.
This also turns out to fix the possibility to have more than one extra
decoder implementation that produces the same type of encoding, for
example several different wrapper formats that all decoder into DER.

Fixes openssl#15199
Now that we have functions to get the name and properties of the
diverse implementations, we can as well display them for clarity.
@levitte

levitte commented Jun 7, 2021

Copy link
Copy Markdown
Member Author

Rebased... and squashed too, while I was at it

@t8m t8m left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor issues/questions, otherwise looks good.

Comment thread crypto/encode_decode/decoder_lib.c Resolved

@t8m t8m left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with or without adding the comment as suggested.

@t8m t8m 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 Jun 8, 2021
Comment thread crypto/encode_decode/decoder_lib.c Resolved
Comment thread doc/man7/openssl-glossary.pod Resolved

@t8m t8m left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still OK.

@openssl-machine

Copy link
Copy Markdown
Collaborator

24 hours has passed since 'approval: done' was set, but as this PR has been updated in that time the label 'approval: ready to merge' is not being automatically set. Please review the updates and set the label manually.

@t8m t8m 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 Jun 9, 2021
@levitte

levitte commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

I removed "Fixes #15199", because this PR alone isn't a proper fix for that issue

openssl-machine pushed a commit that referenced this pull request Jun 9, 2021
This has us switch from the 'structure' "pkcs8" to "PrivateKeyInfo",
which is sensible considering we already have "SubjectPublicKeyInfo".
We also add "EncryptedPrivateKeyInfo", and use it for a special decoder
that detects and decrypts an EncryptedPrivateKeyInfo structured DER
blob into a PrivateKeyInfo structured DER blob and passes that on to
the next decoder implementation.

The result of this change is that PKCS#8 decryption should only happen
once per decoding instead of once for every expected key type.
Furthermore, this new decoder implementation sets the data type to the
OID of the algorithmIdentifier field, thus reducing how many decoder
implementations are tentativaly run further down the call chain.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from #15498)
openssl-machine pushed a commit that referenced this pull request Jun 9, 2021
The new PKCS#8 decoder implementation decodes from DER to DER.
OSSL_DECODER_CTX_add_extra() wasn't suited for this case; we had to
modify it to walk through all existing decoder implementations, and
filter out those that aren't suitable.
This also turns out to fix the possibility to have more than one extra
decoder implementation that produces the same type of encoding, for
example several different wrapper formats that all decoder into DER.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from #15498)
openssl-machine pushed a commit that referenced this pull request Jun 9, 2021
Now that we have functions to get the name and properties of the
diverse implementations, we can as well display them for clarity.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from #15498)
@levitte

levitte commented Jun 9, 2021

Copy link
Copy Markdown
Member Author

Merged

6a2b8ff Decoding PKCS#8: separate decoding of encrypted and unencrypted PKCS#8
8ea5a6b DECODER: Adapt addition of extra decoder implementations
ef2194c DECODER & ENCODER: Add better tracing

@levitte levitte closed this Jun 9, 2021
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
This has us switch from the 'structure' "pkcs8" to "PrivateKeyInfo",
which is sensible considering we already have "SubjectPublicKeyInfo".
We also add "EncryptedPrivateKeyInfo", and use it for a special decoder
that detects and decrypts an EncryptedPrivateKeyInfo structured DER
blob into a PrivateKeyInfo structured DER blob and passes that on to
the next decoder implementation.

The result of this change is that PKCS#8 decryption should only happen
once per decoding instead of once for every expected key type.
Furthermore, this new decoder implementation sets the data type to the
OID of the algorithmIdentifier field, thus reducing how many decoder
implementations are tentativaly run further down the call chain.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from openssl#15498)
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
The new PKCS#8 decoder implementation decodes from DER to DER.
OSSL_DECODER_CTX_add_extra() wasn't suited for this case; we had to
modify it to walk through all existing decoder implementations, and
filter out those that aren't suitable.
This also turns out to fix the possibility to have more than one extra
decoder implementation that produces the same type of encoding, for
example several different wrapper formats that all decoder into DER.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from openssl#15498)
devnexen pushed a commit to devnexen/openssl that referenced this pull request Jul 7, 2021
Now that we have functions to get the name and properties of the
diverse implementations, we can as well display them for clarity.

Reviewed-by: Tomas Mraz <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from openssl#15498)
@levitte
levitte deleted the pkcs8-decoding branch July 8, 2026 08:37
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 Applies to master branch severity: fips change The pull request changes FIPS provider sources

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants