9#include "PdfReference.h"
12#ifndef PODOFO_CRYPT_CTX
13#define PODOFO_CRYPT_CTX void
99class PdfEncryptContext;
148 static std::unique_ptr<PdfEncrypt> CreateFromObject(
const PdfObject&
obj);
230 bool IsOwnerPasswordSet()
const;
238 bool IsPrintAllowed()
const;
246 bool IsEditAllowed()
const;
254 bool IsCopyAllowed()
const;
262 bool IsEditNotesAllowed()
const;
270 bool IsFillAndSignAllowed()
const;
278 bool IsAccessibilityAllowed()
const;
286 bool IsDocAssemblyAllowed()
const;
294 bool IsHighPrintAllowed()
const;
311 unsigned GetKeyLengthBytes()
const;
329 inline bool IsMetadataEncrypted()
const {
return m_EncryptMetadata; }
331 inline bool IsParsed()
const {
return m_IsParsed; }
334 inline const unsigned char* GetUValueRaw()
const {
return m_uValue; }
336 inline const unsigned char* GetOValueRaw()
const {
return m_oValue; }
338 inline const std::string& GetUserPassword()
const {
return m_userPass; }
340 inline const std::string& GetOwnerPassword()
const {
return m_ownerPass; }
342 int64_t GetPValueForSerialization()
const;
346 void InitFromValues(PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
347 PdfPermissions pValue,
const bufferview& uValue,
const bufferview& oValue,
348 bool encryptedMetadata);
352 void InitFromScratch(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
353 PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
354 PdfPermissions pValue,
bool encryptedMetadata);
356 virtual void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
357 const PdfReference& objref,
char* outStr,
size_t& outLen)
const = 0;
359 virtual void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
360 const PdfReference& objref,
char* outStr,
size_t outLen)
const = 0;
362 virtual PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
363 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const = 0;
365 virtual void GenerateEncryptionKey(
366 const std::string_view& documentId, PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
367 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32]) = 0;
370 bool CheckKey(
const unsigned char key1[32],
const unsigned char key2[32])
const;
372 enum class PdfRC4Revision : uint8_t
378 enum class PdfAESV3Revision : uint8_t
387 PdfEncrypt(
const PdfEncrypt& rhs) =
default;
389 PdfEncrypt& operator=(PdfEncrypt& rhs) =
delete;
392 static std::unique_ptr<PdfEncrypt> CreateFromEncrypt(
const PdfEncrypt& rhs);
394 void clearSensitiveInfo();
398 unsigned char m_rValue;
401 unsigned char m_uValue[48];
402 unsigned char m_oValue[48];
403 unsigned char m_uValueSize;
404 unsigned char m_oValueSize;
405 bool m_EncryptMetadata;
408 std::string m_userPass;
409 std::string m_ownerPass;
413class PODOFO_API PdfEncryptContext final
415 friend class PdfEncrypt;
416 friend class PdfEncryptRC4;
417 friend class PdfEncryptAESV2;
418 friend class PdfEncryptAESV3;
423 ~PdfEncryptContext();
425 PdfEncryptContext(
const PdfEncryptContext&);
427 PdfEncryptContext& operator=(
const PdfEncryptContext&);
430 inline PdfAuthResult GetAuthResult() {
return m_AuthResult; }
432 inline const std::string GetDocumentId() {
return m_documentId; }
434 bool IsAuthenticated()
const;
437 inline const unsigned char* GetEncryptionKey()
const {
return m_encryptionKey; }
439 PODOFO_CRYPT_CTX* GetCryptCtx();
441 template <
typename T>
444 if (m_customCtx ==
nullptr)
446 m_customCtx = ::operator
new(
sizeof(T));
447 m_customCtxSize =
sizeof(T);
450 return *(T*)m_customCtx;
454 unsigned char m_encryptionKey[32];
455 std::string m_documentId;
457 PODOFO_CRYPT_CTX* m_cryptCtx;
459 size_t m_customCtxSize;
488 static void ComputeOwnerKey(
const unsigned char userPad[32],
const unsigned char ownerPad[32],
492 static void PadPassword(
const std::string_view&
password,
unsigned char pswd[32]);
495 static void ComputeEncryptionKey(
const std::string_view&
documentID,
547 void generateInitialVector(
const std::string_view&
documentId,
unsigned char iv[])
const;
602 static void generateInitialVector(
unsigned char iv[]);
606 static void preprocessPassword(
const std::string_view&
password,
unsigned char*
outBuf,
unsigned&
len);
621 static void computeOwnerKey(
const unsigned char*
userpswd,
unsigned len,
unsigned revision,
626 unsigned char m_ueValue[32];
627 unsigned char m_oeValue[32];
628 unsigned char m_permsValue[16];
675 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:515
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:556
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:1920
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1911
void CreateEncryptionDictionary(PdfDictionary &dictionary) const override
Fill all keys into a encryption dictionary.
Definition PdfEncrypt.cpp:1625
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:1927
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1831
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:470
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:637
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:13
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition PdfEncrypt.h:111
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:327
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:324
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:321
PdfEncryptionAlgorithm GetEncryptAlgorithm() const
Get the encryption algorithm of this object.
Definition PdfEncrypt.h:220
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.