mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
LibCrypto: Add roundtrip test for parsing RSA private keys
This commit is contained in:
parent
1be411cc73
commit
0359e8848a
Notes:
sideshowbarker
2024-07-16 23:23:26 +09:00
Author: https://github.com/stelar7 Commit: https://github.com/SerenityOS/serenity/commit/0359e8848a Pull-request: https://github.com/SerenityOS/serenity/pull/23674 Issue: https://github.com/SerenityOS/serenity/issues/23657 Reviewed-by: https://github.com/ADKaster ✅
2 changed files with 38 additions and 2 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <LibCrypto/ASN1/PEM.h>
|
#include <LibCrypto/ASN1/PEM.h>
|
||||||
#include <LibCrypto/Hash/SHA2.h>
|
#include <LibCrypto/Hash/SHA2.h>
|
||||||
|
#include <LibCrypto/PK/PK.h>
|
||||||
#include <LibCrypto/PK/RSA.h>
|
#include <LibCrypto/PK/RSA.h>
|
||||||
#include <LibTest/TestCase.h>
|
#include <LibTest/TestCase.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
@ -119,14 +120,19 @@ sV/ETwIDAQABAkBpC37UJkjWQRHyxP83xuasExuO6/mT5sQN692kcppTJ9wHNWoD
|
||||||
RwIhAIDSm8Ajgf7m3RQEoLVrCe/l8WtCqsuWliOsr6rbQq4hAiEAx8R16wvOtZlN
|
RwIhAIDSm8Ajgf7m3RQEoLVrCe/l8WtCqsuWliOsr6rbQq4hAiEAx8R16wvOtZlN
|
||||||
W4jvSU1+WwAaBZl21lfKf8OhLRXrmNkCIG9IRdcSiNR/Ut8QfD3N9Bb1HsUm+Bvz
|
W4jvSU1+WwAaBZl21lfKf8OhLRXrmNkCIG9IRdcSiNR/Ut8QfD3N9Bb1HsUm+Bvz
|
||||||
c8yGzl89pYST
|
c8yGzl89pYST
|
||||||
-----END PRIVATE KEY-----)"sv;
|
-----END PRIVATE KEY-----
|
||||||
|
)"sv;
|
||||||
auto decoded = Crypto::decode_pem(keypem.bytes());
|
auto decoded = Crypto::decode_pem(keypem.bytes());
|
||||||
auto keypair = Crypto::PK::RSA::parse_rsa_key(decoded);
|
auto keypair = Crypto::PK::RSA::parse_rsa_key(decoded);
|
||||||
auto priv_der = MUST(keypair.private_key.export_as_der());
|
auto priv_der = MUST(keypair.private_key.export_as_der());
|
||||||
auto priv_pem = MUST(Crypto::encode_pem(priv_der, Crypto::PEMType::PrivateKey));
|
auto rsa_encryption_oid = Array<int, 7> { 1, 2, 840, 113549, 1, 1, 1 };
|
||||||
|
auto wrapped_priv_der = MUST(Crypto::PK::wrap_in_private_key_info(keypair.private_key, rsa_encryption_oid));
|
||||||
|
auto priv_pem = MUST(Crypto::encode_pem(wrapped_priv_der, Crypto::PEMType::PrivateKey));
|
||||||
auto rsa_from_pair = Crypto::PK::RSA(keypair.public_key, keypair.private_key);
|
auto rsa_from_pair = Crypto::PK::RSA(keypair.public_key, keypair.private_key);
|
||||||
auto rsa_from_pem = Crypto::PK::RSA(priv_pem);
|
auto rsa_from_pem = Crypto::PK::RSA(priv_pem);
|
||||||
|
|
||||||
|
EXPECT_EQ(keypem, StringView(priv_pem));
|
||||||
|
|
||||||
u8 enc_buffer[rsa_from_pair.output_size()];
|
u8 enc_buffer[rsa_from_pair.output_size()];
|
||||||
u8 dec_buffer[rsa_from_pair.output_size()];
|
u8 dec_buffer[rsa_from_pair.output_size()];
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,36 @@
|
||||||
|
|
||||||
namespace Crypto::PK {
|
namespace Crypto::PK {
|
||||||
|
|
||||||
|
template<typename ExportableKey>
|
||||||
|
ErrorOr<ByteBuffer> wrap_in_private_key_info(ExportableKey key, Span<int> algorithm_identifier)
|
||||||
|
requires requires(ExportableKey k) {
|
||||||
|
k.export_as_der();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ASN1::Encoder encoder;
|
||||||
|
TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {
|
||||||
|
TRY(encoder.write(0x00u)); // version
|
||||||
|
|
||||||
|
// AlgorithmIdentifier
|
||||||
|
TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {
|
||||||
|
TRY(encoder.write(algorithm_identifier)); // algorithm
|
||||||
|
|
||||||
|
// FIXME: This assumes we have a NULL parameter, this is not always the case
|
||||||
|
TRY(encoder.write(nullptr)); // parameters
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}));
|
||||||
|
|
||||||
|
// PrivateKey
|
||||||
|
auto data = TRY(key.export_as_der());
|
||||||
|
TRY(encoder.write(data));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}));
|
||||||
|
|
||||||
|
return encoder.finish();
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: Fixing name up for grabs
|
// FIXME: Fixing name up for grabs
|
||||||
template<typename PrivKeyT, typename PubKeyT>
|
template<typename PrivKeyT, typename PubKeyT>
|
||||||
class PKSystem {
|
class PKSystem {
|
||||||
|
|
Loading…
Reference in a new issue