CryptoAlgorithms.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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, ByteBuffer salt, u32 iterations, HashAlgorithmIdentifier hash)
  35. : AlgorithmParams(move(name))
  36. , salt(move(salt))
  37. , iterations(iterations)
  38. , hash(move(hash))
  39. {
  40. }
  41. ByteBuffer 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. // https://w3c.github.io/webcrypto/#dfn-RsaHashedImportParams
  72. struct RsaHashedImportParams : public AlgorithmParams {
  73. virtual ~RsaHashedImportParams() override;
  74. RsaHashedImportParams(String name, HashAlgorithmIdentifier hash)
  75. : AlgorithmParams(move(name))
  76. , hash(move(hash))
  77. {
  78. }
  79. HashAlgorithmIdentifier hash;
  80. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  81. };
  82. // https://w3c.github.io/webcrypto/#dfn-RsaOaepParams
  83. struct RsaOaepParams : public AlgorithmParams {
  84. virtual ~RsaOaepParams() override;
  85. RsaOaepParams(String name, ByteBuffer label)
  86. : AlgorithmParams(move(name))
  87. , label(move(label))
  88. {
  89. }
  90. ByteBuffer label;
  91. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  92. };
  93. class AlgorithmMethods {
  94. public:
  95. virtual ~AlgorithmMethods();
  96. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  97. {
  98. return WebIDL::NotSupportedError::create(m_realm, "encrypt is not supported"_fly_string);
  99. }
  100. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  101. {
  102. return WebIDL::NotSupportedError::create(m_realm, "decrypt is not supported"_fly_string);
  103. }
  104. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&)
  105. {
  106. return WebIDL::NotSupportedError::create(m_realm, "digest is not supported"_fly_string);
  107. }
  108. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&)
  109. {
  110. return WebIDL::NotSupportedError::create(m_realm, "importKey is not supported"_fly_string);
  111. }
  112. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&)
  113. {
  114. return WebIDL::NotSupportedError::create(m_realm, "generateKey is not supported"_fly_string);
  115. }
  116. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>)
  117. {
  118. return WebIDL::NotSupportedError::create(m_realm, "exportKey is not supported"_fly_string);
  119. }
  120. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }
  121. protected:
  122. explicit AlgorithmMethods(JS::Realm& realm)
  123. : m_realm(realm)
  124. {
  125. }
  126. JS::Realm& m_realm;
  127. };
  128. class RSAOAEP : public AlgorithmMethods {
  129. public:
  130. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  131. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  132. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  133. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  134. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new RSAOAEP(realm)); }
  135. private:
  136. explicit RSAOAEP(JS::Realm& realm)
  137. : AlgorithmMethods(realm)
  138. {
  139. }
  140. };
  141. class PBKDF2 : public AlgorithmMethods {
  142. public:
  143. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  144. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new PBKDF2(realm)); }
  145. private:
  146. explicit PBKDF2(JS::Realm& realm)
  147. : AlgorithmMethods(realm)
  148. {
  149. }
  150. };
  151. class SHA : public AlgorithmMethods {
  152. public:
  153. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&) override;
  154. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new SHA(realm)); }
  155. private:
  156. explicit SHA(JS::Realm& realm)
  157. : AlgorithmMethods(realm)
  158. {
  159. }
  160. };
  161. ErrorOr<String> base64_url_uint_encode(::Crypto::UnsignedBigInteger);
  162. WebIDL::ExceptionOr<::Crypto::UnsignedBigInteger> base64_url_uint_decode(JS::Realm&, String const& base64_url_string);
  163. }