| 688 | } |
| 689 | |
| 690 | void testPublicKey(PrivateKey (*factory)()) { |
| 691 | // stringify-deserialize public key. |
| 692 | // sign some data using private key to see whether deserialized public key can verify it. |
| 693 | auto& rng = *deterministicRandom(); |
| 694 | auto pubKeyName = Standalone<StringRef>("somePublicKey"_sr); |
| 695 | auto privKey = factory(); |
| 696 | auto pubKey = privKey.toPublic(); |
| 697 | auto jwks = JsonWebKeySet{}; |
| 698 | jwks.keys.emplace(pubKeyName, pubKey); |
| 699 | auto arena = Arena(); |
| 700 | auto jwksStr = jwks.toStringRef(arena).get(); |
| 701 | fmt::print("Test JWKS: {}\n", jwksStr.toString()); |
| 702 | auto jwksClone = JsonWebKeySet::parse(jwksStr, {}); |
| 703 | ASSERT(jwksClone.present()); |
| 704 | auto pubKeyClone = jwksClone.get().keys[pubKeyName].getPublic(); |
| 705 | auto randByteStr = [&rng, &arena](int len) { |
| 706 | auto buf = new (arena) uint8_t[len]; |
| 707 | for (auto i = 0; i < len; i++) |
| 708 | buf[i] = rng.randomUInt32() % 255u; |
| 709 | return StringRef(buf, len); |
| 710 | }; |
| 711 | auto randData = randByteStr(rng.randomUInt32() % 128 + 16); |
| 712 | auto signature = privKey.sign(arena, randData, *::EVP_sha256()); |
| 713 | ASSERT(pubKeyClone.verify(randData, signature, *::EVP_sha256())); |
| 714 | const_cast<uint8_t&>(*randData.begin())++; |
| 715 | ASSERT(!pubKeyClone.verify(randData, signature, *::EVP_sha256())); |
| 716 | fmt::print("TESTED OK FOR OPENSSL V{} API\n", (OPENSSL_VERSION_NUMBER >> 28)); |
| 717 | } |
| 718 | |
| 719 | void testPrivateKey(PrivateKey (*factory)()) { |
| 720 | // stringify-deserialize private key. |
no test coverage detected