aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/tls/peer_policies.cpp
blob: a87a44cabfba11eda6392a60f1487c075721fa61 (plain) (blame)
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
200
201
202
203
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "peer_policies.h"
#include <vespa/vespalib/regex/regex.h>
#include <iostream>

namespace vespalib::net::tls {

namespace {

bool
is_regex_special_char(char c) noexcept {
    switch (c) {
    case '^':
    case '$':
    case '|':
    case '{':
    case '}':
    case '(':
    case ')':
    case '[':
    case ']':
    case '\\':
    case '+':
    case '.':
    case '?':
    case '*':
        return true;
    default:
        return false;
    }
}

// Important: `delimiter` MUST NOT be a character that needs escaping within a regex [charset]
template <bool SupportSingleCharMatch>
std::string
char_delimited_glob_to_regex(vespalib::stringref glob, char delimiter) {
    std::string ret = "^";
    ret.reserve(glob.size() + 2);
    // Note: we explicitly stop matching at a delimiter boundary.
    // This is to make path fragment matching less vulnerable to dirty tricks.
    const std::string wildcard_pattern    = std::string("[^") + delimiter + "]*";
    // Same applies for single chars; they should only match _within_ a delimited boundary.
    const std::string single_char_pattern = std::string("[^") + delimiter + "]";
    for (auto c : glob) {
        if (c == '*') {
            ret += wildcard_pattern;
        } else if (c == '?' && SupportSingleCharMatch) {
            ret += single_char_pattern;
        } else {
            if (is_regex_special_char(c)) {
                ret += '\\';
            }
            ret += c;
        }
    }
    ret += '$';
    return ret;
}

class RegexHostMatchPattern : public CredentialMatchPattern {
    Regex _pattern_as_regex;
    explicit RegexHostMatchPattern(std::string_view glob_pattern)
        : _pattern_as_regex(Regex::from_pattern(glob_pattern))
    {
    }
public:
    RegexHostMatchPattern(RegexHostMatchPattern&&) noexcept = default;
    ~RegexHostMatchPattern() override = default;

    RegexHostMatchPattern& operator=(RegexHostMatchPattern&&) noexcept = default;

    [[nodiscard]] static RegexHostMatchPattern from_dns_glob_pattern(vespalib::stringref glob_pattern) {
        return RegexHostMatchPattern(char_delimited_glob_to_regex<true>(glob_pattern, '.'));
    }

    [[nodiscard]] static RegexHostMatchPattern from_uri_glob_pattern(vespalib::stringref glob_pattern) {
        return RegexHostMatchPattern(char_delimited_glob_to_regex<false>(glob_pattern, '/'));
    }

    [[nodiscard]] bool matches(vespalib::stringref str) const noexcept override {
        return _pattern_as_regex.full_match(std::string_view(str.data(), str.size()));
    }
};

class ExactMatchPattern : public CredentialMatchPattern {
    vespalib::string _must_match_exactly;
public:
    explicit ExactMatchPattern(vespalib::stringref str_to_match) noexcept // vespalib::string ctors marked noexcept
        : _must_match_exactly(str_to_match)
    {
    }
    ~ExactMatchPattern() override = default;

    [[nodiscard]] bool matches(vespalib::stringref str) const noexcept override {
        return (str == _must_match_exactly);
    }
};

} // anon ns

std::shared_ptr<const CredentialMatchPattern>
CredentialMatchPattern::create_from_dns_glob(vespalib::stringref glob_pattern) {
    return std::make_shared<const RegexHostMatchPattern>(RegexHostMatchPattern::from_dns_glob_pattern(glob_pattern));
}

std::shared_ptr<const CredentialMatchPattern>
    CredentialMatchPattern::create_from_uri_glob(vespalib::stringref glob_pattern) {
    return std::make_shared<const RegexHostMatchPattern>(RegexHostMatchPattern::from_uri_glob_pattern(glob_pattern));
}

std::shared_ptr<const CredentialMatchPattern>
CredentialMatchPattern::create_exact_match(vespalib::stringref str) {
    return std::make_shared<const ExactMatchPattern>(str);
}

RequiredPeerCredential::RequiredPeerCredential(Field field, vespalib::string must_match_pattern)
    : _field(field),
      _original_pattern(std::move(must_match_pattern)),
      _match_pattern(field == Field::SAN_URI ? CredentialMatchPattern::create_from_uri_glob(_original_pattern)
                                             : CredentialMatchPattern::create_from_dns_glob(_original_pattern))
{
}

RequiredPeerCredential::RequiredPeerCredential(const RequiredPeerCredential &) noexcept = default;
RequiredPeerCredential::RequiredPeerCredential(RequiredPeerCredential &&) noexcept = default;
RequiredPeerCredential & RequiredPeerCredential::operator=(RequiredPeerCredential &&) noexcept = default;
RequiredPeerCredential::~RequiredPeerCredential() = default;

PeerPolicy::PeerPolicy() = default;

PeerPolicy::PeerPolicy(std::vector<RequiredPeerCredential> required_peer_credentials)
    : _required_peer_credentials(std::move(required_peer_credentials)),
      _granted_capabilities(CapabilitySet::make_with_all_capabilities())
{
}

PeerPolicy::PeerPolicy(std::vector<RequiredPeerCredential> required_peer_credentials,
                       CapabilitySet granted_capabilities)
    : _required_peer_credentials(std::move(required_peer_credentials)),
      _granted_capabilities(granted_capabilities)
{}

PeerPolicy::~PeerPolicy() = default;

AuthorizedPeers::AuthorizedPeers(const AuthorizedPeers&) = default;
AuthorizedPeers::AuthorizedPeers(AuthorizedPeers&&) noexcept = default;
AuthorizedPeers& AuthorizedPeers::operator=(AuthorizedPeers&&) noexcept = default;
AuthorizedPeers::~AuthorizedPeers() = default;

namespace {
template <typename Collection>
void
print_joined(std::ostream& os, const Collection& coll, const char* sep) {
    bool first = true;
    for (const auto& e : coll) {
        if (!first) {
            os << sep;
        }
        first = false;
        os << e;
    }
}

constexpr const char*
to_string(RequiredPeerCredential::Field field) noexcept {
    switch (field) {
    case RequiredPeerCredential::Field::CN:      return "CN";
    case RequiredPeerCredential::Field::SAN_DNS: return "SAN_DNS";
    case RequiredPeerCredential::Field::SAN_URI: return "SAN_URI";
    }
    abort();
}

}

std::ostream&
operator<<(std::ostream& os, const RequiredPeerCredential& cred) {
    os << "RequiredPeerCredential("
       << to_string(cred.field())
       << " matches '"
       << cred.original_pattern()
       << "')";
    return os;
}

std::ostream&
operator<<(std::ostream& os, const PeerPolicy& policy) {
    os << "PeerPolicy(";
    print_joined(os, policy.required_peer_credentials(), ", ");
    os << ", " << policy.granted_capabilities().to_string() << ")";
    return os;
}

std::ostream&
operator<<(std::ostream& os, const AuthorizedPeers& authorized){
    os << "AuthorizedPeers(";
    print_joined(os, authorized.peer_policies(), ", ");
    os << ")";
    return os;
}

} // vespalib::net::tls