-
Notifications
You must be signed in to change notification settings - Fork 140
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 SSL cert validation callback #825
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2015-2021 The NATS Authors | ||
// 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. | ||
|
||
#include "natsp.h" | ||
|
||
#include "mem.h" | ||
#include "cert.h" | ||
|
||
#if defined(NATS_HAS_TLS) | ||
natsStatus | ||
natsCert_create(natsCert **newCert, X509 *x509Cert) | ||
{ | ||
natsCert *cert = NULL; | ||
const ASN1_TIME* notBefore; | ||
const ASN1_TIME* notAfter; | ||
|
||
if (x509Cert == NULL) | ||
return nats_setDefaultError(NATS_INVALID_ARG); | ||
|
||
cert = NATS_CALLOC(1, sizeof(natsCert)); | ||
if (cert == NULL) | ||
return nats_setDefaultError(NATS_NO_MEMORY); | ||
|
||
cert->subjectName = X509_NAME_oneline(X509_get_subject_name(x509Cert), NULL, 0); | ||
cert->issuerName = X509_NAME_oneline(X509_get_issuer_name(x509Cert), NULL, 0); | ||
|
||
notBefore = X509_get0_notBefore(x509Cert); | ||
ASN1_TIME_to_tm(notBefore, &cert->tmNotBefore); | ||
notAfter = X509_get0_notAfter(x509Cert); | ||
ASN1_TIME_to_tm(notAfter, &cert->tmNotAfter); | ||
|
||
*newCert = cert; | ||
|
||
return NATS_OK; | ||
} | ||
|
||
void | ||
natsCert_free(natsCert* cert) | ||
{ | ||
if (cert == NULL) | ||
return; | ||
|
||
OPENSSL_free((char *)cert->subjectName); | ||
OPENSSL_free((char*)cert->issuerName); | ||
|
||
NATS_FREE(cert); | ||
} | ||
|
||
static natsStatus | ||
_createChainElement(natsCertChain **newElement, X509 *x509Cert) | ||
{ | ||
natsCertChain *element = NULL; | ||
natsStatus s = NATS_OK; | ||
natsCert *cert = NULL; | ||
|
||
s = natsCert_create(&cert, x509Cert); | ||
if (s != NATS_OK) | ||
return s; | ||
|
||
element = NATS_CALLOC(1, sizeof(natsCertChain)); | ||
if (element == NULL) | ||
return nats_setDefaultError(NATS_NO_MEMORY); | ||
|
||
element->cert = cert; | ||
element->next = NULL; | ||
|
||
*newElement = element; | ||
|
||
return NATS_OK; | ||
} | ||
|
||
static natsStatus | ||
_appendChainElement(natsCertChain **chain, X509 *x509Cert) | ||
{ | ||
natsStatus s = NATS_OK; | ||
natsCertChain *newElement = NULL; | ||
natsCertChain *temp = NULL; | ||
|
||
s = _createChainElement(&newElement, x509Cert); | ||
if (s != NATS_OK) | ||
return s; | ||
|
||
if (*chain == NULL) | ||
{ | ||
*chain = newElement; | ||
return NATS_OK; | ||
} | ||
|
||
temp = *chain; | ||
while (temp->next != NULL) | ||
{ | ||
temp = temp->next; | ||
} | ||
temp->next = newElement; | ||
return NATS_OK; | ||
} | ||
|
||
|
||
natsStatus | ||
natsCertChain_create(natsCertChain **newChain, STACK_OF(X509) *x509Chain) | ||
{ | ||
natsCertChain *chain = NULL; | ||
int numElements; | ||
X509 *x509Cert; | ||
natsStatus s = NATS_OK; | ||
|
||
if (x509Chain == NULL) | ||
return nats_setDefaultError(NATS_INVALID_ARG); | ||
|
||
numElements = sk_X509_num(x509Chain); | ||
for (int i = 0; i < numElements; i++) | ||
{ | ||
x509Cert = sk_X509_value(x509Chain, i); | ||
s = _appendChainElement(&chain, x509Cert); | ||
if (s != NATS_OK) | ||
{ | ||
natsCertChain_free(chain); | ||
return s; | ||
} | ||
} | ||
|
||
*newChain = chain; | ||
return NATS_OK; | ||
} | ||
|
||
void | ||
natsCertChain_free(natsCertChain *chain) | ||
{ | ||
natsCertChain *temp; | ||
|
||
if (chain == NULL) | ||
return; | ||
|
||
while (chain != NULL) | ||
{ | ||
temp = chain; | ||
chain = chain->next; | ||
natsCert_free(temp->cert); | ||
NATS_FREE(temp); | ||
} | ||
} | ||
#endif // NATS_HAS_TLS |
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,47 @@ | ||
// Copyright 2015-2024 The NATS Authors | ||
// 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 CERT_H_ | ||
#define CERT_H_ | ||
|
||
#include "status.h" | ||
|
||
struct __natsCert | ||
{ | ||
const char *subjectName; | ||
const char *issuerName; | ||
struct tm tmNotBefore; | ||
struct tm tmNotAfter; | ||
}; | ||
|
||
struct __natsCertChainElement | ||
{ | ||
natsCert* cert; | ||
struct __natsCertChainElement *next; | ||
}; | ||
|
||
#if defined(NATS_HAS_TLS) | ||
natsStatus | ||
natsCert_create(natsCert **newCert, X509 *x509Cert); | ||
|
||
void | ||
natsCert_free(natsCert *cert); | ||
|
||
natsStatus | ||
natsCertChain_create(natsCertChain **newChain, STACK_OF(X509) *x509Chain); | ||
|
||
void | ||
natsCertChain_free(natsCertChain *chain); | ||
#endif // NATS_HAS_TLS | ||
|
||
#endif // CERT_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.
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.
@ckasabula What is the extra value-add of the
natsCert
andnatsCertChain
abstractions, they appear to contain the data that is already present in the X509 structs? (relative to implementing the verification callback with the X509 structs instead?)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.
@levb
I could change the callback from what I have currently:
typedef bool (*CertificateValidationCB)(int preverify_ok, natsCert *cert, natsCertChain *chain);
to
typedef bool (*CertificateValidationCB)(int preverify_ok, X509 *cert, STACK_OF(X509) *chain);
Problem with that is that those openssl types would need to get pulled into nats.h (wrapped with
#if defined(NATS_HAS_TLS)
). I added those wrappers, so clients wouldn't have to write any openssl code.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 think client code would also have to link with openssl too. Adding the wrappers for cert and chain, keeps clients isolated from underlying SSL impl.
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.
@ckasabula Conversely, there is already a
SSL_CTX_set_verify[_callback]
in openSSL; 1/5 we should target enabling the "idiomatic" functionality in this case, rather than adding our own layer. I briefly chatted with @mtmk on this, and he volunteered to look into other implementations and see what path we want to take here.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.
nats.c already implements the callback via SSL_set_verify. I'm calling the client callback from the openssl callback in conn.c.
https://github.com/nats-io/nats.c/blob/main/src/conn.c#L739
https://github.com/ckasabula/nats.c/blob/ck/ssl_verification_callback/src/conn.c#L642
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.
@levb @mtmk
In nats.net cert callback is called with the dotnet "idiomatic" X509Certificate and X509Chain, both from System.Security.Cryptography.X509Certificates. In dotnet these types are included with the framework. In C with headers/libs it's more challenging.
I'm open to having the callback use openssl types. Would you both/either of you be open to a short call to discuss?
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 could have the client provide their own callback, which would replace
_collectSSLErr
when specified. That would be more in line with how the dotnet client works. It would be passed directly toSSL_set_verify
.