9#include "PdfReference.h"
12#ifndef PODOFO_CRYPT_CTX
13#define PODOFO_CRYPT_CTX void
103class PdfEncryptContext;
155 static std::unique_ptr<PdfEncrypt> CreateFromObject(
const PdfObject&
obj);
244 bool IsOwnerPasswordSet()
const;
253 bool IsPrintAllowed()
const;
262 bool IsEditAllowed()
const;
271 bool IsCopyAllowed()
const;
280 bool IsEditNotesAllowed()
const;
289 bool IsFillAndSignAllowed()
const;
298 bool IsAccessibilityAllowed()
const;
307 bool IsDocAssemblyAllowed()
const;
316 bool IsHighPrintAllowed()
const;
337 unsigned GetKeyLengthBytes()
const;
360 inline bool IsMetadataEncrypted()
const {
return m_EncryptMetadata; }
362 inline bool IsParsed()
const {
return m_IsParsed; }
365 inline const unsigned char* GetUValueRaw()
const {
return m_uValue; }
367 inline const unsigned char* GetOValueRaw()
const {
return m_oValue; }
369 inline const std::string& GetUserPassword()
const {
return m_userPass; }
371 inline const std::string& GetOwnerPassword()
const {
return m_ownerPass; }
373 int64_t GetPValueForSerialization()
const;
379 void InitFromValues(PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
380 PdfPermissions pValue,
const bufferview& uValue,
const bufferview& oValue,
381 bool encryptedMetadata);
387 void InitFromScratch(
const std::string_view& userPassword,
const std::string_view& ownerPassword,
388 PdfEncryptionAlgorithm algorithm, PdfKeyLength keyLength,
unsigned char revision,
389 PdfPermissions pValue,
bool encryptedMetadata);
391 virtual void Decrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
392 const PdfReference& objref,
char* outStr,
size_t& outLen)
const = 0;
394 virtual void Encrypt(
const char* inStr,
size_t inLen, PdfEncryptContext& context,
395 const PdfReference& objref,
char* outStr,
size_t outLen)
const = 0;
397 virtual PdfAuthResult Authenticate(
const std::string_view& password,
const std::string_view& documentId,
398 PODOFO_CRYPT_CTX* ctx,
unsigned char encryptionKey[32])
const = 0;
400 virtual void GenerateEncryptionKey(
401 const std::string_view& documentId, PdfAuthResult authResult, PODOFO_CRYPT_CTX* ctx,
402 unsigned char uValue[48],
unsigned char oValue[48],
unsigned char encryptionKey[32]) = 0;
405 bool CheckKey(
const unsigned char key1[32],
const unsigned char key2[32])
const;
407 enum class PdfRC4Revision : uint8_t
413 enum class PdfAESV3Revision : uint8_t
422 PdfEncrypt(
const PdfEncrypt& rhs) =
default;
424 PdfEncrypt& operator=(PdfEncrypt& rhs) =
delete;
427 static std::unique_ptr<PdfEncrypt> CreateFromEncrypt(
const PdfEncrypt& rhs);
429 void clearSensitiveInfo();
433 unsigned char m_rValue;
436 unsigned char m_uValue[48];
437 unsigned char m_oValue[48];
438 unsigned char m_uValueSize;
439 unsigned char m_oValueSize;
440 bool m_EncryptMetadata;
443 std::string m_userPass;
444 std::string m_ownerPass;
448class PODOFO_API PdfEncryptContext final
450 friend class PdfEncrypt;
451 friend class PdfEncryptRC4;
452 friend class PdfEncryptAESV2;
453 friend class PdfEncryptAESV3;
458 ~PdfEncryptContext();
460 PdfEncryptContext(
const PdfEncryptContext&);
462 PdfEncryptContext& operator=(
const PdfEncryptContext&);
465 inline PdfAuthResult GetAuthResult() {
return m_AuthResult; }
467 inline const std::string GetDocumentId() {
return m_documentId; }
469 bool IsAuthenticated()
const;
472 inline const unsigned char* GetEncryptionKey()
const {
return m_encryptionKey; }
474 PODOFO_CRYPT_CTX* GetCryptCtx();
476 template <
typename T>
479 if (m_customCtx ==
nullptr)
481 m_customCtx = ::operator
new(
sizeof(T));
482 m_customCtxSize =
sizeof(T);
485 return *(T*)m_customCtx;
489 unsigned char m_encryptionKey[32];
490 std::string m_documentId;
492 PODOFO_CRYPT_CTX* m_cryptCtx;
494 size_t m_customCtxSize;
524 static void ComputeOwnerKey(
const unsigned char userPad[32],
const unsigned char ownerPad[32],
528 static void PadPassword(
const std::string_view&
password,
unsigned char pswd[32]);
531 static void ComputeEncryptionKey(
const std::string_view&
documentID,
585 void generateInitialVector(
const std::string_view&
documentId,
unsigned char iv[])
const;
641 static void generateInitialVector(
unsigned char iv[]);
645 static void preprocessPassword(
const std::string_view&
password,
unsigned char*
outBuf,
unsigned&
len);
660 static void computeOwnerKey(
const unsigned char*
userpswd,
unsigned len,
unsigned revision,
665 unsigned char m_ueValue[32];
666 unsigned char m_oeValue[32];
667 unsigned char m_permsValue[16];
715 static unsigned normalizeKeyLength(
unsigned keyLength);
An interface for writing blocks of data to a data source.
Definition OutputStream.h:16
The PDF dictionary data type of PoDoFo (inherits from PdfDataContainer, the base class for such repre...
Definition PdfDictionary.h:80
A class that is used to encrypt a PDF file (AES-128)
Definition PdfEncrypt.h:553
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1385
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:1403
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:1394
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1326
A class that is used to encrypt a PDF file (AES-256)
Definition PdfEncrypt.h:595
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:1925
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1916
void CreateEncryptionDictionary(PdfDictionary &dictionary) const override
Fill all keys into a encryption dictionary.
Definition PdfEncrypt.cpp:1630
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:1932
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1836
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:506
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:894
void CreateEncryptionDictionary(PdfDictionary &dictionary) const override
Fill all keys into a encryption dictionary.
Definition PdfEncrypt.cpp:965
A class that is used to encrypt a PDF file (RC4 40-bit and 128-bit)
Definition PdfEncrypt.h:677
size_t CalculateStreamOffset() const override
Calculate stream offset.
Definition PdfEncrypt.cpp:1080
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:1196
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:1106
size_t CalculateStreamLength(size_t length) const override
Calculate stream size.
Definition PdfEncrypt.cpp:1085
A bundle of the encrypt object together a context.
Definition PdfEncryptSession.h:15
A class that is used to encrypt a PDF file and set document permissions on the PDF file.
Definition PdfEncrypt.h:116
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:358
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:354
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:350
PdfEncryptionAlgorithm GetEncryptAlgorithm() const
Get the encryption algorithm of this object.
Definition PdfEncrypt.h:233
This class represents a PDF indirect Object in memory.
Definition PdfObject.h:33
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:22
A string that can be written to a PDF document.
Definition PdfString.h:22
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:35
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfEncryptionAlgorithm
The encryption algorithm.
Definition PdfEncrypt.h:86
@ 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:16
PdfPermissions
Set user permissions/restrictions on a document.
Definition PdfEncrypt.h:62
@ 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:39
PdfAuthResult
Definition PdfEncrypt.h:96
@ User
Success authenticating a user for this PDF.
@ Owner
Success authenticating the owner for this PDF.
@ Failed
Failed to authenticate to this PDF.