-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add support for trusted CA callbacks #2532
Merged
Merged
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
288dedc
Add compile-time option to enable X.509 CA callbacks
5c8df78
Add X.509 CRT verification API using trusted CA callbacks
902451d
Improve documentation of old X.509 CRT verification functions
8bf74f3
Add SSL configuration API for trusted CA callbacks
03cd120
Test for ca list callback
912ed33
Change callback name to ca_callback
557426a
Add a failure testcase for ca callback
1b4a2ba
Add possibility to use ca_callbacks in ssl programs
5adaad9
Add X.509 CA callback to SSL configuration and implement setter API
afd0b0a
Make use of CA callback if present when verifying peer CRT chain
3116fb3
Add prototype for CRT verification with static and dynamic CA list
f53893b
Implement X.509 CRT verification using CA callback
e15dae7
Declare CA callback type even if feature is disabled
cbb5903
Minor fixes to CA callback tests
0350d56
Only run X.509 CRT verification tests with CA callback tests if !CRL
746aaf3
Add ssl-opt.sh tests for trusted CA callbacks
fa738d1
Update query_config.c
3f932bb
Remove trailing whitespace in test_suite_x509parse.function
fed5d9d
Update version_features.c
1bac87c
Correct placement of usage macro in ssl_client2
d6d100b
Fix ssl_client2 and ssl_server2 if !PLATFORM_C
31d9db6
Change the verify function naming
f49fedc
Change docs according to review comments
2ee67a6
Remove mbedtls_ from the static function name
f7a7f9e
Address review comments regarding ssl_client2 and ssl tests
dfd22c4
Address comments for x509 tests
9822c0d
Fix name to function call
d7ecbd6
Fix style issues and a typo
yanesca 846ae7a
Document and test flags in x509_verify
yanesca File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,6 +166,14 @@ typedef struct | |
{ | ||
mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE]; | ||
unsigned len; | ||
|
||
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | ||
/* This stores the list of potential trusted signers obtained from | ||
* the CA callback used for the CRT verification, if configured. | ||
* We must track it somewhere because the callback passes its | ||
* ownership to the caller. */ | ||
mbedtls_x509_crt *trust_ca_cb_result; | ||
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | ||
} mbedtls_x509_crt_verify_chain; | ||
|
||
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | ||
|
@@ -371,7 +379,7 @@ int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix, | |
uint32_t flags ); | ||
|
||
/** | ||
* \brief Verify the certificate signature | ||
* \brief Verify a chain of certificates. | ||
* | ||
* The verify callback is a user-supplied callback that | ||
* can clear / modify / add flags for a certificate. If set, | ||
|
@@ -411,22 +419,25 @@ int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix, | |
* specific peers you know) - in that case, the self-signed | ||
* certificate doesn't need to have the CA bit set. | ||
* | ||
* \param crt a certificate (chain) to be verified | ||
* \param trust_ca the list of trusted CAs (see note above) | ||
* \param ca_crl the list of CRLs for trusted CAs (see note above) | ||
* \param cn expected Common Name (can be set to | ||
* NULL if the CN must not be verified) | ||
* \param flags result of the verification | ||
* \param f_vrfy verification function | ||
* \param p_vrfy verification parameter | ||
* | ||
* \return 0 (and flags set to 0) if the chain was verified and valid, | ||
* MBEDTLS_ERR_X509_CERT_VERIFY_FAILED if the chain was verified | ||
* but found to be invalid, in which case *flags will have one | ||
* or more MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX | ||
* flags set, or another error (and flags set to 0xffffffff) | ||
* in case of a fatal error encountered during the | ||
* verification process. | ||
* \param crt The certificate chain to be verified. | ||
* \param trust_ca The list of trusted CAs. | ||
* \param ca_crl The list of CRLs for trusted CAs. | ||
* \param cn The expected Common Name. This may be \c NULL if the | ||
* CN need not be verified. | ||
* \param flags The address at which to store the result of the verification. | ||
* \param f_vrfy The verification callback to use. See the documentation | ||
* of mbedtls_x509_crt_verify() for more information. | ||
* \param p_vrfy The context to be passed to \p f_vrfy. | ||
* | ||
* \return \c 0 if the chain is valid with respect to the | ||
* passed CN, CAs, CRLs and security profile. | ||
* \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the | ||
* certificate chain verification failed. In this case, | ||
* \c *flags will have one or more | ||
* \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX | ||
* flags set. | ||
* \return Another negative error code in case of a fatal error | ||
* encountered during the verification process. | ||
*/ | ||
int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt, | ||
mbedtls_x509_crt *trust_ca, | ||
|
@@ -436,7 +447,8 @@ int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt, | |
void *p_vrfy ); | ||
|
||
/** | ||
* \brief Verify the certificate signature according to profile | ||
* \brief Verify a chain of certificates with respect to | ||
* a configurable security profile. | ||
* | ||
* \note Same as \c mbedtls_x509_crt_verify(), but with explicit | ||
* security profile. | ||
|
@@ -445,22 +457,26 @@ int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt, | |
* for ECDSA) apply to all certificates: trusted root, | ||
* intermediate CAs if any, and end entity certificate. | ||
* | ||
* \param crt a certificate (chain) to be verified | ||
* \param trust_ca the list of trusted CAs | ||
* \param ca_crl the list of CRLs for trusted CAs | ||
* \param profile security profile for verification | ||
* \param cn expected Common Name (can be set to | ||
* NULL if the CN must not be verified) | ||
* \param flags result of the verification | ||
* \param f_vrfy verification function | ||
* \param p_vrfy verification parameter | ||
* | ||
* \return 0 if successful or MBEDTLS_ERR_X509_CERT_VERIFY_FAILED | ||
* in which case *flags will have one or more | ||
* MBEDTLS_X509_BADCERT_XXX or MBEDTLS_X509_BADCRL_XXX flags | ||
* set, | ||
* or another error in case of a fatal error encountered | ||
* during the verification process. | ||
* \param crt The certificate chain to be verified. | ||
* \param trust_ca The list of trusted CAs. | ||
* \param ca_crl The list of CRLs for trusted CAs. | ||
* \param profile The security profile to use for the verification. | ||
* \param cn The expected Common Name. This may be \c NULL if the | ||
* CN need not be verified. | ||
* \param flags The address at which to store the result of the verification. | ||
* \param f_vrfy The verification callback to use. See the documentation | ||
* of mbedtls_x509_crt_verify() for more information. | ||
* \param p_vrfy The context to be passed to \p f_vrfy. | ||
* | ||
* \return \c 0 if the chain is valid with respect to the | ||
* passed CN, CAs, CRLs and security profile. | ||
* \return #MBEDTLS_ERR_X509_CERT_VERIFY_FAILED in case the | ||
* certificate chain verification failed. In this case, | ||
* \c *flags will have one or more | ||
* \c MBEDTLS_X509_BADCERT_XXX or \c MBEDTLS_X509_BADCRL_XXX | ||
* flags set. | ||
* \return Another negative error code in case of a fatal error | ||
* encountered during the verification process. | ||
*/ | ||
int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt, | ||
mbedtls_x509_crt *trust_ca, | ||
|
@@ -477,16 +493,18 @@ int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt, | |
* but can return early and restart according to the limit | ||
* set with \c mbedtls_ecp_set_max_ops() to reduce blocking. | ||
* | ||
* \param crt a certificate (chain) to be verified | ||
* \param trust_ca the list of trusted CAs | ||
* \param ca_crl the list of CRLs for trusted CAs | ||
* \param profile security profile for verification | ||
* \param cn expected Common Name (can be set to | ||
* NULL if the CN must not be verified) | ||
* \param flags result of the verification | ||
* \param f_vrfy verification function | ||
* \param p_vrfy verification parameter | ||
* \param rs_ctx restart context (NULL to disable restart) | ||
* \param crt The certificate chain to be verified. | ||
* \param trust_ca The list of trusted CAs. | ||
* \param ca_crl The list of CRLs for trusted CAs. | ||
* \param profile The security profile to use for the verification. | ||
* \param cn The expected Common Name. This may be \c NULL if the | ||
* CN need not be verified. | ||
* \param flags The address at which to store the result of the verification. | ||
* \param f_vrfy The verification callback to use. See the documentation | ||
* of mbedtls_x509_crt_verify() for more information. | ||
* \param p_vrfy The context to be passed to \p f_vrfy. | ||
* \param rs_ctx The restart context to use. This may be set to \c NULL | ||
* to disable restartable ECC. | ||
* | ||
* \return See \c mbedtls_crt_verify_with_profile(), or | ||
* \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of | ||
|
@@ -501,6 +519,71 @@ int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt, | |
void *p_vrfy, | ||
mbedtls_x509_crt_restart_ctx *rs_ctx ); | ||
|
||
/** | ||
* \brief The type of trusted certificate callbacks. | ||
* | ||
* Callbacks of this type are passed to and used by the CRT | ||
* verification routine mbedtls_x509_crt_verify_with_ca_cb() | ||
* when looking for trusted signers of a given certificate. | ||
* | ||
* On success, the callback returns a list of trusted | ||
* certificates to be considered as potential signers | ||
* for the input certificate. | ||
* | ||
* \param p_ctx An opaque context passed to the callback. | ||
* \param child The certificate for which to search a potential signer. | ||
* This will point to a readable certificate. | ||
* \param candidate_cas The address at which to store the address of the first | ||
* entry in the generated linked list of candidate signers. | ||
* This will not be \c NULL. | ||
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 disagree with this change, as indicated in the response to @mpg's comment. |
||
* | ||
* \note The callback must only return a non-zero value on a | ||
* fatal error. If, in contrast, the search for a potential | ||
* signer completes without a single candidate, the | ||
* callback must return \c 0 and set \c *candidate_cas | ||
* to \c NULL. | ||
* | ||
* \return \c 0 on success. In this case, \c *candidate_cas points | ||
* to a heap-allocated linked list of instances of | ||
* ::mbedtls_x509_crt, and ownership of this list is passed | ||
* to the caller. | ||
* \return A negative error code on failure. | ||
*/ | ||
typedef int (*mbedtls_x509_crt_ca_cb_t)( void *p_ctx, | ||
mbedtls_x509_crt const *child, | ||
mbedtls_x509_crt **candidate_cas ); | ||
|
||
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK) | ||
/** | ||
* \brief Version of \c mbedtls_x509_crt_verify_with_profile() which | ||
* uses a callback to acquire the list of trusted CA | ||
* certificates. | ||
* | ||
* \param crt The certificate chain to be verified. | ||
* \param f_ca_cb The callback to be used to query for potential signers | ||
* of a given child certificate. See the documentation of | ||
* ::mbedtls_x509_crt_ca_cb_t for more information. | ||
* \param p_ca_cb The opaque context to be passed to \p f_ca_cb. | ||
* \param profile The security profile for the verification. | ||
* \param cn The expected Common Name. This may be \c NULL if the | ||
* CN need not be verified. | ||
* \param flags The address at which to store the result of the verification. | ||
Patater marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* \param f_vrfy The verification callback to use. See the documentation | ||
* of mbedtls_x509_crt_verify() for more information. | ||
* \param p_vrfy The context to be passed to \p f_vrfy. | ||
* | ||
* \return See \c mbedtls_crt_verify_with_profile(). | ||
*/ | ||
int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt, | ||
mbedtls_x509_crt_ca_cb_t f_ca_cb, | ||
void *p_ca_cb, | ||
const mbedtls_x509_crt_profile *profile, | ||
const char *cn, uint32_t *flags, | ||
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), | ||
void *p_vrfy ); | ||
|
||
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */ | ||
|
||
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE) | ||
/** | ||
* \brief Check usage of certificate against keyUsage extension. | ||
|
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.
Maybe trusted_ca_cb_result would be better.
Current name implies this would be true/false without knowing the type.
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.
I agree with Jarno's comment, but I don't think this needs to be changed right now, as this would either make newer code less consistent with existing names, or require changing existing names, which is what I would prefer, but probably out of scope for this PR.