Crypto.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2022, stelar7 <dudedbz@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Random.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibJS/Runtime/TypedArray.h>
  10. #include <LibWeb/Bindings/CryptoPrototype.h>
  11. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  12. #include <LibWeb/Bindings/Intrinsics.h>
  13. #include <LibWeb/Crypto/Crypto.h>
  14. #include <LibWeb/Crypto/SubtleCrypto.h>
  15. #include <LibWeb/WebIDL/Buffers.h>
  16. namespace Web::Crypto {
  17. JS_DEFINE_ALLOCATOR(Crypto);
  18. JS::NonnullGCPtr<Crypto> Crypto::create(JS::Realm& realm)
  19. {
  20. return realm.create<Crypto>(realm);
  21. }
  22. Crypto::Crypto(JS::Realm& realm)
  23. : PlatformObject(realm)
  24. {
  25. }
  26. Crypto::~Crypto() = default;
  27. void Crypto::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(Crypto);
  31. m_subtle = SubtleCrypto::create(realm);
  32. }
  33. JS::NonnullGCPtr<SubtleCrypto> Crypto::subtle() const
  34. {
  35. return *m_subtle;
  36. }
  37. // https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues
  38. WebIDL::ExceptionOr<JS::Handle<WebIDL::ArrayBufferView>> Crypto::get_random_values(JS::Handle<WebIDL::ArrayBufferView> array) const
  39. {
  40. // 1. If array is not an Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array, then throw a TypeMismatchError and terminate the algorithm.
  41. if (!array->is_typed_array_base())
  42. return WebIDL::TypeMismatchError::create(realm(), "array must be one of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array"_string);
  43. auto const& typed_array = *array->bufferable_object().get<JS::NonnullGCPtr<JS::TypedArrayBase>>();
  44. if (!typed_array.element_name().is_one_of("Int8Array", "Uint8Array", "Uint8ClampedArray", "Int16Array", "Uint16Array", "Int32Array", "Uint32Array", "BigInt64Array", "BigUint64Array"))
  45. return WebIDL::TypeMismatchError::create(realm(), "array must be one of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array"_string);
  46. auto typed_array_record = JS::make_typed_array_with_buffer_witness_record(typed_array, JS::ArrayBuffer::Order::SeqCst);
  47. // IMPLEMENTATION DEFINED: If the viewed array buffer is out-of-bounds, throw a InvalidStateError and terminate the algorithm.
  48. if (JS::is_typed_array_out_of_bounds(typed_array_record))
  49. return WebIDL::InvalidStateError::create(realm(), MUST(String::formatted(JS::ErrorType::BufferOutOfBounds.message(), "TypedArray"sv)));
  50. // 2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm.
  51. if (JS::typed_array_byte_length(typed_array_record) > 65536)
  52. return WebIDL::QuotaExceededError::create(realm(), "array's byteLength may not be greater than 65536"_string);
  53. // FIXME: Handle SharedArrayBuffers
  54. // 3. Overwrite all elements of array with cryptographically strong random values of the appropriate type.
  55. fill_with_random(array->viewed_array_buffer()->buffer());
  56. // 4. Return array.
  57. return array;
  58. }
  59. // https://w3c.github.io/webcrypto/#dfn-Crypto-method-randomUUID
  60. WebIDL::ExceptionOr<String> Crypto::random_uuid() const
  61. {
  62. auto& vm = realm().vm();
  63. return TRY_OR_THROW_OOM(vm, generate_random_uuid());
  64. }
  65. void Crypto::visit_edges(Cell::Visitor& visitor)
  66. {
  67. Base::visit_edges(visitor);
  68. visitor.visit(m_subtle);
  69. }
  70. // https://w3c.github.io/webcrypto/#dfn-generate-a-random-uuid
  71. ErrorOr<String> generate_random_uuid()
  72. {
  73. // 1. Let bytes be a byte sequence of length 16.
  74. u8 bytes[16];
  75. // 2. Fill bytes with cryptographically secure random bytes.
  76. fill_with_random(bytes);
  77. // 3. Set the 4 most significant bits of bytes[6], which represent the UUID version, to 0100.
  78. bytes[6] &= ~(1 << 7);
  79. bytes[6] |= 1 << 6;
  80. bytes[6] &= ~(1 << 5);
  81. bytes[6] &= ~(1 << 4);
  82. // 4. Set the 2 most significant bits of bytes[8], which represent the UUID variant, to 10.
  83. bytes[8] |= 1 << 7;
  84. bytes[8] &= ~(1 << 6);
  85. /* 5. Return the string concatenation of
  86. «
  87. hexadecimal representation of bytes[0],
  88. hexadecimal representation of bytes[1],
  89. hexadecimal representation of bytes[2],
  90. hexadecimal representation of bytes[3],
  91. "-",
  92. hexadecimal representation of bytes[4],
  93. hexadecimal representation of bytes[5],
  94. "-",
  95. hexadecimal representation of bytes[6],
  96. hexadecimal representation of bytes[7],
  97. "-",
  98. hexadecimal representation of bytes[8],
  99. hexadecimal representation of bytes[9],
  100. "-",
  101. hexadecimal representation of bytes[10],
  102. hexadecimal representation of bytes[11],
  103. hexadecimal representation of bytes[12],
  104. hexadecimal representation of bytes[13],
  105. hexadecimal representation of bytes[14],
  106. hexadecimal representation of bytes[15]
  107. ».
  108. */
  109. StringBuilder builder;
  110. TRY(builder.try_appendff("{:02x}{:02x}{:02x}{:02x}-", bytes[0], bytes[1], bytes[2], bytes[3]));
  111. TRY(builder.try_appendff("{:02x}{:02x}-", bytes[4], bytes[5]));
  112. TRY(builder.try_appendff("{:02x}{:02x}-", bytes[6], bytes[7]));
  113. TRY(builder.try_appendff("{:02x}{:02x}-", bytes[8], bytes[9]));
  114. TRY(builder.try_appendff("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]));
  115. return builder.to_string();
  116. }
  117. }