GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/ssl.cc
Date: 2025-06-29 02:35:41
Exec Total Coverage
Lines: 39 52 75.0%
Branches: 51 127 40.2%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "ssl.h"
6
7 #include <dirent.h>
8
9 #include <cstdlib>
10 #include <string>
11 #include <vector>
12
13 #include "duplex_curl.h"
14 #include "util/platform.h"
15 #include "util/posix.h"
16 #include "util/string.h"
17
18 namespace {
19
20 666 bool HasCertificates(const std::string &directory) {
21 666 DIR *dirp = opendir(directory.c_str());
22
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 666 times.
666 if (!dirp)
23 return false;
24
25 platform_dirent64 *dirent;
26
1/2
✓ Branch 1 taken 1998 times.
✗ Branch 2 not taken.
1998 while ((dirent = platform_readdir(dirp))) {
27
3/6
✓ Branch 2 taken 1998 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1998 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1998 times.
✗ Branch 9 not taken.
3996 const std::string filename(directory + "/" + std::string(dirent->d_name));
28
29 platform_stat64 stat;
30
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1998 times.
1998 if (platform_stat(filename.c_str(), &stat) != 0)
31 continue;
32
3/4
✓ Branch 0 taken 1332 times.
✓ Branch 1 taken 666 times.
✓ Branch 2 taken 1332 times.
✗ Branch 3 not taken.
1998 if (!(S_ISREG(stat.st_mode) || S_ISLNK(stat.st_mode)))
33 1332 continue;
34
35
3/12
✓ Branch 1 taken 666 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 666 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 666 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
1332 if (HasSuffix(filename, ".pem", /* ignore case = */ false)
36
8/22
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 666 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 666 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 666 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 666 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 666 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 666 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 666 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
1332 || HasSuffix(filename, ".crt", /* ignore case = */ false)) {
37
1/2
✓ Branch 1 taken 666 times.
✗ Branch 2 not taken.
666 closedir(dirp);
38 666 return true;
39 }
40
2/3
✗ Branch 1 not taken.
✓ Branch 2 taken 1332 times.
✓ Branch 3 taken 666 times.
1998 }
41
42 closedir(dirp);
43 return false;
44 }
45
46 } // namespace
47
48
49 3361 SslCertificateStore::SslCertificateStore() {
50 3361 const char *ca_path_env = getenv("X509_CERT_DIR");
51
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3361 if (ca_path_env && *ca_path_env)
52 ca_path_ = ca_path_env;
53 else
54
1/2
✓ Branch 1 taken 3361 times.
✗ Branch 2 not taken.
3361 ca_path_ = "/etc/grid-security/certificates";
55 3361 const char *ca_bundle_env = getenv("X509_CERT_BUNDLE");
56
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3361 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3361 if (ca_bundle_env && *ca_bundle_env)
57 ca_bundle_ = ca_bundle_env;
58 3361 }
59
60
61 bool SslCertificateStore::ApplySslCertificatePath(CURL *handle) const {
62 const CURLcode res1 = curl_easy_setopt(
63 handle, CURLOPT_CAPATH, ca_path_.c_str());
64 CURLcode res2 = CURLE_OK;
65 if (!ca_bundle_.empty())
66 res2 = curl_easy_setopt(handle, CURLOPT_CAINFO, ca_bundle_.c_str());
67
68 return (res1 == CURLE_OK) && (res2 == CURLE_OK);
69 }
70
71
72 666 void SslCertificateStore::UseSystemCertificatePath() {
73 666 std::vector<std::string> candidates;
74
75
2/4
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 666 times.
✗ Branch 6 not taken.
666 candidates.push_back("/etc/ssl/certs");
76
2/4
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 666 times.
✗ Branch 6 not taken.
666 candidates.push_back("/etc/pki/tls/certs");
77
2/4
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 666 times.
✗ Branch 6 not taken.
666 candidates.push_back("/etc/ssl");
78
2/4
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 666 times.
✗ Branch 6 not taken.
666 candidates.push_back("/etc/pki/tls");
79
2/4
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 666 times.
✗ Branch 6 not taken.
666 candidates.push_back("/etc/pki/ca-trust/extracted/pem");
80
2/4
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 666 times.
✗ Branch 6 not taken.
666 candidates.push_back("/etc/ssl");
81
82
1/2
✓ Branch 1 taken 666 times.
✗ Branch 2 not taken.
666 for (unsigned i = 0; i < candidates.size(); ++i) {
83
2/4
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 666 times.
✗ Branch 5 not taken.
666 if (HasCertificates(candidates[i])) {
84
1/2
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
666 const std::string bundle_candidate = candidates[i] + "/ca-bundle.crt";
85 666 if (ca_bundle_.empty()
86
4/8
✓ Branch 0 taken 666 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 666 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 666 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 666 times.
✗ Branch 8 not taken.
1332 && (FileExists(bundle_candidate)
87
2/4
✓ Branch 1 taken 666 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 666 times.
✗ Branch 4 not taken.
666 || SymlinkExists(bundle_candidate))) {
88
1/2
✓ Branch 1 taken 666 times.
✗ Branch 2 not taken.
666 ca_bundle_ = bundle_candidate;
89 }
90
1/2
✓ Branch 2 taken 666 times.
✗ Branch 3 not taken.
666 ca_path_ = candidates[i];
91 666 return;
92 666 }
93 }
94
95 // fallback
96 ca_path_ = candidates[0];
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 666 times.
666 }
98