Encryption.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Span.h>
  8. #include <LibPDF/ObjectDerivatives.h>
  9. namespace Crypto::Cipher {
  10. enum class Intent;
  11. }
  12. namespace PDF {
  13. enum class CryptFilterMethod {
  14. None,
  15. V2, // RC4
  16. AESV2,
  17. AESV3,
  18. };
  19. class SecurityHandler : public RefCounted<SecurityHandler> {
  20. public:
  21. static PDFErrorOr<NonnullRefPtr<SecurityHandler>> create(Document*, NonnullRefPtr<DictObject> encryption_dict);
  22. virtual ~SecurityHandler() = default;
  23. virtual bool try_provide_user_password(StringView password) = 0;
  24. virtual bool has_user_password() const = 0;
  25. virtual void encrypt(NonnullRefPtr<Object>, Reference reference) const = 0;
  26. virtual void decrypt(NonnullRefPtr<Object>, Reference reference) const = 0;
  27. };
  28. class StandardSecurityHandler : public SecurityHandler {
  29. public:
  30. static PDFErrorOr<NonnullRefPtr<StandardSecurityHandler>> create(Document*, NonnullRefPtr<DictObject> encryption_dict);
  31. StandardSecurityHandler(Document*, size_t revision, ByteString const& o_entry, ByteString const& oe_entry, ByteString const& u_entry, ByteString const& ue_entry, ByteString const& perms, u32 flags, bool encrypt_metadata, size_t length, CryptFilterMethod method);
  32. ~StandardSecurityHandler() override = default;
  33. bool try_provide_user_password(StringView password_string) override;
  34. bool has_user_password() const override { return m_encryption_key.has_value(); }
  35. protected:
  36. void encrypt(NonnullRefPtr<Object>, Reference reference) const override;
  37. void decrypt(NonnullRefPtr<Object>, Reference reference) const override;
  38. private:
  39. void crypt(NonnullRefPtr<Object>, Reference reference, Crypto::Cipher::Intent) const;
  40. ByteBuffer compute_user_password_value_r2(ByteBuffer password_string);
  41. ByteBuffer compute_user_password_value_r3_to_r5(ByteBuffer password_string);
  42. bool authenticate_user_password_r2_to_r5(StringView password_string);
  43. bool authenticate_user_password_r6_and_later(StringView password_string);
  44. bool authenticate_owner_password_r6_and_later(StringView password_string);
  45. ByteBuffer compute_encryption_key_r2_to_r5(ByteBuffer password_string);
  46. bool compute_encryption_key_r6_and_later(ByteBuffer password_string);
  47. enum class HashKind {
  48. Owner,
  49. User,
  50. };
  51. ByteBuffer computing_a_hash_r6_and_later(ByteBuffer input, StringView input_password, HashKind);
  52. Document* m_document;
  53. size_t m_revision;
  54. Optional<ByteBuffer> m_encryption_key;
  55. ByteString m_o_entry;
  56. ByteString m_oe_entry;
  57. ByteString m_u_entry;
  58. ByteString m_ue_entry;
  59. ByteString m_perms_entry;
  60. u32 m_flags;
  61. bool m_encrypt_metadata;
  62. size_t m_length;
  63. CryptFilterMethod m_method;
  64. };
  65. class RC4 {
  66. public:
  67. RC4(ReadonlyBytes key);
  68. void generate_bytes(ByteBuffer&);
  69. ByteBuffer encrypt(ReadonlyBytes bytes);
  70. private:
  71. Array<size_t, 256> m_bytes;
  72. };
  73. }