• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1022567176

02 Oct 2023 07:50AM UTC coverage: 80.127% (+2.5%) from 77.645%
1022567176

push

gitlab-ci

kacf
chore: Centralize selection of `std::filesystem` library.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

6447 of 8046 relevant lines covered (80.13%)

9912.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.9
/artifact/sha/platform/openssl/sha.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <cstdint>
16
#include <string>
17
#include <ctime>
18
#include <iomanip>
19
#include <vector>
20
#include <sstream>
21
#include <algorithm>
22

23
#include <openssl/evp.h>
24
#include <artifact/sha/sha.hpp>
25

26
#include <common/common.hpp>
27
#include <common/io.hpp>
28

29

30
namespace mender {
31
namespace sha {
32

33
static const size_t SHA_256_digest_length = 32;
34

35
namespace log = mender::common::log;
36
namespace io = mender::common::io;
37

38
const ErrorCategoryClass ErrorCategory = ErrorCategoryClass();
39

40
const char *ErrorCategoryClass::name() const noexcept {
×
41
        return "ShaSumErrorCategory";
×
42
}
43

44
string ErrorCategoryClass::message(int code) const {
×
45
        switch (code) {
×
46
        case NoError:
47
                return "Success";
×
48
        case InitializationError:
49
                return "Initialization error";
×
50
        case ShasumCreationError:
51
                return "Shasum creation error";
×
52
        case ShasumMismatchError:
53
                return "Shasum mismatch error";
×
54
        default:
55
                return "Unknown";
×
56
        }
57
}
58

59
error::Error MakeError(ErrorCode code, const string &msg) {
1✔
60
        return error::Error(error_condition(code, ErrorCategory), msg);
4✔
61
}
62

63

64
Reader::Reader(io::Reader &reader) :
160✔
65
        Reader::Reader {reader, ""} {
320✔
66
}
160✔
67

68
Reader::Reader(io::Reader &reader, const std::string &expected_sha) :
354✔
69
        sha_handle_(EVP_MD_CTX_new(), [](EVP_MD_CTX *ctx) { EVP_MD_CTX_free(ctx); }),
70
        wrapped_reader_ {reader},
71
        expected_sha_ {expected_sha} {
708✔
72
        if (EVP_DigestInit_ex(sha_handle_.get(), EVP_sha256(), nullptr) != 1) {
354✔
73
                log::Error("Failed to initialize the shasummer");
×
74
                initialized_ = false;
×
75
                return;
×
76
        }
77
        initialized_ = true;
354✔
78
}
×
79

80
expected::ExpectedSize Reader::Read(
1,201✔
81
        vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) {
82
        if (!initialized_) {
1,201✔
83
                return expected::unexpected(MakeError(
×
84
                        InitializationError,
85
                        "The ShaReader was not properly initialized. Shasumming is not possible"));
×
86
        }
87

88
        auto bytes_read = wrapped_reader_.Read(start, end);
1,201✔
89
        if (!bytes_read) {
1,201✔
90
                return bytes_read;
91
        }
92

93
        // bytes_read == 0 == EOF marker in our Reader/Writer interface implementation
94
        if (bytes_read.value() == 0) {
1,201✔
95
                auto real_sha = this->ShaSum();
340✔
96
                if (!real_sha) {
340✔
97
                        return expected::unexpected(real_sha.error());
×
98
                }
99
                if (expected_sha_.size() > 0 and real_sha.value() != expected_sha_) {
340✔
100
                        return expected::unexpected(MakeError(
2✔
101
                                ShasumMismatchError,
102
                                "The checksum of the read byte-stream does not match the expected checksum, (expected): "
103
                                        + expected_sha_ + " (calculated): " + real_sha.value().String()));
6✔
104
                }
105
                this->done_ = true;
338✔
106
                this->shasum_ = real_sha.value();
338✔
107
                return 0;
108
        }
109

110
        if (EVP_DigestUpdate(sha_handle_.get(), &start[0], bytes_read.value()) != 1) {
861✔
111
                return expected::unexpected(MakeError(ShasumCreationError, "Failed to create the shasum"));
×
112
        }
113

114
        return bytes_read.value();
861✔
115
}
116

117

118
ExpectedSHA Reader::ShaSum() {
502✔
119
        if (!initialized_) {
502✔
120
                return expected::unexpected(MakeError(
×
121
                        InitializationError,
122
                        "The ShaReader was not properly initialized. Shasumming is not possible"));
×
123
        }
124
        if (done_) {
502✔
125
                return this->shasum_;
161✔
126
        }
127

128
        vector<uint8_t> hash(EVP_MAX_MD_SIZE);
341✔
129
        unsigned int hash_length = 0;
341✔
130

131
        if (EVP_DigestFinal_ex(sha_handle_.get(), hash.data(), &hash_length) != 1) {
341✔
132
                return expected::unexpected(
×
133
                        MakeError(ShasumCreationError, "Failed to create the shasum. OpenSSL error: "));
×
134
        }
135

136
        if (hash_length != SHA_256_digest_length) {
341✔
137
                return expected::unexpected(MakeError(
×
138
                        ShasumCreationError,
139
                        "SHA of unexpected length: " + std::to_string(hash_length) + " expected length: 32"));
×
140
        }
141

142
        return SHA(hash, SHA_256_digest_length);
682✔
143
}
144

145
ExpectedSHA Shasum(const vector<uint8_t> &data) {
19✔
146
        string in {data.begin(), data.end()};
19✔
147

148
        io::StringReader is {in};
38✔
149

150
        Reader r {is};
38✔
151

152
        auto discard_writer = io::Discard {};
153

154
        auto err = io::Copy(discard_writer, r);
19✔
155
        if (err != error::NoError) {
19✔
156
                return expected::unexpected(err);
×
157
        }
158

159
        return r.ShaSum();
19✔
160
}
161

162
} // namespace sha
163
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc