xrpld
Loading...
Searching...
No Matches
Manifest_test.cpp
1#include <test/jtx/Env.h>
2#include <test/unit_test/utils.h>
3
4#include <xrpld/app/misc/ValidatorList.h>
5
6#include <xrpl/basics/Slice.h>
7#include <xrpl/basics/base64.h>
8#include <xrpl/basics/contract.h>
9#include <xrpl/basics/strHex.h>
10#include <xrpl/beast/unit_test/suite.h>
11#include <xrpl/config/Constants.h>
12#include <xrpl/protocol/HashPrefix.h>
13#include <xrpl/protocol/KeyType.h>
14#include <xrpl/protocol/PublicKey.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STObject.h>
17#include <xrpl/protocol/SecretKey.h>
18#include <xrpl/protocol/Seed.h>
19#include <xrpl/protocol/Serializer.h>
20#include <xrpl/protocol/Sign.h>
21#include <xrpl/protocol/tokens.h>
22#include <xrpl/server/Manifest.h>
23#include <xrpl/server/Wallet.h>
24
25#include <algorithm>
26#include <array>
27#include <cassert>
28#include <cstdint>
29#include <exception>
30#include <filesystem>
31#include <limits>
32#include <memory>
33#include <optional>
34#include <stdexcept>
35#include <string>
36#include <utility>
37#include <vector>
38
39namespace xrpl::test {
40
42{
43private:
44 static PublicKey
49
50 static PublicKey
55
56 static void
58 {
59 using namespace std::filesystem;
60 if (!exists(dbPath) || !is_directory(dbPath) || !is_empty(dbPath))
61 return;
62 remove(dbPath);
63 }
64
65 static void
67 {
68 using namespace std::filesystem;
69 if (!exists(dbPath))
70 {
71 create_directory(dbPath);
72 return;
73 }
74
75 if (!is_directory(dbPath))
76 {
77 // someone created a file where we want to put our directory
78 Throw<std::runtime_error>("Cannot create directory: " + dbPath.string());
79 }
80 }
83 {
84 return std::filesystem::current_path() / "manifest_test_databases";
85 }
86
87public:
89 {
90 try
91 {
93 }
94 catch (std::exception const&) // NOLINT(bugprone-empty-catch)
95 {
96 }
97 }
98 ~Manifest_test() override
99 {
100 try
101 {
103 }
104 catch (std::exception const&) // NOLINT(bugprone-empty-catch)
105 {
106 }
107 }
108
109 static std::string
111 PublicKey const& pk,
112 SecretKey const& sk,
113 PublicKey const& spk,
114 SecretKey const& ssk,
115 int seq)
116 {
118 st[sfSequence] = seq;
119 st[sfPublicKey] = pk;
120 st[sfSigningPubKey] = spk;
121
122 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
123 sign(st, HashPrefix::Manifest, *publicKeyType(spk), ssk);
124
125 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
126 sign(st, HashPrefix::Manifest, *publicKeyType(pk), sk, sfMasterSignature);
127
128 Serializer s;
129 st.add(s);
130
131 return base64Encode(std::string(static_cast<char const*>(s.data()), s.size()));
132 }
133
135 makeRevocationString(SecretKey const& sk, KeyType type, bool invalidSig = false)
136 {
137 auto const pk = derivePublicKey(type, sk);
138
141 st[sfPublicKey] = pk;
142
143 sign(
144 st, HashPrefix::Manifest, type, invalidSig ? randomSecretKey() : sk, sfMasterSignature);
145 BEAST_EXPECT(invalidSig ^ verify(st, HashPrefix::Manifest, pk, sfMasterSignature));
146
147 Serializer s;
148 st.add(s);
149
150 return base64Encode(std::string(static_cast<char const*>(s.data()), s.size()));
151 }
152
154 makeRevocation(SecretKey const& sk, KeyType type, bool invalidSig = false)
155 {
156 auto const pk = derivePublicKey(type, sk);
157
160 st[sfPublicKey] = pk;
161
162 sign(
163 st, HashPrefix::Manifest, type, invalidSig ? randomSecretKey() : sk, sfMasterSignature);
164 BEAST_EXPECT(invalidSig ^ verify(st, HashPrefix::Manifest, pk, sfMasterSignature));
165
166 Serializer s;
167 st.add(s);
168
169 std::string const m(static_cast<char const*>(s.data()), s.size());
170 if (auto r = deserializeManifest(m))
171 return std::move(*r);
172 Throw<std::runtime_error>("Could not create a revocation manifest");
173 return *deserializeManifest(std::string{}); // Silence compiler warning.
174 }
175
178 SecretKey const& sk,
179 KeyType type,
180 SecretKey const& ssk,
181 KeyType stype,
182 int seq,
183 bool invalidSig = false)
184 {
185 auto const pk = derivePublicKey(type, sk);
186 auto const spk = derivePublicKey(stype, ssk);
187
189 st[sfSequence] = seq;
190 st[sfPublicKey] = pk;
191 st[sfSigningPubKey] = spk;
192
193 sign(st, HashPrefix::Manifest, stype, ssk);
194 BEAST_EXPECT(verify(st, HashPrefix::Manifest, spk));
195
196 sign(
197 st, HashPrefix::Manifest, type, invalidSig ? randomSecretKey() : sk, sfMasterSignature);
198 BEAST_EXPECT(invalidSig ^ verify(st, HashPrefix::Manifest, pk, sfMasterSignature));
199
200 Serializer s;
201 st.add(s);
202
203 std::string const m(static_cast<char const*>(s.data()), s.size());
204 if (auto r = deserializeManifest(m))
205 return std::move(*r);
206 Throw<std::runtime_error>("Could not create a manifest");
207 return *deserializeManifest(std::string{}); // Silence compiler warning.
208 }
209
210 static Manifest
211 clone(Manifest const& m)
212 {
214 return m2;
215 }
216
217 void
219 {
220 testcase("load/store");
221
222 std::string const dbName("ManifestCacheTestDB");
223 {
224 jtx::Env env(*this);
225 DatabaseCon::Setup setup;
226 setup.dataDir = getDatabasePath();
227 assert(!setup.useGlobalPragma);
228
229 auto dbCon = makeTestWalletDB(setup, dbName, env.journal);
230
231 auto const getPopulatedManifests =
232 [](ManifestCache const& cache) -> std::vector<Manifest const*> {
234 result.reserve(32);
235 cache.forEachManifest([&result](Manifest const& man) { result.push_back(&man); });
236 return result;
237 };
239 std::ranges::sort(mv, [](Manifest const* lhs, Manifest const* rhs) {
240 return lhs->serialized < rhs->serialized;
241 });
242 return mv;
243 };
244 std::vector<Manifest const*> const inManifests(sort(getPopulatedManifests(m)));
245
246 auto& app = env.app();
248 m, m, env.timeKeeper(), app.config().legacy(Sections::kDatabasePath), env.journal);
249
250 {
251 // save should not store untrusted master keys to db
252 // except for revocations
253 m.save(*dbCon, "ValidatorManifests", [&unl](PublicKey const& pubKey) {
254 return unl->listed(pubKey);
255 });
256
257 ManifestCache loaded;
258
259 loaded.load(*dbCon, "ValidatorManifests");
260
261 // check that all loaded manifests are revocations
262 std::vector<Manifest const*> const loadedManifests(
263 sort(getPopulatedManifests(loaded)));
264
265 for (auto const& man : loadedManifests)
266 BEAST_EXPECT(man->revoked());
267 }
268 {
269 // save should store all trusted master keys to db
271 std::vector<std::string> const keys;
272 s1.reserve(inManifests.size());
273
274 for (auto const& man : inManifests)
275 s1.push_back(toBase58(TokenType::NodePublic, man->masterKey));
276 unl->load({}, s1, keys);
277
278 m.save(*dbCon, "ValidatorManifests", [&unl](PublicKey const& pubKey) {
279 return unl->listed(pubKey);
280 });
281 ManifestCache loaded;
282 loaded.load(*dbCon, "ValidatorManifests");
283
284 // check that the manifest caches are the same
285 std::vector<Manifest const*> const loadedManifests(
286 sort(getPopulatedManifests(loaded)));
287
288 if (inManifests.size() == loadedManifests.size())
289 {
290 BEAST_EXPECT(
292 inManifests,
293 loadedManifests,
294 [](Manifest const* lhs, Manifest const* rhs) { return *lhs == *rhs; }));
295 }
296 else
297 {
298 fail();
299 }
300 }
301 {
302 // load config manifest
303 ManifestCache loaded;
304 std::vector<std::string> const emptyRevocation;
305
306 std::string const badManifest = "bad manifest";
307 BEAST_EXPECT(
308 !loaded.load(*dbCon, "ValidatorManifests", badManifest, emptyRevocation));
309
310 auto const sk = randomSecretKey();
311 auto const pk = derivePublicKey(KeyType::Ed25519, sk);
312 auto const kp = randomKeyPair(KeyType::Secp256k1);
313
314 std::string const cfgManifest = makeManifestString(pk, sk, kp.first, kp.second, 0);
315
316 BEAST_EXPECT(
317 loaded.load(*dbCon, "ValidatorManifests", cfgManifest, emptyRevocation));
318 }
319 {
320 // load config revocation
321 ManifestCache loaded;
322 std::string const emptyManifest;
323
324 std::vector<std::string> const badRevocation = {"bad revocation"};
325 BEAST_EXPECT(
326 !loaded.load(*dbCon, "ValidatorManifests", emptyManifest, badRevocation));
327
328 auto const sk = randomSecretKey();
329 auto const keyType = KeyType::Ed25519;
330 auto const pk = derivePublicKey(keyType, sk);
331 auto const kp = randomKeyPair(KeyType::Secp256k1);
332 std::vector<std::string> const nonRevocation = {
333 makeManifestString(pk, sk, kp.first, kp.second, 0),
334 };
335
336 BEAST_EXPECT(
337 !loaded.load(*dbCon, "ValidatorManifests", emptyManifest, nonRevocation));
338 BEAST_EXPECT(!loaded.revoked(pk));
339
340 std::vector<std::string> const badSigRevocation = {
341 makeRevocationString(sk, keyType, true),
342 };
343 BEAST_EXPECT(
344 !loaded.load(*dbCon, "ValidatorManifests", emptyManifest, badSigRevocation));
345 BEAST_EXPECT(!loaded.revoked(pk));
346
347 std::vector<std::string> const cfgRevocation = {makeRevocationString(sk, keyType)};
348 BEAST_EXPECT(
349 loaded.load(*dbCon, "ValidatorManifests", emptyManifest, cfgRevocation));
350
351 BEAST_EXPECT(loaded.revoked(pk));
352 }
353 }
355 }
356
357 void
359 {
360 testcase("getSignature");
361 auto const sk = randomSecretKey();
362 auto const pk = derivePublicKey(KeyType::Ed25519, sk);
363 auto const kp = randomKeyPair(KeyType::Secp256k1);
364 auto const m = makeManifest(sk, KeyType::Ed25519, kp.second, KeyType::Secp256k1, 0);
365
367 st[sfSequence] = 0;
368 st[sfPublicKey] = pk;
369 st[sfSigningPubKey] = kp.first;
370 Serializer ss;
373 auto const sig = sign(KeyType::Secp256k1, kp.second, ss.slice());
374 BEAST_EXPECT(
375 strHex(sig) ==
376 strHex(*m.getSignature())); // NOLINT(bugprone-unchecked-optional-access)
377
378 auto const masterSig = sign(KeyType::Ed25519, sk, ss.slice());
379 BEAST_EXPECT(strHex(masterSig) == strHex(m.getMasterSignature()));
380 }
381
382 void
384 {
385 testcase("getKeys");
386
387 ManifestCache cache;
388 auto const sk = randomSecretKey();
389 auto const pk = derivePublicKey(KeyType::Ed25519, sk);
390
391 // getSigningKey should return same key if there is no manifest
392 BEAST_EXPECT(cache.getSigningKey(pk) == pk);
393
394 // getSigningKey should return the ephemeral public key
395 // for the listed validator master public key
396 // getMasterKey should return the listed validator master key
397 // for that ephemeral public key
398 auto const kp0 = randomKeyPair(KeyType::Secp256k1);
399 BEAST_EXPECT(
401 cache.applyManifest(
404 BEAST_EXPECT(cache.getSigningKey(pk) == kp0.first);
405 BEAST_EXPECT(cache.getMasterKey(kp0.first) == pk);
406
407 // getSigningKey should return the latest ephemeral public key
408 // for the listed validator master public key
409 // getMasterKey should only return a master key for the latest
410 // ephemeral public key
411 auto const kp1 = randomKeyPair(KeyType::Secp256k1);
412 BEAST_EXPECT(
414 cache.applyManifest(
417 BEAST_EXPECT(cache.getSigningKey(pk) == kp1.first);
418 BEAST_EXPECT(cache.getMasterKey(kp1.first) == pk);
419 BEAST_EXPECT(cache.getMasterKey(kp0.first) == kp0.first);
420
421 // getSigningKey and getMasterKey should fail if a new manifest is
422 // applied with the same signing key but a higher sequence
423 BEAST_EXPECT(
425 cache.applyManifest(
428 BEAST_EXPECT(cache.getSigningKey(pk) == kp1.first);
429 BEAST_EXPECT(cache.getMasterKey(kp1.first) == pk);
430 BEAST_EXPECT(cache.getMasterKey(kp0.first) == kp0.first);
431
432 // getSigningKey should return std::nullopt for a revoked master public
433 // key getMasterKey should return std::nullopt for an ephemeral public
434 // key from a revoked master public key
435 BEAST_EXPECT(
437 cache.applyManifest(
439 BEAST_EXPECT(cache.revoked(pk));
440 BEAST_EXPECT(cache.getSigningKey(pk) == pk);
441 BEAST_EXPECT(cache.getMasterKey(kp0.first) == kp0.first);
442 BEAST_EXPECT(cache.getMasterKey(kp1.first) == kp1.first);
443 }
444
445 void
447 {
448 testcase("validator token");
449
450 {
451 auto const valSecret = parseBase58<SecretKey>(
452 TokenType::NodePrivate, "paQmjZ37pKKPMrgadBLsuf9ab7Y7EUNzh27LQrZqoexpAs31nJi");
453
454 // Format token string to test trim()
455 std::vector<std::string> const tokenBlob = {
456 " eyJ2YWxpZGF0aW9uX3NlY3JldF9rZXkiOiI5ZWQ0NWY4NjYyNDFjYzE4YTI3NDdiNT\n",
457 " \tQzODdjMDYyNTkwNzk3MmY0ZTcxOTAyMzFmYWE5Mzc0NTdmYTlkYWY2IiwibWFuaWZl \n",
458 "\tc3QiOiJKQUFBQUFGeEllMUZ0d21pbXZHdEgyaUNjTUpxQzlnVkZLaWxHZncxL3ZDeE\n",
459 "\t hYWExwbGMyR25NaEFrRTFhZ3FYeEJ3RHdEYklENk9NU1l1TTBGREFscEFnTms4U0tG\t \t\n",
460 "bjdNTzJmZGtjd1JRSWhBT25ndTlzQUtxWFlvdUorbDJWMFcrc0FPa1ZCK1pSUzZQU2\n",
461 "hsSkFmVXNYZkFpQnNWSkdlc2FhZE9KYy9hQVpva1MxdnltR21WcmxIUEtXWDNZeXd1\n",
462 "NmluOEhBU1FLUHVnQkQ2N2tNYVJGR3ZtcEFUSGxHS0pkdkRGbFdQWXk1QXFEZWRGdj\n",
463 "VUSmEydzBpMjFlcTNNWXl3TFZKWm5GT3I3QzBrdzJBaVR6U0NqSXpkaXRROD0ifQ==\n",
464 };
465
466 auto const manifest =
467 "JAAAAAFxIe1FtwmimvGtH2iCcMJqC9gVFKilGfw1/"
468 "vCxHXXLplc2GnMhAkE1agqXxBwD"
469 "wDbID6OMSYuM0FDAlpAgNk8SKFn7MO2fdkcwRQIhAOngu9sAKqXYouJ+l2V0W+"
470 "sAOkVB"
471 "+ZRS6PShlJAfUsXfAiBsVJGesaadOJc/"
472 "aAZokS1vymGmVrlHPKWX3Yywu6in8HASQKPu"
473 "gBD67kMaRFGvmpATHlGKJdvDFlWPYy5AqDedFv5TJa2w0i21eq3MYywLVJZnFO"
474 "r7C0kw"
475 "2AiTzSCjIzditQ8=";
476
477 auto const token = loadValidatorToken(tokenBlob);
478 BEAST_EXPECT(token);
479 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
480 BEAST_EXPECT(test::equal(token->validationSecret, *valSecret));
481 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
482 BEAST_EXPECT(token->manifest == manifest);
483 }
484 {
485 std::vector<std::string> const badToken = {"bad token"};
486 BEAST_EXPECT(!loadValidatorToken(badToken));
487 }
488 }
489
490 void
492 {
493 testcase("Versioning");
494
496 auto const pk = derivePublicKey(KeyType::Ed25519, sk);
497
499 auto const spk = derivePublicKey(KeyType::Secp256k1, ssk);
500
501 auto const buildManifestObject = [&](std::uint16_t version) {
503 st[sfSequence] = 3;
504 st[sfPublicKey] = pk;
505 st[sfSigningPubKey] = spk;
506
507 if (version != 0)
508 st[sfVersion] = version;
509
510 sign(st, HashPrefix::Manifest, KeyType::Ed25519, sk, sfMasterSignature);
512
513 Serializer s;
514 st.add(s);
515
516 return std::string(static_cast<char const*>(s.data()), s.size());
517 };
518
519 // We understand version 0 manifests:
520 BEAST_EXPECT(deserializeManifest(buildManifestObject(0)));
521
522 // We don't understand any other versions:
523 BEAST_EXPECT(!deserializeManifest(buildManifestObject(1)));
524 BEAST_EXPECT(!deserializeManifest(buildManifestObject(2001)));
525 }
526
527 void
529 {
531
532 std::uint32_t sequence = 0;
533
534 // public key with invalid type
535 std::array<std::uint8_t, 33> const badKey{
536 0x99, 0x30, 0xE7, 0xFC, 0x9D, 0x56, 0xBB, 0x25, 0xD6, 0x89, 0x3B,
537 0xA3, 0xF3, 0x17, 0xAE, 0x5B, 0xCF, 0x33, 0xB3, 0x29, 0x1B, 0xD6,
538 0x3D, 0xB3, 0x26, 0x54, 0xA3, 0x13, 0x22, 0x2F, 0x7F, 0xD0, 0x20,
539 };
540
541 // Short public key:
542 std::array<std::uint8_t, 16> const shortKey{
543 0x03,
544 0x30,
545 0xE7,
546 0xFC,
547 0x9D,
548 0x56,
549 0xBB,
550 0x25,
551 0xD6,
552 0x89,
553 0x3B,
554 0xA3,
555 0xF3,
556 0x17,
557 0xAE,
558 0x5B,
559 };
560
561 auto const toString = [](STObject const& st) {
562 Serializer s;
563 st.add(s);
564
565 return std::string(static_cast<char const*>(s.data()), s.size());
566 };
567
568 for (auto const keyType : keyTypes)
569 {
570 auto const sk = generateSecretKey(keyType, randomSeed());
571 auto const pk = derivePublicKey(keyType, sk);
572
573 for (auto const sKeyType : keyTypes)
574 {
575 auto const ssk = generateSecretKey(sKeyType, randomSeed());
576 auto const spk = derivePublicKey(sKeyType, ssk);
577
578 auto const buildManifestObject = [&](std::uint32_t seq,
580 bool noSigningPublic = false,
581 bool noSignature = false) {
583 st[sfSequence] = seq;
584 st[sfPublicKey] = pk;
585
586 if (domain)
587 st[sfDomain] = makeSlice(*domain);
588
589 if (!noSigningPublic)
590 st[sfSigningPubKey] = spk;
591
592 sign(st, HashPrefix::Manifest, keyType, sk, sfMasterSignature);
593
594 if (!noSignature)
595 sign(st, HashPrefix::Manifest, sKeyType, ssk);
596
597 return st;
598 };
599
600 {
601 testcase << "deserializeManifest: normal manifest (" << to_string(keyType)
602 << " + " << to_string(sKeyType) << ")";
603
604 { // valid manifest without domain
605 auto const st = buildManifestObject(++sequence, std::nullopt);
606
607 auto const m = toString(st);
608 auto const manifest = deserializeManifest(m);
609
610 BEAST_EXPECT(manifest);
611 // NOLINTBEGIN(bugprone-unchecked-optional-access)
612 BEAST_EXPECT(manifest->masterKey == pk);
613 BEAST_EXPECT(manifest->signingKey == spk);
614 BEAST_EXPECT(manifest->sequence == sequence);
615 BEAST_EXPECT(manifest->serialized == m);
616 BEAST_EXPECT(manifest->domain.empty());
617 BEAST_EXPECT(manifest->verify());
618 // NOLINTEND(bugprone-unchecked-optional-access)
619 }
620
621 { // invalid manifest (empty domain)
622 auto const st = buildManifestObject(++sequence, std::string{});
623
624 BEAST_EXPECT(!deserializeManifest(toString(st)));
625 }
626
627 { // invalid manifest (domain too short)
628 auto const st = buildManifestObject(++sequence, std::string{"a.b"});
629 BEAST_EXPECT(!deserializeManifest(toString(st)));
630 }
631 { // invalid manifest (domain too long)
632 std::string const s(254, 'a');
633 auto const st = buildManifestObject(++sequence, s + ".example.com");
634 BEAST_EXPECT(!deserializeManifest(toString(st)));
635 }
636 { // invalid manifest (domain component too long)
637 std::string const s(72, 'a');
638 auto const st = buildManifestObject(++sequence, s + ".example.com");
639 BEAST_EXPECT(!deserializeManifest(toString(st)));
640 }
641
642 auto const st = buildManifestObject(++sequence, std::string{"example.com"});
643
644 {
645 // valid manifest with domain
646 auto const m = toString(st);
647 auto const manifest = deserializeManifest(m);
648
649 BEAST_EXPECT(manifest);
650 // NOLINTBEGIN(bugprone-unchecked-optional-access)
651 BEAST_EXPECT(manifest->masterKey == pk);
652 BEAST_EXPECT(manifest->signingKey == spk);
653 BEAST_EXPECT(manifest->sequence == sequence);
654 BEAST_EXPECT(manifest->serialized == m);
655 BEAST_EXPECT(manifest->domain == "example.com");
656 BEAST_EXPECT(manifest->verify());
657 // NOLINTEND(bugprone-unchecked-optional-access)
658 }
659 {
660 // valid manifest with invalid signature
661 auto badSigSt = st;
662 badSigSt[sfSequence] = sequence + 1;
663
664 auto const m = toString(badSigSt);
665 auto const manifest = deserializeManifest(m);
666
667 BEAST_EXPECT(manifest);
668 // NOLINTBEGIN(bugprone-unchecked-optional-access)
669 BEAST_EXPECT(manifest->masterKey == pk);
670 BEAST_EXPECT(manifest->signingKey == spk);
671 BEAST_EXPECT(manifest->sequence == sequence + 1);
672 BEAST_EXPECT(manifest->serialized == m);
673 BEAST_EXPECT(manifest->domain == "example.com");
674 BEAST_EXPECT(!manifest->verify());
675 // NOLINTEND(bugprone-unchecked-optional-access)
676 }
677 {
678 // reject missing sequence
679 auto badSt = st;
680 BEAST_EXPECT(badSt.delField(sfSequence));
681 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
682 }
683 {
684 // reject missing public key
685 auto badSt = st;
686 BEAST_EXPECT(badSt.delField(sfPublicKey));
687 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
688 }
689 {
690 // reject invalid public key type
691 auto badSt = st;
692 badSt[sfPublicKey] = makeSlice(badKey);
693 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
694 }
695 {
696 // reject short public key
697 auto badSt = st;
698 badSt[sfPublicKey] = makeSlice(shortKey);
699 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
700 }
701 {
702 // reject missing signing public key
703 auto badSt = st;
704 BEAST_EXPECT(badSt.delField(sfSigningPubKey));
705 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
706 }
707 {
708 // reject invalid signing public key type
709 auto badSt = st;
710 badSt[sfSigningPubKey] = makeSlice(badKey);
711 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
712 }
713 {
714 // reject short signing public key
715 auto badSt = st;
716 badSt[sfSigningPubKey] = makeSlice(shortKey);
717 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
718 }
719 {
720 // reject missing signature
721 auto badSt = st;
722 BEAST_EXPECT(badSt.delField(sfMasterSignature));
723 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
724 }
725 {
726 // reject missing signing key signature
727 auto badSt = st;
728 BEAST_EXPECT(badSt.delField(sfSignature));
729 BEAST_EXPECT(!deserializeManifest(toString(badSt)));
730 }
731 {
732 // reject matching master & ephemeral keys
734 st[sfSequence] = 314159;
735 st[sfPublicKey] = pk;
736 st[sfSigningPubKey] = pk;
737
738 sign(st, HashPrefix::Manifest, keyType, sk, sfMasterSignature);
739
740 sign(st, HashPrefix::Manifest, sKeyType, sk);
741
742 BEAST_EXPECT(!deserializeManifest(toString(st)));
743 }
744 }
745
746 {
747 testcase << "deserializeManifest: revocation manifest (" << to_string(keyType)
748 << " + " << to_string(sKeyType) << ")";
749
750 // valid revocation
751 {
752 auto const st = buildManifestObject(
753 std::numeric_limits<std::uint32_t>::max(), std::nullopt, true, true);
754
755 auto const m = toString(st);
756 auto const manifest = deserializeManifest(m);
757
758 BEAST_EXPECT(manifest);
759 // NOLINTBEGIN(bugprone-unchecked-optional-access)
760 BEAST_EXPECT(manifest->masterKey == pk);
761
762 // Since this manifest is revoked, it should not have a signingKey
763 BEAST_EXPECT(!manifest->signingKey);
764 BEAST_EXPECT(manifest->revoked());
765 BEAST_EXPECT(manifest->domain.empty());
766 BEAST_EXPECT(manifest->serialized == m);
767 BEAST_EXPECT(manifest->verify());
768 // NOLINTEND(bugprone-unchecked-optional-access)
769 }
770
771 { // can't specify an ephemeral signing key
772 auto const st = buildManifestObject(
773 std::numeric_limits<std::uint32_t>::max(), std::nullopt, true, false);
774
775 BEAST_EXPECT(!deserializeManifest(toString(st)));
776 }
777 { // can't specify an ephemeral signature
778 auto const st = buildManifestObject(
779 std::numeric_limits<std::uint32_t>::max(), std::nullopt, false, true);
780
781 BEAST_EXPECT(!deserializeManifest(toString(st)));
782 }
783 { // can't specify an ephemeral key & signature
784 auto const st = buildManifestObject(
785 std::numeric_limits<std::uint32_t>::max(), std::nullopt, false, false);
786
787 BEAST_EXPECT(!deserializeManifest(toString(st)));
788 }
789 }
790 }
791 }
792 }
793
794 void
796 {
797 testcase("Manifest Domain Names");
798
800 auto const pk1 = derivePublicKey(KeyType::Secp256k1, sk1);
801
803 auto const pk2 = derivePublicKey(KeyType::Secp256k1, sk2);
804
805 auto const test = [&](std::string domain) {
807 st[sfSequence] = 7;
808 st[sfPublicKey] = pk1;
809 st[sfDomain] = makeSlice(domain);
810 st[sfSigningPubKey] = pk2;
811
812 sign(st, HashPrefix::Manifest, KeyType::Secp256k1, sk1, sfMasterSignature);
814
815 Serializer s;
816 st.add(s);
817
818 return deserializeManifest(std::string(static_cast<char const*>(s.data()), s.size()));
819 };
820
821 BEAST_EXPECT(test("example.com"));
822 BEAST_EXPECT(test("test.example.com"));
823 BEAST_EXPECT(test("example-domain.com"));
824 BEAST_EXPECT(test("xn--mxavchb.gr"));
825 BEAST_EXPECT(test("test.xn--mxavchb.gr"));
826 BEAST_EXPECT(test("123.gr"));
827 BEAST_EXPECT(test("x.yz"));
828 BEAST_EXPECT(test(std::string(63, 'a') + ".example.com"));
829 BEAST_EXPECT(test(std::string(63, 'a') + "." + std::string(63, 'b')));
830
831 // No period
832 BEAST_EXPECT(!test("example"));
833
834 // Leading period:
835 BEAST_EXPECT(!test(".com"));
836 BEAST_EXPECT(!test(".example.com"));
837
838 // A trailing period is technically valid but we don't allow it
839 BEAST_EXPECT(!test("example.com."));
840
841 // A component can't start or end with a dash
842 BEAST_EXPECT(!test("-example.com"));
843 BEAST_EXPECT(!test("example-.com"));
844
845 // Empty component:
846 BEAST_EXPECT(!test("double..periods.example.com"));
847
848 // TLD too short or too long:
849 BEAST_EXPECT(!test("example.x"));
850 BEAST_EXPECT(!test("example." + std::string(64, 'a')));
851
852 // Invalid characters:
853 BEAST_EXPECT(!test("example.com-org"));
854 BEAST_EXPECT(!test("bang!.com"));
855 BEAST_EXPECT(!test("bang!.example.com"));
856
857 // Too short
858 BEAST_EXPECT(!test("a.b"));
859
860 // Single component too long:
861 BEAST_EXPECT(!test(std::string(64, 'a') + ".com"));
862 BEAST_EXPECT(!test(std::string(64, 'a') + ".example.com"));
863
864 // Multiple components too long:
865 BEAST_EXPECT(!test(std::string(64, 'a') + "." + std::string(64, 'b')));
866 BEAST_EXPECT(!test(std::string(64, 'a') + "." + std::string(64, 'b')));
867
868 // Overall too long:
869 BEAST_EXPECT(!test(std::string(63, 'a') + "." + std::string(63, 'b') + ".example.com"));
870 }
871
872 void
873 run() override
874 {
875 ManifestCache cache;
876 {
877 testcase("apply");
878
879 auto const skA = randomSecretKey();
880 auto const pkA = derivePublicKey(KeyType::Ed25519, skA);
881 auto const kpA0 = randomKeyPair(KeyType::Secp256k1);
882 auto const kpA1 = randomKeyPair(KeyType::Secp256k1);
883 auto const sA0 =
884 makeManifest(skA, KeyType::Ed25519, kpA0.second, KeyType::Secp256k1, 0);
885 auto const sA1 =
886 makeManifest(skA, KeyType::Ed25519, kpA1.second, KeyType::Secp256k1, 1);
887 auto const sA2 =
888 makeManifest(skA, KeyType::Ed25519, kpA1.second, KeyType::Secp256k1, 2);
889 auto const sAMax = makeRevocation(skA, KeyType::Ed25519);
890
891 auto const skB = randomSecretKey();
892 auto const kpB0 = randomKeyPair(KeyType::Secp256k1);
893 auto const kpB1 = randomKeyPair(KeyType::Secp256k1);
894 auto const kpB2 = randomKeyPair(KeyType::Secp256k1);
895 auto const sB0 =
896 makeManifest(skB, KeyType::Ed25519, kpB0.second, KeyType::Secp256k1, 0);
897 auto const sB1 = makeManifest(
898 skB,
900 kpB1.second,
902 1,
903 true); // invalidSig
904 auto const sB2 = makeManifest(skB, KeyType::Ed25519, kpB2.second, KeyType::Ed25519, 2);
905
906 auto const fake = sB2.serialized + '\0';
907
908 // applyManifest should accept new manifests with
909 // higher sequence numbers
910 auto const seq0 = cache.sequence();
911 BEAST_EXPECT(
914 BEAST_EXPECT(cache.sequence() > seq0);
915
916 auto const seq1 = cache.sequence();
917 BEAST_EXPECT(
920 BEAST_EXPECT(cache.sequence() == seq1);
921
922 BEAST_EXPECT(
925 BEAST_EXPECT(
928 BEAST_EXPECT(
931
932 BEAST_EXPECT(
935
936 // applyManifest should accept manifests with max sequence numbers
937 // that revoke the master public key
938 BEAST_EXPECT(!cache.revoked(pkA));
939 BEAST_EXPECT(sAMax.revoked());
940 BEAST_EXPECT(
943 BEAST_EXPECT(
946 BEAST_EXPECT(
949 BEAST_EXPECT(
952 BEAST_EXPECT(cache.revoked(pkA));
953
954 // applyManifest should reject manifests with invalid signatures
955 BEAST_EXPECT(
958 BEAST_EXPECT(
961 BEAST_EXPECT(!deserializeManifest(fake));
962 BEAST_EXPECT(
965 BEAST_EXPECT(
968
969 auto const sC0 = makeManifest(
971 BEAST_EXPECT(
974 }
975
976 testLoadStore(cache);
978 testGetKeys();
983 }
984};
985
987
988} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Remembers manifests with the highest sequence number.
Definition Manifest.h:374
bool load(DatabaseCon &dbCon, std::string const &dbTable, std::string const &configManifest, std::vector< std::string > const &configRevocation)
Populate manifest cache with manifests in database and config.
std::optional< PublicKey > getSigningKey(PublicKey const &pk) const
Returns master key's current signing key.
ManifestDisposition applyManifest(Manifest m, ManifestRateLimitCapPolicy cap)
Add manifest to cache.
PublicKey getMasterKey(PublicKey const &pk) const
Returns ephemeral signing key's master public key.
std::uint32_t sequence() const
A monotonically increasing number used to detect new manifests.
Definition Manifest.h:443
void save(DatabaseCon &dbCon, std::string const &dbTable, std::function< bool(PublicKey const &)> const &isTrusted)
Save cached manifests to database.
bool revoked(PublicKey const &pk) const
Returns true if master key has been revoked in a manifest.
A public key.
Definition PublicKey.h:53
void addWithoutSigningFields(Serializer &s) const
Definition STObject.h:993
void add(Serializer &s) const override
Definition STObject.cpp:123
A secret key.
Definition SecretKey.h:24
Slice slice() const noexcept
Definition Serializer.h:141
std::size_t size() const noexcept
Definition Serializer.h:147
void const * data() const noexcept
Definition Serializer.h:153
static Manifest clone(Manifest const &m)
void testLoadStore(ManifestCache &m)
static PublicKey randomMasterKey()
void run() override
Runs the suite.
static std::string makeManifestString(PublicKey const &pk, SecretKey const &sk, PublicKey const &spk, SecretKey const &ssk, int seq)
static void cleanupDatabaseDir(std::filesystem::path const &dbPath)
std::string makeRevocationString(SecretKey const &sk, KeyType type, bool invalidSig=false)
Manifest makeManifest(SecretKey const &sk, KeyType type, SecretKey const &ssk, KeyType stype, int seq, bool invalidSig=false)
static PublicKey randomNode()
static std::filesystem::path getDatabasePath()
static void setupDatabaseDir(std::filesystem::path const &dbPath)
Manifest makeRevocation(SecretKey const &sk, KeyType type, bool invalidSig=false)
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
ManualTimeKeeper & timeKeeper()
Definition Env.h:313
beast::Journal const journal
Definition Env.h:204
T create_directory(T... args)
T current_path(T... args)
T equal(T... args)
T exists(T... args)
T is_directory(T... args)
T is_empty(T... args)
T make_unique(T... args)
T max(T... args)
bool equal(STAmount const &sa1, STAmount const &sa2)
void sign(json::Value const &jv, Account const &account, json::Value &sigObject, HashPrefix prefix)
Sign automatically into a specific Json field of the jv object.
Definition utility.cpp:52
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
KeyType
Definition KeyType.h:8
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Seed randomSeed()
Create a seed using secure random numbers.
Definition Seed.cpp:48
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
bool verify(PublicKey const &publicKey, Slice const &m, Slice const &sig) noexcept
Verify a signature on a message.
SField const sfGeneric
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
@ Capped
Subject to the untrusted cap (unlisted peer gossip).
Definition Manifest.h:364
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
std::string base64Encode(std::uint8_t const *data, std::size_t len)
@ Manifest
Manifest.
Definition HashPrefix.h:84
std::unique_ptr< DatabaseCon > makeTestWalletDB(DatabaseCon::Setup const &setup, std::string const &dbname, beast::Journal j)
makeTestWalletDB Opens a test wallet database with an arbitrary name.
Definition Wallet.cpp:50
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
std::optional< ValidatorToken > loadValidatorToken(std::vector< std::string > const &blob, beast::Journal journal=beast::Journal(beast::Journal::getNullSink()))
@ BadMasterKey
The master key is not acceptable to us.
Definition Manifest.h:325
@ Accepted
Manifest is valid.
Definition Manifest.h:321
@ Invalid
Timely, but invalid signature.
Definition Manifest.h:329
@ BadEphemeralKey
The ephemeral key is not acceptable to us.
Definition Manifest.h:327
@ Stale
Sequence is too old.
Definition Manifest.h:323
T push_back(T... args)
T remove(T... args)
T reserve(T... args)
T size(T... args)
T sort(T... args)
std::filesystem::path dataDir
Definition DatabaseCon.h:82
PublicKey masterKey
The master key associated with this manifest.
Definition Manifest.h:84
std::string serialized
The manifest in serialized form.
Definition Manifest.h:79
std::string domain
The domain, if one was specified in the manifest; empty otherwise.
Definition Manifest.h:102
std::optional< PublicKey > signingKey
The ephemeral key associated with this manifest.
Definition Manifest.h:92
std::uint32_t sequence
The sequence number of this manifest.
Definition Manifest.h:97
static constexpr auto kDatabasePath
Definition Constants.h:13