[TLS] Add support for platform-specific CA bundles.
Adds a new OS::get_system_ca_certs method which can be implemented by
platforms to retrieve the list of trusted CA certificates using OS
specific APIs.
The function should return the certificates in PEM format, and is
currently implemented for Windows/macOS/LinuxBSD(*)/Android.
mbedTLS will fall back to bundled certificates when the OS returns no
certificates.
(*) LinuxBSD does not have a standardized certificates store location.
The current implementation will test for common locations and may
return an empty string on some distributions (falling back to the
bundled certificates).
This commit is contained in:
@ -30,6 +30,7 @@
|
||||
|
||||
#include "os_macos.h"
|
||||
|
||||
#include "core/crypto/crypto_core.h"
|
||||
#include "core/version_generated.gen.h"
|
||||
#include "main/main.h"
|
||||
|
||||
@ -671,6 +672,34 @@ Error OS_MacOS::move_to_trash(const String &p_path) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
String OS_MacOS::get_system_ca_certificates() {
|
||||
CFArrayRef result;
|
||||
SecCertificateRef item;
|
||||
CFDataRef der;
|
||||
|
||||
OSStatus ret = SecTrustCopyAnchorCertificates(&result);
|
||||
ERR_FAIL_COND_V(ret != noErr, "");
|
||||
|
||||
CFIndex l = CFArrayGetCount(result);
|
||||
String certs;
|
||||
PackedByteArray pba;
|
||||
for (CFIndex i = 0; i < l; i++) {
|
||||
item = (SecCertificateRef)CFArrayGetValueAtIndex(result, i);
|
||||
der = SecCertificateCopyData(item);
|
||||
int derlen = CFDataGetLength(der);
|
||||
if (pba.size() < derlen * 3) {
|
||||
pba.resize(derlen * 3);
|
||||
}
|
||||
size_t b64len = 0;
|
||||
Error err = CryptoCore::b64_encode(pba.ptrw(), pba.size(), &b64len, (unsigned char *)CFDataGetBytePtr(der), derlen);
|
||||
CFRelease(der);
|
||||
ERR_CONTINUE(err != OK);
|
||||
certs += "-----BEGIN CERTIFICATE-----\n" + String((char *)pba.ptr(), b64len) + "\n-----END CERTIFICATE-----\n";
|
||||
}
|
||||
CFRelease(result);
|
||||
return certs;
|
||||
}
|
||||
|
||||
void OS_MacOS::run() {
|
||||
if (!main_loop) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user