CryptoAlgorithms.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/EnumBits.h>
  8. #include <AK/String.h>
  9. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/GCPtr.h>
  12. #include <LibWeb/Bindings/SubtleCryptoPrototype.h>
  13. #include <LibWeb/Crypto/CryptoBindings.h>
  14. #include <LibWeb/Crypto/CryptoKey.h>
  15. #include <LibWeb/WebIDL/Buffers.h>
  16. #include <LibWeb/WebIDL/ExceptionOr.h>
  17. namespace Web::Crypto {
  18. using AlgorithmIdentifier = Variant<JS::Handle<JS::Object>, String>;
  19. using HashAlgorithmIdentifier = AlgorithmIdentifier;
  20. using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, Bindings::JsonWebKey>;
  21. // https://w3c.github.io/webcrypto/#algorithm-overview
  22. struct AlgorithmParams {
  23. virtual ~AlgorithmParams();
  24. explicit AlgorithmParams(String name)
  25. : name(move(name))
  26. {
  27. }
  28. String name;
  29. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  30. };
  31. // https://w3c.github.io/webcrypto/#pbkdf2-params
  32. struct PBKDF2Params : public AlgorithmParams {
  33. virtual ~PBKDF2Params() override;
  34. PBKDF2Params(String name, JS::Handle<WebIDL::BufferSource> salt, u32 iterations, HashAlgorithmIdentifier hash)
  35. : AlgorithmParams(move(name))
  36. , salt(move(salt))
  37. , iterations(iterations)
  38. , hash(move(hash))
  39. {
  40. }
  41. JS::Handle<WebIDL::BufferSource> salt;
  42. u32 iterations;
  43. HashAlgorithmIdentifier hash;
  44. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  45. };
  46. // https://w3c.github.io/webcrypto/#dfn-RsaKeyGenParams
  47. struct RsaKeyGenParams : public AlgorithmParams {
  48. virtual ~RsaKeyGenParams() override;
  49. RsaKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent)
  50. : AlgorithmParams(move(name))
  51. , modulus_length(modulus_length)
  52. , public_exponent(move(public_exponent))
  53. {
  54. }
  55. u32 modulus_length;
  56. // NOTE that the raw data is going to be in Big Endian u8[] format
  57. ::Crypto::UnsignedBigInteger public_exponent;
  58. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  59. };
  60. // https://w3c.github.io/webcrypto/#dfn-RsaHashedKeyGenParams
  61. struct RsaHashedKeyGenParams : public RsaKeyGenParams {
  62. virtual ~RsaHashedKeyGenParams() override;
  63. RsaHashedKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent, HashAlgorithmIdentifier hash)
  64. : RsaKeyGenParams(move(name), modulus_length, move(public_exponent))
  65. , hash(move(hash))
  66. {
  67. }
  68. HashAlgorithmIdentifier hash;
  69. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  70. };
  71. class AlgorithmMethods {
  72. public:
  73. virtual ~AlgorithmMethods();
  74. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&)
  75. {
  76. return WebIDL::NotSupportedError::create(m_realm, "digest is not supported"_fly_string);
  77. }
  78. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&)
  79. {
  80. return WebIDL::NotSupportedError::create(m_realm, "importKey is not supported"_fly_string);
  81. }
  82. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&)
  83. {
  84. return WebIDL::NotSupportedError::create(m_realm, "generateKey is not supported"_fly_string);
  85. }
  86. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }
  87. protected:
  88. explicit AlgorithmMethods(JS::Realm& realm)
  89. : m_realm(realm)
  90. {
  91. }
  92. JS::Realm& m_realm;
  93. };
  94. class RSAOAEP : public AlgorithmMethods {
  95. public:
  96. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  97. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new RSAOAEP(realm)); }
  98. private:
  99. explicit RSAOAEP(JS::Realm& realm)
  100. : AlgorithmMethods(realm)
  101. {
  102. }
  103. };
  104. class PBKDF2 : public AlgorithmMethods {
  105. public:
  106. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  107. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new PBKDF2(realm)); }
  108. private:
  109. explicit PBKDF2(JS::Realm& realm)
  110. : AlgorithmMethods(realm)
  111. {
  112. }
  113. };
  114. class SHA : public AlgorithmMethods {
  115. public:
  116. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&) override;
  117. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new SHA(realm)); }
  118. private:
  119. explicit SHA(JS::Realm& realm)
  120. : AlgorithmMethods(realm)
  121. {
  122. }
  123. };
  124. }