File size: 6,653 Bytes
2492322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#ifndef MAMBA_VALIDATION_UPDATE_FRAMEWORK_V0_6_HPP
#define MAMBA_VALIDATION_UPDATE_FRAMEWORK_V0_6_HPP

#include <memory>
#include <set>
#include <string>

#include <nlohmann/json_fwd.hpp>

#include "mamba/validation/keys.hpp"
#include "mamba/validation/update_framework.hpp"

namespace mamba::validation::v0_6
{
    /**
     * The Update Frameworkd ``conda-content-trust`` v0.6.0 specific implementation.
     *
     * This is a variation of TUF specification.
     */
    class SpecImpl final : public SpecBase
    {
    public:

        SpecImpl(std::string sv = "0.6.0");

        [[nodiscard]] auto json_key() const -> std::string override;
        [[nodiscard]] auto expiration_json_key() const -> std::string override;

        [[nodiscard]] auto signatures(const nlohmann::json& j) const
            -> std::set<RoleSignature> override;

        [[nodiscard]] auto canonicalize(const nlohmann::json& j) const -> std::string override;
        [[nodiscard]] auto upgradable() const -> bool override;
    };

    class V06RoleBaseExtension
    {
    public:

        void set_timestamp(const std::string& ts);

        [[nodiscard]] auto timestamp() const -> std::string;

    protected:

        std::string m_timestamp;

        void check_timestamp_format() const;
    };

    // Forward declaration of KeyMgrRole.
    class KeyMgrRole;

    /**
     * 'root' role implementation.
     */
    class RootImpl final
        : public RootRole
        , public V06RoleBaseExtension
    {
    public:

        RootImpl(const fs::u8path& p);
        RootImpl(const nlohmann::json& j);
        RootImpl(const std::string& json_str);

        /**
         * Return a ``RepoIndexChecker`` implementation (derived class) from repository base URL.
         */
        auto build_index_checker(
            const Context& context,
            const TimeRef& time_reference,
            const std::string& url,
            const fs::u8path& cache_path
        ) const -> std::unique_ptr<RepoIndexChecker> override;

        [[nodiscard]] auto self_keys() const -> RoleFullKeys override;

        [[nodiscard]] auto upgraded_signable() const -> nlohmann::json;
        auto
        upgraded_signature(const nlohmann::json& j, const std::string& pk, const std::byte* sk) const
            -> RoleSignature;

        [[nodiscard]] auto create_key_mgr(const fs::u8path& p) const -> KeyMgrRole;
        [[nodiscard]] auto create_key_mgr(const nlohmann::json& j) const -> KeyMgrRole;

        friend void to_json(nlohmann::json& j, const RootImpl& r);
        friend void from_json(const nlohmann::json& j, RootImpl& r);

    private:

        void load_from_json(const nlohmann::json& j);

        auto create_update(const nlohmann::json& j) -> std::unique_ptr<RootRole> override;

        [[nodiscard]] auto mandatory_defined_roles() const -> std::set<std::string> override;
        [[nodiscard]] auto optionally_defined_roles() const -> std::set<std::string> override;

        void set_defined_roles(std::map<std::string, RolePubKeys> keys);
    };


    class PkgMgrRole;

    /**
     * The Update Framework 'key_mgr' role implementation.
     */
    class KeyMgrRole final
        : public RoleBase
        , public V06RoleBaseExtension
    {
    public:

        KeyMgrRole(const fs::u8path& p, RoleFullKeys keys, const std::shared_ptr<SpecBase> spec);
        KeyMgrRole(const nlohmann::json& j, RoleFullKeys keys, const std::shared_ptr<SpecBase> spec);
        KeyMgrRole(const std::string& json_str, RoleFullKeys keys, const std::shared_ptr<SpecBase> spec);

        // std::set<std::string> roles() const override;
        [[nodiscard]] auto self_keys() const -> RoleFullKeys override;

        [[nodiscard]] auto create_pkg_mgr(const fs::u8path& p) const -> PkgMgrRole;
        [[nodiscard]] auto create_pkg_mgr(const nlohmann::json& j) const -> PkgMgrRole;

        /**
         * Return a ``RepoIndexChecker`` implementation (derived class) from repository base URL.
         */
        auto build_index_checker(
            const Context& context,
            const TimeRef& time_reference,
            const std::string& url,
            const fs::u8path& cache_path
        ) const -> std::unique_ptr<RepoIndexChecker>;

        friend void to_json(nlohmann::json& j, const KeyMgrRole& r);
        friend void from_json(const nlohmann::json& j, KeyMgrRole& r);

    private:

        void load_from_json(const nlohmann::json& j);

        RoleFullKeys m_keys;
        std::map<std::string, RolePubKeys> m_delegations;

        [[nodiscard]] auto mandatory_defined_roles() const -> std::set<std::string> override;
        [[nodiscard]] auto optionally_defined_roles() const -> std::set<std::string> override;

        void set_defined_roles(std::map<std::string, RolePubKeys> keys);
    };

    /**
     * The Update Framework 'pkg_mgr' role implementation.
     *
     * This role inherits from ``RepoIndexChecker`` and will be used by ``RepoChecker`` to
     * perform the repository index verification.
     */
    class PkgMgrRole final
        : public RoleBase
        , public V06RoleBaseExtension
        , public RepoIndexChecker
    {
    public:

        PkgMgrRole(RoleFullKeys keys, const std::shared_ptr<SpecBase> spec);
        PkgMgrRole(const fs::u8path& p, RoleFullKeys keys, const std::shared_ptr<SpecBase> spec);
        PkgMgrRole(const nlohmann::json& j, RoleFullKeys keys, const std::shared_ptr<SpecBase> spec);
        PkgMgrRole(const std::string& json_str, RoleFullKeys keys, const std::shared_ptr<SpecBase> spec);

        void verify_index(const fs::u8path& p) const override;
        void verify_index(const nlohmann::json& j) const override;
        void
        verify_package(const nlohmann::json& signed_data, const nlohmann::json& signatures) const override;

        friend void to_json(nlohmann::json& j, const PkgMgrRole& r);
        friend void from_json(const nlohmann::json& j, PkgMgrRole& r);

    private:

        void load_from_json(const nlohmann::json& j);

        [[nodiscard]] auto self_keys() const -> RoleFullKeys override;
        [[nodiscard]] auto pkg_signatures(const nlohmann::json& j) const -> std::set<RoleSignature>;
        void
        check_pkg_signatures(const nlohmann::json& signed_data, const nlohmann::json& signatures) const;

        void set_defined_roles(std::map<std::string, RolePubKeys> keys);

        RoleFullKeys m_keys;

        friend class KeyMgrRole;
    };
}
#endif