mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-03 13:02:09 +02:00
LibWeb: Support the exporting of ml-kem keys in pkcs8 format
This commit is contained in:
Notes:
github-actions[bot]
2026-01-06 00:07:25 +00:00
Author: https://github.com/tete17 Commit: https://github.com/LadybirdBrowser/ladybird/commit/a739311fdcc Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/7320 Reviewed-by: https://github.com/shannonbooth ✅
@@ -9340,7 +9340,59 @@ WebIDL::ExceptionOr<GC::Ref<JS::Object>> MLKEM::export_key(Bindings::KeyFormat f
|
||||
// 3. Let result be the result of DER-encoding data.
|
||||
result = JS::ArrayBuffer::create(m_realm, data);
|
||||
}
|
||||
// FIXME: -> If format is "pkcs8":
|
||||
// -> If format is "pkcs8":
|
||||
else if (format == Bindings::KeyFormat::Pkcs8) {
|
||||
// 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
|
||||
if (key->type() != Bindings::KeyType::Private)
|
||||
return WebIDL::InvalidAccessError::create(m_realm, "Key is not a private key"_utf16);
|
||||
|
||||
// 2. Let data be an instance of the PrivateKeyInfo ASN.1 structure defined in [RFC5208] with the following properties:
|
||||
// * Set the version field to 0.
|
||||
// * Set the privateKeyAlgorithm field to a PrivateKeyAlgorithmIdentifier ASN.1 type with the following properties:
|
||||
// * => If the name member of normalizedAlgorithm is "ML-KEM-512":
|
||||
// Set the algorithm object identifier to the id-alg-ml-kem-512 (2.16.840.1.101.3.4.4.1) OID.
|
||||
// * => If the name member of normalizedAlgorithm is "ML-KEM-768":
|
||||
// Set the algorithm object identifier to the id-alg-ml-kem-768 (2.16.840.1.101.3.4.4.2) OID.
|
||||
// * => If the name member of normalizedAlgorithm is "ML-KEM-1024":
|
||||
// Set the algorithm object identifier to the id-alg-ml-kem-1024 (2.16.840.1.101.3.4.4.3) OID.
|
||||
// * => Otherwise:
|
||||
// throw a NotSupportedError.
|
||||
// * Set the privateKey field as follows:
|
||||
// * => If the name member of normalizedAlgorithm is "ML-KEM-512":
|
||||
// Set the privateKey field to the result of DER-encoding a ML-KEM-512-PrivateKey
|
||||
// ASN.1 type that represents the ML-KEM private key seed represented by the
|
||||
// [[handle]] internal slot of key using the seed-only format (using a context-
|
||||
// specific [0] primitive tag with an implicit encoding of OCTET STRING).
|
||||
// * => If the name member of normalizedAlgorithm is "ML-KEM-768":
|
||||
// Set the privateKey field to the result of DER-encoding a ML-KEM-65-PrivateKey
|
||||
// ASN.1 type that represents the ML-KEM private key seed represented by the
|
||||
// [[handle]] internal slot of key using the seed-only format (using a context-
|
||||
// specific [0] primitive tag with an implicit encoding of OCTET STRING).
|
||||
// * => If the name member of normalizedAlgorithm is "ML-KEM-1024":
|
||||
// Set the privateKey field to the result of DER-encoding a ML-KEM-1024-PrivateKey
|
||||
// ASN.1 type that represents the ML-KEM private key seed represented by the
|
||||
// [[handle]] internal slot of key using the seed-only format (using a context-
|
||||
// specific [0] primitive tag with an implicit encoding of OCTET STRING).
|
||||
// * => Otherwise:
|
||||
// throw a NotSupportedError.
|
||||
Array<int, 9> algorithm_oid {};
|
||||
if (key->algorithm_name() == "ML-KEM-512") {
|
||||
algorithm_oid = ::Crypto::ASN1::ml_kem_512_oid;
|
||||
} else if (key->algorithm_name() == "ML-KEM-768") {
|
||||
algorithm_oid = ::Crypto::ASN1::ml_kem_768_oid;
|
||||
} else if (key->algorithm_name() == "ML-KEM-1024") {
|
||||
algorithm_oid = ::Crypto::ASN1::ml_kem_1024_oid;
|
||||
} else {
|
||||
return WebIDL::NotSupportedError::create(m_realm, "Invalid algorithm"_utf16);
|
||||
}
|
||||
|
||||
::Crypto::ASN1::Encoder encoder;
|
||||
VERIFY(handle.has<::Crypto::PK::MLKEMPrivateKey>());
|
||||
auto const data = TRY_OR_THROW_OOM(m_realm->vm(), ::Crypto::PK::wrap_in_private_key_info(handle.get<::Crypto::PK::MLKEMPrivateKey>(), algorithm_oid));
|
||||
|
||||
// 3. Let result be the result of DER-encoding data.
|
||||
result = JS::ArrayBuffer::create(m_realm, data);
|
||||
}
|
||||
// -> If format is "raw-public":
|
||||
else if (format == Bindings::KeyFormat::RawPublic) {
|
||||
// 1. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.
|
||||
|
||||
Reference in New Issue
Block a user