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

mendersoftware / mender / 1022753986

02 Oct 2023 10:37AM UTC coverage: 78.168% (-2.0%) from 80.127%
1022753986

push

gitlab-ci

oleorhagen
feat: Run the authentication loop once upon bootstrap

Ticket: MEN-6658
Changelog: None

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>

32 of 32 new or added lines in 1 file covered. (100.0%)

6996 of 8950 relevant lines covered (78.17%)

10353.4 hits per line

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

49.3
/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) {
3✔
60
        return error::Error(error_condition(code, ErrorCategory), msg);
6✔
61
}
62

63

64
Reader::Reader(io::Reader &reader) :
×
65
        Reader::Reader {reader, ""} {
×
66
}
×
67

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

80
expected::ExpectedSize Reader::Read(
1,196✔
81
        vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) {
82
        if (!initialized_) {
1,196✔
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);
2,392✔
89
        if (!bytes_read) {
1,196✔
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,196✔
95
                auto real_sha = this->ShaSum();
682✔
96
                if (!real_sha) {
341✔
97
                        return expected::unexpected(real_sha.error());
×
98
                }
99
                if (expected_sha_.size() > 0 and real_sha.value() != expected_sha_) {
341✔
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()));
4✔
104
                }
105
                this->done_ = true;
339✔
106
                this->shasum_ = real_sha.value();
339✔
107
                return 0;
339✔
108
        }
109

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

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

117

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

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

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

136
        if (hash_length != SHA_256_digest_length) {
342✔
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);
684✔
143
}
144

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

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

150
        Reader r {is};
40✔
151

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

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

159
        return r.ShaSum();
20✔
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