summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF/Encryption.h
blob: d740202afa6f9267def35420758b78ba1fd34861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Span.h>
#include <LibPDF/ObjectDerivatives.h>

namespace PDF {

class SecurityHandler : public RefCounted<SecurityHandler> {
public:
    static PDFErrorOr<NonnullRefPtr<SecurityHandler>> create(Document*, NonnullRefPtr<DictObject> encryption_dict);

    virtual ~SecurityHandler() = default;

    virtual bool try_provide_user_password(StringView password) = 0;
    virtual bool has_user_password() const = 0;

    virtual void encrypt(NonnullRefPtr<Object>, Reference reference) const = 0;
    virtual void decrypt(NonnullRefPtr<Object>, Reference reference) const = 0;
};

class StandardSecurityHandler : public SecurityHandler {
public:
    static PDFErrorOr<NonnullRefPtr<StandardSecurityHandler>> create(Document*, NonnullRefPtr<DictObject> encryption_dict);

    StandardSecurityHandler(Document*, size_t revision, DeprecatedString const& o_entry, DeprecatedString const& u_entry, u32 flags, bool encrypt_metadata, size_t length);

    ~StandardSecurityHandler() override = default;

    bool try_provide_user_password(StringView password_string) override;

    bool has_user_password() const override { return m_encryption_key.has_value(); }

protected:
    void encrypt(NonnullRefPtr<Object>, Reference reference) const override;
    void decrypt(NonnullRefPtr<Object>, Reference reference) const override;

private:
    template<bool is_revision_2>
    ByteBuffer compute_user_password_value(ByteBuffer password_string);

    ByteBuffer compute_encryption_key(ByteBuffer password_string);

    Document* m_document;
    size_t m_revision;
    Optional<ByteBuffer> m_encryption_key;
    DeprecatedString m_o_entry;
    DeprecatedString m_u_entry;
    u32 m_flags;
    bool m_encrypt_metadata;
    size_t m_length;
};

class RC4 {
public:
    RC4(ReadonlyBytes key);

    void generate_bytes(ByteBuffer&);
    ByteBuffer encrypt(ReadonlyBytes bytes);

private:
    Array<size_t, 256> m_bytes;
};

}