| 67 | } |
| 68 | |
| 69 | bool ValidateTrustedCertFile() { |
| 70 | const auto& fpath = FLAGS_trusted_certificate_file; |
| 71 | if (fpath.empty()) { |
| 72 | // No validation is needed. |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // Make sure the file in question does exist, is readable, and non-empty. |
| 77 | // There might be extra verification to load the certificate(s) from the file, |
| 78 | // but since cURL could have some particular requirements on the contents |
| 79 | // of the file, let's skip that extra validation step and defer to the time |
| 80 | // when cURL loads the file on its own. Also, the validators might run |
| 81 | // at the time when the OpenSSL-based crypto runtime context isn't yet |
| 82 | // initialized, so it's safer to defer to TlsContext::Init() where the |
| 83 | // initialization is done in a proper way. |
| 84 | std::unique_ptr<RandomAccessFile> raf; |
| 85 | if (auto s = Env::Default()->NewRandomAccessFile(fpath, &raf); !s.ok()) { |
| 86 | LOG(ERROR) << Substitute("could not open file for reading: $0", s.ToString()); |
| 87 | return false; |
| 88 | } |
| 89 | // Read just a single byte to make sure that the file is readable. |
| 90 | uint8_t scratch[1]; |
| 91 | Slice data(scratch, sizeof(scratch)); |
| 92 | if (auto s = raf->Read(0, data); !s.ok()) { |
| 93 | LOG(ERROR) << Substitute("could not read from file '$0': $1", |
| 94 | fpath, s.ToString()); |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // The validator uses Env API, so it's necessary to use GROUP_FLAG_VALIDATOR() |
| 101 | // instead of regular gflag's DEFINE_validator() macro to allow for custom |
nothing calls this directly
no test coverage detected