-
Notifications
You must be signed in to change notification settings - Fork 143
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
Update API usage to OpenSSL 3 (RSA) #993
Merged
Lagovas
merged 21 commits into
cossacklabs:openssl3-support
from
iamnotacake:anatolii-T2710-openssl3-porting-rsa
Mar 6, 2023
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5aee50e
Move common RSA util functinos to separate file
iamnotacake bcd0dd5
Create copy of soter_rsa_key.c for OpenSSL 3
iamnotacake fc3df8e
Rewrite RSA keys serialization
iamnotacake bc8f8ef
Implement RSA key deserialization, few related fixes
iamnotacake cc8bac5
Fix build, minor updates
iamnotacake 7d33ed0
Fix build
iamnotacake c244dc8
Fix build
iamnotacake cc289b2
Fix build
iamnotacake 9678f51
Fix build
iamnotacake c3c7c1f
Updates after review
iamnotacake 8c6a531
Simplify bigint serialization, remove redundant functions
iamnotacake 83d24ac
Make RSA key serialization functions reuse bigint
iamnotacake 565f753
Zeroize partially serialized RSA private key on fail
iamnotacake 364b646
Updates after review
iamnotacake 83f07f9
Updates after review
iamnotacake c65552f
Enable building on Ubuntu 22.04, remove experimental flag
iamnotacake 8e65111
Remove unneeded CI job
iamnotacake 10452fc
Enable build with warnings as errors
iamnotacake f6b69fb
Minor updates after review
iamnotacake 8ecea16
Fix build
iamnotacake 3792ead
Add few more checks, move common function to separate file
iamnotacake 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 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 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 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 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 |
---|---|---|
|
@@ -17,7 +17,11 @@ | |
#include "soter/soter_asym_cipher.h" | ||
|
||
#include <openssl/evp.h> | ||
#include <openssl/opensslv.h> | ||
#include <openssl/rsa.h> | ||
#if OPENSSL_VERSION_NUMBER >= 0x30000000 | ||
#include <openssl/core_names.h> | ||
#endif | ||
|
||
#include "soter/openssl/soter_engine.h" | ||
#include "soter/soter_api.h" | ||
|
@@ -30,6 +34,63 @@ | |
#define SOTER_RSA_KEY_LENGTH 2048 | ||
#endif | ||
|
||
#if OPENSSL_VERSION_NUMBER >= 0x30000000 | ||
static bool get_rsa_mod_size(EVP_PKEY* pkey, int* rsa_mod_size) | ||
{ | ||
if (!EVP_PKEY_get_int_param(pkey, OSSL_PKEY_PARAM_BITS, rsa_mod_size)) { | ||
return false; | ||
} | ||
*rsa_mod_size = (*rsa_mod_size + 7) / 8; | ||
return true; | ||
} | ||
#else | ||
static bool get_rsa_mod_size(EVP_PKEY* pkey, int* rsa_mod_size) | ||
{ | ||
RSA* rsa = EVP_PKEY_get0(pkey); | ||
if (!rsa) { | ||
return false; | ||
} | ||
*rsa_mod_size = RSA_size(rsa); | ||
return true; | ||
} | ||
#endif | ||
|
||
#if OPENSSL_VERSION_NUMBER < 0x10100000L | ||
static bool check_rsa_private_key(EVP_PKEY* pkey, EVP_PKEY_CTX* pkey_ctx) | ||
{ | ||
UNUSED(pkey_ctx); | ||
RSA* rsa = EVP_PKEY_get0(pkey); | ||
|
||
if (rsa->d == NULL) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
#elif OPENSSL_VERSION_NUMBER < 0x30000000 | ||
static bool check_rsa_private_key(EVP_PKEY* pkey, EVP_PKEY_CTX* pkey_ctx) | ||
{ | ||
UNUSED(pkey_ctx); | ||
RSA* rsa = EVP_PKEY_get0(pkey); | ||
const BIGNUM* d = NULL; | ||
|
||
RSA_get0_key(rsa, NULL, NULL, &d); | ||
|
||
if (NULL == d) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
#else /* OPENSSL_VERSION_NUMBER >= 0x30000000 */ | ||
static bool check_rsa_private_key(EVP_PKEY* pkey, EVP_PKEY_CTX* pkey_ctx) | ||
{ | ||
UNUSED(pkey); | ||
// EVP_PKEY_private_check was added in 3.0 | ||
return EVP_PKEY_private_check(pkey_ctx) == 1; | ||
} | ||
#endif | ||
|
||
soter_status_t soter_asym_cipher_import_key(soter_asym_cipher_t* asym_cipher_ctx, | ||
const void* key, | ||
size_t key_length) | ||
|
@@ -139,7 +200,6 @@ soter_status_t soter_asym_cipher_encrypt(soter_asym_cipher_t* asym_cipher, | |
size_t* cipher_data_length) | ||
{ | ||
EVP_PKEY* pkey; | ||
RSA* rsa; | ||
int rsa_mod_size; | ||
size_t output_length; | ||
|
||
|
@@ -158,12 +218,10 @@ soter_status_t soter_asym_cipher_encrypt(soter_asym_cipher_t* asym_cipher, | |
return SOTER_INVALID_PARAMETER; | ||
} | ||
|
||
rsa = EVP_PKEY_get0(pkey); | ||
if (NULL == rsa) { | ||
if (!get_rsa_mod_size(pkey, &rsa_mod_size)) { | ||
return SOTER_FAIL; | ||
} | ||
|
||
rsa_mod_size = RSA_size(rsa); | ||
int oaep_max_payload_length = (rsa_mod_size - 2 - (2 * OAEP_HASH_SIZE)); | ||
if (oaep_max_payload_length < 0 || plain_data_length > (size_t)oaep_max_payload_length) { | ||
/* The plaindata is too large for this key size */ | ||
|
@@ -223,8 +281,6 @@ soter_status_t soter_asym_cipher_decrypt(soter_asym_cipher_t* asym_cipher, | |
size_t* plain_data_length) | ||
{ | ||
EVP_PKEY* pkey; | ||
RSA* rsa; | ||
const BIGNUM* d = NULL; | ||
int rsa_mod_size; | ||
size_t output_length; | ||
|
||
|
@@ -243,13 +299,10 @@ soter_status_t soter_asym_cipher_decrypt(soter_asym_cipher_t* asym_cipher, | |
return SOTER_INVALID_PARAMETER; | ||
} | ||
|
||
rsa = EVP_PKEY_get0(pkey); | ||
if (NULL == rsa) { | ||
if (!get_rsa_mod_size(pkey, &rsa_mod_size)) { | ||
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. now code looks cleaner |
||
return SOTER_FAIL; | ||
} | ||
|
||
rsa_mod_size = RSA_size(rsa); | ||
|
||
if (rsa_mod_size < 0 || cipher_data_length < (size_t)rsa_mod_size) { | ||
/* The cipherdata is too small for this key size */ | ||
return SOTER_INVALID_PARAMETER; | ||
|
@@ -258,12 +311,7 @@ soter_status_t soter_asym_cipher_decrypt(soter_asym_cipher_t* asym_cipher, | |
/* we can only decrypt, if we have the private key */ | ||
/* some versions of OpenSSL just crash, if you send RSA public key to EVP_PKEY_decrypt, so we do | ||
* checks here */ | ||
#if OPENSSL_VERSION_NUMBER < 0x10100000L | ||
d = rsa->d; | ||
#else | ||
RSA_get0_key(rsa, NULL, NULL, &d); | ||
#endif | ||
if (NULL == d) { | ||
if (!check_rsa_private_key(pkey, asym_cipher->pkey_ctx)) { | ||
return SOTER_INVALID_PARAMETER; | ||
} | ||
|
||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2023 Cossack Labs Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef THEMIS_SOTER_BIGNUM_UTILS_H | ||
#define THEMIS_SOTER_BIGNUM_UTILS_H | ||
|
||
#include <openssl/bn.h> | ||
#include <openssl/opensslv.h> | ||
|
||
#if OPENSSL_VERSION_NUMBER < 0x10100000L | ||
/* Simple implementation for OpenSSL <1.1.0 where this function is missing */ | ||
static int BN_bn2binpad(const BIGNUM* a, unsigned char* to, int tolen) | ||
{ | ||
int bn_size = BN_num_bytes(a); | ||
int bytes_copied; | ||
|
||
if (a == NULL || to == NULL) { | ||
return -1; | ||
} | ||
|
||
if (tolen < bn_size) { | ||
return -1; | ||
} | ||
|
||
bytes_copied = BN_bn2bin(a, to + (tolen - bn_size)); | ||
|
||
if (bytes_copied != bn_size) { | ||
return -1; | ||
} | ||
|
||
memset(to, 0, (size_t)(tolen - bn_size)); | ||
|
||
return tolen; | ||
} | ||
#endif | ||
|
||
#endif /* THEMIS_SOTER_BIGNUM_UTILS_H */ |
This file contains 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 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed this line, will remove in next commit. BTW, why MSYS2 environment job fails like there is still "no support for OpenSSL 3" in header file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... Looks like the package uses the pristine 0.14.0 tarball, not the source from the current tree.
That is, when you build a package, it's built locally but still references the tarball from release artifacts, not packages the source from whatever the current branch contains. And it's been doing that for a while. Says a lot about how well-maintained MSYS2 builds are.
Uh, well... I think you should leave
WITH_EXPERIMENTAL_OPENSSL_3_SUPPORT: yes
here for the time being. Then someone 🙄 should apply whatever hacks necessary for this job to package the tree being tested.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kinda weird seeing Pacman being used on Windows. Anyway, added this line back and another comment instead, since "stop using deprecated API" is not relevant anymore