9#include "PdfReference.h"
12#ifndef PODOFO_CRYPT_CTX
13#define PODOFO_CRYPT_CTX void
101class PdfEncryptContext;
150 static std::unique_ptr<PdfEncrypt> CreateFromObject(
const PdfObject&
obj);
232 bool IsOwnerPasswordSet()
const;
240 bool IsPrintAllowed()
const;
248 bool IsEditAllowed()
const;
256 bool IsCopyAllowed()
const;
264 bool IsEditNotesAllowed()
const;
272 bool IsFillAndSignAllowed()
const;
280 bool IsAccessibilityAllowed()
const;
288 bool IsDocAssemblyAllowed()
const;
296 bool IsHighPrintAllowed()
const;
313 unsigned GetKeyLengthBytes()
const;
331 inline bool IsMetadataEncrypted()
const {
return m_EncryptMetadata; }
333 inline bool IsParsed()
const {
return m_IsParsed; }
336 inline const unsigned char* GetUValueRaw()
const {
return m_uValue; }
338 inline const unsigned char* GetOValueRaw()
const {
return m_oValue; }
340 inline const std::string& GetUserPassword()
const {
return m_userPass; }
342 inline const std::string& GetOwnerPassword()
const {
return m_ownerPass; }
344 int64_t GetPValueForSerialization()
const;
348 void InitFromValues(PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
349 PdfPermissions pValue,
const bufferview& uValue,
const bufferview& oValue,
350 bool encryptedMetadata);
354 void InitFromScratch(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
355 PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
356 PdfPermissions pValue,
bool encryptedMetadata);
358 virtual void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
359 const PdfReference& objref,
char* outStr,
size_t& outLen)
const = 0;
361 virtual void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
362 const PdfReference& objref,
char* outStr,
size_t outLen)
const = 0;
364 virtual PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
365 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const = 0;
367 virtual void GenerateEncryptionKey(
368 const std::string_view& documentId, PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
369 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32]) = 0;
372 bool CheckKey(
const unsigned char key1[32],
const unsigned char key2[32])
const;
374 enum class PdfRC4Revision : uint8_t
380 enum class PdfAESV3Revision : uint8_t
389 PdfEncrypt(
const PdfEncrypt& rhs) =
default;
391 PdfEncrypt& operator=(PdfEncrypt& rhs) =
delete;
394 static std::unique_ptr<PdfEncrypt> CreateFromEncrypt(
const PdfEncrypt& rhs);
396 void clearSensitiveInfo();
400 unsigned char m_rValue;
403 unsigned char m_uValue[48];
404 unsigned char m_oValue[48];
405 unsigned char m_uValueSize;
406 unsigned char m_oValueSize;
407 bool m_EncryptMetadata;
410 std::string m_userPass;
411 std::string m_ownerPass;
415class PODOFO_API PdfEncryptContext final
417 friend class PdfEncrypt;
418 friend class PdfEncryptRC4;
419 friend class PdfEncryptAESV2;
420 friend class PdfEncryptAESV3;
425 ~PdfEncryptContext();
427 PdfEncryptContext(
const PdfEncryptContext&);
429 PdfEncryptContext& operator=(
const PdfEncryptContext&);
432 inline PdfAuthResult GetAuthResult() {
return m_AuthResult; }
434 inline const std::string GetDocumentId() {
return m_documentId; }
436 bool IsAuthenticated()
const;
439 inline const unsigned char* GetEncryptionKey()
const {
return m_encryptionKey; }
441 PODOFO_CRYPT_CTX* GetCryptCtx();
443 template <
typename T>
446 if (m_customCtx ==
nullptr)
448 m_customCtx = ::operator
new(
sizeof(T));
449 m_customCtxSize =
sizeof(T);
452 return *(T*)m_customCtx;
456 unsigned char m_encryptionKey[32];
457 std::string m_documentId;
459 PODOFO_CRYPT_CTX* m_cryptCtx;
461 size_t m_customCtxSize;
490 static void ComputeOwnerKey(
const unsigned char userPad[32],
const unsigned char ownerPad[32],
494 static void PadPassword(
const std::string_view&
password,
unsigned char pswd[32]);
497 static void ComputeEncryptionKey(
const std::string_view&
documentID,
549 void generateInitialVector(
const std::string_view&
documentId,
unsigned char iv[])
const;
604 static void generateInitialVector(
unsigned char iv[]);
608 static void preprocessPassword(
const std::string_view&
password,
unsigned char*
outBuf,
unsigned&
len);
623 static void computeOwnerKey(
const unsigned char*
userpswd,
unsigned len,
unsigned revision,
628 unsigned char m_ueValue[32];
629 unsigned char m_oeValue[32];
630 unsigned char m_permsValue[16];
677 static unsigned normalizeKeyLength(
unsigned keyLength);
An interface for writing blocks of data to a data source.
Definition OutputStream.h:15
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:77
A class that is used to encrypt a PDF file (AES-128)
Definition PdfEncrypt.h:517
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1377
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:1395
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:1386
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1318
A class that is used to encrypt a PDF file (AES-256)
Definition PdfEncrypt.h:558
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:1917
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1908
void CreateEncryptionDictionary(PdfDictionary &dictionary) const override
Fill all keys into a encryption dictionary.
Definition PdfEncrypt.cpp:1622
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:1924
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1828
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:472
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:886
void CreateEncryptionDictionary(PdfDictionary &dictionary) const override
Fill all keys into a encryption dictionary.
Definition PdfEncrypt.cpp:957
A class that is used to encrypt a PDF file (RC4 40-bit and 128-bit)
Definition PdfEncrypt.h:639
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1072
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:1188
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:1098
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1077
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition PdfEncrypt.h:113
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:329
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:326
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:323
PdfEncryptionAlgorithm GetEncryptAlgorithm() const
Get the encryption algorithm of this object.
Definition PdfEncrypt.h:222
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:31
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:20
A string that can be written to a PDF document.
Definition PdfString.h:21
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfEncryptionAlgorithm
The encryption algorithm.
Definition PdfEncrypt.h:82
@ 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 appearance.
@ Create
Create a new file or truncate existing one for writing/reading.
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:15
PdfPermissions
Set user permissions/restrictions on a document.
Definition PdfEncrypt.h:60
@ 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:38
PdfAuthResult
Definition PdfEncrypt.h:92
@ User
Success authenticating a user for this PDF.
@ Owner
Success authenticating the owner for this PDF.
@ Failed
Failed to authenticate to this PDF.