LibWeb+LibCrypto: Remove OID constants scattered around

Now that `Certificate` has been moved, the OID constants are easily
reachable in `LibCrypto`.
This commit is contained in:
devgianlu 2024-11-24 22:03:11 +01:00 committed by Andreas Kling
parent 49c388b891
commit 506e490793
Notes: github-actions[bot] 2024-11-25 13:12:06 +00:00
2 changed files with 6 additions and 11 deletions

View file

@ -10,12 +10,11 @@
#include <LibCrypto/ASN1/ASN1.h> #include <LibCrypto/ASN1/ASN1.h>
#include <LibCrypto/ASN1/DER.h> #include <LibCrypto/ASN1/DER.h>
#include <LibCrypto/ASN1/PEM.h> #include <LibCrypto/ASN1/PEM.h>
#include <LibCrypto/Certificate/Certificate.h>
#include <LibCrypto/PK/RSA.h> #include <LibCrypto/PK/RSA.h>
namespace Crypto::PK { namespace Crypto::PK {
static constexpr Array<int, 7> pkcs8_rsa_key_oid { 1, 2, 840, 113549, 1, 1, 1 };
RSA::KeyPairType RSA::parse_rsa_key(ReadonlyBytes der) RSA::KeyPairType RSA::parse_rsa_key(ReadonlyBytes der)
{ {
// we are going to assign to at least one of these // we are going to assign to at least one of these
@ -96,7 +95,7 @@ RSA::KeyPairType RSA::parse_rsa_key(ReadonlyBytes der)
auto oid = oid_result.release_value(); auto oid = oid_result.release_value();
// Now let's check that the OID matches "RSA key" // Now let's check that the OID matches "RSA key"
if (oid != pkcs8_rsa_key_oid) { if (oid != Crypto::Certificate::rsa_encryption_oid) {
// Oh well. not an RSA key at all. // Oh well. not an RSA key at all.
dbgln_if(RSA_PARSE_DEBUG, "RSA PKCS#8 public key parse failed: Not an RSA key"); dbgln_if(RSA_PARSE_DEBUG, "RSA PKCS#8 public key parse failed: Not an RSA key");
return false; return false;

View file

@ -1110,8 +1110,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::Object>> RSAOAEP::export_key(Bindings::KeyFormat
// that represents the RSA public key represented by the [[handle]] internal slot of key // that represents the RSA public key represented by the [[handle]] internal slot of key
auto maybe_data = handle.visit( auto maybe_data = handle.visit(
[&](::Crypto::PK::RSAPublicKey<> const& public_key) -> ErrorOr<ByteBuffer> { [&](::Crypto::PK::RSAPublicKey<> const& public_key) -> ErrorOr<ByteBuffer> {
auto rsa_encryption_oid = Array<int, 7> { 1, 2, 840, 113549, 1, 1, 1 }; return TRY(::Crypto::PK::wrap_in_subject_public_key_info(public_key, Array { ::Crypto::Certificate::rsa_encryption_oid }));
return TRY(::Crypto::PK::wrap_in_subject_public_key_info(public_key, rsa_encryption_oid));
}, },
[](auto) -> ErrorOr<ByteBuffer> { [](auto) -> ErrorOr<ByteBuffer> {
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
@ -1138,8 +1137,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::Object>> RSAOAEP::export_key(Bindings::KeyFormat
// that represents the RSA private key represented by the [[handle]] internal slot of key // that represents the RSA private key represented by the [[handle]] internal slot of key
auto maybe_data = handle.visit( auto maybe_data = handle.visit(
[&](::Crypto::PK::RSAPrivateKey<> const& private_key) -> ErrorOr<ByteBuffer> { [&](::Crypto::PK::RSAPrivateKey<> const& private_key) -> ErrorOr<ByteBuffer> {
auto rsa_encryption_oid = Array<int, 7> { 1, 2, 840, 113549, 1, 1, 1 }; return TRY(::Crypto::PK::wrap_in_private_key_info(private_key, Array { ::Crypto::Certificate::rsa_encryption_oid }));
return TRY(::Crypto::PK::wrap_in_private_key_info(private_key, rsa_encryption_oid));
}, },
[](auto) -> ErrorOr<ByteBuffer> { [](auto) -> ErrorOr<ByteBuffer> {
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
@ -3659,8 +3657,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::Object>> X25519::export_key(Bindings::KeyFormat
// Set the algorithm object identifier to the id-X25519 OID defined in [RFC8410]. // Set the algorithm object identifier to the id-X25519 OID defined in [RFC8410].
// Set the subjectPublicKey field to keyData. // Set the subjectPublicKey field to keyData.
auto public_key = handle.get<ByteBuffer>(); auto public_key = handle.get<ByteBuffer>();
auto x25519_oid = Array<int, 7> { 1, 3, 101, 110 }; auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_subject_public_key_info(public_key, Array { ::Crypto::Certificate::x25519_oid }));
auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_subject_public_key_info(public_key, x25519_oid));
// 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data. // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
result = JS::ArrayBuffer::create(m_realm, data); result = JS::ArrayBuffer::create(m_realm, data);
@ -3679,8 +3676,7 @@ WebIDL::ExceptionOr<GC::Ref<JS::Object>> X25519::export_key(Bindings::KeyFormat
// Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type, as defined in Section 7 of [RFC8410], // Set the privateKey field to the result of DER-encoding a CurvePrivateKey ASN.1 type, as defined in Section 7 of [RFC8410],
// that represents the X25519 private key represented by the [[handle]] internal slot of key // that represents the X25519 private key represented by the [[handle]] internal slot of key
auto private_key = handle.get<ByteBuffer>(); auto private_key = handle.get<ByteBuffer>();
auto x25519_oid = Array<int, 7> { 1, 3, 101, 110 }; auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_private_key_info(private_key, Array { ::Crypto::Certificate::x25519_oid }));
auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_private_key_info(private_key, x25519_oid));
// 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data. // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data.
result = JS::ArrayBuffer::create(m_realm, data); result = JS::ArrayBuffer::create(m_realm, data);