Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #if !defined(DART_IO_DISABLED) && !defined(DART_IO_SECURE_SOCKET_DISABLED) | |
| 6 | |
| 7 #include "platform/globals.h" | |
| 8 #if defined(HOST_OS_ANDROID) || defined(HOST_OS_LINUX) || \ | |
| 9 defined(HOST_OS_WINDOWS) || defined(HOST_OS_FUCHSIA) | |
| 10 | |
| 11 #include "bin/security_context.h" | |
| 12 | |
| 13 #include <openssl/bio.h> | |
| 14 #include <openssl/ssl.h> | |
| 15 #include <openssl/x509.h> | |
| 16 | |
| 17 #include "bin/directory.h" | |
| 18 #include "bin/file.h" | |
| 19 #include "bin/log.h" | |
| 20 #include "bin/secure_socket.h" | |
| 21 #include "bin/secure_socket_utils.h" | |
| 22 | |
| 23 namespace dart { | |
| 24 namespace bin { | |
| 25 | |
| 26 // The security context won't necessarily use the compiled-in root certificates, | |
| 27 // but since there is no way to update the size of the allocation after creating | |
| 28 // the weak persistent handle, we assume that it will. Note that when the | |
| 29 // root certs aren't compiled in, |root_certificates_pem_length| is 0. | |
| 30 const intptr_t SSLCertContext::kApproximateSize = | |
| 31 sizeof(SSLCertContext) + root_certificates_pem_length; | |
| 32 | |
| 33 const char* commandline_root_certs_file = NULL; | |
| 34 const char* commandline_root_certs_cache = NULL; | |
| 35 | |
| 36 static void AddCompiledInCerts(SSLCertContext* context) { | |
| 37 if (root_certificates_pem == NULL) { | |
| 38 if (SSL_LOG_STATUS) { | |
| 39 Log::Print("Missing compiled-in roots\n"); | |
| 40 } | |
| 41 return; | |
| 42 } | |
| 43 X509_STORE* store = SSL_CTX_get_cert_store(context->context()); | |
| 44 BIO* roots_bio = | |
| 45 BIO_new_mem_buf(const_cast<unsigned char*>(root_certificates_pem), | |
| 46 root_certificates_pem_length); | |
| 47 X509* root_cert; | |
| 48 // PEM_read_bio_X509 reads PEM-encoded certificates from a bio (in our case, | |
| 49 // backed by a memory buffer), and returns X509 objects, one by one. | |
| 50 // When the end of the bio is reached, it returns null. | |
| 51 while ((root_cert = PEM_read_bio_X509(roots_bio, NULL, NULL, NULL)) != NULL) { | |
| 52 int status = X509_STORE_add_cert(store, root_cert); | |
| 53 // X509_STORE_add_cert increments the reference count of cert on success. | |
| 54 X509_free(root_cert); | |
| 55 if (status == 0) { | |
| 56 break; | |
| 57 } | |
| 58 } | |
| 59 BIO_free(roots_bio); | |
| 60 // If there is an error here, it must be the error indicating that we are done | |
| 61 // reading PEM certificates. | |
| 62 ASSERT((ERR_peek_error() == 0) || SecureSocketUtils::NoPEMStartLine()); | |
| 63 ERR_clear_error(); | |
| 64 } | |
| 65 | |
| 66 | |
| 67 void SSLCertContext::TrustBuiltinRoots() { | |
| 68 // First, try to use locations specified on the command line. | |
| 69 if (commandline_root_certs_file != NULL) { | |
| 70 LoadRootCertFile(commandline_root_certs_file); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 if (commandline_root_certs_cache != NULL) { | |
| 75 LoadRootCertCache(commandline_root_certs_cache); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 #if defined(HOST_OS_ANDROID) | |
|
zra
2017/06/02 22:56:24
This is looking really good.
At this point I thin
bkonyi
2017/06/05 20:25:52
Done.
| |
| 80 // On Android, we don't compile in the trusted root certificates. Insead, | |
| 81 // we use the directory of trusted certificates already present on the device. | |
| 82 // This saves ~240KB from the size of the binary. This has the drawback that | |
| 83 // SSL_do_handshake will synchronously hit the filesystem looking for root | |
| 84 // certs during its trust evaluation. We call SSL_do_handshake directly from | |
| 85 // the Dart thread so that Dart code can be invoked from the "bad certificate" | |
| 86 // callback called by SSL_do_handshake. | |
| 87 const char* android_cacerts = "/system/etc/security/cacerts"; | |
| 88 LoadRootCertCache(android_cacerts); | |
| 89 return; | |
| 90 #elif defined(HOST_OS_LINUX) | |
| 91 // On Linux, we use the compiled-in trusted certs as a last resort. First, | |
| 92 // we try to find the trusted certs in various standard locations. A good | |
| 93 // discussion of the complexities of this endeavor can be found here: | |
| 94 // | |
| 95 // https://www.happyassassin.net/2015/01/12/a-note-about-ssltls-trusted-certif icate-stores-and-platforms/ | |
| 96 const char* bundle = "/etc/pki/tls/certs/ca-bundle.crt"; | |
| 97 const char* cachedir = "/etc/ssl/certs"; | |
| 98 if (File::Exists(bundle)) { | |
| 99 LoadRootCertFile(bundle); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 if (Directory::Exists(cachedir) == Directory::EXISTS) { | |
| 104 LoadRootCertCache(cachedir); | |
| 105 return; | |
| 106 } | |
| 107 #endif // defined(HOST_OS_ANDROID) | |
| 108 | |
| 109 // Fall back on the compiled-in certs if the standard locations don't exist, | |
| 110 // or we aren't on Linux. | |
| 111 if (SSL_LOG_STATUS) { | |
| 112 Log::Print("Trusting compiled-in roots\n"); | |
| 113 } | |
| 114 AddCompiledInCerts(this); | |
| 115 } | |
| 116 | |
| 117 | |
| 118 void SSLFilter::RegisterCallbacks(SSLCertContext* cert_ctx) { | |
| 119 // No callbacks to register for implementations using BoringSSL's built-in | |
| 120 // verification mechanism. | |
| 121 } | |
| 122 | |
| 123 } // namespace bin | |
| 124 } // namespace dart | |
| 125 | |
| 126 #endif // defined(HOST_OS_LINUX) | |
| 127 | |
| 128 #endif // !defined(DART_IO_DISABLED) && | |
| 129 // !defined(DART_IO_SECURE_SOCKET_DISABLED) | |
| OLD | NEW |