diff --git a/Libraries/LibCrypto/PK/PK.h b/Libraries/LibCrypto/PK/PK.h index 00669a7cdd3..2d527d38de9 100644 --- a/Libraries/LibCrypto/PK/PK.h +++ b/Libraries/LibCrypto/PK/PK.h @@ -13,8 +13,8 @@ namespace Crypto::PK { -template -ErrorOr wrap_in_private_key_info(ByteBuffer key, Span algorithm_identifier) +template +ErrorOr wrap_in_private_key_info(ByteBuffer key, Span algorithm_identifier, Params params) { ASN1::Encoder encoder; TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr { @@ -24,8 +24,7 @@ ErrorOr wrap_in_private_key_info(ByteBuffer key, Span algorithm TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr { 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 + TRY(encoder.write(params)); // parameters return {}; })); @@ -39,8 +38,8 @@ ErrorOr wrap_in_private_key_info(ByteBuffer key, Span algorithm return encoder.finish(); } -template -ErrorOr wrap_in_private_key_info(ExportableKey key, Span algorithm_identifier) +template +ErrorOr wrap_in_private_key_info(ExportableKey key, Span algorithm_identifier, Params params) requires requires(ExportableKey k) { k.export_as_der(); } @@ -53,8 +52,7 @@ requires requires(ExportableKey k) { TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr { 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 + TRY(encoder.write(params)); // parameters return {}; })); @@ -69,8 +67,8 @@ requires requires(ExportableKey k) { return encoder.finish(); } -template -ErrorOr wrap_in_subject_public_key_info(ByteBuffer key, Span algorithm_identifier) +template +ErrorOr wrap_in_subject_public_key_info(ByteBuffer key, Span algorithm_identifier, ParamsType const& params) { ASN1::Encoder encoder; TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr { @@ -78,8 +76,7 @@ ErrorOr wrap_in_subject_public_key_info(ByteBuffer key, Span al TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr { 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 + TRY(encoder.write(params)); // parameters return {}; })); @@ -94,8 +91,8 @@ ErrorOr wrap_in_subject_public_key_info(ByteBuffer key, Span al return encoder.finish(); } -template -ErrorOr wrap_in_subject_public_key_info(ExportableKey key, Span algorithm_identifier) +template +ErrorOr wrap_in_subject_public_key_info(ExportableKey key, Span algorithm_identifier, ParamsType const& params) requires requires(ExportableKey k) { k.export_as_der(); } @@ -106,8 +103,7 @@ requires requires(ExportableKey k) { TRY(encoder.write_constructed(ASN1::Class::Universal, ASN1::Kind::Sequence, [&]() -> ErrorOr { 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 + TRY(encoder.write(params)); // parameters return {}; })); diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp index 38628a3d5cd..fb3c22fafdc 100644 --- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp +++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp @@ -1112,7 +1112,7 @@ WebIDL::ExceptionOr> RSAOAEP::export_key(Bindings::KeyFormat // that represents the RSA public key represented by the [[handle]] internal slot of key auto maybe_data = handle.visit( [&](::Crypto::PK::RSAPublicKey<> const& public_key) -> ErrorOr { - 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, Array { ::Crypto::Certificate::rsa_encryption_oid }, nullptr)); }, [](auto) -> ErrorOr { VERIFY_NOT_REACHED(); @@ -1139,7 +1139,7 @@ WebIDL::ExceptionOr> RSAOAEP::export_key(Bindings::KeyFormat // that represents the RSA private key represented by the [[handle]] internal slot of key auto maybe_data = handle.visit( [&](::Crypto::PK::RSAPrivateKey<> const& private_key) -> ErrorOr { - 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, Array { ::Crypto::Certificate::rsa_encryption_oid }, nullptr)); }, [](auto) -> ErrorOr { VERIFY_NOT_REACHED(); @@ -2971,7 +2971,7 @@ WebIDL::ExceptionOr> ED25519::export_key(Bindings::KeyFormat // * Set the algorithm object identifier to the id-Ed25519 OID defined in [RFC8410]. // * Set the subjectPublicKey field to keyData. auto ed25519_oid = ::Crypto::Certificate::ed25519_oid; - auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_subject_public_key_info(key_data, ed25519_oid)); + auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_subject_public_key_info(key_data, ed25519_oid, nullptr)); // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data. return JS::ArrayBuffer::create(m_realm, move(data)); @@ -2990,7 +2990,7 @@ WebIDL::ExceptionOr> ED25519::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], that represents the Ed25519 private key represented by the [[handle]] internal slot of key auto ed25519_oid = ::Crypto::Certificate::ed25519_oid; - auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_private_key_info(key_data, ed25519_oid)); + auto data = TRY_OR_THROW_OOM(vm, ::Crypto::PK::wrap_in_private_key_info(key_data, ed25519_oid, nullptr)); // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data. return JS::ArrayBuffer::create(m_realm, move(data)); @@ -3659,7 +3659,7 @@ WebIDL::ExceptionOr> X25519::export_key(Bindings::KeyFormat // Set the algorithm object identifier to the id-X25519 OID defined in [RFC8410]. // Set the subjectPublicKey field to keyData. auto public_key = handle.get(); - 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, Array { ::Crypto::Certificate::x25519_oid }, nullptr)); // 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); @@ -3678,7 +3678,7 @@ WebIDL::ExceptionOr> 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], // that represents the X25519 private key represented by the [[handle]] internal slot of key auto private_key = handle.get(); - 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, Array { ::Crypto::Certificate::x25519_oid }, nullptr)); // 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); @@ -3903,7 +3903,7 @@ WebIDL::ExceptionOr> X448::export_key(Bindings::KeyFormat fo // * Set the algorithm object identifier to the id-X448 OID defined in [RFC8410]. // * Set the subjectPublicKey field to keyData. auto x448_oid = ::Crypto::Certificate::x448_oid; - auto data = TRY_OR_THROW_OOM(m_realm->vm(), ::Crypto::PK::wrap_in_subject_public_key_info(key_data, x448_oid)); + auto data = TRY_OR_THROW_OOM(m_realm->vm(), ::Crypto::PK::wrap_in_subject_public_key_info(key_data, x448_oid, nullptr)); // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data. return JS::ArrayBuffer::create(m_realm, data); @@ -3921,7 +3921,7 @@ WebIDL::ExceptionOr> X448::export_key(Bindings::KeyFormat fo // * Set the algorithm object identifier to the id-X448 OID defined in [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 X448 private key represented by the [[handle]] internal slot of key auto x448_oid = ::Crypto::Certificate::x448_oid; - auto data = TRY_OR_THROW_OOM(m_realm->vm(), ::Crypto::PK::wrap_in_private_key_info(key_data, x448_oid)); + auto data = TRY_OR_THROW_OOM(m_realm->vm(), ::Crypto::PK::wrap_in_private_key_info(key_data, x448_oid, nullptr)); // 3. Let result be a new ArrayBuffer associated with the relevant global object of this [HTML], and containing data. return JS::ArrayBuffer::create(m_realm, data); diff --git a/Tests/LibCrypto/TestRSA.cpp b/Tests/LibCrypto/TestRSA.cpp index de438ceeb51..aca6ef126b7 100644 --- a/Tests/LibCrypto/TestRSA.cpp +++ b/Tests/LibCrypto/TestRSA.cpp @@ -126,7 +126,7 @@ c8yGzl89pYST auto keypair = Crypto::PK::RSA::parse_rsa_key(decoded); auto priv_der = MUST(keypair.private_key.export_as_der()); auto rsa_encryption_oid = Array { 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 wrapped_priv_der = MUST(Crypto::PK::wrap_in_private_key_info(keypair.private_key, rsa_encryption_oid, nullptr)); 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_pem = Crypto::PK::RSA(priv_pem);