Add exactly one object to context of writer. Object shall contain JWK-encoded public or private key
| 660 | |
| 661 | // Add exactly one object to context of writer. Object shall contain JWK-encoded public or private key |
| 662 | bool encodeKey(rapidjson::Writer<rapidjson::StringBuffer>& writer, StringRef keyName, const PublicOrPrivateKey& key) { |
| 663 | auto const isPublic = key.isPublic(); |
| 664 | auto pKey = std::add_pointer_t<EVP_PKEY>(); |
| 665 | auto alg = PKeyAlgorithm{}; |
| 666 | if (isPublic) { |
| 667 | auto const& keyObj = key.getPublic(); |
| 668 | pKey = keyObj.nativeHandle(); |
| 669 | alg = keyObj.algorithm(); |
| 670 | } else { |
| 671 | auto const& keyObj = key.getPrivate(); |
| 672 | pKey = key.getPrivate().nativeHandle(); |
| 673 | alg = keyObj.algorithm(); |
| 674 | } |
| 675 | if (!pKey) { |
| 676 | JWK_WRITE_ERROR("PKey object to encode is null"); |
| 677 | return false; |
| 678 | } |
| 679 | if (alg == PKeyAlgorithm::EC) { |
| 680 | return encodeEcKey(writer, keyName, pKey, isPublic); |
| 681 | } else if (alg == PKeyAlgorithm::RSA) { |
| 682 | return encodeRsaKey(writer, keyName, pKey, isPublic); |
| 683 | } else { |
| 684 | JWK_WRITE_ERROR("Attempted to encode PKey with unsupported algorithm"); |
| 685 | return false; |
| 686 | } |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | void testPublicKey(PrivateKey (*factory)()) { |
| 691 | // stringify-deserialize public key. |
no test coverage detected