-
Notifications
You must be signed in to change notification settings - Fork 182
ossl_pkey.c: Workaround: Decode with non-zero selections. #669
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
Merged
rhenium
merged 2 commits into
ruby:master
from
junaruga:wip/decode-with-non-zero-selections
Aug 25, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,30 +82,62 @@ ossl_pkey_new(EVP_PKEY *pkey) | |
| #if OSSL_OPENSSL_PREREQ(3, 0, 0) | ||
| # include <openssl/decoder.h> | ||
|
|
||
| EVP_PKEY * | ||
| ossl_pkey_read_generic(BIO *bio, VALUE pass) | ||
| static EVP_PKEY * | ||
| ossl_pkey_read(BIO *bio, const char *input_type, int selection, VALUE pass) | ||
| { | ||
| void *ppass = (void *)pass; | ||
| OSSL_DECODER_CTX *dctx; | ||
| EVP_PKEY *pkey = NULL; | ||
| int pos = 0, pos2; | ||
|
|
||
| dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, NULL, 0, NULL, NULL); | ||
| dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, input_type, NULL, NULL, | ||
| selection, NULL, NULL); | ||
| if (!dctx) | ||
| goto out; | ||
| if (OSSL_DECODER_CTX_set_pem_password_cb(dctx, ossl_pem_passwd_cb, ppass) != 1) | ||
| goto out; | ||
|
|
||
| /* First check DER */ | ||
| if (OSSL_DECODER_from_bio(dctx, bio) == 1) | ||
| if (OSSL_DECODER_CTX_set_pem_password_cb(dctx, ossl_pem_passwd_cb, | ||
| ppass) != 1) | ||
| goto out; | ||
| while (1) { | ||
| if (OSSL_DECODER_from_bio(dctx, bio) == 1) | ||
| goto out; | ||
| if (BIO_eof(bio)) | ||
| break; | ||
| pos2 = BIO_tell(bio); | ||
| if (pos2 < 0 || pos2 <= pos) | ||
| break; | ||
| ossl_clear_error(); | ||
| pos = pos2; | ||
| } | ||
| out: | ||
| OSSL_BIO_reset(bio); | ||
| OSSL_DECODER_CTX_free(dctx); | ||
| return pkey; | ||
| } | ||
|
|
||
| EVP_PKEY * | ||
| ossl_pkey_read_generic(BIO *bio, VALUE pass) | ||
| { | ||
| EVP_PKEY *pkey = NULL; | ||
| /* First check DER, then check PEM. */ | ||
| const char *input_types[] = {"DER", "PEM"}; | ||
| int input_type_num = (int)(sizeof(input_types) / sizeof(char *)); | ||
| /* | ||
| * Then check PEM; multiple OSSL_DECODER_from_bio() calls may be needed. | ||
| * Non-zero selections to try to decode. | ||
| * | ||
| * See EVP_PKEY_fromdata(3) - Selections to see all the selections. | ||
| * | ||
| * First check for private key formats. This is to keep compatibility with | ||
| * ruby/openssl < 3.0 which decoded the following as a private key. | ||
| * This is a workaround for the decoder failing to decode or returning | ||
| * bogus keys with selection 0, if a key management provider is different | ||
| * from a decoder provider. The workaround is to avoid using selection 0. | ||
| * | ||
| * Affected OpenSSL versions: >= 3.1.0, <= 3.1.2, or >= 3.0.0, <= 3.0.10 | ||
| * Fixed OpenSSL versions: 3.2, next release of the 3.1.z and 3.0.z | ||
| * | ||
| * See https://github.com/openssl/openssl/pull/21519 for details. | ||
| * | ||
| * First check for private key formats (EVP_PKEY_KEYPAIR). This is to keep | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the |
||
| * compatibility with ruby/openssl < 3.0 which decoded the following as a | ||
| * private key. | ||
| * | ||
| * $ openssl ecparam -name prime256v1 -genkey -outform PEM | ||
| * -----BEGIN EC PARAMETERS----- | ||
|
|
@@ -126,50 +158,25 @@ ossl_pkey_read_generic(BIO *bio, VALUE pass) | |
| * | ||
| * Note that we need to create the OSSL_DECODER_CTX variable each time when | ||
| * we use the different selection as a workaround. | ||
| * https://github.com/openssl/openssl/issues/20657 | ||
| * See https://github.com/openssl/openssl/issues/20657 for details. | ||
| */ | ||
| OSSL_DECODER_CTX_free(dctx); | ||
| dctx = NULL; | ||
| dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, NULL, | ||
| EVP_PKEY_KEYPAIR, NULL, NULL); | ||
| if (!dctx) | ||
| goto out; | ||
| if (OSSL_DECODER_CTX_set_pem_password_cb(dctx, ossl_pem_passwd_cb, ppass) != 1) | ||
| goto out; | ||
| while (1) { | ||
| if (OSSL_DECODER_from_bio(dctx, bio) == 1) | ||
| goto out; | ||
| if (BIO_eof(bio)) | ||
| break; | ||
| pos2 = BIO_tell(bio); | ||
| if (pos2 < 0 || pos2 <= pos) | ||
| break; | ||
| ossl_clear_error(); | ||
| pos = pos2; | ||
| } | ||
|
|
||
| OSSL_BIO_reset(bio); | ||
| OSSL_DECODER_CTX_free(dctx); | ||
| dctx = NULL; | ||
| dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "PEM", NULL, NULL, 0, NULL, NULL); | ||
| if (!dctx) | ||
| goto out; | ||
| if (OSSL_DECODER_CTX_set_pem_password_cb(dctx, ossl_pem_passwd_cb, ppass) != 1) | ||
| goto out; | ||
| while (1) { | ||
| if (OSSL_DECODER_from_bio(dctx, bio) == 1) | ||
| goto out; | ||
| if (BIO_eof(bio)) | ||
| break; | ||
| pos2 = BIO_tell(bio); | ||
| if (pos2 < 0 || pos2 <= pos) | ||
| break; | ||
| ossl_clear_error(); | ||
| pos = pos2; | ||
| int selections[] = { | ||
| EVP_PKEY_KEYPAIR, | ||
| EVP_PKEY_KEY_PARAMETERS, | ||
| EVP_PKEY_PUBLIC_KEY | ||
| }; | ||
| int selection_num = (int)(sizeof(selections) / sizeof(int)); | ||
| int i, j; | ||
|
|
||
| for (i = 0; i < input_type_num; i++) { | ||
| for (j = 0; j < selection_num; j++) { | ||
| pkey = ossl_pkey_read(bio, input_types[i], selections[j], pass); | ||
| if (pkey) { | ||
| goto out; | ||
| } | ||
| } | ||
junaruga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| out: | ||
| OSSL_DECODER_CTX_free(dctx); | ||
| return pkey; | ||
| } | ||
| #else | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.