-
Notifications
You must be signed in to change notification settings - Fork 711
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
Change pkey parse methods to return s2n_result #4271
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 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 |
---|---|---|
|
@@ -40,7 +40,7 @@ int s2n_pkey_zero_init(struct s2n_pkey *pkey) | |
return 0; | ||
} | ||
|
||
int s2n_pkey_setup_for_type(struct s2n_pkey *pkey, s2n_pkey_type pkey_type) | ||
S2N_RESULT s2n_pkey_setup_for_type(struct s2n_pkey *pkey, s2n_pkey_type pkey_type) | ||
{ | ||
switch (pkey_type) { | ||
case S2N_PKEY_TYPE_RSA: | ||
|
@@ -51,9 +51,9 @@ int s2n_pkey_setup_for_type(struct s2n_pkey *pkey, s2n_pkey_type pkey_type) | |
return s2n_rsa_pss_pkey_init(pkey); | ||
case S2N_PKEY_TYPE_SENTINEL: | ||
case S2N_PKEY_TYPE_UNKNOWN: | ||
POSIX_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED); | ||
RESULT_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED); | ||
} | ||
POSIX_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED); | ||
RESULT_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED); | ||
} | ||
|
||
int s2n_pkey_check_key_exists(const struct s2n_pkey *pkey) | ||
|
@@ -129,7 +129,7 @@ int s2n_pkey_free(struct s2n_pkey *key) | |
return S2N_SUCCESS; | ||
} | ||
|
||
int s2n_asn1der_to_private_key(struct s2n_pkey *priv_key, struct s2n_blob *asn1der, int type_hint) | ||
S2N_RESULT s2n_asn1der_to_private_key(struct s2n_pkey *priv_key, struct s2n_blob *asn1der, int type_hint) | ||
{ | ||
const unsigned char *key_to_parse = asn1der->data; | ||
|
||
|
@@ -151,106 +151,81 @@ int s2n_asn1der_to_private_key(struct s2n_pkey *priv_key, struct s2n_blob *asn1d | |
if (evp_private_key == NULL) { | ||
evp_private_key = d2i_PrivateKey(type_hint, NULL, &key_to_parse, asn1der->size); | ||
} | ||
POSIX_ENSURE(evp_private_key, S2N_ERR_DECODE_PRIVATE_KEY); | ||
RESULT_ENSURE(evp_private_key, S2N_ERR_DECODE_PRIVATE_KEY); | ||
|
||
/* If key parsing is successful, d2i_AutoPrivateKey increments *key_to_parse to the byte following the parsed data */ | ||
uint32_t parsed_len = key_to_parse - asn1der->data; | ||
if (parsed_len != asn1der->size) { | ||
POSIX_BAIL(S2N_ERR_DECODE_PRIVATE_KEY); | ||
} | ||
RESULT_ENSURE(parsed_len == asn1der->size, S2N_ERR_DECODE_PRIVATE_KEY); | ||
|
||
/* Initialize s2n_pkey according to key type */ | ||
int type = EVP_PKEY_base_id(evp_private_key); | ||
|
||
int ret; | ||
switch (type) { | ||
case EVP_PKEY_RSA: | ||
ret = s2n_rsa_pkey_init(priv_key); | ||
if (ret != 0) { | ||
break; | ||
} | ||
ret = s2n_evp_pkey_to_rsa_private_key(&priv_key->key.rsa_key, evp_private_key); | ||
RESULT_GUARD(s2n_rsa_pkey_init(priv_key)); | ||
RESULT_GUARD(s2n_evp_pkey_to_rsa_private_key(&priv_key->key.rsa_key, evp_private_key)); | ||
break; | ||
case EVP_PKEY_RSA_PSS: | ||
ret = s2n_rsa_pss_pkey_init(priv_key); | ||
if (ret != 0) { | ||
break; | ||
} | ||
ret = s2n_evp_pkey_to_rsa_pss_private_key(&priv_key->key.rsa_key, evp_private_key); | ||
RESULT_GUARD(s2n_rsa_pss_pkey_init(priv_key)); | ||
RESULT_GUARD(s2n_evp_pkey_to_rsa_pss_private_key(&priv_key->key.rsa_key, evp_private_key)); | ||
break; | ||
case EVP_PKEY_EC: | ||
ret = s2n_ecdsa_pkey_init(priv_key); | ||
if (ret != 0) { | ||
break; | ||
} | ||
ret = s2n_evp_pkey_to_ecdsa_private_key(&priv_key->key.ecdsa_key, evp_private_key); | ||
RESULT_GUARD(s2n_ecdsa_pkey_init(priv_key)); | ||
RESULT_GUARD(s2n_evp_pkey_to_ecdsa_private_key(&priv_key->key.ecdsa_key, evp_private_key)); | ||
break; | ||
default: | ||
POSIX_BAIL(S2N_ERR_DECODE_PRIVATE_KEY); | ||
RESULT_BAIL(S2N_ERR_DECODE_PRIVATE_KEY); | ||
} | ||
|
||
priv_key->pkey = evp_private_key; | ||
/* Reset to avoid DEFER_CLEANUP freeing our key */ | ||
evp_private_key = NULL; | ||
ZERO_TO_DISABLE_DEFER_CLEANUP(evp_private_key); | ||
|
||
return ret; | ||
return S2N_RESULT_OK; | ||
} | ||
|
||
int s2n_asn1der_to_public_key_and_type(struct s2n_pkey *pub_key, s2n_pkey_type *pkey_type_out, struct s2n_blob *asn1der) | ||
S2N_RESULT s2n_asn1der_to_public_key_and_type(struct s2n_pkey *pub_key, | ||
s2n_pkey_type *pkey_type_out, struct s2n_blob *asn1der) | ||
{ | ||
uint8_t *cert_to_parse = asn1der->data; | ||
DEFER_CLEANUP(X509 *cert = NULL, X509_free_pointer); | ||
|
||
cert = d2i_X509(NULL, (const unsigned char **) (void *) &cert_to_parse, asn1der->size); | ||
S2N_ERROR_IF(cert == NULL, S2N_ERR_DECODE_CERTIFICATE); | ||
RESULT_ENSURE(cert != NULL, S2N_ERR_DECODE_CERTIFICATE); | ||
|
||
/* If cert parsing is successful, d2i_X509 increments *cert_to_parse to the byte following the parsed data */ | ||
uint32_t parsed_len = cert_to_parse - asn1der->data; | ||
|
||
/* Some TLS clients in the wild send extra trailing bytes after the Certificate. | ||
* Allow this in s2n for backwards compatibility with existing clients. */ | ||
uint32_t trailing_bytes = asn1der->size - parsed_len; | ||
POSIX_ENSURE(trailing_bytes <= S2N_MAX_ALLOWED_CERT_TRAILING_BYTES, S2N_ERR_DECODE_CERTIFICATE); | ||
RESULT_ENSURE(trailing_bytes <= S2N_MAX_ALLOWED_CERT_TRAILING_BYTES, S2N_ERR_DECODE_CERTIFICATE); | ||
|
||
DEFER_CLEANUP(EVP_PKEY *evp_public_key = X509_get_pubkey(cert), EVP_PKEY_free_pointer); | ||
S2N_ERROR_IF(evp_public_key == NULL, S2N_ERR_DECODE_CERTIFICATE); | ||
RESULT_ENSURE(evp_public_key != NULL, S2N_ERR_DECODE_CERTIFICATE); | ||
|
||
/* Check for success in decoding certificate according to type */ | ||
int type = EVP_PKEY_base_id(evp_public_key); | ||
|
||
int ret; | ||
switch (type) { | ||
case EVP_PKEY_RSA: | ||
ret = s2n_rsa_pkey_init(pub_key); | ||
if (ret != 0) { | ||
break; | ||
} | ||
ret = s2n_evp_pkey_to_rsa_public_key(&pub_key->key.rsa_key, evp_public_key); | ||
RESULT_GUARD(s2n_rsa_pkey_init(pub_key)); | ||
RESULT_GUARD(s2n_evp_pkey_to_rsa_public_key(&pub_key->key.rsa_key, evp_public_key)); | ||
*pkey_type_out = S2N_PKEY_TYPE_RSA; | ||
break; | ||
case EVP_PKEY_RSA_PSS: | ||
ret = s2n_rsa_pss_pkey_init(pub_key); | ||
if (ret != 0) { | ||
break; | ||
} | ||
ret = s2n_evp_pkey_to_rsa_pss_public_key(&pub_key->key.rsa_key, evp_public_key); | ||
RESULT_GUARD(s2n_rsa_pss_pkey_init(pub_key)); | ||
RESULT_GUARD(s2n_evp_pkey_to_rsa_pss_public_key(&pub_key->key.rsa_key, evp_public_key)); | ||
*pkey_type_out = S2N_PKEY_TYPE_RSA_PSS; | ||
break; | ||
case EVP_PKEY_EC: | ||
ret = s2n_ecdsa_pkey_init(pub_key); | ||
if (ret != 0) { | ||
break; | ||
} | ||
ret = s2n_evp_pkey_to_ecdsa_public_key(&pub_key->key.ecdsa_key, evp_public_key); | ||
RESULT_GUARD(s2n_ecdsa_pkey_init(pub_key)); | ||
RESULT_GUARD(s2n_evp_pkey_to_ecdsa_public_key(&pub_key->key.ecdsa_key, evp_public_key)); | ||
*pkey_type_out = S2N_PKEY_TYPE_ECDSA; | ||
break; | ||
default: | ||
POSIX_BAIL(S2N_ERR_DECODE_CERTIFICATE); | ||
RESULT_BAIL(S2N_ERR_DECODE_CERTIFICATE); | ||
} | ||
|
||
pub_key->pkey = evp_public_key; | ||
/* Reset to avoid DEFER_CLEANUP freeing our key */ | ||
evp_public_key = NULL; | ||
ZERO_TO_DISABLE_DEFER_CLEANUP(evp_public_key); | ||
|
||
return ret; | ||
return S2N_RESULT_OK; | ||
Comment on lines
218
to
+230
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. This pattern of breaking on failure likely predates DEFER_CLEANUP. Without DEFER_CLEANUP, you'd want to break so that you can still set pkey and have the key cleaned up later, just like on success. With DEFER_CLEANUP, this is unnecessary. |
||
} |
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.
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.
This is the only non-trivial RESULT_ENSURE swap/refactor I did