Truly independent web browser
LibCrypto: Wrap keys in PrivateKeyInfo and SPKI with two functions
Shannon Booth2e586a4d635f
diff --git a/Libraries/LibCrypto/CMakeLists.txt b/Libraries/LibCrypto/CMakeLists.txtindex 7222c3eea2a..b1c969bea93 100644--- a/Libraries/LibCrypto/CMakeLists.txt+++ b/Libraries/LibCrypto/CMakeLists.txt@@ -23,6 +23,7 @@ set(SOURCESPK/MLDSA.cppPK/MLKEM.cppPK/ModuleLatticePrivateKey.cpp+ PK/PK.cppPK/RSA.cpp)diff --git a/Libraries/LibCrypto/KeyHandle.cpp b/Libraries/LibCrypto/KeyHandle.cppindex 8f78ea67154..c1d5e038a38 100644--- a/Libraries/LibCrypto/KeyHandle.cpp+++ b/Libraries/LibCrypto/KeyHandle.cpp@@ -142,7 +142,7 @@ void crypto_key_encode_handle(KeyHandle const* handle, void* state, void (*steps},[&](::Crypto::PK::MLKEMPublicKey const& mlkem_key) {fields.handle_tag = to_underlying(HandleTag::MLKEMPublicKey);- add_exported(mlkem_key.public_key());+ add(mlkem_key.public_key().bytes());},[&](::Crypto::PK::MLKEMPrivateKey const& mlkem_key) {fields.handle_tag = to_underlying(HandleTag::MLKEMPrivateKey);diff --git a/Libraries/LibCrypto/PK/MLKEM.h b/Libraries/LibCrypto/PK/MLKEM.hindex 0f40219f18f..4b6b3a7e4df 100644--- a/Libraries/LibCrypto/PK/MLKEM.h+++ b/Libraries/LibCrypto/PK/MLKEM.h@@ -26,7 +26,7 @@ public:MLKEMPublicKey() = default;- ByteBuffer public_key() const { return m_public_key; }+ ByteBuffer const& public_key() const { return m_public_key; }private:ByteBuffer m_public_key;diff --git a/Libraries/LibCrypto/PK/PK.cpp b/Libraries/LibCrypto/PK/PK.cppnew file mode 100644index 00000000000..da2c45f0328--- /dev/null+++ b/Libraries/LibCrypto/PK/PK.cpp@@ -0,0 +1,49 @@+/*+ * Copyright (c) 2020, Ali Mohammad Pur <[email protected]>+ * Copyright (c) 2024, stelar7 <[email protected]>+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCrypto/ASN1/DER.h>+#include <LibCrypto/PK/PK.h>++namespace Crypto::PK {++static ErrorOr<void> write_algorithm_identifier(ASN1::Encoder& encoder, Span<u32 const> algorithm, AlgorithmParameters const& parameters)+{+ return encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {+ TRY(encoder.write(algorithm));+ TRY(parameters.visit(+ [](Empty) -> ErrorOr<void> { return {}; },+ [&](auto const& value) { return encoder.write(value); }));+ return {};+ });+}++ErrorOr<ByteBuffer> wrap_in_private_key_info(ReadonlyBytes private_key, Span<u32 const> algorithm, AlgorithmParameters parameters)+{+ ASN1::Encoder encoder;+ TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {+ TRY(encoder.write(0x00u)); // version+ TRY(write_algorithm_identifier(encoder, algorithm, parameters));+ TRY(encoder.write(private_key));+ return {};+ }));++ return encoder.finish();+}++ErrorOr<ByteBuffer> wrap_in_subject_public_key_info(ReadonlyBytes public_key, Span<u32 const> algorithm, AlgorithmParameters parameters)+{+ ASN1::Encoder encoder;+ TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {+ TRY(write_algorithm_identifier(encoder, algorithm, parameters));+ TRY(encoder.write(ASN1::BitStringView(public_key, 0)));+ return {};+ }));++ return encoder.finish();+}++}diff --git a/Libraries/LibCrypto/PK/PK.h b/Libraries/LibCrypto/PK/PK.hindex a7d1c98071c..e3c76311627 100644--- a/Libraries/LibCrypto/PK/PK.h+++ b/Libraries/LibCrypto/PK/PK.h@@ -8,213 +8,17 @@#pragma once#include <AK/ByteBuffer.h>-#include <AK/ByteString.h>-#include <LibCrypto/ASN1/DER.h>+#include <AK/Variant.h>namespace Crypto::PK {-template<class ByteBuffer>-ErrorOr<ByteBuffer> wrap_in_private_key_info(ByteBuffer key, Span<u32 const> algorithm_identifier)-{- ASN1::Encoder encoder;- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- TRY(encoder.write(0x00u)); // version+// The parameters of an AlgorithmIdentifier: absent, NULL, or a named curve.+using AlgorithmParameters = Variant<Empty, nullptr_t, Span<u32 const>>;- // AlgorithmIdentifier- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- TRY(encoder.write(algorithm_identifier)); // algorithm+// https://www.rfc-editor.org/rfc/rfc5208#section-5+ErrorOr<ByteBuffer> wrap_in_private_key_info(ReadonlyBytes private_key, Span<u32 const> algorithm, AlgorithmParameters = {});- return {};- }));-- // PrivateKey- TRY(encoder.write(key));-- return {};- }));-- return encoder.finish();-}--template<class ByteBuffer, typename Params>-ErrorOr<ByteBuffer> wrap_in_private_key_info(ByteBuffer key, Span<u32 const> algorithm_identifier, Params params)-{- 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-- TRY(encoder.write(params)); // parameters-- return {};- }));-- // PrivateKey- TRY(encoder.write(key));-- return {};- }));-- return encoder.finish();-}--template<typename ExportableKey>-ErrorOr<ByteBuffer> wrap_in_private_key_info(ExportableKey key, Span<u32 const> 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-- return {};- }));-- // PrivateKey- auto data = TRY(key.export_as_der());- TRY(encoder.write(data));-- return {};- }));-- return encoder.finish();-}--template<typename ExportableKey, typename Params>-ErrorOr<ByteBuffer> wrap_in_private_key_info(ExportableKey key, Span<u32 const> algorithm_identifier, Params params)-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-- TRY(encoder.write(params)); // parameters-- return {};- }));-- // PrivateKey- auto data = TRY(key.export_as_der());- TRY(encoder.write(data));-- return {};- }));-- return encoder.finish();-}--template<class ByteBuffer>-ErrorOr<ByteBuffer> wrap_in_subject_public_key_info(ByteBuffer key, Span<u32 const> algorithm_identifier)-{- ASN1::Encoder encoder;- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- // AlgorithmIdentifier- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- TRY(encoder.write(algorithm_identifier)); // algorithm-- return {};- }));-- // subjectPublicKey- auto bitstring = ::Crypto::ASN1::BitStringView(key, 0);- TRY(encoder.write(bitstring));-- return {};- }));-- return encoder.finish();-}--template<class ByteBuffer, typename ParamsType>-ErrorOr<ByteBuffer> wrap_in_subject_public_key_info(ByteBuffer key, Span<u32 const> algorithm_identifier, ParamsType const& params)-{- ASN1::Encoder encoder;- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- // AlgorithmIdentifier- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- TRY(encoder.write(algorithm_identifier)); // algorithm-- TRY(encoder.write(params)); // parameters-- return {};- }));-- // subjectPublicKey- auto bitstring = ::Crypto::ASN1::BitStringView(key, 0);- TRY(encoder.write(bitstring));-- return {};- }));-- return encoder.finish();-}--template<typename ExportableKey>-ErrorOr<ByteBuffer> wrap_in_subject_public_key_info(ExportableKey key, Span<u32 const> 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> {- // AlgorithmIdentifier- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- TRY(encoder.write(algorithm_identifier)); // algorithm-- return {};- }));-- // subjectPublicKey- auto data = TRY(key.export_as_der());- auto bitstring = ::Crypto::ASN1::BitStringView(data, 0);- TRY(encoder.write(bitstring));-- return {};- }));-- return encoder.finish();-}--template<typename ExportableKey, typename ParamsType>-ErrorOr<ByteBuffer> wrap_in_subject_public_key_info(ExportableKey key, Span<u32 const> algorithm_identifier, ParamsType const& params)-requires requires(ExportableKey k) {- k.export_as_der();-}-{- ASN1::Encoder encoder;- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- // AlgorithmIdentifier- TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr<void> {- TRY(encoder.write(algorithm_identifier)); // algorithm-- TRY(encoder.write(params)); // parameters-- return {};- }));-- // subjectPublicKey- auto data = TRY(key.export_as_der());- auto bitstring = ::Crypto::ASN1::BitStringView(data, 0);- TRY(encoder.write(bitstring));-- return {};- }));-- return encoder.finish();-}+// https://www.rfc-editor.org/rfc/rfc5280#section-4.1+ErrorOr<ByteBuffer> wrap_in_subject_public_key_info(ReadonlyBytes public_key, Span<u32 const> algorithm, AlgorithmParameters = {});}diff --git a/Libraries/LibCrypto/RustBindings.cpp b/Libraries/LibCrypto/RustBindings.cppindex 0f23f713e75..75dacf2650d 100644--- a/Libraries/LibCrypto/RustBindings.cpp+++ b/Libraries/LibCrypto/RustBindings.cpp@@ -241,9 +241,15 @@ KeyHandle* crypto_rsa_key_from_jwk_components(bool is_private, ReadonlySpan<Readbool crypto_rsa_export_der(KeyHandle const* key, bool is_public, void* state, ReceiveEncodedBytes receive){- auto result = is_public- ? ::Crypto::PK::wrap_in_subject_public_key_info(key->get<::Crypto::PK::RSAPublicKey>(), ::Crypto::ASN1::rsa_encryption_oid, nullptr)- : ::Crypto::PK::wrap_in_private_key_info(key->get<::Crypto::PK::RSAPrivateKey>(), ::Crypto::ASN1::rsa_encryption_oid, nullptr);+ auto result = [&]() -> ErrorOr<ByteBuffer> {+ if (is_public) {+ auto public_key = TRY(key->get<::Crypto::PK::RSAPublicKey>().export_as_der());+ return ::Crypto::PK::wrap_in_subject_public_key_info(public_key, ::Crypto::ASN1::rsa_encryption_oid, nullptr);+ }+ auto private_key = TRY(key->get<::Crypto::PK::RSAPrivateKey>().export_as_der());+ ScopeGuard const clear_private_key = [&] { secure_zero(private_key.data(), private_key.size()); };+ return ::Crypto::PK::wrap_in_private_key_info(private_key, ::Crypto::ASN1::rsa_encryption_oid, nullptr);+ }();return receive_byte_result(result, state, receive);}@@ -606,7 +612,9 @@ bool crypto_ec_export_der(EcCurve curve, KeyHandle const* key, bool is_public, vauto public_key_bytes = TRY(key->get<::Crypto::PK::ECPublicKey>().to_uncompressed());return ::Crypto::PK::wrap_in_subject_public_key_info(public_key_bytes, ::Crypto::ASN1::ec_public_key_encryption_oid, ec_params);}- return ::Crypto::PK::wrap_in_private_key_info(key->get<::Crypto::PK::ECPrivateKey>(), ::Crypto::ASN1::ec_public_key_encryption_oid, ec_params);+ auto private_key = TRY(key->get<::Crypto::PK::ECPrivateKey>().export_as_der());+ ScopeGuard const clear_private_key = [&] { secure_zero(private_key.data(), private_key.size()); };+ return ::Crypto::PK::wrap_in_private_key_info(private_key, ::Crypto::ASN1::ec_public_key_encryption_oid, ec_params);}();return receive_byte_result(result, state, receive);}@@ -761,10 +769,12 @@ bool crypto_okp_export_der(OkpCurve curve, bool is_public, ReadonlyBytes key, voauto result = [&]() -> ErrorOr<ByteBuffer> {auto oid = okp_curve_oid(curve);if (is_public)- return ::Crypto::PK::wrap_in_subject_public_key_info(TRY(ByteBuffer::copy(key)), oid);+ return ::Crypto::PK::wrap_in_subject_public_key_info(key, oid);::Crypto::ASN1::Encoder encoder;TRY(encoder.write(key));- return ::Crypto::PK::wrap_in_private_key_info(encoder.finish(), oid);+ auto private_key = encoder.finish();+ ScopeGuard const clear_private_key = [&] { secure_zero(private_key.data(), private_key.size()); };+ return ::Crypto::PK::wrap_in_private_key_info(private_key, oid);}();return receive_byte_result(result, state, receive);}@@ -853,9 +863,13 @@ void crypto_mldsa_seed_bytes(KeyHandle const* key, ReadonlyBytes* bytes)bool crypto_mldsa_export_der(KeyHandle const* key, bool is_public, ReadonlySpan<u32> oid, void* state, ReceiveEncodedBytes receive){- auto result = is_public- ? ::Crypto::PK::wrap_in_subject_public_key_info(key->get<::Crypto::PK::MLDSAPublicKey>().public_key(), oid)- : ::Crypto::PK::wrap_in_private_key_info(key->get<::Crypto::PK::MLDSAPrivateKey>(), oid);+ auto result = [&]() -> ErrorOr<ByteBuffer> {+ if (is_public)+ return ::Crypto::PK::wrap_in_subject_public_key_info(key->get<::Crypto::PK::MLDSAPublicKey>().public_key(), oid);+ auto private_key = TRY(key->get<::Crypto::PK::MLDSAPrivateKey>().export_as_der());+ ScopeGuard const clear_private_key = [&] { secure_zero(private_key.data(), private_key.size()); };+ return ::Crypto::PK::wrap_in_private_key_info(private_key, oid);+ }();return receive_byte_result(result, state, receive);}@@ -896,8 +910,7 @@ KeyHandle* crypto_mlkem_read_public_key(ReadonlyBytes bytes)void crypto_mlkem_public_bytes(KeyHandle const* key, void* state, ReceiveEncodedBytes receive){- auto public_key = key->get<::Crypto::PK::MLKEMPublicKey>().public_key();- receive(state, public_key);+ receive(state, key->get<::Crypto::PK::MLKEMPublicKey>().public_key());}void crypto_mlkem_seed_bytes(KeyHandle const* key, ReadonlyBytes* bytes)@@ -907,9 +920,13 @@ void crypto_mlkem_seed_bytes(KeyHandle const* key, ReadonlyBytes* bytes)bool crypto_mlkem_export_der(KeyHandle const* key, bool is_public, ReadonlySpan<u32> oid, void* state, ReceiveEncodedBytes receive){- auto result = is_public- ? ::Crypto::PK::wrap_in_subject_public_key_info(key->get<::Crypto::PK::MLKEMPublicKey>().public_key(), oid)- : ::Crypto::PK::wrap_in_private_key_info(key->get<::Crypto::PK::MLKEMPrivateKey>(), oid);+ auto result = [&]() -> ErrorOr<ByteBuffer> {+ if (is_public)+ return ::Crypto::PK::wrap_in_subject_public_key_info(key->get<::Crypto::PK::MLKEMPublicKey>().public_key(), oid);+ auto private_key = TRY(key->get<::Crypto::PK::MLKEMPrivateKey>().export_as_der());+ ScopeGuard const clear_private_key = [&] { secure_zero(private_key.data(), private_key.size()); };+ return ::Crypto::PK::wrap_in_private_key_info(private_key, oid);+ }();return receive_byte_result(result, state, receive);}