11#include "PdfReference.h"
14#ifndef PODOFO_CRYPT_CTX
15#define PODOFO_CRYPT_CTX void
105class PdfEncryptContext;
157 static std::unique_ptr<PdfEncrypt> CreateFromObject(
const PdfObject&
obj);
246 bool IsOwnerPasswordSet()
const;
255 bool IsPrintAllowed()
const;
264 bool IsEditAllowed()
const;
273 bool IsCopyAllowed()
const;
282 bool IsEditNotesAllowed()
const;
291 bool IsFillAndSignAllowed()
const;
300 bool IsAccessibilityAllowed()
const;
309 bool IsDocAssemblyAllowed()
const;
318 bool IsHighPrintAllowed()
const;
339 unsigned GetKeyLengthBytes()
const;
362 inline bool IsMetadataEncrypted()
const {
return m_EncryptMetadata; }
364 inline bool IsParsed()
const {
return m_IsParsed; }
367 inline const unsigned char* GetUValueRaw()
const {
return m_uValue; }
369 inline const unsigned char* GetOValueRaw()
const {
return m_oValue; }
371 inline const std::string& GetUserPassword()
const {
return m_userPass; }
373 inline const std::string& GetOwnerPassword()
const {
return m_ownerPass; }
375 int64_t GetPValueForSerialization()
const;
381 void InitFromValues(PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
382 PdfPermissions pValue,
const bufferview& uValue,
const bufferview& oValue,
383 bool encryptedMetadata);
389 void InitFromScratch(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
390 PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
391 PdfPermissions pValue,
bool encryptedMetadata);
393 virtual void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
394 const PdfReference& objref,
char* outStr,
size_t& outLen)
const = 0;
396 virtual void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
397 const PdfReference& objref,
char* outStr,
size_t outLen)
const = 0;
399 virtual PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
400 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const = 0;
402 virtual void GenerateEncryptionKey(
403 const std::string_view& documentId, PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
404 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32]) = 0;
407 bool CheckKey(
const unsigned char key1[32],
const unsigned char key2[32])
const;
409 enum class PdfRC4Revision : uint8_t
415 enum class PdfAESV3Revision : uint8_t
424 PdfEncrypt(
const PdfEncrypt& rhs) =
default;
426 PdfEncrypt& operator=(PdfEncrypt& rhs) =
delete;
429 static std::unique_ptr<PdfEncrypt> CreateFromEncrypt(
const PdfEncrypt& rhs);
431 void clearSensitiveInfo();
435 unsigned char m_rValue;
438 unsigned char m_uValue[48];
439 unsigned char m_oValue[48];
440 unsigned char m_uValueSize;
441 unsigned char m_oValueSize;
442 bool m_EncryptMetadata;
445 std::string m_userPass;
446 std::string m_ownerPass;
450class PODOFO_API PdfEncryptContext final
452 friend class PdfEncrypt;
453 friend class PdfEncryptRC4;
454 friend class PdfEncryptAESV2;
455 friend class PdfEncryptAESV3;
460 ~PdfEncryptContext();
462 PdfEncryptContext(
const PdfEncryptContext&);
464 PdfEncryptContext& operator=(
const PdfEncryptContext&);
467 inline PdfAuthResult GetAuthResult() {
return m_AuthResult; }
469 inline const std::string GetDocumentId() {
return m_documentId; }
471 bool IsAuthenticated()
const;
474 inline const unsigned char* GetEncryptionKey()
const {
return m_encryptionKey; }
476 PODOFO_CRYPT_CTX* GetCryptCtx();
478 template <
typename T>
481 if (m_customCtx ==
nullptr)
483 m_customCtx = ::operator
new(
sizeof(T));
484 m_customCtxSize =
sizeof(T);
487 return *(T*)m_customCtx;
491 unsigned char m_encryptionKey[32];
492 std::string m_documentId;
494 PODOFO_CRYPT_CTX* m_cryptCtx;
496 size_t m_customCtxSize;
526 static void ComputeOwnerKey(
const unsigned char userPad[32],
const unsigned char ownerPad[32],
530 static void PadPassword(
const std::string_view&
password,
unsigned char pswd[32]);
533 static void ComputeEncryptionKey(
const std::string_view&
documentID,
587 void generateInitialVector(
const std::string_view&
documentId,
unsigned char iv[])
const;
643 static void generateInitialVector(
unsigned char iv[]);
647 static void preprocessPassword(
const std::string_view&
password,
unsigned char*
outBuf,
unsigned&
len);
662 static void computeOwnerKey(
const unsigned char*
userpswd,
unsigned len,
unsigned revision,
667 unsigned char m_ueValue[32];
668 unsigned char m_oeValue[32];
669 unsigned char m_permsValue[16];
717 static unsigned normalizeKeyLength(
unsigned keyLength);
An interface for writing blocks of data to a data source.
Definition OutputStream.h:18
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:82
A class that is used to encrypt a PDF file (AES-128)
Definition PdfEncrypt.h:555
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1380
std::unique_ptr< OutputStream > CreateEncryptionOutputStream(OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const override
Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncr...
Definition PdfEncrypt.cpp:1398
std::unique_ptr< InputStream > CreateEncryptionInputStream(InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const override
Create an InputStream that decrypts all data read from it using the current settings of the PdfEncryp...
Definition PdfEncrypt.cpp:1389
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1321
A class that is used to encrypt a PDF file (AES-256)
Definition PdfEncrypt.h:597
std::unique_ptr< InputStream > CreateEncryptionInputStream(InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const override
Create an InputStream that decrypts all data read from it using the current settings of the PdfEncryp...
Definition PdfEncrypt.cpp:1896
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1887
void CreateEncryptionDictionary(PdfDictionary &dictionary) const override
Fill all keys into a encryption dictionary.
Definition PdfEncrypt.cpp:1623
std::unique_ptr< OutputStream > CreateEncryptionOutputStream(OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const override
Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncr...
Definition PdfEncrypt.cpp:1903
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1807
A pure virtual class that is used to encrypt a PDF file (RC4, AES-128) This class is the base for cla...
Definition PdfEncrypt.h:508
void CreateObjKey(unsigned char objkey[16], unsigned &pnKeyLen, const unsigned char m_encryptionKey[32], const PdfReference &objref) const
Create the encryption key for the current object.
Definition PdfEncrypt.cpp:889
void CreateEncryptionDictionary(PdfDictionary &dictionary) const override
Fill all keys into a encryption dictionary.
Definition PdfEncrypt.cpp:960
A class that is used to encrypt a PDF file (RC4 40-bit and 128-bit)
Definition PdfEncrypt.h:679
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1075
std::unique_ptr< OutputStream > CreateEncryptionOutputStream(OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const override
Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncr...
Definition PdfEncrypt.cpp:1191
std::unique_ptr< InputStream > CreateEncryptionInputStream(InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const override
Create an InputStream that decrypts all data read from it using the current settings of the PdfEncryp...
Definition PdfEncrypt.cpp:1101
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1080
A bundle of the encrypt object together a context.
Definition PdfEncryptSession.h:18
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition PdfEncrypt.h:118
virtual size_t CalculateStreamLength(size_t length) const =0
Calculate stream size.
unsigned GetRevision() const
Get the revision number of the encryption method.
Definition PdfEncrypt.h:360
virtual size_t CalculateStreamOffset() const =0
Calculate stream offset.
virtual std::unique_ptr< OutputStream > CreateEncryptionOutputStream(OutputStream &outputStream, PdfEncryptContext &context, const PdfReference &objref) const =0
Create an OutputStream that encrypts all data written to it using the current settings of the PdfEncr...
void Authenticate(const std::string_view &password, const PdfString &documentId, PdfEncryptContext &context) const
Tries to authenticate a user using either the user or owner password.
virtual std::unique_ptr< InputStream > CreateEncryptionInputStream(InputStream &inputStream, size_t inputLen, PdfEncryptContext &context, const PdfReference &objref) const =0
Create an InputStream that decrypts all data read from it using the current settings of the PdfEncryp...
PdfPermissions GetPValue() const
Get the P object value (protection)
Definition PdfEncrypt.h:356
virtual void CreateEncryptionDictionary(PdfDictionary &dictionary) const =0
Fill all keys into a encryption dictionary.
PdfKeyLength GetKeyLength() const
Get the length of the encryption key in bits.
Definition PdfEncrypt.h:352
PdfEncryptionAlgorithm GetEncryptAlgorithm() const
Get the encryption algorithm of this object.
Definition PdfEncrypt.h:235
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:35
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition PdfReference.h:24
A string that can be written to a PDF document.
Definition PdfString.h:24
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:38
SPDX-FileCopyrightText: (C) 2022 Francesco Pretto ceztko@gmail.com SPDX-License-Identifier: LGPL-2....
Definition basetypes.h:16
PdfEncryptionAlgorithm
The encryption algorithm.
Definition PdfEncrypt.h:88
@ AESV3R5
AES encryption with a 256 bit key (PDF1.7 extension 3, deprecated in PDF 2.0)
@ AESV3R6
AES encryption with a 256 bit key, Revision 6 (PDF1.7 extension 8, PDF 2.0)
@ AESV2
AES encryption with a 128 bit key (PDF1.6)
@ RC4V1
RC4 Version 1 encryption using a 40bit key.
@ RC4V2
RC4 Version 2 encryption using a key with 40-128bit.
@ None
Do not add a default appearrance.
@ Create
Create a new file or truncate existing one for writing/reading.
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:19
PdfPermissions
Set user permissions/restrictions on a document.
Definition PdfEncrypt.h:64
@ FillAndSign
Fill in existing form or signature fields.
@ HighPrint
Print a high resolution version of the document.
@ Copy
Allow text and graphic extraction.
@ DocAssembly
Assemble the document: insert, create, rotate delete pages or add bookmarks.
@ Edit
Allow modifying the document besides annotations, form fields or changing pages.
@ EditNotes
Add or modify text annotations or form fields (if PdfPermissions::Edit is set also allow to create in...
@ Accessible
Extract text and graphics to support user with disabilities.
PdfKeyLength
A enum specifying a valid keylength for a PDF encryption key.
Definition PdfEncrypt.h:41
PdfAuthResult
Definition PdfEncrypt.h:98
@ User
Success authenticating a user for this PDF.
@ Owner
Success authenticating the owner for this PDF.
@ Failed
Failed to authenticate to this PDF.