CryptoAlgorithms.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>)
  87. {
  88. return WebIDL::NotSupportedError::create(m_realm, "exportKey is not supported"_fly_string);
  89. }
  90. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }
  91. protected:
  92. explicit AlgorithmMethods(JS::Realm& realm)
  93. : m_realm(realm)
  94. {
  95. }
  96. JS::Realm& m_realm;
  97. };
  98. class RSAOAEP : public AlgorithmMethods {
  99. public:
  100. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  101. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  102. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new RSAOAEP(realm)); }
  103. private:
  104. explicit RSAOAEP(JS::Realm& realm)
  105. : AlgorithmMethods(realm)
  106. {
  107. }
  108. };
  109. class PBKDF2 : public AlgorithmMethods {
  110. public:
  111. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  112. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new PBKDF2(realm)); }
  113. private:
  114. explicit PBKDF2(JS::Realm& realm)
  115. : AlgorithmMethods(realm)
  116. {
  117. }
  118. };
  119. class SHA : public AlgorithmMethods {
  120. public:
  121. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&) override;
  122. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new SHA(realm)); }
  123. private:
  124. explicit SHA(JS::Realm& realm)
  125. : AlgorithmMethods(realm)
  126. {
  127. }
  128. };
  129. ErrorOr<String> base64_url_uint_encode(::Crypto::UnsignedBigInteger);
  130. }