Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 5 | #include "kerberos/config_parser.h" |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 6 | |
| 7 | #include <ostream> |
| 8 | #include <string> |
| 9 | |
| 10 | #include <base/logging.h> |
Qijiang Fan | 886c469 | 2021-02-19 11:54:10 +0900 | [diff] [blame] | 11 | #include <base/notreached.h> |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 12 | #include <gtest/gtest.h> |
| 13 | |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 14 | #include "kerberos/kerberos_metrics.h" |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 15 | #include "kerberos/proto_bindings/kerberos_service.pb.h" |
| 16 | |
| 17 | namespace kerberos { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 18 | namespace { |
| 19 | constexpr char kCompleteKrb5Conf[] = R"( |
| 20 | # Comment |
| 21 | ; Another comment |
| 22 | |
| 23 | [libdefaults] |
| 24 | clockskew = 123 |
| 25 | default_tkt_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 26 | default_tgs_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 27 | permitted_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 28 | renew_lifetime* = 7d |
| 29 | ticket_lifetime* = 1d |
| 30 | A.EXAMPLE.COM = { |
| 31 | clockskew = 300 |
| 32 | } |
| 33 | B.EXAMPLE.COM = |
| 34 | { |
| 35 | ; Tests = whether { can be on new line |
| 36 | clockskew = 500 |
| 37 | } |
| 38 | |
| 39 | [realms] |
| 40 | kdc = 5.6.7.8 |
| 41 | EXAMPLE.COM = { |
| 42 | kdc = 1.2.3.4 |
| 43 | admin_server = kdc.example.com |
| 44 | auth_to_local = RULE:[2:$1](johndoe)s/^.*$/guest/ |
| 45 | auth_to_local_names = { |
| 46 | hans = jack |
| 47 | joerg = jerk |
| 48 | } |
| 49 | }* |
| 50 | |
| 51 | [domain_realm]* |
| 52 | any.thing = IS.ACCEPTED.HERE |
| 53 | |
| 54 | [capaths] |
| 55 | here = AS.WELL)"; |
Sergey Poromov | 38cec24 | 2021-03-12 13:47:57 +0100 | [diff] [blame] | 56 | |
| 57 | std::string GenerateNestedGroups(int count) { |
| 58 | std::string result; |
| 59 | for (int i = 0; i < count; i++) { |
| 60 | result += "A={\n"; |
| 61 | } |
| 62 | for (int i = 0; i < count; i++) { |
| 63 | result += "}\n"; |
| 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 68 | } // namespace |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 69 | |
| 70 | std::ostream& operator<<(std::ostream& os, ConfigErrorCode code) { |
| 71 | switch (code) { |
| 72 | case CONFIG_ERROR_NONE: |
| 73 | return os << "OK"; |
| 74 | case CONFIG_ERROR_SECTION_NESTED_IN_GROUP: |
| 75 | return os << "Section nested in group"; |
| 76 | case CONFIG_ERROR_SECTION_SYNTAX: |
| 77 | return os << "ESection syntax error, expected '[section]'"; |
| 78 | case CONFIG_ERROR_EXPECTED_OPENING_CURLY_BRACE: |
| 79 | return os << "Expected opening curly brace '{'"; |
| 80 | case CONFIG_ERROR_EXTRA_CURLY_BRACE: |
| 81 | return os << "Extra curly brace"; |
| 82 | case CONFIG_ERROR_RELATION_SYNTAX: |
| 83 | return os << "Relation syntax error, expected 'key = ...'"; |
| 84 | case CONFIG_ERROR_KEY_NOT_SUPPORTED: |
| 85 | return os << "Key not supported"; |
| 86 | case CONFIG_ERROR_SECTION_NOT_SUPPORTED: |
| 87 | return os << "Section not supported"; |
| 88 | case CONFIG_ERROR_KRB5_FAILED_TO_PARSE: |
| 89 | return os << "KRB5 failed to parse"; |
Sergey Poromov | 38cec24 | 2021-03-12 13:47:57 +0100 | [diff] [blame] | 90 | case CONFIG_ERROR_TOO_MANY_NESTED_GROUPS: |
| 91 | return os << "Too many nested groups"; |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 92 | case CONFIG_ERROR_COUNT: |
| 93 | NOTREACHED(); |
| 94 | } |
| 95 | return os; |
| 96 | } |
| 97 | |
| 98 | std::ostream& operator<<(std::ostream& os, const ConfigErrorInfo& error_info) { |
| 99 | if (error_info.code() == CONFIG_ERROR_NONE) |
| 100 | return os << "[no error]"; |
| 101 | |
| 102 | return os << error_info.code() << " at line " << error_info.line_index(); |
| 103 | } |
| 104 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 105 | class ConfigParserTest : public ::testing::Test { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 106 | protected: |
Sergey Poromov | 38cec24 | 2021-03-12 13:47:57 +0100 | [diff] [blame] | 107 | void ExpectNoError(const std::string& krb5conf) { |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 108 | ConfigErrorInfo error_info = config_parser_.Validate(krb5conf); |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 109 | |
| 110 | EXPECT_TRUE(error_info.has_code()) << error_info; |
| 111 | EXPECT_EQ(CONFIG_ERROR_NONE, error_info.code()) << error_info; |
| 112 | EXPECT_FALSE(error_info.has_line_index()) << error_info; |
| 113 | } |
| 114 | |
Lutz Justen | 70496c1 | 2019-07-24 11:11:55 +0200 | [diff] [blame] | 115 | void ExpectError(const std::string& krb5conf, |
| 116 | ConfigErrorCode code, |
| 117 | int line_index) { |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 118 | ConfigErrorInfo error_info = config_parser_.Validate(krb5conf); |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 119 | |
| 120 | EXPECT_TRUE(error_info.has_code()) << error_info; |
| 121 | EXPECT_EQ(code, error_info.code()) << error_info; |
| 122 | EXPECT_TRUE(error_info.has_line_index()) << error_info; |
| 123 | EXPECT_EQ(line_index, error_info.line_index()) << error_info; |
| 124 | } |
| 125 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 126 | void ExpectEncryptionTypes( |
| 127 | const std::string& krb5conf, |
| 128 | KerberosEncryptionTypes expected_encryption_types) { |
| 129 | KerberosEncryptionTypes encryption_types; |
| 130 | |
| 131 | EXPECT_TRUE(config_parser_.GetEncryptionTypes(krb5conf, &encryption_types)); |
| 132 | EXPECT_EQ(expected_encryption_types, encryption_types); |
| 133 | } |
| 134 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 135 | ConfigParser config_parser_; |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 138 | TEST_F(ConfigParserTest, ValidConfig) { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 139 | ExpectNoError(kCompleteKrb5Conf); |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 142 | TEST_F(ConfigParserTest, Empty) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 143 | ExpectNoError(""); |
| 144 | ExpectNoError("\n"); |
| 145 | ExpectNoError("\n\n\n"); |
| 146 | ExpectNoError("[libdefaults]"); |
| 147 | ExpectNoError("[libdefaults]\n"); |
| 148 | ExpectNoError("[libdefaults]\n\n\n"); |
| 149 | } |
| 150 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 151 | TEST_F(ConfigParserTest, ModulesAndIncludesBlocked) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 152 | ExpectError("module MODULEPATH:RESIDUAL", CONFIG_ERROR_KEY_NOT_SUPPORTED, 0); |
| 153 | ExpectError("include /path/to/file", CONFIG_ERROR_KEY_NOT_SUPPORTED, 0); |
| 154 | ExpectError("includedir /path/to/files", CONFIG_ERROR_KEY_NOT_SUPPORTED, 0); |
| 155 | |
| 156 | constexpr char kKrb5Conf[] = R"( |
| 157 | [libdefaults] |
| 158 | includedir /path/to/files)"; |
| 159 | ExpectError(kKrb5Conf, CONFIG_ERROR_KEY_NOT_SUPPORTED, 2); |
| 160 | } |
| 161 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 162 | TEST_F(ConfigParserTest, UnsupportedLibdefaultsKey) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 163 | constexpr char kKrb5Conf[] = R"( |
| 164 | [libdefaults] |
| 165 | stonkskew = 123)"; |
| 166 | ExpectError(kKrb5Conf, CONFIG_ERROR_KEY_NOT_SUPPORTED, 2); |
| 167 | } |
| 168 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 169 | TEST_F(ConfigParserTest, UnsupportedNestedLibdefaultsKey) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 170 | constexpr char kKrb5Conf[] = R"( |
| 171 | [libdefaults] |
| 172 | A.EXAMPLE.COM = { |
| 173 | stonkskew = 300 |
| 174 | })"; |
| 175 | ExpectError(kKrb5Conf, CONFIG_ERROR_KEY_NOT_SUPPORTED, 3); |
| 176 | } |
| 177 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 178 | TEST_F(ConfigParserTest, UnsupportedRealmKey) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 179 | constexpr char kKrb5Conf[] = R"( |
| 180 | [realms] |
| 181 | BEISPIEL.FIR = { |
| 182 | meister_svz = svz.beispiel.fir |
| 183 | })"; |
| 184 | ExpectError(kKrb5Conf, CONFIG_ERROR_KEY_NOT_SUPPORTED, 3); |
| 185 | } |
| 186 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 187 | TEST_F(ConfigParserTest, RelationSyntaxErrorKeyWithoutEquals) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 188 | constexpr char kKrb5Conf[] = R"( |
| 189 | [libdefaults] |
| 190 | kdc: kdc.example.com |
| 191 | )"; |
| 192 | ExpectError(kKrb5Conf, CONFIG_ERROR_RELATION_SYNTAX, 2); |
| 193 | } |
| 194 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 195 | TEST_F(ConfigParserTest, UnsupportedSection) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 196 | ExpectError("[appdefaults]", CONFIG_ERROR_SECTION_NOT_SUPPORTED, 0); |
| 197 | } |
| 198 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 199 | TEST_F(ConfigParserTest, SectionNestedInGroup) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 200 | constexpr char kKrb5Conf[] = R"( |
| 201 | [realms] |
| 202 | EXAMPLE.COM = { |
| 203 | [libdefaults] |
| 204 | })"; |
| 205 | ExpectError(kKrb5Conf, CONFIG_ERROR_SECTION_NESTED_IN_GROUP, 3); |
| 206 | } |
| 207 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 208 | TEST_F(ConfigParserTest, MissingSectionBrackets) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 209 | ExpectError("[realms", CONFIG_ERROR_SECTION_SYNTAX, 0); |
| 210 | } |
| 211 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 212 | TEST_F(ConfigParserTest, SpacesBeforeSectionEndMarker) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 213 | // Note that the krb5 parser appears to accept spaces before the ']', but |
| 214 | // it's a different section than without the spaces, so we reject it. |
| 215 | ExpectError("[realms ]", CONFIG_ERROR_SECTION_NOT_SUPPORTED, 0); |
| 216 | } |
| 217 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 218 | TEST_F(ConfigParserTest, ExtraStuffBeforeSectionBrackets) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 219 | ExpectError("extra [realms]", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 220 | } |
| 221 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 222 | TEST_F(ConfigParserTest, ExtraStuffAfterSectionBrackets) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 223 | ExpectError("[realms] extra", CONFIG_ERROR_SECTION_SYNTAX, 0); |
| 224 | } |
| 225 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 226 | TEST_F(ConfigParserTest, FinalMarkersAllowed) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 227 | ExpectNoError("[libdefaults]* \nclockskew*=9"); |
| 228 | } |
| 229 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 230 | TEST_F(ConfigParserTest, FinalMarkersWithSpacesNotAllowed) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 231 | ExpectError("[libdefaults] *)", CONFIG_ERROR_SECTION_SYNTAX, 0); |
| 232 | ExpectError("[libdefaults]\nclockskew *=9", CONFIG_ERROR_RELATION_SYNTAX, 1); |
| 233 | } |
| 234 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 235 | TEST_F(ConfigParserTest, RelationSyntaxError) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 236 | ExpectError("[libdefaults]\nclockskew", CONFIG_ERROR_RELATION_SYNTAX, 1); |
| 237 | ExpectError("[libdefaults]\nclockskew ", CONFIG_ERROR_RELATION_SYNTAX, 1); |
| 238 | ExpectError("[libdefaults]\nclockskew* ", CONFIG_ERROR_RELATION_SYNTAX, 1); |
| 239 | ExpectError("[libdefaults]\n=clockskew*", CONFIG_ERROR_RELATION_SYNTAX, 1); |
| 240 | } |
| 241 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 242 | TEST_F(ConfigParserTest, TwoEqualSignsAllowed) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 243 | ExpectNoError("[libdefaults]\nclockskew=1=2"); |
| 244 | } |
| 245 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 246 | TEST_F(ConfigParserTest, RelationSyntaxEdgeCases) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 247 | ExpectError("*", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 248 | ExpectError("*=", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 249 | ExpectError("=", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 250 | |
| 251 | ExpectError(" *", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 252 | ExpectError(" *=", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 253 | ExpectError(" =", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 254 | |
| 255 | ExpectError("* ", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 256 | ExpectError("*= ", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 257 | ExpectError("= ", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 258 | |
| 259 | ExpectError(" * ", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 260 | ExpectError(" *= ", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 261 | ExpectError(" = ", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 262 | |
| 263 | ExpectError(" * = ", CONFIG_ERROR_RELATION_SYNTAX, 0); |
| 264 | } |
| 265 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 266 | TEST_F(ConfigParserTest, WhitespaceBeforeAndAfterSectionBrackets) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 267 | ExpectNoError(" [realms] "); |
| 268 | } |
| 269 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 270 | TEST_F(ConfigParserTest, MissingOpeningCurlyBrace) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 271 | constexpr char kKrb5Conf[] = R"( |
| 272 | [realms] |
| 273 | EXAMPLE.COM = |
| 274 | |
| 275 | kdc = kdc.example.com |
| 276 | })"; |
| 277 | ExpectError(kKrb5Conf, CONFIG_ERROR_EXPECTED_OPENING_CURLY_BRACE, 3); |
| 278 | } |
| 279 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 280 | TEST_F(ConfigParserTest, ExtraCurlyBraceFound) { |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 281 | constexpr char kKrb5Conf[] = R"( |
| 282 | [realms] |
| 283 | EXAMPLE.COM = |
| 284 | { |
| 285 | kdc = kdc.example.com |
| 286 | } |
| 287 | })"; |
| 288 | ExpectError(kKrb5Conf, CONFIG_ERROR_EXTRA_CURLY_BRACE, 6); |
| 289 | } |
| 290 | |
Lutz Justen | 70496c1 | 2019-07-24 11:11:55 +0200 | [diff] [blame] | 291 | // Things that the fuzzer found. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 292 | TEST_F(ConfigParserTest, FuzzerRegressionTests) { |
Lutz Justen | 70496c1 | 2019-07-24 11:11:55 +0200 | [diff] [blame] | 293 | // Code was looking at character after "include" to check if it's a space. |
| 294 | ExpectError("include", CONFIG_ERROR_KEY_NOT_SUPPORTED, 0); |
| 295 | |
| 296 | // Code was accepting "[realms\0]" as a valid section. Embedded \0's should be |
| 297 | // handled in a c_str() kind of way. |
| 298 | std::string krb5confWithZero = "[realms\0]"; |
| 299 | krb5confWithZero[7] = 0; |
| 300 | ExpectError(krb5confWithZero, CONFIG_ERROR_SECTION_SYNTAX, 0); |
| 301 | |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 302 | // Code was allowing spaces in keys. Note that ConfigParser allows all keys |
Lutz Justen | 70496c1 | 2019-07-24 11:11:55 +0200 | [diff] [blame] | 303 | // in the [domain_realm] section, but it should still check spaces! |
| 304 | ExpectError("[domain_realm]\nkey x=", CONFIG_ERROR_RELATION_SYNTAX, 1); |
| 305 | |
| 306 | // \r should not be counted as newline character. |
| 307 | ExpectError("[domain_realm]\rkey=", CONFIG_ERROR_SECTION_SYNTAX, 0); |
| 308 | |
| 309 | // Double == is always a relation, cannot be the start of a group. |
| 310 | ExpectError("[capaths]\nkey==\n{", CONFIG_ERROR_RELATION_SYNTAX, 2); |
Sergey Poromov | 38cec24 | 2021-03-12 13:47:57 +0100 | [diff] [blame] | 311 | |
| 312 | // Too many nested groups should lead to an error preventing stack overflow |
| 313 | // in krb5 parser. |
| 314 | ExpectNoError(GenerateNestedGroups(1000)); |
| 315 | ExpectError(GenerateNestedGroups(1001), CONFIG_ERROR_TOO_MANY_NESTED_GROUPS, |
| 316 | 1000); |
Lutz Justen | 70496c1 | 2019-07-24 11:11:55 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 319 | // |GetEncryptionTypes| with a complete config to be parsed. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 320 | TEST_F(ConfigParserTest, GetEncryptionTypesCompleteConfig) { |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 321 | ExpectEncryptionTypes(kCompleteKrb5Conf, KerberosEncryptionTypes::kStrong); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // |GetEncryptionTypes| with all encryption types allowed. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 325 | TEST_F(ConfigParserTest, GetEncryptionTypesAll) { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 326 | constexpr char kKrb5Conf[] = R"( |
| 327 | [libdefaults] |
| 328 | default_tkt_enctypes = aes256-cts-hmac-sha1-96 arcfour-hmac-md5-exp |
| 329 | default_tgs_enctypes = aes256-cts-hmac-sha1-96 arcfour-hmac-md5-exp |
| 330 | permitted_enctypes = aes256-cts-hmac-sha1-96 arcfour-hmac-md5-exp)"; |
| 331 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 332 | ExpectEncryptionTypes(kKrb5Conf, KerberosEncryptionTypes::kAll); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // |GetEncryptionTypes| with only strong encryption types allowed. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 336 | TEST_F(ConfigParserTest, GetEncryptionTypesStrong) { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 337 | constexpr char kKrb5Conf[] = R"( |
| 338 | [libdefaults] |
| 339 | default_tkt_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 340 | default_tgs_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 341 | permitted_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96)"; |
| 342 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 343 | ExpectEncryptionTypes(kKrb5Conf, KerberosEncryptionTypes::kStrong); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // |GetEncryptionTypes| with only legacy encryption types allowed. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 347 | TEST_F(ConfigParserTest, GetEncryptionTypesLegacy) { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 348 | constexpr char kKrb5Conf[] = R"( |
| 349 | [libdefaults] |
| 350 | default_tkt_enctypes = arcfour-hmac-md5-exp des3-cbc-raw |
| 351 | default_tgs_enctypes = arcfour-hmac-md5-exp des3-cbc-raw |
| 352 | permitted_enctypes = arcfour-hmac-md5-exp des3-cbc-raw)"; |
| 353 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 354 | ExpectEncryptionTypes(kKrb5Conf, KerberosEncryptionTypes::kLegacy); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // |GetEncryptionTypes| with enctypes fields missing. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 358 | TEST_F(ConfigParserTest, GetEncryptionTypesMissingFields) { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 359 | // Empty config allows all encryption types. |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 360 | ExpectEncryptionTypes("", KerberosEncryptionTypes::kAll); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 361 | |
| 362 | constexpr char kKrb5Conf1[] = R"( |
| 363 | [libdefaults] |
| 364 | default_tkt_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 365 | default_tgs_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96)"; |
| 366 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 367 | ExpectEncryptionTypes(kKrb5Conf1, KerberosEncryptionTypes::kAll); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 368 | |
| 369 | constexpr char kKrb5Conf2[] = R"( |
| 370 | [libdefaults] |
| 371 | default_tkt_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 372 | permitted_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96)"; |
| 373 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 374 | ExpectEncryptionTypes(kKrb5Conf2, KerberosEncryptionTypes::kAll); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 375 | |
| 376 | constexpr char kKrb5Conf3[] = R"( |
| 377 | [libdefaults] |
| 378 | default_tgs_enctypes = aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96 |
| 379 | permitted_enctypes =aes256-cts-hmac-sha1-96 aes128-cts-hmac-sha1-96)"; |
| 380 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 381 | ExpectEncryptionTypes(kKrb5Conf3, KerberosEncryptionTypes::kAll); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // |GetEncryptionTypes| with |DEFAULT| enctypes assigned. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 385 | TEST_F(ConfigParserTest, GetEncryptionTypesDefaultValues) { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 386 | constexpr char kKrb5Conf[] = R"( |
| 387 | [libdefaults] |
| 388 | default_tkt_enctypes = DEFAULT |
| 389 | default_tgs_enctypes = DEFAULT |
| 390 | permitted_enctypes = DEFAULT)"; |
| 391 | |
| 392 | // |DEFAULT| value allows all encryption types. |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 393 | ExpectEncryptionTypes(kKrb5Conf, KerberosEncryptionTypes::kAll); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // |GetEncryptionTypes| with comma separated encryption types list. |
Felipe Andrade | a0cbde7 | 2020-04-01 15:40:10 +0200 | [diff] [blame] | 397 | TEST_F(ConfigParserTest, GetEncryptionTypesCommaSeparated) { |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 398 | constexpr char kKrb5Conf[] = R"( |
| 399 | [libdefaults] |
| 400 | default_tkt_enctypes = aes256-cts-hmac-sha1-96, arcfour-hmac-md5-exp |
| 401 | default_tgs_enctypes = aes256-cts-hmac-sha1-96, arcfour-hmac-md5-exp |
| 402 | permitted_enctypes = aes256-cts-hmac-sha1-96,arcfour-hmac-md5-exp)"; |
| 403 | |
Felipe Andrade | 90cb84e | 2020-04-07 20:28:33 +0200 | [diff] [blame] | 404 | ExpectEncryptionTypes(kKrb5Conf, KerberosEncryptionTypes::kAll); |
| 405 | } |
| 406 | |
| 407 | // |GetEncryptionTypes| with invalid config. |
| 408 | TEST_F(ConfigParserTest, GetEncryptionTypesInvalidConfig) { |
| 409 | constexpr char kKrb5Conf[] = R"( |
| 410 | [libdefaults] |
| 411 | stonkskew = 123)"; |
| 412 | |
| 413 | KerberosEncryptionTypes encryption_types; |
| 414 | |
| 415 | EXPECT_FALSE(config_parser_.GetEncryptionTypes(kKrb5Conf, &encryption_types)); |
| 416 | // |encryption_types| should've been set to the default value in our feature. |
| 417 | EXPECT_EQ(KerberosEncryptionTypes::kStrong, encryption_types); |
Felipe Andrade | 66aaf6b | 2020-03-24 13:05:57 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Lutz Justen | 5163109 | 2019-07-05 09:19:58 +0200 | [diff] [blame] | 420 | } // namespace kerberos |