1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:37:43 +00:00

LibCrypto: Change the signatures of RSA::import_[XXX]_key to use Span.

This commit is contained in:
asynts 2020-07-27 15:28:38 +02:00 committed by Andreas Kling
parent 3de4e08b46
commit ed327e7feb
2 changed files with 21 additions and 13 deletions

View file

@ -163,11 +163,15 @@ void RSA::verify(const ByteBuffer& in, ByteBuffer& out)
out = out.slice(out.size() - size, size); out = out.slice(out.size() - size, size);
} }
void RSA::import_private_key(const ByteBuffer& buffer, bool pem) void RSA::import_private_key(ReadonlyBytes bytes, bool pem)
{ {
// so gods help me, I hate DER ByteBuffer buffer;
auto decoded_buffer = pem ? decode_pem(buffer.span()) : buffer; if (pem) {
auto key = parse_rsa_key(decoded_buffer.span()); buffer = decode_pem(bytes);
bytes = buffer.span();
}
auto key = parse_rsa_key(bytes);
if (!key.private_key.length()) { if (!key.private_key.length()) {
dbg() << "We expected to see a private key, but we found none"; dbg() << "We expected to see a private key, but we found none";
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
@ -175,11 +179,15 @@ void RSA::import_private_key(const ByteBuffer& buffer, bool pem)
m_private_key = key.private_key; m_private_key = key.private_key;
} }
void RSA::import_public_key(const ByteBuffer& buffer, bool pem) void RSA::import_public_key(ReadonlyBytes bytes, bool pem)
{ {
// so gods help me, I hate DER ByteBuffer buffer;
auto decoded_buffer = pem ? decode_pem(buffer.span()) : buffer; if (pem) {
auto key = parse_rsa_key(decoded_buffer.span()); buffer = decode_pem(bytes);
bytes = buffer.span();
}
auto key = parse_rsa_key(bytes);
if (!key.public_key.length()) { if (!key.public_key.length()) {
dbg() << "We expected to see a public key, but we found none"; dbg() << "We expected to see a public key, but we found none";
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();

View file

@ -160,13 +160,13 @@ public:
RSA(const ByteBuffer& publicKeyPEM, const ByteBuffer& privateKeyPEM) RSA(const ByteBuffer& publicKeyPEM, const ByteBuffer& privateKeyPEM)
{ {
import_public_key(publicKeyPEM); import_public_key(publicKeyPEM.span());
import_private_key(privateKeyPEM); import_private_key(privateKeyPEM.span());
} }
RSA(const StringView& privKeyPEM) RSA(const StringView& privKeyPEM)
{ {
import_private_key(ByteBuffer::wrap(privKeyPEM.characters_without_null_termination(), privKeyPEM.length())); import_private_key(privKeyPEM.bytes());
m_public_key.set(m_private_key.modulus(), m_private_key.public_exponent()); m_public_key.set(m_private_key.modulus(), m_private_key.public_exponent());
} }
@ -188,8 +188,8 @@ public:
virtual size_t output_size() const override { return m_public_key.length(); } virtual size_t output_size() const override { return m_public_key.length(); }
void import_public_key(const ByteBuffer& buffer, bool pem = true); void import_public_key(ReadonlyBytes, bool pem = true);
void import_private_key(const ByteBuffer& buffer, bool pem = true); void import_private_key(ReadonlyBytes, bool pem = true);
const PrivateKeyType& private_key() const { return m_private_key; } const PrivateKeyType& private_key() const { return m_private_key; }
const PublicKeyType& public_key() const { return m_public_key; } const PublicKeyType& public_key() const { return m_public_key; }